44 #if defined(STM32H7XX)
52 static void initRam0nc(
void);
53 static void init_ram_areas(
const ram_init_area_t *rap);
54 static void mpuConfigureNonCachedRam(
void);
57 #if USE_HARD_FAULT_RECOVERY
59 #if defined(STM32F4XX) || defined (STM32F7XX)
60 #define BCKP_SECTION ".ram5"
61 #define IN_BCKP_SECTION(var) var __attribute__ ((section(BCKP_SECTION), aligned(8)))
62 #elif defined(STM32H7XX)
63 #define BCKP_SECTION ".ram7"
64 #define IN_BCKP_SECTION(var) var __attribute__ ((section(BCKP_SECTION), aligned(8)))
66 #error "No backup ram available"
68 IN_BCKP_SECTION(
volatile bool hard_fault);
75 CH_IRQ_HANDLER(HardFault_Handler)
81 CH_IRQ_HANDLER(NMI_Handler)
87 CH_IRQ_HANDLER(MemManage_Handler)
93 CH_IRQ_HANDLER(BusFault_Handler)
99 CH_IRQ_HANDLER(UsageFault_Handler)
105 bool recovering_from_hard_fault;
108 #if defined(STM32F4XX)
109 #define __PWR_BACKUP_REG PWR->CSR
110 #define __PWR_BACKUP_ENABLE PWR_CSR_BRE
111 #define __PWR_BACKUP_READY PWR_CSR_BRR
112 #define __RCC_RESET_REGISTER RCC->CSR
113 #define __RCC_RESET_FLAG RCC_CSR_SFTRSTF
114 #define __RCC_RESET_REMOVE_FLAG RCC_CSR_RMVF
115 #elif defined(STM32F7XX)
116 #define __PWR_BACKUP_REG PWR->CSR1
117 #define __PWR_BACKUP_ENABLE PWR_CSR1_BRE
118 #define __PWR_BACKUP_READY PWR_CSR1_BRR
119 #define __RCC_RESET_REGISTER RCC->CSR
120 #define __RCC_RESET_FLAG RCC_CSR_SFTRSTF
121 #define __RCC_RESET_REMOVE_FLAG RCC_CSR_RMVF
122 #elif defined(STM32H7XX)
123 #define __PWR_BACKUP_REG PWR->CR2
124 #define __PWR_BACKUP_ENABLE PWR_CR2_BREN
125 #define __PWR_BACKUP_READY PWR_CR2_BRRDY
126 #define __RCC_RESET_REGISTER RCC->RSR
127 #define __RCC_RESET_FLAG RCC_RSR_SFTRSTF
128 #define __RCC_RESET_REMOVE_FLAG RCC_RSR_RMVF
130 #error Hard fault recovery not supported
148 #if defined(USE_RTC_BACKUP)
167 #if defined(STM32H7XX)
168 mpuConfigureNonCachedRam();
171 #if USE_HARD_FAULT_RECOVERY
173 #if defined(STM32F4XX) || defined(STM32F7XX)
174 RCC->AHB1ENR |= RCC_AHB1ENR_BKPSRAMEN;
175 __PWR_BACKUP_REG |= __PWR_BACKUP_ENABLE;
176 while ((__PWR_BACKUP_REG & __PWR_BACKUP_READY) == 0) ;
180 recovering_from_hard_fault =
false;
181 if (!(__RCC_RESET_REGISTER & __RCC_RESET_FLAG)) {
184 }
else if ((__RCC_RESET_REGISTER & __RCC_RESET_FLAG) && !hard_fault) {
189 recovering_from_hard_fault =
true;
193 __RCC_RESET_REGISTER = __RCC_RESET_REMOVE_FLAG;
214 #if defined(USE_RTC_BACKUP)
232 #if defined(ENERGY_SAVE_INPUTS)
234 palSetLineMode(input_line, PAL_MODE_INPUT);
237 #if defined(ENERGY_SAVE_LOWS)
239 palClearLine(input_low);
254 #if defined(STM32F4XX)
256 PWR->CR &= ~(PWR_CR_PDDS | PWR_CR_LPDS);
258 PWR->CR |= (PWR_CR_LPDS | PWR_CR_CSBF | PWR_CR_CWUF);
259 #elif defined(STM32F7XX)
261 PWR->CR1 &= ~(PWR_CR1_PDDS | PWR_CR1_LPDS);
263 PWR->CR1 |= (PWR_CR1_LPDS | PWR_CR1_CSBF);
264 #elif defined(STM32H7XX)
266 PWR->CR1 &= ~PWR_CR1_LPDS;
268 PWR->CR1 |= PWR_CR1_LPDS;
272 SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
283 SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
286 #if defined(USE_RTC_BACKUP)
293 #if !defined(STM32F1)
294 if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) {
296 RCC->BDCR |= RCC_BDCR_RTCEN;
299 PWR->CR |= PWR_CR_DBP;
301 PWR->CR1 |= PWR_CR1_DBP;
307 dr[0] = (
val) & 0xFFFF;
309 #elif defined(STM32G4)
319 #if defined(STM32H7XX)
327 extern const uint32_t __ram0nc_base__;
328 extern const uint32_t __ram0nc_size__;
329 extern const uint32_t __ram3_base__;
330 extern const uint32_t __ram3_size__;
331 extern const uint32_t __ram4_base__;
332 extern const uint32_t __ram4_size__;
337 chDbgAssert(__builtin_popcount(ldSize) == 1U,
"MPU region size must be 2^n");
338 chDbgAssert(ldSize >= 32U,
"MPU region size must be >= 32");
339 return MPU_RASR_SIZE(__builtin_ctz(ldSize) - 1U);
343 static void mpuConfigureNonCachedRam(
void)
345 const uint32_t mpuSharedOption = MPU_RASR_ATTR_AP_RW_RW |
346 MPU_RASR_ATTR_NON_CACHEABLE | MPU_RASR_ATTR_S |
357 chDbgAssert(ram0nc_base == 0x24000000,
"MPU ram0nc addr mismatch");
358 chDbgAssert(ram3_base == 0x30040000,
"MPU ram3 addr mismatch");
359 chDbgAssert(ram4_base == 0x38000000,
"MPU ram4 addr mismatch");
361 chDbgAssert((ram0nc_base % ram0nc_size) == 0,
"MPU ram0nc base addr must be size aligned");
362 chDbgAssert(ram0nc_size == 128 * 1024,
"MPU ram0nc size must be 128K");
363 chDbgAssert((ram3_base % ram3_size) == 0,
"MPU ram3 base addr must be size aligned");
364 chDbgAssert((ram4_base % ram4_size) == 0,
"MPU ram4 base addr must be size aligned");
365 chDbgAssert(getMPU_RASR_SIZE(ram0nc_size) == MPU_RASR_SIZE_128K,
"getMPU_RASR_SIZE error");
368 mpuConfigureRegion(MPU_REGION_6,
370 getMPU_RASR_SIZE(ram0nc_size) | mpuSharedOption
372 mpuConfigureRegion(MPU_REGION_5,
374 getMPU_RASR_SIZE(ram3_size) | mpuSharedOption
376 mpuConfigureRegion(MPU_REGION_4,
378 getMPU_RASR_SIZE(ram4_size) | mpuSharedOption
381 mpuEnable(MPU_CTRL_PRIVDEFENA);
384 SCB_CleanInvalidateDCache();
388 static void initRam0nc(
void)
390 extern uint32_t __ram0nc_init_text__, __ram0nc_init__, __ram0nc_clear__, __ram0nc_noinit__;
391 static const ram_init_area_t ram_areas[1] = {
392 {&__ram0nc_init_text__, &__ram0nc_init__, &__ram0nc_clear__, &__ram0nc_noinit__},
394 init_ram_areas(ram_areas);
398 static void init_ram_areas(
const ram_init_area_t *rap)
404 while (p < rap->clear_area) {
411 while (p < rap->no_init_area) {
#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.
@ 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.
static void __enable_irq(void)
static void __disable_irq(void)
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.