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