altos: Massive product config cleanup
[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 #if HAS_DBG
286 #include <ao_dbg.h>
287 #endif
288
289 #if HAS_SERIAL_0 || HAS_SERIAL_1 || HAS_SERIAL_2 || HAS_SERIAL_3
290 #include <ao_serial.h>
291 #endif
292
293
294 /*
295  * ao_spi_slave.c
296  */
297
298 uint8_t
299 ao_spi_slave_recv(uint8_t *buf, uint8_t len);
300
301 void
302 ao_spi_slave_send(uint8_t *buf, uint8_t len);
303
304 void
305 ao_spi_slave_init(void);
306
307 /* This must be defined by the product; it will get called when chip
308  * select goes low, at which point it should use ao_spi_read and
309  * ao_spi_write to deal with the request
310  */
311
312 void
313 ao_spi_slave(void);
314
315 /*
316  * ao_telemetry.c
317  */
318 #define AO_MAX_CALLSIGN                 8
319 #define AO_MAX_VERSION                  8
320 #if LEGACY_MONITOR
321 #define AO_MAX_TELEMETRY                128
322 #else
323 #define AO_MAX_TELEMETRY                32
324 #endif
325
326 struct ao_telemetry_generic {
327         uint16_t        serial;         /* 0 */
328         uint16_t        tick;           /* 2 */
329         uint8_t         type;           /* 4 */
330         uint8_t         payload[27];    /* 5 */
331         /* 32 */
332 };
333
334 #define AO_TELEMETRY_SENSOR_TELEMETRUM  0x01
335 #define AO_TELEMETRY_SENSOR_TELEMINI    0x02
336 #define AO_TELEMETRY_SENSOR_TELENANO    0x03
337
338 struct ao_telemetry_sensor {
339         uint16_t        serial;         /*  0 */
340         uint16_t        tick;           /*  2 */
341         uint8_t         type;           /*  4 */
342
343         uint8_t         state;          /*  5 flight state */
344         int16_t         accel;          /*  6 accelerometer (TM only) */
345         int16_t         pres;           /*  8 pressure sensor */
346         int16_t         temp;           /* 10 temperature sensor */
347         int16_t         v_batt;         /* 12 battery voltage */
348         int16_t         sense_d;        /* 14 drogue continuity sense (TM/Tm) */
349         int16_t         sense_m;        /* 16 main continuity sense (TM/Tm) */
350
351         int16_t         acceleration;   /* 18 m/s² * 16 */
352         int16_t         speed;          /* 20 m/s * 16 */
353         int16_t         height;         /* 22 m */
354
355         int16_t         ground_pres;    /* 24 average pres on pad */
356         int16_t         ground_accel;   /* 26 average accel on pad */
357         int16_t         accel_plus_g;   /* 28 accel calibration at +1g */
358         int16_t         accel_minus_g;  /* 30 accel calibration at -1g */
359         /* 32 */
360 };
361
362 #define AO_TELEMETRY_CONFIGURATION      0x04
363
364 struct ao_telemetry_configuration {
365         uint16_t        serial;                         /*  0 */
366         uint16_t        tick;                           /*  2 */
367         uint8_t         type;                           /*  4 */
368
369         uint8_t         device;                         /*  5 device type */
370         uint16_t        flight;                         /*  6 flight number */
371         uint8_t         config_major;                   /*  8 Config major version */
372         uint8_t         config_minor;                   /*  9 Config minor version */
373         uint16_t        apogee_delay;                   /* 10 Apogee deploy delay in seconds */
374         uint16_t        main_deploy;                    /* 12 Main deploy alt in meters */
375         uint16_t        flight_log_max;                 /* 14 Maximum flight log size in kB */
376         char            callsign[AO_MAX_CALLSIGN];      /* 16 Radio operator identity */
377         char            version[AO_MAX_VERSION];        /* 24 Software version */
378         /* 32 */
379 };
380
381 #define AO_TELEMETRY_LOCATION           0x05
382
383 #define AO_GPS_MODE_NOT_VALID           'N'
384 #define AO_GPS_MODE_AUTONOMOUS          'A'
385 #define AO_GPS_MODE_DIFFERENTIAL        'D'
386 #define AO_GPS_MODE_ESTIMATED           'E'
387 #define AO_GPS_MODE_MANUAL              'M'
388 #define AO_GPS_MODE_SIMULATED           'S'
389
390 struct ao_telemetry_location {
391         uint16_t        serial;         /*  0 */
392         uint16_t        tick;           /*  2 */
393         uint8_t         type;           /*  4 */
394
395         uint8_t         flags;          /*  5 Number of sats and other flags */
396         int16_t         altitude;       /*  6 GPS reported altitude (m) */
397         int32_t         latitude;       /*  8 latitude (degrees * 10⁷) */
398         int32_t         longitude;      /* 12 longitude (degrees * 10⁷) */
399         uint8_t         year;           /* 16 (- 2000) */
400         uint8_t         month;          /* 17 (1-12) */
401         uint8_t         day;            /* 18 (1-31) */
402         uint8_t         hour;           /* 19 (0-23) */
403         uint8_t         minute;         /* 20 (0-59) */
404         uint8_t         second;         /* 21 (0-59) */
405         uint8_t         pdop;           /* 22 (m * 5) */
406         uint8_t         hdop;           /* 23 (m * 5) */
407         uint8_t         vdop;           /* 24 (m * 5) */
408         uint8_t         mode;           /* 25 */
409         uint16_t        ground_speed;   /* 26 cm/s */
410         int16_t         climb_rate;     /* 28 cm/s */
411         uint8_t         course;         /* 30 degrees / 2 */
412         uint8_t         unused[1];      /* 31 */
413         /* 32 */
414 };
415
416 #define AO_TELEMETRY_SATELLITE          0x06
417
418 struct ao_telemetry_satellite_info {
419         uint8_t         svid;
420         uint8_t         c_n_1;
421 };
422
423 struct ao_telemetry_satellite {
424         uint16_t                                serial;         /*  0 */
425         uint16_t                                tick;           /*  2 */
426         uint8_t                                 type;           /*  4 */
427         uint8_t                                 channels;       /*  5 number of reported sats */
428
429         struct ao_telemetry_satellite_info      sats[12];       /* 6 */
430         uint8_t                                 unused[2];      /* 30 */
431         /* 32 */
432 };
433
434 #define AO_TELEMETRY_COMPANION          0x07
435
436 #define AO_COMPANION_MAX_CHANNELS       12
437
438 struct ao_telemetry_companion {
439         uint16_t                                serial;         /*  0 */
440         uint16_t                                tick;           /*  2 */
441         uint8_t                                 type;           /*  4 */
442         uint8_t                                 board_id;       /*  5 */
443
444         uint8_t                                 update_period;  /*  6 */
445         uint8_t                                 channels;       /*  7 */
446         uint16_t                                companion_data[AO_COMPANION_MAX_CHANNELS];      /*  8 */
447         /* 32 */
448 };
449         
450 /* #define AO_SEND_ALL_BARO */
451
452 #define AO_TELEMETRY_BARO               0x80
453
454 /*
455  * This packet allows the full sampling rate baro
456  * data to be captured over the RF link so that the
457  * flight software can be tested using 'real' data.
458  *
459  * Along with this telemetry packet, the flight
460  * code is modified to send full-rate telemetry all the time
461  * and never send an RDF tone; this ensure that the full radio
462  * link is available.
463  */
464 struct ao_telemetry_baro {
465         uint16_t                                serial;         /*  0 */
466         uint16_t                                tick;           /*  2 */
467         uint8_t                                 type;           /*  4 */
468         uint8_t                                 samples;        /*  5 number samples */
469
470         int16_t                                 baro[12];       /* 6 samples */
471         /* 32 */
472 };
473
474 union ao_telemetry_all {
475         struct ao_telemetry_generic             generic;
476         struct ao_telemetry_sensor              sensor;
477         struct ao_telemetry_configuration       configuration;
478         struct ao_telemetry_location            location;
479         struct ao_telemetry_satellite           satellite;
480         struct ao_telemetry_companion           companion;
481         struct ao_telemetry_baro                baro;
482 };
483
484 struct ao_telemetry_all_recv {
485         union ao_telemetry_all          telemetry;
486         int8_t                          rssi;
487         uint8_t                         status;
488 };
489
490 /*
491  * ao_gps.c
492  */
493
494 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
495 #define AO_GPS_NUM_SAT_SHIFT    (0)
496
497 #define AO_GPS_VALID            (1 << 4)
498 #define AO_GPS_RUNNING          (1 << 5)
499 #define AO_GPS_DATE_VALID       (1 << 6)
500 #define AO_GPS_COURSE_VALID     (1 << 7)
501
502 extern __pdata uint16_t ao_gps_tick;
503 extern __xdata uint8_t ao_gps_mutex;
504 extern __xdata struct ao_telemetry_location ao_gps_data;
505 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
506
507 struct ao_gps_orig {
508         uint8_t                 year;
509         uint8_t                 month;
510         uint8_t                 day;
511         uint8_t                 hour;
512         uint8_t                 minute;
513         uint8_t                 second;
514         uint8_t                 flags;
515         int32_t                 latitude;       /* degrees * 10⁷ */
516         int32_t                 longitude;      /* degrees * 10⁷ */
517         int16_t                 altitude;       /* m */
518         uint16_t                ground_speed;   /* cm/s */
519         uint8_t                 course;         /* degrees / 2 */
520         uint8_t                 hdop;           /* * 5 */
521         int16_t                 climb_rate;     /* cm/s */
522         uint16_t                h_error;        /* m */
523         uint16_t                v_error;        /* m */
524 };
525
526 struct ao_gps_sat_orig {
527         uint8_t         svid;
528         uint8_t         c_n_1;
529 };
530
531 #define AO_MAX_GPS_TRACKING     12
532
533 struct ao_gps_tracking_orig {
534         uint8_t                 channels;
535         struct ao_gps_sat_orig  sats[AO_MAX_GPS_TRACKING];
536 };
537
538 void
539 ao_gps(void);
540
541 void
542 ao_gps_print(__xdata struct ao_gps_orig *gps_data);
543
544 void
545 ao_gps_tracking_print(__xdata struct ao_gps_tracking_orig *gps_tracking_data);
546
547 void
548 ao_gps_init(void);
549
550 /*
551  * ao_gps_report.c
552  */
553
554 void
555 ao_gps_report(void);
556
557 void
558 ao_gps_report_init(void);
559
560 /*
561  * ao_telemetry_orig.c
562  */
563
564 struct ao_telemetry_orig {
565         uint16_t                serial;
566         uint16_t                flight;
567         uint8_t                 flight_state;
568         int16_t                 accel;
569         int16_t                 ground_accel;
570         union {
571                 struct {
572                         int16_t                 speed;
573                         int16_t                 unused;
574                 } k;
575                 int32_t         flight_vel;
576         } u;
577         int16_t                 height;
578         int16_t                 ground_pres;
579         int16_t                 accel_plus_g;
580         int16_t                 accel_minus_g;
581         struct ao_adc           adc;
582         struct ao_gps_orig      gps;
583         char                    callsign[AO_MAX_CALLSIGN];
584         struct ao_gps_tracking_orig     gps_tracking;
585 };
586
587 struct ao_telemetry_tiny {
588         uint16_t                serial;
589         uint16_t                flight;
590         uint8_t                 flight_state;
591         int16_t                 height;         /* AGL in meters */
592         int16_t                 speed;          /* in m/s * 16 */
593         int16_t                 accel;          /* in m/s² * 16 */
594         int16_t                 ground_pres;    /* sensor units */
595         struct ao_adc           adc;            /* raw ADC readings */
596         char                    callsign[AO_MAX_CALLSIGN];
597 };
598
599 struct ao_telemetry_orig_recv {
600         struct ao_telemetry_orig        telemetry_orig;
601         int8_t                          rssi;
602         uint8_t                         status;
603 };
604
605 struct ao_telemetry_tiny_recv {
606         struct ao_telemetry_tiny        telemetry_tiny;
607         int8_t                          rssi;
608         uint8_t                         status;
609 };
610
611 /*
612  * ao_radio_recv tacks on rssi and status bytes
613  */
614
615 struct ao_telemetry_raw_recv {
616         uint8_t                 packet[AO_MAX_TELEMETRY + 2];
617 };
618
619 /* Set delay between telemetry reports (0 to disable) */
620
621 #ifdef AO_SEND_ALL_BARO
622 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(100)
623 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(100)
624 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(100)
625 #else
626 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(1000)
627 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(100)
628 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(1000)
629 #endif
630
631 void
632 ao_telemetry_set_interval(uint16_t interval);
633
634 void
635 ao_rdf_set(uint8_t rdf);
636
637 void
638 ao_telemetry_init(void);
639
640 void
641 ao_telemetry_orig_init(void);
642
643 void
644 ao_telemetry_tiny_init(void);
645
646 /*
647  * ao_radio.c
648  */
649
650 extern __xdata uint8_t  ao_radio_dma;
651 extern __xdata uint8_t ao_radio_dma_done;
652 extern __xdata uint8_t ao_radio_done;
653 extern __xdata uint8_t ao_radio_mutex;
654
655 void
656 ao_radio_general_isr(void) ao_arch_interrupt(16);
657
658 void
659 ao_radio_get(uint8_t len);
660
661 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
662
663 void
664 ao_radio_set_packet(void);
665
666 void
667 ao_radio_send(__xdata void *d, uint8_t size) __reentrant;
668
669 uint8_t
670 ao_radio_recv(__xdata void *d, uint8_t size) __reentrant;
671
672 void
673 ao_radio_recv_abort(void);
674
675 /*
676  * Compute the packet length as follows:
677  *
678  * 2000 bps (for a 1kHz tone)
679  * so, for 'ms' milliseconds, we need
680  * 2 * ms bits, or ms / 4 bytes
681  */
682
683 #define AO_MS_TO_RDF_LEN(ms) ((ms) > 255 * 4 ? 255 : ((ms) >> 2))
684
685 void
686 ao_radio_rdf(uint8_t pkt_len);
687
688 void
689 ao_radio_rdf_abort(void);
690
691 void
692 ao_radio_idle(void);
693
694 void
695 ao_radio_init(void);
696
697 /*
698  * ao_monitor.c
699  */
700
701 extern const char const * const ao_state_names[];
702
703 #define AO_MONITOR_RING 8
704
705 union ao_monitor {
706         struct ao_telemetry_raw_recv    raw;
707         struct ao_telemetry_all_recv    all;
708         struct ao_telemetry_orig_recv   orig;
709         struct ao_telemetry_tiny_recv   tiny;
710 };
711
712 extern __xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
713
714 #define ao_monitor_ring_next(n) (((n) + 1) & (AO_MONITOR_RING - 1))
715
716 extern __data uint8_t ao_monitoring;
717 extern __data uint8_t ao_monitor_head;
718
719 void
720 ao_monitor(void);
721
722 #define AO_MONITORING_OFF       0
723 #define AO_MONITORING_ORIG      1
724
725 void
726 ao_monitor_set(uint8_t monitoring);
727
728 void
729 ao_monitor_disable(void);
730
731 void
732 ao_monitor_enable(void);
733
734 void
735 ao_monitor_init(void) __reentrant;
736
737 /*
738  * ao_stdio.c
739  */
740
741 #define AO_READ_AGAIN   ((char) -1)
742
743 struct ao_stdio {
744         char    (*pollchar)(void);
745         void    (*putchar)(char c) __reentrant;
746         void    (*flush)(void);
747         uint8_t echo;
748 };
749
750 extern __xdata struct ao_stdio ao_stdios[];
751 extern __pdata int8_t ao_cur_stdio;
752 extern __pdata int8_t ao_num_stdios;
753
754 void
755 flush(void);
756
757 extern __xdata uint8_t ao_stdin_ready;
758
759 uint8_t
760 ao_echo(void);
761
762 int8_t
763 ao_add_stdio(char (*pollchar)(void),
764              void (*putchar)(char) __reentrant,
765              void (*flush)(void)) __reentrant;
766
767 /*
768  * ao_ignite.c
769  */
770
771 enum ao_igniter {
772         ao_igniter_drogue = 0,
773         ao_igniter_main = 1
774 };
775
776 void
777 ao_ignite(enum ao_igniter igniter);
778
779 enum ao_igniter_status {
780         ao_igniter_unknown,     /* unknown status (ambiguous voltage) */
781         ao_igniter_ready,       /* continuity detected */
782         ao_igniter_active,      /* igniter firing */
783         ao_igniter_open,        /* open circuit detected */
784 };
785
786 struct ao_ignition {
787         uint8_t request;
788         uint8_t fired;
789         uint8_t firing;
790 };
791
792 extern __xdata struct ao_ignition ao_ignition[2];
793
794 enum ao_igniter_status
795 ao_igniter_status(enum ao_igniter igniter);
796
797 extern __pdata uint8_t ao_igniter_present;
798
799 void
800 ao_ignite_set_pins(void);
801
802 void
803 ao_igniter_init(void);
804
805 /*
806  * ao_config.c
807  */
808
809 #define AO_CONFIG_MAJOR 1
810 #define AO_CONFIG_MINOR 11
811
812 #define AO_AES_LEN 16
813
814 struct ao_config {
815         uint8_t         major;
816         uint8_t         minor;
817         uint16_t        main_deploy;
818         int16_t         accel_plus_g;           /* changed for minor version 2 */
819         uint8_t         _legacy_radio_channel;
820         char            callsign[AO_MAX_CALLSIGN + 1];
821         uint8_t         apogee_delay;           /* minor version 1 */
822         int16_t         accel_minus_g;          /* minor version 2 */
823         uint32_t        radio_cal;              /* minor version 3 */
824         uint32_t        flight_log_max;         /* minor version 4 */
825         uint8_t         ignite_mode;            /* minor version 5 */
826         uint8_t         pad_orientation;        /* minor version 6 */
827         uint32_t        radio_setting;          /* minor version 7 */
828         uint8_t         radio_enable;           /* minor version 8 */
829         uint8_t         aes_key[AO_AES_LEN];    /* minor version 9 */
830         uint32_t        frequency;              /* minor version 10 */
831         uint16_t        apogee_lockout;         /* minor version 11 */
832 };
833
834 #define AO_IGNITE_MODE_DUAL             0
835 #define AO_IGNITE_MODE_APOGEE           1
836 #define AO_IGNITE_MODE_MAIN             2
837
838 #define AO_PAD_ORIENTATION_ANTENNA_UP   0
839 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
840
841 extern __xdata struct ao_config ao_config;
842
843 #define AO_CONFIG_MAX_SIZE      128
844
845 void
846 ao_config_get(void);
847
848 void
849 ao_config_put(void);
850
851 void
852 ao_config_set_radio(void);
853
854 void
855 ao_config_init(void);
856
857 /*
858  * ao_rssi.c
859  */
860
861 void
862 ao_rssi_set(int rssi_value);
863
864 void
865 ao_rssi_init(uint8_t rssi_led);
866
867 /*
868  * ao_product.c
869  *
870  * values which need to be defined for
871  * each instance of a product
872  */
873
874 extern const char ao_version[];
875 extern const char ao_manufacturer[];
876 extern const char ao_product[];
877
878 /*
879  * Fifos
880  */
881
882 #define AO_FIFO_SIZE    32
883
884 struct ao_fifo {
885         uint8_t insert;
886         uint8_t remove;
887         char    fifo[AO_FIFO_SIZE];
888 };
889
890 #define ao_fifo_insert(f,c) do { \
891         (f).fifo[(f).insert] = (c); \
892         (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
893 } while(0)
894
895 #define ao_fifo_remove(f,c) do {\
896         c = (f).fifo[(f).remove]; \
897         (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
898 } while(0)
899
900 #define ao_fifo_full(f)         ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
901 #define ao_fifo_empty(f)        ((f).insert == (f).remove)
902
903 /*
904  * ao_packet.c
905  *
906  * Packet-based command interface
907  */
908
909 #define AO_PACKET_MAX           64
910 #define AO_PACKET_SYN           (uint8_t) 0xff
911
912 struct ao_packet {
913         uint8_t         addr;
914         uint8_t         len;
915         uint8_t         seq;
916         uint8_t         ack;
917         uint8_t         d[AO_PACKET_MAX];
918         uint8_t         callsign[AO_MAX_CALLSIGN];
919 };
920
921 struct ao_packet_recv {
922         struct ao_packet        packet;
923         int8_t                  rssi;
924         uint8_t                 status;
925 };
926
927 extern __xdata struct ao_packet_recv ao_rx_packet;
928 extern __xdata struct ao_packet ao_tx_packet;
929 extern __xdata struct ao_task   ao_packet_task;
930 extern __xdata uint8_t ao_packet_enable;
931 extern __xdata uint8_t ao_packet_master_sleeping;
932 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
933
934 void
935 ao_packet_send(void);
936
937 uint8_t
938 ao_packet_recv(void);
939
940 void
941 ao_packet_flush(void);
942
943 void
944 ao_packet_putchar(char c) __reentrant;
945
946 char
947 ao_packet_pollchar(void) __critical;
948
949 /* ao_packet_master.c */
950
951 void
952 ao_packet_master_init(void);
953
954 /* ao_packet_slave.c */
955
956 void
957 ao_packet_slave_start(void);
958
959 void
960 ao_packet_slave_stop(void);
961
962 void
963 ao_packet_slave_init(uint8_t enable);
964
965 /* ao_btm.c */
966
967 /* If bt_link is on P2, this interrupt is shared by USB, so the USB
968  * code calls this function. Otherwise, it's a regular ISR.
969  */
970
971 void
972 ao_btm_isr(void)
973 #if BT_LINK_ON_P1
974         __interrupt 15
975 #endif
976         ;
977
978 void
979 ao_btm_init(void);
980
981 /* ao_companion.c */
982
983 #define AO_COMPANION_SETUP              1
984 #define AO_COMPANION_FETCH              2
985 #define AO_COMPANION_NOTIFY             3
986
987 struct ao_companion_command {
988         uint8_t         command;
989         uint8_t         flight_state;
990         uint16_t        tick;
991         uint16_t        serial;
992         uint16_t        flight;
993 };
994
995 struct ao_companion_setup {
996         uint16_t        board_id;
997         uint16_t        board_id_inverse;
998         uint8_t         update_period;
999         uint8_t         channels;
1000 };
1001
1002 extern __pdata uint8_t                          ao_companion_running;
1003 extern __xdata uint8_t                          ao_companion_mutex;
1004 extern __xdata struct ao_companion_command      ao_companion_command;
1005 extern __xdata struct ao_companion_setup        ao_companion_setup;
1006 extern __xdata uint16_t                         ao_companion_data[AO_COMPANION_MAX_CHANNELS];
1007
1008 void
1009 ao_companion_init(void);
1010
1011 /* ao_lcd.c */
1012   
1013 void
1014 ao_lcd_putchar(uint8_t d);
1015
1016 void
1017 ao_lcd_putstring(char *string);
1018
1019 void
1020 ao_lcd_contrast_set(uint8_t contrast);
1021
1022 void
1023 ao_lcd_clear(void);
1024
1025 void
1026 ao_lcd_cursor_on(void);
1027
1028 void
1029 ao_lcd_cursor_off(void);
1030
1031 #define AO_LCD_ADDR(row,col)    ((row << 6) | (col))
1032
1033 void
1034 ao_lcd_goto(uint8_t addr);
1035
1036 void
1037 ao_lcd_start(void);
1038
1039 void
1040 ao_lcd_init(void);
1041
1042 /* ao_lcd_port.c */
1043
1044 void
1045 ao_lcd_port_put_nibble(uint8_t rs, uint8_t d);
1046
1047 void
1048 ao_lcd_port_init(void);
1049
1050 /* ao_aes.c */
1051
1052 extern __xdata uint8_t ao_aes_mutex;
1053
1054 /* AES keys and blocks are 128 bits */
1055
1056 enum ao_aes_mode {
1057         ao_aes_mode_cbc_mac
1058 };
1059
1060 #if HAS_AES
1061 void
1062 ao_aes_isr(void) __interrupt 4;
1063 #endif
1064
1065 void
1066 ao_aes_set_mode(enum ao_aes_mode mode);
1067
1068 void
1069 ao_aes_set_key(__xdata uint8_t *in);
1070
1071 void
1072 ao_aes_zero_iv(void);
1073
1074 void
1075 ao_aes_run(__xdata uint8_t *in,
1076            __xdata uint8_t *out);
1077
1078 void
1079 ao_aes_init(void);
1080
1081 /* ao_radio_cmac.c */
1082
1083 int8_t
1084 ao_radio_cmac_send(__xdata void *packet, uint8_t len) __reentrant;
1085
1086 #define AO_RADIO_CMAC_OK        0
1087 #define AO_RADIO_CMAC_LEN_ERROR -1
1088 #define AO_RADIO_CMAC_CRC_ERROR -2
1089 #define AO_RADIO_CMAC_MAC_ERROR -3
1090 #define AO_RADIO_CMAC_TIMEOUT   -4
1091
1092 int8_t
1093 ao_radio_cmac_recv(__xdata void *packet, uint8_t len, uint16_t timeout) __reentrant;
1094
1095 void
1096 ao_radio_cmac_init(void);
1097
1098 /* ao_launch.c */
1099
1100 struct ao_launch_command {
1101         uint16_t        tick;
1102         uint16_t        serial;
1103         uint8_t         cmd;
1104         uint8_t         channel;
1105         uint16_t        unused;
1106 };
1107
1108 #define AO_LAUNCH_QUERY         1
1109
1110 struct ao_launch_query {
1111         uint16_t        tick;
1112         uint16_t        serial;
1113         uint8_t         channel;
1114         uint8_t         valid;
1115         uint8_t         arm_status;
1116         uint8_t         igniter_status;
1117 };
1118
1119 #define AO_LAUNCH_ARM           2
1120 #define AO_LAUNCH_FIRE          3
1121
1122 void
1123 ao_launch_init(void);
1124
1125 /*
1126  * ao_log_single.c
1127  */
1128
1129 #define AO_LOG_TELESCIENCE_START        ((uint8_t) 's')
1130 #define AO_LOG_TELESCIENCE_DATA         ((uint8_t) 'd')
1131
1132 #define AO_LOG_TELESCIENCE_NUM_ADC      12
1133
1134 struct ao_log_telescience {
1135         uint8_t         type;
1136         uint8_t         csum;
1137         uint16_t        tick;
1138         uint16_t        tm_tick;
1139         uint8_t         tm_state;
1140         uint8_t         unused;
1141         uint16_t        adc[AO_LOG_TELESCIENCE_NUM_ADC];
1142 };
1143
1144 #define AO_LOG_SINGLE_SIZE              32
1145
1146 union ao_log_single {
1147         struct ao_log_telescience       telescience;
1148         union ao_telemetry_all          telemetry;
1149         uint8_t                         bytes[AO_LOG_SINGLE_SIZE];
1150 };
1151
1152 extern __xdata union ao_log_single      ao_log_single_write_data;
1153 extern __xdata union ao_log_single      ao_log_single_read_data;
1154
1155 void
1156 ao_log_single_extra_query(void);
1157
1158 void
1159 ao_log_single_list(void);
1160
1161 void
1162 ao_log_single_main(void);
1163
1164 uint8_t
1165 ao_log_single_write(void);
1166
1167 uint8_t
1168 ao_log_single_read(uint32_t pos);
1169
1170 void
1171 ao_log_single_start(void);
1172
1173 void
1174 ao_log_single_stop(void);
1175
1176 void
1177 ao_log_single_restart(void);
1178
1179 void
1180 ao_log_single_set(void);
1181
1182 void
1183 ao_log_single_delete(void);
1184
1185 void
1186 ao_log_single_init(void);
1187
1188 void
1189 ao_log_single(void);
1190
1191 /*
1192  * ao_pyro_slave.c
1193  */
1194
1195 #define AO_TELEPYRO_NUM_ADC     9
1196
1197 #ifndef ao_xmemcpy
1198 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
1199 #define ao_xmemset(d,v,c) memset(d,v,c)
1200 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
1201 #endif
1202
1203 /*
1204  * ao_terraui.c
1205  */
1206
1207 void
1208 ao_terraui_init(void);
1209
1210 /*
1211  * ao_battery.c
1212  */
1213
1214 #ifdef BATTERY_PIN
1215 void
1216 ao_battery_isr(void) ao_arch_interrupt(1);
1217
1218 uint16_t
1219 ao_battery_get(void);
1220
1221 void
1222 ao_battery_init(void);
1223 #endif /* BATTERY_PIN */
1224
1225 /*
1226  * ao_sqrt.c
1227  */
1228
1229 uint32_t
1230 ao_sqrt(uint32_t op);
1231
1232 /*
1233  * ao_freq.c
1234  */
1235
1236 int32_t ao_freq_to_set(int32_t freq, int32_t cal) __reentrant;
1237
1238 #include <ao_arch_funcs.h>
1239
1240 /*
1241  * ao_ms5607.c
1242  */
1243
1244 void ao_ms5607_init(void);
1245
1246 #endif /* _AO_H_ */