Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the newer spi_flash APIs for read/write/erase flash operations #28

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion components/mkspiffs/src/spiffs/esp_spiffs.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@

#include "spiffs.h"

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
#include <esp_flash.h>
#include <spi_flash_mmap.h>
#else
#include <esp_spi_flash.h>
#endif

s32_t esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) {
u32_t aaddr;
Expand Down Expand Up @@ -63,7 +68,11 @@ s32_t esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) {

abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & (u32_t)-4);

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_read(NULL, (void *)abuff, aaddr, asize) != ESP_OK) {
#else
if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) {
#endif
free(buff);
return SPIFFS_ERR_INTERNAL;
}
Expand All @@ -72,7 +81,11 @@ s32_t esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) {

free(buff);
} else {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_read(NULL, (void *)dst, addr, size) != ESP_OK) {
#else
if (spi_flash_read(addr, (void *)dst, size) != 0) {
#endif
return SPIFFS_ERR_INTERNAL;
}
}
Expand All @@ -87,7 +100,7 @@ s32_t esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) {
u32_t asize;

asize = size;

// Align address to 4 byte
aaddr = (addr + (4 - 1)) & -4;
if (aaddr != addr) {
Expand All @@ -107,21 +120,33 @@ s32_t esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) {

abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & -4);

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_read(NULL, (void *)abuff, aaddr, asize) != ESP_OK) {
#else
if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) {
#endif
free(buff);
return SPIFFS_ERR_INTERNAL;
}

memcpy(abuff + (addr - aaddr), src, size);

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_write(NULL, (uint32_t *)abuff, aaddr, asize) != ESP_OK) {
#else
if (spi_flash_write(aaddr, (uint32_t *)abuff, asize) != 0) {
#endif
free(buff);
return SPIFFS_ERR_INTERNAL;
}

free(buff);
} else {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_write(NULL, (uint32_t *)src, addr, size) != ESP_OK) {
#else
if (spi_flash_write(addr, (uint32_t *)src, size) != 0) {
#endif
return SPIFFS_ERR_INTERNAL;
}
}
Expand All @@ -130,7 +155,11 @@ s32_t esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) {
}

s32_t IRAM_ATTR esp32_spi_flash_erase(u32_t addr, u32_t size) {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_erase_region(NULL, addr, size) != ESP_OK) {
#else
if (spi_flash_erase_sector(addr >> 12) != 0) {
#endif
return SPIFFS_ERR_INTERNAL;
}

Expand Down
2 changes: 1 addition & 1 deletion components/spiffs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FILE(GLOB SOURCES *.c)
idf_component_register(SRCS ${SOURCES} INCLUDE_DIRS "."
REQUIRES spi_flash)
REQUIRES vfs spi_flash)
29 changes: 29 additions & 0 deletions components/spiffs/esp_spiffs.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@

#include "spiffs.h"

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
#include <esp_flash.h>
#include <spi_flash_mmap.h>
#else
#include <esp_spi_flash.h>
#endif

s32_t IRAM_ATTR esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) {
u32_t aaddr;
Expand Down Expand Up @@ -63,7 +68,11 @@ s32_t IRAM_ATTR esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) {

abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & (u32_t)-4);

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_read(NULL, (void *)abuff, aaddr, asize) != ESP_OK) {
#else
if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) {
#endif
free(buff);
return SPIFFS_ERR_INTERNAL;
}
Expand All @@ -72,7 +81,11 @@ s32_t IRAM_ATTR esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) {

free(buff);
} else {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_read(NULL, (void *)dst, addr, size) != ESP_OK) {
#else
if (spi_flash_read(addr, (void *)dst, size) != 0) {
#endif
return SPIFFS_ERR_INTERNAL;
}
}
Expand Down Expand Up @@ -107,21 +120,33 @@ s32_t IRAM_ATTR esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) {

abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & -4);

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_read(NULL, (void *)abuff, aaddr, asize) != ESP_OK) {
#else
if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) {
#endif
free(buff);
return SPIFFS_ERR_INTERNAL;
}

memcpy(abuff + (addr - aaddr), src, size);

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_write(NULL, (uint32_t *)abuff, aaddr, asize) != ESP_OK) {
#else
if (spi_flash_write(aaddr, (uint32_t *)abuff, asize) != 0) {
#endif
free(buff);
return SPIFFS_ERR_INTERNAL;
}

free(buff);
} else {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_write(NULL, (uint32_t *)src, addr, size) != ESP_OK) {
#else
if (spi_flash_write(addr, (uint32_t *)src, size) != 0) {
#endif
return SPIFFS_ERR_INTERNAL;
}
}
Expand All @@ -130,7 +155,11 @@ s32_t IRAM_ATTR esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) {
}

s32_t IRAM_ATTR esp32_spi_flash_erase(u32_t addr, u32_t size) {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
if (esp_flash_erase_region(NULL, addr, size) != ESP_OK) {
#else
if (spi_flash_erase_sector(addr >> 12) != 0) {
#endif
return SPIFFS_ERR_INTERNAL;
}

Expand Down