Skip to content

Commit

Permalink
xtensa/esp32s3: Support to read data from flash to PSRAM
Browse files Browse the repository at this point in the history
  • Loading branch information
donghengqaz committed Oct 13, 2023
1 parent 061be5f commit c525992
Showing 1 changed file with 84 additions and 10 deletions.
94 changes: 84 additions & 10 deletions arch/xtensa/src/esp32s3/esp32s3_spiflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,48 @@ static sem_t g_disable_non_iram_isr_on_core[CONFIG_SMP_NCPUS];
* Private Functions
****************************************************************************/

/****************************************************************************
* Name: spiflash_suspend_cache
*
* Description:
* Suspend CPU cache.
*
****************************************************************************/

static void spiflash_suspend_cache(void)
{
int cpu = up_cpu_index();
#ifdef CONFIG_SMP
int other_cpu = cpu ? 0 : 1;
#endif

spi_flash_disable_cache(cpu);
#ifdef CONFIG_SMP
spi_flash_disable_cache(other_cpu);
#endif
}

/****************************************************************************
* Name: spiflash_resume_cache
*
* Description:
* Resume CPU cache.
*
****************************************************************************/

static void spiflash_resume_cache(void)
{
int cpu = up_cpu_index();
#ifdef CONFIG_SMP
int other_cpu = cpu ? 0 : 1;
#endif

spi_flash_restore_cache(cpu);
#ifdef CONFIG_SMP
spi_flash_restore_cache(other_cpu);
#endif
}

/****************************************************************************
* Name: spiflash_start
*
Expand Down Expand Up @@ -304,11 +346,6 @@ static void spiflash_start(void)
nxsched_set_priority(tcb, saved_priority);

esp32s3_irq_noniram_disable();

spi_flash_disable_cache(cpu);
#ifdef CONFIG_SMP
spi_flash_disable_cache(other_cpu);
#endif
}

/****************************************************************************
Expand All @@ -334,11 +371,6 @@ static void spiflash_end(void)
DEBUGASSERT(other_cpu != cpu);
#endif

spi_flash_restore_cache(cpu);
#ifdef CONFIG_SMP
spi_flash_restore_cache(other_cpu);
#endif

/* Signal to spi_flash_op_block_task that flash operation is complete */

g_flash_op_complete = true;
Expand Down Expand Up @@ -812,6 +844,7 @@ int spi_flash_erase_range(uint32_t start_address, uint32_t size)
uint32_t addr = start_address;

spiflash_start();
spiflash_suspend_cache();

for (uint32_t i = 0; i < size; i += FLASH_SECTOR_SIZE)
{
Expand All @@ -825,6 +858,7 @@ int spi_flash_erase_range(uint32_t start_address, uint32_t size)
wait_flash_idle();
disable_flash_write();

spiflash_resume_cache();
spiflash_end();

return ret;
Expand Down Expand Up @@ -854,6 +888,9 @@ int spi_flash_write(uint32_t dest_addr, const void *buffer, uint32_t size)
uint32_t tx_addr = dest_addr;

spiflash_start();
#ifndef CONFIG_ESP32S3_SPIRAM
spiflash_suspend_cache();
#endif

for (int i = 0; i < size; i += SPI_BUFFER_BYTES)
{
Expand All @@ -862,11 +899,25 @@ int spi_flash_write(uint32_t dest_addr, const void *buffer, uint32_t size)

memcpy(spi_buffer, tx_buf, n);

#ifdef CONFIG_ESP32S3_SPIRAM

/* Disable cache, and then write data from SRAM to flash */

spiflash_suspend_cache();
#endif

wait_flash_idle();
enable_flash_write();

WRITE_DATA_TO_FLASH(tx_addr, spi_buffer, n);

#ifdef CONFIG_ESP32S3_SPIRAM

/* Re-enable cache, and then copy data from PSRAM to SRAM */

spiflash_resume_cache();
#endif

tx_bytes -= n;
tx_buf += n;
tx_addr += n;
Expand All @@ -875,6 +926,9 @@ int spi_flash_write(uint32_t dest_addr, const void *buffer, uint32_t size)
wait_flash_idle();
disable_flash_write();

#ifndef CONFIG_ESP32S3_SPIRAM
spiflash_resume_cache();
#endif
spiflash_end();

return ret;
Expand Down Expand Up @@ -904,20 +958,40 @@ int spi_flash_read(uint32_t src_addr, void *dest, uint32_t size)
uint32_t rx_addr = src_addr;

spiflash_start();
#ifndef CONFIG_ESP32S3_SPIRAM
spiflash_suspend_cache();
#endif

for (uint32_t i = 0; i < size; i += SPI_BUFFER_BYTES)
{
uint32_t spi_buffer[SPI_BUFFER_WORDS];
uint32_t n = MIN(rx_bytes, SPI_BUFFER_BYTES);

#ifdef CONFIG_ESP32S3_SPIRAM

/* Disable cache, and then read data from flash to SRAM */

spiflash_suspend_cache();
#endif

READ_DATA_FROM_FLASH(rx_addr, spi_buffer, n);

#ifdef CONFIG_ESP32S3_SPIRAM

/* Re-enable cache, and then copy data from SRAM to PSRAM */

spiflash_resume_cache();
#endif

memcpy(rx_buf, spi_buffer, n);
rx_bytes -= n;
rx_buf += n;
rx_addr += n;
}

#ifndef CONFIG_ESP32S3_SPIRAM
spiflash_resume_cache();
#endif
spiflash_end();

return ret;
Expand Down

0 comments on commit c525992

Please sign in to comment.