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 __pdata uint32_t ao_log_current_pos;
23 __pdata uint32_t ao_log_end_pos;
24 __pdata uint32_t ao_log_start_pos;
25 __xdata uint8_t ao_log_running;
26 __pdata enum ao_flight_state ao_log_state;
27 __xdata uint16_t ao_flight_number;
36 * When erasing a flight log, make sure the config block
37 * has an up-to-date version of the current flight number
45 static __xdata struct ao_log_erase erase;
47 #define LOG_MAX_ERASE 16
50 ao_log_erase_pos(uint8_t i)
52 return i * sizeof (struct ao_log_erase) + AO_CONFIG_MAX_SIZE;
56 ao_log_write_erase(uint8_t pos)
59 erase.flight = ao_flight_number;
60 ao_config_write(ao_log_erase_pos(pos), &erase, sizeof (erase));
65 ao_log_read_erase(uint8_t pos)
67 ao_config_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
72 ao_log_erase_mark(void)
76 for (i = 0; i < LOG_MAX_ERASE; i++) {
78 if (erase.unused == 0 && erase.flight == ao_flight_number)
80 if (erase.unused == 0xff) {
81 ao_log_write_erase(i);
91 return (uint8_t) (ao_storage_log_max / ao_config.flight_log_max);
95 ao_log_pos(uint8_t slot)
97 return ((slot) * ao_config.flight_log_max);
101 ao_log_max_flight(void)
106 uint16_t max_flight = 0;
108 /* Scan the log space looking for the biggest flight number */
109 log_slots = ao_log_slots();
110 for (log_slot = 0; log_slot < log_slots; log_slot++) {
111 log_flight = ao_log_flight(log_slot);
114 if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0)
115 max_flight = log_flight;
121 ao_log_scan(void) __reentrant
129 ao_flight_number = ao_log_max_flight();
130 if (ao_flight_number)
131 if (++ao_flight_number == 0)
132 ao_flight_number = 1;
134 /* Now look through the log of flight numbers from erase operations and
135 * see if the last one is bigger than what we found above
137 for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
138 ao_log_read_erase(log_slot);
139 if (erase.unused == 0) {
140 if (ao_flight_number == 0 ||
141 (int16_t) (erase.flight - ao_flight_number) > 0)
142 ao_flight_number = erase.flight;
146 if (ao_flight_number == 0)
147 ao_flight_number = 1;
149 /* With a flight number in hand, find a place to write a new log,
150 * use the target flight number to index the available log slots so
151 * that we write logs to each spot about the same number of times.
154 /* Find a log slot for the next flight, if available */
155 ao_log_current_pos = ao_log_end_pos = 0;
156 log_slots = ao_log_slots();
157 log_want = (ao_flight_number - 1) % log_slots;
160 if (ao_log_flight(log_slot) == 0) {
161 ao_log_current_pos = ao_log_pos(log_slot);
162 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
165 if (++log_slot >= log_slots)
167 } while (log_slot != log_want);
169 ao_wakeup(&ao_flight_number);
177 ao_wakeup(&ao_log_running);
190 return ao_log_max_flight() != 0;
196 return ao_log_current_pos == ao_log_end_pos;
200 static __xdata struct ao_task ao_log_task;
204 ao_log_list(void) __reentrant
210 slots = ao_log_slots();
211 for (slot = 0; slot < slots; slot++)
213 flight = ao_log_flight(slot);
215 printf ("flight %d start %x end %x\n",
217 (uint16_t) (ao_log_pos(slot) >> 8),
218 (uint16_t) (ao_log_pos(slot+1) >> 8));
224 ao_log_delete(void) __reentrant
230 if (ao_cmd_status != ao_cmd_success)
233 slots = ao_log_slots();
234 /* Look for the flight log matching the requested flight */
236 for (slot = 0; slot < slots; slot++) {
237 if (ao_log_flight(slot) == ao_cmd_lex_i) {
239 ao_log_current_pos = ao_log_pos(slot);
240 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
241 while (ao_log_current_pos < ao_log_end_pos) {
243 static __xdata uint8_t b;
246 * Check to see if we've reached the end of
247 * the used memory to avoid re-erasing the same
248 * memory over and over again
250 for (i = 0; i < 16; i++) {
251 if (ao_storage_read(ao_log_current_pos + i, &b, 1))
257 ao_storage_erase(ao_log_current_pos);
258 ao_log_current_pos += ao_storage_block;
265 printf("No such flight: %d\n", ao_cmd_lex_i);
268 __code struct ao_cmds ao_log_cmds[] = {
269 { ao_log_list, "l\0List logs" },
270 { ao_log_delete, "d <flight-number>\0Delete flight" },
279 /* For now, just log the flight starting at the begining of eeprom */
280 ao_log_state = ao_flight_invalid;
282 ao_cmd_register(&ao_log_cmds[0]);
285 #error Define HAS_ADC for ao_log.c
288 /* Create a task to log events to eeprom */
289 ao_add_task(&ao_log_task, ao_log, "log");