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