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 uint16_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 = (uint8_t) ~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 /* Position of first flight record in slot */
116 ao_log_pos(uint8_t slot)
118 return ((slot) * ao_config.flight_log_max);
121 /* Start of erase block containing first flight record */
123 ao_log_pos_block_start(uint8_t slot)
125 return ao_log_pos(slot) & ~(ao_storage_block - 1);
128 /* End of erase block containing last flight record */
130 ao_log_pos_block_end(uint8_t slot)
132 return ao_log_pos_block_start(slot + 1);
135 #ifndef AO_LOG_UNCOMMON
137 * Common logging functions which depend on the type of the log data
141 ao_log_type ao_log_data;
144 ao_log_csum(uint8_t *b)
149 for (i = 0; i < sizeof (ao_log_type); i++)
155 ao_log_write(ao_log_type *log)
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)
164 if (ao_log_running) {
166 ao_storage_write(ao_log_current_pos,
168 sizeof (ao_log_type));
169 ao_log_current_pos += sizeof (ao_log_type);
171 } ao_mutex_put(&ao_log_mutex);
176 ao_log_check_data(void)
178 if (ao_log_csum((uint8_t *) &ao_log_data) != 0)
184 ao_log_flight(uint8_t slot)
186 if (ao_storage_is_erased(ao_log_pos_block_start(slot)))
189 if (!ao_storage_read(ao_log_pos(slot),
191 sizeof (ao_log_type)))
192 return -(int32_t) (slot + 1);
194 if (!ao_log_check_data() || ao_log_data.type != AO_LOG_FLIGHT)
195 return -(int32_t) (slot + 1);
197 return (int32_t) ao_log_data.u.flight.flight;
204 return (uint8_t) (ao_storage_log_max / ao_config.flight_log_max);
208 ao_log_max_flight(void)
213 uint16_t max_flight = 0;
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);
221 if (max_flight == 0 || log_flight > max_flight)
222 max_flight = (uint16_t) log_flight;
228 ao_log_erase(uint8_t slot)
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);
240 ao_log_find_max_erase_flight(void)
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
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;
256 if (ao_flight_number == 0)
257 ao_flight_number = 1;
265 #if FLIGHT_LOG_APPEND
273 /* Get any existing flight number */
274 ao_flight_number = ao_log_max_flight();
278 #if FLIGHT_LOG_APPEND
280 /* Deal with older OS versions which stored multiple
281 * flights in rom by erasing everything after the first
284 if (ao_config.flight_log_max != ao_storage_log_max) {
285 log_slots = ao_log_slots();
286 for (log_slot = 1; log_slot < log_slots; log_slot++) {
287 if (ao_log_flight(log_slot) != 0)
288 if (!ao_log_erase(log_slot))
289 printf("erase %d failed\n", log_slot);
291 ao_config_log_fix_append();
293 ao_log_current_pos = ao_log_pos(0);
294 ao_log_end_pos = ao_log_pos_block_end(0);
296 if (ao_flight_number) {
297 uint32_t full = (ao_log_current_pos) / AO_LOG_SIZE;
298 uint32_t empty = (ao_log_end_pos - AO_LOG_SIZE) / AO_LOG_SIZE;
300 /* If there's already a flight started, then find the
304 uint32_t current = (full + empty) >> 1;
305 ao_log_current_pos = current * AO_LOG_SIZE;
307 if (current == full) {
308 if (ao_log_check(ao_log_current_pos) != AO_LOG_EMPTY)
309 ao_log_current_pos += AO_LOG_SIZE;
312 if (current == empty)
315 if (ao_log_check(ao_log_current_pos) != AO_LOG_EMPTY) {
323 ao_log_find_max_erase_flight();
326 ao_wakeup(&ao_flight_number);
329 if (ao_flight_number) {
331 if (ao_flight_number <= 0)
332 ao_flight_number = 1;
335 ao_log_find_max_erase_flight();
337 /* With a flight number in hand, find a place to write a new log,
338 * use the target flight number to index the available log slots so
339 * that we write logs to each spot about the same number of times.
342 /* Find a log slot for the next flight, if available */
343 ao_log_current_pos = ao_log_end_pos = 0;
344 log_slots = ao_log_slots();
345 log_want = (uint8_t) ((ao_flight_number - 1) % log_slots);
348 if (ao_log_flight(log_slot) == 0) {
349 ao_log_current_pos = ao_log_pos(log_slot);
350 ao_log_end_pos = ao_log_pos_block_end(log_slot);
353 if (++log_slot >= log_slots)
355 } while (log_slot != log_want);
356 ao_wakeup(&ao_flight_number);
366 ao_wakeup(&ao_log_running);
379 return ao_log_max_flight() != 0;
385 return ao_log_current_pos == ao_log_end_pos;
389 #define LOG_ADC HAS_ADC
393 static struct ao_task ao_log_task;
403 slots = ao_log_slots();
404 for (slot = 0; slot < slots; slot++)
406 flight = ao_log_flight(slot);
408 printf ("flight %ld start %x end %x\n",
410 (uint16_t) (ao_log_pos(slot) >> 8),
411 (uint16_t) (ao_log_pos_block_end(slot) >> 8));
413 printf ("slot %d start %x end %x\n",
415 (uint16_t) (ao_log_pos(slot) >> 8),
416 (uint16_t) (ao_log_pos_block_end(slot) >> 8));
426 int32_t cmd_flight = 1;
429 if (ao_cmd_lex_c == '-') {
433 cmd_flight *= (int32_t) ao_cmd_decimal();
434 if (ao_cmd_status != ao_cmd_success)
437 slots = ao_log_slots();
438 /* Look for the flight log matching the requested flight */
440 for (slot = 0; slot < slots; slot++) {
441 if (ao_log_flight(slot) == cmd_flight) {
443 ao_tracker_erase_start(cmd_flight);
445 if (ao_log_erase(slot))
448 puts("Failed to erase");
450 ao_tracker_erase_end();
456 printf("No such flight: %ld\n", cmd_flight);
459 const struct ao_cmds ao_log_cmds[] = {
460 { ao_log_list, "l\0List logs" },
461 { ao_log_delete, "d <flight-number>\0Delete flight" },
470 /* For now, just log the flight starting at the begining of eeprom */
471 ao_log_state = ao_flight_invalid;
473 ao_cmd_register(&ao_log_cmds[0]);
476 #error Define HAS_ADC for ao_log.c
479 /* Create a task to log events to eeprom */
480 ao_add_task(&ao_log_task, ao_log, "log");