Skip to content

Commit d5da858

Browse files
committed
refactor: remove dependency on spi_flash include for sector size
1 parent f18eb5a commit d5da858

File tree

42 files changed

+198
-136
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+198
-136
lines changed

components/app_update/esp_ota_ops.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -80,7 +80,7 @@ static const esp_partition_t *read_otadata(esp_ota_select_entry_t *two_otadata)
8080
return NULL;
8181
} else {
8282
memcpy(&two_otadata[0], result, sizeof(esp_ota_select_entry_t));
83-
memcpy(&two_otadata[1], result + SPI_FLASH_SEC_SIZE, sizeof(esp_ota_select_entry_t));
83+
memcpy(&two_otadata[1], result + otadata_partition->erase_size, sizeof(esp_ota_select_entry_t));
8484
esp_partition_munmap(ota_data_map);
8585
}
8686
return otadata_partition;
@@ -149,7 +149,7 @@ esp_err_t esp_ota_begin(const esp_partition_t *partition, size_t image_size, esp
149149
if ((image_size == 0) || (image_size == OTA_SIZE_UNKNOWN)) {
150150
ret = esp_partition_erase_range(partition, 0, partition->size);
151151
} else {
152-
const int aligned_erase_size = (image_size + SPI_FLASH_SEC_SIZE - 1) & ~(SPI_FLASH_SEC_SIZE - 1);
152+
const int aligned_erase_size = (image_size + partition->erase_size - 1) & ~(partition->erase_size - 1);
153153
ret = esp_partition_erase_range(partition, 0, aligned_erase_size);
154154
}
155155
if (ret != ESP_OK) {
@@ -192,14 +192,14 @@ esp_err_t esp_ota_write(esp_ota_handle_t handle, const void *data, size_t size)
192192
if (it->handle == handle) {
193193
if (it->need_erase) {
194194
// must erase the partition before writing to it
195-
uint32_t first_sector = it->wrote_size / SPI_FLASH_SEC_SIZE; // first affected sector
196-
uint32_t last_sector = (it->wrote_size + size - 1) / SPI_FLASH_SEC_SIZE; // last affected sector
195+
uint32_t first_sector = it->wrote_size / it->part->erase_size; // first affected sector
196+
uint32_t last_sector = (it->wrote_size + size - 1) / it->part->erase_size; // last affected sector
197197

198198
ret = ESP_OK;
199-
if ((it->wrote_size % SPI_FLASH_SEC_SIZE) == 0) {
200-
ret = esp_partition_erase_range(it->part, it->wrote_size, ((last_sector - first_sector) + 1) * SPI_FLASH_SEC_SIZE);
199+
if ((it->wrote_size % it->part->erase_size) == 0) {
200+
ret = esp_partition_erase_range(it->part, it->wrote_size, ((last_sector - first_sector) + 1) * it->part->erase_size);
201201
} else if (first_sector != last_sector) {
202-
ret = esp_partition_erase_range(it->part, (first_sector + 1) * SPI_FLASH_SEC_SIZE, (last_sector - first_sector) * SPI_FLASH_SEC_SIZE);
202+
ret = esp_partition_erase_range(it->part, (first_sector + 1) * it->part->erase_size, (last_sector - first_sector) * it->part->erase_size);
203203
}
204204
if (ret != ESP_OK) {
205205
return ret;
@@ -369,11 +369,11 @@ static esp_err_t rewrite_ota_seq(esp_ota_select_entry_t *two_otadata, uint32_t s
369369

370370
two_otadata[sec_id].ota_seq = seq;
371371
two_otadata[sec_id].crc = bootloader_common_ota_select_crc(&two_otadata[sec_id]);
372-
esp_err_t ret = esp_partition_erase_range(ota_data_partition, sec_id * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);
372+
esp_err_t ret = esp_partition_erase_range(ota_data_partition, sec_id * ota_data_partition->erase_size, ota_data_partition->erase_size);
373373
if (ret != ESP_OK) {
374374
return ret;
375375
} else {
376-
return esp_partition_write(ota_data_partition, SPI_FLASH_SEC_SIZE * sec_id, &two_otadata[sec_id], sizeof(esp_ota_select_entry_t));
376+
return esp_partition_write(ota_data_partition, ota_data_partition->erase_size * sec_id, &two_otadata[sec_id], sizeof(esp_ota_select_entry_t));
377377
}
378378
}
379379

@@ -906,7 +906,7 @@ esp_err_t esp_ota_erase_last_boot_app_partition(void)
906906
}
907907

908908
int sec_id = inactive_otadata;
909-
err = esp_partition_erase_range(ota_data_partition, sec_id * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);
909+
err = esp_partition_erase_range(ota_data_partition, sec_id * ota_data_partition->erase_size, ota_data_partition->erase_size);
910910
if (err != ESP_OK) {
911911
return err;
912912
}

components/app_update/test_apps/test_app_update/main/test_switch_ota.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -177,7 +177,7 @@ static void erase_ota_data(void)
177177
{
178178
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL);
179179
TEST_ASSERT_NOT_EQUAL(NULL, data_partition);
180-
TEST_ESP_OK(esp_partition_erase_range(data_partition, 0, 2 * SPI_FLASH_SEC_SIZE));
180+
TEST_ESP_OK(esp_partition_erase_range(data_partition, 0, 2 * data_partition->erase_size));
181181
}
182182

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

253253
memcpy(ota_data_0, ota_select_map, sizeof(esp_ota_select_entry_t));
254-
memcpy(ota_data_1, (uint8_t *)ota_select_map + SPI_FLASH_SEC_SIZE, sizeof(esp_ota_select_entry_t));
254+
memcpy(ota_data_1, (uint8_t *)ota_select_map + otadata_partition->erase_size, sizeof(esp_ota_select_entry_t));
255255
bootloader_munmap(ota_select_map);
256256
}
257257
}
@@ -264,7 +264,7 @@ static void get_ota_data(const esp_partition_t *otadata_partition, esp_ota_selec
264264
*/
265265
static void write_ota_data(const esp_partition_t *otadata_partition, esp_ota_select_entry_t *ota_data, int sec_id)
266266
{
267-
esp_partition_write(otadata_partition, SPI_FLASH_SEC_SIZE * sec_id, &ota_data[sec_id], sizeof(esp_ota_select_entry_t));
267+
esp_partition_write(otadata_partition, otadata_partition->erase_size * sec_id, &ota_data[sec_id], sizeof(esp_ota_select_entry_t));
268268
}
269269

270270
/* @brief Makes a corrupt of ota_data.

components/esp_driver_spi/test_apps/master/main/test_spi_bus_lock.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -10,7 +10,6 @@
1010
#include "esp_flash_spi_init.h"
1111
#include "test_utils.h"
1212
#include "test_spi_utils.h"
13-
#include "spi_flash_mmap.h"
1413
#include "unity.h"
1514

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

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

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

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

200199
write_large_buffer(chip, part, source_buf, test_len);
201200
read_and_check(chip, part, source_buf, test_len);

components/esp_partition/include/esp_partition.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -464,6 +464,14 @@ esp_err_t esp_partition_deregister_external(const esp_partition_t* partition);
464464
*/
465465
void esp_partition_unload_all(void);
466466

467+
/**
468+
* @brief Get the main flash sector size
469+
* @return
470+
* - SPI_FLASH_SEC_SIZE - For esp32xx target
471+
* - ESP_PARTITION_EMULATED_SECTOR_SIZE - For linux target
472+
*/
473+
uint32_t esp_partition_get_main_flash_sector_size(void);
474+
467475
#ifdef __cplusplus
468476
}
469477
#endif

components/esp_partition/partition_linux.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,11 @@ esp_partition_file_mmap_ctrl_t *esp_partition_get_file_mmap_ctrl_act(void)
547547
return &s_esp_partition_file_mmap_ctrl_act;
548548
}
549549

550+
uint32_t esp_partition_get_main_flash_sector_size(void)
551+
{
552+
return ESP_PARTITION_EMULATED_SECTOR_SIZE;
553+
}
554+
550555
#ifdef CONFIG_ESP_PARTITION_ENABLE_STATS
551556
// timing data for ESP8266, 160MHz CPU frequency, 80MHz flash frequency
552557
// all values in microseconds

components/esp_partition/partition_target.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,8 @@ bool esp_partition_main_flash_region_safe(size_t addr, size_t size)
233233
}
234234
return true;
235235
}
236+
237+
uint32_t esp_partition_get_main_flash_sector_size(void)
238+
{
239+
return SPI_FLASH_SEC_SIZE;
240+
}

