altos: Define ao_log_mutex in ao_log.c rather than every log product
[fw/altos] / src / kernel / ao_log.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19 #include <ao_log.h>
20 #include <ao_config.h>
21
22 __xdata uint8_t ao_log_mutex;
23 __pdata uint32_t ao_log_current_pos;
24 __pdata uint32_t ao_log_end_pos;
25 __pdata uint32_t ao_log_start_pos;
26 __xdata uint8_t ao_log_running;
27 __pdata enum ao_flight_state ao_log_state;
28 __xdata uint16_t ao_flight_number;
29
30 void
31 ao_log_flush(void)
32 {
33         ao_storage_flush();
34 }
35
36 /*
37  * When erasing a flight log, make sure the config block
38  * has an up-to-date version of the current flight number
39  */
40
41 struct ao_log_erase {
42         uint8_t mark;
43         uint16_t flight;
44 };
45
46 static __xdata struct ao_log_erase erase;
47
48 #ifndef LOG_MAX_ERASE
49 #define LOG_MAX_ERASE   16
50 #endif
51
52 #ifndef LOG_ERASE_MARK
53 #if USE_EEPROM_CONFIG
54 #error "Must define LOG_ERASE_MARK with USE_EEPROM_CONFIG"
55 #endif
56 #define LOG_ERASE_MARK  0x00
57 #endif
58
59 static uint32_t
60 ao_log_erase_pos(uint8_t i)
61 {
62         return i * sizeof (struct ao_log_erase) + AO_CONFIG_MAX_SIZE;
63 }
64
65 void
66 ao_log_write_erase(uint8_t pos)
67 {
68         erase.mark = LOG_ERASE_MARK;
69         erase.flight = ao_flight_number;
70         ao_config_write(ao_log_erase_pos(pos),  &erase, sizeof (erase));
71
72 #if USE_EEPROM_CONFIG
73         if (pos == 0) {
74                 uint8_t i;
75                 for (i = 1; i < LOG_MAX_ERASE; i++) {
76                         erase.mark = ~LOG_ERASE_MARK;
77                         erase.flight = 0;
78                         ao_config_write(ao_log_erase_pos(i), &erase, sizeof (erase));
79                 }
80         }
81 #endif
82
83         ao_config_flush();
84 }
85
86 static void
87 ao_log_read_erase(uint8_t pos)
88 {
89         ao_config_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
90 }
91
92
93 static void
94 ao_log_erase_mark(void)
95 {
96         uint8_t                         i;
97
98         for (i = 0; i < LOG_MAX_ERASE; i++) {
99                 ao_log_read_erase(i);
100                 if (erase.mark == LOG_ERASE_MARK && erase.flight == ao_flight_number)
101                         return;
102                 if (erase.mark != LOG_ERASE_MARK) {
103                         ao_log_write_erase(i);
104                         return;
105                 }
106         }
107         ao_config_put();
108 }
109
110 static uint8_t
111 ao_log_slots()
112 {
113         return (uint8_t) (ao_storage_log_max / ao_config.flight_log_max);
114 }
115
116 uint32_t
117 ao_log_pos(uint8_t slot)
118 {
119         return ((slot) * ao_config.flight_log_max);
120 }
121
122 static uint16_t
123 ao_log_max_flight(void)
124 {
125         uint8_t         log_slot;
126         uint8_t         log_slots;
127         uint16_t        log_flight;
128         uint16_t        max_flight = 0;
129
130         /* Scan the log space looking for the biggest flight number */
131         log_slots = ao_log_slots();
132         for (log_slot = 0; log_slot < log_slots; log_slot++) {
133                 log_flight = ao_log_flight(log_slot);
134                 if (!log_flight)
135                         continue;
136                 if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0)
137                         max_flight = log_flight;
138         }
139         return max_flight;
140 }
141
142 void
143 ao_log_scan(void) __reentrant
144 {
145         uint8_t         log_slot;
146         uint8_t         log_slots;
147         uint8_t         log_want;
148
149         ao_config_get();
150
151         ao_flight_number = ao_log_max_flight();
152         if (ao_flight_number)
153                 if (++ao_flight_number == 0)
154                         ao_flight_number = 1;
155
156         /* Now look through the log of flight numbers from erase operations and
157          * see if the last one is bigger than what we found above
158          */
159         for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
160                 ao_log_read_erase(log_slot);
161                 if (erase.mark == LOG_ERASE_MARK) {
162                         if (ao_flight_number == 0 ||
163                             (int16_t) (erase.flight - ao_flight_number) > 0)
164                                 ao_flight_number = erase.flight;
165                         break;
166                 }
167         }
168         if (ao_flight_number == 0)
169                 ao_flight_number = 1;
170
171         /* With a flight number in hand, find a place to write a new log,
172          * use the target flight number to index the available log slots so
173          * that we write logs to each spot about the same number of times.
174          */
175
176         /* Find a log slot for the next flight, if available */
177         ao_log_current_pos = ao_log_end_pos = 0;
178         log_slots = ao_log_slots();
179         log_want = (ao_flight_number - 1) % log_slots;
180         log_slot = log_want;
181         do {
182                 if (ao_log_flight(log_slot) == 0) {
183                         ao_log_current_pos = ao_log_pos(log_slot);
184                         ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
185                         break;
186                 }
187                 if (++log_slot >= log_slots)
188                         log_slot = 0;
189         } while (log_slot != log_want);
190
191         ao_wakeup(&ao_flight_number);
192 }
193
194 void
195 ao_log_start(void)
196 {
197         /* start logging */
198         ao_log_running = 1;
199         ao_wakeup(&ao_log_running);
200 }
201
202 void
203 ao_log_stop(void)
204 {
205         ao_log_running = 0;
206         ao_log_flush();
207 }
208
209 uint8_t
210 ao_log_present(void)
211 {
212         return ao_log_max_flight() != 0;
213 }
214
215 uint8_t
216 ao_log_full(void)
217 {
218         return ao_log_current_pos == ao_log_end_pos;
219 }
220
221 #ifndef LOG_ADC
222 #define LOG_ADC HAS_ADC
223 #endif
224
225 #if LOG_ADC
226 static __xdata struct ao_task ao_log_task;
227 #endif
228
229 void
230 ao_log_list(void) __reentrant
231 {
232         uint8_t slot;
233         uint8_t slots;
234         uint16_t flight;
235
236         slots = ao_log_slots();
237         for (slot = 0; slot < slots; slot++)
238         {
239                 flight = ao_log_flight(slot);
240                 if (flight)
241                         printf ("flight %d start %x end %x\n",
242                                 flight,
243                                 (uint16_t) (ao_log_pos(slot) >> 8),
244                                 (uint16_t) (ao_log_pos(slot+1) >> 8));
245         }
246         printf ("done\n");
247 }
248
249 void
250 ao_log_delete(void) __reentrant
251 {
252         uint8_t slot;
253         uint8_t slots;
254
255         ao_cmd_decimal();
256         if (ao_cmd_status != ao_cmd_success)
257                 return;
258
259         slots = ao_log_slots();
260         /* Look for the flight log matching the requested flight */
261         if (ao_cmd_lex_i) {
262                 for (slot = 0; slot < slots; slot++) {
263                         if (ao_log_flight(slot) == ao_cmd_lex_i) {
264                                 ao_log_erase_mark();
265                                 ao_log_current_pos = ao_log_pos(slot);
266                                 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
267                                 while (ao_log_current_pos < ao_log_end_pos) {
268                                         uint8_t i;
269                                         static __xdata uint8_t b;
270
271                                         /*
272                                          * Check to see if we've reached the end of
273                                          * the used memory to avoid re-erasing the same
274                                          * memory over and over again
275                                          */
276                                         for (i = 0; i < 16; i++) {
277                                                 if (ao_storage_read(ao_log_current_pos + i, &b, 1))
278                                                         if (b != 0xff)
279                                                                 break;
280                                         }
281                                         if (i == 16)
282                                                 break;
283                                         ao_storage_erase(ao_log_current_pos);
284                                         ao_log_current_pos += ao_storage_block;
285                                 }
286                                 puts("Erased");
287                                 return;
288                         }
289                 }
290         }
291         printf("No such flight: %d\n", ao_cmd_lex_i);
292 }
293
294 __code struct ao_cmds ao_log_cmds[] = {
295         { ao_log_list,  "l\0List logs" },
296         { ao_log_delete,        "d <flight-number>\0Delete flight" },
297         { 0,    NULL },
298 };
299
300 void
301 ao_log_init(void)
302 {
303         ao_log_running = 0;
304
305         /* For now, just log the flight starting at the begining of eeprom */
306         ao_log_state = ao_flight_invalid;
307
308         ao_cmd_register(&ao_log_cmds[0]);
309
310 #ifndef HAS_ADC
311 #error Define HAS_ADC for ao_log.c
312 #endif
313 #if LOG_ADC
314         /* Create a task to log events to eeprom */
315         ao_add_task(&ao_log_task, ao_log, "log");
316 #endif
317 }