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 
43 class ROSflight;
44 
46 {
47 
48 public:
49  struct State
50  {
51  bool armed;
52  bool failsafe;
53  bool error;
54  uint16_t error_codes;
55  };
56 
57  enum Event
58  {
68  };
69 
70  enum : uint16_t
71  {
72  ERROR_NONE = 0x0000,
75  ERROR_RC_LOST = 0x0004,
79  };
80 
87  struct __attribute__((packed)) BackupData
88  {
89  static constexpr uint32_t ARM_MAGIC = 0xbad2fa11;
90 
91  uint16_t reset_count = 0;
92  uint16_t error_code = 0;
93  uint32_t arm_flag = 0;
94 
100  struct DebugInfo
101  {
102  uint32_t r0;
103  uint32_t r1;
104  uint32_t r2;
105  uint32_t r3;
106  uint32_t r12;
107  uint32_t lr;
108  uint32_t pc;
109  uint32_t psr;
110  } debug;
111 
112  uint32_t checksum = 0;
113 
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 
156  void write_backup_data(const BackupData::DebugInfo& debug);
157 
158  void check_backup_memory();
159 
160 private:
163 
164  uint32_t next_led_blink_ms_ = 0;
166 
167  uint32_t hardfault_count_ = 0;
168 
169  enum FsmState
170  {
177  };
178 
180  void process_errors();
181 
182  void update_leds();
183 };
184 
185 } // namespace rosflight_firmware
186 
187 #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:87
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:51
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 Fri Apr 3 2020 03:19:24