Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mcu_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 AggieAir, A Remote Sensing Unmanned Aerial System for Scientific Applications
3  * Utah State University, http://aggieair.usu.edu/
4  *
5  * Michal Podhradsky (michal.podhradsky@aggiemail.usu.edu)
6  * Calvin Coopmans (c.r.coopmans@ieee.org)
7  *
8  * 2016 Gautier Hattenberger <gautier.hattenberger@enac.fr>
9  * 2016 Alexandre Bustico <alexandre.bustico@enac.fr>
10  *
11  * This file is part of paparazzi.
12  *
13  * paparazzi is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * paparazzi is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with paparazzi; see the file COPYING. If not, write to
25  * the Free Software Foundation, 59 Temple Place - Suite 330,
26  * Boston, MA 02111-1307, USA.
27  */
36 /* ChibiOS includes */
37 #include "ch.h"
38 #include "hal.h"
39 /* Paparazzi includes */
40 #include "mcu.h"
41 
42 #if USE_HARD_FAULT_RECOVERY
43 
44 #ifdef STM32F4
45 #define BCKP_SECTION ".ram5"
46 #define IN_BCKP_SECTION(var) var __attribute__ ((section(BCKP_SECTION), aligned(8)))
47 #else
48 #error "No backup ram available"
49 #endif
50 IN_BCKP_SECTION(volatile bool hard_fault);
51 
52 /*
53  * Set hard fault handlers to trigger a soft reset
54  * This will set a flag that can be tested at startup
55  */
56 
57 CH_IRQ_HANDLER(HardFault_Handler)
58 {
59  hard_fault = true;
60  NVIC_SystemReset();
61 }
62 
63 CH_IRQ_HANDLER(NMI_Handler)
64 {
65  hard_fault = true;
66  NVIC_SystemReset();
67 }
68 
69 CH_IRQ_HANDLER(MemManage_Handler)
70 {
71  hard_fault = true;
72  NVIC_SystemReset();
73 }
74 
75 CH_IRQ_HANDLER(BusFault_Handler)
76 {
77  hard_fault = true;
78  NVIC_SystemReset();
79 }
80 
81 CH_IRQ_HANDLER(UsageFault_Handler)
82 {
83  hard_fault = true;
84  NVIC_SystemReset();
85 }
86 
87 bool recovering_from_hard_fault;
88 #endif
89 
90 
91 /*
92  * SCB_VTOR has to be relocated if Luftboot is used
93  */
94 void mcu_arch_init(void)
95 {
96 #if LUFTBOOT
97  PRINT_CONFIG_MSG("We are running luftboot, the interrupt vector is being relocated.")
98 #if defined STM32F4
99  PRINT_CONFIG_MSG("STM32F4")
100  SCB_VTOR = 0x00004000;
101 #else
102  PRINT_CONFIG_MSG("STM32F1")
103  SCB_VTOR = 0x00002000;
104 #endif
105 #endif
106 
107  /*
108  * System initializations.
109  * - HAL initialization, this also initializes the configured device drivers
110  * and performs the board-specific initializations.
111  * - Kernel initialization, the main() function becomes a thread and the
112  * RTOS is active.
113  */
114  halInit();
115  chSysInit();
116 
117 #if USE_HARD_FAULT_RECOVERY
118  /* Backup domain SRAM enable, and with it, the regulator */
119 #if STM32F4
120  RCC->AHB1ENR |= RCC_AHB1ENR_BKPSRAMEN;
121  PWR->CSR |= PWR_CSR_BRE;
122  while ((PWR->CSR & PWR_CSR_BRR) == 0) ; /* Waits until the regulator is stable */
123 #endif
124 
125  // test if last reset was a 'real' hard fault
126  recovering_from_hard_fault = false;
127  if (!(RCC->CSR & RCC_CSR_SFTRSTF)) {
128  // not coming from soft reset
129  hard_fault = false;
130  } else if ((RCC->CSR & RCC_CSR_SFTRSTF) && !hard_fault) {
131  // this is a soft reset, probably from a debug probe, so let's start in normal mode
132  hard_fault = false;
133  } else {
134  // else real hard fault
135  recovering_from_hard_fault = true;
136  hard_fault = false;
137  }
138  // *MANDATORY* clear of rcc bits
139  RCC->CSR = RCC_CSR_RMVF;
140  // end of reset bit probing
141 #endif
142 
143 }
144 
void mcu_arch_init(void)
Definition: mcu_arch.c:94
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
Arch independent mcu ( Micro Controller Unit ) utilities.