37
37
#include "pinmap.h"
38
38
#include "PeripheralPins.h"
39
39
40
+ #ifndef DEBUG_STDIO
41
+ # define DEBUG_STDIO 0
42
+ #endif
43
+
44
+ #if DEBUG_STDIO
45
+ # include <stdio.h>
46
+ # define DEBUG_PRINTF (...) do { printf(__VA_ARGS__); } while(0)
47
+ #else
48
+ # define DEBUG_PRINTF (...) {}
49
+ #endif
50
+
40
51
/* Timeout values are based on core clock and I2C clock.
41
52
The BYTE_TIMEOUT is computed as twice the number of cycles it would
42
53
take to send 10 bits over I2C. Most Flags should take less than that.
@@ -378,11 +389,14 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
378
389
}
379
390
380
391
if ((timeout == 0 ) || (obj_s -> event != I2C_EVENT_TRANSFER_COMPLETE )) {
392
+ DEBUG_PRINTF (" TIMEOUT or error in i2c_read\r\n" );
381
393
/* re-init IP to try and get back in a working state */
382
394
i2c_init (obj , obj_s -> sda , obj_s -> scl );
383
395
} else {
384
396
count = length ;
385
397
}
398
+ } else {
399
+ DEBUG_PRINTF ("ERROR in i2c_read\r\n" );
386
400
}
387
401
388
402
return count ;
@@ -420,11 +434,14 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
420
434
}
421
435
422
436
if ((timeout == 0 ) || (obj_s -> event != I2C_EVENT_TRANSFER_COMPLETE )) {
437
+ DEBUG_PRINTF (" TIMEOUT or error in i2c_write\r\n" );
423
438
/* re-init IP to try and get back in a working state */
424
439
i2c_init (obj , obj_s -> sda , obj_s -> scl );
425
440
} else {
426
441
count = length ;
427
442
}
443
+ } else {
444
+ DEBUG_PRINTF ("ERROR in i2c_read\r\n" );
428
445
}
429
446
430
447
return count ;
@@ -623,8 +640,11 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
623
640
wait_us (1 );
624
641
}
625
642
626
- if (timeout != 0 )
627
- count = length ;
643
+ if (timeout != 0 ) {
644
+ count = length ;
645
+ } else {
646
+ DEBUG_PRINTF ("TIMEOUT or error in i2c_slave_read\r\n" );
647
+ }
628
648
}
629
649
630
650
return count ;
@@ -646,8 +666,11 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
646
666
wait_us (1 );
647
667
}
648
668
649
- if (timeout != 0 )
650
- count = length ;
669
+ if (timeout != 0 ) {
670
+ count = length ;
671
+ } else {
672
+ DEBUG_PRINTF ("TIMEOUT or error in i2c_slave_write\r\n" );
673
+ }
651
674
}
652
675
653
676
return count ;
@@ -694,6 +717,8 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
694
717
i2c_t * obj = get_i2c_obj (hi2c );
695
718
struct i2c_s * obj_s = I2C_S (obj );
696
719
720
+ DEBUG_PRINTF ("HAL_I2C_ErrorCallback:%d, index=%d\r\n" , (int ) hi2c -> ErrorCode , obj_s -> index );
721
+
697
722
/* re-init IP to try and get back in a working state */
698
723
i2c_init (obj , obj_s -> sda , obj_s -> scl );
699
724
0 commit comments