2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
26 #include "drivers/exti.h"
27 #include "drivers/nvic.h"
28 #include "drivers/system.h"
29 #include "drivers/persistent.h"
30 #include "at32f435_437_clock.h"
32 // See RM_AT32F435_437_EN_V2.05.pdf reference manual table 5-6 for more info.
33 #if 256 < TARGET_FLASH_SIZE
34 #define USD_EOPB0_SRAM_CONFIG_MASK 0x7
36 #define USD_EOPB0_SRAM_CONFIG_MASK 0x3
39 static flash_usd_eopb0_type
get_sram_config(void)
41 extern uint32_t _SRAM_SIZE
; // Defined in linker file
42 switch ((uint32_t)&_SRAM_SIZE
) {
43 #if 256 == TARGET_FLASH_SIZE
45 return FLASH_EOPB0_SRAM_448K
;
47 return FLASH_EOPB0_SRAM_512K
;
50 return FLASH_EOPB0_SRAM_384K
;
51 #elif 448 == TARGET_FLASH_SIZE
53 return FLASH_EOPB0_SRAM_256K
;
55 return FLASH_EOPB0_SRAM_320K
;
57 return FLASH_EOPB0_SRAM_384K
;
59 return FLASH_EOPB0_SRAM_448K
;
61 return FLASH_EOPB0_SRAM_512K
;
64 return FLASH_EOPB0_SRAM_192K
;
65 #elif 1024 <= TARGET_FLASH_SIZE
67 return FLASH_EOPB0_SRAM_128K
;
69 return FLASH_EOPB0_SRAM_256K
;
71 return FLASH_EOPB0_SRAM_320K
;
73 return FLASH_EOPB0_SRAM_384K
;
75 return FLASH_EOPB0_SRAM_448K
;
77 return FLASH_EOPB0_SRAM_512K
;
80 return FLASH_EOPB0_SRAM_192K
;
85 static void init_sram_config(void)
87 // Make sure the SRAM config is correct
88 const flash_usd_eopb0_type sram_cfg
= get_sram_config();
89 if (((USD
->eopb0
) & USD_EOPB0_SRAM_CONFIG_MASK
) != sram_cfg
) {
91 flash_user_system_data_erase();
92 flash_eopb0_config(sram_cfg
);
97 void systemReset(void)
103 void systemResetToBootloader(bootloaderRequestType_e requestType
)
105 switch (requestType
) {
106 case BOOTLOADER_REQUEST_ROM
:
108 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON
, RESET_BOOTLOADER_REQUEST_ROM
);
117 typedef void resetHandler_t(void);
119 typedef struct isrVector_s
{
120 __I
uint32_t stackEnd
;
121 resetHandler_t
*resetHandler
;
124 void checkForBootLoaderRequest(void)
126 volatile uint32_t bootloaderRequest
= persistentObjectRead(PERSISTENT_OBJECT_RESET_REASON
);
128 if (bootloaderRequest
!= RESET_BOOTLOADER_REQUEST_ROM
) {
131 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON
, RESET_NONE
);
133 extern isrVector_t system_isr_vector_table_base
;
135 __set_MSP(system_isr_vector_table_base
.stackEnd
);
136 system_isr_vector_table_base
.resetHandler();
140 void enableGPIOPowerUsageAndNoiseReductions(void)
142 //enable all needed periph
143 crm_periph_clock_enable(
144 CRM_GPIOA_PERIPH_CLOCK
|
145 CRM_GPIOB_PERIPH_CLOCK
|
146 CRM_GPIOC_PERIPH_CLOCK
|
147 CRM_GPIOD_PERIPH_CLOCK
|
148 CRM_GPIOE_PERIPH_CLOCK
|
149 CRM_DMA1_PERIPH_CLOCK
|
150 CRM_DMA2_PERIPH_CLOCK
|
154 bool isMPUSoftReset(void)
156 if (cachedRccCsrValue
& CRM_SW_RESET_FLAG
)
162 void systemInit(void)
166 persistentObjectInit();
168 checkForBootLoaderRequest();
170 system_clock_config();//config system clock to 288mhz usb 48mhz
172 // Configure NVIC preempt/priority groups
173 nvic_priority_group_config(NVIC_PRIORITY_GROUPING
);
175 // cache RCC->CSR value to use it in isMPUSoftReset() and others
176 cachedRccCsrValue
= CRM
->ctrlsts
;
178 // Although VTOR is already loaded with a possible vector table in RAM,
179 // removing the call to NVIC_SetVectorTable causes USB not to become active,
180 extern uint8_t isr_vector_table_base
;
181 nvic_vector_table_set((uint32_t)&isr_vector_table_base
, 0x0);
183 crm_periph_clock_enable(CRM_OTGFS2_PERIPH_CLOCK
|CRM_OTGFS1_PERIPH_CLOCK
,FALSE
);
185 CRM
->ctrlsts_bit
.rstfc
= TRUE
;
187 enableGPIOPowerUsageAndNoiseReductions();
189 // Init cycle counter
193 SysTick_Config(system_core_clock
/ 1000);