Auto updated submodule references [18-01-2025]
[betaflight.git] / src / platform / APM32 / system_apm32f4xx.c
blob0bf5b42231d2870c9bbb951a05a73b1230c1b49a
1 /*
2 * This file is part of Betaflight.
4 * Betaflight is free software. You can redistribute this software
5 * and/or modify this software under the terms of the GNU General
6 * Public License as published by the Free Software Foundation,
7 * either version 3 of the License, or (at your option) any later
8 * version.
10 * Betaflight is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this software.
19 * If not, see <http://www.gnu.org/licenses/>.
22 #include <stdbool.h>
23 #include <stdint.h>
25 #include "platform.h"
27 #include "drivers/accgyro/accgyro_mpu.h"
28 #include "drivers/exti.h"
29 #include "drivers/nvic.h"
30 #include "drivers/system.h"
31 #include "drivers/persistent.h"
33 void systemReset(void)
35 __disable_irq();
36 NVIC_SystemReset();
39 void systemResetToBootloader(bootloaderRequestType_e requestType)
41 switch (requestType) {
42 case BOOTLOADER_REQUEST_ROM:
43 default:
44 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_BOOTLOADER_REQUEST_ROM);
46 break;
49 __disable_irq();
50 NVIC_SystemReset();
53 typedef void resetHandler_t(void);
55 typedef struct isrVector_s {
56 __I uint32_t stackEnd;
57 resetHandler_t *resetHandler;
58 } isrVector_t;
60 // Used in the startup files for F4
61 void checkForBootLoaderRequest(void)
63 uint32_t bootloaderRequest = persistentObjectRead(PERSISTENT_OBJECT_RESET_REASON);
65 if (bootloaderRequest != RESET_BOOTLOADER_REQUEST_ROM) {
66 return;
68 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_NONE);
70 extern isrVector_t system_isr_vector_table_base;
72 __set_MSP(system_isr_vector_table_base.stackEnd);
73 system_isr_vector_table_base.resetHandler();
74 while (1);
77 void enableGPIOPowerUsageAndNoiseReductions(void)
79 __DAL_RCM_BKPSRAM_CLK_ENABLE();
80 __DAL_RCM_DMA1_CLK_ENABLE();
81 __DAL_RCM_DMA2_CLK_ENABLE();
83 __DAL_RCM_TMR2_CLK_ENABLE();
84 __DAL_RCM_TMR3_CLK_ENABLE();
85 __DAL_RCM_TMR4_CLK_ENABLE();
86 __DAL_RCM_TMR5_CLK_ENABLE();
87 __DAL_RCM_TMR6_CLK_ENABLE();
88 __DAL_RCM_TMR7_CLK_ENABLE();
89 __DAL_RCM_TMR12_CLK_ENABLE();
90 __DAL_RCM_TMR13_CLK_ENABLE();
91 __DAL_RCM_TMR14_CLK_ENABLE();
92 __DAL_RCM_WWDT_CLK_ENABLE();
93 __DAL_RCM_SPI2_CLK_ENABLE();
94 __DAL_RCM_SPI3_CLK_ENABLE();
95 __DAL_RCM_USART2_CLK_ENABLE();
96 __DAL_RCM_USART3_CLK_ENABLE();
97 __DAL_RCM_UART4_CLK_ENABLE();
98 __DAL_RCM_UART5_CLK_ENABLE();
99 __DAL_RCM_I2C1_CLK_ENABLE();
100 __DAL_RCM_I2C2_CLK_ENABLE();
101 __DAL_RCM_I2C3_CLK_ENABLE();
102 __DAL_RCM_CAN1_CLK_ENABLE();
103 __DAL_RCM_CAN2_CLK_ENABLE();
104 __DAL_RCM_PMU_CLK_ENABLE();
105 __DAL_RCM_DAC_CLK_ENABLE();
107 __DAL_RCM_TMR1_CLK_ENABLE();
108 __DAL_RCM_TMR8_CLK_ENABLE();
109 __DAL_RCM_USART1_CLK_ENABLE();
110 __DAL_RCM_USART6_CLK_ENABLE();
111 __DAL_RCM_ADC1_CLK_ENABLE();
112 __DAL_RCM_ADC2_CLK_ENABLE();
113 __DAL_RCM_ADC3_CLK_ENABLE();
114 __DAL_RCM_SDIO_CLK_ENABLE();
115 __DAL_RCM_SPI1_CLK_ENABLE();
116 __DAL_RCM_SYSCFG_CLK_ENABLE();
117 __DAL_RCM_TMR9_CLK_ENABLE();
118 __DAL_RCM_TMR10_CLK_ENABLE();
119 __DAL_RCM_TMR11_CLK_ENABLE();
122 bool isMPUSoftReset(void)
124 if (cachedRccCsrValue & RCM_CSTS_SWRSTFLG)
125 return true;
126 else
127 return false;
130 void systemInit(void)
132 persistentObjectInit();
134 checkForBootLoaderRequest();
136 #ifdef USE_DAL_DRIVER
137 DAL_Init();
138 #endif
140 /* Configure the System clock source, PLL1 and HCLK */
141 DAL_SysClkConfig();
143 /* Update SystemCoreClock variable */
144 SystemCoreClockUpdate();
146 // Configure NVIC preempt/priority groups
147 DAL_NVIC_SetPriorityGrouping(NVIC_PRIORITY_GROUPING);
149 // cache RCM->CSTS value to use it in isMPUSoftReset() and others
150 cachedRccCsrValue = READ_REG(RCM->CSTS);
152 // Although VTOR is already loaded with a possible vector table in RAM,
153 // removing the call to NVIC_SetVectorTable causes USB not to become active,
155 extern uint8_t isr_vector_table_base;
156 SCB->VTOR = (uint32_t)&isr_vector_table_base | (0x0 & (uint32_t)0x1FFFFF80);
158 // Disable USB OTG FS clock
159 __DAL_RCM_USB_OTG_FS_CLK_DISABLE();
161 // Clear reset flags
162 SET_BIT(RCM->CSTS, RCM_CSTS_RSTFLGCLR);
164 enableGPIOPowerUsageAndNoiseReductions();
166 // Init cycle counter
167 cycleCounterInit();
169 //--Todo: Set systick here ? // SysTick is updated whenever DAL_RCM_ClockConfig is called.