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