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