1 | #include "stm32f10x_conf.h"
|
2 |
|
3 | I2C_TypeDef * I2C_Module;
|
4 | volatile uint8_t deviceAddress;
|
5 | volatile uint8_t i2cBusyFlag;
|
6 |
|
7 | void I2C1_EV_IRQHandler(void);
|
8 | void I2C1_ER_IRQHandler(void);
|
9 |
|
10 |
|
11 | #define I2C_OWN_ADRESS 44
|
12 |
|
13 | // default: PB6 - I2C1_SCL; PB7 - I2C1_SDA
|
14 |
|
15 | int main(void)
|
16 | {
|
17 | GPIO_InitTypeDef GPIO_InitStructure;
|
18 | NVIC_InitTypeDef NVIC_InitStructure;
|
19 | I2C_InitTypeDef I2C_InitStructure;
|
20 |
|
21 | SystemInit();
|
22 |
|
23 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
|
24 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
|
25 |
|
26 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
|
27 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
|
28 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
29 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
30 |
|
31 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
32 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
33 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
34 | GPIO_Init(GPIOC, &GPIO_InitStructure);
|
35 |
|
36 | NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
|
37 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
38 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
39 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
40 | NVIC_Init(&NVIC_InitStructure);
|
41 |
|
42 | NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;
|
43 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
44 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
45 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
46 | NVIC_Init(&NVIC_InitStructure);
|
47 |
|
48 | I2C_DeInit(I2C1);
|
49 |
|
50 | I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
|
51 | I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
52 | I2C_InitStructure.I2C_ClockSpeed = 100000;
|
53 | I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
|
54 | I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
|
55 | I2C_InitStructure.I2C_OwnAddress1 = I2C_OWN_ADRESS;
|
56 | I2C_Init(I2C1, &I2C_InitStructure);
|
57 |
|
58 | I2C_ITConfig(I2C1, I2C_IT_EVT, ENABLE);
|
59 | I2C_ITConfig(I2C1, I2C_IT_BUF, ENABLE);
|
60 | I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE);
|
61 |
|
62 | I2C_Cmd(I2C1, ENABLE);
|
63 |
|
64 | while(1)
|
65 | {
|
66 |
|
67 | }
|
68 | }
|
69 |
|
70 | void I2C3_EV_IRQHandler()
|
71 | {
|
72 | switch (I2C_GetLastEvent(I2C1))
|
73 | {
|
74 |
|
75 | case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
|
76 | GPIO_WriteBit(GPIOC, GPIO_Pin_9, Bit_SET);
|
77 | break;
|
78 |
|
79 | case I2C_EVENT_SLAVE_BYTE_RECEIVED:
|
80 | GPIO_WriteBit(GPIOC, GPIO_Pin_9, Bit_SET);
|
81 | break;
|
82 |
|
83 | case I2C_EVENT_SLAVE_STOP_DETECTED:
|
84 | GPIO_WriteBit(GPIOC, GPIO_Pin_9, Bit_SET);
|
85 | break;
|
86 | }
|
87 |
|
88 | I2C_CleanADDRandSTOPF();
|
89 | }
|
90 |
|
91 | inline void I2C_CleanADDRandSTOPF()
|
92 | {
|
93 | while ((I2C1->SR1 & I2C_SR1_ADDR) == I2C_SR1_ADDR)
|
94 | {
|
95 | volatile uint32_t temp;
|
96 | temp=I2C1->SR1;
|
97 | temp=I2C1->SR2;
|
98 | }
|
99 | while ((I2C1->SR1&I2C_SR1_STOPF) == I2C_SR1_STOPF)
|
100 | {
|
101 | volatile uint32_t temp;
|
102 | temp=I2C1->SR1;
|
103 | I2C1->CR1 |= 0x1;
|
104 | }
|
105 | }
|
106 |
|
107 | void I2C1_ER_IRQHandler(void){
|
108 |
|
109 | }
|