[4.4.2] Remove 15 m/s limit on estimated vario (#12788)
[betaflight.git] / src / main / drivers / system_stm32h7xx.c
blob8f4f8ce79b956d5098d1ac1d70cb2e4c01bb3012
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 #if defined(USE_FLASH_MEMORY_MAPPED)
68 * Memory mapped targets use a bootloader which enables memory mapped mode before running the firmware directly from external flash.
69 * Code running from external flash, i.e. most of the firmware, must not disable peripherals or reconfigure pins used by the CPU to access the flash chip.
70 * Refer to reference manuals and linker scripts for addresses of memory mapped regions.
71 * STM32H830 - RM0468 "Table 6. Memory map and default device memory area attributes"
73 * If the config is also stored on the same flash chip that code is running from then VERY special care must be taken when detecting the flash chip
74 * and when writing an updated config back to the flash.
77 static bool memoryMappedModeEnabledOnBoot = false;
79 bool isMemoryMappedModeEnabledOnBoot(void)
81 return memoryMappedModeEnabledOnBoot;
84 void memoryMappedModeInit(void)
86 #if defined(STM32H730xx) || defined(STM32H723xx)
87 // Smaller MCU packages have ONE OCTOSPI interface which supports memory mapped mode.
88 memoryMappedModeEnabledOnBoot = READ_BIT(OCTOSPI1->CR, OCTOSPI_CR_FMODE) == OCTOSPI_CR_FMODE;
89 #else
90 #error No Memory Mapped implementation on current MCU.
91 #endif
93 #else
94 bool isMemoryMappedModeEnabledOnBoot(void)
96 return false;
98 #endif
100 void systemInit(void)
102 // Configure NVIC preempt/priority groups
103 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITY_GROUPING);
105 // cache RCC->RSR value to use it in isMPUSoftReset() and others
106 cachedRccCsrValue = RCC->RSR;
108 /* Accounts for OP Bootloader, set the Vector Table base address as specified in .ld file */
109 //extern void *isr_vector_table_base;
110 //NVIC_SetVectorTable((uint32_t)&isr_vector_table_base, 0x0);
111 //__HAL_RCC_USB_OTG_FS_CLK_DISABLE;
113 //RCC_ClearFlag();
115 #if defined(STM32H743xx) || defined(STM32H750xx)
116 __HAL_RCC_D2SRAM1_CLK_ENABLE();
117 __HAL_RCC_D2SRAM2_CLK_ENABLE();
118 __HAL_RCC_D2SRAM3_CLK_ENABLE();
119 #elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ)
120 __HAL_RCC_AHBSRAM1_CLK_ENABLE();
121 __HAL_RCC_AHBSRAM2_CLK_ENABLE();
122 #elif defined(STM32H723xx) || defined(STM32H725xx) || defined(STM32H730xx)
123 __HAL_RCC_D2SRAM1_CLK_ENABLE();
124 __HAL_RCC_D2SRAM2_CLK_ENABLE();
125 #else
126 #error Unknown MCU
127 #endif
129 #ifdef USE_MCO_OUTPUTS
130 configureMasterClockOutputs();
131 #endif
133 // Init cycle counter
134 cycleCounterInit();
136 // SysTick is updated whenever HAL_RCC_ClockConfig is called.
139 void systemReset(void)
141 SCB_DisableDCache();
142 SCB_DisableICache();
144 __disable_irq();
145 NVIC_SystemReset();
148 void systemResetWithoutDisablingCaches(void)
150 __disable_irq();
151 NVIC_SystemReset();
154 void systemResetToBootloader(bootloaderRequestType_e requestType)
156 switch (requestType) {
157 #if defined(USE_FLASH_BOOT_LOADER)
158 case BOOTLOADER_REQUEST_FLASH:
159 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_BOOTLOADER_REQUEST_FLASH);
161 break;
162 #endif
163 case BOOTLOADER_REQUEST_ROM:
164 default:
165 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_BOOTLOADER_REQUEST_ROM);
167 break;
170 __disable_irq();
171 NVIC_SystemReset();
175 #if defined(STM32H743xx) || defined(STM32H750xx) || defined(STM32H723xx) || defined(STM32H725xx) || defined(STM32H730xx)
176 #define SYSMEMBOOT_VECTOR_TABLE ((uint32_t *)0x1ff09800)
177 #elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ)
178 #define SYSMEMBOOT_VECTOR_TABLE ((uint32_t *)0x1ff0a000)
179 #else
180 #error Unknown MCU
181 #endif
183 typedef void *(*bootJumpPtr)(void);
185 void systemJumpToBootloader(void)
187 __SYSCFG_CLK_ENABLE();
189 uint32_t bootStack = SYSMEMBOOT_VECTOR_TABLE[0];
191 bootJumpPtr SysMemBootJump = (bootJumpPtr)SYSMEMBOOT_VECTOR_TABLE[1];
193 __set_MSP(bootStack); //Set the main stack pointer to its default values
195 SysMemBootJump();
197 while (1);
201 void systemProcessResetReason(void)
203 uint32_t bootloaderRequest = persistentObjectRead(PERSISTENT_OBJECT_RESET_REASON);
205 switch (bootloaderRequest) {
206 #if defined(USE_FLASH_BOOT_LOADER)
207 case RESET_BOOTLOADER_REQUEST_FLASH:
208 #endif
209 case RESET_BOOTLOADER_REQUEST_ROM:
210 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_BOOTLOADER_POST);
211 systemJumpToBootloader();
213 break;
215 case RESET_FORCED:
216 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_NONE);
217 break;
219 case RESET_BOOTLOADER_POST:
220 // Boot loader activity magically prevents SysTick from interrupting.
221 // Issue a soft reset to prevent the condition.
222 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_FORCED);
223 systemResetWithoutDisablingCaches(); // observed that disabling dcache after cold boot with BOOT pin high causes segfault.
225 break;
227 case RESET_MSC_REQUEST:
228 case RESET_NONE:
229 default:
230 break;