-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBSP_I2C.c
329 lines (292 loc) · 9.93 KB
/
BSP_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
/* Copyright (c) 2018-2022 UT Longhorn Racing Solar */
#include "BSP_I2C.h"
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_i2c.h"
#define TIMEOUT_THRESHOLD 1200000 // 15 ms delay threshold (3x the write time)
/**
* @brief Initializes the I2C port that interfaces with the EEPROM.
* @param None
* @return None
*/
void BSP_I2C_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct;
I2C_InitTypeDef I2C_InitStruct;
// Uses PA8 for SCL and PC9 for SDA
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C3, ENABLE);
// Initialize PA8 for SCL on I2C3
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // Watch out for Output Type. I2C is an open-drain comm protocol unlike SPI/UART
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// Initialize PC9 for SDA on I2C3
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; // Only need to change pin since the rest of the pin configurations are the same
GPIO_Init(GPIOC, &GPIO_InitStruct);
// Set which alternate function mode we want for these pins.
// We want to make these pins use I2C3.
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_I2C3); // PA8
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_I2C3); // PC9
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // regular mode
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle on clock
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // 7-bit addressing mode
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; // Enable ack to confirm if the slave device has received the data or not
I2C_InitStruct.I2C_ClockSpeed = 100000; // Generic speed
I2C_InitStruct.I2C_OwnAddress1 = 0x50 << 1; // Any address will do. I just chose the same address as the EEPROM.
// Note I left shifted the address. That's because the address is supposed
// to be 7-bits and the least significant bit indicates the R/W setting
I2C_Init(I2C3, &I2C_InitStruct);
I2C_Cmd(I2C3, ENABLE);
}
/**
* @brief Transmits data onto the I2C bus.
* @param deviceAddr : the device/IC that the data is intended for.
* @param regAddr : the register address to write to in the IC's memory.
* @param txData : the data array to be sent onto the bus.
* @param txLen : the length of the data array.
* @return error status, 0 if fail, 1 if success
*/
uint8_t BSP_I2C_Write(uint8_t deviceAddr, uint16_t regAddr, uint8_t *txData, uint32_t txLen) {
volatile int timeout_count = 0;
while(I2C_GetFlagStatus(I2C3, I2C_FLAG_BUSY)){
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
I2C_AcknowledgeConfig(I2C3, ENABLE);
// First write address that you want to read from
writePoll:
// Since no one is using the I2C bus, take control
I2C_GenerateSTART(I2C3, ENABLE);
// Wait until start edge event occurred
timeout_count = 0;
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_MODE_SELECT)) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
// Select device to talk to
I2C_Send7bitAddress(I2C3, deviceAddr, I2C_Direction_Transmitter); // Sets RW bit to 0
// THIS IS WHERE WE GOT STUCK
timeout_count = 0;
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) {
if(I2C3->SR1 & 0x0400) {
I2C3->SR1 &= ~0x0400;
I2C_GenerateSTOP(I2C3, ENABLE);
goto writePoll;
}
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
// Send start address (MSB first)
I2C_SendData(I2C3, (uint8_t)((regAddr & 0xFF00) >> 8));
// Wait until transmit event occurred.
timeout_count = 0;
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
// Send rest of start address (LSB)
I2C_SendData(I2C3, (uint8_t)(regAddr & 0x00FF));
// Wait until transmit event occurred.
timeout_count = 0;
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
// Wait until byte transmission finished
timeout_count = 0;
while(I2C_GetFlagStatus(I2C3, I2C_FLAG_BTF) == RESET) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
// Start writing data sequentially
while(txLen > 0){
// Wait until BTF flag is set
timeout_count = 0;
while(I2C_GetFlagStatus(I2C3, I2C_FLAG_BTF) == RESET);
I2C_SendData(I2C3, *txData);
txData++;
txLen--;
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
I2C_GenerateSTOP(I2C3, ENABLE);
return SUCCESS;
}
/**
* @brief Gets the data from a device through the I2C bus.
* @param deviceAddr : the device/IC that the data needs to be read from.
* @param regAddr : the register address to read from the IC's memory.
* @param rxData : the data array to store the data that is received.
* @param rxLen : the length of the data array.
* @return error status, 0 if fail, other if success
*/
uint8_t BSP_I2C_Read(uint8_t deviceAddr, uint16_t regAddr, uint8_t *rxData, uint32_t rxLen) {
volatile int timeout_count = 0;
while(I2C_GetFlagStatus(I2C3, I2C_FLAG_BUSY)){
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
I2C_AcknowledgeConfig(I2C3, ENABLE);
// First write address that you want to read from
readPoll:
// Since no one is using the I2C bus, take control
I2C_GenerateSTART(I2C3, ENABLE);
// Wait until start edge event occurred
timeout_count = 0;
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_MODE_SELECT)) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
// Select device to talk to
I2C_Send7bitAddress(I2C3, deviceAddr, I2C_Direction_Transmitter); // Sets RW bit to 0
// Wait until finished sending
timeout_count = 0;
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) {
if(I2C3->SR1 & 0x0400) {
I2C3->SR1 &= ~0x0400;
I2C_GenerateSTOP(I2C3, ENABLE);
goto readPoll;
}
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
// Send start address (MSB first)
I2C_SendData(I2C3, (uint8_t)((regAddr & 0xFF00) >> 8));
// Wait until transmit event occurred.
timeout_count = 0;
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
// Send rest of start address (LSB)
I2C_SendData(I2C3, (uint8_t)(regAddr & 0x00FF));
// Wait until transmit event occurred.
timeout_count = 0;
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
// Wait until byte transmission finished
timeout_count = 0;
while(I2C_GetFlagStatus(I2C3, I2C_FLAG_BTF) == RESET) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
// Now read data starting from address that was sent
// Since no one is using the I2C bus, take control
I2C_GenerateSTART(I2C3, ENABLE);
// Wait until start edge event occurred
timeout_count = 0;
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_MODE_SELECT)) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
// Select device to talk to
I2C_Send7bitAddress(I2C3, deviceAddr, I2C_Direction_Receiver); // Sets RW bit to 1
// Wait until finished sending
timeout_count = 0;
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
while(rxLen > 0){
if(rxLen > 1){
// Wait until byte transmission finished
timeout_count = 0;
while(I2C_GetFlagStatus(I2C3, I2C_FLAG_BTF) == RESET) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
*rxData = I2C_ReceiveData(I2C3);
rxData++;
rxLen--;
} else {
// Disable ack, since this is the last byte
I2C_AcknowledgeConfig(I2C3, DISABLE);
timeout_count = 0;
while(I2C_GetFlagStatus(I2C3, I2C_FLAG_RXNE) == RESET) {
// Assume running at 80 MHz
timeout_count++;
// Returns and breaks after timeout threshold
if(timeout_count > TIMEOUT_THRESHOLD) {
return ERROR;
}
}
*rxData = I2C_ReceiveData(I2C3);
rxData++;
rxLen = 0;
}
}
// Generate the stop
I2C_GenerateSTOP(I2C3, ENABLE);
// Return Success if all executed properly
return SUCCESS;
}