Files
pico/projects/tzpuPico/esp32/main/include/flash_ram.h
2026-04-30 08:58:26 +01:00

287 lines
14 KiB
C
Vendored

/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Name: flash_ram.h
// Created: Sep 2024
// Author(s): Philip Smart
// Description: Flash RAM control and layout.
// Credits:
// Copyright: (c) 2019-2026 Philip Smart <philip.smart@net2net.org>
//
// History: Oct 2024 v1.0 - Initial write of the flash ram definition.
//
// Notes: See Makefile to enable/disable conditional components
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// This source file is free software: you can redistribute it and#or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This source file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef FLASH_RAM_H_
#define FLASH_RAM_H_
// ------------------------------------------------------------------------------------------------------
// FlashRAM (16M) organisation.
//
// - 0x10000000 - 0x1001FFFF = Bootloader 128K Bootloader App, boots active application.
// - 0x10020000 - 0x1051FFFF = App 1 5M Application 1
// - 0x10520000 - 0x10A1FFFF = App 2 5M Application 2
// - 0x10A20000 - 0x10C9FFFF = App Config 1 2.5M Config data (ROMS) for Application 1
// - 0x10CA0000 - 0x10F1FFFF = App Config 2 2.5M Config data (ROMS) for Application 2
// - 0x10F20000 - 0x10FFEFFF = Config 892K General configuration/scratch pad.
// - 0x10FFF000 - 0x11000000 = Partition Table 4K Partition Table
// ------------------------------------------------------------------------------------------------------
// Application program offset in flash (128*1024). Exclude the physical address as definition is used for Flash manipulation routines.
// This should agree with the linker script for the application program. - See main_memmap.ld FLASH origin/size.
//#define FLASH_APP_PROGRAM_OFFSET FW_BOOTLOADER_SIZE
// Number of applications in Flash. App 0 = bootloader.
#define FLASH_APP_MAX_INSTANCES 3
#define FLASH_APP_BOOTLOADER 0
#define FLASH_APP_INSTANCE1 1
#define FLASH_APP_INSTANCE2 2
// Sizes.
#define FLASHHDR_LICENSE_SIZE 10
#define FLASHHDR_AUTHOR_SIZE 30
#define FLASHHDR_DESCRIPTION_SIZE 80
#define FLASHHDR_VERSION_SIZE 10
#define FLASHHDR_VERSIONDATE_SIZE 20
#define FLASHHDR_COPYRIGHT_SIZE 80
#define FLASH_APP_PERSIST_BLOCKS 4
#define FLASH_MAX_ROM_FILENAME_SIZE 256 // Maximum size of a ROM filename including path.
#define FLASH_MAX_ROM_IMAGES 64 // Maximum number of ROMS which can be stored in FlashRAM (subject to space).
#define FLASH_MAX_ROM_IMAGE_SIZE (512 * 1024) // Maximum single ROM image size.
#define FLASH_SECTOR_SIZE (4 * 1024) // Same as FW_SECTOR_SIZE but may not be defined in all includes.
#define FLASH_APP_MAX_JSON_SIZE (64 * 1024) // 64K to store minified JSON configuration.
//
#define FLASH_APP_BOOTLOADER_SIZE (128 * 1024) // 128K Bootloader App.
#define FLASH_APP_SIZE (5 * 1024 * 1024) // 5MB per App.
#define FLASH_APP_CONFIG_SIZE ((2 * 1024 * 1024) + \
(512 * 1024)) // 2.5MB configuration storage per App.
#define FLASH_CONFIG_HEADER_SIZE FLASH_SECTOR_SIZE + \
(FLASH_APP_PERSIST_BLOCKS * FLASH_SECTOR_SIZE)
#define FLASH_CONFIG_SIZE (892 * 1024) // 892K Config, Spare or Scratch space.
#define FLASH_APP_CONFIG_HEADER_SIZE FLASH_SECTOR_SIZE + \
FLASH_APP_MAX_JSON_SIZE + \
(FLASH_MAX_ROM_IMAGES * FLASH_MAX_ROM_FILENAME_SIZE) + \
(FLASH_APP_PERSIST_BLOCKS * FLASH_SECTOR_SIZE)
#define FLASH_PARTITION_SIZE (4 * 1024) // 4K partition.
#define FLASH_APP_PARTITION_SIZE (4 * 1024) // 4K partition for each application config.
/*
#define FLASH_APP_DATA_SIZE FLASH_APP_CONFIG_SIZE - (FLASH_APP_MAX_JSON_SIZE - \
(FLASH_APP_PERSIST_BLOCKS * FLASH_SECTOR_SIZE)\
)
*/
// Addresses in the FlashRAM according to above memory map. Values are coded as this header is shared with the ESP32 which has no
// knowledge of the rp2350.h configurations.
#define FLASH_RAM_BASE 0x10000000
#define FLASH_APP_BOOTLOADER_ADDR FLASH_RAM_BASE
#define FLASH_APP_START_ADDR 0x10020000
#define FLASH_APP_START_CONFIG_ADDR 0x10A20000
#define FLASH_START_CONFIG_ADDR 0x10F20000
#define FLASH_START_PARTITION_TABLE_ADDR 0x10FFF000
// Addresses in the FlashRAM relative to the FlashRAM base at 0.
#define FLASH_APP_BOOTLOADER_POS 0
#define FLASH_APP_START_POS (FLASH_APP_START_ADDR - FLASH_APP_BOOTLOADER_ADDR)
#define FLASH_APP_START_CONFIG_POS (FLASH_APP_START_CONFIG_ADDR - FLASH_APP_BOOTLOADER_ADDR)
#define FLASH_START_CONFIG_POS (FLASH_START_CONFIG_ADDR - FLASH_APP_BOOTLOADER_ADDR)
#define FLASH_START_PARTITION_TABLE_POS (FLASH_START_PARTITION_TABLE_ADDR - FLASH_APP_BOOTLOADER_ADDR)
// Flash header offset in flash - last sector in FlashRAM.
#define FLASH_HEADER_OFFSET (PICO_FLASH_SIZE_BYTES - FLASH_PARTITION_SIZE)
// Boot mode flag indicator.
#define FLASH_APP_BOOT_MODE_BOOTLOADER 0x00
#define FLASH_APP_BOOT_MODE_APPLICATION 0xF7
// Position of the application signature in the Flash binary image.
#define FLASH_APP_CONFIGURED_FLAG 0xAAFF5500
#define FLASH_APP_SIG_POS 0x124
#define FLASH_APP_SIG_BYTE_1 0xF2
#define FLASH_APP_SIG_BYTE_2 0xEB
#define FLASH_APP_SIG_BYTE_3 0x88
#define FLASH_APP_SIG_BYTE_4 0x71
// ------------------------------------------------------------------------------------------------------
// FlashRAM (16M) organisation.
//
// - 0x10000000 - 0x1001FFFF = Bootloader 128K Bootloader App, boots active application.
// - 0x10020000 - 0x1051FFFF = App 1 5M Application 1
// - 0x10520000 - 0x10A1FFFF = App 2 5M Application 2
// - 0x10A20000 - 0x10C9FFFF = App Config 1 2.5M Config data for Application 1
// - 0x10CA0000 - 0x10F1FFFF = App Config 2 2.5M Config data for Application 2
// - 0x10F20000 - 0x10FFEFFF = Config 892K General configuration/scratch pad.
// - 0x10FFF000 - 0x11000000 = Partition Table 4K Partition Table
// ------------------------------------------------------------------------------------------------------
// Boot table entry description for one application.
typedef struct
{
uint32_t addr; // Execution address of application.
uint32_t size; // Size of application in bytes.
uint32_t chksum; // Checksum of application image.
uint32_t cfgAddr; // Storage address of instance configuration data.
uint32_t cfgSize; // Size of instance configuration data.
char license[FLASHHDR_LICENSE_SIZE]; // License string of application.
char author[FLASHHDR_AUTHOR_SIZE]; // Author string of application.
char description[FLASHHDR_DESCRIPTION_SIZE]; // Description string of application.
char version[FLASHHDR_VERSION_SIZE]; // Version string of application.
char versionDate[FLASHHDR_VERSIONDATE_SIZE]; // Version Date String of application.
char copyright[FLASHHDR_COPYRIGHT_SIZE]; // Copyright string of application.
} t_FlashPartitionInstance;
// Flash partition table.
typedef struct
{
uint32_t configured; // ID = 0xAAFF5500
uint8_t bootMode; // F7 - Boot application, /= F7 - Bootloader.
uint8_t activeApp; // Active application.
t_FlashPartitionInstance config[FLASH_APP_MAX_INSTANCES];
} t_FlashPartitionHeader;
// Extended INF payload — partition header + runtime core config.
// Maximum size of the active driver summary string sent via INF IPC.
#define FLASHHDR_DRIVER_SUMMARY_SIZE 128
// Sent by RP2350 ESP_sendVersionInfo, received by ESP32 storeRP2350Info.
typedef struct
{
t_FlashPartitionHeader header;
int32_t cpufreq; // RP2350 CPU frequency in Hz (0 = default)
int32_t psramfreq; // PSRAM SPI frequency in Hz (0 = default)
int32_t voltage; // Core voltage setting (VREG enum)
uint32_t flashSize; // RP2350 Flash size in bytes
uint32_t psramSize; // RP2350 PSRAM size in bytes
uint32_t hostClkHz; // Measured host clock frequency in Hz
uint32_t emulSpeedHz; // Effective Z80 emulation speed in Hz
char driverSummary[FLASHHDR_DRIVER_SUMMARY_SIZE]; // Active drivers and interfaces
} t_FlashInfoPayload;
// Structure to describe a single file stored in ROM. Indexed by its filename (matching a filename appearing in JSON including path)
// along with the size of the file (ROM image or program/data) and the address the image can be found within the ROM.
typedef struct
{
char filename[FLASH_MAX_ROM_FILENAME_SIZE]; // Name of file stored in FlashRAM.
uint32_t size; // Size of the image.
uint32_t addr; // Address in FlashRAM where image is stored.
} t_ROMCfg;
// Definition of an application configuration storage.
// An application configuration is defined by a JSON structure on the SD card. As it takes time for the SD
// card to come online, the configuration is organised into this structure and saved within the FlashRAM.
// On boot up, all the config is immediately available, speeding up the online time of the CPU.
// -----------------------------------------------------------------------------------------------------------
// -
// - CPU Specific Block
// - SRAM memptr Array
// - PSRAM memPtr, memioPtr, ioPtr pointer Array
// - t_Drivers array - this depicts the rom name and where the image is loaded.
// - Number of ROM Images
// - Array ROM Name, FlashPositionAddr, Size, RAMPositionAddr (non-driver defined images)
// -
// - Scratch sector
// -----------------------------------------------------------------------------------------------------------
struct s_FlashAppConfig
{
// Array of Flash Sector blocks used for regular r/w to persist state info as needed.
uint8_t persistBlock[FLASH_APP_PERSIST_BLOCKS][FLASH_SECTOR_SIZE];
// Configuration signature - indicates if config is valid.
uint32_t configured; // ID = 0xAAFF5500
// Minified configuration JSON, used on power up to configure the App.
char configJSON[FLASH_APP_MAX_JSON_SIZE];
// Core override values, if not 0 then these are used in preference to the core or default values
// giving the app fine control on system parameters.
int cpufreq; // RP2350 Core Frequency
int psramfreq; // PSRAM Frequency
int voltage; // RP2350 Voltage
// Store ROM descriptions in this header.
// The remainder of the Application Config Data space is used for ROM/file data storage.
int romCount; // Number of rom images stored.
t_ROMCfg ROMS[FLASH_MAX_ROM_IMAGES]; // ROM descriptors, described a ROM image stored in FlashRAM.
};
// Define type as a Union for FlashAppConfigHeader to ensure size is padded to a multiple of FLASH_SECTOR_SIZE
typedef union
{
struct s_FlashAppConfig s;
uint8_t pad[FLASH_APP_CONFIG_HEADER_SIZE];
} t_FlashAppConfigHeader;
// Define type as a Union for FlashAppConfig to ensure size is padded to AppConfig block.
typedef union
{
t_FlashAppConfigHeader t;
uint8_t pad[FLASH_APP_CONFIG_SIZE];
} t_FlashAppConfig;
// Definition of Core (RP2350) configuration persistence,
struct s_FlashCoreConfig
{
// Configuration signature - indicates if config is valid.
uint32_t configured; // ID = 0xAAFF5500
int cpufreq; // RP2350 Core Frequency
int psramfreq; // PSRAM Frequency
int voltage; // RP2350 Voltage
};
// Define type as a Union for FlashCoreConfig to ensure size is padded to a sector multiple size.
typedef union
{
struct s_FlashCoreConfig s;
uint8_t pad[FLASH_SECTOR_SIZE];
} t_FlashCoreConfig;
// Definition of the tzpuPico configuration.
// This structure stores any non-app data which is needed to persist across reboots/power cycles.
struct s_FlashConfigHeader
{
// Array of Flash Sector blocks used for regular r/w to persist state info as needed.
uint8_t persistBlock[FLASH_APP_PERSIST_BLOCKS][FLASH_SECTOR_SIZE];
// Structure containing core configuration.
t_FlashCoreConfig core;
};
// Define type as a Union for FlashConfigHeader to ensure size is padded to a multiple of FLASH_SECTOR_SIZE
typedef union
{
struct s_FlashConfigHeader s;
uint8_t pad[FLASH_CONFIG_HEADER_SIZE];
} t_FlashConfigHeader;
// Define type as a Union for FlashConfig to ensure size is padded to size allocated in Flash.
typedef union
{
t_FlashConfigHeader t;
uint8_t pad[FLASH_CONFIG_SIZE];
} t_FlashConfig;
// Definition of the Flash RAM as a structure. This can be overlaid to reference the correct locations in Flash.
typedef struct
{
uint8_t bootloader[FLASH_APP_BOOTLOADER_SIZE];
uint8_t app[FLASH_APP_MAX_INSTANCES - 1][FLASH_APP_SIZE];
t_FlashAppConfig app_config[FLASH_APP_MAX_INSTANCES - 1];
t_FlashConfig config;
uint8_t partitionTable[FLASH_PARTITION_SIZE];
} t_FlashLayout;
#endif