From 0aab17eb8bf6ff2a6d6c75c6fb22e5ff6b92d68e Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Sun, 30 Jul 2023 17:21:24 +0200 Subject: [PATCH] Improving abstraction for flash module --- Configurations/ESP32-8048S070.ini | 4 + Configurations/Linux.ini | 3 + Configurations/WT32-SC01.ini | 4 + Configurations/WT32-SC01_Plus.ini | 4 + lib/Xila/include/Configuration/Preset.hpp | 0 lib/Xila/include/Flash/Flash.hpp | 94 +++------------ lib/Xila/include/Power/Power.hpp | 2 - lib/Xila/src/Flash/Drivers/ESP32.cpp | 129 ++++++++++++++++++++ lib/Xila/src/Flash/Drivers/None.cpp | 88 ++++++++++++++ lib/Xila/src/Flash/Flash.cpp | 141 ---------------------- 10 files changed, 251 insertions(+), 218 deletions(-) create mode 100644 lib/Xila/include/Configuration/Preset.hpp create mode 100644 lib/Xila/src/Flash/Drivers/ESP32.cpp create mode 100644 lib/Xila/src/Flash/Drivers/None.cpp delete mode 100644 lib/Xila/src/Flash/Flash.cpp diff --git a/Configurations/ESP32-8048S070.ini b/Configurations/ESP32-8048S070.ini index fbe1ab44..d550e7eb 100644 --- a/Configurations/ESP32-8048S070.ini +++ b/Configurations/ESP32-8048S070.ini @@ -27,6 +27,10 @@ build_flags = ; - Display -D Xila_Display_Hardware_ESP32_8048S070 + ; Flash + + -D Xila_Flash_Hardware_ESP32 + ; Drive -D Xila_Drive_Hardware_SD_SPI -D SD_SPI_Clock_Pin=12 diff --git a/Configurations/Linux.ini b/Configurations/Linux.ini index e6889371..2385e904 100644 --- a/Configurations/Linux.ini +++ b/Configurations/Linux.ini @@ -33,6 +33,9 @@ build_flags = ; - Board -D Xila_Board_Hardware_Wireless_Tag_WT32_SC01_Plus + ; - Flash + -D Xila_Flash_Hardware_None + ; - Display -D Xila_Display_Hardware_Wireless_Tag_WT32_SC01_Plus diff --git a/Configurations/WT32-SC01.ini b/Configurations/WT32-SC01.ini index 87e23507..85e4efad 100644 --- a/Configurations/WT32-SC01.ini +++ b/Configurations/WT32-SC01.ini @@ -33,6 +33,10 @@ build_flags = -D Xila_Processor_Hardware_Register_Size=32 + ; Flash + + -D Xila_Flash_Hardware_ESP32 + ; Display -D Xila_Display_Hardware_Wireless_Tag_WT32_SC01 diff --git a/Configurations/WT32-SC01_Plus.ini b/Configurations/WT32-SC01_Plus.ini index de01951f..449abf42 100644 --- a/Configurations/WT32-SC01_Plus.ini +++ b/Configurations/WT32-SC01_Plus.ini @@ -30,6 +30,10 @@ build_flags = ; - Display -D Xila_Display_Hardware_Wireless_Tag_WT32_SC01_Plus + ; Flash + + -D Xila_Flash_Hardware_ESP32 + ; Drive -D Xila_Drive_Hardware_SD_SPI -D SD_SPI_Clock_Pin=39 diff --git a/lib/Xila/include/Configuration/Preset.hpp b/lib/Xila/include/Configuration/Preset.hpp new file mode 100644 index 00000000..e69de29b diff --git a/lib/Xila/include/Flash/Flash.hpp b/lib/Xila/include/Flash/Flash.hpp index 91047515..9e9ca56b 100644 --- a/lib/Xila/include/Flash/Flash.hpp +++ b/lib/Xila/include/Flash/Flash.hpp @@ -11,20 +11,7 @@ #ifndef Flash_Hpp_Included #define Flash_Hpp_Included -#include "Esp.h" -#include "rom/spi_flash.h" -#include "esp_sleep.h" -#include "esp_spi_flash.h" -#include -#include -#include -#include -extern "C" -{ -#include "esp_ota_ops.h" -#include "esp_image_format.h" -} -#include + #include "../Module/Module.hpp" @@ -37,56 +24,7 @@ namespace Xila_Namespace { namespace Flash_Types { - /// @brief Flash partition type. - typedef enum Partition_Subtype_Enumeration - { - Bootloader_Partition = ESP_PARTITION_SUBTYPE_APP_FACTORY, //!< Factory application partition - OTA_Minimum = ESP_PARTITION_SUBTYPE_APP_OTA_MIN, //!< Base for OTA partition subtypes - Xila_Partition = ESP_PARTITION_SUBTYPE_APP_OTA_0, //!< OTA partition 0 - OTA_1 = ESP_PARTITION_SUBTYPE_APP_OTA_1, //!< OTA partition 1 - OTA_2 = ESP_PARTITION_SUBTYPE_APP_OTA_2, //!< OTA partition 2 - OTA_3 = ESP_PARTITION_SUBTYPE_APP_OTA_3, //!< OTA partition 3 - OTA_4 = ESP_PARTITION_SUBTYPE_APP_OTA_4, //!< OTA partition 4 - OTA_5 = ESP_PARTITION_SUBTYPE_APP_OTA_5, //!< OTA partition 5 - OTA_6 = ESP_PARTITION_SUBTYPE_APP_OTA_6, //!< OTA partition 6 - OTA_7 = ESP_PARTITION_SUBTYPE_APP_OTA_7, //!< OTA partition 7 - OTA_8 = ESP_PARTITION_SUBTYPE_APP_OTA_8, //!< OTA partition 8 - OTA_9 = ESP_PARTITION_SUBTYPE_APP_OTA_9, //!< OTA partition 9 - OTA_10 = ESP_PARTITION_SUBTYPE_APP_OTA_10, //!< OTA partition 10 - OTA_11 = ESP_PARTITION_SUBTYPE_APP_OTA_11, //!< OTA partition 11 - OTA_12 = ESP_PARTITION_SUBTYPE_APP_OTA_12, //!< OTA partition 12 - OTA_13 = ESP_PARTITION_SUBTYPE_APP_OTA_13, //!< OTA partition 13 - OTA_14 = ESP_PARTITION_SUBTYPE_APP_OTA_14, //!< OTA partition 14 - OTA_15 = ESP_PARTITION_SUBTYPE_APP_OTA_15, //!< OTA partition 15 - OTA_Maximum = ESP_PARTITION_SUBTYPE_APP_OTA_MAX, //!< Max subtype of OTA partition - Application_Test = ESP_PARTITION_SUBTYPE_APP_TEST, //!< Test application partition - - Data_OTA = ESP_PARTITION_SUBTYPE_DATA_OTA, //!< OTA selection partition - Data_PHY = ESP_PARTITION_SUBTYPE_DATA_PHY, //!< PHY init data partition - Data_NVS = ESP_PARTITION_SUBTYPE_DATA_NVS, //!< NVS partition - Data_Core_Dump = ESP_PARTITION_SUBTYPE_DATA_COREDUMP, //!< COREDUMP partition - Data_NVS_Keys = ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS, //!< Partition for NVS keys - Data_eFuse = ESP_PARTITION_SUBTYPE_DATA_EFUSE_EM, //!< Partition for emulate eFuse bits - Data_Undefined = ESP_PARTITION_SUBTYPE_DATA_UNDEFINED, //!< Undefined (or unspecified) data partition - - Data_ESPHTTPD = ESP_PARTITION_SUBTYPE_DATA_ESPHTTPD, //!< ESPHTTPD partition - Data_Fat = ESP_PARTITION_SUBTYPE_DATA_FAT, //!< FAT partition - Data_SPIFFS = ESP_PARTITION_SUBTYPE_DATA_SPIFFS, //!< SPIFFS partition - - Any = ESP_PARTITION_SUBTYPE_ANY, //!< Used to search for partitions with any subtype - } Partition_Subtype_Type; - - /// @brief Flash mode type - enum class Flash_Mode_Type - { - QIO = FM_QIO, - QOUT = FM_QOUT, - DIO = FM_DIO, - DOUT = FM_DOUT, - Fast_Read = FM_FAST_READ, - Slow_Read = FM_SLOW_READ, - Unknown = FM_UNKNOWN - }; + typedef void Partition_Type; } /// @brief Flash module class. @@ -100,22 +38,22 @@ namespace Xila_Namespace /// @brief Get the size the of the flash. /// @return Size in bytes. - uint32_t Get_Size(); + Size_Type Get_Size(); /// @brief Get the flash speed. /// @return Speed in hertz. - uint32_t Get_Speed(); + QWord_Type Get_Speed(); /// @brief Get flash chip mode. /// @return Flash_Mode_Type - Flash_Types::Flash_Mode_Type Get_Mode(); + Natural_Type Get_Mode(); /// @brief Read data from flash chip. /// @param Offset Offset of the data to read. /// @param Data Data buffer pointer. /// @param Size Size of the data to read. /// @return Result_Type - Result_Type Read(uint32_t Offset, uint32_t *Data, size_t Size); + Result_Type Read(uint32_t Offset, uint32_t *Data, Size_Type Size); /// @brief Read data from partition. /// @param Partition Partition to read from. @@ -123,7 +61,7 @@ namespace Xila_Namespace /// @param Data Data buffer pointer. /// @param Size Size of the data to read. /// @return Result_Type - Result_Type Partition_Read(const esp_partition_t *Partition, uint32_t Offset, uint32_t *Data, size_t Size); + Result_Type Partition_Read(const Flash_Types::Partition_Type *Partition, uint32_t Offset, uint32_t *Data, Size_Type Size); /// @brief Get the sketch size. /// @@ -139,17 +77,23 @@ namespace Xila_Namespace /// @return Free space int bytes. Size_Type Get_Sketch_Free_Space(); + /// @brief Find a partition according to the given parameters. + /// @param Type Partition type. + /// @param Subtype Partition subtype. + /// @param Label Partition label. + /// @param Index Partition index. + /// @return Flash_Types::Partition_Type* Pointer to the partition. + const Flash_Types::Partition_Type *Find(Natural_Type Type, Natural_Type Subtype, const char *Label = NULL, Natural_Type Index = 0); + private: // -- Methods - Result_Type Set_Boot_Partition(Flash_Types::Partition_Subtype_Type Partition_Subtype); + Result_Type Set_Boot_Partition(uint8_t Partition_Subtype); Result_Type Erase_Sector(uint32_t Sector); - Result_Type Write(uint32_t Offset, uint32_t *Data, size_t Size); - - Result_Type Partition_Write(const esp_partition_t *Partition, uint32_t Offset_, uint32_t *Data, size_t Size); - Result_Type Partition_Erase_Range(const esp_partition_t *Partition, uint32_t Offset, size_t Size); + Result_Type Write(uint32_t Offset, uint32_t *Data, Size_Type Size); - static uint32_t Sketch_Size(sketchSize_t Response); + Result_Type Partition_Write(const Flash_Types::Partition_Type *Partition, uint32_t Offset_, uint32_t *Data, Size_Type Size); + Result_Type Partition_Erase_Range(const Flash_Types::Partition_Type *Partition, uint32_t Offset, Size_Type Size); friend class System_Class; } Flash_Type; diff --git a/lib/Xila/include/Power/Power.hpp b/lib/Xila/include/Power/Power.hpp index b4c57f20..8aea1007 100644 --- a/lib/Xila/include/Power/Power.hpp +++ b/lib/Xila/include/Power/Power.hpp @@ -13,8 +13,6 @@ #include "../Module/Module.hpp" -#include "Battery_Library.hpp" - //==============================================================================// /// /// @brief Power management module. diff --git a/lib/Xila/src/Flash/Drivers/ESP32.cpp b/lib/Xila/src/Flash/Drivers/ESP32.cpp new file mode 100644 index 00000000..bd4f05d1 --- /dev/null +++ b/lib/Xila/src/Flash/Drivers/ESP32.cpp @@ -0,0 +1,129 @@ +/// @file Flash.cpp +/// @author Alix ANNERAUD (alix.anneraud@outlook.fr) +/// @brief +/// @version 0.1 +/// @date 11-07-2021 +/// +/// @copyright Copyright (c) 2021 + +#ifdef Xila_Flash_Hardware_ESP32 + +#include "Flash/Flash.hpp" + +#include "Esp.h" +#include "rom/spi_flash.h" +#include "esp_sleep.h" +#include "esp_spi_flash.h" +#include +#include +#include +#include +extern "C" +{ +#include "esp_ota_ops.h" +#include "esp_image_format.h" +} +#include + +using namespace Xila_Namespace; +using namespace Xila_Namespace::Flash_Types; + +Flash_Type Xila_Namespace::Flash; + +Size_Type Flash_Class::Get_Sketch_Size() +{ + return ESP.getSketchSize(); +} + +Result_Type Flash_Class::Read(uint32_t Offset, uint32_t *Data, Size_Type Size) +{ + if (spi_flash_read(Offset, (uint32_t *)Data, Size) != ESP_OK) + return Result_Type::Error; + + return Result_Type::Success; +} + +Result_Type Flash_Class::Partition_Read(const Partition_Type *Partition, uint32_t Offset, uint32_t *Data, Size_Type Size) +{ + if (esp_partition_read((const esp_partition_t *)Partition, Offset, Data, Size) != ESP_OK) + return Result_Type::Error; + return Result_Type::Success; +} + +Result_Type Flash_Class::Erase_Sector(uint32_t Sector) +{ + if (spi_flash_erase_sector(Sector) != ESP_OK) + return Result_Type::Error; + return Result_Type::Success; +} + +Result_Type Flash_Class::Write(uint32_t Offset, uint32_t *Data, Size_Type Size) +{ + if (spi_flash_write(Offset, (uint32_t *)Data, Size) != ESP_OK) + return Result_Type::Error; + return Result_Type::Success; +} + +Result_Type Flash_Class::Partition_Erase_Range(const Partition_Type *Partition, uint32_t Offset, Size_Type Size) +{ + if (esp_partition_erase_range((const esp_partition_t *)Partition, Offset, Size) != ESP_OK) + return Result_Type::Error; + return Result_Type::Success; +} + +Result_Type Flash_Class::Partition_Write(const Partition_Type *Partition, uint32_t Offset, uint32_t *Data, Size_Type Size) +{ + if (esp_partition_write((const esp_partition_t *)Partition, Offset, Data, Size) != ESP_OK) + return Result_Type::Error; + return Result_Type::Success; +} + +Result_Type Flash_Class::Set_Boot_Partition(uint8_t Partition_Subtype) +{ + if (esp_ota_set_boot_partition(esp_partition_find_first(ESP_PARTITION_TYPE_APP, (esp_partition_subtype_t)Partition_Subtype, NULL)) != ESP_OK) + return Result_Type::Error; + return Result_Type::Success; +} + +Size_Type Flash_Class::Get_Sketch_Free_Space() +{ + return ESP.getFreeSketchSpace(); +} + +String_Type& Flash_Class::Get_Sketch_MD5(String_Type &String) +{ + String = ESP.getSketchMD5().c_str(); + return String; +} + +Size_Type Flash_Class::Get_Size() +{ + return ESP.getFlashChipSize(); +} + +QWord_Type Flash_Class::Get_Speed() +{ + return ESP.getFlashChipSpeed(); +} + +Natural_Type Flash_Class::Get_Mode() +{ + return ESP.getFlashChipMode(); +} + +const Partition_Type *Flash_Class::Find(Natural_Type Type, Natural_Type Subtype, const char *Label, Natural_Type Index) +{ + if (Index == 0) + return esp_partition_find_first((esp_partition_type_t)Type, (esp_partition_subtype_t)Subtype, Label); + + auto Partition_Iterator = esp_partition_find((esp_partition_type_t)Type, (esp_partition_subtype_t)Subtype, Label); + for (uint8_t i = 0; i < Index; i++) + Partition_Iterator = esp_partition_next(Partition_Iterator); + + auto Partition = esp_partition_get(Partition_Iterator); + esp_partition_iterator_release(Partition_Iterator); + + return Partition; +} + +#endif \ No newline at end of file diff --git a/lib/Xila/src/Flash/Drivers/None.cpp b/lib/Xila/src/Flash/Drivers/None.cpp new file mode 100644 index 00000000..3b66320f --- /dev/null +++ b/lib/Xila/src/Flash/Drivers/None.cpp @@ -0,0 +1,88 @@ +/// @file Flash.cpp +/// @author Alix ANNERAUD (alix.anneraud@outlook.fr) +/// @brief +/// @version 0.1 +/// @date 11-07-2021 +/// +/// @copyright Copyright (c) 2021 + +#ifdef Xila_Flash_Hardware_None + +#include "Flash/Flash.hpp" + +using namespace Xila_Namespace; +using namespace Xila_Namespace::Flash_Types; + +Flash_Type Xila_Namespace::Flash; + +Size_Type Flash_Class::Get_Sketch_Size() +{ + return 0; +} + +Result_Type Flash_Class::Read(uint32_t Offset, uint32_t *Data, Size_Type Size) +{ + return Result_Type::Error; +} + +Result_Type Flash_Class::Partition_Read(const Partition_Type *Partition, uint32_t Offset, uint32_t *Data, Size_Type Size) +{ + return Result_Type::Error; +} + +Result_Type Flash_Class::Erase_Sector(uint32_t Sector) +{ + return Result_Type::Error; +} + +Result_Type Flash_Class::Write(uint32_t Offset, uint32_t *Data, Size_Type Size) +{ + return Result_Type::Error; +} + +Result_Type Flash_Class::Partition_Erase_Range(const Partition_Type *Partition, uint32_t Offset, Size_Type Size) +{ + return Result_Type::Error; +} + +Result_Type Flash_Class::Partition_Write(const Partition_Type *Partition, uint32_t Offset, uint32_t *Data, Size_Type Size) +{ + return Result_Type::Error; +} + +Result_Type Flash_Class::Set_Boot_Partition(uint8_t Partition_Subtype) +{ + return Result_Type::Error; +} + +Size_Type Flash_Class::Get_Sketch_Free_Space() +{ + return 0; +} + +String_Type &Flash_Class::Get_Sketch_MD5(String_Type &String) +{ + return String; +} + +Size_Type Flash_Class::Get_Size() +{ + return 0; +} + +QWord_Type Flash_Class::Get_Speed() +{ + return 0; +} + +Natural_Type Flash_Class::Get_Mode() +{ + return 0; +} + +const Partition_Type *Flash_Class::Find(Natural_Type Type, Natural_Type Subtype, const char *Label, Natural_Type Index) +{ + return NULL; +} + +#endif \ No newline at end of file diff --git a/lib/Xila/src/Flash/Flash.cpp b/lib/Xila/src/Flash/Flash.cpp deleted file mode 100644 index 044246f6..00000000 --- a/lib/Xila/src/Flash/Flash.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/// @file Flash.cpp -/// @author Alix ANNERAUD (alix.anneraud@outlook.fr) -/// @brief -/// @version 0.1 -/// @date 11-07-2021 -/// -/// @copyright Copyright (c) 2021 - -#include "Flash/Flash.hpp" - -#include "esp_partition.h" - -using namespace Xila_Namespace; -using namespace Xila_Namespace::Flash_Types; - -Flash_Type Xila_Namespace::Flash; - -uint32_t Xila_Namespace::Flash_Class::Get_Sketch_Size() -{ - return Sketch_Size(SKETCH_SIZE_TOTAL); -} - -Result_Type Xila_Namespace::Flash_Class::Read(uint32_t Offset, uint32_t *Data, size_t Size) -{ - if (spi_flash_read(Offset, (uint32_t *)Data, Size) == ESP_OK) - { - return Result_Type::Success; - } - return Result_Type::Error; -} - -Result_Type Xila_Namespace::Flash_Class::Partition_Read(const esp_partition_t *Partition, uint32_t Offset, uint32_t *Data, size_t Size) -{ - if (esp_partition_read(Partition, Offset, Data, Size) == ESP_OK) - { - return Result_Type::Success; - } - return Result_Type::Error; -} - -Result_Type Xila_Namespace::Flash_Class::Erase_Sector(uint32_t Sector) -{ - if (spi_flash_erase_sector(Sector) == ESP_OK) - { - return Result_Type::Success; - } - return Result_Type::Error; -} - -Result_Type Xila_Namespace::Flash_Class::Write(uint32_t Offset, uint32_t *Data, size_t Size) -{ - if (spi_flash_write(Offset, (uint32_t *)Data, Size) == ESP_OK) - { - return Result_Type::Success; - } - return Result_Type::Error; -} - -Result_Type Xila_Namespace::Flash_Class::Partition_Erase_Range(const esp_partition_t *Partition, uint32_t Offset, size_t Size) -{ - if (esp_partition_erase_range(Partition, Offset, Size) == ESP_OK) - { - return Result_Type::Success; - } - return Result_Type::Error; -} - -Result_Type Xila_Namespace::Flash_Class::Partition_Write(const esp_partition_t *Partition, uint32_t Offset, uint32_t *Data, size_t Size) -{ - if (esp_partition_write(Partition, Offset, Data, Size) == ESP_OK) - { - return Result_Type::Success; - } - return Result_Type::Error; -} - -Result_Type Xila_Namespace::Flash_Class::Set_Boot_Partition(Partition_Subtype_Type Partition_Subtype) -{ - if (Partition_Subtype > Xila_Partition) - { - return Result_Type::Error; - } - if (esp_ota_set_boot_partition(esp_partition_find_first(ESP_PARTITION_TYPE_APP, (esp_partition_subtype_t)Partition_Subtype, NULL)) == ESP_OK) - { - return Result_Type::Success; - } - return Result_Type::Error; -} - -uint32_t Xila_Namespace::Flash_Class::Get_Sketch_Free_Space() -{ - const esp_partition_t *Partition = esp_ota_get_next_update_partition(NULL); - if (!Partition) - { - return 0; - } - return Partition->size; -} - -String_Type &Xila_Namespace::Flash_Class::Get_Sketch_MD5(String_Type &String) -{ - String = ESP.getSketchMD5().c_str(); - return String; -} - -uint32_t Xila_Namespace::Flash_Class::Sketch_Size(sketchSize_t Response) -{ - esp_image_metadata_t data; - const esp_partition_t *running = esp_ota_get_running_partition(); - if (!running) - return 0; - const esp_partition_pos_t running_pos = { - .offset = running->address, - .size = running->size, - }; - data.start_addr = running_pos.offset; - esp_image_verify(ESP_IMAGE_VERIFY, &running_pos, &data); - if (Response) - { - return running_pos.size - data.image_len; - } - else - { - return data.image_len; - } -} - -Size_Type Xila_Namespace::Flash_Class::Get_Size() -{ - return ESP.getFlashChipSize(); -} - -Size_Type Xila_Namespace::Flash_Class::Get_Speed() -{ - return ESP.getFlashChipSpeed(); -} - -Flash_Mode_Type Xila_Namespace::Flash_Class::Get_Mode() -{ - return (Flash_Mode_Type) ESP.getFlashChipMode(); -} \ No newline at end of file