Skip to content

Commit

Permalink
Merge branch 'refactor/remove_spi_flash_include' into 'master'
Browse files Browse the repository at this point in the history
refactor: remove dependency on spi_flash include for sector size

See merge request espressif/esp-idf!31392
  • Loading branch information
RathiSonika committed Sep 26, 2024
2 parents b8cc161 + d5da858 commit 82b1d5e
Show file tree
Hide file tree
Showing 42 changed files with 198 additions and 136 deletions.
22 changes: 11 additions & 11 deletions components/app_update/esp_ota_ops.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -80,7 +80,7 @@ static const esp_partition_t *read_otadata(esp_ota_select_entry_t *two_otadata)
return NULL;
} else {
memcpy(&two_otadata[0], result, sizeof(esp_ota_select_entry_t));
memcpy(&two_otadata[1], result + SPI_FLASH_SEC_SIZE, sizeof(esp_ota_select_entry_t));
memcpy(&two_otadata[1], result + otadata_partition->erase_size, sizeof(esp_ota_select_entry_t));
esp_partition_munmap(ota_data_map);
}
return otadata_partition;
Expand Down Expand Up @@ -149,7 +149,7 @@ esp_err_t esp_ota_begin(const esp_partition_t *partition, size_t image_size, esp
if ((image_size == 0) || (image_size == OTA_SIZE_UNKNOWN)) {
ret = esp_partition_erase_range(partition, 0, partition->size);
} else {
const int aligned_erase_size = (image_size + SPI_FLASH_SEC_SIZE - 1) & ~(SPI_FLASH_SEC_SIZE - 1);
const int aligned_erase_size = (image_size + partition->erase_size - 1) & ~(partition->erase_size - 1);
ret = esp_partition_erase_range(partition, 0, aligned_erase_size);
}
if (ret != ESP_OK) {
Expand Down Expand Up @@ -192,14 +192,14 @@ esp_err_t esp_ota_write(esp_ota_handle_t handle, const void *data, size_t size)
if (it->handle == handle) {
if (it->need_erase) {
// must erase the partition before writing to it
uint32_t first_sector = it->wrote_size / SPI_FLASH_SEC_SIZE; // first affected sector
uint32_t last_sector = (it->wrote_size + size - 1) / SPI_FLASH_SEC_SIZE; // last affected sector
uint32_t first_sector = it->wrote_size / it->part->erase_size; // first affected sector
uint32_t last_sector = (it->wrote_size + size - 1) / it->part->erase_size; // last affected sector

ret = ESP_OK;
if ((it->wrote_size % SPI_FLASH_SEC_SIZE) == 0) {
ret = esp_partition_erase_range(it->part, it->wrote_size, ((last_sector - first_sector) + 1) * SPI_FLASH_SEC_SIZE);
if ((it->wrote_size % it->part->erase_size) == 0) {
ret = esp_partition_erase_range(it->part, it->wrote_size, ((last_sector - first_sector) + 1) * it->part->erase_size);
} else if (first_sector != last_sector) {
ret = esp_partition_erase_range(it->part, (first_sector + 1) * SPI_FLASH_SEC_SIZE, (last_sector - first_sector) * SPI_FLASH_SEC_SIZE);
ret = esp_partition_erase_range(it->part, (first_sector + 1) * it->part->erase_size, (last_sector - first_sector) * it->part->erase_size);
}
if (ret != ESP_OK) {
return ret;
Expand Down Expand Up @@ -369,11 +369,11 @@ static esp_err_t rewrite_ota_seq(esp_ota_select_entry_t *two_otadata, uint32_t s

two_otadata[sec_id].ota_seq = seq;
two_otadata[sec_id].crc = bootloader_common_ota_select_crc(&two_otadata[sec_id]);
esp_err_t ret = esp_partition_erase_range(ota_data_partition, sec_id * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);
esp_err_t ret = esp_partition_erase_range(ota_data_partition, sec_id * ota_data_partition->erase_size, ota_data_partition->erase_size);
if (ret != ESP_OK) {
return ret;
} else {
return esp_partition_write(ota_data_partition, SPI_FLASH_SEC_SIZE * sec_id, &two_otadata[sec_id], sizeof(esp_ota_select_entry_t));
return esp_partition_write(ota_data_partition, ota_data_partition->erase_size * sec_id, &two_otadata[sec_id], sizeof(esp_ota_select_entry_t));
}
}

Expand Down Expand Up @@ -906,7 +906,7 @@ esp_err_t esp_ota_erase_last_boot_app_partition(void)
}

int sec_id = inactive_otadata;
err = esp_partition_erase_range(ota_data_partition, sec_id * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);
err = esp_partition_erase_range(ota_data_partition, sec_id * ota_data_partition->erase_size, ota_data_partition->erase_size);
if (err != ESP_OK) {
return err;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -177,7 +177,7 @@ static void erase_ota_data(void)
{
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL);
TEST_ASSERT_NOT_EQUAL(NULL, data_partition);
TEST_ESP_OK(esp_partition_erase_range(data_partition, 0, 2 * SPI_FLASH_SEC_SIZE));
TEST_ESP_OK(esp_partition_erase_range(data_partition, 0, 2 * data_partition->erase_size));
}

/* @brief Reboots ESP using mode deep sleep. This mode guaranty that RTC_DATA_ATTR variables is not reset.
Expand Down Expand Up @@ -251,7 +251,7 @@ static void get_ota_data(const esp_partition_t *otadata_partition, esp_ota_selec
TEST_ASSERT_NOT_EQUAL(NULL, ota_select_map);

memcpy(ota_data_0, ota_select_map, sizeof(esp_ota_select_entry_t));
memcpy(ota_data_1, (uint8_t *)ota_select_map + SPI_FLASH_SEC_SIZE, sizeof(esp_ota_select_entry_t));
memcpy(ota_data_1, (uint8_t *)ota_select_map + otadata_partition->erase_size, sizeof(esp_ota_select_entry_t));
bootloader_munmap(ota_select_map);
}
}
Expand All @@ -264,7 +264,7 @@ static void get_ota_data(const esp_partition_t *otadata_partition, esp_ota_selec
*/
static void write_ota_data(const esp_partition_t *otadata_partition, esp_ota_select_entry_t *ota_data, int sec_id)
{
esp_partition_write(otadata_partition, SPI_FLASH_SEC_SIZE * sec_id, &ota_data[sec_id], sizeof(esp_ota_select_entry_t));
esp_partition_write(otadata_partition, otadata_partition->erase_size * sec_id, &ota_data[sec_id], sizeof(esp_ota_select_entry_t));
}

/* @brief Makes a corrupt of ota_data.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -10,7 +10,6 @@
#include "esp_flash_spi_init.h"
#include "test_utils.h"
#include "test_spi_utils.h"
#include "spi_flash_mmap.h"
#include "unity.h"

#if CONFIG_IDF_TARGET_ESP32
Expand Down Expand Up @@ -146,7 +145,7 @@ static void write_large_buffer(esp_flash_t *chip, const esp_partition_t *part, c
{
printf("Erasing chip %p, %d bytes\n", chip, length);

TEST_ESP_OK(esp_flash_erase_region(chip, part->address, (length + SPI_FLASH_SEC_SIZE) & ~(SPI_FLASH_SEC_SIZE - 1)));
TEST_ESP_OK(esp_flash_erase_region(chip, part->address, (length + part->erase_size) & ~(part->erase_size - 1)));

printf("Writing chip %p, %d bytes from source %p\n", chip, length, source);
// note writing to unaligned address
Expand Down Expand Up @@ -195,7 +194,7 @@ void spi_task4(void* arg)

ESP_LOGI(TAG, "Testing chip %p...", chip);
const esp_partition_t *part = get_test_data_partition();
TEST_ASSERT(part->size > test_len + 2 + SPI_FLASH_SEC_SIZE);
TEST_ASSERT(part->size > test_len + 2 + part->erase_size);

write_large_buffer(chip, part, source_buf, test_len);
read_and_check(chip, part, source_buf, test_len);
Expand Down
10 changes: 9 additions & 1 deletion components/esp_partition/include/esp_partition.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -464,6 +464,14 @@ esp_err_t esp_partition_deregister_external(const esp_partition_t* partition);
*/
void esp_partition_unload_all(void);

/**
* @brief Get the main flash sector size
* @return
* - SPI_FLASH_SEC_SIZE - For esp32xx target
* - ESP_PARTITION_EMULATED_SECTOR_SIZE - For linux target
*/
uint32_t esp_partition_get_main_flash_sector_size(void);

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 5 additions & 0 deletions components/esp_partition/partition_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,11 @@ esp_partition_file_mmap_ctrl_t *esp_partition_get_file_mmap_ctrl_act(void)
return &s_esp_partition_file_mmap_ctrl_act;
}

uint32_t esp_partition_get_main_flash_sector_size(void)
{
return ESP_PARTITION_EMULATED_SECTOR_SIZE;
}

#ifdef CONFIG_ESP_PARTITION_ENABLE_STATS
// timing data for ESP8266, 160MHz CPU frequency, 80MHz flash frequency
// all values in microseconds
Expand Down
5 changes: 5 additions & 0 deletions components/esp_partition/partition_target.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,8 @@ bool esp_partition_main_flash_region_safe(size_t addr, size_t size)
}
return true;
}

uint32_t esp_partition_get_main_flash_sector_size(void)
{
return SPI_FLASH_SEC_SIZE;
}
1 change: 0 additions & 1 deletion components/nvs_flash/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ idf_component_register(SRCS "${srcs}"
REQUIRES "${requires}"
PRIV_REQUIRES "${priv_requires}"
INCLUDE_DIRS "include"
"../spi_flash/include"
PRIV_INCLUDE_DIRS "private_include")

# If we use the linux target, we need to redirect the crc functions to the linux
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ idf_component_register(SRCS "test_nvs.cpp"
"../../../private_include"
"../../../../mbedtls/mbedtls/include"
WHOLE_ARCHIVE
REQUIRES nvs_flash)
REQUIRES nvs_flash
PRIV_REQUIRES spi_flash)

if(CMAKE_C_COMPILER_ID MATCHES "Clang")
target_compile_options(${COMPONENT_LIB} PRIVATE -std=gnu++20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <random>
#include <fcntl.h>
#include <unistd.h>
#include "esp_partition.h"

class PartitionEmulationFixture {
public:
Expand All @@ -22,8 +23,9 @@ class PartitionEmulationFixture {
FAIL("Failed to initialize esp_partition_file_mmap");
}

esp_partition.address = start_sector * SPI_FLASH_SEC_SIZE;
esp_partition.size = (start_sector + sector_size) * SPI_FLASH_SEC_SIZE;
const uint32_t sec_size = esp_partition_get_main_flash_sector_size();
esp_partition.address = start_sector * sec_size;
esp_partition.size = (start_sector + sector_size) * sec_size;
esp_partition.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE;
esp_partition.type = ESP_PARTITION_TYPE_DATA;
esp_partition.subtype = ESP_PARTITION_SUBTYPE_DATA_NVS;
Expand All @@ -42,6 +44,7 @@ class PartitionEmulationFixture {
off_t size = -1;
void *p_buff = nullptr;
char const *fail_msg = nullptr;
const uint32_t sec_size = esp_partition_get_main_flash_sector_size();

do {
// get file size
Expand All @@ -57,7 +60,7 @@ class PartitionEmulationFixture {
}

// check if file fits into the partitiion
if (size > sector_size * SPI_FLASH_SEC_SIZE) {
if (size > sector_size * sec_size) {
fail_msg = "file with partition content doesn't fit into the partition";
break;
}
Expand All @@ -82,7 +85,7 @@ class PartitionEmulationFixture {
}

// erase whole partition
if (ESP_OK != esp_partition_erase_range(&esp_partition, 0, sector_size * SPI_FLASH_SEC_SIZE)) {
if (ESP_OK != esp_partition_erase_range(&esp_partition, 0, sector_size * sec_size)) {
fail_msg = "cannot erase partition prior to write partition binary from file";
break;
}
Expand Down Expand Up @@ -123,7 +126,8 @@ class PartitionEmulationFixture {
// absolute sectorNumber is used here
bool erase(size_t sectorNumber)
{
size_t offset = sectorNumber * SPI_FLASH_SEC_SIZE;
const uint32_t sec_size = esp_partition_get_main_flash_sector_size();
size_t offset = sectorNumber * sec_size;

// check the upper bound
esp_partition_file_mmap_ctrl_t *p_ctrl = esp_partition_get_file_mmap_ctrl_act();
Expand All @@ -135,7 +139,7 @@ class PartitionEmulationFixture {
// esp_partition_erase_range uses offset relative to the beginning of partition
return (esp_partition_erase_range(&esp_partition,
offset - esp_partition.address,
SPI_FLASH_SEC_SIZE) == ESP_OK);
sec_size) == ESP_OK);
}

~PartitionEmulationFixture()
Expand Down Expand Up @@ -175,9 +179,10 @@ class PartitionEmulationFixture2 : public PartitionEmulationFixture {
) :
PartitionEmulationFixture(start_sector1, sector_size1, partition_name1), esp_partition2()
{
const uint32_t sec_size = esp_partition_get_main_flash_sector_size();
// for 2nd partition
esp_partition2.address = start_sector2 * SPI_FLASH_SEC_SIZE;
esp_partition2.size = (start_sector2 + sector_size2) * SPI_FLASH_SEC_SIZE;
esp_partition2.address = start_sector2 * sec_size;
esp_partition2.size = (start_sector2 + sector_size2) * sec_size;
esp_partition2.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE;
esp_partition2.type = ESP_PARTITION_TYPE_DATA;
esp_partition2.subtype = ESP_PARTITION_SUBTYPE_DATA_NVS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <string>
#include <random>
#include "test_fixtures.hpp"
#include "spi_flash_mmap.h"

#define TEST_ESP_ERR(rc, res) CHECK((rc) == (res))
#define TEST_ESP_OK(rc) CHECK((rc) == ESP_OK)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -9,6 +9,7 @@
#include "nvs_partition.hpp"
#include "test_fixtures.hpp"
#include <string.h>
#include "esp_partition.h"

TEST_CASE("nvs_flash_init_partition_ptr fails due to nullptr arg", "[nvs_custom_part]")
{
Expand All @@ -22,13 +23,15 @@ TEST_CASE("nvs_flash_init_partition_ptr inits one partition", "[nvs_custom_part]
{
const uint32_t NVS_FLASH_SECTOR = 6;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
const uint32_t sec_size = esp_partition_get_main_flash_sector_size();

uint8_t *p_part_desc_addr_start;
CHECK(esp_partition_file_mmap((const uint8_t **)&p_part_desc_addr_start) == ESP_OK);

esp_partition_t partition = {};
strcpy(partition.label, "test");
partition.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE;
partition.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE;
partition.address = NVS_FLASH_SECTOR * sec_size;
partition.size = NVS_FLASH_SECTOR_COUNT_MIN * sec_size;

CHECK(nvs_flash_init_partition_ptr(&partition) == ESP_OK);
CHECK(nvs::NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ idf_component_register(SRCS "nvs_page_test.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/../../../src"
PRIV_INCLUDE_DIRS
"${CMAKE_CURRENT_SOURCE_DIR}/../../../private_include"
REQUIRES nvs_flash)
REQUIRES nvs_flash
PRIV_REQUIRES spi_flash)

target_compile_options(${COMPONENT_LIB} PUBLIC --coverage)
target_link_libraries(${COMPONENT_LIB} --coverage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ static const char* TAG = "nvs_page_host_test";
#include "unity.h"
#include "test_fixtures.hpp"
#include "esp_log.h"
#include "spi_flash_mmap.h"

#if defined(SEGGER_H) && defined(GLOBAL_H)
NVS_GUARD_SYSVIEW_MACRO_EXPANSION_PUSH();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -37,8 +37,9 @@ class PartitionEmulationFixture {
TEST_FAIL_MESSAGE("Failed to initialize esp_partition_file_mmap");
}

esp_partition.address = start_sector * SPI_FLASH_SEC_SIZE;
esp_partition.size = (start_sector + sector_count) * SPI_FLASH_SEC_SIZE;
const uint32_t sec_size = esp_partition_get_main_flash_sector_size();
esp_partition.address = start_sector * sec_size;
esp_partition.size = (start_sector + sector_count) * sec_size;
esp_partition.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE;
esp_partition.type = ESP_PARTITION_TYPE_DATA;
esp_partition.subtype = ESP_PARTITION_SUBTYPE_DATA_NVS;
Expand Down
3 changes: 2 additions & 1 deletion components/nvs_flash/src/nvs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ extern "C" esp_err_t nvs_flash_init_partition_ptr(const esp_partition_t *partiti
return ESP_ERR_NO_MEM;
}

const uint32_t sec_size = esp_partition_get_main_flash_sector_size();
esp_err_t init_res = NVSPartitionManager::get_instance()->init_custom(part,
0,
partition->size / SPI_FLASH_SEC_SIZE);
partition->size / sec_size);

if (init_res != ESP_OK) {
delete part;
Expand Down
7 changes: 5 additions & 2 deletions components/nvs_flash/src/nvs_page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
#include <cstdio>
#include <cstring>
#include "nvs_internal.h"
#include "esp_partition.h"

namespace nvs {

Page::Page() : mPartition(nullptr) { }

const uint32_t nvs::Page::SEC_SIZE = esp_partition_get_main_flash_sector_size();

uint32_t Page::Header::calculateCrc32()
{
return esp_rom_crc32_le(0xffffffff,
Expand Down Expand Up @@ -49,7 +52,7 @@ esp_err_t Page::load(Partition *partition, uint32_t sectorNumber)
return ESP_ERR_NO_MEM;
}

for (uint32_t i = 0; i < SPI_FLASH_SEC_SIZE; i += 4 * BLOCK_SIZE) {
for (uint32_t i = 0; i < SEC_SIZE; i += 4 * BLOCK_SIZE) {
rc = mPartition->read_raw(mBaseAddress + i, block, 4 * BLOCK_SIZE);
if (rc != ESP_OK) {
mState = PageState::INVALID;
Expand Down Expand Up @@ -1020,7 +1023,7 @@ esp_err_t Page::setVersion(uint8_t ver)

esp_err_t Page::erase()
{
auto rc = mPartition->erase_range(mBaseAddress, SPI_FLASH_SEC_SIZE);
auto rc = mPartition->erase_range(mBaseAddress, SEC_SIZE);
if (rc != ESP_OK) {
mState = PageState::INVALID;
return rc;
Expand Down
Loading

0 comments on commit 82b1d5e

Please sign in to comment.