Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / system_stm32h7xx.c
blob49f96f831be4e95177d3714690b98bf36b07b09e
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/persistent.h"
29 #include "drivers/system.h"
32 void SystemClock_Config(void);
34 void configureMasterClockOutputs(void)
36 // Initialize pins for MCO1 and MCO2 for clock testing/verification
38 GPIO_InitTypeDef GPIO_InitStruct;
40 GPIO_InitStruct.Pin = GPIO_PIN_8;
41 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
42 GPIO_InitStruct.Pull = GPIO_NOPULL;
43 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
44 GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
46 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
48 GPIO_InitStruct.Pin = GPIO_PIN_9;
49 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
50 GPIO_InitStruct.Pull = GPIO_NOPULL;
51 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
52 GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
54 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
57 bool isMPUSoftReset(void)
59 if (cachedRccCsrValue & RCC_RSR_SFTRSTF)
60 return true;
61 else
62 return false;
65 void systemInit(void)
67 // Configure NVIC preempt/priority groups
68 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITY_GROUPING);
70 // cache RCC->RSR value to use it in isMPUSoftReset() and others
71 cachedRccCsrValue = RCC->RSR;
73 /* Accounts for OP Bootloader, set the Vector Table base address as specified in .ld file */
74 //extern void *isr_vector_table_base;
75 //NVIC_SetVectorTable((uint32_t)&isr_vector_table_base, 0x0);
76 //__HAL_RCC_USB_OTG_FS_CLK_DISABLE;
78 //RCC_ClearFlag();
80 #if defined(STM32H743xx) || defined(STM32H750xx)
81 __HAL_RCC_D2SRAM1_CLK_ENABLE();
82 __HAL_RCC_D2SRAM2_CLK_ENABLE();
83 __HAL_RCC_D2SRAM3_CLK_ENABLE();
84 #elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ)
85 __HAL_RCC_AHBSRAM1_CLK_ENABLE();
86 __HAL_RCC_AHBSRAM2_CLK_ENABLE();
87 #elif defined(STM32H723xx) || defined(STM32H725xx) || defined(STM32H730xx)
88 __HAL_RCC_D2SRAM1_CLK_ENABLE();
89 __HAL_RCC_D2SRAM2_CLK_ENABLE();
90 #else
91 #error Unknown MCU
92 #endif
94 #ifdef USE_MCO_OUTPUTS
95 configureMasterClockOutputs();
96 #endif
98 // Init cycle counter
99 cycleCounterInit();
101 // SysTick is updated whenever HAL_RCC_ClockConfig is called.
104 void systemReset(void)
106 SCB_DisableDCache();
107 SCB_DisableICache();
109 __disable_irq();
110 NVIC_SystemReset();
113 void systemResetWithoutDisablingCaches(void)
115 __disable_irq();
116 NVIC_SystemReset();
119 void systemResetToBootloader(bootloaderRequestType_e requestType)
121 switch (requestType) {
122 #if defined(USE_FLASH_BOOT_LOADER)
123 case BOOTLOADER_REQUEST_FLASH:
124 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_BOOTLOADER_REQUEST_FLASH);
126 break;
127 #endif
128 case BOOTLOADER_REQUEST_ROM:
129 default:
130 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_BOOTLOADER_REQUEST_ROM);
132 break;
135 __disable_irq();
136 NVIC_SystemReset();
140 #if defined(STM32H743xx) || defined(STM32H750xx) || defined(STM32H723xx) || defined(STM32H725xx) || defined(STM32H730xx)
141 #define SYSMEMBOOT_VECTOR_TABLE ((uint32_t *)0x1ff09800)
142 #elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ)
143 #define SYSMEMBOOT_VECTOR_TABLE ((uint32_t *)0x1ff0a000)
144 #else
145 #error Unknown MCU
146 #endif
148 typedef void *(*bootJumpPtr)(void);
150 void systemJumpToBootloader(void)
152 __SYSCFG_CLK_ENABLE();
154 uint32_t bootStack = SYSMEMBOOT_VECTOR_TABLE[0];
156 bootJumpPtr SysMemBootJump = (bootJumpPtr)SYSMEMBOOT_VECTOR_TABLE[1];
158 __set_MSP(bootStack); //Set the main stack pointer to its default values
160 SysMemBootJump();
162 while (1);
166 void systemProcessResetReason(void)
168 uint32_t bootloaderRequest = persistentObjectRead(PERSISTENT_OBJECT_RESET_REASON);
170 switch (bootloaderRequest) {
171 #if defined(USE_FLASH_BOOT_LOADER)
172 case RESET_BOOTLOADER_REQUEST_FLASH:
173 #endif
174 case RESET_BOOTLOADER_REQUEST_ROM:
175 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_BOOTLOADER_POST);
176 systemJumpToBootloader();
178 break;
180 case RESET_FORCED:
181 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_NONE);
182 break;
184 case RESET_BOOTLOADER_POST:
185 // Boot loader activity magically prevents SysTick from interrupting.
186 // Issue a soft reset to prevent the condition.
187 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_FORCED);
188 systemResetWithoutDisablingCaches(); // observed that disabling dcache after cold boot with BOOT pin high causes segfault.
190 break;
192 case RESET_MSC_REQUEST:
193 case RESET_NONE:
194 default:
195 break;