2022-02-21 16:09:24 -05:00
|
|
|
/*
|
2023-09-07 09:59:47 -04:00
|
|
|
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
2022-02-21 16:09:24 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-07-04 19:01:03 -04:00
|
|
|
#include "WL_Ext_Perf.h"
|
2023-09-07 09:59:47 -04:00
|
|
|
#include "Partition.h"
|
2018-07-04 19:01:03 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
|
|
|
static const char *TAG = "wl_ext_perf";
|
|
|
|
|
|
|
|
#define WL_EXT_RESULT_CHECK(result) \
|
|
|
|
if (result != ESP_OK) { \
|
|
|
|
ESP_LOGE(TAG,"%s(%d): result = 0x%08x", __FUNCTION__, __LINE__, result); \
|
|
|
|
return (result); \
|
|
|
|
}
|
|
|
|
|
|
|
|
WL_Ext_Perf::WL_Ext_Perf(): WL_Flash()
|
|
|
|
{
|
|
|
|
this->sector_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WL_Ext_Perf::~WL_Ext_Perf()
|
|
|
|
{
|
|
|
|
free(this->sector_buffer);
|
|
|
|
}
|
|
|
|
|
2023-09-07 09:59:47 -04:00
|
|
|
esp_err_t WL_Ext_Perf::config(WL_Config_s *cfg, Partition *partition)
|
2018-07-04 19:01:03 -04:00
|
|
|
{
|
2023-03-30 10:15:43 -04:00
|
|
|
wl_ext_cfg_t *ext_cfg = (wl_ext_cfg_t *)cfg;
|
2018-07-04 19:01:03 -04:00
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
this->flash_sector_size = ext_cfg->flash_sector_size;
|
|
|
|
this->fat_sector_size = ext_cfg->fat_sector_size;
|
2018-07-04 19:01:03 -04:00
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
/*when flash and fat sector sizes are not equal, (where flash_sector_size >= fat_sector_size)
|
|
|
|
this flash_fat_sector_size_factor will be used while flash sector erase or read-write operation */
|
|
|
|
this->flash_fat_sector_size_factor = this->flash_sector_size / this->fat_sector_size;
|
|
|
|
if (this->flash_fat_sector_size_factor < 1) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
2018-07-04 19:01:03 -04:00
|
|
|
}
|
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
this->sector_buffer = (uint32_t *)malloc(ext_cfg->flash_sector_size);
|
|
|
|
if (this->sector_buffer == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
2018-07-04 19:01:03 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 09:59:47 -04:00
|
|
|
return WL_Flash::config(cfg, partition);
|
2018-07-04 19:01:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t WL_Ext_Perf::init()
|
|
|
|
{
|
|
|
|
return WL_Flash::init();
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
size_t WL_Ext_Perf::get_flash_size()
|
2018-07-04 19:01:03 -04:00
|
|
|
{
|
2023-03-30 10:15:43 -04:00
|
|
|
return WL_Flash::get_flash_size();
|
2018-07-04 19:01:03 -04:00
|
|
|
}
|
2023-03-30 10:15:43 -04:00
|
|
|
|
|
|
|
size_t WL_Ext_Perf::get_sector_size()
|
2018-07-04 19:01:03 -04:00
|
|
|
{
|
|
|
|
return this->fat_sector_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t WL_Ext_Perf::erase_sector(size_t sector)
|
|
|
|
{
|
2023-03-30 10:15:43 -04:00
|
|
|
return WL_Flash::erase_sector(sector);
|
2018-07-04 19:01:03 -04:00
|
|
|
}
|
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
/*
|
|
|
|
erase_sector_fit function is needed in case flash_sector_size != fat_sector_size and
|
|
|
|
sector to be erased is not multiple of flash_fat_sector_size_factor
|
|
|
|
*/
|
|
|
|
esp_err_t WL_Ext_Perf::erase_sector_fit(uint32_t first_erase_sector, uint32_t count)
|
2018-07-04 19:01:03 -04:00
|
|
|
{
|
2023-03-30 10:15:43 -04:00
|
|
|
// This method works with one flash device sector and able to erase "count" of fatfs sectors from this first_erase_sector
|
2018-07-04 19:01:03 -04:00
|
|
|
esp_err_t result = ESP_OK;
|
2023-03-30 10:15:43 -04:00
|
|
|
ESP_LOGV(TAG, "%s begin, first_erase_sector = 0x%08x, count = %i", __func__, first_erase_sector, count);
|
2018-07-04 19:01:03 -04:00
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
uint32_t flash_sector_base_addr = first_erase_sector / this->flash_fat_sector_size_factor;
|
|
|
|
uint32_t pre_check_start = first_erase_sector % this->flash_fat_sector_size_factor;
|
2018-07-04 19:01:03 -04:00
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
// Except pre check and post check data area, read and store all other data to sector_buffer
|
|
|
|
for (int i = 0; i < this->flash_fat_sector_size_factor; i++) {
|
2018-07-04 19:01:03 -04:00
|
|
|
if ((i < pre_check_start) || (i >= count + pre_check_start)) {
|
2023-03-30 10:15:43 -04:00
|
|
|
result = this->read(flash_sector_base_addr * this->flash_sector_size + i * this->fat_sector_size,
|
|
|
|
&this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)],
|
|
|
|
this->fat_sector_size);
|
2018-07-04 19:01:03 -04:00
|
|
|
WL_EXT_RESULT_CHECK(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
//erase complete flash sector which includes pre and post check data area
|
|
|
|
result = WL_Flash::erase_sector(flash_sector_base_addr);
|
2018-07-04 19:01:03 -04:00
|
|
|
WL_EXT_RESULT_CHECK(result);
|
2023-03-30 10:15:43 -04:00
|
|
|
|
|
|
|
/* Restore data which was previously stored to sector_buffer
|
|
|
|
back to data area which was not part of pre and post check data */
|
|
|
|
for (int i = 0; i < this->flash_fat_sector_size_factor; i++) {
|
2018-07-04 19:01:03 -04:00
|
|
|
if ((i < pre_check_start) || (i >= count + pre_check_start)) {
|
2023-03-30 10:15:43 -04:00
|
|
|
result = this->write(flash_sector_base_addr * this->flash_sector_size + i * this->fat_sector_size,
|
|
|
|
&this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)],
|
|
|
|
this->fat_sector_size);
|
2018-07-04 19:01:03 -04:00
|
|
|
WL_EXT_RESULT_CHECK(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t WL_Ext_Perf::erase_range(size_t start_address, size_t size)
|
|
|
|
{
|
|
|
|
esp_err_t result = ESP_OK;
|
2023-03-30 10:15:43 -04:00
|
|
|
|
|
|
|
//start_address as well as size should be aligned to fat_sector_size
|
2018-07-04 19:01:03 -04:00
|
|
|
if ((start_address % this->fat_sector_size) != 0) {
|
|
|
|
result = ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if (((size % this->fat_sector_size) != 0) || (size == 0)) {
|
2023-03-30 10:15:43 -04:00
|
|
|
result = ESP_ERR_INVALID_SIZE;
|
2018-07-04 19:01:03 -04:00
|
|
|
}
|
|
|
|
WL_EXT_RESULT_CHECK(result);
|
|
|
|
|
|
|
|
// The range to erase could be allocated in any possible way
|
|
|
|
// ---------------------------------------------------------
|
|
|
|
// | | | | |
|
|
|
|
// |0|0|x|x|x|x|x|x|x|x|x|x|x|x|0|0|
|
|
|
|
// | pre | rest | rest | post | <- check ranges
|
|
|
|
//
|
2023-03-30 10:15:43 -04:00
|
|
|
// Pre check - the data that does not fit in the full sector at the begining of the erased block
|
|
|
|
// Post check - the data that does not fit in the full sector at the end of the erased block
|
|
|
|
// rest check - the data that fits in the full sector at the middle of the erased block
|
2018-07-04 19:01:03 -04:00
|
|
|
//
|
2023-03-30 10:15:43 -04:00
|
|
|
// In case of pre and post check situations, the data of the area which should not be erased
|
|
|
|
// as part of pre and post check data area have to read first, store at other location,
|
|
|
|
// erase complete sector and restore data of area which should not be erased.
|
|
|
|
// For the rest check area, this operation not needed because complete flash device sector will be erased.
|
2018-07-04 19:01:03 -04:00
|
|
|
|
|
|
|
ESP_LOGV(TAG, "%s begin, addr = 0x%08x, size = %i", __func__, start_address, size);
|
|
|
|
uint32_t sectors_count = size / this->fat_sector_size;
|
2023-03-30 10:15:43 -04:00
|
|
|
|
|
|
|
// Calculate pre check values
|
|
|
|
uint32_t pre_check_start = (start_address / this->fat_sector_size) % this->flash_fat_sector_size_factor;
|
|
|
|
uint32_t pre_check_count = (this->flash_fat_sector_size_factor - pre_check_start);
|
|
|
|
//maximum sectors count that need to be erased is sectors_count
|
2018-07-04 19:01:03 -04:00
|
|
|
if (pre_check_count > sectors_count) {
|
|
|
|
pre_check_count = sectors_count;
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
// Calculate post check values
|
|
|
|
uint32_t post_check_count = (sectors_count - pre_check_count) % this->flash_fat_sector_size_factor;
|
2018-07-04 19:01:03 -04:00
|
|
|
uint32_t post_check_start = ((start_address + size - post_check_count * this->fat_sector_size) / this->fat_sector_size);
|
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
// Calculate rest check values
|
2018-07-04 19:01:03 -04:00
|
|
|
uint32_t rest_check_count = sectors_count - pre_check_count - post_check_count;
|
2023-03-30 10:15:43 -04:00
|
|
|
if ((pre_check_count == this->flash_fat_sector_size_factor) && (0 == pre_check_start)) {
|
|
|
|
rest_check_count+=this->flash_fat_sector_size_factor;
|
2018-07-04 19:01:03 -04:00
|
|
|
pre_check_count = 0;
|
|
|
|
}
|
|
|
|
uint32_t rest_check_start = start_address + pre_check_count * this->fat_sector_size;
|
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
// Clear pre_check_count sectors
|
2018-07-04 19:01:03 -04:00
|
|
|
if (pre_check_count != 0) {
|
|
|
|
result = this->erase_sector_fit(start_address / this->fat_sector_size, pre_check_count);
|
|
|
|
WL_EXT_RESULT_CHECK(result);
|
|
|
|
}
|
2023-06-08 14:56:11 -04:00
|
|
|
ESP_LOGV(TAG, "%s rest_check_start = %i, pre_check_count=%i, rest_check_count=%i, post_check_count=%i", __func__, rest_check_start, pre_check_count, rest_check_count, post_check_count);
|
2023-03-30 10:15:43 -04:00
|
|
|
|
|
|
|
// Clear rest_check_count sectors
|
2018-07-04 19:01:03 -04:00
|
|
|
if (rest_check_count > 0) {
|
2023-03-30 10:15:43 -04:00
|
|
|
rest_check_count = rest_check_count / this->flash_fat_sector_size_factor;
|
2018-07-04 19:01:03 -04:00
|
|
|
size_t start_sector = rest_check_start / this->flash_sector_size;
|
|
|
|
for (size_t i = 0; i < rest_check_count; i++) {
|
|
|
|
result = WL_Flash::erase_sector(start_sector + i);
|
|
|
|
WL_EXT_RESULT_CHECK(result);
|
|
|
|
}
|
|
|
|
}
|
2023-03-30 10:15:43 -04:00
|
|
|
|
|
|
|
// Clear post_check_count sectors
|
2018-07-04 19:01:03 -04:00
|
|
|
if (post_check_count != 0) {
|
|
|
|
result = this->erase_sector_fit(post_check_start, post_check_count);
|
|
|
|
WL_EXT_RESULT_CHECK(result);
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|