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

+62
View File
@@ -0,0 +1,62 @@
#ifndef SYS_APIS_H_
#define SYS_APIS_H_
#include "common.h"
//-----------------------------------------------------------------------------
// Jump to the Any image after setting the stack pointer.(You jump, i jump)
// For chips that require setting the interrupt vector table,
// this function assumes by default that it is in the header of AnyImage.
//-----------------------------------------------------------------------------
void __attribute__((noreturn)) JumpToAnyImage(uint32_t stack_top, uint32_t entry_point);
//-----------------------------------------------------------------------------
// Config system clock
// This is usually done in the BOOTROM firmware.
//-----------------------------------------------------------------------------
void ConfigSystemClocks(void);
//-----------------------------------------------------------------------------
// Get the main chip type of the current firmware.
// Note: It is determined at compile time, rather than through some register information.
//-----------------------------------------------------------------------------
STATIC_FORCE_INLINE main_chip_type_t GetChipType(void);
//-----------------------------------------------------------------------------
// Get ID of the chip
// Note: It is not the unique ID of the chip. It is the ID related the chip model.
//-----------------------------------------------------------------------------
STATIC_FORCE_INLINE uint32_t GetChipId(void);
//-----------------------------------------------------------------------------
// Get the unique ID of the chip
// size: the size of the unique ID in bytes, set to 0 if no unique id available(and return null ptr).
// Note: Not all chips have a unique ID(Such as AT91SAM7S).
//-----------------------------------------------------------------------------
STATIC_FORCE_INLINE uint8_t* GetChipUniqueId(uint8_t *size);
//-----------------------------------------------------------------------------
// Reset the chip processor
// Note: Resetting the chip will restart code execution from the bootROM.
//-----------------------------------------------------------------------------
STATIC_FORCE_INLINE void ResetChip(void);
//-----------------------------------------------------------------------------
// Get flash size of chip
// ROM size max in bytes
//-----------------------------------------------------------------------------
STATIC_FORCE_INLINE uint32_t GetChipFlashSize(void);
//-----------------------------------------------------------------------------
// Check if the reset is caused by a reset with SRAM retention,
// which means the RAM content is retained and not cleared.
//-----------------------------------------------------------------------------
STATIC_FORCE_INLINE bool CheckRSTWithSRAMRetention(void);
#ifdef PM5
#include "sys_hw_at32.h"
#else
#include "sys_hw_at91.h"
#endif
#endif // SYS_APIS_H_
+445
View File
@@ -0,0 +1,445 @@
//
// Created by dxl on 2026/5/23.
//
#include "sys_apis.h"
#include "config_gpio_proxmark5.h"
// --- at32 ---
#include "at32f435_437_crm.h"
#include "at32f435_437_pwc.h"
#include "at32f435_437_flash.h"
#include "at32f435_437_misc.h"
#include "at32f435_437_ertc.h"
// ---
#define SYS_SIMPLE_RESET_BPR_MAGIC 0x504D3352U
#define SYS_SIMPLE_RESET_BPR_UNLOCK_KEY1 0xCAU
#define SYS_SIMPLE_RESET_BPR_UNLOCK_KEY2 0x53U
#define SYS_SIMPLE_RESET_BPR_LOCK_KEY 0xFFU
uint8_t g_system_reset_method = 0; // Default set to 0 for call system_simple_reset() + jump bootrom.
/**
* @brief empty call definition, avoid errors linking libc.a
* @param fn_name function name to define for libc.a
*/
#define EMPTY_CALL(fn_name) \
void fn_name(void); \
__WEAK void fn_name(void) {}
// For simple reset & jump to bootrom restart the device.
extern uint32_t _bootrom_start[], _stack_end[];
// Empty init definition to avoid errors linking libc.a
EMPTY_CALL(_init)
// Empty _fini definition to avoid errors linking libc.a
EMPTY_CALL(_fini)
/**
* Write data to BPR(Battery powered domain data) register 1
* @param data The data to write to the BPR register 1
*/
static void at32_bpr_write_dt1(uint32_t data) {
CRM->apb1en_bit.pwcen = TRUE;
PWC->ctrl_bit.bpwen = TRUE;
CRM->bpdc_bit.ertcen = TRUE;
ERTC->wp = SYS_SIMPLE_RESET_BPR_UNLOCK_KEY1;
ERTC->wp = SYS_SIMPLE_RESET_BPR_UNLOCK_KEY2;
ERTC->dt1 = data;
ERTC->wp = SYS_SIMPLE_RESET_BPR_LOCK_KEY;
CRM->apb1en_bit.pwcen = FALSE;
PWC->ctrl_bit.bpwen = FALSE;
CRM->bpdc_bit.ertcen = FALSE;
}
/**
* @brief check BPR register 1, if it is equal to SYS_SIMPLE_RESET_BPR_MAGIC,
* clear it and return true, otherwise return false.
* @return true if the BPR register 1 is equal to SYS_SIMPLE_RESET_BPR_MAGIC, false otherwise
*/
bool system_bpr_chk_clear(void) {
CRM->apb1en_bit.pwcen = TRUE;
PWC->ctrl_bit.bpwen = TRUE;
CRM->bpdc_bit.ertcen = TRUE;
if (ERTC->dt1 == SYS_SIMPLE_RESET_BPR_MAGIC) {
at32_bpr_write_dt1(0);
return true;
}
return false;
}
/**
* @brief this function handles nmi exception.
* @retval none
*/
void NMI_Handler(void) {
}
/**
* @brief this function handles hard fault exception.
* @retval none
*/
void HardFault_Handler(void) {
/* go to infinite loop when hard fault exception occurs */
while (1) {
}
}
/**
* @brief this function handles memory manage exception.
* @retval none
*/
void MemManage_Handler(void) {
/* go to infinite loop when memory manage exception occurs */
while (1) {
}
}
/**
* @brief this function handles bus fault exception.
* @retval none
*/
void BusFault_Handler(void) {
/* go to infinite loop when bus fault exception occurs */
while (1) {
}
}
/**
* @brief this function handles usage fault exception.
* @retval none
*/
void UsageFault_Handler(void) {
/* go to infinite loop when usage fault exception occurs */
while (1) {
}
}
/**
* @brief this function handles svcall exception.
* @retval none
*/
void SVC_Handler(void) {
}
/**
* @brief this function handles debug monitor exception.
* @retval none
*/
void DebugMon_Handler(void) {
}
/**
* @brief this function handles pendsv_handler exception.
* @retval none
*/
void PendSV_Handler(void) {
}
/**
* @brief this function handles systick handler.
* @retval none
*/
void SysTick_Handler(void) {
}
/**
* @brief system clock config program
* @note the system clock is configured as follow:
* system clock (sclk) = (hext * pll_ns)/(pll_ms * pll_fr)
* system clock source = HEXT_VALUE
* - hext = 8000000
* - sclk = 48000000
* - ahbdiv = 1
* - ahbclk = 48000000
* - apb1div = 2
* - apb1clk = 24000000
* - apb2div = 1
* - apb2clk = 48000000
* - pll_ns = 96
* - pll_ms = 1
* - pll_fr = 16
* @retval none
*/
void system_clock_config_48m(void) {
/* reset crm */
crm_reset();
/* enable pwc periph clock */
crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
/* config ldo voltage */
pwc_ldo_output_voltage_set(PWC_LDO_OUTPUT_1V1);
/* set the flash clock divider */
flash_clock_divider_set(FLASH_CLOCK_DIV_2);
/* enable hext */
crm_clock_source_enable(CRM_CLOCK_SOURCE_HEXT, TRUE);
/* wait till hext is ready */
while (crm_hext_stable_wait() == ERROR) {
}
/* config pll clock resource
common frequency config list: pll source selected hick or hext(8mhz)
_________________________________________________________________________________________________
| | | | | | | | | | |
|pll(mhz)| 288 | 252 | 216 | 192 | 180 | 144 | 108 | 72 | 36 |
|________|_________|_________|_________|_________|_________|_________|_________|_________________|
| | | | | | | | | | |
|pll_ns | 144 | 126 | 108 | 96 | 90 | 72 | 108 | 72 | 72 |
| | | | | | | | | | |
|pll_ms | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| | | | | | | | | | |
|pll_fr | FR_4 | FR_4 | FR_4 | FR_4 | FR_4 | FR_4 | FR_8 | FR_8 | FR_16|
|________|_________|_________|_________|_________|_________|_________|_________|________|________|
if pll clock source selects hext with other frequency values, or configure pll to other
frequency values, please use the at32 new clock configuration tool for configuration. */
crm_pll_config(CRM_PLL_SOURCE_HEXT, 96, 1, CRM_PLL_FR_16);
/* enable pll */
crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);
/* wait till pll is ready */
while (crm_flag_get(CRM_PLL_STABLE_FLAG) != SET) {
}
/* config ahbclk */
crm_ahb_div_set(CRM_AHB_DIV_1);
/* config apb2clk */
crm_apb2_div_set(CRM_APB2_DIV_1);
/* config apb1clk */
crm_apb1_div_set(CRM_APB1_DIV_2);
/* select pll as system clock source */
crm_sysclk_switch(CRM_SCLK_PLL);
/* wait till pll is used as system clock source */
while (crm_sysclk_switch_status_get() != CRM_SCLK_PLL) {
}
/* update system_core_clock global variable */
system_core_clock_update();
}
/**
* @brief system clock config
* @note the system clock is configured as follow:
* system clock (sclk) = (hext * pll_ns)/(pll_ms * pll_fr)
* system clock source = pll (hext)
* - hext = HEXT_VALUE
* - sclk = 288000000
* - ahbdiv = 1
* - ahbclk = 288000000
* - apb2div = 2
* - apb2clk = 144000000
* - apb1div = 2
* - apb1clk = 144000000
* - pll_ns = 144
* - pll_ms = 1
* - pll_fr = 4
* @retval none
*/
void system_clock_config_288m(void) {
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
/* reset crm */
crm_reset();
/* enable pwc periph clock */
crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
/* config ldo voltage */
pwc_ldo_output_voltage_set(PWC_LDO_OUTPUT_1V3);
/* set the flash clock divider */
flash_clock_divider_set(FLASH_CLOCK_DIV_3);
crm_clock_source_enable(CRM_CLOCK_SOURCE_HEXT, TRUE);
/* wait till hext is ready */
while (crm_hext_stable_wait() == ERROR) {
}
/* config pll clock resource
common frequency config list: pll source selected hick or hext(8mhz)
_________________________________________________________________________________________________
| | | | | | | | | | |
|pll(mhz)| 288 | 252 | 216 | 192 | 180 | 144 | 108 | 72 | 36 |
|________|_________|_________|_________|_________|_________|_________|_________|_________________|
| | | | | | | | | | |
|pll_ns | 144 | 126 | 108 | 96 | 90 | 72 | 108 | 72 | 72 |
| | | | | | | | | | |
|pll_ms | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| | | | | | | | | | |
|pll_fr | FR_4 | FR_4 | FR_4 | FR_4 | FR_4 | FR_4 | FR_8 | FR_8 | FR_16|
|________|_________|_________|_________|_________|_________|_________|_________|________|________|
if pll clock source selects hext with other frequency values, or configure pll to other
frequency values, please use the at32 new clock configuration tool for configuration. */
crm_pll_config(CRM_PLL_SOURCE_HEXT, 144, 1, CRM_PLL_FR_4);
/* enable pll */
crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);
/* wait till pll is ready */
while (crm_flag_get(CRM_PLL_STABLE_FLAG) != SET) {
}
/* config ahbclk */
crm_ahb_div_set(CRM_AHB_DIV_1); // 288mhz
/* config apb2clk, the maximum frequency of APB1/APB2 clock is 144 MHz */
crm_apb2_div_set(CRM_APB2_DIV_2); // 144mhz(288mhz / 2)
/* config apb1clk, the maximum frequency of APB1/APB2 clock is 144 MHz */
crm_apb1_div_set(CRM_APB1_DIV_2); // 144mhz(288mhz / 2)
/* enable auto step mode */
crm_auto_step_mode_enable(TRUE);
/* select pll as system clock source */
crm_sysclk_switch(CRM_SCLK_PLL);
/* wait till pll is used as system clock source */
while (crm_sysclk_switch_status_get() != CRM_SCLK_PLL) {
}
/* disable auto step mode */
crm_auto_step_mode_enable(FALSE);
/* update system_core_clock global variable */
system_core_clock_update();
}
/**
* @brief system clock config
* @note the system clock is configured as follow:
* system clock (sclk) = (hext * pll_ns)/(pll_ms * pll_fr)
* system clock source = pll (hext)
* - hext = HEXT_VALUE
* - sclk = 288000000
* - ahbdiv = 1
* - ahbclk = 288000000
* - apb2div = 2
* - apb2clk = 144000000
* - apb1div = 2
* - apb1clk = 144000000
* - pll_ns = 144
* - pll_ms = 1
* - pll_fr = 4
* @retval none
*/
void ConfigSystemClocks(void) {
// system_clock_config_48m();
system_clock_config_288m();
}
/**
* The state of GPIOB (especially PB0: arm power on) is preserved,
* while other peripherals are reset/clock-gated as much as possible, without using NVIC reset.
*/
void system_simple_reset(void) {
const uint32_t keep_gpiob_clock_mask = CRM_REG_BIT(AT32_GPIO_ARM_POWER_LOCK_CLK);
uint32_t ahb1_reset_mask =
CRM_REG_BIT(CRM_GPIOA_PERIPH_RESET) |
CRM_REG_BIT(CRM_GPIOC_PERIPH_RESET) |
CRM_REG_BIT(CRM_GPIOD_PERIPH_RESET) |
CRM_REG_BIT(CRM_GPIOE_PERIPH_RESET) |
CRM_REG_BIT(CRM_GPIOF_PERIPH_RESET) |
CRM_REG_BIT(CRM_GPIOG_PERIPH_RESET) |
CRM_REG_BIT(CRM_GPIOH_PERIPH_RESET) |
CRM_REG_BIT(CRM_CRC_PERIPH_RESET) |
CRM_REG_BIT(CRM_EDMA_PERIPH_RESET) |
CRM_REG_BIT(CRM_DMA1_PERIPH_RESET) |
CRM_REG_BIT(CRM_DMA2_PERIPH_RESET) |
CRM_REG_BIT(CRM_OTGFS2_PERIPH_RESET);
#if defined(AT32F437xx)
ahb1_reset_mask |= CRM_REG_BIT(CRM_EMAC_PERIPH_RESET);
#endif
__disable_irq();
/* Return to HICK first, then close PLL/HEXT and reset CRM clock tree settings. */
CRM->ctrl_bit.hicken = TRUE;
while (CRM->ctrl_bit.hickstbl != SET) {
}
CRM->cfg_bit.sclksel = CRM_SCLK_HICK;
while (CRM->cfg_bit.sclksts != CRM_SCLK_HICK) {
}
CRM->ctrl &= ~(0x010D0000U);
CRM->cfg = 0;
CRM->pllcfg = 0x00033002U;
CRM->misc1 = 0;
CRM->misc2 = 0;
/* Reset AHB peripherals directly, excluding GPIOB to keep PB0 state stable. */
CRM->ahbrst1 = ahb1_reset_mask;
CRM->ahbrst2 = CRM_REG_BIT(CRM_DVP_PERIPH_RESET) |
CRM_REG_BIT(CRM_OTGFS1_PERIPH_RESET) |
CRM_REG_BIT(CRM_SDIO1_PERIPH_RESET);
CRM->ahbrst3 = CRM_REG_BIT(CRM_XMC_PERIPH_RESET) |
CRM_REG_BIT(CRM_QSPI1_PERIPH_RESET) |
CRM_REG_BIT(CRM_QSPI2_PERIPH_RESET) |
CRM_REG_BIT(CRM_SDIO2_PERIPH_RESET);
CRM->ahbrst1 = 0;
CRM->ahbrst2 = 0;
CRM->ahbrst3 = 0;
/* Disable all peripheral clocks directly, but keep GPIOB clock for PB0 control path. */
CRM->ahben1 = keep_gpiob_clock_mask;
CRM->ahben2 = 0;
CRM->ahben3 = 0;
CRM->apb1rst = 0xFFFF;
CRM->apb1rst = 0;
CRM->apb1en = 0;
CRM->apb2rst = 0xFFFF;
CRM->apb2rst = 0;
CRM->apb2en = 0;
CRM->clkint = 0x009F0000U;
// Write BPR_1 before jump to bootrom to restart.
at32_bpr_write_dt1(SYS_SIMPLE_RESET_BPR_MAGIC);
// Jump to bootrom
JumpToAnyImage((uint32_t) _stack_end, (uint32_t) _bootrom_start);
}
// Refer to the code described in the following link to implement the jump.
// https://community.st.com/t5/stm32-mcus-products/jump-to-application-from-bootloader-not-working/td-p/620734
void __NO_RETURN JumpToAnyImage(uint32_t stack_top, uint32_t entry_point) {
// Disable and clear all pending interrupts in the Bootloader
__disable_irq();
for (int i = 0; i < sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0]); i++) {
NVIC->ICER[i] = 0xFFFFFFFF;
NVIC->ICPR[i] = 0xFFFFFFFF;
}
// Disable SysTick
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
SCB->VTOR = entry_point; // Update the Vector Table Offset Register (VTOR)
__set_MSP(stack_top); // Set the Main Stack Pointer to the App's stack address
__DSB(); // Ensure the VTOR and SP operations are complete
__ISB(); // Flush the pipeline because of SP change
// Re-enable all interrupts before new application running.
__enable_irq();
// Run the Application Reset Handler
uint32_t reset = *(uint32_t *)(entry_point + 4);
((void (*)(void))(reset | 1U))();
while (1); // No Warning.
}
+116
View File
@@ -0,0 +1,116 @@
#ifndef SYS_HW_AT32_H
#define SYS_HW_AT32_H
#include "common.h"
#include "at32f435_437_misc.h"
#include "at32f435_437_crm.h"
#include "at32f435_437_pwc.h"
/**
* What's method for system reset we are using?
* 0(default): system_simple_reset() + jump to bootrom
* 1: nvic_system_reset() [NOT IMPLEMENT]
*/
extern uint8_t g_system_reset_method;
// --- Handlers
void NMI_Handler(void);
void HardFault_Handler(void);
void MemManage_Handler(void);
void BusFault_Handler(void);
void UsageFault_Handler(void);
void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
// ---
void system_clock_config_48m(void);
void system_clock_config_288m(void);
void system_simple_reset(void);
bool system_bpr_chk_clear(void);
STATIC_FORCE_INLINE main_chip_type_t GetChipType(void) {
return MAIN_CHIP_TYPE_AT32;
}
STATIC_FORCE_INLINE uint32_t GetChipId(void) {
// DEBUG_IDCODE
return *((uint32_t *) 0xE0042000);
}
STATIC_FORCE_INLINE uint8_t *GetChipUniqueId(uint8_t *size) {
// See: Unique device ID register, doc 1.3.2
// The unique device ID is a 96-bit value that is programmed by the manufacturer.
// It is used to uniquely identify each device and can be used for various purposes such as licensing, security, and tracking.
if (size) {
*size = 12; // 96 bits = 12 bytes
}
return (uint8_t *) 0x1FFFF7E8;
}
STATIC_FORCE_INLINE void ResetChip(void) {
// Which reset method should be used?
if (g_system_reset_method == 0) {
// Call system_simple_reset() to simply reset the state of most peripherals, and jump to boot.
system_simple_reset();
} else {
// On current hardware, resetting the NVIC will cause the ARM_POWER_ON pin to return to input mode. However,
// it takes 10ms to 25ms from RESET to startup, during which time the device has already been powered off.
// If future hardware support keeping the GPIO level of ARM_POWER_ON during RESET, then NVIC reset can be enabled.
nvic_system_reset();
}
}
STATIC_FORCE_INLINE uint32_t GetChipFlashSize(void) {
// See: Flash capacity register, doc 1.3.1
// Flash storage capacity, measured in KBytes
// For example: 0x0080 = 128KByte
return *((uint32_t *) 0x1FFFF7E0) * 1024; // '<< 10' or '* 1024', Best to reduce the difficulty of understanding.
}
STATIC_FORCE_INLINE bool CheckRSTWithSRAMRetention(void) {
if (system_bpr_chk_clear()) {
crm_flag_clear(CRM_ALL_RESET_FLAG);
return true;
}
// If enter Standby Mode, the sram will power off.
if (CRM->ctrlsts_bit.lprstf && PWC->ctrlsts_bit.swef && PWC->ctrlsts_bit.sef) {
crm_flag_clear(CRM_ALL_RESET_FLAG);
return false;
}
// WDT reset & WWDT reset
if (CRM->ctrlsts_bit.wwdtrstf || CRM->ctrlsts_bit.wdtrstf) {
crm_flag_clear(CRM_ALL_RESET_FLAG);
return true;
}
// CPU software reset
if (CRM->ctrlsts_bit.swrstf) {
crm_flag_clear(CRM_ALL_RESET_FLAG);
return true;
}
// NRST reset (pin)
// When powered on for the first time, nrstf will also be set, which we need to confirm together with por.
if (CRM->ctrlsts_bit.nrstf && CRM->ctrlsts_bit.porrstf == 0 && CRM->ctrlsts_bit.swrstf == 0) {
crm_flag_clear(CRM_ALL_RESET_FLAG);
return true;
}
// POR/LVR reset flag? Or otherwise case...
crm_flag_clear(CRM_ALL_RESET_FLAG);
return false;
}
#endif //SYS_HW_AT32_H
+93
View File
@@ -0,0 +1,93 @@
//
// Created by dxl on 2026/5/23.
//
#include "sys_apis.h"
#include "proxmark3_arm.h"
#include "at91sam7s512.h"
void mck_from_pll_to_slck(void) {
// switch main clk to slow clk, first CSS then PRES
AT91C_BASE_PMC->PMC_MCKR = AT91C_PMC_PRES_CLK_2 | AT91C_PMC_CSS_SLOW_CLK;
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY)) {};
AT91C_BASE_PMC->PMC_MCKR = AT91C_PMC_PRES_CLK | AT91C_PMC_CSS_SLOW_CLK;
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY)) {};
// disable the PLL
AT91C_BASE_PMC->PMC_PLLR = 0x0;
// disable main oscillator
AT91C_BASE_PMC->PMC_MOR = 0;
}
void mck_from_slck_to_pll(void) {
// worst case scenario, with MAINCK = 16MHz xtal, startup delay is 1.4ms
// if SLCK slow clock runs at its worst case (max) frequency of 42kHz
// max startup delay = (1.4ms*42k)/8 = 7.356 so round up to 8
// UPDATE:
// we observed on 10% of the devices very wrong initial slow clock RC TIA measures.
// Bumping delay to 16 helps fixing the issue even on the most screwed RC.
// enable main oscillator and set startup delay
AT91C_BASE_PMC->PMC_MOR =
AT91C_CKGR_MOSCEN |
PMC_MAIN_OSC_STARTUP_DELAY(16);
// wait for main oscillator to stabilize
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MOSCS)) {};
// PLL output clock frequency in range 80 - 160 MHz needs CKGR_PLL = 00
// PLL output clock frequency in range 150 - 180 MHz needs CKGR_PLL = 10
// PLL output is MAINCK * multiplier / divisor = 16MHz * 12 / 2 = 96MHz
AT91C_BASE_PMC->PMC_PLLR =
PMC_PLL_DIVISOR(2) |
//PMC_PLL_COUNT_BEFORE_LOCK(0x10) |
PMC_PLL_COUNT_BEFORE_LOCK(0x3F) |
PMC_PLL_FREQUENCY_RANGE(0) |
PMC_PLL_MULTIPLIER(12) |
PMC_PLL_USB_DIVISOR(1);
// wait for PLL to lock
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_LOCK)) {};
// we want a master clock (MCK) to be PLL clock / 2 = 96MHz / 2 = 48MHz
// datasheet recommends that this register is programmed in two operations
// when changing to PLL, program the prescaler first then the source
AT91C_BASE_PMC->PMC_MCKR = AT91C_PMC_PRES_CLK_2 | AT91C_PMC_CSS_SLOW_CLK;
// wait for main clock ready signal
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY)) {};
// set the source to PLL
AT91C_BASE_PMC->PMC_MCKR = AT91C_PMC_PRES_CLK_2 | AT91C_PMC_CSS_PLL_CLK;
// wait for main clock ready signal
while (!(AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY)) {};
}
void ConfigSystemClocks(void) {
// we are using a 16 MHz crystal as the basis for everything
// slow clock runs at 32kHz typical regardless of crystal
// enable system clock and USB clock
AT91C_BASE_PMC->PMC_SCER |= AT91C_PMC_PCK | AT91C_PMC_UDP;
// enable the clock to the following peripherals
AT91C_BASE_PMC->PMC_PCER =
(1 << AT91C_ID_PIOA) |
(1 << AT91C_ID_ADC) |
(1 << AT91C_ID_SPI) |
(1 << AT91C_ID_SSC) |
(1 << AT91C_ID_PWMC) |
(1 << AT91C_ID_UDP);
mck_from_slck_to_pll();
}
void __attribute__((noreturn)) JumpToAnyImage(uint32_t stack_top, uint32_t entry_point) {
// Set stack top pointer
__asm("mov sp, %0\n" : : "r"(stack_top));
// jump to Flash address of the osimage(any image) entry point (LSBit set for thumb mode)
__asm("bx %0\n" : : "r"(((uint32_t)entry_point) | 0x1));
while (1); // No warning.
}
+75
View File
@@ -0,0 +1,75 @@
#ifndef SYS_HW_AT91_H
#define SYS_HW_AT91_H
#include "common.h"
#include "at91sam7s512.h"
#include "proxmark3_arm.h"
void mck_from_pll_to_slck(void);
void mck_from_slck_to_pll(void);
STATIC_FORCE_INLINE main_chip_type_t GetChipType(void) {
return MAIN_CHIP_TYPE_AT91;
}
STATIC_FORCE_INLINE uint32_t GetChipId(void) {
return *(AT91C_DBGU_CIDR);
}
STATIC_FORCE_INLINE uint8_t* GetChipUniqueId(uint8_t *size) {
// !!! UNSUPPORTED !!!
if (size) {
*size = 0;
}
return NULL;
}
STATIC_FORCE_INLINE void ResetChip(void) {
AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
}
STATIC_FORCE_INLINE uint32_t GetChipFlashSize(void) {
uint8_t nvpsiz = (GetChipId() & 0xF00) >> 8;
if (nvpsiz == 0) {
return 0;
}
if (nvpsiz == 1) {
return 8 * 1024;
}
if (nvpsiz == 2) {
return 16 * 1024;
}
if (nvpsiz == 3) {
return 32 * 1024;
}
if (nvpsiz == 5) {
return 64 * 1024;
}
if (nvpsiz == 7) {
return 128 * 1024;
}
if (nvpsiz == 9) {
return 256 * 1024;
}
if (nvpsiz == 10) {
return 512 * 1024;
}
if (nvpsiz == 12) {
return 1024 * 1024;
}
// for 'reserved' values, guess 2MB
return 2048 * 1024;
}
STATIC_FORCE_INLINE bool CheckRSTWithSRAMRetention(void) {
if ((AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_RSTTYP) == AT91C_RSTC_RSTTYP_WATCHDOG ||
(AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_RSTTYP) == AT91C_RSTC_RSTTYP_SOFTWARE ||
(AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_RSTTYP) == AT91C_RSTC_RSTTYP_USER) {
return true;
}
/* Otherwise, initialize it from scratch */
return false;
}
#endif //SYS_HW_AT91_H