Skip to content

Commit 362a24e

Browse files
author
Wu Han
committed
Add i2c bus error handling in ssd1306 example
1 parent 1aba10c commit 362a24e

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

port/u8g_port.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,29 @@ uint8_t u8x8_byte_rt_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *ar
3434
/* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */
3535
struct rt_i2c_msg msgs;
3636
static uint8_t buffer[32];
37-
static uint8_t buf_idx;
38-
uint8_t *data;
37+
static uint8_t buf_idx;
38+
uint8_t *data;
3939

4040

4141
rt_uint8_t t = 0;
4242
switch(msg)
4343
{
4444
case U8X8_MSG_BYTE_SEND:
4545
data = (uint8_t *)arg_ptr;
46-
while( arg_int > 0 )
47-
{
46+
while( arg_int > 0 )
47+
{
4848
buffer[buf_idx++] = *data;
4949
data++;
5050
arg_int--;
51-
}
51+
}
5252
break;
5353
case U8X8_MSG_BYTE_INIT:
5454
i2c_bus = rt_i2c_bus_device_find(I2C_DEVICE_NAME);
55+
if (i2c_bus == RT_NULL)
56+
{
57+
// rt_kprintf("Failed to find bus\n");
58+
return 0;
59+
}
5560
break;
5661
case U8X8_MSG_BYTE_SET_DC:
5762
break;
@@ -61,15 +66,24 @@ uint8_t u8x8_byte_rt_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *ar
6166
buf_idx = 0;
6267
break;
6368
case U8X8_MSG_BYTE_END_TRANSFER:
69+
if (i2c_bus == RT_NULL)
70+
{
71+
rt_kprintf("Failed to find bus\n");
72+
return 0;
73+
}
6474
// I2C Data Transfer
65-
msgs.addr = u8x8_GetI2CAddress(u8x8)>>1;
75+
msgs.addr = u8x8_GetI2CAddress(u8x8)>>1;
6676
msgs.flags = RT_I2C_WR;
6777
msgs.buf = buffer;
6878
msgs.len = buf_idx;
6979
while(rt_i2c_transfer(i2c_bus, &msgs, 1) != 1 && t < MAX_RETRY)
7080
{
7181
t++;
7282
};
83+
if(t >= MAX_RETRY)
84+
{
85+
return 0;
86+
}
7387
break;
7488
default:
7589
return 0;

0 commit comments

Comments
 (0)