Skip to content

Commit

Permalink
Merge pull request #28 from shubhamdp/support-idf-v5.1
Browse files Browse the repository at this point in the history
Use the newer spi_flash APIs for read/write/erase flash operations
  • Loading branch information
jeremyjh committed Aug 1, 2023
2 parents 3344a81 + a9482e3 commit a6299b6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
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

0 comments on commit a6299b6

Please sign in to comment.