-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTM32_I2C.c
348 lines (338 loc) · 6.24 KB
/
STM32_I2C.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "STM32_I2C.h"
#define I2C_ADDRESS(addr, mode) ((addr<<1)| mode)
#define NumberOfTicks 2000000
ErrorStatus STATE = SUCCESS;
void I2CInit()
{
int Ticks = NumberOfTicks;
//Turn on clocking I2C and GPIOB
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
//PB6 (SCL), PB7 (SDA) AF settings
GPIOB->CRL |= GPIO_CRL_MODE6|GPIO_CRL_MODE7;
GPIOB->CRL |= GPIO_CRL_CNF6|GPIO_CRL_CNF7;
//Reset I2C
I2C1->CR1|=I2C_CR1_SWRST;
I2C1->CR1^=I2C_CR1_SWRST;
//writing freq to CR2
I2C1->CR2 &= ~I2C_CR2_FREQ;
I2C1->CR2 |= 36; // 36 MHz of APB
//Event and buffer interrupt enable
//I2C1->CR2 |= I2C_CR2_ITEVTEN|I2C_CR2_ITBUFEN;
//Error interrupt enable
I2C1->CR2 |= I2C_CR2_ITERREN;
// FastMode, 400 kHz
I2C1->CCR |= I2C_CCR_FS;
//Clock control register
I2C1->CCR &= ~I2C_CCR_CCR;
I2C1->CCR |= 90;
//Speed of signal's rising
I2C1->TRISE = 15;
//Enable I2C block
I2C1->CR1 |= I2C_CR1_PE;
//NVIC_EnableIRQ(I2C1_ER_IRQn);
while (Ticks)
{
Ticks--;
}
STATE=SUCCESS;
return;
}
void I2CReset(void)
{
int Ticks = NumberOfTicks;
I2C1->CR1 |= I2C_CR1_SWRST;
while (Ticks)
{
Ticks--;
}
I2CInit();
return;
}
ErrorStatus I2CStart()
{
int Ticks = NumberOfTicks;
//Start
I2C1->CR1 |= I2C_CR1_START;
//checking START and sending address
while (!((I2C1->SR1)&I2C_SR1_SB))
{
if (STATE==ERROR)
{
return STATE;
}
Ticks--;
if (Ticks == 0)
{
return ERROR;
}
}
//Clear SB flag (+ followed writing in DR)
(void) I2C1->SR1;
return STATE;
}
void I2CStop()
{
//STOP
I2C1->CR1|=I2C_CR1_STOP;
return;
}
ErrorStatus I2CSendAddr(int Addr, I2C_MODE Mode)
{
int Ticks = NumberOfTicks;
//Send adress + mode
I2C1->DR = I2C_ADDRESS(Addr,Mode);
//Waiting for sending adress
while (!(I2C1->SR1 & I2C_SR1_ADDR))
{
if (STATE==ERROR)
{
return STATE;
}
Ticks--;
if (Ticks == 0)
{
return ERROR;
}
}
return STATE;
}
void I2CClearADDR()
{
//clear address flag
(void) I2C1->SR2;
}
void I2CSetPOS()
{
I2C1->CR1 |= I2C_CR1_POS;
}
void I2CUnSetPOS()
{
I2C1->CR1 &= ~I2C_CR1_POS;
}
void I2CSetAck()
{
I2C1->CR1 |= I2C_CR1_ACK;
}
void I2CUnSetAck()
{
I2C1->CR1 &= ~I2C_CR1_ACK;
}
ErrorStatus I2CWaitForByte()
{
int Ticks = NumberOfTicks;
//Waiting for RXNE flag to be set
while (!(I2C1->SR1 & I2C_SR1_RXNE))
{
if (STATE==ERROR)
{
return STATE;
}
Ticks--;
if (Ticks == 0)
{
return ERROR;
}
}
return STATE;
}
ErrorStatus I2CWaitForBTF()
{
int Ticks = NumberOfTicks;
//Waiting for BTF flag to be set
while (!(I2C1->SR1 & I2C_SR1_BTF))
{
if (STATE==ERROR)
{
return STATE;
}
Ticks--;
if (Ticks == 0)
{
return ERROR;
}
}
return STATE;
}
ErrorStatus I2CWaitBusyLine()
{
int Ticks = NumberOfTicks;
//Waiting for Bus to be free
while (I2C1->SR2 & I2C_SR2_BUSY)
{
if (STATE==ERROR)
{
return STATE;
}
Ticks--;
if (Ticks == 0)
{
return ERROR;
}
}
return STATE;
}
void I2CReadDR(int *byte)
{
*byte=I2C1->DR;
}
ErrorStatus I2CSendBytes(int* Bytes, int NumberOfSendBytes, int SlaveAddr)
{
if(I2CWaitBusyLine() == ERROR)
return ERROR;
if(I2CStart() == ERROR)
return ERROR;
if(I2CSendAddr(SlaveAddr,I2C_MODE_WRITE)== ERROR)
return ERROR;
I2CClearADDR();
while (NumberOfSendBytes)
{
//sending Byte
I2C1->DR = *Bytes;
//Waiting for sending Byte
if (I2CWaitForBTF() == ERROR)
return ERROR;
//Clear BTF flag
(void) I2C1->SR1;
Bytes++;
NumberOfSendBytes--;
}
I2CStop();
if(I2CWaitForStopToBeCleared() == ERROR)
return ERROR;
return SUCCESS;
}
void I2CSearch()
{
int Address = 0x7F;
while (Address!=0)
{
int Ticks = NumberOfTicks/1000;
I2CSetAck();
I2CWaitBusyLine();
I2CStart();
//Send adress + mode Read
I2C1->DR = I2C_ADDRESS(Address,I2C_MODE_WRITE);
//Waiting for sending adress
while ((!(I2C1->SR1 & I2C_SR1_ADDR))&&(Ticks!=0))
{
Ticks--;
}
I2CStop();
I2CWaitForStopToBeCleared();
if (Ticks!=0)
{
int device = Address;
}
Address--;
I2C1->SR1&=~I2C_SR1_AF;
//clear address flag
(void) I2C1->SR2;
}
}
ErrorStatus I2CWaitForStopToBeCleared()
{
int Ticks = NumberOfTicks;
while (I2C1->CR1 & I2C_CR1_STOP)
{
if (STATE == ERROR)
{
return STATE;
}
Ticks--;
if (Ticks == 0)
{
return ERROR;
}
}
return STATE;
}
ErrorStatus I2CReadBytes(int*PResults, int NumBytesToRead, int SlaveAddr)
{
if(I2CWaitBusyLine()== ERROR)
return ERROR;
if (NumBytesToRead == 1)
{
if(I2CStart() == ERROR)
return ERROR;
if(I2CSendAddr(SlaveAddr,I2C_MODE_READ)== ERROR)
return ERROR;
I2CUnSetAck();
__disable_irq();
I2CClearADDR();
I2CStop();
__enable_irq();
if(I2CWaitForByte()== ERROR)
return ERROR;
I2CReadDR(PResults);
if(I2CWaitForStopToBeCleared()== ERROR)
return ERROR;
I2CSetAck();
return SUCCESS;
}
else if (NumBytesToRead == 2)
{
I2CSetAck();
I2CSetPOS();
if(I2CStart() == ERROR)
return ERROR;
if(I2CSendAddr(SlaveAddr,I2C_MODE_READ)== ERROR)
return ERROR;
__disable_irq();
I2CClearADDR();
I2CUnSetAck();
__enable_irq();
if(I2CWaitForBTF()== ERROR)
return ERROR;
__disable_irq();
I2CStop();
I2CReadDR(PResults);
__enable_irq();
PResults++;
I2CReadDR(PResults);
if(I2CWaitForStopToBeCleared()== ERROR)
return ERROR;
I2CUnSetPOS();
I2CSetAck();
}
else
{
if(I2CStart() == ERROR)
return ERROR;
if(I2CSendAddr(SlaveAddr,I2C_MODE_READ)== ERROR)
return ERROR;
I2CClearADDR();
while (NumBytesToRead)
{
if (NumBytesToRead != 3)
{
if(I2CWaitForBTF()== ERROR)
return ERROR;
I2CReadDR(PResults);
PResults++;
NumBytesToRead--;
}
if (NumBytesToRead == 3)
{
if(I2CWaitForBTF()== ERROR)
return ERROR;
I2CUnSetAck();
__disable_irq();
I2CReadDR(PResults);
PResults++;
I2CStop();
I2CReadDR(PResults);
__enable_irq();
PResults++;
if(I2CWaitForByte()== ERROR)
return ERROR;
I2CReadDR(PResults);
NumBytesToRead = 0;
}
}
if(I2CWaitForStopToBeCleared()== ERROR)
return ERROR;
I2CSetAck();
}
return SUCCESS;
}