Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / system_stm32f7xx.c
blobfd89b0c3ead91f847b47c9d02567f3d815cf39a9
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 <string.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <stdlib.h>
26 #include "platform.h"
28 #include "drivers/accgyro/accgyro_mpu.h"
29 #include "drivers/exti.h"
30 #include "drivers/nvic.h"
31 #include "drivers/system.h"
32 #include "drivers/persistent.h"
34 #include "stm32f7xx_ll_cortex.h"
37 #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
39 #define DEFAULT_STACK_POINTER ((uint32_t *) 0x1FF00000)
40 #define SYSTEM_MEMORY_RESET_VECTOR ((uint32_t *) 0x1FF00004)
42 void SystemClock_Config(void);
44 void systemReset(void)
46 __disable_irq();
47 NVIC_SystemReset();
50 void systemResetToBootloader(bootloaderRequestType_e requestType)
52 switch (requestType) {
53 case BOOTLOADER_REQUEST_ROM:
54 default:
55 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_BOOTLOADER_REQUEST_ROM);
57 break;
60 __disable_irq();
61 NVIC_SystemReset();
64 bool isMPUSoftReset(void)
66 if (cachedRccCsrValue & RCC_CSR_SFTRSTF)
67 return true;
68 else
69 return false;
72 static void checkForBootLoaderRequest(void)
74 uint32_t bootloaderRequest = persistentObjectRead(PERSISTENT_OBJECT_RESET_REASON);
76 if (bootloaderRequest != RESET_BOOTLOADER_REQUEST_ROM) {
77 return;
79 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_BOOTLOADER_POST);
81 __SYSCFG_CLK_ENABLE();
82 SYSCFG->MEMRMP |= SYSCFG_MEM_BOOT_ADD0 ;
84 __set_MSP(*DEFAULT_STACK_POINTER);
86 ((void (*)(void))*SYSTEM_MEMORY_RESET_VECTOR)();
88 while (1);
91 void systemInit(void)
93 checkForBootLoaderRequest();
95 // Mark ITCM-RAM as read-only
96 LL_MPU_ConfigRegion(LL_MPU_REGION_NUMBER0, 0, RAMITCM_BASE, LL_MPU_REGION_SIZE_16KB | LL_MPU_REGION_PRIV_RO_URO);
97 LL_MPU_Enable(LL_MPU_CTRL_PRIVILEGED_DEFAULT);
99 //SystemClock_Config();
101 // Configure NVIC preempt/priority groups
102 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITY_GROUPING);
104 // cache RCC->CSR value to use it in isMPUSoftReset() and others
105 cachedRccCsrValue = RCC->CSR;
107 /* Accounts for OP Bootloader, set the Vector Table base address as specified in .ld file */
108 //extern void *isr_vector_table_base;
109 //NVIC_SetVectorTable((uint32_t)&isr_vector_table_base, 0x0);
110 //__HAL_RCC_USB_OTG_FS_CLK_DISABLE;
112 //RCC_ClearFlag();
114 // Init cycle counter
115 cycleCounterInit();
117 // SysTick
118 //SysTick_Config(SystemCoreClock / 1000);
119 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
121 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);