altos: Add bit-bang i2c driver
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20 #include <ao_log.h>
21 #include <ao_config.h>
22 #if HAS_TRACKER
23 #include <ao_tracker.h>
24 #endif
25
26 uint8_t ao_log_mutex;
27 uint32_t ao_log_current_pos;
28 uint32_t ao_log_end_pos;
29 uint32_t ao_log_start_pos;
30 uint8_t ao_log_running;
31 enum ao_flight_state ao_log_state;
32 int16_t ao_flight_number;
33
34 void
35 ao_log_flush(void)
36 {
37         ao_storage_flush();
38 }
39
40 /*
41  * When erasing a flight log, make sure the config block
42  * has an up-to-date version of the current flight number
43  */
44
45 struct ao_log_erase {
46         uint8_t mark;
47         uint16_t flight;
48 };
49
50 static struct ao_log_erase erase;
51
52 #ifndef LOG_MAX_ERASE
53 #define LOG_MAX_ERASE   16
54 #endif
55
56 #ifndef LOG_ERASE_MARK
57 #if USE_EEPROM_CONFIG
58 #error "Must define LOG_ERASE_MARK with USE_EEPROM_CONFIG"
59 #endif
60 #define LOG_ERASE_MARK  0x00
61 #endif
62
63 static uint32_t
64 ao_log_erase_pos(uint8_t i)
65 {
66         return i * sizeof (struct ao_log_erase) + AO_CONFIG_MAX_SIZE;
67 }
68
69 void
70 ao_log_write_erase(uint8_t pos)
71 {
72         erase.mark = LOG_ERASE_MARK;
73         erase.flight = ao_flight_number;
74         ao_config_write(ao_log_erase_pos(pos),  &erase, sizeof (erase));
75
76 #if USE_EEPROM_CONFIG
77         if (pos == 0) {
78                 uint8_t i;
79                 for (i = 1; i < LOG_MAX_ERASE; i++) {
80                         erase.mark = ~LOG_ERASE_MARK;
81                         erase.flight = 0;
82                         ao_config_write(ao_log_erase_pos(i), &erase, sizeof (erase));
83                 }
84         }
85 #endif
86
87         ao_config_flush();
88 }
89
90 static void
91 ao_log_read_erase(uint8_t pos)
92 {
93         ao_config_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
94 }
95
96
97 static void
98 ao_log_erase_mark(void)
99 {
100         uint8_t                         i;
101
102         for (i = 0; i < LOG_MAX_ERASE; i++) {
103                 ao_log_read_erase(i);
104                 if (erase.mark == LOG_ERASE_MARK && erase.flight == ao_flight_number)
105                         return;
106                 if (erase.mark != LOG_ERASE_MARK) {
107                         ao_log_write_erase(i);
108                         return;
109                 }
110         }
111         ao_config_put();
112 }
113
114 /* Position of first flight record in slot */
115 static uint32_t
116 ao_log_pos(uint8_t slot)
117 {
118         return ((slot) * ao_config.flight_log_max);
119 }
120
121 /* Start of erase block containing first flight record */
122 static uint32_t
123 ao_log_pos_block_start(uint8_t slot)
124 {
125         return ao_log_pos(slot) & ~(ao_storage_block - 1);
126 }
127
128 /* End of erase block containing last flight record */
129 static uint32_t
130 ao_log_pos_block_end(uint8_t slot)
131 {
132         return ao_log_pos_block_start(slot + 1);
133 }
134
135 #ifndef AO_LOG_UNCOMMON
136 /*
137  * Common logging functions which depend on the type of the log data
138  * structure.
139  */
140
141 ao_log_type ao_log_data;
142
143 static uint8_t
144 ao_log_csum(uint8_t *b) 
145 {
146         uint8_t sum = 0x5a;
147         uint8_t i;
148
149         for (i = 0; i < sizeof (ao_log_type); i++)
150                 sum += *b++;
151         return -sum;
152 }
153
154 uint8_t
155 ao_log_write(ao_log_type *log) 
156 {
157         uint8_t wrote = 0;
158         /* set checksum */
159         log->csum = 0;
160         log->csum = ao_log_csum((uint8_t *) log);
161         ao_mutex_get(&ao_log_mutex); {
162                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
163                         ao_log_stop();
164                 if (ao_log_running) {
165                         wrote = 1;
166                         ao_storage_write(ao_log_current_pos,
167                                          log,
168                                          sizeof (ao_log_type));
169                         ao_log_current_pos += sizeof (ao_log_type);
170                 }
171         } ao_mutex_put(&ao_log_mutex);
172         return wrote;
173 }
174
175 uint8_t
176 ao_log_check_data(void)
177 {
178         if (ao_log_csum((uint8_t *) &ao_log_data) != 0)
179                 return 0;
180         return 1;
181 }
182
183 int16_t
184 ao_log_flight(uint8_t slot)
185 {
186         if (ao_storage_is_erased(ao_log_pos_block_start(slot)))
187                 return 0;
188
189         if (!ao_storage_read(ao_log_pos(slot),
190                              &ao_log_data,
191                              sizeof (ao_log_type)))
192                 return -(int16_t) (slot + 1);
193
194         if (!ao_log_check_data() || ao_log_data.type != AO_LOG_FLIGHT)
195                 return -(int16_t) (slot + 1);
196
197         return ao_log_data.u.flight.flight;
198 }
199 #endif
200
201 static uint8_t
202 ao_log_slots(void)
203 {
204         return (uint8_t) (ao_storage_log_max / ao_config.flight_log_max);
205 }
206
207 static int16_t
208 ao_log_max_flight(void)
209 {
210         uint8_t         log_slot;
211         uint8_t         log_slots;
212         int16_t         log_flight;
213         int16_t         max_flight = 0;
214
215         /* Scan the log space looking for the biggest flight number */
216         log_slots = ao_log_slots();
217         for (log_slot = 0; log_slot < log_slots; log_slot++) {
218                 log_flight = ao_log_flight(log_slot);
219                 if (log_flight <= 0)
220                         continue;
221                 if (max_flight == 0 || log_flight > max_flight)
222                         max_flight = log_flight;
223         }
224         return max_flight;
225 }
226
227 static uint8_t
228 ao_log_erase(uint8_t slot) 
229 {
230         uint32_t start_pos;
231         uint32_t end_pos;
232
233         ao_log_erase_mark();
234         start_pos = ao_log_pos_block_start(slot);
235         end_pos = ao_log_pos_block_end(slot);
236         return ao_storage_erase(start_pos, end_pos - start_pos);
237 }
238
239 static void
240 ao_log_find_max_erase_flight(void) 
241 {
242         uint8_t log_slot;
243
244         /* Now look through the log of flight numbers from erase operations and
245          * see if the last one is bigger than what we found above
246          */
247         for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
248                 ao_log_read_erase(log_slot);
249                 if (erase.mark == LOG_ERASE_MARK) {
250                         if (ao_flight_number == 0 ||
251                             (int16_t) (erase.flight - ao_flight_number) > 0)
252                                 ao_flight_number = erase.flight;
253                         break;
254                 }
255         }
256         if (ao_flight_number == 0)
257                 ao_flight_number = 1;
258 }
259
260 uint8_t
261 ao_log_scan(void) 
262 {
263         uint8_t         log_slot;
264         uint8_t         log_slots;
265 #if FLIGHT_LOG_APPEND
266         uint8_t         ret;
267 #else
268         uint8_t         log_want;
269 #endif
270
271         ao_config_get();
272
273         /* Get any existing flight number */
274         ao_flight_number = ao_log_max_flight();
275
276 #if FLIGHT_LOG_APPEND
277
278         /* Deal with older OS versions which stored multiple
279          * flights in rom by erasing everything after the first
280          * slot
281          */
282         if (ao_config.flight_log_max != ao_storage_log_max) {
283                 log_slots = ao_log_slots();
284                 for (log_slot = 1; log_slot < log_slots; log_slot++) {
285                         if (ao_log_flight(log_slot) != 0)
286                                 if (!ao_log_erase(log_slot))
287                                         printf("erase %d failed\n", log_slot);
288                 }
289                 ao_config_log_fix_append();
290         }
291         ao_log_current_pos = ao_log_pos(0);
292         ao_log_end_pos = ao_log_pos_block_end(0);
293
294         if (ao_flight_number) {
295                 uint32_t        full = (ao_log_current_pos) / AO_LOG_SIZE;
296                 uint32_t        empty = (ao_log_end_pos - AO_LOG_SIZE) / AO_LOG_SIZE;
297
298                 /* If there's already a flight started, then find the
299                  * end of it
300                  */
301                 for (;;) {
302                         uint32_t current = (full + empty) >> 1;
303                         ao_log_current_pos = current * AO_LOG_SIZE;
304
305                         if (current == full) {
306                                 if (ao_log_check(ao_log_current_pos) != AO_LOG_EMPTY)
307                                         ao_log_current_pos += AO_LOG_SIZE;
308                                 break;
309                         }
310                         if (current == empty)
311                                 break;
312
313                         if (ao_log_check(ao_log_current_pos) != AO_LOG_EMPTY) {
314                                 full = current;
315                         } else {
316                                 empty = current;
317                         }
318                 }
319                 ret = 1;
320         } else {
321                 ao_log_find_max_erase_flight();
322                 ret = 0;
323         }
324         ao_wakeup(&ao_flight_number);
325         return ret;
326 #else
327         if (ao_flight_number) {
328                 ++ao_flight_number;
329                 if (ao_flight_number <= 0)
330                         ao_flight_number = 1;
331         }
332
333         ao_log_find_max_erase_flight();
334
335         /* With a flight number in hand, find a place to write a new log,
336          * use the target flight number to index the available log slots so
337          * that we write logs to each spot about the same number of times.
338          */
339
340         /* Find a log slot for the next flight, if available */
341         ao_log_current_pos = ao_log_end_pos = 0;
342         log_slots = ao_log_slots();
343         log_want = (ao_flight_number - 1) % log_slots;
344         log_slot = log_want;
345         do {
346                 if (ao_log_flight(log_slot) == 0) {
347                         ao_log_current_pos = ao_log_pos(log_slot);
348                         ao_log_end_pos = ao_log_pos_block_end(log_slot);
349                         break;
350                 }
351                 if (++log_slot >= log_slots)
352                         log_slot = 0;
353         } while (log_slot != log_want);
354         ao_wakeup(&ao_flight_number);
355         return 0;
356 #endif
357 }
358
359 void
360 ao_log_start(void)
361 {
362         /* start logging */
363         ao_log_running = 1;
364         ao_wakeup(&ao_log_running);
365 }
366
367 void
368 ao_log_stop(void)
369 {
370         ao_log_running = 0;
371         ao_log_flush();
372 }
373
374 uint8_t
375 ao_log_present(void)
376 {
377         return ao_log_max_flight() != 0;
378 }
379
380 uint8_t
381 ao_log_full(void)
382 {
383         return ao_log_current_pos == ao_log_end_pos;
384 }
385
386 #ifndef LOG_ADC
387 #define LOG_ADC HAS_ADC
388 #endif
389
390 #if LOG_ADC
391 static struct ao_task ao_log_task;
392 #endif
393
394 static void
395 ao_log_list(void) 
396 {
397         uint8_t slot;
398         uint8_t slots;
399         int16_t flight;
400
401         slots = ao_log_slots();
402         for (slot = 0; slot < slots; slot++)
403         {
404                 flight = ao_log_flight(slot);
405                 if (flight)
406                         printf ("flight %d start %x end %x\n",
407                                 flight,
408                                 (uint16_t) (ao_log_pos(slot) >> 8),
409                                 (uint16_t) (ao_log_pos_block_end(slot) >> 8));
410                 else
411                         printf ("slot %d  start %x end %x\n",
412                                 slot,
413                                 (uint16_t) (ao_log_pos(slot) >> 8),
414                                 (uint16_t) (ao_log_pos_block_end(slot) >> 8));
415         }
416         printf ("done\n");
417 }
418
419 static void
420 ao_log_delete(void) 
421 {
422         uint8_t slot;
423         uint8_t slots;
424         int16_t cmd_flight = 1;
425
426         ao_cmd_white();
427         if (ao_cmd_lex_c == '-') {
428                 cmd_flight = -1;
429                 ao_cmd_lex();
430         }
431         cmd_flight *= ao_cmd_decimal();
432         if (ao_cmd_status != ao_cmd_success)
433                 return;
434
435         slots = ao_log_slots();
436         /* Look for the flight log matching the requested flight */
437         if (cmd_flight) {
438                 for (slot = 0; slot < slots; slot++) {
439                         if (ao_log_flight(slot) == cmd_flight) {
440 #if HAS_TRACKER
441                                 ao_tracker_erase_start(cmd_flight);
442 #endif
443                                 if (ao_log_erase(slot))
444                                         puts("Erased");
445                                 else
446                                         puts("Failed to erase");
447 #if HAS_TRACKER
448                                 ao_tracker_erase_end();
449 #endif
450                                 return;
451                         }
452                 }
453         }
454         printf("No such flight: %d\n", cmd_flight);
455 }
456
457 const struct ao_cmds ao_log_cmds[] = {
458         { ao_log_list,  "l\0List logs" },
459         { ao_log_delete,        "d <flight-number>\0Delete flight" },
460         { 0,    NULL },
461 };
462
463 void
464 ao_log_init(void)
465 {
466         ao_log_running = 0;
467
468         /* For now, just log the flight starting at the begining of eeprom */
469         ao_log_state = ao_flight_invalid;
470
471         ao_cmd_register(&ao_log_cmds[0]);
472
473 #ifndef HAS_ADC
474 #error Define HAS_ADC for ao_log.c
475 #endif
476 #if LOG_ADC
477         /* Create a task to log events to eeprom */
478         ao_add_task(&ao_log_task, ao_log, "log");
479 #endif
480 }