airbourne/main.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson and Daniel Koch, BYU MAGICC Lab
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "airbourne_board.h"
33 #include "backup_sram.h"
34 #include "board.h"
35 #include "mavlink.h"
36 #include "state_manager.h"
37 
38 #include "rosflight.h"
39 
40 rosflight_firmware::ROSflight* rosflight_ptr = nullptr; // used to access firmware in case of a hard fault
41 void write_backup_data(const rosflight_firmware::StateManager::BackupData::DebugInfo& debug)
42 {
43  if (rosflight_ptr != nullptr)
44  {
45  rosflight_ptr->state_manager_.write_backup_data(debug);
46  }
47 }
48 
49 extern "C"
50 {
51  /* The prototype shows it is a naked function - in effect this is just an
52  assembly function. */
53  void HardFault_Handler(void) __attribute__((naked));
54 
55  /* The fault handler implementation calls a function called
56  prvGetRegistersFromStack(). */
57  void HardFault_Handler(void)
58  {
59  __asm volatile(
60  " tst lr, #4 \n"
61  " ite eq \n"
62  " mrseq r0, msp \n"
63  " mrsne r0, psp \n"
64  " ldr r1, [r0, #24] \n"
65  " ldr r2, handler2_address_const \n"
66  " bx r2 \n"
67  " handler2_address_const: .word prvGetRegistersFromStack \n");
68  }
69 
70  void prvGetRegistersFromStack(uint32_t* pulFaultStackAddress)
71  {
72  /* These are volatile to try and prevent the compiler/linker optimising them
73  away as the variables never actually get used. If the debugger won't show the
74  values of the variables, make them global my moving their declaration outside
75  of this function. */
76  volatile uint32_t r0;
77  volatile uint32_t r1;
78  volatile uint32_t r2;
79  volatile uint32_t r3;
80  volatile uint32_t r12;
81  volatile uint32_t lr; /* Link register. */
82  volatile uint32_t pc; /* Program counter. */
83  volatile uint32_t psr; /* Program status register. */
84 
85  r0 = pulFaultStackAddress[0];
86  r1 = pulFaultStackAddress[1];
87  r2 = pulFaultStackAddress[2];
88  r3 = pulFaultStackAddress[3];
89 
90  r12 = pulFaultStackAddress[4];
91  lr = pulFaultStackAddress[5];
92  pc = pulFaultStackAddress[6];
93  psr = pulFaultStackAddress[7];
94 
95  /* When the following line is hit, the variables contain the register values. */
96 
97  // save crash information to backup SRAM
98  rosflight_firmware::StateManager::BackupData::DebugInfo debug = {r0, r1, r2, r3, r12, lr, pc, psr};
99  write_backup_data(debug);
100 
101  // reboot
103  }
104 
105  void NMI_Handler()
106  {
107  while (1)
108  {
109  }
110  }
111 
113  {
114  while (1)
115  {
116  }
117  }
118 
120  {
121  while (1)
122  {
123  }
124  }
125 
127  {
128  while (1)
129  {
130  }
131  }
132 
134  {
135  while (1)
136  {
137  }
138  }
139 } // extern "C"
140 
141 int main(void)
142 {
144  rosflight_firmware::Mavlink mavlink(board);
145  rosflight_firmware::ROSflight firmware(board, mavlink);
146 
147  rosflight_ptr = &firmware; // this allows crashes to grab some info
148 
149  board.init_board();
150  firmware.init();
151 
152  while (true)
153  {
154  firmware.run();
155  }
156 
157  return 0;
158 }
void HardFault_Handler(void) __attribute__((naked))
void write_backup_data(const BackupData::DebugInfo &debug)
Write recovery data to backup memory in the case of a hard fault.
rosflight_firmware::ROSflight * rosflight_ptr
void prvGetRegistersFromStack(uint32_t *pulFaultStackAddress)
__STATIC_INLINE void NVIC_SystemReset(void)
System Reset.
Definition: core_cm0.h:647
void BusFault_Handler()
void WWDG_IRQHandler()
void run()
Main loop for the ROSflight autopilot flight stack.
Definition: rosflight.cpp:100
void UsageFault_Handler()
void write_backup_data(const rosflight_firmware::StateManager::BackupData::DebugInfo &debug)
int main(void)
struct __attribute__((packed))
Definition: usbd_cdc_vcp.h:66
void MemManage_Handler()
void init()
Main initialization routine for the ROSflight autopilot flight stack.
Definition: rosflight.cpp:55
void NMI_Handler()


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Sat May 9 2020 03:16:52