spi_flash: Linux target emulation of Partition API

Emulator of Partition API layer for Linux OS
This commit is contained in:
Martin Vychodil
2022-01-28 18:16:13 +01:00
committed by BOT
parent 19ddb8bde1
commit 9a5f39ec2c
16 changed files with 975 additions and 435 deletions

View File

@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(COMPONENTS main)
# Freertos is included via common components, however, currently only the mock component is compatible with linux
# target.
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/")
project(partition_api_test)

View File

@@ -0,0 +1,18 @@
| Supported Targets | Linux |
| ----------------- | ----- |
This is a test project for partition-related APIs on Linux target (CONFIG_IDF_TARGET_LINUX).
# Build
Source the IDF environment as usual.
Once this is done, build the application:
```bash
idf.py build partition-table
```
Note that for the time being, `partition-table` target needs to be built manually.
# Run
```bash
`build/partition_api_test.elf`
```

View File

@@ -0,0 +1,2 @@
idf_component_register(SRCS "partition_api_test.cpp"
REQUIRES spi_flash)

View File

@@ -0,0 +1,106 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*
* Linux host partition API test
*/
#include <string.h>
#include "esp_err.h"
#include "esp_partition.h"
#include "esp_private/partition_linux.h"
int main(int argc, char **argv)
{
printf("Partition API Linux emulation test: ");
////////////////////////////////////////
//PARTITION LOOKUP:
//1. esp_partition_find (label=STORAGE)
esp_partition_iterator_t iter = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage");
assert(iter);
//2. esp_partition_get (label=STORAGE)
const esp_partition_t *part = esp_partition_get(iter);
assert(part);
//3. esp_partition_iterator_release (label STORAGE iter): assumed OK
esp_partition_iterator_release(iter);
////////////////////////////////////////
//ITERATORS, PARTITION PROPERTIES:
//4. esp_partition_find_first (type=APP, subtype=ANY)
const esp_partition_t *partition_app = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, NULL);
assert(partition_app);
//5. enumerate all APP partitions
iter = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, NULL);
assert(iter);
size_t counter = 0;
while (iter != NULL) {
const esp_partition_t *part_data = esp_partition_get(iter);
counter++;
assert(part_data);
iter = esp_partition_next(iter);
}
esp_partition_iterator_release(iter);
//6. enumerate all DATA partitions and print details for each
iter = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, NULL);
assert(iter);
counter = 0;
while (iter != NULL) {
const esp_partition_t *part_data = esp_partition_get(iter);
counter++;
assert(part_data);
iter = esp_partition_next(iter);
}
esp_partition_iterator_release(iter);
//7. esp_partition_find_first (type=DATA, label=STORAGE)
const esp_partition_t *partition_data = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage");
assert(partition_data);
/////////////////////////////////////
//OPERATIONS
uint8_t buff[] = "ABCDEFGHIJKLMNOP";
size_t bufsize = sizeof(buff);
size_t off = 0x100;
//8. esp_partition_write/raw
esp_err_t err = esp_partition_write(partition_data, off, (const void *)buff, bufsize);
assert(err == ESP_OK);
//9. esp_partition_read/raw
uint8_t buffout[32] = {0};
err = esp_partition_read(partition_data, off, (void *)buffout, bufsize);
assert(err == ESP_OK);
//10. esp_partition_erase_range
uint8_t buferase[bufsize];
memset(buferase, 0xFF, bufsize);
memset(buffout, 0, sizeof(buffout));
size_t sector_off = 0; //erase works per whole sector - offset must be aligned to 4kB boundaries
err = esp_partition_erase_range(partition_data, sector_off, SPI_FLASH_SEC_SIZE);
assert(esp_partition_read(partition_data, off, (void *)buffout, bufsize) == ESP_OK);
assert(err == ESP_OK && memcmp(buffout, buferase, bufsize) == 0);
//11. esp_partition_verify (partition_data)
const esp_partition_t *verified_partition = esp_partition_verify(partition_data);
assert(verified_partition != NULL);
//12. release SPI FLASH emulation block from memory
err = esp_partition_file_munmap();
assert(err == ESP_OK);
printf("OK\n");
return 0;
}

View File

@@ -0,0 +1,6 @@
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
storage, data, , , 0x40000,
1 # Name, Type, SubType, Offset, Size, Flags
2 # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3 nvs, data, nvs, 0x9000, 0x6000,
4 phy_init, data, phy, 0xf000, 0x1000,
5 factory, app, factory, 0x10000, 1M,
6 storage, data, , , 0x40000,

View File

@@ -0,0 +1,7 @@
CONFIG_IDF_TARGET="linux"
CONFIG_COMPILER_CXX_EXCEPTIONS=y
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partition_table.csv"
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y