altos: Send SPI message at flight state changes
[fw/altos] / src / ao_log.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 __pdata uint32_t ao_log_current_pos;
21 __pdata uint32_t ao_log_end_pos;
22 __pdata uint32_t ao_log_start_pos;
23 __xdata uint8_t ao_log_running;
24 __pdata enum flight_state ao_log_state;
25 __xdata uint16_t ao_flight_number;
26
27 __code uint8_t ao_log_format = AO_LOG_FORMAT_FULL;
28
29 void
30 ao_log_flush(void)
31 {
32         ao_storage_flush();
33 }
34
35 /*
36  * When erasing a flight log, make sure the config block
37  * has an up-to-date version of the current flight number
38  */
39
40 struct ao_log_erase {
41         uint8_t unused;
42         uint16_t flight;
43 };
44
45 static __xdata struct ao_log_erase erase;
46
47 #define LOG_MAX_ERASE   16
48
49 static uint32_t
50 ao_log_erase_pos(uint8_t i)
51 {
52         return i * sizeof (struct ao_log_erase) + AO_STORAGE_ERASE_LOG;
53 }
54
55 void
56 ao_log_write_erase(uint8_t pos)
57 {
58         erase.unused = 0x00;
59         erase.flight = ao_flight_number;
60         ao_storage_write(ao_log_erase_pos(pos),  &erase, sizeof (erase));
61         ao_storage_flush();
62 }
63
64 static void
65 ao_log_read_erase(uint8_t pos)
66 {
67         ao_storage_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
68 }
69
70
71 static void
72 ao_log_erase_mark(void)
73 {
74         uint8_t                         i;
75
76         for (i = 0; i < LOG_MAX_ERASE; i++) {
77                 ao_log_read_erase(i);
78                 if (erase.unused == 0 && erase.flight == ao_flight_number)
79                         return;
80                 if (erase.unused == 0xff) {
81                         ao_log_write_erase(i);
82                         return;
83                 }
84         }
85         ao_config_put();
86 }
87
88 static uint8_t
89 ao_log_slots()
90 {
91         return (uint8_t) (ao_storage_config / ao_config.flight_log_max);
92 }
93
94 uint32_t
95 ao_log_pos(uint8_t slot)
96 {
97         return ((slot) * ao_config.flight_log_max);
98 }
99
100 static uint16_t
101 ao_log_max_flight(void)
102 {
103         uint8_t         log_slot;
104         uint8_t         log_slots;
105         uint16_t        log_flight;
106         uint16_t        max_flight = 0;
107
108         /* Scan the log space looking for the biggest flight number */
109         log_slots = ao_log_slots();
110         for (log_slot = 0; log_slot < log_slots; log_slot++) {
111                 log_flight = ao_log_flight(log_slot);
112                 if (!log_flight)
113                         continue;
114                 if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0)
115                         max_flight = log_flight;
116         }
117         return max_flight;
118 }
119
120 void
121 ao_log_scan(void) __reentrant
122 {
123         uint8_t         log_slot;
124         uint8_t         log_slots;
125         uint8_t         log_want;
126
127         ao_config_get();
128
129         ao_flight_number = ao_log_max_flight();
130         if (ao_flight_number)
131                 if (++ao_flight_number == 0)
132                         ao_flight_number = 1;
133
134         /* Now look through the log of flight numbers from erase operations and
135          * see if the last one is bigger than what we found above
136          */
137         for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
138                 ao_log_read_erase(log_slot);
139                 if (erase.unused == 0) {
140                         if (ao_flight_number == 0 ||
141                             (int16_t) (erase.flight - ao_flight_number) > 0)
142                                 ao_flight_number = erase.flight;
143                         break;
144                 }
145         }
146         if (ao_flight_number == 0)
147                 ao_flight_number = 1;
148
149         /* With a flight number in hand, find a place to write a new log,
150          * use the target flight number to index the available log slots so
151          * that we write logs to each spot about the same number of times.
152          */
153
154         /* Find a log slot for the next flight, if available */
155         ao_log_current_pos = ao_log_end_pos = 0;
156         log_slots = ao_log_slots();
157         log_want = (ao_flight_number - 1) % log_slots;
158         log_slot = log_want;
159         do {
160                 if (ao_log_flight(log_slot) == 0) {
161                         ao_log_current_pos = ao_log_pos(log_slot);
162                         ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
163                         break;
164                 }
165                 if (++log_slot >= log_slots)
166                         log_slot = 0;
167         } while (log_slot != log_want);
168
169         ao_wakeup(&ao_flight_number);
170 }
171
172 void
173 ao_log_start(void)
174 {
175         /* start logging */
176         ao_log_running = 1;
177         ao_wakeup(&ao_log_running);
178 }
179
180 void
181 ao_log_stop(void)
182 {
183         ao_log_running = 0;
184         ao_log_flush();
185 }
186
187 uint8_t
188 ao_log_present(void)
189 {
190         return ao_log_max_flight() != 0;
191 }
192
193 uint8_t
194 ao_log_full(void)
195 {
196         return ao_log_current_pos == ao_log_end_pos;
197 }
198
199 static __xdata struct ao_task ao_log_task;
200
201 void
202 ao_log_list(void) __reentrant
203 {
204         uint8_t slot;
205         uint8_t slots;
206         uint16_t flight;
207
208         slots = ao_log_slots();
209         for (slot = 0; slot < slots; slot++)
210         {
211                 flight = ao_log_flight(slot);
212                 if (flight)
213                         printf ("flight %d start %x end %x\n",
214                                 flight,
215                                 (uint16_t) (ao_log_pos(slot) >> 8),
216                                 (uint16_t) (ao_log_pos(slot+1) >> 8));
217         }
218         printf ("done\n");
219 }
220
221 void
222 ao_log_delete(void) __reentrant
223 {
224         uint8_t slot;
225         uint8_t slots;
226
227         ao_cmd_decimal();
228         if (ao_cmd_status != ao_cmd_success)
229                 return;
230
231         slots = ao_log_slots();
232         /* Look for the flight log matching the requested flight */
233         if (ao_cmd_lex_i) {
234                 for (slot = 0; slot < slots; slot++) {
235                         if (ao_log_flight(slot) == ao_cmd_lex_i) {
236                                 ao_log_erase_mark();
237                                 ao_log_current_pos = ao_log_pos(slot);
238                                 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
239                                 while (ao_log_current_pos < ao_log_end_pos) {
240                                         uint8_t i;
241                                         static __xdata uint8_t b;
242
243                                         /*
244                                          * Check to see if we've reached the end of
245                                          * the used memory to avoid re-erasing the same
246                                          * memory over and over again
247                                          */
248                                         for (i = 0; i < 16; i++) {
249                                                 if (ao_storage_read(ao_log_current_pos + i, &b, 1))
250                                                         if (b != 0xff)
251                                                                 break;
252                                         }
253                                         if (i == 16)
254                                                 break;
255                                         ao_storage_erase(ao_log_current_pos);
256                                         ao_log_current_pos += ao_storage_block;
257                                 }
258                                 puts("Erased");
259                                 return;
260                         }
261                 }
262         }
263         printf("No such flight: %d\n", ao_cmd_lex_i);
264 }
265
266 __code struct ao_cmds ao_log_cmds[] = {
267         { ao_log_list,  "l\0List flight logs" },
268         { ao_log_delete,        "d <flight-number>\0Delete flight" },
269         { 0,    NULL },
270 };
271
272 void
273 ao_log_init(void)
274 {
275         ao_log_running = 0;
276
277         /* For now, just log the flight starting at the begining of eeprom */
278         ao_log_state = ao_flight_invalid;
279
280         ao_cmd_register(&ao_log_cmds[0]);
281
282         /* Create a task to log events to eeprom */
283         ao_add_task(&ao_log_task, ao_log, "log");
284 }