Skip to content

Commit b491165

Browse files
committed
[STM32F4] Add few prints for I2C debug
1 parent a65bacc commit b491165

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

targets/TARGET_STM/TARGET_STM32F4/i2c_api.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@
3737
#include "pinmap.h"
3838
#include "PeripheralPins.h"
3939

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+
4051
/* Timeout values are based on core clock and I2C clock.
4152
The BYTE_TIMEOUT is computed as twice the number of cycles it would
4253
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) {
378389
}
379390

380391
if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
392+
DEBUG_PRINTF(" TIMEOUT or error in i2c_read\r\n");
381393
/* re-init IP to try and get back in a working state */
382394
i2c_init(obj, obj_s->sda, obj_s->scl);
383395
} else {
384396
count = length;
385397
}
398+
} else {
399+
DEBUG_PRINTF("ERROR in i2c_read\r\n");
386400
}
387401

388402
return count;
@@ -420,11 +434,14 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
420434
}
421435

422436
if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
437+
DEBUG_PRINTF(" TIMEOUT or error in i2c_write\r\n");
423438
/* re-init IP to try and get back in a working state */
424439
i2c_init(obj, obj_s->sda, obj_s->scl);
425440
} else {
426441
count = length;
427442
}
443+
} else {
444+
DEBUG_PRINTF("ERROR in i2c_read\r\n");
428445
}
429446

430447
return count;
@@ -623,8 +640,11 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
623640
wait_us(1);
624641
}
625642

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+
}
628648
}
629649

630650
return count;
@@ -646,8 +666,11 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
646666
wait_us(1);
647667
}
648668

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+
}
651674
}
652675

653676
return count;
@@ -694,6 +717,8 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
694717
i2c_t *obj = get_i2c_obj(hi2c);
695718
struct i2c_s *obj_s = I2C_S(obj);
696719

720+
DEBUG_PRINTF("HAL_I2C_ErrorCallback:%d, index=%d\r\n", (int) hi2c->ErrorCode, obj_s->index);
721+
697722
/* re-init IP to try and get back in a working state */
698723
i2c_init(obj, obj_s->sda, obj_s->scl);
699724

0 commit comments

Comments
 (0)