@@ -93,6 +93,9 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
93
93
94
94
// I2C configuration
95
95
i2c_frequency (obj , 100000 ); // 100 kHz per default
96
+
97
+ // I2C master by default
98
+ obj -> slave = 0 ;
96
99
}
97
100
98
101
void i2c_frequency (i2c_t * obj , int hz ) {
@@ -109,6 +112,10 @@ void i2c_frequency(i2c_t *obj, int hz) {
109
112
I2cHandle .Init .OwnAddress1 = 0 ;
110
113
I2cHandle .Init .OwnAddress2 = 0 ;
111
114
HAL_I2C_Init (& I2cHandle );
115
+ if (obj -> slave ) {
116
+ /* Enable Address Acknowledge */
117
+ I2cHandle .Instance -> CR1 |= I2C_CR1_ACK ;
118
+ }
112
119
}
113
120
else {
114
121
error ("I2C error: frequency setting failed (max 400kHz)." );
@@ -233,7 +240,7 @@ void i2c_reset(i2c_t *obj) {
233
240
234
241
void i2c_slave_address (i2c_t * obj , int idx , uint32_t address , uint32_t mask ) {
235
242
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
236
- uint16_t tmpreg ;
243
+ uint16_t tmpreg = 0 ;
237
244
238
245
// Get the old register value
239
246
tmpreg = i2c -> OAR1 ;
@@ -246,7 +253,12 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
246
253
}
247
254
248
255
void i2c_slave_mode (i2c_t * obj , int enable_slave ) {
249
- // Nothing to do
256
+ I2cHandle .Instance = (I2C_TypeDef * )(obj -> i2c );
257
+ if (enable_slave ) {
258
+ obj -> slave = 1 ;
259
+ /* Enable Address Acknowledge */
260
+ I2cHandle .Instance -> CR1 |= I2C_CR1_ACK ;
261
+ }
250
262
}
251
263
252
264
// See I2CSlave.h
@@ -256,34 +268,140 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
256
268
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
257
269
258
270
int i2c_slave_receive (i2c_t * obj ) {
259
- // TO BE DONE
260
- return (0 );
271
+ int retValue = NoData ;
272
+
273
+ if (__HAL_I2C_GET_FLAG (& I2cHandle , I2C_FLAG_BUSY ) == 1 ) {
274
+ if (__HAL_I2C_GET_FLAG (& I2cHandle , I2C_FLAG_ADDR ) == 1 ) {
275
+ if (__HAL_I2C_GET_FLAG (& I2cHandle , I2C_FLAG_TRA ) == 1 )
276
+ retValue = ReadAddressed ;
277
+ else
278
+ retValue = WriteAddressed ;
279
+
280
+ __HAL_I2C_CLEAR_FLAG (& I2cHandle , I2C_FLAG_ADDR );
281
+ }
282
+ }
283
+
284
+ return (retValue );
261
285
}
262
286
263
287
int i2c_slave_read (i2c_t * obj , char * data , int length ) {
288
+ uint32_t Timeout ;
289
+ int size = 0 ;
264
290
if (length == 0 ) return 0 ;
265
291
266
292
I2cHandle .Instance = (I2C_TypeDef * )(obj -> i2c );
267
293
268
- // Reception process with 5 seconds timeout
269
- if (HAL_I2C_Slave_Receive (& I2cHandle , (uint8_t * )data , length , 5000 ) != HAL_OK ) {
270
- return 0 ; // Error
294
+ while (length > 0 )
295
+ {
296
+ /* Wait until RXNE flag is set */
297
+ // Wait until the byte is received
298
+ Timeout = FLAG_TIMEOUT ;
299
+ while (__HAL_I2C_GET_FLAG (& I2cHandle , I2C_FLAG_RXNE ) == RESET ) {
300
+ Timeout -- ;
301
+ if (Timeout == 0 ) {
302
+ return 0 ;
303
+ }
304
+ }
305
+
306
+ /* Read data from DR */
307
+ (* data ++ ) = I2cHandle .Instance -> DR ;
308
+ length -- ;
309
+ size ++ ;
310
+
311
+ if ((__HAL_I2C_GET_FLAG (& I2cHandle , I2C_FLAG_BTF ) == SET ) && (length != 0 )){
312
+ /* Read data from DR */
313
+ (* data ++ ) = I2cHandle .Instance -> DR ;
314
+ length -- ;
315
+ size ++ ;
316
+ }
271
317
}
272
318
273
- return length ;
319
+ /* Wait until STOP flag is set */
320
+ Timeout = FLAG_TIMEOUT ;
321
+ while (__HAL_I2C_GET_FLAG (& I2cHandle , I2C_FLAG_STOPF ) == RESET ) {
322
+ Timeout -- ;
323
+ if (Timeout == 0 ) {
324
+ return 0 ;
325
+ }
326
+ }
327
+
328
+ /* Clear STOP flag */
329
+ __HAL_I2C_CLEAR_STOPFLAG (& I2cHandle );
330
+
331
+ /* Wait until BUSY flag is reset */
332
+ Timeout = FLAG_TIMEOUT ;
333
+ while (__HAL_I2C_GET_FLAG (& I2cHandle , I2C_FLAG_BUSY ) == SET ) {
334
+ Timeout -- ;
335
+ if (Timeout == 0 ) {
336
+ return 0 ;
337
+ }
338
+ }
339
+
340
+ return size ;
274
341
}
275
342
276
343
int i2c_slave_write (i2c_t * obj , const char * data , int length ) {
344
+ uint32_t Timeout ;
345
+ int size = 0 ;
277
346
if (length == 0 ) return 0 ;
278
347
279
348
I2cHandle .Instance = (I2C_TypeDef * )(obj -> i2c );
280
349
281
- // Transmission process with 5 seconds timeout
282
- if (HAL_I2C_Slave_Transmit (& I2cHandle , (uint8_t * )data , length , 5000 ) != HAL_OK ) {
283
- return 0 ; // Error
350
+ while (length > 0 )
351
+ {
352
+ /* Wait until TXE flag is set */
353
+ Timeout = FLAG_TIMEOUT ;
354
+ while (__HAL_I2C_GET_FLAG (& I2cHandle , I2C_FLAG_TXE ) == RESET ) {
355
+ Timeout -- ;
356
+ if (Timeout == 0 ) {
357
+ return 0 ;
358
+ }
359
+ }
360
+
361
+
362
+ /* Write data to DR */
363
+ I2cHandle .Instance -> DR = (* data ++ );
364
+ length -- ;
365
+ size ++ ;
366
+
367
+ if ((__HAL_I2C_GET_FLAG (& I2cHandle , I2C_FLAG_BTF ) == SET ) && (length != 0 ))
368
+ {
369
+ /* Write data to DR */
370
+ I2cHandle .Instance -> DR = (* data ++ );
371
+ length -- ;
372
+ size ++ ;
373
+ }
284
374
}
375
+
376
+ /* Wait until AF flag is set */
377
+ Timeout = FLAG_TIMEOUT ;
378
+ while (__HAL_I2C_GET_FLAG (& I2cHandle , I2C_FLAG_AF ) == RESET ) {
379
+ Timeout -- ;
380
+ if (Timeout == 0 ) {
381
+ return 0 ;
382
+ }
383
+ }
285
384
286
- return length ;
385
+
386
+ /* Clear AF flag */
387
+ __HAL_I2C_CLEAR_FLAG (& I2cHandle , I2C_FLAG_AF );
388
+
389
+
390
+ /* Wait until BUSY flag is reset */
391
+ Timeout = FLAG_TIMEOUT ;
392
+ while (__HAL_I2C_GET_FLAG (& I2cHandle , I2C_FLAG_BUSY ) == SET ) {
393
+ Timeout -- ;
394
+ if (Timeout == 0 ) {
395
+ return 0 ;
396
+ }
397
+ }
398
+
399
+ I2cHandle .State = HAL_I2C_STATE_READY ;
400
+
401
+ /* Process Unlocked */
402
+ __HAL_UNLOCK (& I2cHandle );
403
+
404
+ return size ;
287
405
}
288
406
289
407
0 commit comments