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 /* 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 -(int16_t) (slot + 1);
194 if (!ao_log_check_data() || ao_log_data.type != AO_LOG_FLIGHT)
195 return -(int16_t) (slot + 1);
197 return 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 int16_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 = 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();
276 #if FLIGHT_LOG_APPEND
278 /* Deal with older OS versions which stored multiple
279 * flights in rom by erasing everything after the first
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);
289 ao_config_log_fix_append();
291 ao_log_current_pos = ao_log_pos(0);
292 ao_log_end_pos = ao_log_pos_block_end(0);
294 if (ao_flight_number) {
295 uint32_t full = ao_log_current_pos;
296 uint32_t empty = ao_log_end_pos - AO_LOG_SIZE;
298 /* If there's already a flight started, then find the
302 ao_log_current_pos = (full + empty) >> 1;
303 ao_log_current_pos -= ao_log_current_pos % AO_LOG_SIZE;
305 if (ao_log_current_pos == full) {
306 if (ao_log_check(ao_log_current_pos) != AO_LOG_EMPTY)
307 ao_log_current_pos += AO_LOG_SIZE;
310 if (ao_log_current_pos == empty)
313 if (ao_log_check(ao_log_current_pos) != AO_LOG_EMPTY) {
314 full = ao_log_current_pos;
316 empty = ao_log_current_pos;
321 ao_log_find_max_erase_flight();
324 ao_wakeup(&ao_flight_number);
327 if (ao_flight_number) {
329 if (ao_flight_number <= 0)
330 ao_flight_number = 1;
333 ao_log_find_max_erase_flight();
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.
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;
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);
351 if (++log_slot >= log_slots)
353 } while (log_slot != log_want);
354 ao_wakeup(&ao_flight_number);
364 ao_wakeup(&ao_log_running);
377 return ao_log_max_flight() != 0;
383 return ao_log_current_pos == ao_log_end_pos;
387 #define LOG_ADC HAS_ADC
391 static struct ao_task ao_log_task;
401 slots = ao_log_slots();
402 for (slot = 0; slot < slots; slot++)
404 flight = ao_log_flight(slot);
406 printf ("flight %d start %x end %x\n",
408 (uint16_t) (ao_log_pos(slot) >> 8),
409 (uint16_t) (ao_log_pos_block_end(slot) >> 8));
411 printf ("slot %d start %x end %x\n",
413 (uint16_t) (ao_log_pos(slot) >> 8),
414 (uint16_t) (ao_log_pos_block_end(slot) >> 8));
424 int16_t cmd_flight = 1;
427 if (ao_cmd_lex_c == '-') {
431 cmd_flight *= ao_cmd_decimal();
432 if (ao_cmd_status != ao_cmd_success)
435 slots = ao_log_slots();
436 /* Look for the flight log matching the requested flight */
438 for (slot = 0; slot < slots; slot++) {
439 if (ao_log_flight(slot) == cmd_flight) {
441 ao_tracker_erase_start(cmd_flight);
443 if (ao_log_erase(slot))
446 puts("Failed to erase");
448 ao_tracker_erase_end();
454 printf("No such flight: %d\n", cmd_flight);
457 const struct ao_cmds ao_log_cmds[] = {
458 { ao_log_list, "l\0List logs" },
459 { ao_log_delete, "d <flight-number>\0Delete flight" },
468 /* For now, just log the flight starting at the begining of eeprom */
469 ao_log_state = ao_flight_invalid;
471 ao_cmd_register(&ao_log_cmds[0]);
474 #error Define HAS_ADC for ao_log.c
477 /* Create a task to log events to eeprom */
478 ao_add_task(&ao_log_task, ao_log, "log");