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; version 2 of the License.
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.
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.
20 #include <ao_config.h>
22 #include <ao_tracker.h>
25 __xdata uint8_t ao_log_mutex;
26 __pdata uint32_t ao_log_current_pos;
27 __pdata uint32_t ao_log_end_pos;
28 __pdata uint32_t ao_log_start_pos;
29 __xdata uint8_t ao_log_running;
30 __pdata enum ao_flight_state ao_log_state;
31 __xdata uint16_t ao_flight_number;
40 * When erasing a flight log, make sure the config block
41 * has an up-to-date version of the current flight number
49 static __xdata struct ao_log_erase erase;
52 #define LOG_MAX_ERASE 16
55 #ifndef LOG_ERASE_MARK
57 #error "Must define LOG_ERASE_MARK with USE_EEPROM_CONFIG"
59 #define LOG_ERASE_MARK 0x00
63 ao_log_erase_pos(uint8_t i)
65 return i * sizeof (struct ao_log_erase) + AO_CONFIG_MAX_SIZE;
69 ao_log_write_erase(uint8_t pos)
71 erase.mark = LOG_ERASE_MARK;
72 erase.flight = ao_flight_number;
73 ao_config_write(ao_log_erase_pos(pos), &erase, sizeof (erase));
78 for (i = 1; i < LOG_MAX_ERASE; i++) {
79 erase.mark = ~LOG_ERASE_MARK;
81 ao_config_write(ao_log_erase_pos(i), &erase, sizeof (erase));
90 ao_log_read_erase(uint8_t pos)
92 ao_config_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
97 ao_log_erase_mark(void)
101 for (i = 0; i < LOG_MAX_ERASE; i++) {
102 ao_log_read_erase(i);
103 if (erase.mark == LOG_ERASE_MARK && erase.flight == ao_flight_number)
105 if (erase.mark != LOG_ERASE_MARK) {
106 ao_log_write_erase(i);
116 return (uint8_t) (ao_storage_log_max / ao_config.flight_log_max);
120 ao_log_pos(uint8_t slot)
122 return ((slot) * ao_config.flight_log_max);
126 ao_log_max_flight(void)
131 uint16_t max_flight = 0;
133 /* Scan the log space looking for the biggest flight number */
134 log_slots = ao_log_slots();
135 for (log_slot = 0; log_slot < log_slots; log_slot++) {
136 log_flight = ao_log_flight(log_slot);
139 if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0)
140 max_flight = log_flight;
146 ao_log_erase(uint8_t slot) __reentrant
148 uint32_t log_current_pos, log_end_pos;
151 log_current_pos = ao_log_pos(slot);
152 log_end_pos = log_current_pos + ao_config.flight_log_max;
153 while (log_current_pos < log_end_pos) {
155 static __xdata uint8_t b;
158 * Check to see if we've reached the end of
159 * the used memory to avoid re-erasing the same
160 * memory over and over again
162 for (i = 0; i < 16; i++) {
163 if (ao_storage_read(log_current_pos + i, &b, 1))
169 ao_storage_erase(log_current_pos);
170 log_current_pos += ao_storage_block;
175 ao_log_find_max_erase_flight(void) __reentrant
179 /* Now look through the log of flight numbers from erase operations and
180 * see if the last one is bigger than what we found above
182 for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
183 ao_log_read_erase(log_slot);
184 if (erase.mark == LOG_ERASE_MARK) {
185 if (ao_flight_number == 0 ||
186 (int16_t) (erase.flight - ao_flight_number) > 0)
187 ao_flight_number = erase.flight;
191 if (ao_flight_number == 0)
192 ao_flight_number = 1;
196 ao_log_scan(void) __reentrant
200 #if FLIGHT_LOG_APPEND
208 /* Get any existing flight number */
209 ao_flight_number = ao_log_max_flight();
211 #if FLIGHT_LOG_APPEND
213 /* Deal with older OS versions which stored multiple
214 * flights in rom by erasing everything after the first
217 if (ao_config.flight_log_max != ao_storage_log_max) {
218 log_slots = ao_log_slots();
219 for (log_slot = 1; log_slot < log_slots; log_slot++) {
220 if (ao_log_flight(log_slot) != 0)
221 ao_log_erase(log_slot);
223 ao_config_log_fix_append();
225 ao_log_current_pos = ao_log_pos(0);
226 ao_log_end_pos = ao_log_current_pos + ao_storage_log_max;
228 if (ao_flight_number) {
229 uint32_t full = ao_log_current_pos;
230 uint32_t empty = ao_log_end_pos - ao_log_size;
232 /* If there's already a flight started, then find the
236 ao_log_current_pos = (full + empty) >> 1;
237 ao_log_current_pos -= ao_log_current_pos % ao_log_size;
239 if (ao_log_current_pos == full) {
240 if (ao_log_check(ao_log_current_pos))
241 ao_log_current_pos += ao_log_size;
244 if (ao_log_current_pos == empty)
247 if (ao_log_check(ao_log_current_pos)) {
248 full = ao_log_current_pos;
250 empty = ao_log_current_pos;
255 ao_log_find_max_erase_flight();
258 ao_wakeup(&ao_flight_number);
262 if (ao_flight_number)
263 if (++ao_flight_number == 0)
264 ao_flight_number = 1;
266 ao_log_find_max_erase_flight();
268 /* With a flight number in hand, find a place to write a new log,
269 * use the target flight number to index the available log slots so
270 * that we write logs to each spot about the same number of times.
273 /* Find a log slot for the next flight, if available */
274 ao_log_current_pos = ao_log_end_pos = 0;
275 log_slots = ao_log_slots();
276 log_want = (ao_flight_number - 1) % log_slots;
279 if (ao_log_flight(log_slot) == 0) {
280 ao_log_current_pos = ao_log_pos(log_slot);
281 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
284 if (++log_slot >= log_slots)
286 } while (log_slot != log_want);
287 ao_wakeup(&ao_flight_number);
297 ao_wakeup(&ao_log_running);
310 return ao_log_max_flight() != 0;
316 return ao_log_current_pos == ao_log_end_pos;
320 #define LOG_ADC HAS_ADC
324 static __xdata struct ao_task ao_log_task;
328 ao_log_list(void) __reentrant
334 slots = ao_log_slots();
335 for (slot = 0; slot < slots; slot++)
337 flight = ao_log_flight(slot);
339 printf ("flight %d start %x end %x\n",
341 (uint16_t) (ao_log_pos(slot) >> 8),
342 (uint16_t) (ao_log_pos(slot+1) >> 8));
348 ao_log_delete(void) __reentrant
354 if (ao_cmd_status != ao_cmd_success)
357 slots = ao_log_slots();
358 /* Look for the flight log matching the requested flight */
360 for (slot = 0; slot < slots; slot++) {
361 if (ao_log_flight(slot) == ao_cmd_lex_i) {
363 ao_tracker_erase_start(ao_cmd_lex_i);
367 ao_tracker_erase_end();
374 printf("No such flight: %d\n", ao_cmd_lex_i);
377 __code struct ao_cmds ao_log_cmds[] = {
378 { ao_log_list, "l\0List logs" },
379 { ao_log_delete, "d <flight-number>\0Delete flight" },
388 /* For now, just log the flight starting at the begining of eeprom */
389 ao_log_state = ao_flight_invalid;
391 ao_cmd_register(&ao_log_cmds[0]);
394 #error Define HAS_ADC for ao_log.c
397 /* Create a task to log events to eeprom */
398 ao_add_task(&ao_log_task, ao_log, "log");