Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / drivers / system_stm32f10x.c
blob77bdedaac731b7de64bc84bf032c8ae33e9f1e70
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 // from system_stm32f10x.c
32 void SetSysClock(bool overclock);
34 void systemReset(void)
36 // Generate system reset
37 SCB->AIRCR = AIRCR_VECTKEY_MASK | (uint32_t)0x04;
40 void systemResetToBootloader(bootloaderRequestType_e requestType)
42 UNUSED(requestType);
44 // 1FFFF000 -> 20000200 -> SP
45 // 1FFFF004 -> 1FFFF021 -> PC
47 *((uint32_t *)0x20004FF0) = 0xDEADBEEF; // 20KB STM32F103
48 systemReset();
51 static void checkForBootLoaderRequest(void)
53 void(*bootJump)(void);
55 if (*((uint32_t *)0x20004FF0) == 0xDEADBEEF) {
57 *((uint32_t *)0x20004FF0) = 0x0;
59 __enable_irq();
60 __set_MSP(*((uint32_t *)0x1FFFF000));
62 bootJump = (void(*)(void))(*((uint32_t *) 0x1FFFF004));
63 bootJump();
64 while (1);
68 void enableGPIOPowerUsageAndNoiseReductions(void)
72 bool isMPUSoftReset(void)
74 if (cachedRccCsrValue & RCC_CSR_SFTRSTF)
75 return true;
76 else
77 return false;
80 void systemInit(void)
82 checkForBootLoaderRequest();
84 SetSysClock(false);
86 #if defined(OPBL)
87 /* Accounts for OP Bootloader, set the Vector Table base address as specified in .ld file */
88 extern void *isr_vector_table_base;
90 NVIC_SetVectorTable((uint32_t)&isr_vector_table_base, 0x0);
91 #endif
92 // Configure NVIC preempt/priority groups
93 NVIC_PriorityGroupConfig(NVIC_PRIORITY_GROUPING);
95 // Turn on clocks for stuff we use
96 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
98 // cache RCC->CSR value to use it in isMPUSoftReset() and others
99 cachedRccCsrValue = RCC->CSR;
100 RCC_ClearFlag();
102 enableGPIOPowerUsageAndNoiseReductions();
104 // Set USART1 TX (PA9) to output and high state to prevent a rs232 break condition on reset.
105 // See issue https://github.com/cleanflight/cleanflight/issues/1433
106 GPIO_InitTypeDef GPIO_InitStructure = {
107 .GPIO_Mode = GPIO_Mode_Out_PP,
108 .GPIO_Pin = GPIO_Pin_9,
109 .GPIO_Speed = GPIO_Speed_2MHz
112 GPIOA->BSRR = GPIO_InitStructure.GPIO_Pin;
113 GPIO_Init(GPIOA, &GPIO_InitStructure);
115 // Turn off JTAG port 'cause we're using the GPIO for leds
116 #define AFIO_MAPR_SWJ_CFG_NO_JTAG_SW (0x2 << 24)
117 AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_NO_JTAG_SW;
119 // Init cycle counter
120 cycleCounterInit();
122 // SysTick
123 SysTick_Config(SystemCoreClock / 1000);