Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / system_stm32f30x.c
blob7d3db84a0d1adf61c948dff7fed777b799e7465c
1 /*
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)
8 * any later version.
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/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
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)
38 void SetSysClock();
40 void systemReset(void)
42 // Generate system reset
43 SCB->AIRCR = AIRCR_VECTKEY_MASK | (uint32_t)0x04;
46 void systemResetToBootloader(bootloaderRequestType_e requestType)
48 UNUSED(requestType);
49 // 1FFFF000 -> 20000200 -> SP
50 // 1FFFF004 -> 1FFFF021 -> PC
52 *BOOT_TARGET_REGISTER = BOOTLOADER_MAGIC;
54 systemReset();
58 void enableGPIOPowerUsageAndNoiseReductions(void)
62 bool isMPUSoftReset(void)
64 if (cachedRccCsrValue & RCC_CSR_SFTRSTF)
65 return true;
66 else
67 return false;
70 static void checkForBootLoaderRequest(void)
72 if (*BOOT_TARGET_REGISTER == BOOTLOADER_MAGIC) {
74 *BOOT_TARGET_REGISTER = 0x0;
76 __enable_irq();
77 __set_MSP(*DEFAULT_STACK_POINTER);
79 ((void(*)(void))(*SYSTEM_MEMORY_RESET_VECTOR))();
81 while (1);
85 void systemInit(void)
87 checkForBootLoaderRequest();
89 // Enable FPU
90 SCB->CPACR = (0x3 << (10 * 2)) | (0x3 << (11 * 2));
91 SetSysClock();
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;
98 RCC_ClearFlag();
100 enableGPIOPowerUsageAndNoiseReductions();
102 // Init cycle counter
103 cycleCounterInit();
105 // SysTick
106 SysTick_Config(SystemCoreClock / 1000);