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