mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-20 03:13:44 +08:00
Hardened the EEPROM attach routine for param storage
This commit is contained in:
@@ -502,6 +502,33 @@ FAR struct mtd_dev_s *at24c_initialize(FAR struct i2c_dev_s *dev) {
|
||||
priv->perf_write_errors = perf_alloc(PC_COUNT, "EEPROM write errors");
|
||||
}
|
||||
|
||||
/* attempt to read to validate device is present */
|
||||
unsigned char buf[5];
|
||||
uint8_t addrbuf[2] = {0, 0};
|
||||
|
||||
struct i2c_msg_s msgv[2] = {
|
||||
{
|
||||
.addr = priv->addr,
|
||||
.flags = 0,
|
||||
.buffer = &addrbuf[0],
|
||||
.length = sizeof(addrbuf),
|
||||
},
|
||||
{
|
||||
.addr = priv->addr,
|
||||
.flags = I2C_M_READ,
|
||||
.buffer = &buf[0],
|
||||
.length = sizeof(buf),
|
||||
}
|
||||
};
|
||||
|
||||
perf_begin(priv->perf_reads);
|
||||
int ret = I2C_TRANSFER(priv->dev, &msgv[0], 2);
|
||||
perf_end(priv->perf_reads);
|
||||
|
||||
if (ret < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Return the implementation-specific state structure as the MTD device */
|
||||
|
||||
fvdbg("Return %p\n", priv);
|
||||
|
||||
@@ -118,9 +118,19 @@ eeprom_attach(void)
|
||||
if (i2c == NULL)
|
||||
errx(1, "failed to locate I2C bus");
|
||||
|
||||
/* start the MTD driver */
|
||||
eeprom_mtd = at24c_initialize(i2c);
|
||||
/* start the MTD driver, attempt 5 times */
|
||||
for (int i = 0; i < 5; i++) {
|
||||
eeprom_mtd = at24c_initialize(i2c);
|
||||
if (eeprom_mtd) {
|
||||
/* abort on first valid result */
|
||||
if (i > 0) {
|
||||
warnx("warning: EEPROM needed %d attempts to attach", i+1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* if last attempt is still unsuccessful, abort */
|
||||
if (eeprom_mtd == NULL)
|
||||
errx(1, "failed to initialize EEPROM driver");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user