altos: Add telefire-v0.1
[fw/altos] / src / core / 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 "ao_pins.h"
26 #include <ao_arch.h>
27
28 #define TRUE 1
29 #define FALSE 0
30
31 /* Convert a __data pointer into an __xdata pointer */
32 #ifndef DATA_TO_XDATA
33 #define DATA_TO_XDATA(a)        (a)
34 #endif
35 #ifndef PDATA_TO_XDATA
36 #define PDATA_TO_XDATA(a)       (a)
37 #endif
38 #ifndef CODE_TO_XDATA
39 #define CODE_TO_XDATA(a)        (a)
40 #endif
41
42 /* An AltOS task */
43 struct ao_task {
44         __xdata void *wchan;            /* current wait channel (NULL if running) */
45         uint16_t alarm;                 /* abort ao_sleep time */
46         ao_arch_task_members            /* any architecture-specific fields */
47         uint8_t task_id;                /* unique id */
48         __code char *name;              /* task name */
49         uint8_t stack[AO_STACK_SIZE];   /* saved stack */
50 };
51
52 extern __xdata struct ao_task *__data ao_cur_task;
53
54 #define AO_NUM_TASKS            16      /* maximum number of tasks */
55 #define AO_NO_TASK              0       /* no task id */
56
57 /*
58  ao_task.c
59  */
60
61 /* Suspend the current task until wchan is awoken.
62  * returns:
63  *  0 on normal wake
64  *  1 on alarm
65  */
66 uint8_t
67 ao_sleep(__xdata void *wchan);
68
69 /* Wake all tasks sleeping on wchan */
70 void
71 ao_wakeup(__xdata void *wchan);
72
73 /* set an alarm to go off in 'delay' ticks */
74 void
75 ao_alarm(uint16_t delay);
76
77 /* Clear any pending alarm */
78 void
79 ao_clear_alarm(void);
80
81 /* Yield the processor to another task */
82 void
83 ao_yield(void) ao_arch_naked_declare;
84
85 /* Add a task to the run queue */
86 void
87 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
88
89 /* Terminate the current task */
90 void
91 ao_exit(void);
92
93 /* Dump task info to console */
94 void
95 ao_task_info(void);
96
97 /* Start the scheduler. This will not return */
98 void
99 ao_start_scheduler(void);
100
101 /*
102  * ao_panic.c
103  */
104
105 #define AO_PANIC_NO_TASK        1       /* AO_NUM_TASKS is not large enough */
106 #define AO_PANIC_DMA            2       /* Attempt to start DMA while active */
107 #define AO_PANIC_MUTEX          3       /* Mis-using mutex API */
108 #define AO_PANIC_EE             4       /* Mis-using eeprom API */
109 #define AO_PANIC_LOG            5       /* Failing to read/write log data */
110 #define AO_PANIC_CMD            6       /* Too many command sets registered */
111 #define AO_PANIC_STDIO          7       /* Too many stdio handlers registered */
112 #define AO_PANIC_REBOOT         8       /* Reboot failed */
113 #define AO_PANIC_FLASH          9       /* Invalid flash part (or wrong blocksize) */
114 #define AO_PANIC_USB            10      /* Trying to send USB packet while busy */
115 #define AO_PANIC_BT             11      /* Communications with bluetooth device failed */
116 #define AO_PANIC_STACK          12      /* Stack overflow */
117 #define AO_PANIC_SPI            13      /* SPI communication failure */
118 #define AO_PANIC_SELF_TEST      14      /* Self test failure */
119
120 /* Stop the operating system, beeping and blinking the reason */
121 void
122 ao_panic(uint8_t reason);
123
124 /*
125  * ao_timer.c
126  */
127
128 extern volatile __data uint16_t ao_tick_count;
129
130 /* Our timer runs at 100Hz */
131 #define AO_HERTZ                100
132 #define AO_MS_TO_TICKS(ms)      ((ms) / (1000 / AO_HERTZ))
133 #define AO_SEC_TO_TICKS(s)      ((s) * AO_HERTZ)
134
135 /* Returns the current time in ticks */
136 uint16_t
137 ao_time(void);
138
139 /* Suspend the current task until ticks time has passed */
140 void
141 ao_delay(uint16_t ticks);
142
143 /* Set the ADC interval */
144 void
145 ao_timer_set_adc_interval(uint8_t interval) __critical;
146
147 /* Timer interrupt */
148 void
149 ao_timer_isr(void) ao_arch_interrupt(9);
150
151 /* Initialize the timer */
152 void
153 ao_timer_init(void);
154
155 /* Initialize the hardware clock. Must be called first */
156 void
157 ao_clock_init(void);
158
159 /*
160  * ao_mutex.c
161  */
162
163 void
164 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
165
166 void
167 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
168
169 /*
170  * ao_cmd.c
171  */
172
173 enum ao_cmd_status {
174         ao_cmd_success = 0,
175         ao_cmd_lex_error = 1,
176         ao_cmd_syntax_error = 2,
177 };
178
179 extern __pdata uint16_t ao_cmd_lex_i;
180 extern __pdata uint32_t ao_cmd_lex_u32;
181 extern __pdata char     ao_cmd_lex_c;
182 extern __pdata enum ao_cmd_status ao_cmd_status;
183
184 void
185 ao_cmd_lex(void);
186
187 void
188 ao_cmd_put8(uint8_t v);
189
190 void
191 ao_cmd_put16(uint16_t v);
192
193 uint8_t
194 ao_cmd_is_white(void);
195
196 void
197 ao_cmd_white(void);
198
199 int8_t
200 ao_cmd_hexchar(char c);
201
202 void
203 ao_cmd_hexbyte(void);
204
205 void
206 ao_cmd_hex(void);
207
208 void
209 ao_cmd_decimal(void);
210
211 uint8_t
212 ao_match_word(__code char *word);
213
214 struct ao_cmds {
215         void            (*func)(void);
216         __code char     *help;
217 };
218
219 void
220 ao_cmd_register(const __code struct ao_cmds *cmds);
221
222 void
223 ao_cmd_init(void);
224
225 #if HAS_CMD_FILTER
226 /*
227  * Provided by an external module to filter raw command lines
228  */
229 uint8_t
230 ao_cmd_filter(void);
231 #endif
232
233 /*
234  * Various drivers
235  */
236 #if HAS_ADC
237 #include <ao_adc.h>
238 #endif
239
240 #if HAS_BEEP
241 #include <ao_beep.h>
242 #endif
243
244 #if LEDS_AVAILABLE
245 #include <ao_led.h>
246 #endif
247
248 #if HAS_USB
249 #include <ao_usb.h>
250 #endif
251
252 #if HAS_EEPROM
253 #include <ao_storage.h>
254 #endif
255
256 #if HAS_LOG
257 #include <ao_log.h>
258 #endif
259
260 #if HAS_FLIGHT
261 #include <ao_flight.h>
262 #include <ao_sample.h>
263 #endif
264
265 /*
266  * ao_report.c
267  */
268
269 #define AO_RDF_INTERVAL_TICKS   AO_SEC_TO_TICKS(5)
270 #define AO_RDF_LENGTH_MS        500
271 #define AO_RDF_CONTINUITY_MS    32
272 #define AO_RDF_CONTINUITY_PAUSE 96
273 #define AO_RDF_CONTINUITY_TOTAL ((AO_RDF_CONTINUITY_PAUSE + AO_RDF_CONTINUITY_MS) * 3 + AO_RDF_CONTINUITY_PAUSE)
274
275 /* This assumes that we're generating a 1kHz tone, which
276  * modulates the carrier at 2kbps, or 250kBps
277  */
278 #define AO_MS_TO_RDF_LEN(ms) ((ms) / 4)
279
280 #define AO_RADIO_RDF_LEN        AO_MS_TO_RDF_LEN(AO_RDF_LENGTH_MS)
281 #define AO_RADIO_CONT_TONE_LEN  AO_MS_TO_RDF_LEN(AO_RDF_CONTINUITY_MS)
282 #define AO_RADIO_CONT_PAUSE_LEN AO_MS_TO_RDF_LEN(AO_RDF_CONTINUITY_PAUSE)
283 #define AO_RADIO_CONT_TOTAL_LEN AO_MS_TO_RDF_LEN(AO_RDF_CONTINUITY_TOTAL)
284
285 /* returns a value 0-3 to indicate igniter continuity */
286 uint8_t
287 ao_report_igniter(void);
288
289 void
290 ao_report_init(void);
291
292 /*
293  * ao_convert.c
294  *
295  * Given raw data, convert to SI units
296  */
297
298 /* pressure from the sensor to altitude in meters */
299 int16_t
300 ao_pres_to_altitude(int16_t pres) __reentrant;
301
302 int16_t
303 ao_altitude_to_pres(int16_t alt) __reentrant;
304
305 int16_t
306 ao_temp_to_dC(int16_t temp) __reentrant;
307
308 /*
309  * ao_convert_pa.c
310  *
311  * Convert between pressure in Pa and altitude in meters
312  */
313
314 int32_t
315 ao_pa_to_altitude(int32_t pa);
316
317 int32_t
318 ao_altitude_to_pa(int32_t alt);
319
320 #if HAS_DBG
321 #include <ao_dbg.h>
322 #endif
323
324 #if HAS_SERIAL_0 || HAS_SERIAL_1 || HAS_SERIAL_2 || HAS_SERIAL_3
325 #include <ao_serial.h>
326 #endif
327
328
329 /*
330  * ao_spi_slave.c
331  */
332
333 uint8_t
334 ao_spi_slave_recv(uint8_t *buf, uint8_t len);
335
336 void
337 ao_spi_slave_send(uint8_t *buf, uint8_t len);
338
339 void
340 ao_spi_slave_init(void);
341
342 /* This must be defined by the product; it will get called when chip
343  * select goes low, at which point it should use ao_spi_read and
344  * ao_spi_write to deal with the request
345  */
346
347 void
348 ao_spi_slave(void);
349
350 #include <ao_telemetry.h>
351 /*
352  * ao_gps.c
353  */
354
355 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
356 #define AO_GPS_NUM_SAT_SHIFT    (0)
357
358 #define AO_GPS_VALID            (1 << 4)
359 #define AO_GPS_RUNNING          (1 << 5)
360 #define AO_GPS_DATE_VALID       (1 << 6)
361 #define AO_GPS_COURSE_VALID     (1 << 7)
362
363 extern __pdata uint16_t ao_gps_tick;
364 extern __xdata uint8_t ao_gps_mutex;
365 extern __xdata struct ao_telemetry_location ao_gps_data;
366 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
367
368 struct ao_gps_orig {
369         uint8_t                 year;
370         uint8_t                 month;
371         uint8_t                 day;
372         uint8_t                 hour;
373         uint8_t                 minute;
374         uint8_t                 second;
375         uint8_t                 flags;
376         int32_t                 latitude;       /* degrees * 10⁷ */
377         int32_t                 longitude;      /* degrees * 10⁷ */
378         int16_t                 altitude;       /* m */
379         uint16_t                ground_speed;   /* cm/s */
380         uint8_t                 course;         /* degrees / 2 */
381         uint8_t                 hdop;           /* * 5 */
382         int16_t                 climb_rate;     /* cm/s */
383         uint16_t                h_error;        /* m */
384         uint16_t                v_error;        /* m */
385 };
386
387 struct ao_gps_sat_orig {
388         uint8_t         svid;
389         uint8_t         c_n_1;
390 };
391
392 #define AO_MAX_GPS_TRACKING     12
393
394 struct ao_gps_tracking_orig {
395         uint8_t                 channels;
396         struct ao_gps_sat_orig  sats[AO_MAX_GPS_TRACKING];
397 };
398
399 void
400 ao_gps(void);
401
402 void
403 ao_gps_print(__xdata struct ao_gps_orig *gps_data);
404
405 void
406 ao_gps_tracking_print(__xdata struct ao_gps_tracking_orig *gps_tracking_data);
407
408 void
409 ao_gps_init(void);
410
411 /*
412  * ao_gps_report.c
413  */
414
415 void
416 ao_gps_report(void);
417
418 void
419 ao_gps_report_init(void);
420
421 /*
422  * ao_gps_report_mega.c
423  */
424
425 void
426 ao_gps_report_mega(void);
427
428 void
429 ao_gps_report_mega_init(void);
430
431 /*
432  * ao_telemetry_orig.c
433  */
434
435 #if LEGACY_MONITOR
436 struct ao_adc_orig {
437         uint16_t        tick;           /* tick when the sample was read */
438         int16_t         accel;          /* accelerometer */
439         int16_t         pres;           /* pressure sensor */
440         int16_t         temp;           /* temperature sensor */
441         int16_t         v_batt;         /* battery voltage */
442         int16_t         sense_d;        /* drogue continuity sense */
443         int16_t         sense_m;        /* main continuity sense */
444 };
445
446 struct ao_telemetry_orig {
447         uint16_t                serial;
448         uint16_t                flight;
449         uint8_t                 flight_state;
450         int16_t                 accel;
451         int16_t                 ground_accel;
452         union {
453                 struct {
454                         int16_t                 speed;
455                         int16_t                 unused;
456                 } k;
457                 int32_t         flight_vel;
458         } u;
459         int16_t                 height;
460         int16_t                 ground_pres;
461         int16_t                 accel_plus_g;
462         int16_t                 accel_minus_g;
463         struct ao_adc_orig      adc;
464         struct ao_gps_orig      gps;
465         char                    callsign[AO_MAX_CALLSIGN];
466         struct ao_gps_tracking_orig     gps_tracking;
467 };
468
469 struct ao_telemetry_tiny {
470         uint16_t                serial;
471         uint16_t                flight;
472         uint8_t                 flight_state;
473         int16_t                 height;         /* AGL in meters */
474         int16_t                 speed;          /* in m/s * 16 */
475         int16_t                 accel;          /* in m/s² * 16 */
476         int16_t                 ground_pres;    /* sensor units */
477         struct ao_adc           adc;            /* raw ADC readings */
478         char                    callsign[AO_MAX_CALLSIGN];
479 };
480
481 struct ao_telemetry_orig_recv {
482         struct ao_telemetry_orig        telemetry_orig;
483         int8_t                          rssi;
484         uint8_t                         status;
485 };
486
487 struct ao_telemetry_tiny_recv {
488         struct ao_telemetry_tiny        telemetry_tiny;
489         int8_t                          rssi;
490         uint8_t                         status;
491 };
492
493 #endif /* LEGACY_MONITOR */
494
495 /* Unfortunately, we've exposed the CC1111 rssi units as the 'usual' method
496  * for reporting RSSI. So, now we use these values everywhere
497  */
498 #define AO_RSSI_FROM_RADIO(radio)       ((int16_t) ((int8_t) (radio) >> 1) - 74)
499 #define AO_RADIO_FROM_RSSI(rssi)        (((int8_t) (rssi) + 74) << 1)
500
501 /*
502  * ao_radio_recv tacks on rssi and status bytes
503  */
504
505 struct ao_telemetry_raw_recv {
506         uint8_t                 packet[AO_MAX_TELEMETRY + 2];
507 };
508
509 /* Set delay between telemetry reports (0 to disable) */
510
511 #ifdef AO_SEND_ALL_BARO
512 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(100)
513 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(100)
514 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(100)
515 #else
516 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(1000)
517 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(100)
518 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(1000)
519 #endif
520
521 void
522 ao_telemetry_set_interval(uint16_t interval);
523
524 void
525 ao_rdf_set(uint8_t rdf);
526
527 void
528 ao_telemetry_init(void);
529
530 void
531 ao_telemetry_orig_init(void);
532
533 void
534 ao_telemetry_tiny_init(void);
535
536 /*
537  * ao_radio.c
538  */
539
540 extern __xdata uint8_t  ao_radio_dma;
541 extern __xdata uint8_t ao_radio_dma_done;
542 extern __xdata uint8_t ao_radio_done;
543 extern __xdata uint8_t ao_radio_mutex;
544
545 #ifdef PKT_APPEND_STATUS_1_CRC_OK
546 #define AO_RADIO_STATUS_CRC_OK  PKT_APPEND_STATUS_1_CRC_OK
547 #else
548 #include <ao_fec.h>
549 #define AO_RADIO_STATUS_CRC_OK  AO_FEC_DECODE_CRC_OK
550 #endif
551
552 void
553 ao_radio_general_isr(void) ao_arch_interrupt(16);
554
555 void
556 ao_radio_send(const __xdata void *d, uint8_t size) __reentrant;
557
558 uint8_t
559 ao_radio_recv(__xdata void *d, uint8_t size) __reentrant;
560
561 void
562 ao_radio_recv_abort(void);
563
564 /*
565  * Compute the packet length as follows:
566  *
567  * 2000 bps (for a 1kHz tone)
568  * so, for 'ms' milliseconds, we need
569  * 2 * ms bits, or ms / 4 bytes
570  */
571
572 void
573 ao_radio_rdf(void);
574
575 void
576 ao_radio_continuity(uint8_t c);
577
578 void
579 ao_radio_rdf_abort(void);
580
581 void
582 ao_radio_init(void);
583
584 /*
585  * ao_monitor.c
586  */
587
588 #if HAS_MONITOR
589
590 extern const char const * const ao_state_names[];
591
592 #define AO_MONITOR_RING 8
593
594 union ao_monitor {
595         struct ao_telemetry_raw_recv    raw;
596         struct ao_telemetry_all_recv    all;
597         struct ao_telemetry_orig_recv   orig;
598         struct ao_telemetry_tiny_recv   tiny;
599 };
600
601 extern __xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
602
603 #define ao_monitor_ring_next(n) (((n) + 1) & (AO_MONITOR_RING - 1))
604
605 extern __data uint8_t ao_monitoring;
606 extern __data uint8_t ao_monitor_head;
607
608 void
609 ao_monitor(void);
610
611 #define AO_MONITORING_OFF       0
612 #define AO_MONITORING_ORIG      1
613
614 void
615 ao_monitor_set(uint8_t monitoring);
616
617 void
618 ao_monitor_disable(void);
619
620 void
621 ao_monitor_enable(void);
622
623 void
624 ao_monitor_init(void) __reentrant;
625
626 #endif
627
628 /*
629  * ao_stdio.c
630  */
631
632 #define AO_READ_AGAIN   ((char) -1)
633
634 struct ao_stdio {
635         char    (*pollchar)(void);
636         void    (*putchar)(char c) __reentrant;
637         void    (*flush)(void);
638         uint8_t echo;
639 };
640
641 extern __xdata struct ao_stdio ao_stdios[];
642 extern __pdata int8_t ao_cur_stdio;
643 extern __pdata int8_t ao_num_stdios;
644
645 void
646 flush(void);
647
648 extern __xdata uint8_t ao_stdin_ready;
649
650 uint8_t
651 ao_echo(void);
652
653 int8_t
654 ao_add_stdio(char (*pollchar)(void),
655              void (*putchar)(char) __reentrant,
656              void (*flush)(void)) __reentrant;
657
658 /*
659  * ao_ignite.c
660  */
661
662 enum ao_igniter {
663         ao_igniter_drogue = 0,
664         ao_igniter_main = 1
665 };
666
667 void
668 ao_ignite(enum ao_igniter igniter);
669
670 enum ao_igniter_status {
671         ao_igniter_unknown,     /* unknown status (ambiguous voltage) */
672         ao_igniter_ready,       /* continuity detected */
673         ao_igniter_active,      /* igniter firing */
674         ao_igniter_open,        /* open circuit detected */
675 };
676
677 struct ao_ignition {
678         uint8_t request;
679         uint8_t fired;
680         uint8_t firing;
681 };
682
683 extern __xdata struct ao_ignition ao_ignition[2];
684
685 enum ao_igniter_status
686 ao_igniter_status(enum ao_igniter igniter);
687
688 extern __pdata uint8_t ao_igniter_present;
689
690 void
691 ao_ignite_set_pins(void);
692
693 void
694 ao_igniter_init(void);
695
696 /*
697  * ao_config.c
698  */
699
700 #if AO_PYRO_NUM
701 #include <ao_pyro.h>
702 #endif
703
704 #if HAS_FORCE_FREQ
705 /*
706  * Set this to force the frequency to 434.550MHz
707  */
708 extern __xdata uint8_t ao_force_freq;
709 #endif
710
711 #define AO_CONFIG_MAJOR 1
712 #define AO_CONFIG_MINOR 12
713
714 #define AO_AES_LEN 16
715
716 struct ao_config {
717         uint8_t         major;
718         uint8_t         minor;
719         uint16_t        main_deploy;
720         int16_t         accel_plus_g;           /* changed for minor version 2 */
721         uint8_t         _legacy_radio_channel;
722         char            callsign[AO_MAX_CALLSIGN + 1];
723         uint8_t         apogee_delay;           /* minor version 1 */
724         int16_t         accel_minus_g;          /* minor version 2 */
725         uint32_t        radio_cal;              /* minor version 3 */
726         uint32_t        flight_log_max;         /* minor version 4 */
727         uint8_t         ignite_mode;            /* minor version 5 */
728         uint8_t         pad_orientation;        /* minor version 6 */
729         uint32_t        radio_setting;          /* minor version 7 */
730         uint8_t         radio_enable;           /* minor version 8 */
731         uint8_t         aes_key[AO_AES_LEN];    /* minor version 9 */
732         uint32_t        frequency;              /* minor version 10 */
733         uint16_t        apogee_lockout;         /* minor version 11 */
734 #if AO_PYRO_NUM
735         struct ao_pyro  pyro[AO_PYRO_NUM];      /* minor version 12 */
736 #endif
737 };
738
739 #define AO_IGNITE_MODE_DUAL             0
740 #define AO_IGNITE_MODE_APOGEE           1
741 #define AO_IGNITE_MODE_MAIN             2
742
743 #define AO_PAD_ORIENTATION_ANTENNA_UP   0
744 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
745
746 extern __xdata struct ao_config ao_config;
747
748 #define AO_CONFIG_MAX_SIZE      128
749
750 void
751 _ao_config_edit_start(void);
752
753 void
754 _ao_config_edit_finish(void);
755
756 void
757 ao_config_get(void);
758
759 void
760 ao_config_put(void);
761
762 void
763 ao_config_set_radio(void);
764
765 void
766 ao_config_init(void);
767
768 /*
769  * ao_rssi.c
770  */
771
772 void
773 ao_rssi_set(int rssi_value);
774
775 void
776 ao_rssi_init(uint8_t rssi_led);
777
778 /*
779  * ao_product.c
780  *
781  * values which need to be defined for
782  * each instance of a product
783  */
784
785 extern const char ao_version[];
786 extern const char ao_manufacturer[];
787 extern const char ao_product[];
788
789 /*
790  * Fifos
791  */
792
793 #define AO_FIFO_SIZE    32
794
795 struct ao_fifo {
796         uint8_t insert;
797         uint8_t remove;
798         char    fifo[AO_FIFO_SIZE];
799 };
800
801 #define ao_fifo_insert(f,c) do { \
802         (f).fifo[(f).insert] = (c); \
803         (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
804 } while(0)
805
806 #define ao_fifo_remove(f,c) do {\
807         c = (f).fifo[(f).remove]; \
808         (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
809 } while(0)
810
811 #define ao_fifo_full(f)         ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
812 #define ao_fifo_empty(f)        ((f).insert == (f).remove)
813
814 #if PACKET_HAS_MASTER || PACKET_HAS_SLAVE
815 #include <ao_packet.h>
816 #endif
817
818 #if HAS_BTM
819 #include <ao_btm.h>
820 #endif
821
822 #if HAS_COMPANION
823 #include <ao_companion.h>
824 #endif
825
826 #if HAS_LCD
827 #include <ao_lcd.h>
828 #endif
829
830 #if HAS_AES
831 #include <ao_aes.h>
832 #endif
833
834 /* ao_launch.c */
835
836 struct ao_launch_command {
837         uint16_t        tick;
838         uint16_t        serial;
839         uint8_t         cmd;
840         uint8_t         channel;
841         uint16_t        unused;
842 };
843
844 #define AO_LAUNCH_QUERY         1
845
846 struct ao_launch_query {
847         uint16_t        tick;
848         uint16_t        serial;
849         uint8_t         channel;
850         uint8_t         valid;
851         uint8_t         arm_status;
852         uint8_t         igniter_status;
853 };
854
855 #define AO_LAUNCH_ARM           2
856 #define AO_LAUNCH_FIRE          3
857
858 void
859 ao_launch_init(void);
860
861 /*
862  * ao_log_single.c
863  */
864
865 #define AO_LOG_TELESCIENCE_START        ((uint8_t) 's')
866 #define AO_LOG_TELESCIENCE_DATA         ((uint8_t) 'd')
867
868 #define AO_LOG_TELESCIENCE_NUM_ADC      12
869
870 struct ao_log_telescience {
871         uint8_t         type;
872         uint8_t         csum;
873         uint16_t        tick;
874         uint16_t        tm_tick;
875         uint8_t         tm_state;
876         uint8_t         unused;
877         uint16_t        adc[AO_LOG_TELESCIENCE_NUM_ADC];
878 };
879
880 #define AO_LOG_SINGLE_SIZE              32
881
882 union ao_log_single {
883         struct ao_log_telescience       telescience;
884         union ao_telemetry_all          telemetry;
885         uint8_t                         bytes[AO_LOG_SINGLE_SIZE];
886 };
887
888 extern __xdata union ao_log_single      ao_log_single_write_data;
889 extern __xdata union ao_log_single      ao_log_single_read_data;
890
891 void
892 ao_log_single_extra_query(void);
893
894 void
895 ao_log_single_list(void);
896
897 void
898 ao_log_single_main(void);
899
900 uint8_t
901 ao_log_single_write(void);
902
903 uint8_t
904 ao_log_single_read(uint32_t pos);
905
906 void
907 ao_log_single_start(void);
908
909 void
910 ao_log_single_stop(void);
911
912 void
913 ao_log_single_restart(void);
914
915 void
916 ao_log_single_set(void);
917
918 void
919 ao_log_single_delete(void);
920
921 void
922 ao_log_single_init(void);
923
924 void
925 ao_log_single(void);
926
927 /*
928  * ao_pyro_slave.c
929  */
930
931 #define AO_TELEPYRO_NUM_ADC     9
932
933 #ifndef ao_xmemcpy
934 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
935 #define ao_xmemset(d,v,c) memset(d,v,c)
936 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
937 #endif
938
939 /*
940  * ao_terraui.c
941  */
942
943 void
944 ao_terraui_init(void);
945
946 /*
947  * ao_battery.c
948  */
949
950 #ifdef BATTERY_PIN
951 void
952 ao_battery_isr(void) ao_arch_interrupt(1);
953
954 uint16_t
955 ao_battery_get(void);
956
957 void
958 ao_battery_init(void);
959 #endif /* BATTERY_PIN */
960
961 /*
962  * ao_sqrt.c
963  */
964
965 uint32_t
966 ao_sqrt(uint32_t op);
967
968 /*
969  * ao_freq.c
970  */
971
972 int32_t ao_freq_to_set(int32_t freq, int32_t cal) __reentrant;
973
974 #include <ao_arch_funcs.h>
975
976 /*
977  * ao_ms5607.c
978  */
979
980 void ao_ms5607_init(void);
981
982 #endif /* _AO_H_ */