components/nvs_flash/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ idf_component_register(SRCS "${srcs}"
2525
REQUIRES "${requires}"
2626
PRIV_REQUIRES "${priv_requires}"
2727
INCLUDE_DIRS "include"
28-
"../spi_flash/include"
2928
PRIV_INCLUDE_DIRS "private_include")
3029

3130
# If we use the linux target, we need to redirect the crc functions to the linux

components/nvs_flash/host_test/nvs_host_test/main/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ idf_component_register(SRCS "test_nvs.cpp"
99
"../../../private_include"
1010
"../../../../mbedtls/mbedtls/include"
1111
WHOLE_ARCHIVE
12-
REQUIRES nvs_flash)
12+
REQUIRES nvs_flash
13+
PRIV_REQUIRES spi_flash)
1314

1415
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1516
target_compile_options(${COMPONENT_LIB} PRIVATE -std=gnu++20)

components/nvs_flash/host_test/nvs_host_test/main/test_fixtures.hpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <random>
1010
#include <fcntl.h>
1111
#include <unistd.h>
12+
#include "esp_partition.h"
1213

1314
class PartitionEmulationFixture {
1415
public:
@@ -22,8 +23,9 @@ class PartitionEmulationFixture {
2223
FAIL("Failed to initialize esp_partition_file_mmap");
2324
}
2425

25-
esp_partition.address = start_sector * SPI_FLASH_SEC_SIZE;
26-
esp_partition.size = (start_sector + sector_size) * SPI_FLASH_SEC_SIZE;
26+
const uint32_t sec_size = esp_partition_get_main_flash_sector_size();
27+
esp_partition.address = start_sector * sec_size;
28+
esp_partition.size = (start_sector + sector_size) * sec_size;
2729
esp_partition.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE;
2830
esp_partition.type = ESP_PARTITION_TYPE_DATA;
2931
esp_partition.subtype = ESP_PARTITION_SUBTYPE_DATA_NVS;
@@ -42,6 +44,7 @@ class PartitionEmulationFixture {
4244
off_t size = -1;
4345
void *p_buff = nullptr;
4446
char const *fail_msg = nullptr;
47+
const uint32_t sec_size = esp_partition_get_main_flash_sector_size();
4548

4649
do {
4750
// get file size
@@ -57,7 +60,7 @@ class PartitionEmulationFixture {
5760
}
5861

5962
// check if file fits into the partitiion
60-
if (size > sector_size * SPI_FLASH_SEC_SIZE) {
63+
if (size > sector_size * sec_size) {
6164
fail_msg = "file with partition content doesn't fit into the partition";
6265
break;
6366
}
@@ -82,7 +85,7 @@ class PartitionEmulationFixture {
8285
}
8386

8487
// erase whole partition
85-
if (ESP_OK != esp_partition_erase_range(&esp_partition, 0, sector_size * SPI_FLASH_SEC_SIZE)) {
88+
if (ESP_OK != esp_partition_erase_range(&esp_partition, 0, sector_size * sec_size)) {
8689
fail_msg = "cannot erase partition prior to write partition binary from file";
8790
break;
8891
}
@@ -123,7 +126,8 @@ class PartitionEmulationFixture {
123126
// absolute sectorNumber is used here
124127
bool erase(size_t sectorNumber)
125128
{
126-
size_t offset = sectorNumber * SPI_FLASH_SEC_SIZE;
129+
const uint32_t sec_size = esp_partition_get_main_flash_sector_size();
130+
size_t offset = sectorNumber * sec_size;
127131

128132
// check the upper bound
129133
esp_partition_file_mmap_ctrl_t *p_ctrl = esp_partition_get_file_mmap_ctrl_act();
@@ -135,7 +139,7 @@ class PartitionEmulationFixture {
135139
// esp_partition_erase_range uses offset relative to the beginning of partition
136140
return (esp_partition_erase_range(&esp_partition,
137141
offset - esp_partition.address,
138-
SPI_FLASH_SEC_SIZE) == ESP_OK);
142+
sec_size) == ESP_OK);
139143
}
140144

141145
~PartitionEmulationFixture()
@@ -175,9 +179,10 @@ class PartitionEmulationFixture2 : public PartitionEmulationFixture {
175179
) :
176180
PartitionEmulationFixture(start_sector1, sector_size1, partition_name1), esp_partition2()
177181
{
182+
const uint32_t sec_size = esp_partition_get_main_flash_sector_size();
178183
// for 2nd partition
179-
esp_partition2.address = start_sector2 * SPI_FLASH_SEC_SIZE;
180-
esp_partition2.size = (start_sector2 + sector_size2) * SPI_FLASH_SEC_SIZE;
184+
esp_partition2.address = start_sector2 * sec_size;
185+
esp_partition2.size = (start_sector2 + sector_size2) * sec_size;
181186
esp_partition2.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE;
182187
esp_partition2.type = ESP_PARTITION_TYPE_DATA;
183188
esp_partition2.subtype = ESP_PARTITION_SUBTYPE_DATA_NVS;

components/nvs_flash/host_test/nvs_host_test/main/test_nvs.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <string>
1919
#include <random>
2020
#include "test_fixtures.hpp"
21+
#include "spi_flash_mmap.h"
2122

2223
#define TEST_ESP_ERR(rc, res) CHECK((rc) == (res))
2324
#define TEST_ESP_OK(rc) CHECK((rc) == ESP_OK)

0 commit comments

Comments
 (0)