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.
21 __pdata uint32_t ao_log_current_pos;
22 __pdata uint32_t ao_log_end_pos;
23 __pdata uint32_t ao_log_start_pos;
24 __xdata uint8_t ao_log_running;
25 __pdata enum ao_flight_state ao_log_state;
26 __xdata uint16_t ao_flight_number;
35 * When erasing a flight log, make sure the config block
36 * has an up-to-date version of the current flight number
44 static __xdata struct ao_log_erase erase;
46 #define LOG_MAX_ERASE 16
49 ao_log_erase_pos(uint8_t i)
51 return i * sizeof (struct ao_log_erase) + AO_STORAGE_ERASE_LOG;
55 ao_log_write_erase(uint8_t pos)
58 erase.flight = ao_flight_number;
59 ao_storage_write(ao_log_erase_pos(pos), &erase, sizeof (erase));
64 ao_log_read_erase(uint8_t pos)
66 ao_storage_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
71 ao_log_erase_mark(void)
75 for (i = 0; i < LOG_MAX_ERASE; i++) {
77 if (erase.unused == 0 && erase.flight == ao_flight_number)
79 if (erase.unused == 0xff) {
80 ao_log_write_erase(i);
90 return (uint8_t) (ao_storage_config / ao_config.flight_log_max);
94 ao_log_pos(uint8_t slot)
96 return ((slot) * ao_config.flight_log_max);
100 ao_log_max_flight(void)
105 uint16_t max_flight = 0;
107 /* Scan the log space looking for the biggest flight number */
108 log_slots = ao_log_slots();
109 for (log_slot = 0; log_slot < log_slots; log_slot++) {
110 log_flight = ao_log_flight(log_slot);
113 if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0)
114 max_flight = log_flight;
120 ao_log_scan(void) __reentrant
128 ao_flight_number = ao_log_max_flight();
129 if (ao_flight_number)
130 if (++ao_flight_number == 0)
131 ao_flight_number = 1;
133 /* Now look through the log of flight numbers from erase operations and
134 * see if the last one is bigger than what we found above
136 for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
137 ao_log_read_erase(log_slot);
138 if (erase.unused == 0) {
139 if (ao_flight_number == 0 ||
140 (int16_t) (erase.flight - ao_flight_number) > 0)
141 ao_flight_number = erase.flight;
145 if (ao_flight_number == 0)
146 ao_flight_number = 1;
148 /* With a flight number in hand, find a place to write a new log,
149 * use the target flight number to index the available log slots so
150 * that we write logs to each spot about the same number of times.
153 /* Find a log slot for the next flight, if available */
154 ao_log_current_pos = ao_log_end_pos = 0;
155 log_slots = ao_log_slots();
156 log_want = (ao_flight_number - 1) % log_slots;
159 if (ao_log_flight(log_slot) == 0) {
160 ao_log_current_pos = ao_log_pos(log_slot);
161 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
164 if (++log_slot >= log_slots)
166 } while (log_slot != log_want);
168 ao_wakeup(&ao_flight_number);
176 ao_wakeup(&ao_log_running);
189 return ao_log_max_flight() != 0;
195 return ao_log_current_pos == ao_log_end_pos;
198 static __xdata struct ao_task ao_log_task;
201 ao_log_list(void) __reentrant
207 slots = ao_log_slots();
208 for (slot = 0; slot < slots; slot++)
210 flight = ao_log_flight(slot);
212 printf ("flight %d start %x end %x\n",
214 (uint16_t) (ao_log_pos(slot) >> 8),
215 (uint16_t) (ao_log_pos(slot+1) >> 8));
221 ao_log_delete(void) __reentrant
227 if (ao_cmd_status != ao_cmd_success)
230 slots = ao_log_slots();
231 /* Look for the flight log matching the requested flight */
233 for (slot = 0; slot < slots; slot++) {
234 if (ao_log_flight(slot) == ao_cmd_lex_i) {
236 ao_log_current_pos = ao_log_pos(slot);
237 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
238 while (ao_log_current_pos < ao_log_end_pos) {
240 static __xdata uint8_t b;
243 * Check to see if we've reached the end of
244 * the used memory to avoid re-erasing the same
245 * memory over and over again
247 for (i = 0; i < 16; i++) {
248 if (ao_storage_read(ao_log_current_pos + i, &b, 1))
254 ao_storage_erase(ao_log_current_pos);
255 ao_log_current_pos += ao_storage_block;
262 printf("No such flight: %d\n", ao_cmd_lex_i);
265 __code struct ao_cmds ao_log_cmds[] = {
266 { ao_log_list, "l\0List logs" },
267 { ao_log_delete, "d <flight-number>\0Delete flight" },
276 /* For now, just log the flight starting at the begining of eeprom */
277 ao_log_state = ao_flight_invalid;
279 ao_cmd_register(&ao_log_cmds[0]);
281 /* Create a task to log events to eeprom */
282 ao_add_task(&ao_log_task, ao_log, "log");