altos: check for valid flight number in ao_log_delete
[fw/altos] / src / ao.h
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 #ifndef _AO_H_
19 #define _AO_H_
20
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stddef.h>
25 #include "cc1111.h"
26 #include "ao_pins.h"
27
28 #define TRUE 1
29 #define FALSE 0
30
31 /* Convert a __data pointer into an __xdata pointer */
32 #define DATA_TO_XDATA(a)        ((void __xdata *) ((uint8_t) (a) | 0xff00))
33
34 /* Stack runs from above the allocated __data space to 0xfe, which avoids
35  * writing to 0xff as that triggers the stack overflow indicator
36  */
37 #define AO_STACK_START  0x80
38 #define AO_STACK_END    0xfe
39 #define AO_STACK_SIZE   (AO_STACK_END - AO_STACK_START + 1)
40
41 /* An AltOS task */
42 struct ao_task {
43         __xdata void *wchan;            /* current wait channel (NULL if running) */
44         uint16_t alarm;                 /* abort ao_sleep time */
45         uint8_t stack_count;            /* amount of saved stack */
46         uint8_t task_id;                /* unique id */
47         __code char *name;              /* task name */
48         uint8_t stack[AO_STACK_SIZE];   /* saved stack */
49 };
50
51 extern __xdata struct ao_task *__data ao_cur_task;
52
53 #define AO_NUM_TASKS            16      /* maximum number of tasks */
54 #define AO_NO_TASK              0       /* no task id */
55
56 /*
57  ao_task.c
58  */
59
60 /* Suspend the current task until wchan is awoken.
61  * returns:
62  *  0 on normal wake
63  *  1 on alarm
64  */
65 uint8_t
66 ao_sleep(__xdata void *wchan);
67
68 /* Wake all tasks sleeping on wchan */
69 void
70 ao_wakeup(__xdata void *wchan);
71
72 /* set an alarm to go off in 'delay' ticks */
73 void
74 ao_alarm(uint16_t delay);
75
76 /* Yield the processor to another task */
77 void
78 ao_yield(void) __naked;
79
80 /* Add a task to the run queue */
81 void
82 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
83
84 /* Terminate the current task */
85 void
86 ao_exit(void);
87
88 /* Dump task info to console */
89 void
90 ao_task_info(void);
91
92 /* Start the scheduler. This will not return */
93 void
94 ao_start_scheduler(void);
95
96 /*
97  * ao_panic.c
98  */
99
100 #define AO_PANIC_NO_TASK        1       /* AO_NUM_TASKS is not large enough */
101 #define AO_PANIC_DMA            2       /* Attempt to start DMA while active */
102 #define AO_PANIC_MUTEX          3       /* Mis-using mutex API */
103 #define AO_PANIC_EE             4       /* Mis-using eeprom API */
104 #define AO_PANIC_LOG            5       /* Failing to read/write log data */
105 #define AO_PANIC_CMD            6       /* Too many command sets registered */
106 #define AO_PANIC_STDIO          7       /* Too many stdio handlers registered */
107 #define AO_PANIC_REBOOT         8       /* Reboot failed */
108 #define AO_PANIC_FLASH          9       /* Invalid flash part (or wrong blocksize) */
109 #define AO_PANIC_USB            10      /* Trying to send USB packet while busy */
110
111 /* Stop the operating system, beeping and blinking the reason */
112 void
113 ao_panic(uint8_t reason);
114
115 /*
116  * ao_timer.c
117  */
118
119 /* Our timer runs at 100Hz */
120 #define AO_HERTZ                100
121 #define AO_MS_TO_TICKS(ms)      ((ms) / (1000 / AO_HERTZ))
122 #define AO_SEC_TO_TICKS(s)      ((s) * AO_HERTZ)
123
124 /* Returns the current time in ticks */
125 uint16_t
126 ao_time(void);
127
128 /* Suspend the current task until ticks time has passed */
129 void
130 ao_delay(uint16_t ticks);
131
132 /* Set the ADC interval */
133 void
134 ao_timer_set_adc_interval(uint8_t interval) __critical;
135
136 /* Timer interrupt */
137 void
138 ao_timer_isr(void) __interrupt 9;
139
140 /* Initialize the timer */
141 void
142 ao_timer_init(void);
143
144 /* Initialize the hardware clock. Must be called first */
145 void
146 ao_clock_init(void);
147
148 /*
149  * One set of samples read from the A/D converter or telemetry
150  */
151 struct ao_adc {
152         uint16_t        tick;           /* tick when the sample was read */
153         int16_t         accel;          /* accelerometer */
154         int16_t         pres;           /* pressure sensor */
155         int16_t         temp;           /* temperature sensor */
156         int16_t         v_batt;         /* battery voltage */
157         int16_t         sense_d;        /* drogue continuity sense */
158         int16_t         sense_m;        /* main continuity sense */
159 };
160
161 #ifndef HAS_ADC
162 #error Please define HAS_ADC
163 #endif
164
165 #if HAS_ADC
166 /*
167  * ao_adc.c
168  */
169
170 #define AO_ADC_RING     32
171 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
172 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
173
174
175 /*
176  * A/D data is stored in a ring, with the next sample to be written
177  * at ao_adc_head
178  */
179 extern volatile __xdata struct ao_adc   ao_adc_ring[AO_ADC_RING];
180 extern volatile __data uint8_t          ao_adc_head;
181
182 /* Trigger a conversion sequence (called from the timer interrupt) */
183 void
184 ao_adc_poll(void);
185
186 /* Suspend the current task until another A/D sample is converted */
187 void
188 ao_adc_sleep(void);
189
190 /* Get a copy of the last complete A/D sample set */
191 void
192 ao_adc_get(__xdata struct ao_adc *packet);
193
194 /* The A/D interrupt handler */
195
196 void
197 ao_adc_isr(void) __interrupt 1;
198
199 /* Initialize the A/D converter */
200 void
201 ao_adc_init(void);
202
203 #endif /* HAS_ADC */
204
205 /*
206  * ao_beep.c
207  */
208
209 /*
210  * Various pre-defined beep frequencies
211  *
212  * frequency = 1/2 (24e6/32) / beep
213  */
214
215 #define AO_BEEP_LOW     150     /* 2500Hz */
216 #define AO_BEEP_MID     94      /* 3989Hz */
217 #define AO_BEEP_HIGH    75      /* 5000Hz */
218 #define AO_BEEP_OFF     0       /* off */
219
220 #define AO_BEEP_g       240     /* 1562.5Hz */
221 #define AO_BEEP_gs      227     /* 1652Hz (1655Hz) */
222 #define AO_BEEP_aa      214     /* 1752Hz (1754Hz) */
223 #define AO_BEEP_bbf     202     /* 1856Hz (1858Hz) */
224 #define AO_BEEP_bb      190     /* 1974Hz (1969Hz) */
225 #define AO_BEEP_cc      180     /* 2083Hz (2086Hz) */
226 #define AO_BEEP_ccs     170     /* 2205Hz (2210Hz) */
227 #define AO_BEEP_dd      160     /* 2344Hz (2341Hz) */
228 #define AO_BEEP_eef     151     /* 2483Hz (2480Hz) */
229 #define AO_BEEP_ee      143     /* 2622Hz (2628Hz) */
230 #define AO_BEEP_ff      135     /* 2778Hz (2784Hz) */
231 #define AO_BEEP_ffs     127     /* 2953Hz (2950Hz) */
232 #define AO_BEEP_gg      120     /* 3125Hz */
233 #define AO_BEEP_ggs     113     /* 3319Hz (3311Hz) */
234 #define AO_BEEP_aaa     107     /* 3504Hz (3508Hz) */
235 #define AO_BEEP_bbbf    101     /* 3713Hz (3716Hz) */
236 #define AO_BEEP_bbb     95      /* 3947Hz (3937Hz) */
237 #define AO_BEEP_ccc     90      /* 4167Hz (4171Hz) */
238 #define AO_BEEP_cccs    85      /* 4412Hz (4419Hz) */
239 #define AO_BEEP_ddd     80      /* 4688Hz (4682Hz) */
240 #define AO_BEEP_eeef    76      /* 4934Hz (4961Hz) */
241 #define AO_BEEP_eee     71      /* 5282Hz (5256Hz) */
242 #define AO_BEEP_fff     67      /* 5597Hz (5568Hz) */
243 #define AO_BEEP_fffs    64      /* 5859Hz (5899Hz) */
244 #define AO_BEEP_ggg     60      /* 6250Hz */
245
246 /* Set the beeper to the specified tone */
247 void
248 ao_beep(uint8_t beep);
249
250 /* Turn on the beeper for the specified time */
251 void
252 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
253
254 /* Initialize the beeper */
255 void
256 ao_beep_init(void);
257
258 /*
259  * ao_led.c
260  */
261
262 #define AO_LED_NONE     0
263
264 /* Turn on the specified LEDs */
265 void
266 ao_led_on(uint8_t colors);
267
268 /* Turn off the specified LEDs */
269 void
270 ao_led_off(uint8_t colors);
271
272 /* Set all of the LEDs to the specified state */
273 void
274 ao_led_set(uint8_t colors);
275
276 /* Toggle the specified LEDs */
277 void
278 ao_led_toggle(uint8_t colors);
279
280 /* Turn on the specified LEDs for the indicated interval */
281 void
282 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
283
284 /* Initialize the LEDs */
285 void
286 ao_led_init(uint8_t enable);
287
288 /*
289  * ao_romconfig.c
290  */
291
292 #define AO_ROMCONFIG_VERSION    2
293
294 extern __code __at (0x00a0) uint16_t ao_romconfig_version;
295 extern __code __at (0x00a2) uint16_t ao_romconfig_check;
296 extern __code __at (0x00a4) uint16_t ao_serial_number;
297 extern __code __at (0x00a6) uint32_t ao_radio_cal;
298 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
299
300 /*
301  * ao_usb.c
302  */
303
304 /* Put one character to the USB output queue */
305 void
306 ao_usb_putchar(char c);
307
308 /* Get one character from the USB input queue */
309 char
310 ao_usb_getchar(void);
311
312 /* Poll for a charcter on the USB input queue.
313  * returns AO_READ_AGAIN if none are available
314  */
315 char
316 ao_usb_pollchar(void);
317
318 /* Flush the USB output queue */
319 void
320 ao_usb_flush(void);
321
322 /* USB interrupt handler */
323 void
324 ao_usb_isr(void) __interrupt 6;
325
326 /* Enable the USB controller */
327 void
328 ao_usb_enable(void);
329
330 /* Disable the USB controller */
331 void
332 ao_usb_disable(void);
333
334 /* Initialize the USB system */
335 void
336 ao_usb_init(void);
337
338 /*
339  * ao_cmd.c
340  */
341
342 enum ao_cmd_status {
343         ao_cmd_success = 0,
344         ao_cmd_lex_error = 1,
345         ao_cmd_syntax_error = 2,
346 };
347
348 extern __xdata uint16_t ao_cmd_lex_i;
349 extern __xdata uint32_t ao_cmd_lex_u32;
350 extern __xdata char     ao_cmd_lex_c;
351 extern __xdata enum ao_cmd_status ao_cmd_status;
352
353 void
354 ao_cmd_lex(void);
355
356 void
357 ao_cmd_put8(uint8_t v);
358
359 void
360 ao_cmd_put16(uint16_t v);
361
362 void
363 ao_cmd_white(void);
364
365 void
366 ao_cmd_hex(void);
367
368 void
369 ao_cmd_decimal(void);
370
371 uint8_t
372 ao_match_word(__code char *word);
373
374 struct ao_cmds {
375         char            cmd;
376         void            (*func)(void);
377         const char      *help;
378 };
379
380 void
381 ao_cmd_register(__code struct ao_cmds *cmds);
382
383 void
384 ao_cmd_init(void);
385
386 /*
387  * ao_dma.c
388  */
389
390 /* Allocate a DMA channel. the 'done' parameter will be set when the
391  * dma is finished and will be used to wakeup any waiters
392  */
393
394 uint8_t
395 ao_dma_alloc(__xdata uint8_t * done);
396
397 /* Setup a DMA channel */
398 void
399 ao_dma_set_transfer(uint8_t id,
400                     void __xdata *srcaddr,
401                     void __xdata *dstaddr,
402                     uint16_t count,
403                     uint8_t cfg0,
404                     uint8_t cfg1);
405
406 /* Start a DMA channel */
407 void
408 ao_dma_start(uint8_t id);
409
410 /* Manually trigger a DMA channel */
411 void
412 ao_dma_trigger(uint8_t id);
413
414 /* Abort a running DMA transfer */
415 void
416 ao_dma_abort(uint8_t id);
417
418 /* DMA interrupt routine */
419 void
420 ao_dma_isr(void) __interrupt 8;
421
422 /*
423  * ao_mutex.c
424  */
425
426 void
427 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
428
429 void
430 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
431
432 /*
433  * Storage interface, provided by one of the eeprom or flash
434  * drivers
435  */
436
437 /* Total bytes of available storage */
438 extern __xdata uint32_t ao_storage_total;
439
440 /* Block size - device is erased in these units. At least 256 bytes */
441 extern __xdata uint32_t ao_storage_block;
442
443 /* Byte offset of config block. Will be ao_storage_block bytes long */
444 extern __xdata uint32_t ao_storage_config;
445
446 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
447 extern __xdata uint16_t ao_storage_unit;
448
449 #define AO_STORAGE_ERASE_LOG    (ao_storage_config + AO_CONFIG_MAX_SIZE)
450
451 /* Initialize above values. Can only be called once the OS is running */
452 void
453 ao_storage_setup(void);
454
455 /* Write data. Returns 0 on failure, 1 on success */
456 uint8_t
457 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
458
459 /* Read data. Returns 0 on failure, 1 on success */
460 uint8_t
461 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
462
463 /* Erase a block of storage. This always clears ao_storage_block bytes */
464 uint8_t
465 ao_storage_erase(uint32_t pos) __reentrant;
466
467 /* Flush any pending writes to stable storage */
468 void
469 ao_storage_flush(void) __reentrant;
470
471 /* Initialize the storage code */
472 void
473 ao_storage_init(void);
474
475 /*
476  * Low-level functions wrapped by ao_storage.c
477  */
478
479 /* Read data within a storage unit */
480 uint8_t
481 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
482
483 /* Write data within a storage unit */
484 uint8_t
485 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
486
487 /* Initialize low-level device bits */
488 void
489 ao_storage_device_init(void);
490
491 /* Print out information about flash chips */
492 void
493 ao_storage_device_info(void) __reentrant;
494
495 /*
496  * ao_log.c
497  */
498
499 /*
500  * The data log is recorded in the eeprom as a sequence
501  * of data packets.
502  *
503  * Each packet starts with a 4-byte header that has the
504  * packet type, the packet checksum and the tick count. Then
505  * they all contain 2 16 bit values which hold packet-specific
506  * data.
507  *
508  * For each flight, the first packet
509  * is FLIGHT packet, indicating the serial number of the
510  * device and a unique number marking the number of flights
511  * recorded by this device.
512  *
513  * During flight, data from the accelerometer and barometer
514  * are recorded in SENSOR packets, using the raw 16-bit values
515  * read from the A/D converter.
516  *
517  * Also during flight, but at a lower rate, the deployment
518  * sensors are recorded in DEPLOY packets. The goal here is to
519  * detect failure in the deployment circuits.
520  *
521  * STATE packets hold state transitions as the flight computer
522  * transitions through different stages of the flight.
523  */
524 #define AO_LOG_FLIGHT           'F'
525 #define AO_LOG_SENSOR           'A'
526 #define AO_LOG_TEMP_VOLT        'T'
527 #define AO_LOG_DEPLOY           'D'
528 #define AO_LOG_STATE            'S'
529 #define AO_LOG_GPS_TIME         'G'
530 #define AO_LOG_GPS_LAT          'N'
531 #define AO_LOG_GPS_LON          'W'
532 #define AO_LOG_GPS_ALT          'H'
533 #define AO_LOG_GPS_SAT          'V'
534 #define AO_LOG_GPS_DATE         'Y'
535
536 #define AO_LOG_POS_NONE         (~0UL)
537
538 struct ao_log_record {
539         char                    type;
540         uint8_t                 csum;
541         uint16_t                tick;
542         union {
543                 struct {
544                         int16_t         ground_accel;
545                         uint16_t        flight;
546                 } flight;
547                 struct {
548                         int16_t         accel;
549                         int16_t         pres;
550                 } sensor;
551                 struct {
552                         int16_t         temp;
553                         int16_t         v_batt;
554                 } temp_volt;
555                 struct {
556                         int16_t         drogue;
557                         int16_t         main;
558                 } deploy;
559                 struct {
560                         uint16_t        state;
561                         uint16_t        reason;
562                 } state;
563                 struct {
564                         uint8_t         hour;
565                         uint8_t         minute;
566                         uint8_t         second;
567                         uint8_t         flags;
568                 } gps_time;
569                 int32_t         gps_latitude;
570                 int32_t         gps_longitude;
571                 struct {
572                         int16_t         altitude;
573                         uint16_t        unused;
574                 } gps_altitude;
575                 struct {
576                         uint16_t        svid;
577                         uint8_t         unused;
578                         uint8_t         c_n;
579                 } gps_sat;
580                 struct {
581                         uint8_t         year;
582                         uint8_t         month;
583                         uint8_t         day;
584                         uint8_t         extra;
585                 } gps_date;
586                 struct {
587                         uint16_t        d0;
588                         uint16_t        d1;
589                 } anon;
590         } u;
591 };
592
593 /* Write a record to the eeprom log */
594 uint8_t
595 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
596
597 /* Flush the log */
598 void
599 ao_log_flush(void);
600
601 /* We record flight numbers in the first record of
602  * the log. Tasks may wait for this to be initialized
603  * by sleeping on this variable.
604  */
605 extern __xdata uint16_t ao_flight_number;
606
607 /* Logging thread main routine */
608 void
609 ao_log(void);
610
611 /* Start logging to eeprom */
612 void
613 ao_log_start(void);
614
615 /* Stop logging */
616 void
617 ao_log_stop(void);
618
619 /* Initialize the logging system */
620 void
621 ao_log_init(void);
622
623 /* Write out the current flight number to the erase log */
624 void
625 ao_log_write_erase(uint8_t pos);
626
627 /* Returns true if there are any logs stored in eeprom */
628 uint8_t
629 ao_log_present(void);
630
631 /* Returns true if there is no more storage space available */
632 uint8_t
633 ao_log_full(void);
634
635 /*
636  * ao_flight.c
637  */
638
639 enum ao_flight_state {
640         ao_flight_startup = 0,
641         ao_flight_idle = 1,
642         ao_flight_pad = 2,
643         ao_flight_boost = 3,
644         ao_flight_fast = 4,
645         ao_flight_coast = 5,
646         ao_flight_drogue = 6,
647         ao_flight_main = 7,
648         ao_flight_landed = 8,
649         ao_flight_invalid = 9
650 };
651
652 extern __xdata struct ao_adc            ao_flight_data;
653 extern __pdata enum ao_flight_state     ao_flight_state;
654 extern __pdata uint16_t                 ao_flight_tick;
655 extern __pdata int16_t                  ao_flight_accel;
656 extern __pdata int16_t                  ao_flight_pres;
657 extern __pdata int32_t                  ao_flight_vel;
658 extern __pdata int16_t                  ao_ground_pres;
659 extern __pdata int16_t                  ao_ground_accel;
660 extern __pdata int16_t                  ao_min_pres;
661 extern __pdata uint16_t                 ao_launch_time;
662 extern __xdata uint8_t                  ao_flight_force_idle;
663
664 /* Flight thread */
665 void
666 ao_flight(void);
667
668 /* Initialize flight thread */
669 void
670 ao_flight_init(void);
671
672 /*
673  * ao_report.c
674  */
675
676 void
677 ao_report_init(void);
678
679 /*
680  * ao_convert.c
681  *
682  * Given raw data, convert to SI units
683  */
684
685 /* pressure from the sensor to altitude in meters */
686 int16_t
687 ao_pres_to_altitude(int16_t pres) __reentrant;
688
689 int16_t
690 ao_altitude_to_pres(int16_t alt) __reentrant;
691
692 int16_t
693 ao_temp_to_dC(int16_t temp) __reentrant;
694
695 /*
696  * ao_dbg.c
697  *
698  * debug another telemetrum board
699  */
700
701 /* Send a byte to the dbg target */
702 void
703 ao_dbg_send_byte(uint8_t byte);
704
705 /* Receive a byte from the dbg target */
706 uint8_t
707 ao_dbg_recv_byte(void);
708
709 /* Start a bulk transfer to/from dbg target memory */
710 void
711 ao_dbg_start_transfer(uint16_t addr);
712
713 /* End a bulk transfer to/from dbg target memory */
714 void
715 ao_dbg_end_transfer(void);
716
717 /* Write a byte to dbg target memory */
718 void
719 ao_dbg_write_byte(uint8_t byte);
720
721 /* Read a byte from dbg target memory */
722 uint8_t
723 ao_dbg_read_byte(void);
724
725 /* Enable dbg mode, switching use of the pins */
726 void
727 ao_dbg_debug_mode(void);
728
729 /* Reset the dbg target */
730 void
731 ao_dbg_reset(void);
732
733 void
734 ao_dbg_init(void);
735
736 /*
737  * ao_serial.c
738  */
739
740 #ifndef HAS_SERIAL_1
741 #error Please define HAS_SERIAL_1
742 #endif
743
744 #if HAS_SERIAL_1
745 void
746 ao_serial_rx1_isr(void) __interrupt 3;
747
748 void
749 ao_serial_tx1_isr(void) __interrupt 14;
750
751 char
752 ao_serial_getchar(void) __critical;
753
754 void
755 ao_serial_putchar(char c) __critical;
756
757 #define AO_SERIAL_SPEED_4800    0
758 #define AO_SERIAL_SPEED_9600    1
759 #define AO_SERIAL_SPEED_57600   2
760
761 void
762 ao_serial_set_speed(uint8_t speed);
763
764 void
765 ao_serial_init(void);
766 #endif
767
768 /*
769  * ao_spi.c
770  */
771
772 void
773 ao_spi_send(void __xdata *block, uint16_t len) __reentrant;
774
775 void
776 ao_spi_recv(void __xdata *block, uint16_t len) __reentrant;
777
778 void
779 ao_spi_init(void);
780
781 /*
782  * ao_gps.c
783  */
784
785 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
786 #define AO_GPS_NUM_SAT_SHIFT    (0)
787
788 #define AO_GPS_VALID            (1 << 4)
789 #define AO_GPS_RUNNING          (1 << 5)
790 #define AO_GPS_DATE_VALID       (1 << 6)
791
792 extern __xdata uint16_t ao_gps_tick;
793
794 struct ao_gps_data {
795         uint8_t                 year;
796         uint8_t                 month;
797         uint8_t                 day;
798         uint8_t                 hour;
799         uint8_t                 minute;
800         uint8_t                 second;
801         uint8_t                 flags;
802         int32_t                 latitude;       /* degrees * 10⁷ */
803         int32_t                 longitude;      /* degrees * 10⁷ */
804         int16_t                 altitude;       /* m */
805         uint16_t                ground_speed;   /* cm/s */
806         uint8_t                 course;         /* degrees / 2 */
807         uint8_t                 hdop;           /* * 5 */
808         int16_t                 climb_rate;     /* cm/s */
809         uint16_t                h_error;        /* m */
810         uint16_t                v_error;        /* m */
811 };
812
813 struct ao_gps_sat_data {
814         uint8_t         svid;
815         uint8_t         c_n_1;
816 };
817
818 #define AO_MAX_GPS_TRACKING     12
819
820 struct ao_gps_tracking_data {
821         uint8_t                 channels;
822         struct ao_gps_sat_data  sats[AO_MAX_GPS_TRACKING];
823 };
824
825 extern __xdata uint8_t ao_gps_mutex;
826 extern __xdata struct ao_gps_data ao_gps_data;
827 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
828
829 void
830 ao_gps(void);
831
832 void
833 ao_gps_print(__xdata struct ao_gps_data *gps_data);
834
835 void
836 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
837
838 void
839 ao_gps_init(void);
840
841 /*
842  * ao_gps_report.c
843  */
844
845 void
846 ao_gps_report(void);
847
848 void
849 ao_gps_report_init(void);
850
851 /*
852  * ao_telemetry.c
853  */
854
855 #define AO_MAX_CALLSIGN         8
856 #define AO_TELEMETRY_VERSION    3
857
858 struct ao_telemetry {
859         uint8_t                 addr;
860         uint16_t                flight;
861         uint8_t                 flight_state;
862         int16_t                 flight_accel;
863         int16_t                 ground_accel;
864         int32_t                 flight_vel;
865         int16_t                 flight_pres;
866         int16_t                 ground_pres;
867         int16_t                 accel_plus_g;
868         int16_t                 accel_minus_g;
869         struct ao_adc           adc;
870         struct ao_gps_data      gps;
871         char                    callsign[AO_MAX_CALLSIGN];
872         struct ao_gps_tracking_data     gps_tracking;
873 };
874
875 /*
876  * ao_radio_recv tacks on rssi and status bytes
877  */
878 struct ao_telemetry_recv {
879         struct ao_telemetry     telemetry;
880         int8_t                  rssi;
881         uint8_t                 status;
882 };
883
884 /* Set delay between telemetry reports (0 to disable) */
885
886 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(1000)
887 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(50)
888 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(1000)
889
890 void
891 ao_telemetry_set_interval(uint16_t interval);
892
893 void
894 ao_rdf_set(uint8_t rdf);
895
896 void
897 ao_telemetry_init(void);
898
899 /*
900  * ao_radio.c
901  */
902
903 extern __xdata uint8_t  ao_radio_dma;
904 extern __xdata uint8_t ao_radio_dma_done;
905 extern __xdata uint8_t ao_radio_done;
906 extern __xdata uint8_t ao_radio_mutex;
907
908 void
909 ao_radio_general_isr(void) __interrupt 16;
910
911 void
912 ao_radio_get(void);
913
914 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
915
916 void
917 ao_radio_set_telemetry(void);
918
919 void
920 ao_radio_set_packet(void);
921
922 void
923 ao_radio_set_rdf(void);
924
925 void
926 ao_radio_send(__xdata void *data, uint8_t size) __reentrant;
927
928 uint8_t
929 ao_radio_recv(__xdata void *data, uint8_t size) __reentrant;
930
931 void
932 ao_radio_recv_abort(void);
933
934 void
935 ao_radio_rdf(int ms);
936
937 void
938 ao_radio_rdf_abort(void);
939
940 void
941 ao_radio_idle(void);
942
943 void
944 ao_radio_init(void);
945
946 /*
947  * ao_monitor.c
948  */
949
950 extern const char const * const ao_state_names[];
951
952 void
953 ao_monitor(void);
954
955 void
956 ao_set_monitor(uint8_t monitoring);
957
958 void
959 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
960
961 /*
962  * ao_stdio.c
963  */
964
965 #define AO_READ_AGAIN   ((char) -1)
966
967 struct ao_stdio {
968         char    (*pollchar)(void);
969         void    (*putchar)(char c) __reentrant;
970         void    (*flush)(void);
971 };
972
973 void
974 flush(void);
975
976 extern __xdata uint8_t ao_stdin_ready;
977
978 void
979 ao_add_stdio(char (*pollchar)(void),
980              void (*putchar)(char) __reentrant,
981              void (*flush)(void));
982
983 /*
984  * ao_ignite.c
985  */
986
987 enum ao_igniter {
988         ao_igniter_drogue = 0,
989         ao_igniter_main = 1
990 };
991
992 void
993 ao_ignite(enum ao_igniter igniter);
994
995 enum ao_igniter_status {
996         ao_igniter_unknown,     /* unknown status (ambiguous voltage) */
997         ao_igniter_ready,       /* continuity detected */
998         ao_igniter_active,      /* igniter firing */
999         ao_igniter_open,        /* open circuit detected */
1000 };
1001
1002 enum ao_igniter_status
1003 ao_igniter_status(enum ao_igniter igniter);
1004
1005 void
1006 ao_igniter_init(void);
1007
1008 /*
1009  * ao_config.c
1010  */
1011
1012 #define AO_CONFIG_MAJOR 1
1013 #define AO_CONFIG_MINOR 4
1014
1015 struct ao_config {
1016         uint8_t         major;
1017         uint8_t         minor;
1018         uint16_t        main_deploy;
1019         int16_t         accel_plus_g;           /* changed for minor version 2 */
1020         uint8_t         radio_channel;
1021         char            callsign[AO_MAX_CALLSIGN + 1];
1022         uint8_t         apogee_delay;           /* minor version 1 */
1023         int16_t         accel_minus_g;          /* minor version 2 */
1024         uint32_t        radio_cal;              /* minor version 3 */
1025         uint32_t        flight_log_max;         /* minor version 4 */
1026 };
1027
1028 extern __xdata struct ao_config ao_config;
1029
1030 #define AO_CONFIG_MAX_SIZE      128
1031
1032 void
1033 ao_config_get(void);
1034
1035 void
1036 ao_config_put(void);
1037
1038 void
1039 ao_config_init(void);
1040
1041 /*
1042  * ao_rssi.c
1043  */
1044
1045 void
1046 ao_rssi_set(int rssi_value);
1047
1048 void
1049 ao_rssi_init(uint8_t rssi_led);
1050
1051 /*
1052  * ao_product.c
1053  *
1054  * values which need to be defined for
1055  * each instance of a product
1056  */
1057
1058 extern __code __at(0x00aa) uint8_t ao_usb_descriptors [];
1059 extern const char ao_version[];
1060 extern const char ao_manufacturer[];
1061 extern const char ao_product[];
1062
1063 /*
1064  * Fifos
1065  */
1066
1067 #define AO_FIFO_SIZE    32
1068
1069 struct ao_fifo {
1070         uint8_t insert;
1071         uint8_t remove;
1072         char    fifo[AO_FIFO_SIZE];
1073 };
1074
1075 #define ao_fifo_insert(f,c) do { \
1076         (f).fifo[(f).insert] = (c); \
1077         (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1078 } while(0)
1079
1080 #define ao_fifo_remove(f,c) do {\
1081         c = (f).fifo[(f).remove]; \
1082         (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1083 } while(0)
1084
1085 #define ao_fifo_full(f)         ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1086 #define ao_fifo_empty(f)        ((f).insert == (f).remove)
1087
1088 /*
1089  * ao_packet.c
1090  *
1091  * Packet-based command interface
1092  */
1093
1094 #define AO_PACKET_MAX           64
1095 #define AO_PACKET_SYN           (uint8_t) 0xff
1096
1097 struct ao_packet {
1098         uint8_t         addr;
1099         uint8_t         len;
1100         uint8_t         seq;
1101         uint8_t         ack;
1102         uint8_t         d[AO_PACKET_MAX];
1103         uint8_t         callsign[AO_MAX_CALLSIGN];
1104 };
1105
1106 struct ao_packet_recv {
1107         struct ao_packet        packet;
1108         int8_t                  rssi;
1109         uint8_t                 status;
1110 };
1111
1112 extern __xdata struct ao_packet_recv ao_rx_packet;
1113 extern __xdata struct ao_packet ao_tx_packet;
1114 extern __xdata struct ao_task   ao_packet_task;
1115 extern __xdata uint8_t ao_packet_enable;
1116 extern __xdata uint8_t ao_packet_master_sleeping;
1117 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1118
1119 void
1120 ao_packet_send(void);
1121
1122 uint8_t
1123 ao_packet_recv(void);
1124
1125 void
1126 ao_packet_flush(void);
1127
1128 void
1129 ao_packet_putchar(char c) __reentrant;
1130
1131 char
1132 ao_packet_pollchar(void) __critical;
1133
1134 /* ao_packet_master.c */
1135
1136 void
1137 ao_packet_master_init(void);
1138
1139 /* ao_packet_slave.c */
1140
1141 void
1142 ao_packet_slave_start(void);
1143
1144 void
1145 ao_packet_slave_stop(void);
1146
1147 void
1148 ao_packet_slave_init(void);
1149
1150 #endif /* _AO_H_ */