diff --git a/src/target/esp32c3.c b/src/target/esp32c3.c index 7da731fe8eb..63934dc6a8c 100644 --- a/src/target/esp32c3.c +++ b/src/target/esp32c3.c @@ -125,6 +125,7 @@ typedef struct esp32c3_priv { uint32_t wdt_config[4]; target_addr_t last_invalidated_sector; + flash_erase_func spi_flash_erase; } esp32c3_priv_s; static void esp32c3_disable_wdts(target_s *target); @@ -205,6 +206,8 @@ bool esp32c3_probe(target_s *const target) /* Adjust the resulting capacity to not exceed the available memory mapping window size */ flash->flash.length = MIN(flash->flash.length, ESP32_C3_IBUS_FLASH_SIZE); /* Adjust over to our slightly modified versions of the Flash routines */ + esp32c3_priv_s *const priv = (esp32c3_priv_s *)target->target_storage; + priv->spi_flash_erase = flash->flash.erase; flash->flash.write = esp32c3_spi_flash_write; flash->flash.erase = esp32c3_spi_flash_erase; } @@ -434,20 +437,15 @@ static bool esp32c3_exit_flash_mode(target_s *const target) static bool esp32c3_spi_flash_erase(target_flash_s *const flash, const target_addr_t addr, const size_t length) { - (void)length; target_s *const target = flash->t; - const spi_flash_s *const spi_flash = (spi_flash_s *)flash; - esp32c3_spi_run_command(target, SPI_FLASH_CMD_WRITE_ENABLE, 0U); - if (!(esp32c3_spi_read_status(target) & SPI_FLASH_STATUS_WRITE_ENABLED)) + esp32c3_priv_s *const priv = (esp32c3_priv_s *)target->target_storage; + /* Call the underlying erase routine */ + const bool result = priv->spi_flash_erase(flash, addr, length); + /* If it didn't work out, propergate the failure */ + if (!result) return false; - - esp32c3_spi_run_command( - target, SPI_FLASH_CMD_SECTOR_ERASE | SPI_FLASH_OPCODE(spi_flash->sector_erase_opcode), addr - flash->start); - while (esp32c3_spi_read_status(target) & SPI_FLASH_STATUS_BUSY) - continue; /* Update the address of the last invalidated sector so we can correctly invalidate the i-cache and reload it */ - esp32c3_priv_s *const priv = (esp32c3_priv_s *)target->target_storage; - priv->last_invalidated_sector = addr + length; + priv->last_invalidated_sector = addr + flash->blocksize; return true; }