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 , GPIO_OType_OD , GPIO_PuPd_NOPULL , GPIO_AF_1 )},
45
+ {PB_9 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF , GPIO_OType_OD , GPIO_PuPd_UP , GPIO_AF_1 )},
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 , GPIO_OType_OD , GPIO_PuPd_NOPULL , GPIO_AF_1 )},
50
+ {PB_8 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF , GPIO_OType_OD , GPIO_PuPd_UP , GPIO_AF_1 )},
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 );
@@ -120,12 +120,15 @@ void i2c_frequency(i2c_t *obj, int hz) {
120
120
121
121
inline int i2c_start (i2c_t * obj ) {
122
122
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
123
- int timeout = LONG_TIMEOUT ;
123
+ int timeout ;
124
124
125
125
// Test BUSY Flag
126
- while (I2C_GetFlagStatus (i2c , I2C_ISR_BUSY ) != RESET )
127
- {
128
- if ((timeout -- ) == 0 ) return 0 ;
126
+ timeout = LONG_TIMEOUT ;
127
+ while (I2C_GetFlagStatus (i2c , I2C_ISR_BUSY ) != RESET ) {
128
+ timeout -- ;
129
+ if (timeout == 0 ) {
130
+ return 0 ;
131
+ }
129
132
}
130
133
131
134
I2C_GenerateSTART (i2c , ENABLE );
@@ -135,68 +138,47 @@ inline int i2c_start(i2c_t *obj) {
135
138
136
139
inline int i2c_stop (i2c_t * obj ) {
137
140
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
138
-
141
+
139
142
I2C_GenerateSTOP (i2c , ENABLE );
140
-
143
+
141
144
return 0 ;
142
145
}
143
146
144
147
int i2c_read (i2c_t * obj , int address , char * data , int length , int stop ) {
145
148
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
146
- int timeout ;
147
149
int count ;
148
150
int value ;
149
-
151
+
150
152
if (length == 0 ) return 0 ;
151
153
152
154
// Configure slave address, nbytes, reload, end mode and start or stop generation
153
- if (stop ) {
154
- I2C_TransferHandling (i2c , address , length , I2C_AutoEnd_Mode , I2C_Generate_Start_Read );
155
- }
156
- else {
157
- I2C_TransferHandling (i2c , address , length , I2C_Reload_Mode , I2C_Generate_Start_Read );
158
- }
155
+ I2C_TransferHandling (i2c , address , length , I2C_AutoEnd_Mode , I2C_Generate_Start_Read );
159
156
160
157
// Read all bytes
161
158
for (count = 0 ; count < length ; count ++ ) {
162
159
value = i2c_byte_read (obj , 0 );
163
160
data [count ] = (char )value ;
164
161
}
165
162
166
- if (stop ) {
167
- // Wait until STOPF flag is set
168
- timeout = LONG_TIMEOUT ;
169
- while (I2C_GetFlagStatus (i2c , I2C_ISR_STOPF ) == RESET )
170
- {
171
- if ((timeout -- ) == 0 ) return 0 ;
172
- }
173
-
174
- // Clear STOPF flag
175
- I2C_ClearFlag (i2c , I2C_ICR_STOPCF );
176
- }
177
-
178
163
return length ;
179
164
}
180
165
181
166
int i2c_write (i2c_t * obj , int address , const char * data , int length , int stop ) {
182
167
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
183
- int timeout ;
168
+ // int timeout;
184
169
int count ;
170
+
171
+ if (length == 0 ) return 0 ;
172
+
173
+ // TODO: the stop is always sent even with I2C_SoftEnd_Mode. To be corrected.
185
174
186
- // Test BUSY Flag
187
- //timeout = LONG_TIMEOUT;
188
- //while (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) != RESET)
189
- //{
190
- // if((timeout--) == 0) return 0;
191
- //}
192
-
193
175
// Configure slave address, nbytes, reload, end mode and start or stop generation
194
- if (stop ) {
176
+ // if (stop) {
195
177
I2C_TransferHandling (i2c , address , length , I2C_AutoEnd_Mode , I2C_Generate_Start_Write );
196
- }
197
- else {
198
- I2C_TransferHandling (i2c , address , length , I2C_Reload_Mode , I2C_Generate_Start_Write );
199
- }
178
+ // }
179
+ // else {
180
+ // I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode , I2C_Generate_Start_Write);
181
+ // }
200
182
201
183
// Write all bytes
202
184
for (count = 0 ; count < length ; count ++ ) {
@@ -206,29 +188,36 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
206
188
}
207
189
}
208
190
191
+ /*
209
192
if (stop) {
210
193
// Wait until STOPF flag is set
211
194
timeout = LONG_TIMEOUT;
212
- while (I2C_GetFlagStatus (i2c , I2C_ISR_STOPF ) == RESET )
213
- {
214
- if ((timeout -- ) == 0 ) return 0 ;
215
- }
216
-
195
+ while (I2C_GetFlagStatus(i2c, I2C_ISR_STOPF) == RESET) {
196
+ timeout--;
197
+ if (timeout == 0) {
198
+ return 0;
199
+ }
200
+ }
217
201
// Clear STOPF flag
218
202
I2C_ClearFlag(i2c, I2C_ICR_STOPCF);
219
203
}
204
+ */
220
205
221
206
return count ;
222
207
}
223
208
224
209
int i2c_byte_read (i2c_t * obj , int last ) {
225
210
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
226
211
uint8_t data ;
227
- int timeout = FLAG_TIMEOUT ;
212
+ int timeout ;
228
213
229
214
// Wait until the byte is received
215
+ timeout = FLAG_TIMEOUT ;
230
216
while (I2C_GetFlagStatus (i2c , I2C_ISR_RXNE ) == RESET ) {
231
- if ((timeout -- ) == 0 ) return 0 ;
217
+ timeout -- ;
218
+ if (timeout == 0 ) {
219
+ return 0 ;
220
+ }
232
221
}
233
222
234
223
data = I2C_ReceiveData (i2c );
@@ -238,21 +227,18 @@ int i2c_byte_read(i2c_t *obj, int last) {
238
227
239
228
int i2c_byte_write (i2c_t * obj , int data ) {
240
229
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
241
- int timeout = FLAG_TIMEOUT ;
230
+ int timeout ;
242
231
243
- // Wait until TXIS flag is set
244
- timeout = LONG_TIMEOUT ;
245
- while (I2C_GetFlagStatus (i2c , I2C_ISR_TXIS ) == RESET )
246
- {
247
- if ((timeout -- ) == 0 ) return 0 ;
232
+ // Wait until the previous byte is transmitted
233
+ timeout = FLAG_TIMEOUT ;
234
+ while (I2C_GetFlagStatus (i2c , I2C_ISR_TXIS ) == RESET ) {
235
+ timeout -- ;
236
+ if (timeout == 0 ) {
237
+ return 0 ;
238
+ }
248
239
}
249
-
240
+
250
241
I2C_SendData (i2c , (uint8_t )data );
251
-
252
- // Wait until the byte is transmitted
253
- //while (I2C_GetFlagStatus(i2c, I2C_ISR_TCR) == RESET) {
254
- // if ((timeout--) == 0) return 0;
255
- //}
256
242
257
243
return 1 ;
258
244
}
0 commit comments