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 "rosflight.h"
34 #include "mavlink.h"
35 #include "backup_sram.h"
36 #include "board.h"
37 #include "state_manager.h"
38 
39 //#pragma GCC diagnostic ignored "-Wmissing-field-initializers" //Because this was unnecessary and annoying
40 
41 uint32_t error_count_ = 0;//Used for counting resets
42 rosflight_firmware::ROSflight *rosflight=nullptr;//Used to access important variables in case of a hard fault
43 rosflight_firmware::StateManager::State get_state()//Used in case of a hard fault
44 {
45  if (rosflight==nullptr)
46  {
47 #pragma GCC diagnostic push //Ignore blank fields in struct
48 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
50 #pragma GCC diagnostic pop
51  return ret;
52  }
53  return rosflight->state_manager_.state();
54 }
55 
56 extern "C" {
57  /* The prototype shows it is a naked function - in effect this is just an
58  assembly function. */
59  void HardFault_Handler(void) __attribute__((naked));
60 
61  /* The fault handler implementation calls a function called
62  prvGetRegistersFromStack(). */
63  void HardFault_Handler(void)
64  {
65  __asm volatile
66  (
67  " tst lr, #4 \n"
68  " ite eq \n"
69  " mrseq r0, msp \n"
70  " mrsne r0, psp \n"
71  " ldr r1, [r0, #24] \n"
72  " ldr r2, handler2_address_const \n"
73  " bx r2 \n"
74  " handler2_address_const: .word prvGetRegistersFromStack \n"
75  );
76  }
77 
78  void prvGetRegistersFromStack(uint32_t *pulFaultStackAddress)
79  {
80  /* These are volatile to try and prevent the compiler/linker optimising them
81  away as the variables never actually get used. If the debugger won't show the
82  values of the variables, make them global my moving their declaration outside
83  of this function. */
84  volatile uint32_t r0;
85  volatile uint32_t r1;
86  volatile uint32_t r2;
87  volatile uint32_t r3;
88  volatile uint32_t r12;
89  volatile uint32_t lr; /* Link register. */
90  volatile uint32_t pc; /* Program counter. */
91  volatile uint32_t psr;/* Program status register. */
92 
93  r0 = pulFaultStackAddress[ 0 ];
94  r1 = pulFaultStackAddress[ 1 ];
95  r2 = pulFaultStackAddress[ 2 ];
96  r3 = pulFaultStackAddress[ 3 ];
97 
98  r12 = pulFaultStackAddress[ 4 ];
99  lr = pulFaultStackAddress[ 5 ];
100  pc = pulFaultStackAddress[ 6 ];
101  psr = pulFaultStackAddress[ 7 ];
102 
103  // avoid compiler warnings about unused variables
104  (void) r0;
105  (void) r1;
106  (void) r2;
107  (void) r3;
108  (void) r12;
109  (void) lr;
110  (void) pc;
111  (void) psr;
112 
113  /* When the following line is hit, the variables contain the register values. */
114 
115  //save crash information to backup SRAM
116  rosflight_firmware::BackupData backup_data;
117  backup_data.debug_info= {r0,r1,r2,r3,r12,lr,pc,psr};
118  backup_data.reset_count=error_count_+1;
119  backup_data.error_code=1;
120  backup_data.state = get_state();
121  if (backup_data.state.armed)
122  backup_data.arm_status=rosflight_firmware::ARM_MAGIC;//magic number for extra certainty on rearm
123  backup_data.checksum=generate_backup_checksum(backup_data);
124  backup_sram_write(backup_data);
125 
127  }
128 
129  void NMI_Handler()
130  {
131  while (1) {}
132  }
133 
134 
136  {
137  while (1) {}
138  }
139 
141  {
142  while (1) {}
143  }
144 
146  {
147  while (1) {}
148  }
149 
151  {
152  while (1) {}
153  }
154 }
155 
156 
157 
158 int main(void)
159 {
161  rosflight_firmware::Mavlink mavlink(board);
162  rosflight_firmware::ROSflight firmware(board, mavlink);
163  rosflight = &firmware; //this allows crashes to grab some info
164  board.init_board();
165  firmware.init();
166  //Because the USB driver breaks the backup sram, the backup sram must be initalized after
169  error_count_ = backup_data.reset_count;
170 
171  while (true)
172  {
173  firmware.run();
174  }
175  return 0;
176 }
void HardFault_Handler(void) __attribute__((naked))
void backup_sram_write(const rosflight_firmware::BackupData &)
Definition: backup_sram.cpp:21
uint32_t error_count_
debug_info_t debug_info
Definition: board.h:59
void backup_sram_init()
Definition: backup_sram.cpp:6
void prvGetRegistersFromStack(uint32_t *pulFaultStackAddress)
__STATIC_INLINE void NVIC_SystemReset(void)
System Reset.
Definition: core_cm0.h:647
void BusFault_Handler()
rosflight_firmware::StateManager::State get_state()
void WWDG_IRQHandler()
void run()
Main loop for the ROSflight autopilot flight stack.
Definition: rosflight.cpp:92
rosflight_firmware::BackupData backup_sram_read()
Definition: backup_sram.cpp:50
const State & state() const
Definition: state_manager.h:82
rosflight_firmware::ROSflight * rosflight
StateManager::State state
Definition: board.h:63
uint32_t generate_backup_checksum(const rosflight_firmware::BackupData &)
Definition: backup_sram.cpp:63
void UsageFault_Handler()
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:52
const uint32_t ARM_MAGIC
Definition: board.h:67
void NMI_Handler()


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:24