54static void initRam0nc(
void);
55static void init_ram_areas(
const ram_init_area_t *rap);
56static void mpuConfigureNonCachedRam(
void);
59#if USE_HARD_FAULT_RECOVERY
61#if defined(STM32F4XX) || defined (STM32F7XX)
62#define BCKP_SECTION ".ram5"
63#define IN_BCKP_SECTION(var) var __attribute__ ((section(BCKP_SECTION), aligned(8)))
64#elif defined(STM32H7XX)
65#define BCKP_SECTION ".ram7"
66#define IN_BCKP_SECTION(var) var __attribute__ ((section(BCKP_SECTION), aligned(8)))
68#error "No backup ram available"
70IN_BCKP_SECTION(
volatile bool hard_fault);
77CH_IRQ_HANDLER(HardFault_Handler)
83CH_IRQ_HANDLER(NMI_Handler)
89CH_IRQ_HANDLER(MemManage_Handler)
95CH_IRQ_HANDLER(BusFault_Handler)
101CH_IRQ_HANDLER(UsageFault_Handler)
107bool recovering_from_hard_fault;
110#if defined(STM32F4XX)
111#define __PWR_BACKUP_REG PWR->CSR
112#define __PWR_BACKUP_ENABLE PWR_CSR_BRE
113#define __PWR_BACKUP_READY PWR_CSR_BRR
114#define __RCC_RESET_REGISTER RCC->CSR
115#define __RCC_RESET_FLAG RCC_CSR_SFTRSTF
116#define __RCC_RESET_REMOVE_FLAG RCC_CSR_RMVF
117#elif defined(STM32F7XX)
118#define __PWR_BACKUP_REG PWR->CSR1
119#define __PWR_BACKUP_ENABLE PWR_CSR1_BRE
120#define __PWR_BACKUP_READY PWR_CSR1_BRR
121#define __RCC_RESET_REGISTER RCC->CSR
122#define __RCC_RESET_FLAG RCC_CSR_SFTRSTF
123#define __RCC_RESET_REMOVE_FLAG RCC_CSR_RMVF
124#elif defined(STM32H7XX)
125#define __PWR_BACKUP_REG PWR->CR2
126#define __PWR_BACKUP_ENABLE PWR_CR2_BREN
127#define __PWR_BACKUP_READY PWR_CR2_BRRDY
128#define __RCC_RESET_REGISTER RCC->RSR
129#define __RCC_RESET_FLAG RCC_RSR_SFTRSTF
130#define __RCC_RESET_REMOVE_FLAG RCC_RSR_RMVF
132#error Hard fault recovery not supported
150#if defined(USE_RTC_BACKUP)
169#if defined(STM32H7XX)
170 mpuConfigureNonCachedRam();
173#if USE_HARD_FAULT_RECOVERY
175#if defined(STM32F4XX) || defined(STM32F7XX)
176 RCC->AHB1ENR |= RCC_AHB1ENR_BKPSRAMEN;
177 __PWR_BACKUP_REG |= __PWR_BACKUP_ENABLE;
178 while ((__PWR_BACKUP_REG & __PWR_BACKUP_READY) == 0) ;
182 recovering_from_hard_fault =
false;
183 if (!(__RCC_RESET_REGISTER & __RCC_RESET_FLAG)) {
186 }
else if ((__RCC_RESET_REGISTER & __RCC_RESET_FLAG) && !hard_fault) {
191 recovering_from_hard_fault =
true;
195 __RCC_RESET_REGISTER |= __RCC_RESET_REMOVE_FLAG;
216#if defined(USE_RTC_BACKUP)
234#if defined(ENERGY_SAVE_INPUTS)
236 palSetLineMode(input_line, PAL_MODE_INPUT);
239#if defined(ENERGY_SAVE_LOWS)
241 palClearLine(input_low);
256 memcpy(uid, (
const uint8_t *)UID_BASE, len);
275#if defined(STM32F4XX)
277 PWR->CR &= ~(PWR_CR_PDDS | PWR_CR_LPDS);
279 PWR->CR |= (PWR_CR_LPDS | PWR_CR_CSBF | PWR_CR_CWUF);
280#elif defined(STM32F7XX)
282 PWR->CR1 &= ~(PWR_CR1_PDDS | PWR_CR1_LPDS);
284 PWR->CR1 |= (PWR_CR1_LPDS | PWR_CR1_CSBF);
285#elif defined(STM32H7XX)
287 PWR->CR1 &= ~PWR_CR1_LPDS;
289 PWR->CR1 |= PWR_CR1_LPDS;
293 SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
304 SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
307#if defined(USE_RTC_BACKUP)
315 if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) {
317 RCC->BDCR |= RCC_BDCR_RTCEN;
320 PWR->CR |= PWR_CR_DBP;
322 PWR->CR1 |= PWR_CR1_DBP;
328 dr[0] = (
val) & 0xFFFF;
330#elif defined(STM32G4)
340#if defined(STM32H7XX)
348extern const uint32_t __ram0nc_base__;
349extern const uint32_t __ram0nc_size__;
358 chDbgAssert(__builtin_popcount(ldSize) == 1U,
"MPU region size must be 2^n");
359 chDbgAssert(ldSize >= 32U,
"MPU region size must be >= 32");
360 return MPU_RASR_SIZE(__builtin_ctz(ldSize) - 1U);
364static void mpuConfigureNonCachedRam(
void)
366 const uint32_t mpuSharedOption = MPU_RASR_ATTR_AP_RW_RW |
367 MPU_RASR_ATTR_NON_CACHEABLE | MPU_RASR_ATTR_S |
378 chDbgAssert(ram0nc_base == 0x24000000,
"MPU ram0nc addr mismatch");
379 chDbgAssert(ram3_base == 0x30040000,
"MPU ram3 addr mismatch");
380 chDbgAssert(ram4_base == 0x38000000,
"MPU ram4 addr mismatch");
382 chDbgAssert((ram0nc_base % ram0nc_size) == 0,
"MPU ram0nc base addr must be size aligned");
383 chDbgAssert(ram0nc_size == 128 * 1024,
"MPU ram0nc size must be 128K");
384 chDbgAssert((ram3_base % ram3_size) == 0,
"MPU ram3 base addr must be size aligned");
385 chDbgAssert((ram4_base % ram4_size) == 0,
"MPU ram4 base addr must be size aligned");
386 chDbgAssert(getMPU_RASR_SIZE(ram0nc_size) == MPU_RASR_SIZE_128K,
"getMPU_RASR_SIZE error");
389 mpuConfigureRegion(MPU_REGION_6,
391 getMPU_RASR_SIZE(ram0nc_size) | mpuSharedOption
393 mpuConfigureRegion(MPU_REGION_5,
395 getMPU_RASR_SIZE(ram3_size) | mpuSharedOption
397 mpuConfigureRegion(MPU_REGION_4,
399 getMPU_RASR_SIZE(ram4_size) | mpuSharedOption
402 mpuEnable(MPU_CTRL_PRIVDEFENA);
405 SCB_CleanInvalidateDCache();
409static void initRam0nc(
void)
411 extern uint32_t __ram0nc_init_text__, __ram0nc_init__, __ram0nc_clear__, __ram0nc_noinit__;
412 static const ram_init_area_t ram_areas[1] = {
413 {&__ram0nc_init_text__, &__ram0nc_init__, &__ram0nc_clear__, &__ram0nc_noinit__},
415 init_ram_areas(ram_areas);
419static void init_ram_areas(
const ram_init_area_t *rap)
#define BOARD_GROUP_DECLFOREACH(line, group)
rtc_boot_magic
RTC backup register values.
@ RTC_BOOT_FAST
No timeout in bootloader.
@ RTC_BOOT_OFF
Normal boot.
@ RTC_BOOT_CANBL
CAN bootloader, ORd with 8 bit local node ID.
@ RTC_BOOT_HOLD
Hold in bootloader, do not boot application.
@ RTC_BOOT_FWOK
indicates FW ran for 30s
static void mcu_deep_sleep(void)
Put MCU into deep sleep mode.
void mcu_arch_init(void)
Initialize the specific archittecture functions.
#define ENERGY_SAVE_INPUTS
void mcu_reboot(enum reboot_state_t reboot_state)
Reboot the MCU.
reboot_state_t
The requested reboot states.
void mcu_energy_save(void)
Save energy for performing operations on shutdown Used for example to shutdown SD-card logging.
uint8_t mcu_get_uid(uint8_t *uid, uint8_t max_len)
Get the 96-bit unique device ID of the STM32.
@ MCU_REBOOT_BOOTLOADER
Go to bootloader.
@ MCU_REBOOT_POWEROFF
Poweroff the device.
@ MCU_REBOOT_FAST
Fast reboot (skip bootloader).
Arch independent mcu ( Micro Controller Unit ) utilities.
Specific RAM section for DMA usage on F7.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.