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