42
42
#define LONG_TIMEOUT ((int)0x8000)
43
43
44
44
static const PinMap PinMap_I2C_SDA [] = {
45
+ {PB_7 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF , GPIO_OType_OD , GPIO_PuPd_UP , GPIO_AF_1 )},
45
46
{PB_9 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF , GPIO_OType_OD , GPIO_PuPd_UP , GPIO_AF_1 )},
47
+ {PB_11 , I2C_2 , STM_PIN_DATA (GPIO_Mode_AF , GPIO_OType_OD , GPIO_PuPd_UP , GPIO_AF_1 )},
46
48
{NC , NC , 0 }
47
49
};
48
50
49
51
static const PinMap PinMap_I2C_SCL [] = {
52
+ {PB_6 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF , GPIO_OType_OD , GPIO_PuPd_UP , GPIO_AF_1 )},
50
53
{PB_8 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF , GPIO_OType_OD , GPIO_PuPd_UP , GPIO_AF_1 )},
54
+ {PB_10 , I2C_2 , STM_PIN_DATA (GPIO_Mode_AF , GPIO_OType_OD , GPIO_PuPd_UP , GPIO_AF_1 )},
51
55
{NC , NC , 0 }
52
56
};
53
57
@@ -66,9 +70,9 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
66
70
if (obj -> i2c == I2C_1 ) {
67
71
RCC_APB1PeriphClockCmd (RCC_APB1Periph_I2C1 , ENABLE );
68
72
}
69
- // if (obj->i2c == I2C_2) {
70
- // RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
71
- // }
73
+ if (obj -> i2c == I2C_2 ) {
74
+ RCC_APB1PeriphClockCmd (RCC_APB1Periph_I2C2 , ENABLE );
75
+ }
72
76
73
77
// Configure I2C pins
74
78
pinmap_pinout (scl , PinMap_I2C_SCL );
@@ -196,39 +200,23 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
196
200
197
201
if (length == 0 ) return 0 ;
198
202
199
- // TODO: the stop is always sent even with I2C_SoftEnd_Mode. To be corrected.
200
-
201
203
// Configure slave address, nbytes, reload, end mode and start or stop generation
202
- // if (stop) {
204
+ if (stop ) {
203
205
I2C_TransferHandling (i2c , address , length , I2C_AutoEnd_Mode , I2C_Generate_Start_Write );
204
- //}
205
- //else {
206
- // I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
207
- //}
206
+
207
+ }
208
+ else {
209
+ I2C_TransferHandling (i2c , address , length , I2C_SoftEnd_Mode , I2C_Generate_Start_Write );
210
+ }
208
211
209
212
// Write all bytes
210
213
for (count = 0 ; count < length ; count ++ ) {
211
214
if (i2c_byte_write (obj , data [count ]) != 1 ) {
212
- i2c_stop (obj );
215
+ if (! stop ) i2c_stop (obj );
213
216
return 0 ;
214
217
}
215
218
}
216
219
217
- /*
218
- if (stop) {
219
- // Wait until STOPF flag is set
220
- timeout = LONG_TIMEOUT;
221
- while (I2C_GetFlagStatus(i2c, I2C_ISR_STOPF) == RESET) {
222
- timeout--;
223
- if (timeout == 0) {
224
- return 0;
225
- }
226
- }
227
- // Clear STOPF flag
228
- I2C_ClearFlag(i2c, I2C_ICR_STOPCF);
229
- }
230
- */
231
-
232
220
return count ;
233
221
}
234
222
@@ -274,10 +262,10 @@ void i2c_reset(i2c_t *obj) {
274
262
RCC_APB1PeriphResetCmd (RCC_APB1Periph_I2C1 , ENABLE );
275
263
RCC_APB1PeriphResetCmd (RCC_APB1Periph_I2C1 , DISABLE );
276
264
}
277
- // if (obj->i2c == I2C_2) {
278
- // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
279
- // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
280
- // }
265
+ if (obj -> i2c == I2C_2 ) {
266
+ RCC_APB1PeriphResetCmd (RCC_APB1Periph_I2C2 , ENABLE );
267
+ RCC_APB1PeriphResetCmd (RCC_APB1Periph_I2C2 , DISABLE );
268
+ }
281
269
}
282
270
283
271
#if DEVICE_I2CSLAVE
@@ -286,14 +274,17 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
286
274
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
287
275
uint16_t tmpreg ;
288
276
277
+ // reset own address enable
278
+ i2c -> OAR1 &=~ I2C_OAR1_OA1EN ;
279
+
289
280
// Get the old register value
290
281
tmpreg = i2c -> OAR1 ;
291
282
// Reset address bits
292
283
tmpreg &= 0xFC00 ;
293
284
// Set new address
294
285
tmpreg |= (uint16_t )((uint16_t )address & (uint16_t )0x00FE ); // 7-bits
295
286
// Store the new register value
296
- i2c -> OAR1 = tmpreg ;
287
+ i2c -> OAR1 = tmpreg | I2C_OAR1_OA1EN ;
297
288
}
298
289
299
290
void i2c_slave_mode (i2c_t * obj , int enable_slave ) {
@@ -307,8 +298,27 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
307
298
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
308
299
309
300
int i2c_slave_receive (i2c_t * obj ) {
310
- // TO BE DONE
311
- return (0 );
301
+ I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
302
+ int event = 0 ;
303
+ int timeout ;
304
+
305
+ // Wait until address match
306
+ timeout = FLAG_TIMEOUT ;
307
+ while (I2C_GetFlagStatus (i2c , I2C_ISR_ADDR ) == RESET ) {
308
+ timeout -- ;
309
+ if (timeout == 0 ) {
310
+ return 0 ;
311
+ }
312
+ }
313
+ // Check direction
314
+ if (i2c -> ISR & I2C_ISR_DIR ) {
315
+ event = ReadAddressed ;
316
+ }
317
+ else event = WriteAddressed ;
318
+ // Clear adress match flag to generate an acknowledge
319
+ i2c -> ICR |= I2C_ICR_ADDRCF ;
320
+
321
+ return event ;
312
322
}
313
323
314
324
int i2c_slave_read (i2c_t * obj , char * data , int length ) {
0 commit comments