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 __pdata uint32_t ao_log_current_pos;
21 __pdata uint32_t ao_log_end_pos;
22 __pdata uint32_t ao_log_start_pos;
23 __xdata uint8_t ao_log_running;
24 __xdata enum flight_state ao_log_state;
25 __xdata uint16_t ao_flight_number;
34 * When erasing a flight log, make sure the config block
35 * has an up-to-date version of the current flight number
43 static __xdata struct ao_log_erase erase;
45 #define LOG_MAX_ERASE 16
48 ao_log_erase_pos(uint8_t i)
50 return i * sizeof (struct ao_log_erase) + AO_STORAGE_ERASE_LOG;
54 ao_log_write_erase(uint8_t pos)
57 erase.flight = ao_flight_number;
58 ao_storage_write(ao_log_erase_pos(pos), &erase, sizeof (erase));
63 ao_log_read_erase(uint8_t pos)
65 ao_storage_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
70 ao_log_erase_mark(void)
74 for (i = 0; i < LOG_MAX_ERASE; i++) {
76 if (erase.unused == 0 && erase.flight == ao_flight_number)
78 if (erase.unused == 0xff) {
79 ao_log_write_erase(i);
89 return (uint8_t) (ao_storage_config / ao_config.flight_log_max);
93 ao_log_pos(uint8_t slot)
95 return ((slot) * ao_config.flight_log_max);
99 ao_log_max_flight(void)
104 uint16_t max_flight = 0;
106 /* Scan the log space looking for the biggest flight number */
107 log_slots = ao_log_slots();
108 for (log_slot = 0; log_slot < log_slots; log_slot++) {
109 log_flight = ao_log_flight(log_slot);
112 if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0)
113 max_flight = log_flight;
119 ao_log_scan(void) __reentrant
127 ao_flight_number = ao_log_max_flight();
128 if (ao_flight_number)
129 if (++ao_flight_number == 0)
130 ao_flight_number = 1;
132 /* Now look through the log of flight numbers from erase operations and
133 * see if the last one is bigger than what we found above
135 for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
136 ao_log_read_erase(log_slot);
137 if (erase.unused == 0) {
138 if (ao_flight_number == 0 ||
139 (int16_t) (erase.flight - ao_flight_number) > 0)
140 ao_flight_number = erase.flight;
144 if (ao_flight_number == 0)
145 ao_flight_number = 1;
147 /* With a flight number in hand, find a place to write a new log,
148 * use the target flight number to index the available log slots so
149 * that we write logs to each spot about the same number of times.
152 /* Find a log slot for the next flight, if available */
153 ao_log_current_pos = ao_log_end_pos = 0;
154 log_slots = ao_log_slots();
155 log_want = (ao_flight_number - 1) % log_slots;
158 if (ao_log_flight(log_slot) == 0) {
159 ao_log_current_pos = ao_log_pos(log_slot);
160 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
163 if (++log_slot >= log_slots)
165 } while (log_slot != log_want);
167 ao_wakeup(&ao_flight_number);
175 ao_wakeup(&ao_log_running);
188 return ao_log_max_flight() != 0;
194 return ao_log_current_pos == ao_log_end_pos;
197 static __xdata struct ao_task ao_log_task;
200 ao_log_list(void) __reentrant
206 slots = ao_log_slots();
207 for (slot = 0; slot < slots; slot++)
209 flight = ao_log_flight(slot);
211 printf ("flight %d start %x end %x\n",
213 (uint16_t) (ao_log_pos(slot) >> 8),
214 (uint16_t) (ao_log_pos(slot+1) >> 8));
220 ao_log_delete(void) __reentrant
226 if (ao_cmd_status != ao_cmd_success)
229 slots = ao_log_slots();
230 /* Look for the flight log matching the requested flight */
232 for (slot = 0; slot < slots; slot++) {
233 if (ao_log_flight(slot) == ao_cmd_lex_i) {
235 ao_log_current_pos = ao_log_pos(slot);
236 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
237 while (ao_log_current_pos < ao_log_end_pos) {
239 static __xdata uint8_t b;
242 * Check to see if we've reached the end of
243 * the used memory to avoid re-erasing the same
244 * memory over and over again
246 for (i = 0; i < 16; i++) {
247 if (ao_storage_read(ao_log_current_pos + i, &b, 1))
253 ao_storage_erase(ao_log_current_pos);
254 ao_log_current_pos += ao_storage_block;
261 printf("No such flight: %d\n", ao_cmd_lex_i);
264 __code struct ao_cmds ao_log_cmds[] = {
265 { ao_log_list, "l\0List stored flight logs" },
266 { ao_log_delete, "d <flight-number>\0Delete stored flight" },
275 /* For now, just log the flight starting at the begining of eeprom */
276 ao_log_state = ao_flight_invalid;
278 ao_cmd_register(&ao_log_cmds[0]);
280 /* Create a task to log events to eeprom */
281 ao_add_task(&ao_log_task, ao_log, "log");