altos: Clear out eeprom erase records when writing entry 0
[fw/altos] / src / kernel / 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 #include <ao_log.h>
20 #include <ao_config.h>
21
22 __pdata uint32_t ao_log_current_pos;
23 __pdata uint32_t ao_log_end_pos;
24 __pdata uint32_t ao_log_start_pos;
25 __xdata uint8_t ao_log_running;
26 __pdata enum ao_flight_state ao_log_state;
27 __xdata uint16_t ao_flight_number;
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 mark;
42         uint16_t flight;
43 };
44
45 static __xdata struct ao_log_erase erase;
46
47 #ifndef LOG_MAX_ERASE
48 #define LOG_MAX_ERASE   16
49 #endif
50
51 #ifndef LOG_ERASE_MARK
52 #if USE_EEPROM_CONFIG
53 #error "Must define LOG_ERASE_MARK with USE_EEPROM_CONFIG"
54 #endif
55 #define LOG_ERASE_MARK  0x00
56 #endif
57
58 static uint32_t
59 ao_log_erase_pos(uint8_t i)
60 {
61         return i * sizeof (struct ao_log_erase) + AO_CONFIG_MAX_SIZE;
62 }
63
64 void
65 ao_log_write_erase(uint8_t pos)
66 {
67         erase.mark = LOG_ERASE_MARK;
68         erase.flight = ao_flight_number;
69         ao_config_write(ao_log_erase_pos(pos),  &erase, sizeof (erase));
70
71 #if USE_EEPROM_CONFIG
72         if (pos == 0) {
73                 uint8_t i;
74                 for (i = 1; i < LOG_MAX_ERASE; i++) {
75                         erase.mark = ~LOG_ERASE_MARK;
76                         erase.flight = 0;
77                         ao_config_write(ao_log_erase_pos(i), &erase, sizeof (erase));
78                 }
79         }
80 #endif
81
82         ao_config_flush();
83 }
84
85 static void
86 ao_log_read_erase(uint8_t pos)
87 {
88         ao_config_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
89 }
90
91
92 static void
93 ao_log_erase_mark(void)
94 {
95         uint8_t                         i;
96
97         for (i = 0; i < LOG_MAX_ERASE; i++) {
98                 ao_log_read_erase(i);
99                 if (erase.mark == LOG_ERASE_MARK && erase.flight == ao_flight_number)
100                         return;
101                 if (erase.mark != LOG_ERASE_MARK) {
102                         ao_log_write_erase(i);
103                         return;
104                 }
105         }
106         ao_config_put();
107 }
108
109 static uint8_t
110 ao_log_slots()
111 {
112         return (uint8_t) (ao_storage_log_max / ao_config.flight_log_max);
113 }
114
115 uint32_t
116 ao_log_pos(uint8_t slot)
117 {
118         return ((slot) * ao_config.flight_log_max);
119 }
120
121 static uint16_t
122 ao_log_max_flight(void)
123 {
124         uint8_t         log_slot;
125         uint8_t         log_slots;
126         uint16_t        log_flight;
127         uint16_t        max_flight = 0;
128
129         /* Scan the log space looking for the biggest flight number */
130         log_slots = ao_log_slots();
131         for (log_slot = 0; log_slot < log_slots; log_slot++) {
132                 log_flight = ao_log_flight(log_slot);
133                 if (!log_flight)
134                         continue;
135                 if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0)
136                         max_flight = log_flight;
137         }
138         return max_flight;
139 }
140
141 void
142 ao_log_scan(void) __reentrant
143 {
144         uint8_t         log_slot;
145         uint8_t         log_slots;
146         uint8_t         log_want;
147
148         ao_config_get();
149
150         ao_flight_number = ao_log_max_flight();
151         if (ao_flight_number)
152                 if (++ao_flight_number == 0)
153                         ao_flight_number = 1;
154
155         /* Now look through the log of flight numbers from erase operations and
156          * see if the last one is bigger than what we found above
157          */
158         for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
159                 ao_log_read_erase(log_slot);
160                 if (erase.mark == LOG_ERASE_MARK) {
161                         if (ao_flight_number == 0 ||
162                             (int16_t) (erase.flight - ao_flight_number) > 0)
163                                 ao_flight_number = erase.flight;
164                         break;
165                 }
166         }
167         if (ao_flight_number == 0)
168                 ao_flight_number = 1;
169
170         /* With a flight number in hand, find a place to write a new log,
171          * use the target flight number to index the available log slots so
172          * that we write logs to each spot about the same number of times.
173          */
174
175         /* Find a log slot for the next flight, if available */
176         ao_log_current_pos = ao_log_end_pos = 0;
177         log_slots = ao_log_slots();
178         log_want = (ao_flight_number - 1) % log_slots;
179         log_slot = log_want;
180         do {
181                 if (ao_log_flight(log_slot) == 0) {
182                         ao_log_current_pos = ao_log_pos(log_slot);
183                         ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
184                         break;
185                 }
186                 if (++log_slot >= log_slots)
187                         log_slot = 0;
188         } while (log_slot != log_want);
189
190         ao_wakeup(&ao_flight_number);
191 }
192
193 void
194 ao_log_start(void)
195 {
196         /* start logging */
197         ao_log_running = 1;
198         ao_wakeup(&ao_log_running);
199 }
200
201 void
202 ao_log_stop(void)
203 {
204         ao_log_running = 0;
205         ao_log_flush();
206 }
207
208 uint8_t
209 ao_log_present(void)
210 {
211         return ao_log_max_flight() != 0;
212 }
213
214 uint8_t
215 ao_log_full(void)
216 {
217         return ao_log_current_pos == ao_log_end_pos;
218 }
219
220 #ifndef LOG_ADC
221 #define LOG_ADC HAS_ADC
222 #endif
223
224 #if LOG_ADC
225 static __xdata struct ao_task ao_log_task;
226 #endif
227
228 void
229 ao_log_list(void) __reentrant
230 {
231         uint8_t slot;
232         uint8_t slots;
233         uint16_t flight;
234
235         slots = ao_log_slots();
236         for (slot = 0; slot < slots; slot++)
237         {
238                 flight = ao_log_flight(slot);
239                 if (flight)
240                         printf ("flight %d start %x end %x\n",
241                                 flight,
242                                 (uint16_t) (ao_log_pos(slot) >> 8),
243                                 (uint16_t) (ao_log_pos(slot+1) >> 8));
244         }
245         printf ("done\n");
246 }
247
248 void
249 ao_log_delete(void) __reentrant
250 {
251         uint8_t slot;
252         uint8_t slots;
253
254         ao_cmd_decimal();
255         if (ao_cmd_status != ao_cmd_success)
256                 return;
257
258         slots = ao_log_slots();
259         /* Look for the flight log matching the requested flight */
260         if (ao_cmd_lex_i) {
261                 for (slot = 0; slot < slots; slot++) {
262                         if (ao_log_flight(slot) == ao_cmd_lex_i) {
263                                 ao_log_erase_mark();
264                                 ao_log_current_pos = ao_log_pos(slot);
265                                 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
266                                 while (ao_log_current_pos < ao_log_end_pos) {
267                                         uint8_t i;
268                                         static __xdata uint8_t b;
269
270                                         /*
271                                          * Check to see if we've reached the end of
272                                          * the used memory to avoid re-erasing the same
273                                          * memory over and over again
274                                          */
275                                         for (i = 0; i < 16; i++) {
276                                                 if (ao_storage_read(ao_log_current_pos + i, &b, 1))
277                                                         if (b != 0xff)
278                                                                 break;
279                                         }
280                                         if (i == 16)
281                                                 break;
282                                         ao_storage_erase(ao_log_current_pos);
283                                         ao_log_current_pos += ao_storage_block;
284                                 }
285                                 puts("Erased");
286                                 return;
287                         }
288                 }
289         }
290         printf("No such flight: %d\n", ao_cmd_lex_i);
291 }
292
293 __code struct ao_cmds ao_log_cmds[] = {
294         { ao_log_list,  "l\0List logs" },
295         { ao_log_delete,        "d <flight-number>\0Delete flight" },
296         { 0,    NULL },
297 };
298
299 void
300 ao_log_init(void)
301 {
302         ao_log_running = 0;
303
304         /* For now, just log the flight starting at the begining of eeprom */
305         ao_log_state = ao_flight_invalid;
306
307         ao_cmd_register(&ao_log_cmds[0]);
308
309 #ifndef HAS_ADC
310 #error Define HAS_ADC for ao_log.c
311 #endif
312 #if LOG_ADC
313         /* Create a task to log events to eeprom */
314         ao_add_task(&ao_log_task, ao_log, "log");
315 #endif
316 }