2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 #include <ao_config.h>
23 #include <ao_tracker.h>
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;
41 * When erasing a flight log, make sure the config block
42 * has an up-to-date version of the current flight number
50 static struct ao_log_erase erase;
53 #define LOG_MAX_ERASE 16
56 #ifndef LOG_ERASE_MARK
58 #error "Must define LOG_ERASE_MARK with USE_EEPROM_CONFIG"
60 #define LOG_ERASE_MARK 0x00
64 ao_log_erase_pos(uint8_t i)
66 return i * sizeof (struct ao_log_erase) + AO_CONFIG_MAX_SIZE;
70 ao_log_write_erase(uint8_t pos)
72 erase.mark = LOG_ERASE_MARK;
73 erase.flight = ao_flight_number;
74 ao_config_write(ao_log_erase_pos(pos), &erase, sizeof (erase));
79 for (i = 1; i < LOG_MAX_ERASE; i++) {
80 erase.mark = ~LOG_ERASE_MARK;
82 ao_config_write(ao_log_erase_pos(i), &erase, sizeof (erase));
91 ao_log_read_erase(uint8_t pos)
93 ao_config_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
98 ao_log_erase_mark(void)
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)
106 if (erase.mark != LOG_ERASE_MARK) {
107 ao_log_write_erase(i);
114 #ifndef AO_LOG_UNCOMMON
116 * Common logging functions which depend on the type of the log data
120 ao_log_type ao_log_data;
123 ao_log_csum(uint8_t *b)
128 for (i = 0; i < sizeof (ao_log_type); i++)
134 ao_log_write(ao_log_type *log)
139 log->csum = ao_log_csum((uint8_t *) log);
140 ao_mutex_get(&ao_log_mutex); {
141 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
143 if (ao_log_running) {
145 ao_storage_write(ao_log_current_pos,
147 sizeof (ao_log_type));
148 ao_log_current_pos += sizeof (ao_log_type);
150 } ao_mutex_put(&ao_log_mutex);
155 ao_log_check_data(void)
157 if (ao_log_csum((uint8_t *) &ao_log_data) != 0)
163 ao_log_check_clear(void)
165 uint8_t *b = (uint8_t *) &ao_log_data;
168 for (i = 0; i < sizeof (ao_log_type); i++) {
176 ao_log_flight(uint8_t slot)
178 if (!ao_storage_read(ao_log_pos(slot),
180 sizeof (ao_log_type)))
181 return -(int16_t) (slot + 1);
183 if (ao_log_check_clear())
186 if (!ao_log_check_data() || ao_log_data.type != AO_LOG_FLIGHT)
187 return -(int16_t) (slot + 1);
189 return ao_log_data.u.flight.flight;
196 return (uint8_t) (ao_storage_log_max / ao_config.flight_log_max);
200 ao_log_pos(uint8_t slot)
202 return ((slot) * ao_config.flight_log_max);
206 ao_log_max_flight(void)
211 int16_t max_flight = 0;
213 /* Scan the log space looking for the biggest flight number */
214 log_slots = ao_log_slots();
215 for (log_slot = 0; log_slot < log_slots; log_slot++) {
216 log_flight = ao_log_flight(log_slot);
219 if (max_flight == 0 || log_flight > max_flight)
220 max_flight = log_flight;
226 ao_log_erase(uint8_t slot)
228 uint32_t log_current_pos, log_end_pos;
231 log_current_pos = ao_log_pos(slot);
232 log_end_pos = log_current_pos + ao_config.flight_log_max;
233 while (log_current_pos < log_end_pos) {
238 * Check to see if we've reached the end of
239 * the used memory to avoid re-erasing the same
240 * memory over and over again
242 for (i = 0; i < 16; i++) {
243 if (ao_storage_read(log_current_pos + i, &b, 1))
249 ao_storage_erase(log_current_pos);
250 log_current_pos += ao_storage_block;
255 ao_log_find_max_erase_flight(void)
259 /* Now look through the log of flight numbers from erase operations and
260 * see if the last one is bigger than what we found above
262 for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
263 ao_log_read_erase(log_slot);
264 if (erase.mark == LOG_ERASE_MARK) {
265 if (ao_flight_number == 0 ||
266 (int16_t) (erase.flight - ao_flight_number) > 0)
267 ao_flight_number = erase.flight;
271 if (ao_flight_number == 0)
272 ao_flight_number = 1;
280 #if FLIGHT_LOG_APPEND
288 /* Get any existing flight number */
289 ao_flight_number = ao_log_max_flight();
291 #if FLIGHT_LOG_APPEND
293 /* Deal with older OS versions which stored multiple
294 * flights in rom by erasing everything after the first
297 if (ao_config.flight_log_max != ao_storage_log_max) {
298 log_slots = ao_log_slots();
299 for (log_slot = 1; log_slot < log_slots; log_slot++) {
300 if (ao_log_flight(log_slot) != 0)
301 ao_log_erase(log_slot);
303 ao_config_log_fix_append();
305 ao_log_current_pos = ao_log_pos(0);
306 ao_log_end_pos = ao_log_current_pos + ao_storage_log_max;
308 if (ao_flight_number) {
309 uint32_t full = ao_log_current_pos;
310 uint32_t empty = ao_log_end_pos - AO_LOG_SIZE;
312 /* If there's already a flight started, then find the
316 ao_log_current_pos = (full + empty) >> 1;
317 ao_log_current_pos -= ao_log_current_pos % AO_LOG_SIZE;
319 if (ao_log_current_pos == full) {
320 if (ao_log_check(ao_log_current_pos) != AO_LOG_EMPTY)
321 ao_log_current_pos += AO_LOG_SIZE;
324 if (ao_log_current_pos == empty)
327 if (ao_log_check(ao_log_current_pos) != AO_LOG_EMPTY) {
328 full = ao_log_current_pos;
330 empty = ao_log_current_pos;
335 ao_log_find_max_erase_flight();
338 ao_wakeup(&ao_flight_number);
341 if (ao_flight_number) {
343 if (ao_flight_number <= 0)
344 ao_flight_number = 1;
347 ao_log_find_max_erase_flight();
349 /* With a flight number in hand, find a place to write a new log,
350 * use the target flight number to index the available log slots so
351 * that we write logs to each spot about the same number of times.
354 /* Find a log slot for the next flight, if available */
355 ao_log_current_pos = ao_log_end_pos = 0;
356 log_slots = ao_log_slots();
357 log_want = (ao_flight_number - 1) % log_slots;
360 if (ao_log_flight(log_slot) == 0) {
361 ao_log_current_pos = ao_log_pos(log_slot);
362 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
365 if (++log_slot >= log_slots)
367 } while (log_slot != log_want);
368 ao_wakeup(&ao_flight_number);
378 ao_wakeup(&ao_log_running);
391 return ao_log_max_flight() != 0;
397 return ao_log_current_pos == ao_log_end_pos;
401 #define LOG_ADC HAS_ADC
405 static struct ao_task ao_log_task;
415 slots = ao_log_slots();
416 for (slot = 0; slot < slots; slot++)
418 flight = ao_log_flight(slot);
420 printf ("flight %d start %x end %x\n",
422 (uint16_t) (ao_log_pos(slot) >> 8),
423 (uint16_t) (ao_log_pos(slot+1) >> 8));
433 int16_t cmd_flight = 1;
436 if (ao_cmd_lex_c == '-') {
440 cmd_flight *= ao_cmd_decimal();
441 if (ao_cmd_status != ao_cmd_success)
444 slots = ao_log_slots();
445 /* Look for the flight log matching the requested flight */
447 for (slot = 0; slot < slots; slot++) {
448 if (ao_log_flight(slot) == cmd_flight) {
450 ao_tracker_erase_start(cmd_flight);
454 ao_tracker_erase_end();
461 printf("No such flight: %d\n", cmd_flight);
464 const struct ao_cmds ao_log_cmds[] = {
465 { ao_log_list, "l\0List logs" },
466 { ao_log_delete, "d <flight-number>\0Delete flight" },
475 /* For now, just log the flight starting at the begining of eeprom */
476 ao_log_state = ao_flight_invalid;
478 ao_cmd_register(&ao_log_cmds[0]);
481 #error Define HAS_ADC for ao_log.c
484 /* Create a task to log events to eeprom */
485 ao_add_task(&ao_log_task, ao_log, "log");