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 28, 2024
1 parent a98bfa6 commit e4e956e
Showing 1 changed file with 27 additions and 35 deletions.
62 changes: 27 additions & 35 deletions platforms/nuttx/src/px4/common/px4_24xxxx_mtd.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,43 +603,35 @@ 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);

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

at24c_eraseall(&g_at24c[number_of_instances]);

/* Try to read again */

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

if (ret != 1) {
perf_free(priv->perf_transfers);
perf_free(priv->perf_resets_retries);
perf_free(priv->perf_errors);
priv->perf_transfers = NULL;
priv->perf_resets_retries = NULL;
priv->perf_errors = NULL;
return ret;
}
};

BOARD_EEPROM_WP_CTRL(true);

perf_begin(priv->perf_transfers);
int ret = I2C_TRANSFER(priv->dev, &msgv[0], 2);
perf_end(priv->perf_transfers);

if (ret < 0) {
perf_free(priv->perf_transfers);
perf_free(priv->perf_resets_retries);
perf_free(priv->perf_errors);

priv->perf_transfers = NULL;
priv->perf_resets_retries = NULL;
priv->perf_errors = NULL;

return ret;
}

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

0 comments on commit e4e956e

Please sign in to comment.