2022-02-21 16:09:24 -05:00
|
|
|
/*
|
2023-10-17 10:13:32 -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 "esp_log.h"
|
|
|
|
#include "SPI_Flash.h"
|
2022-06-27 03:16:50 -04:00
|
|
|
#include "spi_flash_mmap.h"
|
2022-06-27 03:24:07 -04:00
|
|
|
#include "esp_flash.h"
|
2023-10-17 10:13:32 -04:00
|
|
|
#include <inttypes.h>
|
2022-06-27 03:24:07 -04:00
|
|
|
|
2018-07-04 19:01:03 -04:00
|
|
|
static const char *TAG = "spi_flash";
|
|
|
|
|
|
|
|
SPI_Flash::SPI_Flash()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
size_t SPI_Flash::get_flash_size()
|
2018-07-04 19:01:03 -04:00
|
|
|
{
|
2022-06-27 03:24:07 -04:00
|
|
|
uint32_t chip_size;
|
|
|
|
esp_flash_get_size(NULL, &chip_size);
|
|
|
|
return chip_size;
|
2018-07-04 19:01:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t SPI_Flash::erase_sector(size_t sector)
|
|
|
|
{
|
2022-06-27 03:24:07 -04:00
|
|
|
esp_err_t result = esp_flash_erase_region(NULL, sector * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);;
|
2018-07-04 19:01:03 -04:00
|
|
|
if (result == ESP_OK) {
|
2023-10-17 10:13:32 -04:00
|
|
|
ESP_LOGV(TAG, "erase_sector - sector=0x%08" PRIx32 ", result=0x%08x", (uint32_t) sector, result);
|
2018-07-04 19:01:03 -04:00
|
|
|
} else {
|
2023-10-17 10:13:32 -04:00
|
|
|
ESP_LOGE(TAG, "erase_sector - sector=0x%08" PRIx32 ", result=0x%08x", (uint32_t) sector, result);
|
2018-07-04 19:01:03 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
esp_err_t SPI_Flash::erase_range(size_t start_address, size_t size)
|
|
|
|
{
|
|
|
|
size = (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
|
|
|
|
size = size * SPI_FLASH_SEC_SIZE;
|
2022-06-27 03:24:07 -04:00
|
|
|
esp_err_t result = esp_flash_erase_region(NULL, start_address, size);
|
2018-07-04 19:01:03 -04:00
|
|
|
if (result == ESP_OK) {
|
2023-10-17 10:13:32 -04:00
|
|
|
ESP_LOGV(TAG, "erase_range - start_address=0x%08" PRIx32 ", size=0x%08" PRIx32 ", result=0x%08x", (uint32_t) start_address, (uint32_t) size, result);
|
2018-07-04 19:01:03 -04:00
|
|
|
} else {
|
2023-10-17 10:13:32 -04:00
|
|
|
ESP_LOGE(TAG, "erase_range - start_address=0x%08" PRIx32 ", size=0x%08" PRIx32 ", result=0x%08x", (uint32_t) start_address, (uint32_t) size, result);
|
2018-07-04 19:01:03 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t SPI_Flash::write(size_t dest_addr, const void *src, size_t size)
|
|
|
|
{
|
2022-06-27 03:24:07 -04:00
|
|
|
esp_err_t result = esp_flash_write(NULL, src, dest_addr, size);
|
2018-07-04 19:01:03 -04:00
|
|
|
if (result == ESP_OK) {
|
2023-10-17 10:13:32 -04:00
|
|
|
ESP_LOGV(TAG, "write - dest_addr=0x%08" PRIx32 ", size=0x%08" PRIx32 ", result=0x%08x", (uint32_t) dest_addr, (uint32_t) size, result);
|
2018-07-04 19:01:03 -04:00
|
|
|
} else {
|
2023-10-17 10:13:32 -04:00
|
|
|
ESP_LOGE(TAG, "write - dest_addr=0x%08" PRIx32 ", size=0x%08" PRIx32 ", result=0x%08x", (uint32_t) dest_addr, (uint32_t) size, result);
|
2018-07-04 19:01:03 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t SPI_Flash::read(size_t src_addr, void *dest, size_t size)
|
|
|
|
{
|
2022-06-27 03:24:07 -04:00
|
|
|
esp_err_t result = esp_flash_read(NULL, dest, src_addr, size);
|
2018-07-04 19:01:03 -04:00
|
|
|
if (result == ESP_OK) {
|
2023-10-17 10:13:32 -04:00
|
|
|
ESP_LOGV(TAG, "read - src_addr=0x%08" PRIx32 ", size=0x%08" PRIx32 ", result=0x%08x", (uint32_t) src_addr, (uint32_t) size, result);
|
2018-07-04 19:01:03 -04:00
|
|
|
} else {
|
2023-10-17 10:13:32 -04:00
|
|
|
ESP_LOGE(TAG, "read - src_addr=0x%08" PRIx32 ", size=0x%08" PRIx32 ", result=0x%08x", (uint32_t) src_addr, (uint32_t) size, result);
|
2018-07-04 19:01:03 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:15:43 -04:00
|
|
|
size_t SPI_Flash::get_sector_size()
|
2018-07-04 19:01:03 -04:00
|
|
|
{
|
|
|
|
return SPI_FLASH_SEC_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPI_Flash::~SPI_Flash()
|
|
|
|
{
|
|
|
|
}
|