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