diff --git a/platforms/nuttx/src/px4/common/px4_24xxxx_mtd.c b/platforms/nuttx/src/px4/common/px4_24xxxx_mtd.c index 3617a802bd13..cdb1363e5de9 100644 --- a/platforms/nuttx/src/px4/common/px4_24xxxx_mtd.c +++ b/platforms/nuttx/src/px4/common/px4_24xxxx_mtd.c @@ -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;