Skip to content

Commit 562bcff

Browse files
author
danicampora
committed
cc3200: Improve robustness of the I2C driver.
When scanning for devices, try reading then writing. Increase the timeout of the transactions from 10 to 20 ms.
1 parent ed8db2e commit 562bcff

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

cc3200/mods/pybi2c.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ typedef struct _pyb_i2c_obj_t {
6363
#define PYBI2C_MIN_BAUD_RATE_HZ (50000)
6464
#define PYBI2C_MAX_BAUD_RATE_HZ (400000)
6565

66-
#define PYBI2C_TRANSC_TIMEOUT_MS (10)
66+
#define PYBI2C_TRANSC_TIMEOUT_MS (20)
6767
#define PYBI2C_TRANSAC_WAIT_DELAY_US (10)
6868

6969
#define PYBI2C_TIMEOUT_TO_COUNT(to_us, baud) (((baud) * to_us) / 16000000)
@@ -78,9 +78,13 @@ typedef struct _pyb_i2c_obj_t {
7878
DECLARE PRIVATE DATA
7979
******************************************************************************/
8080
STATIC pyb_i2c_obj_t pyb_i2c_obj = {.baudrate = 0};
81-
8281
STATIC const mp_obj_t pyb_i2c_def_pin[2] = {&pin_GP13, &pin_GP23};
8382

83+
/******************************************************************************
84+
DECLARE PRIVATE FUNCTIONS
85+
******************************************************************************/
86+
STATIC bool pyb_i2c_write(byte addr, byte *data, uint len, bool stop);
87+
8488
/******************************************************************************
8589
DEFINE PRIVATE FUNCTIONS
8690
******************************************************************************/
@@ -107,13 +111,13 @@ STATIC bool pyb_i2c_transaction(uint cmd) {
107111
// Wait until the current byte has been transferred.
108112
// Poll on the raw interrupt status.
109113
while ((MAP_I2CMasterIntStatusEx(I2CA0_BASE, false) & (I2C_MASTER_INT_DATA | I2C_MASTER_INT_TIMEOUT)) == 0) {
110-
// wait for a few microseconds
111-
UtilsDelay(UTILS_DELAY_US_TO_COUNT(PYBI2C_TRANSAC_WAIT_DELAY_US));
112-
timeout -= PYBI2C_TRANSAC_WAIT_DELAY_US;
113114
if (timeout < 0) {
114115
// the peripheral is not responding, so stop
115116
return false;
116117
}
118+
// wait for a few microseconds
119+
UtilsDelay(UTILS_DELAY_US_TO_COUNT(PYBI2C_TRANSAC_WAIT_DELAY_US));
120+
timeout -= PYBI2C_TRANSAC_WAIT_DELAY_US;
117121
}
118122

119123
// Check for any errors in the transfer
@@ -145,13 +149,22 @@ STATIC void pyb_i2c_check_init(pyb_i2c_obj_t *self) {
145149
}
146150

147151
STATIC bool pyb_i2c_scan_device(byte devAddr) {
148-
// Set I2C codec slave address
152+
bool ret = false;
153+
// Set the I2C slave address
149154
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, devAddr, true);
150155
// Initiate the transfer.
151-
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_SINGLE_RECEIVE));
152-
// Since this is a hack, send the stop bit anyway
156+
if (pyb_i2c_transaction(I2C_MASTER_CMD_SINGLE_RECEIVE)) {
157+
ret = true;
158+
}
159+
// Send the stop bit to cancel the read transaction
153160
MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
154-
return true;
161+
if (!ret) {
162+
uint8_t data = 0;
163+
if (pyb_i2c_write(devAddr, &data, sizeof(data), true)) {
164+
ret = true;
165+
}
166+
}
167+
return ret;
155168
}
156169

157170
STATIC bool pyb_i2c_mem_addr_write (byte addr, byte *mem_addr, uint mem_addr_len) {
@@ -365,7 +378,7 @@ STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) {
365378
pyb_i2c_check_init(&pyb_i2c_obj);
366379
mp_obj_t list = mp_obj_new_list(0, NULL);
367380
for (uint addr = 1; addr <= 127; addr++) {
368-
for (int i = 0; i < 7; i++) {
381+
for (int i = 0; i < 3; i++) {
369382
if (pyb_i2c_scan_device(addr)) {
370383
mp_obj_list_append(list, mp_obj_new_int(addr));
371384
break;

0 commit comments

Comments
 (0)