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
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/>.
27 #include "drivers/exti.h"
28 #include "drivers/nvic.h"
29 #include "drivers/system.h"
30 #include "drivers/persistent.h"
31 #include "at32f435_437_clock.h"
33 // See RM_AT32F435_437_EN_V2.05.pdf reference manual table 5-6 for more info.
34 #if 256 < TARGET_FLASH_SIZE
35 #define USD_EOPB0_SRAM_CONFIG_MASK 0x7
37 #define USD_EOPB0_SRAM_CONFIG_MASK 0x3
40 static flash_usd_eopb0_type
get_sram_config(void)
42 extern uint32_t _SRAM_SIZE
; // Defined in linker file
43 switch ((uint32_t)&_SRAM_SIZE
) {
44 #if 256 == TARGET_FLASH_SIZE
46 return FLASH_EOPB0_SRAM_448K
;
48 return FLASH_EOPB0_SRAM_512K
;
51 return FLASH_EOPB0_SRAM_384K
;
52 #elif 448 == TARGET_FLASH_SIZE
54 return FLASH_EOPB0_SRAM_256K
;
56 return FLASH_EOPB0_SRAM_320K
;
58 return FLASH_EOPB0_SRAM_384K
;
60 return FLASH_EOPB0_SRAM_448K
;
62 return FLASH_EOPB0_SRAM_512K
;
65 return FLASH_EOPB0_SRAM_192K
;
66 #elif 1024 <= TARGET_FLASH_SIZE
68 return FLASH_EOPB0_SRAM_128K
;
70 return FLASH_EOPB0_SRAM_256K
;
72 return FLASH_EOPB0_SRAM_320K
;
74 return FLASH_EOPB0_SRAM_384K
;
76 return FLASH_EOPB0_SRAM_448K
;
78 return FLASH_EOPB0_SRAM_512K
;
81 return FLASH_EOPB0_SRAM_192K
;
86 static void init_sram_config(void)
88 // Make sure the SRAM config is correct
89 const flash_usd_eopb0_type sram_cfg
= get_sram_config();
90 if (((USD
->eopb0
) & USD_EOPB0_SRAM_CONFIG_MASK
) != sram_cfg
) {
92 flash_user_system_data_erase();
93 flash_eopb0_config(sram_cfg
);
98 void systemReset(void)
104 void systemResetToBootloader(bootloaderRequestType_e requestType
)
106 switch (requestType
) {
107 case BOOTLOADER_REQUEST_ROM
:
109 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON
, RESET_BOOTLOADER_REQUEST_ROM
);
118 typedef void resetHandler_t(void);
120 typedef struct isrVector_s
{
121 __I
uint32_t stackEnd
;
122 resetHandler_t
*resetHandler
;
125 static void checkForBootLoaderRequest(void)
127 volatile uint32_t bootloaderRequest
= persistentObjectRead(PERSISTENT_OBJECT_RESET_REASON
);
129 if (bootloaderRequest
!= RESET_BOOTLOADER_REQUEST_ROM
) {
132 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON
, RESET_NONE
);
134 extern isrVector_t system_isr_vector_table_base
;
136 __set_MSP(system_isr_vector_table_base
.stackEnd
);
137 system_isr_vector_table_base
.resetHandler();
141 void enableGPIOPowerUsageAndNoiseReductions(void)
143 //enable all needed periph
144 crm_periph_clock_enable(
145 CRM_GPIOA_PERIPH_CLOCK
|
146 CRM_GPIOB_PERIPH_CLOCK
|
147 CRM_GPIOC_PERIPH_CLOCK
|
148 CRM_GPIOD_PERIPH_CLOCK
|
149 CRM_GPIOE_PERIPH_CLOCK
|
150 CRM_DMA1_PERIPH_CLOCK
|
151 CRM_DMA2_PERIPH_CLOCK
|
155 bool isMPUSoftReset(void)
157 if (cachedRccCsrValue
& CRM_SW_RESET_FLAG
)
163 void systemInit(void)
167 persistentObjectInit();
169 checkForBootLoaderRequest();
171 system_clock_config();//config system clock to 288mhz usb 48mhz
173 // Configure NVIC preempt/priority groups
174 nvic_priority_group_config(NVIC_PRIORITY_GROUPING
);
176 // cache RCC->CSR value to use it in isMPUSoftReset() and others
177 cachedRccCsrValue
= CRM
->ctrlsts
;
179 // Although VTOR is already loaded with a possible vector table in RAM,
180 // removing the call to NVIC_SetVectorTable causes USB not to become active,
181 extern uint8_t isr_vector_table_base
;
182 nvic_vector_table_set((uint32_t)&isr_vector_table_base
, 0x0);
184 crm_periph_clock_enable(CRM_OTGFS2_PERIPH_CLOCK
|CRM_OTGFS1_PERIPH_CLOCK
,FALSE
);
186 CRM
->ctrlsts_bit
.rstfc
= TRUE
;
188 enableGPIOPowerUsageAndNoiseReductions();
190 // Init cycle counter
194 SysTick_Config(system_core_clock
/ 1000);