Skip to content

Commit

Permalink
In case px4 at24xx eeprom's last page is corrupt, perform mass erase …
Browse files Browse the repository at this point in the history
…for the device

This initializes the CRC codes for every page for a new device

Signed-off-by: Jukka Laitinen <[email protected]>
  • Loading branch information
jlaitine committed Nov 29, 2024
1 parent a98bfa6 commit 29060c5
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions platforms/nuttx/src/px4/common/px4_24xxxx_mtd.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,34 +603,28 @@ int px4_at24c_initialize(FAR struct i2c_master_s *dev,
priv->perf_errors = perf_alloc(PC_COUNT, "[at24c] eeprom errors");
}

/* attempt to read to validate device is present */
unsigned char buf[5];
uint8_t addrbuf[2] = {0, 0};
/* attempt to read last page to validate device is present */

struct i2c_msg_s msgv[2] = {
{
.frequency = 400000,
.addr = priv->addr,
.flags = 0,
.buffer = &addrbuf[0],
.length = sizeof(addrbuf),
},
{
.frequency = 400000,
.addr = priv->addr,
.flags = I2C_M_READ,
.buffer = &buf[0],
.length = sizeof(buf),
}
};
uint8_t buf[AT24XX_PAGESIZE];
int ret = at24c_bread((FAR struct mtd_dev_s *)priv,
AT24XX_NPAGES - 1,
1,
buf);

BOARD_EEPROM_WP_CTRL(true);
if (ret != 1) {
ferr("Read failed, mass erase, ret %d\n", ret);

at24c_eraseall(&g_at24c[number_of_instances]);

perf_begin(priv->perf_transfers);
int ret = I2C_TRANSFER(priv->dev, &msgv[0], 2);
perf_end(priv->perf_transfers);
/* Try to read again */

ret = at24c_bread((FAR struct mtd_dev_s *)priv,
AT24XX_NPAGES - 1,
1,
buf);
}

if (ret < 0) {
if (ret != 1) {
perf_free(priv->perf_transfers);
perf_free(priv->perf_resets_retries);
perf_free(priv->perf_errors);
Expand All @@ -639,7 +633,7 @@ int px4_at24c_initialize(FAR struct i2c_master_s *dev,
priv->perf_resets_retries = NULL;
priv->perf_errors = NULL;

return ret;
return -1;
}

*mtd_dev = (FAR struct mtd_dev_s *)priv;
Expand Down

0 comments on commit 29060c5

Please sign in to comment.