Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / system_stm32g4xx.c
blobd9778728ec21b06291e7dafbf1a4532c0f609193
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <string.h>
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <stdlib.h>
23 #include "platform.h"
25 #include "drivers/accgyro/accgyro_mpu.h"
26 #include "drivers/exti.h"
27 #include "drivers/nvic.h"
28 #include "drivers/memprot.h"
29 #include "drivers/persistent.h"
30 #include "drivers/system.h"
32 bool isMPUSoftReset(void)
34 if (cachedRccCsrValue & RCC_CSR_SFTRSTF)
35 return true;
36 else
37 return false;
40 void systemInit(void)
42 memProtReset();
43 memProtConfigure(mpuRegions, mpuRegionCount);
45 // Configure NVIC preempt/priority groups
46 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITY_GROUPING);
48 // cache RCC->RSR value to use it in isMPUSoftReset() and others
49 cachedRccCsrValue = RCC->CSR;
51 /* Accounts for OP Bootloader, set the Vector Table base address as specified in .ld file */
52 //extern void *isr_vector_table_base;
53 //NVIC_SetVectorTable((uint32_t)&isr_vector_table_base, 0x0);
54 //__HAL_RCC_USB_OTG_FS_CLK_DISABLE;
56 //RCC_ClearFlag();
58 // Init cycle counter
59 cycleCounterInit();
61 // SysTick is updated whenever HAL_RCC_ClockConfig is called.
63 // Disable UCPD pull-down functionality on PB4
64 // AN5093 6.3.3:
65 // (after device startup this pull-down on PB4 can be disabled by
66 // setting UCPD1_DBDIS bit in the PWR_CR3 register).
68 // There is also a similar UCPD stand-by functionality control,
69 // but don't fiddle with this one. In particular, disabling it by
70 // calling HAL_PWREx_DisableUSBStandByModePD() will cause boot loader
71 // invocation to fail.
73 #if defined(PWR_CR3_UCPD_DBDIS)
74 HAL_PWREx_DisableUSBDeadBatteryPD();
75 #endif
78 void systemReset(void)
80 // SCB_DisableDCache();
81 // SCB_DisableICache();
83 __disable_irq();
84 NVIC_SystemReset();
87 void systemResetToBootloader(bootloaderRequestType_e requestType)
89 UNUSED(requestType);
91 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_BOOTLOADER_REQUEST_ROM);
93 __disable_irq();
94 NVIC_SystemReset();
97 #define SYSMEMBOOT_VECTOR_TABLE ((uint32_t *)0x1fff0000)
99 typedef void *(*bootJumpPtr)(void);
101 void systemJumpToBootloader(void)
103 __SYSCFG_CLK_ENABLE();
105 uint32_t bootStack = SYSMEMBOOT_VECTOR_TABLE[0];
107 bootJumpPtr SysMemBootJump = (bootJumpPtr)SYSMEMBOOT_VECTOR_TABLE[1];
109 __set_MSP(bootStack); //Set the main stack pointer to its default values
111 SysMemBootJump();
113 while (1);
117 void systemProcessResetReason(void)
119 uint32_t bootloaderRequest = persistentObjectRead(PERSISTENT_OBJECT_RESET_REASON);
121 switch (bootloaderRequest) {
122 case RESET_MSC_REQUEST:
123 // RESET_REASON will be reset by MSC
124 case RESET_NONE:
125 break;
127 case RESET_BOOTLOADER_REQUEST_ROM:
128 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_NONE);
130 systemJumpToBootloader();
132 break;