132711091de385a36a0abe562cb570fbf883a5b6
[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     64
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 to 1
353  * when the dma is finished and will be used to wakeup any waiters
354  */
355 uint8_t
356 ao_dma_alloc(__xdata uint8_t * done);
357
358 /* Setup a DMA channel */
359 void
360 ao_dma_set_transfer(uint8_t id,
361                     void __xdata *srcaddr,
362                     void __xdata *dstaddr,
363                     uint16_t count,
364                     uint8_t cfg0,
365                     uint8_t cfg1);
366
367 /* Start a DMA channel */
368 void
369 ao_dma_start(uint8_t id);
370
371 /* Manually trigger a DMA channel */
372 void
373 ao_dma_trigger(uint8_t id);
374
375 /* Abort a running DMA transfer */
376 void
377 ao_dma_abort(uint8_t id);
378
379 /* DMA interrupt routine */
380 void
381 ao_dma_isr(void) interrupt 8;
382
383 /*
384  * ao_mutex.c
385  */
386
387 void
388 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
389
390 void
391 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
392
393 /*
394  * ao_ee.c
395  */
396
397 /*
398  * We reserve the last block on the device for
399  * configuration space. Writes and reads in this
400  * area return errors.
401  */
402
403 #define AO_EE_BLOCK_SIZE        ((uint16_t) (256))
404 #define AO_EE_DEVICE_SIZE       ((uint32_t) 128 * (uint32_t) 1024)
405 #define AO_EE_DATA_SIZE         (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
406 #define AO_EE_CONFIG_BLOCK      ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
407
408 void
409 ao_ee_flush(void) __reentrant;
410
411 /* Write to the eeprom */
412 uint8_t
413 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
414
415 /* Read from the eeprom */
416 uint8_t
417 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
418
419 /* Write the config block (at the end of the eeprom) */
420 uint8_t
421 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
422
423 /* Read the config block (at the end of the eeprom) */
424 uint8_t
425 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
426
427 /* Initialize the EEPROM code */
428 void
429 ao_ee_init(void);
430
431 /*
432  * ao_log.c
433  */
434
435 /*
436  * The data log is recorded in the eeprom as a sequence
437  * of data packets.
438  *
439  * Each packet starts with a 4-byte header that has the
440  * packet type, the packet checksum and the tick count. Then
441  * they all contain 2 16 bit values which hold packet-specific
442  * data.
443  *
444  * For each flight, the first packet
445  * is FLIGHT packet, indicating the serial number of the
446  * device and a unique number marking the number of flights
447  * recorded by this device.
448  *
449  * During flight, data from the accelerometer and barometer
450  * are recorded in SENSOR packets, using the raw 16-bit values
451  * read from the A/D converter.
452  *
453  * Also during flight, but at a lower rate, the deployment
454  * sensors are recorded in DEPLOY packets. The goal here is to
455  * detect failure in the deployment circuits.
456  *
457  * STATE packets hold state transitions as the flight computer
458  * transitions through different stages of the flight.
459  */
460 #define AO_LOG_FLIGHT           'F'
461 #define AO_LOG_SENSOR           'A'
462 #define AO_LOG_TEMP_VOLT        'T'
463 #define AO_LOG_DEPLOY           'D'
464 #define AO_LOG_STATE            'S'
465 #define AO_LOG_GPS_TIME         'G'
466 #define AO_LOG_GPS_LAT          'N'
467 #define AO_LOG_GPS_LON          'W'
468 #define AO_LOG_GPS_ALT          'H'
469 #define AO_LOG_GPS_SAT          'V'
470
471 #define AO_LOG_POS_NONE         (~0UL)
472
473 struct ao_log_record {
474         char                    type;
475         uint8_t                 csum;
476         uint16_t                tick;
477         union {
478                 struct {
479                         int16_t         ground_accel;
480                         uint16_t        flight;
481                 } flight;
482                 struct {
483                         int16_t         accel;
484                         int16_t         pres;
485                 } sensor;
486                 struct {
487                         int16_t         temp;
488                         int16_t         v_batt;
489                 } temp_volt;
490                 struct {
491                         int16_t         drogue;
492                         int16_t         main;
493                 } deploy;
494                 struct {
495                         uint16_t        state;
496                         uint16_t        reason;
497                 } state;
498                 struct {
499                         uint8_t         hour;
500                         uint8_t         minute;
501                         uint8_t         second;
502                         uint8_t         flags;
503                 } gps_time;
504                 int32_t         gps_latitude;
505                 int32_t         gps_longitude;
506                 struct {
507                         int16_t         altitude;
508                         uint16_t        unused;
509                 } gps_altitude;
510                 struct {
511                         uint16_t        svid;
512                         uint8_t         state;
513                         uint8_t         c_n;
514                 } gps_sat;
515                 struct {
516                         uint16_t        d0;
517                         uint16_t        d1;
518                 } anon;
519         } u;
520 };
521
522 /* Write a record to the eeprom log */
523 void
524 ao_log_data(struct ao_log_record *log);
525
526 /* Flush the log */
527 void
528 ao_log_flush(void);
529
530 /* Log dumping API:
531  * ao_log_dump_first() - get first log record
532  * ao_log_dump_next()  - get next log record
533  */
534 extern __xdata struct ao_log_record ao_log_dump;
535
536 /* Retrieve first log record for the current flight */
537 uint8_t
538 ao_log_dump_first(void);
539
540 /* return next log record for the current flight */
541 uint8_t
542 ao_log_dump_next(void);
543
544 /* Logging thread main routine */
545 void
546 ao_log(void);
547
548 /* Start logging to eeprom */
549 void
550 ao_log_start(void);
551
552 /* Stop logging */
553 void
554 ao_log_stop(void);
555
556 /* Initialize the logging system */
557 void
558 ao_log_init(void);
559
560 /*
561  * ao_flight.c
562  */
563
564 enum ao_flight_state {
565         ao_flight_startup = 0,
566         ao_flight_idle = 1,
567         ao_flight_pad = 2,
568         ao_flight_boost = 3,
569         ao_flight_fast = 4,
570         ao_flight_coast = 5,
571         ao_flight_drogue = 6,
572         ao_flight_main = 7,
573         ao_flight_landed = 8,
574         ao_flight_invalid = 9
575 };
576
577 extern __xdata struct ao_adc            ao_flight_data;
578 extern __pdata enum ao_flight_state     ao_flight_state;
579 extern __pdata uint16_t                 ao_flight_tick;
580 extern __pdata int16_t                  ao_flight_accel;
581 extern __pdata int16_t                  ao_flight_pres;
582 extern __pdata int32_t                  ao_flight_vel;
583 extern __pdata int16_t                  ao_ground_pres;
584 extern __pdata int16_t                  ao_ground_accel;
585 extern __pdata int16_t                  ao_min_pres;
586 extern __pdata uint16_t                 ao_launch_time;
587
588 /* Flight thread */
589 void
590 ao_flight(void);
591
592 /* Initialize flight thread */
593 void
594 ao_flight_init(void);
595
596 /*
597  * ao_report.c
598  */
599
600 void
601 ao_report_init(void);
602
603 /*
604  * ao_convert.c
605  *
606  * Given raw data, convert to SI units
607  */
608
609 /* pressure from the sensor to altitude in meters */
610 int16_t
611 ao_pres_to_altitude(int16_t pres) __reentrant;
612
613 int16_t
614 ao_altitude_to_pres(int16_t alt) __reentrant;
615
616 int16_t
617 ao_temp_to_dC(int16_t temp) __reentrant;
618
619 /*
620  * ao_dbg.c
621  *
622  * debug another telemetrum board
623  */
624
625 /* Send a byte to the dbg target */
626 void
627 ao_dbg_send_byte(uint8_t byte);
628
629 /* Receive a byte from the dbg target */
630 uint8_t
631 ao_dbg_recv_byte(void);
632
633 /* Start a bulk transfer to/from dbg target memory */
634 void
635 ao_dbg_start_transfer(uint16_t addr);
636
637 /* End a bulk transfer to/from dbg target memory */
638 void
639 ao_dbg_end_transfer(void);
640
641 /* Write a byte to dbg target memory */
642 void
643 ao_dbg_write_byte(uint8_t byte);
644
645 /* Read a byte from dbg target memory */
646 uint8_t
647 ao_dbg_read_byte(void);
648
649 /* Enable dbg mode, switching use of the pins */
650 void
651 ao_dbg_debug_mode(void);
652
653 /* Reset the dbg target */
654 void
655 ao_dbg_reset(void);
656
657 void
658 ao_dbg_init(void);
659
660 /*
661  * ao_serial.c
662  */
663
664 #if !AO_NO_SERIAL_ISR
665 void
666 ao_serial_rx1_isr(void) interrupt 3;
667
668 void
669 ao_serial_tx1_isr(void) interrupt 14;
670 #endif
671
672 char
673 ao_serial_getchar(void) __critical;
674
675 void
676 ao_serial_putchar(char c) __critical;
677
678 #define AO_SERIAL_SPEED_4800    0
679 #define AO_SERIAL_SPEED_9600    1
680 #define AO_SERIAL_SPEED_57600   2
681
682 void
683 ao_serial_set_speed(uint8_t speed);
684
685 void
686 ao_serial_init(void);
687
688 /*
689  * ao_gps.c
690  */
691
692 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
693 #define AO_GPS_NUM_SAT_SHIFT    (0)
694
695 #define AO_GPS_VALID            (1 << 4)
696 #define AO_GPS_RUNNING          (1 << 5)
697
698 struct ao_gps_data {
699         uint8_t                 hour;
700         uint8_t                 minute;
701         uint8_t                 second;
702         uint8_t                 flags;
703         int32_t                 latitude;       /* degrees * 10⁷ */
704         int32_t                 longitude;      /* degrees * 10⁷ */
705         int16_t                 altitude;       /* m */
706         uint16_t                ground_speed;   /* cm/s */
707         uint8_t                 course;         /* degrees / 2 */
708         uint8_t                 hdop;           /* * 5 */
709         int16_t                 climb_rate;     /* cm/s */
710         uint16_t                h_error;        /* m */
711         uint16_t                v_error;        /* m */
712 };
713
714 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
715 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
716 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
717 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
718 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
719 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
720 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
721 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
722
723 struct ao_gps_sat_data {
724         uint8_t         svid;
725         uint8_t         state;
726         uint8_t         c_n_1;
727 };
728
729 struct ao_gps_tracking_data {
730         uint8_t                 channels;
731         struct ao_gps_sat_data  sats[12];
732 };
733
734 extern __xdata uint8_t ao_gps_mutex;
735 extern __xdata struct ao_gps_data ao_gps_data;
736 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
737
738 void
739 ao_gps(void);
740
741 void
742 ao_gps_print(__xdata struct ao_gps_data *gps_data);
743
744 void
745 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
746
747 void
748 ao_gps_init(void);
749
750 /*
751  * ao_gps_report.c
752  */
753
754 void
755 ao_gps_report(void);
756
757 void
758 ao_gps_report_init(void);
759
760 /*
761  * ao_telemetry.c
762  */
763
764 #define AO_MAX_CALLSIGN         8
765
766 struct ao_telemetry {
767         uint8_t                 addr;
768         uint8_t                 flight_state;
769         int16_t                 flight_accel;
770         int16_t                 ground_accel;
771         int32_t                 flight_vel;
772         int16_t                 flight_pres;
773         int16_t                 ground_pres;
774         struct ao_adc           adc;
775         struct ao_gps_data      gps;
776         char                    callsign[AO_MAX_CALLSIGN];
777         struct ao_gps_tracking_data     gps_tracking;
778 };
779
780 /* Set delay between telemetry reports (0 to disable) */
781
782 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(1000)
783 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(50)
784 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(1000)
785
786 void
787 ao_telemetry_set_interval(uint16_t interval);
788
789 void
790 ao_rdf_set(uint8_t rdf);
791
792 void
793 ao_telemetry_init(void);
794
795 /*
796  * ao_radio.c
797  */
798
799 void
800 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant;
801
802 struct ao_radio_recv {
803         struct ao_telemetry     telemetry;
804         int8_t                  rssi;
805         uint8_t                 status;
806 };
807
808 void
809 ao_radio_recv(__xdata struct ao_radio_recv *recv) __reentrant;
810
811 void
812 ao_radio_rdf(int ms);
813
814 void
815 ao_radio_rdf_abort(void);
816
817 void
818 ao_radio_init(void);
819
820 /*
821  * ao_monitor.c
822  */
823
824 extern const char const * const ao_state_names[];
825
826 void
827 ao_monitor(void);
828
829 void
830 ao_set_monitor(uint8_t monitoring);
831
832 void
833 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
834
835 /*
836  * ao_stdio.c
837  */
838
839 void
840 flush(void);
841
842 /*
843  * ao_ignite.c
844  */
845
846 enum ao_igniter {
847         ao_igniter_drogue = 0,
848         ao_igniter_main = 1
849 };
850
851 void
852 ao_ignite(enum ao_igniter igniter);
853
854 enum ao_igniter_status {
855         ao_igniter_unknown,     /* unknown status (ambiguous voltage) */
856         ao_igniter_ready,       /* continuity detected */
857         ao_igniter_active,      /* igniter firing */
858         ao_igniter_open,        /* open circuit detected */
859 };
860
861 enum ao_igniter_status
862 ao_igniter_status(enum ao_igniter igniter);
863
864 void
865 ao_igniter_init(void);
866
867 /*
868  * ao_config.c
869  */
870
871 #define AO_CONFIG_MAJOR 1
872 #define AO_CONFIG_MINOR 1
873
874 struct ao_config {
875         uint8_t         major;
876         uint8_t         minor;
877         uint16_t        main_deploy;
878         int16_t         accel_zero_g;
879         uint8_t         radio_channel;
880         char            callsign[AO_MAX_CALLSIGN + 1];
881         uint8_t         apogee_delay;
882 };
883
884 extern __xdata struct ao_config ao_config;
885
886 void
887 ao_config_get(void);
888
889 void
890 ao_config_init(void);
891
892 /*
893  * ao_rssi.c
894  */
895
896 void
897 ao_rssi_set(int rssi_value);
898
899 void
900 ao_rssi_init(uint8_t rssi_led);
901
902 /*
903  * ao_product.c
904  *
905  * values which need to be defined for
906  * each instance of a product
907  */
908
909 extern const uint8_t ao_usb_descriptors [];
910 extern const uint16_t ao_serial_number;
911 extern const char ao_version[];
912 extern const char ao_manufacturer[];
913 extern const char ao_product[];
914
915 /*
916  * Fifos
917  */
918
919 #define AO_FIFO_SIZE    32
920
921 struct ao_fifo {
922         uint8_t insert;
923         uint8_t remove;
924         char    fifo[AO_FIFO_SIZE];
925 };
926
927 #define ao_fifo_insert(f,c) do { \
928         (f).fifo[(f).insert] = (c); \
929         (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
930 } while(0)
931
932 #define ao_fifo_remove(f,c) do {\
933         c = (f).fifo[(f).remove]; \
934         (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
935 } while(0)
936
937 #define ao_fifo_full(f)         ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
938 #define ao_fifo_empty(f)        ((f).insert == (f).remove)
939
940 /*
941  * ao_packet.c
942  *
943  * Packet-based command interface
944  */
945
946 #define AO_PACKET_MAX   32
947 #define AO_PACKET_WIN   256
948
949 #define AO_PACKET_FIN   (1 << 0)
950 #define AO_PACKET_SYN   (1 << 1)
951 #define AO_PACKET_RST   (1 << 2)
952 #define AO_PACKET_ACK   (1 << 3)
953
954 struct ao_packet {
955         uint8_t         addr;
956         uint8_t         flags;
957         uint16_t        seq;
958         uint16_t        ack;
959         uint16_t        window;
960         uint8_t         len;
961         uint8_t         d[AO_PACKET_MAX];
962 };
963
964 uint8_t
965 ao_packet_connect(uint8_t dest);
966
967 uint8_t
968 ao_packet_accept(void);
969
970 int
971 ao_packet_send(uint8_t *data, int len);
972
973 int
974 ao_packet_recv(uint8_t *data, int len);
975
976 void
977 ao_packet_init(void);
978
979 #endif /* _AO_H_ */