Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Improving abstraction for flash module
Browse files Browse the repository at this point in the history
  • Loading branch information
AlixANNERAUD committed Jul 30, 2023
1 parent 48fbb9d commit 0aab17e
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 218 deletions.
4 changes: 4 additions & 0 deletions Configurations/ESP32-8048S070.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Configurations/Linux.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions Configurations/WT32-SC01.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions Configurations/WT32-SC01_Plus.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file.
94 changes: 19 additions & 75 deletions lib/Xila/include/Flash/Flash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <memory>
#include <soc/soc.h>
#include <soc/efuse_reg.h>
#include <esp_partition.h>
extern "C"
{
#include "esp_ota_ops.h"
#include "esp_image_format.h"
}
#include <MD5Builder.h>


#include "../Module/Module.hpp"

Expand All @@ -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.
Expand All @@ -100,30 +38,30 @@ 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.
/// @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 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.
///
Expand All @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions lib/Xila/include/Power/Power.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

#include "../Module/Module.hpp"

#include "Battery_Library.hpp"

//==============================================================================//
///
/// @brief Power management module.
Expand Down
129 changes: 129 additions & 0 deletions lib/Xila/src/Flash/Drivers/ESP32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/// @file Flash.cpp
/// @author Alix ANNERAUD ([email protected])
/// @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 <memory>
#include <soc/soc.h>
#include <soc/efuse_reg.h>
#include <esp_partition.h>
extern "C"
{
#include "esp_ota_ops.h"
#include "esp_image_format.h"
}
#include <MD5Builder.h>

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
Loading

0 comments on commit 0aab17e

Please sign in to comment.