Make common_arm to hardware abstraction layer.

This commit is contained in:
dxl authored and Philippe Teuwen committed 2026-08-19 15:32:39 +02:00
1 parent 51d0ff260a
commit 36f1c7e44c
61 files changed
+8050 -1670

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
//
// Created by dxl on 2026/5/25.
//
#ifndef FLASH_CODE_APIS_H
#define FLASH_CODE_APIS_H
#include "common.h"
/**
* Write code flash minimum unit. The implementation of this function is very specific to different platforms.
* The minimum flash rewriting unit varies from platform to platform,
* so the data length must strictly comply with the length returned by FlashCodeGetEWMinUnit.
*
* @param flash_address The flash address to write to. Must be aligned to 4 bytes.
* @param data The data to write. u32 only.
* @param flash_start The flash start address of firmware code.
* @param status The pointer to store the platform specific status code of flash erase/write.
* @return Whether the operation is successful. If it fails, you can refer to the status.
*/
bool FlashCodeEWriteMinUnit(uint32_t flash_address, const uint32_t *data, uint32_t *flash_start, uint32_t *status);
/**
* Minimum unit for Flash erase/write.
* @return Minimum number of bytes per erase/write.
*/
STATIC_FORCE_INLINE uint16_t FlashCodeGetEWMinUnit(void);
/**
* Initialize the FLASH that stores firmware/code.
* Configure the clock speed of FLASH, for example.
*/
STATIC_FORCE_INLINE void FlashCodeInit(void);
#ifdef PM5
#include "flash_code_hw_at32.h"
#else
#include "flash_code_hw_at91.h"
#endif
#endif //FLASH_CODE_APIS_H
@@ -0,0 +1,73 @@
//
// Created by dxl on 2026/5/26.
//
#include "at32f435_437_flash.h"
#include "at32f435_437_misc.h"
#include "flash_code_apis.h"
// The configuration is 512K. If the code execution speed is desired,
// please define the functions as a code segment executed by RAM.
// 512K_SRAM -> Flash memory zero wait delay area 128K bytes
// 448K_SRAM -> Flash memory zero wait delay area 192K bytes
// 384K_SRAM -> Flash memory zero wait delay area 256K bytes
// 320K_SRAM -> Flash memory zero wait delay area 320K bytes
// 256K_SRAM -> Flash memory zero wait delay area 384K bytes
// 192K_SRAM -> Flash memory zero wait delay area 448K bytes
// 128K_SRAM -> Flash memory zero wait delay area 512K bytes
#define AT32_EXTEND_SRAM FLASH_EOPB0_SRAM_512K
void Extend_SRAM(void) {
#ifdef AS_BOOTROM // !!! Warning: this function only works in bootrom. Otherwise, it may cause crash/infinite restart.
// check if ram has been set to expectant size, if not, change eopb0
if (((USD->eopb0) & 0x07) != AT32_EXTEND_SRAM) {
// unlock flash first
flash_unlock();
// erase user system data bytes
flash_user_system_data_erase();
// change sram size. Theoretically, we need to judge whether it can be set to this size according to the flash size,
// but PM5 is only 1M, so we will not judge it temporarily.
flash_eopb0_config(AT32_EXTEND_SRAM);
// system reset
nvic_system_reset();
}
#endif
}
bool FlashCodeEWriteMinUnit(uint32_t flash_address, const uint32_t *data, uint32_t *flash_start, uint32_t *status) {
const uint32_t min_ew_unit = FlashCodeGetEWMinUnit();
const uint32_t min_ew_unit_u32 = min_ew_unit / sizeof(uint32_t);
UNUSED(flash_start);
flash_unlock();
// Wait for operation to be completed
*status = flash_operation_wait_for(ERASE_TIMEOUT);
if((*status == FLASH_PROGRAM_ERROR) || (*status == FLASH_EPP_ERROR)) {
flash_flag_clear(FLASH_PRGMERR_FLAG | FLASH_EPPERR_FLAG);
} else if(*status == FLASH_OPERATE_TIMEOUT) {
return false;
}
// Erase and write using the starting address of the sector.
flash_address = (flash_address / min_ew_unit) * min_ew_unit;
// Erase
*status = flash_sector_erase(flash_address);
if(*status != FLASH_OPERATE_DONE) {
return false;
}
// Write
for(uint32_t i = 0; i < min_ew_unit_u32; i++) {
uint32_t w_addr = flash_address + i * sizeof(uint32_t);
*status = flash_word_program(w_addr, data[i]);
if(*status != FLASH_OPERATE_DONE) {
return false;
}
}
flash_lock();
return true;
}
@@ -0,0 +1,65 @@
//
// Created by dxl on 2026/5/25.
//
#ifndef FLASH_CODE_HW_AT32_H
#define FLASH_CODE_HW_AT32_H
#include "common.h"
#include "sys_apis.h"
#include "at32f435_437_flash.h"
/**
* Config the sram extend for MORE ram size.
* Note: sacrifice non-0 wait FLASH area. And this configuration function must be called
* before accessing a larger memory area, otherwise HW FAULT may result.
*/
void Extend_SRAM(void);
// It is not allowed to hard code 4096 or 2048, but should be determined according to the current chip capacity.
STATIC_FORCE_INLINE uint16_t FlashCodeGetEWMinUnit(void) {
// 4032K:
// The flash memory capacity of slice 1 is 2048K bytes, including 32 blocks, each block has 16 sectors, and each sector size is 4K bytes;
// The flash memory capacity of slice 2 is 1984K bytes, including 31 blocks. Each block has 16 sectors, and the size of each sector is 4K bytes.
// The user system data area is 4K bytes in total.
// 1024K:
// The main memory is divided into chip 1 and chip 2 flash memory. Each flash memory has a capacity of 512K bytes and contains 8 blocks,
// each block contains 32 sectors, and the size of each sector is 2K bytes.
// The user system data area is 512 bytes in total.
// 256K:
// The 256K byte main memory has only one flash memory, which contains 4 blocks.
// Each block contains 32 sectors, and each sector is 2K bytes in size.
// The user system data area is 512 bytes in total.
if (GetChipFlashSize() > 1024 * 1024) {
return 4096;
}
return 2048;
}
/**
* Improve the performance of flash
* See: https://www.arterytek.com/download/APNOTE/AN0092_AT32F435_437_Performance_Improve_V2.0.1_EN.pdf
*/
STATIC_FORCE_INLINE void FlashCodeInit(void) {
/*
Note: If you want to improve the performance of the non-zero wait flash area,
you need to pay attention to the following specification limits.
+--------+--------------------------+--------------------------------+---------------------+-------+-------+------+
| Symbol | Parameter | Condition | Sub-Condition | Min | Max | Unit |
+--------+--------------------------+--------------------------------+---------------------+-------+-------+------+
| | | NZW_BST acceleration off | LDO Voltage 1.3 V | 0 | 288 | |
| | | | LDO Voltage 1.2 V | 0 | 240 | |
| f_HCLK | Internal AHB clock freq | | LDO Voltage 1.1 V | 0 | 144 | MHz |
| | |--------------------------------+---------------------+-------+-------+ |
| | | NZW_BST acceleration on | LDO Voltage 1.3 V | 0 | 192 | |
| | | | LDO Voltage 1.2 V | 0 | 160 | |
| | | | LDO Voltage 1.1 V | 0 | 108 | |
+--------+--------------------------+--------------------------------+---------------------+-------+-------+------+
*/
// Improve the performance of continuous flash reading, Note: increased power consumption.
flash_continue_read_enable(TRUE); // FLASH->contr_bit.fcontr_en = TRUE;
}
#endif //FLASH_CODE_HW_AT32_H
@@ -0,0 +1,47 @@
//
// Created by dxl on 2026/5/27.
//
#include "flash_code_apis.h"
RAMFUNC
bool FlashCodeEWriteMinUnit(uint32_t flash_address, const uint32_t *data, uint32_t *flash_start, uint32_t *status) {
*status = 0x00;
// The default is AT91C_BASE_EFC0. If the current write address exceeds AT91C_BASE_EFC0,
// it will automatically switch to AT91C_BASE_EFC1.
AT91PS_EFC efc_bank = AT91C_BASE_EFC0;
// If bank1 is currently being writing, we need to calculate the offset to get the starting position of bank1 in flash.
int offset = 0;
// Calculate how many pages have been written in total.
uint32_t page_n = (flash_address - (uint32_t) flash_start) / AT91C_IFLASH_PAGE_SIZE;
if (page_n >= AT91C_IFLASH_NB_OF_PAGES / 2) {
// When writing to bank2, we need to recalculate the page from 0 to 1023.
page_n -= AT91C_IFLASH_NB_OF_PAGES / 2;
// Switch to AT91C_BASE_EFC1
efc_bank = AT91C_BASE_EFC1;
// We need to offset the writes or it will not fill the correct bank write buffer.
// offset = 65535, 65535 * 4(u32) = 262,140, for write bank1 not bank0.
offset = (AT91C_IFLASH_NB_OF_PAGES / 2) * AT91C_IFLASH_PAGE_SIZE / sizeof(uint32_t);
}
// The Flash of the SAM7S512/256/128 contains a 256-byte write buffer, accessible through a 32-bit interface.
// The Flash of the SAM7S64/321/32/161/16 contains a 128-byte write buffer, accessible through a 32-bit interface.
// The writing is not directly written to the flash, but committed to the latch buffer,
// and then to write the EFC register triggers the erase and write.
// In addition, the write operation only considers the address of the lower eight bits,
// so actually only needs to write data to flash_start,
// and the chip will automatically copy the data to the latch buffer and increase count.
for (int i = 0; i < FlashCodeGetEWMinUnit() / sizeof(uint32_t); i++) {
flash_start[offset + i] = data[i];
}
efc_bank->EFC_FCR = MC_FLASH_COMMAND_KEY |
MC_FLASH_COMMAND_PAGEN(page_n) |
AT91C_MC_FCMD_START_PROG;
// Wait until flashing of page finishes
uint32_t sr;
while (!((sr = efc_bank->EFC_FSR) & AT91C_MC_FRDY));
if (sr & (AT91C_MC_LOCKE | AT91C_MC_PROGE)) {
*status = sr;
return false;
}
return true;
}
@@ -0,0 +1,32 @@
//
// Created by dxl on 2026/5/25.
//
#ifndef FLASH_CODE_HW_AT91_H
#define FLASH_CODE_HW_AT91_H
#include "flash_code_apis.h"
#include "proxmark3_arm.h"
#include "at91sam7s512.h"
#include "sys_apis.h"
STATIC_FORCE_INLINE uint16_t FlashCodeGetEWMinUnit(void) {
// The page size of the chip used by pm3 is only 256 bytes.
// No 128/64 byte page size.
// If a pm3 device really uses such a small capacity chip, remember to add this compatibility support.
return AT91C_IFLASH_PAGE_SIZE;
}
STATIC_FORCE_INLINE void FlashCodeInit(void) {
// Set the first 256KB memory flashspeed
AT91C_BASE_EFC0->EFC_FMR = AT91C_MC_FWS_1FWS | MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48);
// 9 = 256, 10+ is 512KB
uint8_t id = (GetChipId() & 0xF00) >> 8;
if (id > 9) {
// Set the second 256KB memory flashspeed, if it exists
AT91C_BASE_EFC1->EFC_FMR = AT91C_MC_FWS_1FWS | MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48);
}
}
#endif //FLASH_CODE_HW_AT91_H