@@ -118,7 +118,6 @@ inline int i2c_start(i2c_t *obj) {
118
118
119
119
// Wait the START condition has been correctly sent
120
120
timeout = FLAG_TIMEOUT ;
121
- //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
122
121
while (I2C_GetFlagStatus (i2c , I2C_FLAG_SB ) == RESET ) {
123
122
timeout -- ;
124
123
if (timeout == 0 ) {
@@ -145,17 +144,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
145
144
146
145
if (length == 0 ) return 0 ;
147
146
148
- /*
149
- // Wait until the bus is not busy anymore
150
- timeout = LONG_TIMEOUT;
151
- while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
152
- timeout--;
153
- if (timeout == 0) {
154
- return 0;
155
- }
156
- }
157
- */
158
-
159
147
i2c_start (obj );
160
148
161
149
// Send slave address for read
@@ -194,17 +182,6 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
194
182
int timeout ;
195
183
int count ;
196
184
197
- /*
198
- // Wait until the bus is not busy anymore
199
- timeout = LONG_TIMEOUT;
200
- while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
201
- timeout--;
202
- if (timeout == 0) {
203
- return 0;
204
- }
205
- }
206
- */
207
-
208
185
i2c_start (obj );
209
186
210
187
// Send slave address for write
@@ -269,7 +246,6 @@ int i2c_byte_write(i2c_t *obj, int data) {
269
246
270
247
// Wait until the byte is transmitted
271
248
timeout = FLAG_TIMEOUT ;
272
- //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) {
273
249
while ((I2C_GetFlagStatus (i2c , I2C_FLAG_TXE ) == RESET ) &&
274
250
(I2C_GetFlagStatus (i2c , I2C_FLAG_BTF ) == RESET )) {
275
251
timeout -- ;
@@ -319,8 +295,46 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
319
295
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
320
296
321
297
int i2c_slave_receive (i2c_t * obj ) {
322
- // TO BE DONE
323
- return (0 );
298
+ int retValue = NoData ;
299
+ uint32_t event ;
300
+ I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
301
+
302
+ event = I2C_GetLastEvent ( i2c );
303
+ if (event != 0 )
304
+ {
305
+ switch (event ){
306
+ case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED :
307
+ retValue = WriteAddressed ;
308
+ break ;
309
+ case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED :
310
+ retValue = ReadAddressed ;
311
+ break ;
312
+ case I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED :
313
+ retValue = WriteGeneral ;
314
+ break ;
315
+ default :
316
+ retValue = NoData ;
317
+ break ;
318
+ }
319
+
320
+ // clear ADDR
321
+ if ((retValue == WriteAddressed ) || (retValue == ReadAddressed )){
322
+ // read SR to clear ADDR flag
323
+ i2c -> SR1 ;
324
+ i2c -> SR2 ;
325
+ }
326
+ // clear stopf
327
+ if (I2C_GetFlagStatus (i2c , I2C_FLAG_STOPF ) == SET ) {
328
+ // read SR1 and write CR1 to clear STOP flag
329
+ i2c -> SR1 ;
330
+ I2C_Cmd (i2c , ENABLE );
331
+ }
332
+ // clear AF
333
+ if (I2C_GetFlagStatus (i2c , I2C_FLAG_AF ) == SET ) {
334
+ I2C_ClearFlag (i2c , I2C_FLAG_AF );
335
+ }
336
+ }
337
+ return (retValue );
324
338
}
325
339
326
340
int i2c_slave_read (i2c_t * obj , char * data , int length ) {
0 commit comments