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