state_manager.h
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 #ifndef ROSFLIGHT_FIRMWARE_STATE_MANAGER_H
33 #define ROSFLIGHT_FIRMWARE_STATE_MANAGER_H
34 
35 #include "util.h"
36 
37 #include <cstddef>
38 #include <cstdint>
39 
40 namespace rosflight_firmware
41 {
42 class ROSflight;
43 
45 {
46 public:
47  struct State
48  {
49  bool armed;
50  bool failsafe;
51  bool error;
52  uint16_t error_codes;
53  };
54 
55  enum Event
56  {
66  };
67 
68  enum : uint16_t
69  {
70  ERROR_NONE = 0x0000,
73  ERROR_RC_LOST = 0x0004,
77  };
78 
85  struct __attribute__((packed)) BackupData
86  {
87  static constexpr uint32_t ARM_MAGIC =
88  0xbad2fa11;
89 
90  uint16_t reset_count = 0;
91  uint16_t error_code = 0;
92  uint32_t arm_flag = 0;
93 
99  struct DebugInfo
100  {
101  uint32_t r0;
102  uint32_t r1;
103  uint32_t r2;
104  uint32_t r3;
105  uint32_t r12;
106  uint32_t lr;
107  uint32_t pc;
108  uint32_t psr;
109  } debug;
110 
111  uint32_t checksum = 0;
112 
119  void finalize()
120  {
121  checksum = checksum_fletcher16(reinterpret_cast<uint8_t*>(this), sizeof(BackupData) - sizeof(checksum));
122  }
123 
130  bool valid_checksum()
131  {
132  return checksum == checksum_fletcher16(reinterpret_cast<uint8_t*>(this), sizeof(BackupData) - sizeof(checksum));
133  }
134  };
135 
136  StateManager(ROSflight& parent);
137  void init();
138  void run();
139 
140  inline const State& state() const { return state_; }
141 
142  void set_event(Event event);
143  void set_error(uint16_t error);
144  void clear_error(uint16_t error);
145 
157  void write_backup_data(const BackupData::DebugInfo& debug);
158 
159  void check_backup_memory();
160 
161 private:
164 
165  uint32_t next_led_blink_ms_ = 0;
167 
168  uint32_t hardfault_count_ = 0;
169 
170  enum FsmState
171  {
178  };
179 
181  void process_errors();
182 
183  void update_leds();
184 };
185 
186 } // namespace rosflight_firmware
187 
188 #endif // ROSFLIGHT_FIRMWARE_STATE_MANAGER_H
void write_backup_data(const BackupData::DebugInfo &debug)
Write recovery data to backup memory in the case of a hard fault.
struct __attribute__((packed)) BackupData
Stores backup data for restoring the system state after a hard fault.
Definition: state_manager.h:85
uint16_t checksum_fletcher16(const uint8_t *src, size_t len, bool finalize=true, uint16_t start=0)
Fletcher 16-bit checksum.
Definition: util.h:52
const State & state() const
void set_error(uint16_t error)
void clear_error(uint16_t error)


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:07:48