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