Skip to content

Commit

Permalink
esp32c3: Switched out the custom Flash erase implementation for the o…
Browse files Browse the repository at this point in the history
…ne in spi.c w/ a small modification
  • Loading branch information
dragonmux committed Nov 17, 2023
1 parent 7feec2b commit 9e9abfe
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/target/esp32c3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 9e9abfe

Please sign in to comment.