more idf5.1 preps: Berry SPI (#19273)

This commit is contained in:
Christian Baars 2023-08-06 21:10:36 +02:00 committed by GitHub
parent 7488ed03f0
commit 76aa6747a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,14 +24,12 @@
#include "esp_idf_version.h" #include "esp_idf_version.h"
#if ESP_IDF_VERSION_MAJOR >= 5 #if ESP_IDF_VERSION_MAJOR >= 5
// esp_spi_flash.h is deprecated, please use spi_flash_mmap.h instead #include "esp_flash.h"
#include "spi_flash_mmap.h"
#else #else
#include "esp_spi_flash.h" #include "esp_spi_flash.h"
#endif #endif
size_t FlashWriteSubSector(uint32_t address_start, const uint8_t *data, size_t size) { size_t FlashWriteSubSector(uint32_t address_start, const uint8_t *data, size_t size) {
#if ESP_IDF_VERSION_MAJOR < 5
uint32_t addr = address_start; uint32_t addr = address_start;
size_t size_left = size; size_t size_left = size;
size_t current_offset = 0; size_t current_offset = 0;
@ -52,13 +50,26 @@ size_t FlashWriteSubSector(uint32_t address_start, const uint8_t *data, size_t s
if (addr_in_page == 0 && size_in_page == SPI_FLASH_SEC_SIZE) { if (addr_in_page == 0 && size_in_page == SPI_FLASH_SEC_SIZE) {
memcpy(buffer, data + current_offset, SPI_FLASH_SEC_SIZE); memcpy(buffer, data + current_offset, SPI_FLASH_SEC_SIZE);
} else { } else {
#if ESP_IDF_VERSION_MAJOR < 5
ret = spi_flash_read(page_addr, buffer, SPI_FLASH_SEC_SIZE); ret = spi_flash_read(page_addr, buffer, SPI_FLASH_SEC_SIZE);
#else
ret = esp_flash_read(NULL, buffer, page_addr, SPI_FLASH_SEC_SIZE);
#endif
if (ret) { AddLog(LOG_LEVEL_INFO, "BRY: could not read flash %p (0x%X) ret=%i", page_addr, SPI_FLASH_SEC_SIZE, ret); return 0; } if (ret) { AddLog(LOG_LEVEL_INFO, "BRY: could not read flash %p (0x%X) ret=%i", page_addr, SPI_FLASH_SEC_SIZE, ret); return 0; }
memcpy(buffer + addr_in_page, data + current_offset, size_in_page); memcpy(buffer + addr_in_page, data + current_offset, size_in_page);
} }
#if ESP_IDF_VERSION_MAJOR < 5
ret = spi_flash_erase_sector(page_addr / SPI_FLASH_SEC_SIZE); ret = spi_flash_erase_sector(page_addr / SPI_FLASH_SEC_SIZE);
#else
ret = esp_flash_erase_region(NULL, page_addr, SPI_FLASH_SEC_SIZE);
#endif
if (ret) { AddLog(LOG_LEVEL_INFO, "BRY: could not erase flash sector 0x%X ret=%i", page_addr / SPI_FLASH_SEC_SIZE, ret); return 0; } if (ret) { AddLog(LOG_LEVEL_INFO, "BRY: could not erase flash sector 0x%X ret=%i", page_addr / SPI_FLASH_SEC_SIZE, ret); return 0; }
#if ESP_IDF_VERSION_MAJOR < 5
spi_flash_write(page_addr, buffer, SPI_FLASH_SEC_SIZE); spi_flash_write(page_addr, buffer, SPI_FLASH_SEC_SIZE);
#else
esp_flash_write(NULL, buffer, page_addr, SPI_FLASH_SEC_SIZE);
#endif
if (ret) { AddLog(LOG_LEVEL_INFO, "BRY: could not write flash %p (0x%X) ret=%i", page_addr, SPI_FLASH_SEC_SIZE, ret); return 0; } if (ret) { AddLog(LOG_LEVEL_INFO, "BRY: could not write flash %p (0x%X) ret=%i", page_addr, SPI_FLASH_SEC_SIZE, ret); return 0; }
addr += size_in_page; addr += size_in_page;
@ -67,9 +78,6 @@ size_t FlashWriteSubSector(uint32_t address_start, const uint8_t *data, size_t s
} }
return current_offset; return current_offset;
#else
// TODO ESPIDF 5
#endif
} }
/*********************************************************************************************\ /*********************************************************************************************\
@ -84,7 +92,6 @@ extern "C" {
// If length is not specified, it is full block 4KB // If length is not specified, it is full block 4KB
int32_t p_flash_read(struct bvm *vm); int32_t p_flash_read(struct bvm *vm);
int32_t p_flash_read(struct bvm *vm) { int32_t p_flash_read(struct bvm *vm) {
#if ESP_IDF_VERSION_MAJOR < 5
int32_t argc = be_top(vm); // Get the number of arguments int32_t argc = be_top(vm); // Get the number of arguments
if (argc >= 1 && be_isint(vm, 1) && if (argc >= 1 && be_isint(vm, 1) &&
(argc < 2 || be_isint(vm, 2)) ) { // optional second argument must be int (argc < 2 || be_isint(vm, 2)) ) { // optional second argument must be int
@ -96,14 +103,17 @@ extern "C" {
} }
// allocate a buffer in the heap that will be automatically freed when going out of scope // allocate a buffer in the heap that will be automatically freed when going out of scope
auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[length]); auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[length]);
#if ESP_IDF_VERSION_MAJOR < 5
esp_err_t ret = spi_flash_read(address, buf.get(), length); esp_err_t ret = spi_flash_read(address, buf.get(), length);
#else
esp_err_t ret = esp_flash_read(NULL, buf.get(), address, length);
#endif
if (ret) { if (ret) {
be_raisef(vm, "internal_error", "Error calling spi_flash_read(0x%X, %i)", address, length); be_raisef(vm, "internal_error", "Error calling spi_flash_read(0x%X, %i)", address, length);
} }
be_pushbytes(vm, buf.get(), length); be_pushbytes(vm, buf.get(), length);
be_return(vm); be_return(vm);
} }
#endif
be_raise(vm, kTypeError, nullptr); be_raise(vm, kTypeError, nullptr);
} }
@ -111,7 +121,6 @@ extern "C" {
// if `no_erase` is true, just call spi_flash_write // if `no_erase` is true, just call spi_flash_write
int32_t p_flash_write(struct bvm *vm); int32_t p_flash_write(struct bvm *vm);
int32_t p_flash_write(struct bvm *vm) { int32_t p_flash_write(struct bvm *vm) {
#if ESP_IDF_VERSION_MAJOR < 5
int32_t argc = be_top(vm); // Get the number of arguments int32_t argc = be_top(vm); // Get the number of arguments
if (argc >= 2 && be_isint(vm, 1) && be_isinstance(vm, 2)) { if (argc >= 2 && be_isint(vm, 1) && be_isinstance(vm, 2)) {
be_getglobal(vm, "bytes"); /* get the bytes class */ /* TODO eventually replace with be_getbuiltin */ be_getglobal(vm, "bytes"); /* get the bytes class */ /* TODO eventually replace with be_getbuiltin */
@ -125,7 +134,11 @@ extern "C" {
const void * bytes = be_tobytes(vm, 2, &length); const void * bytes = be_tobytes(vm, 2, &length);
if (bytes && length > 0) { if (bytes && length > 0) {
if (no_erase) { if (no_erase) {
#if ESP_IDF_VERSION_MAJOR < 5
esp_err_t ret = spi_flash_write(address, (const uint8_t*)bytes, length); esp_err_t ret = spi_flash_write(address, (const uint8_t*)bytes, length);
#else
esp_err_t ret = esp_flash_write(NULL, (const uint8_t*)bytes, address, length);
#endif
if (ret) { if (ret) {
be_raisef(vm, "internal_error", "Error calling spi_flash_write() ret=%i", ret); be_raisef(vm, "internal_error", "Error calling spi_flash_write() ret=%i", ret);
} }
@ -140,7 +153,6 @@ extern "C" {
} }
} }
} }
#endif
be_raise(vm, kTypeError, nullptr); be_raise(vm, kTypeError, nullptr);
} }
@ -149,7 +161,6 @@ extern "C" {
// Address and length must be 4KB aligned // Address and length must be 4KB aligned
int32_t p_flash_erase(struct bvm *vm); int32_t p_flash_erase(struct bvm *vm);
int32_t p_flash_erase(struct bvm *vm) { int32_t p_flash_erase(struct bvm *vm) {
#if ESP_IDF_VERSION_MAJOR < 5
int32_t argc = be_top(vm); // Get the number of arguments int32_t argc = be_top(vm); // Get the number of arguments
if (argc >= 2 && be_isint(vm, 1) && be_isint(vm, 2)) { if (argc >= 2 && be_isint(vm, 1) && be_isint(vm, 2)) {
int32_t address = be_toint(vm, 1); int32_t address = be_toint(vm, 1);
@ -160,10 +171,13 @@ extern "C" {
if ((length % 0x1000) != 0 || length < 0) { if ((length % 0x1000) != 0 || length < 0) {
be_raise(vm, "value_error", "Length must be a multiple of 0x1000"); be_raise(vm, "value_error", "Length must be a multiple of 0x1000");
} }
#if ESP_IDF_VERSION_MAJOR < 5
esp_err_t ret = spi_flash_erase_range(address, length); esp_err_t ret = spi_flash_erase_range(address, length);
#else
esp_err_t ret = esp_flash_erase_region(NULL, address, length);
#endif
be_return_nil(vm); be_return_nil(vm);
} }
#endif
be_raise(vm, kTypeError, nullptr); be_raise(vm, kTypeError, nullptr);
} }