42
42
#define LONG_TIMEOUT ((int)0x8000)
43
43
44
44
static const PinMap PinMap_I2C_SDA [] = {
45
- {PB_9 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF_OD , 8 )}, // GPIO_Remap_I2C1
45
+ {PB_9 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF , GPIO_OType_OD , GPIO_PuPd_UP , GPIO_AF_I2C1 )},
46
46
{NC , NC , 0 }
47
47
};
48
48
49
49
static const PinMap PinMap_I2C_SCL [] = {
50
- {PB_8 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF_OD , 8 )}, // GPIO_Remap_I2C1
50
+ {PB_8 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF , GPIO_OType_OD , GPIO_PuPd_UP , GPIO_AF_I2C1 )},
51
51
{NC , NC , 0 }
52
52
};
53
53
@@ -71,10 +71,10 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
71
71
}
72
72
73
73
// Configure I2C pins
74
- pinmap_pinout (sda , PinMap_I2C_SDA );
75
74
pinmap_pinout (scl , PinMap_I2C_SCL );
76
- pin_mode (sda , OpenDrain );
77
75
pin_mode (scl , OpenDrain );
76
+ pinmap_pinout (sda , PinMap_I2C_SDA );
77
+ pin_mode (sda , OpenDrain );
78
78
79
79
// Reset to clear pending flags if any
80
80
i2c_reset (obj );
@@ -88,15 +88,18 @@ void i2c_frequency(i2c_t *obj, int hz) {
88
88
I2C_InitTypeDef I2C_InitStructure ;
89
89
90
90
if ((hz != 0 ) && (hz <= 400000 )) {
91
+ I2C_DeInit (i2c );
92
+
91
93
// I2C configuration
92
94
I2C_InitStructure .I2C_Mode = I2C_Mode_I2C ;
93
95
I2C_InitStructure .I2C_DutyCycle = I2C_DutyCycle_2 ;
94
96
I2C_InitStructure .I2C_OwnAddress1 = 0 ;
95
97
I2C_InitStructure .I2C_Ack = I2C_Ack_Enable ;
96
98
I2C_InitStructure .I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit ;
97
99
I2C_InitStructure .I2C_ClockSpeed = hz ;
100
+ I2C_Init (i2c , & I2C_InitStructure );
101
+
98
102
I2C_Cmd (i2c , ENABLE );
99
- I2C_Init (i2c , & I2C_InitStructure );
100
103
}
101
104
}
102
105
@@ -113,9 +116,10 @@ inline int i2c_start(i2c_t *obj) {
113
116
timeout = FLAG_TIMEOUT ;
114
117
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
115
118
while (I2C_GetFlagStatus (i2c , I2C_FLAG_SB ) == RESET ) {
116
- if ((timeout -- ) == 0 ) {
117
- return 1 ;
118
- }
119
+ timeout -- ;
120
+ if (timeout == 0 ) {
121
+ return 1 ;
122
+ }
119
123
}
120
124
121
125
return 0 ;
@@ -141,7 +145,8 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
141
145
// Wait until the bus is not busy anymore
142
146
timeout = LONG_TIMEOUT;
143
147
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
144
- if ((timeout--) == 0) {
148
+ timeout--;
149
+ if (timeout == 0) {
145
150
return 0;
146
151
}
147
152
}
@@ -155,9 +160,10 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
155
160
// Wait address is acknowledged
156
161
timeout = FLAG_TIMEOUT ;
157
162
while (I2C_CheckEvent (i2c , I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ) == ERROR ) {
158
- if ((timeout -- ) == 0 ) {
159
- return 0 ;
160
- }
163
+ timeout -- ;
164
+ if (timeout == 0 ) {
165
+ return 0 ;
166
+ }
161
167
}
162
168
163
169
// Read all bytes except last one
@@ -188,7 +194,8 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
188
194
// Wait until the bus is not busy anymore
189
195
timeout = LONG_TIMEOUT;
190
196
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
191
- if ((timeout--) == 0) {
197
+ timeout--;
198
+ if (timeout == 0) {
192
199
return 0;
193
200
}
194
201
}
@@ -202,9 +209,10 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
202
209
// Wait address is acknowledged
203
210
timeout = FLAG_TIMEOUT ;
204
211
while (I2C_CheckEvent (i2c , I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) == ERROR ) {
205
- if ((timeout -- ) == 0 ) {
206
- return 0 ;
207
- }
212
+ timeout -- ;
213
+ if (timeout == 0 ) {
214
+ return 0 ;
215
+ }
208
216
}
209
217
210
218
for (count = 0 ; count < length ; count ++ ) {
@@ -238,9 +246,10 @@ int i2c_byte_read(i2c_t *obj, int last) {
238
246
// Wait until the byte is received
239
247
timeout = FLAG_TIMEOUT ;
240
248
while (I2C_GetFlagStatus (i2c , I2C_FLAG_RXNE ) == RESET ) {
241
- if ((timeout -- ) == 0 ) {
242
- return 0 ;
243
- }
249
+ timeout -- ;
250
+ if (timeout == 0 ) {
251
+ return 0 ;
252
+ }
244
253
}
245
254
246
255
data = I2C_ReceiveData (i2c );
@@ -259,7 +268,8 @@ int i2c_byte_write(i2c_t *obj, int data) {
259
268
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) {
260
269
while ((I2C_GetFlagStatus (i2c , I2C_FLAG_TXE ) == RESET ) &&
261
270
(I2C_GetFlagStatus (i2c , I2C_FLAG_BTF ) == RESET )) {
262
- if ((timeout -- ) == 0 ) {
271
+ timeout -- ;
272
+ if (timeout == 0 ) {
263
273
return 0 ;
264
274
}
265
275
}
0 commit comments