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/nvic.h"
27 #include "drivers/system.h"
29 #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
31 #define BOOTLOADER_MAGIC 0xDEADBEEF
32 // 40KB SRAM STM32F30X
33 #define BOOT_TARGET_REGISTER ((uint32_t *)0x20009FFC)
35 #define DEFAULT_STACK_POINTER ((uint32_t *)0x1FFFD800)
36 #define SYSTEM_MEMORY_RESET_VECTOR ((uint32_t *) 0x1FFFD804)
40 void systemReset(void)
42 // Generate system reset
43 SCB
->AIRCR
= AIRCR_VECTKEY_MASK
| (uint32_t)0x04;
46 void systemResetToBootloader(bootloaderRequestType_e requestType
)
49 // 1FFFF000 -> 20000200 -> SP
50 // 1FFFF004 -> 1FFFF021 -> PC
52 *BOOT_TARGET_REGISTER
= BOOTLOADER_MAGIC
;
58 void enableGPIOPowerUsageAndNoiseReductions(void)
62 bool isMPUSoftReset(void)
64 if (cachedRccCsrValue
& RCC_CSR_SFTRSTF
)
70 static void checkForBootLoaderRequest(void)
72 if (*BOOT_TARGET_REGISTER
== BOOTLOADER_MAGIC
) {
74 *BOOT_TARGET_REGISTER
= 0x0;
77 __set_MSP(*DEFAULT_STACK_POINTER
);
79 ((void(*)(void))(*SYSTEM_MEMORY_RESET_VECTOR
))();
87 checkForBootLoaderRequest();
90 SCB
->CPACR
= (0x3 << (10 * 2)) | (0x3 << (11 * 2));
93 // Configure NVIC preempt/priority groups
94 NVIC_PriorityGroupConfig(NVIC_PRIORITY_GROUPING
);
96 // cache RCC->CSR value to use it in isMPUSoftReset() and others
97 cachedRccCsrValue
= RCC
->CSR
;
100 enableGPIOPowerUsageAndNoiseReductions();
102 // Init cycle counter
106 SysTick_Config(SystemCoreClock
/ 1000);