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;
28 __code uint8_t ao_log_format = AO_LOG_FORMAT_FULL;
37 * When erasing a flight log, make sure the config block
38 * has an up-to-date version of the current flight number
46 static __xdata struct ao_log_erase erase;
48 #define LOG_MAX_ERASE 16
51 ao_log_erase_pos(uint8_t i)
53 return i * sizeof (struct ao_log_erase) + AO_STORAGE_ERASE_LOG;
57 ao_log_write_erase(uint8_t pos)
60 erase.flight = ao_flight_number;
61 ao_storage_write(ao_log_erase_pos(pos), &erase, sizeof (erase));
66 ao_log_read_erase(uint8_t pos)
68 ao_storage_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
73 ao_log_erase_mark(void)
77 for (i = 0; i < LOG_MAX_ERASE; i++) {
79 if (erase.unused == 0 && erase.flight == ao_flight_number)
81 if (erase.unused == 0xff) {
82 ao_log_write_erase(i);
92 return (uint8_t) (ao_storage_config / ao_config.flight_log_max);
96 ao_log_pos(uint8_t slot)
98 return ((slot) * ao_config.flight_log_max);
102 ao_log_max_flight(void)
107 uint16_t max_flight = 0;
109 /* Scan the log space looking for the biggest flight number */
110 log_slots = ao_log_slots();
111 for (log_slot = 0; log_slot < log_slots; log_slot++) {
112 log_flight = ao_log_flight(log_slot);
115 if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0)
116 max_flight = log_flight;
122 ao_log_scan(void) __reentrant
130 ao_flight_number = ao_log_max_flight();
131 if (ao_flight_number)
132 if (++ao_flight_number == 0)
133 ao_flight_number = 1;
135 /* Now look through the log of flight numbers from erase operations and
136 * see if the last one is bigger than what we found above
138 for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
139 ao_log_read_erase(log_slot);
140 if (erase.unused == 0) {
141 if (ao_flight_number == 0 ||
142 (int16_t) (erase.flight - ao_flight_number) > 0)
143 ao_flight_number = erase.flight;
147 if (ao_flight_number == 0)
148 ao_flight_number = 1;
150 /* With a flight number in hand, find a place to write a new log,
151 * use the target flight number to index the available log slots so
152 * that we write logs to each spot about the same number of times.
155 /* Find a log slot for the next flight, if available */
156 ao_log_current_pos = ao_log_end_pos = 0;
157 log_slots = ao_log_slots();
158 log_want = (ao_flight_number - 1) % log_slots;
161 if (ao_log_flight(log_slot) == 0) {
162 ao_log_current_pos = ao_log_pos(log_slot);
163 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
166 if (++log_slot >= log_slots)
168 } while (log_slot != log_want);
170 ao_wakeup(&ao_flight_number);
178 ao_wakeup(&ao_log_running);
191 return ao_log_max_flight() != 0;
197 return ao_log_current_pos == ao_log_end_pos;
200 static __xdata struct ao_task ao_log_task;
203 ao_log_list(void) __reentrant
209 slots = ao_log_slots();
210 for (slot = 0; slot < slots; slot++)
212 flight = ao_log_flight(slot);
214 printf ("flight %d start %x end %x\n",
216 (uint16_t) (ao_log_pos(slot) >> 8),
217 (uint16_t) (ao_log_pos(slot+1) >> 8));
223 ao_log_delete(void) __reentrant
229 if (ao_cmd_status != ao_cmd_success)
232 slots = ao_log_slots();
233 /* Look for the flight log matching the requested flight */
235 for (slot = 0; slot < slots; slot++) {
236 if (ao_log_flight(slot) == ao_cmd_lex_i) {
238 ao_log_current_pos = ao_log_pos(slot);
239 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
240 while (ao_log_current_pos < ao_log_end_pos) {
242 static __xdata uint8_t b;
245 * Check to see if we've reached the end of
246 * the used memory to avoid re-erasing the same
247 * memory over and over again
249 for (i = 0; i < 16; i++) {
250 if (ao_storage_read(ao_log_current_pos + i, &b, 1))
256 ao_storage_erase(ao_log_current_pos);
257 ao_log_current_pos += ao_storage_block;
264 printf("No such flight: %d\n", ao_cmd_lex_i);
267 __code struct ao_cmds ao_log_cmds[] = {
268 { ao_log_list, "l\0List logs" },
269 { ao_log_delete, "d <flight-number>\0Delete flight" },
278 /* For now, just log the flight starting at the begining of eeprom */
279 ao_log_state = ao_flight_invalid;
281 ao_cmd_register(&ao_log_cmds[0]);
283 /* Create a task to log events to eeprom */
284 ao_add_task(&ao_log_task, ao_log, "log");