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