Skip to content

Commit 556168d

Browse files
Merge pull request #1046 from iNavFlight/uninitialized-i2c-fix
Fix a bug with accessing the non-initialized I2C bus (backport to master)
2 parents 1f55448 + 4393694 commit 556168d

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/main/drivers/bus_i2c_stm32f10x.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ bool i2cWrite(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t data)
514514
{
515515
static uint8_t writeBuf[1];
516516

517+
// Don't try to access the non-initialized device
518+
if (!busState[device].initialized)
519+
return false;
520+
517521
// Set up write transaction
518522
writeBuf[0] = data;
519523

@@ -533,6 +537,10 @@ bool i2cWrite(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t data)
533537

534538
bool i2cRead(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t len, uint8_t* buf)
535539
{
540+
// Don't try to access the non-initialized device
541+
if (!busState[device].initialized)
542+
return false;
543+
536544
// Set up read transaction
537545
busState[device].addr = addr << 1;
538546
busState[device].reg = reg;

src/main/drivers/bus_i2c_stm32f30x.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ bool i2cWrite(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t data)
376376
{
377377
static uint8_t writeBuf[1];
378378

379+
// Don't try to access the non-initialized device
380+
if (!busState[device].initialized)
381+
return false;
382+
379383
// Set up write transaction
380384
writeBuf[0] = data;
381385

@@ -395,6 +399,10 @@ bool i2cWrite(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t data)
395399

396400
bool i2cRead(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t len, uint8_t* buf)
397401
{
402+
// Don't try to access the non-initialized device
403+
if (!busState[device].initialized)
404+
return false;
405+
398406
// Set up read transaction
399407
busState[device].addr = addr << 1;
400408
busState[device].reg = reg;

0 commit comments

Comments
 (0)