altos: get avr-demo to build. Pull in AVR drivers and LCD driver
[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
36 /* An AltOS task */
37 struct ao_task {
38         __xdata void *wchan;            /* current wait channel (NULL if running) */
39         uint16_t alarm;                 /* abort ao_sleep time */
40         uint8_t task_id;                /* unique id */
41         __code char *name;              /* task name */
42         ao_arch_task_members            /* any architecture-specific fields */
43         uint8_t stack[AO_STACK_SIZE];   /* saved stack */
44 };
45
46 extern __xdata struct ao_task *__data ao_cur_task;
47
48 #define AO_NUM_TASKS            16      /* maximum number of tasks */
49 #define AO_NO_TASK              0       /* no task id */
50
51 /*
52  ao_task.c
53  */
54
55 /* Suspend the current task until wchan is awoken.
56  * returns:
57  *  0 on normal wake
58  *  1 on alarm
59  */
60 uint8_t
61 ao_sleep(__xdata void *wchan);
62
63 /* Wake all tasks sleeping on wchan */
64 void
65 ao_wakeup(__xdata void *wchan);
66
67 /* set an alarm to go off in 'delay' ticks */
68 void
69 ao_alarm(uint16_t delay);
70
71 /* Yield the processor to another task */
72 void
73 ao_yield(void) ao_arch_naked_declare;
74
75 /* Add a task to the run queue */
76 void
77 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
78
79 /* Terminate the current task */
80 void
81 ao_exit(void);
82
83 /* Dump task info to console */
84 void
85 ao_task_info(void);
86
87 /* Start the scheduler. This will not return */
88 void
89 ao_start_scheduler(void);
90
91 /*
92  * ao_panic.c
93  */
94
95 #define AO_PANIC_NO_TASK        1       /* AO_NUM_TASKS is not large enough */
96 #define AO_PANIC_DMA            2       /* Attempt to start DMA while active */
97 #define AO_PANIC_MUTEX          3       /* Mis-using mutex API */
98 #define AO_PANIC_EE             4       /* Mis-using eeprom API */
99 #define AO_PANIC_LOG            5       /* Failing to read/write log data */
100 #define AO_PANIC_CMD            6       /* Too many command sets registered */
101 #define AO_PANIC_STDIO          7       /* Too many stdio handlers registered */
102 #define AO_PANIC_REBOOT         8       /* Reboot failed */
103 #define AO_PANIC_FLASH          9       /* Invalid flash part (or wrong blocksize) */
104 #define AO_PANIC_USB            10      /* Trying to send USB packet while busy */
105 #define AO_PANIC_BT             11      /* Communications with bluetooth device failed */
106
107 /* Stop the operating system, beeping and blinking the reason */
108 void
109 ao_panic(uint8_t reason);
110
111 /*
112  * ao_timer.c
113  */
114
115 /* Our timer runs at 100Hz */
116 #define AO_HERTZ                100
117 #define AO_MS_TO_TICKS(ms)      ((ms) / (1000 / AO_HERTZ))
118 #define AO_SEC_TO_TICKS(s)      ((s) * AO_HERTZ)
119
120 /* Returns the current time in ticks */
121 uint16_t
122 ao_time(void);
123
124 /* Suspend the current task until ticks time has passed */
125 void
126 ao_delay(uint16_t ticks);
127
128 /* Set the ADC interval */
129 void
130 ao_timer_set_adc_interval(uint8_t interval) __critical;
131
132 /* Timer interrupt */
133 void
134 ao_timer_isr(void) ao_arch_interrupt(9);
135
136 /* Initialize the timer */
137 void
138 ao_timer_init(void);
139
140 /* Initialize the hardware clock. Must be called first */
141 void
142 ao_clock_init(void);
143
144 /*
145  * One set of samples read from the A/D converter or telemetry
146  */
147 struct ao_adc {
148         uint16_t        tick;           /* tick when the sample was read */
149         int16_t         accel;          /* accelerometer */
150         int16_t         pres;           /* pressure sensor */
151         int16_t         temp;           /* temperature sensor */
152         int16_t         v_batt;         /* battery voltage */
153         int16_t         sense_d;        /* drogue continuity sense */
154         int16_t         sense_m;        /* main continuity sense */
155 };
156
157 #if HAS_ADC
158
159 /*
160  * ao_adc.c
161  */
162
163 #define AO_ADC_RING     32
164 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
165 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
166
167
168 /*
169  * A/D data is stored in a ring, with the next sample to be written
170  * at ao_adc_head
171  */
172 extern volatile __xdata struct ao_adc   ao_adc_ring[AO_ADC_RING];
173 extern volatile __data uint8_t          ao_adc_head;
174 #if HAS_ACCEL_REF
175 extern volatile __xdata uint16_t        ao_accel_ref[AO_ADC_RING];
176 #endif
177
178 /* Trigger a conversion sequence (called from the timer interrupt) */
179 void
180 ao_adc_poll(void);
181
182 /* Suspend the current task until another A/D sample is converted */
183 void
184 ao_adc_sleep(void);
185
186 /* Get a copy of the last complete A/D sample set */
187 void
188 ao_adc_get(__xdata struct ao_adc *packet);
189
190 /* The A/D interrupt handler */
191
192 void
193 ao_adc_isr(void) ao_arch_interrupt(1);
194
195 /* Initialize the A/D converter */
196 void
197 ao_adc_init(void);
198
199 #endif /* HAS_ADC */
200
201 /*
202  * ao_beep.c
203  */
204
205 /*
206  * Various pre-defined beep frequencies
207  *
208  * frequency = 1/2 (24e6/32) / beep
209  */
210
211 #define AO_BEEP_LOW     150     /* 2500Hz */
212 #define AO_BEEP_MID     94      /* 3989Hz */
213 #define AO_BEEP_HIGH    75      /* 5000Hz */
214 #define AO_BEEP_OFF     0       /* off */
215
216 #define AO_BEEP_g       240     /* 1562.5Hz */
217 #define AO_BEEP_gs      227     /* 1652Hz (1655Hz) */
218 #define AO_BEEP_aa      214     /* 1752Hz (1754Hz) */
219 #define AO_BEEP_bbf     202     /* 1856Hz (1858Hz) */
220 #define AO_BEEP_bb      190     /* 1974Hz (1969Hz) */
221 #define AO_BEEP_cc      180     /* 2083Hz (2086Hz) */
222 #define AO_BEEP_ccs     170     /* 2205Hz (2210Hz) */
223 #define AO_BEEP_dd      160     /* 2344Hz (2341Hz) */
224 #define AO_BEEP_eef     151     /* 2483Hz (2480Hz) */
225 #define AO_BEEP_ee      143     /* 2622Hz (2628Hz) */
226 #define AO_BEEP_ff      135     /* 2778Hz (2784Hz) */
227 #define AO_BEEP_ffs     127     /* 2953Hz (2950Hz) */
228 #define AO_BEEP_gg      120     /* 3125Hz */
229 #define AO_BEEP_ggs     113     /* 3319Hz (3311Hz) */
230 #define AO_BEEP_aaa     107     /* 3504Hz (3508Hz) */
231 #define AO_BEEP_bbbf    101     /* 3713Hz (3716Hz) */
232 #define AO_BEEP_bbb     95      /* 3947Hz (3937Hz) */
233 #define AO_BEEP_ccc     90      /* 4167Hz (4171Hz) */
234 #define AO_BEEP_cccs    85      /* 4412Hz (4419Hz) */
235 #define AO_BEEP_ddd     80      /* 4688Hz (4682Hz) */
236 #define AO_BEEP_eeef    76      /* 4934Hz (4961Hz) */
237 #define AO_BEEP_eee     71      /* 5282Hz (5256Hz) */
238 #define AO_BEEP_fff     67      /* 5597Hz (5568Hz) */
239 #define AO_BEEP_fffs    64      /* 5859Hz (5899Hz) */
240 #define AO_BEEP_ggg     60      /* 6250Hz */
241
242 /* Set the beeper to the specified tone */
243 void
244 ao_beep(uint8_t beep);
245
246 /* Turn on the beeper for the specified time */
247 void
248 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
249
250 /* Initialize the beeper */
251 void
252 ao_beep_init(void);
253
254 /*
255  * ao_led.c
256  */
257
258 #define AO_LED_NONE     0
259
260 /* Turn on the specified LEDs */
261 void
262 ao_led_on(uint8_t colors);
263
264 /* Turn off the specified LEDs */
265 void
266 ao_led_off(uint8_t colors);
267
268 /* Set all of the LEDs to the specified state */
269 void
270 ao_led_set(uint8_t colors);
271
272 /* Toggle the specified LEDs */
273 void
274 ao_led_toggle(uint8_t colors);
275
276 /* Turn on the specified LEDs for the indicated interval */
277 void
278 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
279
280 /* Initialize the LEDs */
281 void
282 ao_led_init(uint8_t enable);
283
284 /*
285  * ao_usb.c
286  */
287
288 /* Put one character to the USB output queue */
289 void
290 ao_usb_putchar(char c);
291
292 /* Get one character from the USB input queue */
293 char
294 ao_usb_getchar(void);
295
296 /* Poll for a charcter on the USB input queue.
297  * returns AO_READ_AGAIN if none are available
298  */
299 char
300 ao_usb_pollchar(void);
301
302 /* Flush the USB output queue */
303 void
304 ao_usb_flush(void);
305
306 #if HAS_USB
307 /* USB interrupt handler */
308 void
309 ao_usb_isr(void) ao_arch_interrupt(6);
310 #endif
311
312 /* Enable the USB controller */
313 void
314 ao_usb_enable(void);
315
316 /* Disable the USB controller */
317 void
318 ao_usb_disable(void);
319
320 /* Initialize the USB system */
321 void
322 ao_usb_init(void);
323
324 #if HAS_USB
325 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
326 #endif
327
328 /*
329  * ao_cmd.c
330  */
331
332 enum ao_cmd_status {
333         ao_cmd_success = 0,
334         ao_cmd_lex_error = 1,
335         ao_cmd_syntax_error = 2,
336 };
337
338 extern __pdata uint16_t ao_cmd_lex_i;
339 extern __pdata uint32_t ao_cmd_lex_u32;
340 extern __pdata char     ao_cmd_lex_c;
341 extern __pdata enum ao_cmd_status ao_cmd_status;
342
343 void
344 ao_cmd_lex(void);
345
346 void
347 ao_cmd_put8(uint8_t v);
348
349 void
350 ao_cmd_put16(uint16_t v);
351
352 void
353 ao_cmd_white(void);
354
355 void
356 ao_cmd_hex(void);
357
358 void
359 ao_cmd_decimal(void);
360
361 uint8_t
362 ao_match_word(__code char *word);
363
364 struct ao_cmds {
365         void            (*func)(void);
366         __code char     *help;
367 };
368
369 void
370 ao_cmd_register(__code struct ao_cmds *cmds);
371
372 void
373 ao_cmd_init(void);
374
375 #if HAS_CMD_FILTER
376 /*
377  * Provided by an external module to filter raw command lines
378  */
379 uint8_t
380 ao_cmd_filter(void);
381 #endif
382
383 /*
384  * ao_dma.c
385  */
386
387 /* Allocate a DMA channel. the 'done' parameter will be set when the
388  * dma is finished and will be used to wakeup any waiters
389  */
390
391 uint8_t
392 ao_dma_alloc(__xdata uint8_t * done);
393
394 /* Setup a DMA channel */
395 void
396 ao_dma_set_transfer(uint8_t id,
397                     void __xdata *srcaddr,
398                     void __xdata *dstaddr,
399                     uint16_t count,
400                     uint8_t cfg0,
401                     uint8_t cfg1);
402
403 /* Start a DMA channel */
404 void
405 ao_dma_start(uint8_t id);
406
407 /* Manually trigger a DMA channel */
408 void
409 ao_dma_trigger(uint8_t id);
410
411 /* Abort a running DMA transfer */
412 void
413 ao_dma_abort(uint8_t id);
414
415 /* DMA interrupt routine */
416 void
417 ao_dma_isr(void) ao_arch_interrupt(8);
418
419 /*
420  * ao_mutex.c
421  */
422
423 void
424 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
425
426 void
427 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
428
429 /*
430  * Storage interface, provided by one of the eeprom or flash
431  * drivers
432  */
433
434 /* Total bytes of available storage */
435 extern __pdata uint32_t ao_storage_total;
436
437 /* Block size - device is erased in these units. At least 256 bytes */
438 extern __pdata uint32_t ao_storage_block;
439
440 /* Byte offset of config block. Will be ao_storage_block bytes long */
441 extern __pdata uint32_t ao_storage_config;
442
443 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
444 extern __pdata uint16_t ao_storage_unit;
445
446 #define AO_STORAGE_ERASE_LOG    (ao_storage_config + AO_CONFIG_MAX_SIZE)
447
448 /* Initialize above values. Can only be called once the OS is running */
449 void
450 ao_storage_setup(void) __reentrant;
451
452 /* Write data. Returns 0 on failure, 1 on success */
453 uint8_t
454 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
455
456 /* Read data. Returns 0 on failure, 1 on success */
457 uint8_t
458 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
459
460 /* Erase a block of storage. This always clears ao_storage_block bytes */
461 uint8_t
462 ao_storage_erase(uint32_t pos) __reentrant;
463
464 /* Flush any pending writes to stable storage */
465 void
466 ao_storage_flush(void) __reentrant;
467
468 /* Initialize the storage code */
469 void
470 ao_storage_init(void);
471
472 /*
473  * Low-level functions wrapped by ao_storage.c
474  */
475
476 /* Read data within a storage unit */
477 uint8_t
478 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
479
480 /* Write data within a storage unit */
481 uint8_t
482 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
483
484 /* Initialize low-level device bits */
485 void
486 ao_storage_device_init(void);
487
488 /* Print out information about flash chips */
489 void
490 ao_storage_device_info(void) __reentrant;
491
492 /*
493  * ao_log.c
494  */
495
496 /* We record flight numbers in the first record of
497  * the log. Tasks may wait for this to be initialized
498  * by sleeping on this variable.
499  */
500 extern __xdata uint16_t ao_flight_number;
501
502 extern __pdata uint32_t ao_log_current_pos;
503 extern __pdata uint32_t ao_log_end_pos;
504 extern __pdata uint32_t ao_log_start_pos;
505 extern __xdata uint8_t  ao_log_running;
506 extern __pdata enum flight_state ao_log_state;
507
508 /* required functions from the underlying log system */
509
510 #define AO_LOG_FORMAT_UNKNOWN           0       /* unknown; altosui will have to guess */
511 #define AO_LOG_FORMAT_FULL              1       /* 8 byte typed log records */
512 #define AO_LOG_FORMAT_TINY              2       /* two byte state/baro records */
513 #define AO_LOG_FORMAT_TELEMETRY         3       /* 32 byte ao_telemetry records */
514 #define AO_LOG_FORMAT_TELESCIENCE       4       /* 32 byte typed telescience records */
515 #define AO_LOG_FORMAT_NONE              127     /* No log at all */
516
517 extern __code uint8_t ao_log_format;
518
519 /* Return the flight number from the given log slot, 0 if none */
520 uint16_t
521 ao_log_flight(uint8_t slot);
522
523 /* Flush the log */
524 void
525 ao_log_flush(void);
526
527 /* Logging thread main routine */
528 void
529 ao_log(void);
530
531 /* functions provided in ao_log.c */
532
533 /* Figure out the current flight number */
534 void
535 ao_log_scan(void) __reentrant;
536
537 /* Return the position of the start of the given log slot */
538 uint32_t
539 ao_log_pos(uint8_t slot);
540
541 /* Start logging to eeprom */
542 void
543 ao_log_start(void);
544
545 /* Stop logging */
546 void
547 ao_log_stop(void);
548
549 /* Initialize the logging system */
550 void
551 ao_log_init(void);
552
553 /* Write out the current flight number to the erase log */
554 void
555 ao_log_write_erase(uint8_t pos);
556
557 /* Returns true if there are any logs stored in eeprom */
558 uint8_t
559 ao_log_present(void);
560
561 /* Returns true if there is no more storage space available */
562 uint8_t
563 ao_log_full(void);
564
565 /*
566  * ao_log_big.c
567  */
568
569 /*
570  * The data log is recorded in the eeprom as a sequence
571  * of data packets.
572  *
573  * Each packet starts with a 4-byte header that has the
574  * packet type, the packet checksum and the tick count. Then
575  * they all contain 2 16 bit values which hold packet-specific
576  * data.
577  *
578  * For each flight, the first packet
579  * is FLIGHT packet, indicating the serial number of the
580  * device and a unique number marking the number of flights
581  * recorded by this device.
582  *
583  * During flight, data from the accelerometer and barometer
584  * are recorded in SENSOR packets, using the raw 16-bit values
585  * read from the A/D converter.
586  *
587  * Also during flight, but at a lower rate, the deployment
588  * sensors are recorded in DEPLOY packets. The goal here is to
589  * detect failure in the deployment circuits.
590  *
591  * STATE packets hold state transitions as the flight computer
592  * transitions through different stages of the flight.
593  */
594 #define AO_LOG_FLIGHT           'F'
595 #define AO_LOG_SENSOR           'A'
596 #define AO_LOG_TEMP_VOLT        'T'
597 #define AO_LOG_DEPLOY           'D'
598 #define AO_LOG_STATE            'S'
599 #define AO_LOG_GPS_TIME         'G'
600 #define AO_LOG_GPS_LAT          'N'
601 #define AO_LOG_GPS_LON          'W'
602 #define AO_LOG_GPS_ALT          'H'
603 #define AO_LOG_GPS_SAT          'V'
604 #define AO_LOG_GPS_DATE         'Y'
605
606 #define AO_LOG_POS_NONE         (~0UL)
607
608 struct ao_log_record {
609         char                    type;
610         uint8_t                 csum;
611         uint16_t                tick;
612         union {
613                 struct {
614                         int16_t         ground_accel;
615                         uint16_t        flight;
616                 } flight;
617                 struct {
618                         int16_t         accel;
619                         int16_t         pres;
620                 } sensor;
621                 struct {
622                         int16_t         temp;
623                         int16_t         v_batt;
624                 } temp_volt;
625                 struct {
626                         int16_t         drogue;
627                         int16_t         main;
628                 } deploy;
629                 struct {
630                         uint16_t        state;
631                         uint16_t        reason;
632                 } state;
633                 struct {
634                         uint8_t         hour;
635                         uint8_t         minute;
636                         uint8_t         second;
637                         uint8_t         flags;
638                 } gps_time;
639                 int32_t         gps_latitude;
640                 int32_t         gps_longitude;
641                 struct {
642                         int16_t         altitude;
643                         uint16_t        unused;
644                 } gps_altitude;
645                 struct {
646                         uint16_t        svid;
647                         uint8_t         unused;
648                         uint8_t         c_n;
649                 } gps_sat;
650                 struct {
651                         uint8_t         year;
652                         uint8_t         month;
653                         uint8_t         day;
654                         uint8_t         extra;
655                 } gps_date;
656                 struct {
657                         uint16_t        d0;
658                         uint16_t        d1;
659                 } anon;
660         } u;
661 };
662
663 /* Write a record to the eeprom log */
664 uint8_t
665 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
666
667 /*
668  * ao_flight.c
669  */
670
671 enum ao_flight_state {
672         ao_flight_startup = 0,
673         ao_flight_idle = 1,
674         ao_flight_pad = 2,
675         ao_flight_boost = 3,
676         ao_flight_fast = 4,
677         ao_flight_coast = 5,
678         ao_flight_drogue = 6,
679         ao_flight_main = 7,
680         ao_flight_landed = 8,
681         ao_flight_invalid = 9
682 };
683
684 extern __pdata enum ao_flight_state     ao_flight_state;
685
686 extern __pdata uint16_t                 ao_launch_time;
687 extern __pdata uint8_t                  ao_flight_force_idle;
688
689 /* Flight thread */
690 void
691 ao_flight(void);
692
693 /* Initialize flight thread */
694 void
695 ao_flight_init(void);
696
697 /*
698  * ao_flight_nano.c
699  */
700
701 void
702 ao_flight_nano_init(void);
703
704 /*
705  * ao_sample.c
706  */
707
708 /*
709  * Barometer calibration
710  *
711  * We directly sample the barometer. The specs say:
712  *
713  * Pressure range: 15-115 kPa
714  * Voltage at 115kPa: 2.82
715  * Output scale: 27mV/kPa
716  *
717  * If we want to detect launch with the barometer, we need
718  * a large enough bump to not be fooled by noise. At typical
719  * launch elevations (0-2000m), a 200Pa pressure change cooresponds
720  * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
721  * As all of our calculations are done in 16 bits, we'll actually see a change
722  * of 16 times this though
723  *
724  * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
725  */
726
727 /* Accelerometer calibration
728  *
729  * We're sampling the accelerometer through a resistor divider which
730  * consists of 5k and 10k resistors. This multiplies the values by 2/3.
731  * That goes into the cc1111 A/D converter, which is running at 11 bits
732  * of precision with the bits in the MSB of the 16 bit value. Only positive
733  * values are used, so values should range from 0-32752 for 0-3.3V. The
734  * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
735  * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
736  * for a final computation of:
737  *
738  * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
739  *
740  * Zero g was measured at 16000 (we would expect 16384).
741  * Note that this value is only require to tell if the
742  * rocket is standing upright. Once that is determined,
743  * the value of the accelerometer is averaged for 100 samples
744  * to find the resting accelerometer value, which is used
745  * for all further flight computations
746  */
747
748 #define GRAVITY 9.80665
749
750 /*
751  * Above this height, the baro sensor doesn't work
752  */
753 #define AO_MAX_BARO_HEIGHT      12000
754
755 /*
756  * Above this speed, baro measurements are unreliable
757  */
758 #define AO_MAX_BARO_SPEED       200
759
760 #define ACCEL_NOSE_UP   (ao_accel_2g >> 2)
761
762 /*
763  * Speed and acceleration are scaled by 16 to provide a bit more
764  * resolution while still having reasonable range. Note that this
765  * limits speed to 2047m/s (around mach 6) and acceleration to
766  * 2047m/s² (over 200g)
767  */
768
769 #define AO_M_TO_HEIGHT(m)       ((int16_t) (m))
770 #define AO_MS_TO_SPEED(ms)      ((int16_t) ((ms) * 16))
771 #define AO_MSS_TO_ACCEL(mss)    ((int16_t) ((mss) * 16))
772
773 extern __pdata uint16_t ao_sample_tick;         /* time of last data */
774 extern __pdata int16_t  ao_sample_pres;         /* most recent pressure sensor reading */
775 extern __pdata int16_t  ao_sample_alt;          /* MSL of ao_sample_pres */
776 extern __pdata int16_t  ao_sample_height;       /* AGL of ao_sample_pres */
777 extern __data uint8_t   ao_sample_adc;          /* Ring position of last processed sample */
778
779 #if HAS_ACCEL
780 extern __pdata int16_t  ao_sample_accel;        /* most recent accel sensor reading */
781 #endif
782
783 extern __pdata int16_t  ao_ground_pres;         /* startup pressure */
784 extern __pdata int16_t  ao_ground_height;       /* MSL of ao_ground_pres */
785
786 #if HAS_ACCEL
787 extern __pdata int16_t  ao_ground_accel;        /* startup acceleration */
788 extern __pdata int16_t  ao_accel_2g;            /* factory accel calibration */
789 extern __pdata int32_t  ao_accel_scale;         /* sensor to m/s² conversion */
790 #endif
791
792 void ao_sample_init(void);
793
794 /* returns FALSE in preflight mode, TRUE in flight mode */
795 uint8_t ao_sample(void);
796
797 /*
798  * ao_kalman.c
799  */
800
801 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
802 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
803 #define from_fix(x)     ((x) >> 16)
804
805 extern __pdata int16_t                  ao_height;      /* meters */
806 extern __pdata int16_t                  ao_speed;       /* m/s * 16 */
807 extern __pdata int16_t                  ao_accel;       /* m/s² * 16 */
808 extern __pdata int16_t                  ao_max_height;  /* max of ao_height */
809 extern __pdata int16_t                  ao_avg_height;  /* running average of height */
810
811 extern __pdata int16_t                  ao_error_h;
812 extern __pdata int16_t                  ao_error_h_sq_avg;
813
814 #if HAS_ACCEL
815 extern __pdata int16_t                  ao_error_a;
816 #endif
817
818 void ao_kalman(void);
819
820 /*
821  * ao_report.c
822  */
823
824 void
825 ao_report_init(void);
826
827 /*
828  * ao_convert.c
829  *
830  * Given raw data, convert to SI units
831  */
832
833 /* pressure from the sensor to altitude in meters */
834 int16_t
835 ao_pres_to_altitude(int16_t pres) __reentrant;
836
837 int16_t
838 ao_altitude_to_pres(int16_t alt) __reentrant;
839
840 int16_t
841 ao_temp_to_dC(int16_t temp) __reentrant;
842
843 /*
844  * ao_dbg.c
845  *
846  * debug another telemetrum board
847  */
848
849 /* Send a byte to the dbg target */
850 void
851 ao_dbg_send_byte(uint8_t byte);
852
853 /* Receive a byte from the dbg target */
854 uint8_t
855 ao_dbg_recv_byte(void);
856
857 /* Start a bulk transfer to/from dbg target memory */
858 void
859 ao_dbg_start_transfer(uint16_t addr);
860
861 /* End a bulk transfer to/from dbg target memory */
862 void
863 ao_dbg_end_transfer(void);
864
865 /* Write a byte to dbg target memory */
866 void
867 ao_dbg_write_byte(uint8_t byte);
868
869 /* Read a byte from dbg target memory */
870 uint8_t
871 ao_dbg_read_byte(void);
872
873 /* Enable dbg mode, switching use of the pins */
874 void
875 ao_dbg_debug_mode(void);
876
877 /* Reset the dbg target */
878 void
879 ao_dbg_reset(void);
880
881 void
882 ao_dbg_init(void);
883
884 /*
885  * ao_serial.c
886  */
887
888 #ifndef HAS_SERIAL_1
889 #error Please define HAS_SERIAL_1
890 #endif
891
892 #if HAS_SERIAL_1
893 #ifndef USE_SERIAL_STDIN
894 #error Please define USE_SERIAL_STDIN
895 #endif
896
897 void
898 ao_serial_rx1_isr(void) ao_arch_interrupt(3);
899
900 void
901 ao_serial_tx1_isr(void) ao_arch_interrupt(14);
902
903 char
904 ao_serial_getchar(void) __critical;
905
906 #if USE_SERIAL_STDIN
907 char
908 ao_serial_pollchar(void) __critical;
909
910 void
911 ao_serial_set_stdin(uint8_t stdin);
912 #endif
913
914 void
915 ao_serial_putchar(char c) __critical;
916
917 void
918 ao_serial_drain(void) __critical;
919
920 #define AO_SERIAL_SPEED_4800    0
921 #define AO_SERIAL_SPEED_9600    1
922 #define AO_SERIAL_SPEED_19200   2
923 #define AO_SERIAL_SPEED_57600   3
924
925 void
926 ao_serial_set_speed(uint8_t speed);
927
928 void
929 ao_serial_init(void);
930 #endif
931
932 /*
933  * ao_spi.c
934  */
935
936 extern __xdata uint8_t  ao_spi_mutex;
937
938 #define ao_spi_get_mask(reg,mask) do {\
939         ao_mutex_get(&ao_spi_mutex); \
940         (reg) &= ~(mask); \
941         } while (0)
942
943 #define ao_spi_put_mask(reg,mask) do { \
944         (reg) |= (mask); \
945         ao_mutex_put(&ao_spi_mutex); \
946         } while (0)
947
948 #define ao_spi_get_bit(bit) do {\
949         ao_mutex_get(&ao_spi_mutex); \
950         (bit) = 0; \
951         } while (0)
952
953 #define ao_spi_put_bit(bit) do { \
954         (bit) = 1; \
955         ao_mutex_put(&ao_spi_mutex); \
956         } while (0)
957
958 /*
959  * The SPI mutex must be held to call either of these
960  * functions -- this mutex covers the entire SPI operation,
961  * from chip select low to chip select high
962  */
963
964 void
965 ao_spi_send(void __xdata *block, uint16_t len) __reentrant;
966
967 void
968 ao_spi_recv(void __xdata *block, uint16_t len) __reentrant;
969
970 void
971 ao_spi_init(void);
972
973 /*
974  * ao_telemetry.c
975  */
976 #define AO_MAX_CALLSIGN                 8
977 #define AO_MAX_VERSION                  8
978 #define AO_MAX_TELEMETRY                128
979
980 struct ao_telemetry_generic {
981         uint16_t        serial;         /* 0 */
982         uint16_t        tick;           /* 2 */
983         uint8_t         type;           /* 4 */
984         uint8_t         payload[27];    /* 5 */
985         /* 32 */
986 };
987
988 #define AO_TELEMETRY_SENSOR_TELEMETRUM  0x01
989 #define AO_TELEMETRY_SENSOR_TELEMINI    0x02
990 #define AO_TELEMETRY_SENSOR_TELENANO    0x03
991
992 struct ao_telemetry_sensor {
993         uint16_t        serial;         /*  0 */
994         uint16_t        tick;           /*  2 */
995         uint8_t         type;           /*  4 */
996
997         uint8_t         state;          /*  5 flight state */
998         int16_t         accel;          /*  6 accelerometer (TM only) */
999         int16_t         pres;           /*  8 pressure sensor */
1000         int16_t         temp;           /* 10 temperature sensor */
1001         int16_t         v_batt;         /* 12 battery voltage */
1002         int16_t         sense_d;        /* 14 drogue continuity sense (TM/Tm) */
1003         int16_t         sense_m;        /* 16 main continuity sense (TM/Tm) */
1004
1005         int16_t         acceleration;   /* 18 m/s² * 16 */
1006         int16_t         speed;          /* 20 m/s * 16 */
1007         int16_t         height;         /* 22 m */
1008
1009         int16_t         ground_pres;    /* 24 average pres on pad */
1010         int16_t         ground_accel;   /* 26 average accel on pad */
1011         int16_t         accel_plus_g;   /* 28 accel calibration at +1g */
1012         int16_t         accel_minus_g;  /* 30 accel calibration at -1g */
1013         /* 32 */
1014 };
1015
1016 #define AO_TELEMETRY_CONFIGURATION      0x04
1017
1018 struct ao_telemetry_configuration {
1019         uint16_t        serial;                         /*  0 */
1020         uint16_t        tick;                           /*  2 */
1021         uint8_t         type;                           /*  4 */
1022
1023         uint8_t         device;                         /*  5 device type */
1024         uint16_t        flight;                         /*  6 flight number */
1025         uint8_t         config_major;                   /*  8 Config major version */
1026         uint8_t         config_minor;                   /*  9 Config minor version */
1027         uint16_t        apogee_delay;                   /* 10 Apogee deploy delay in seconds */
1028         uint16_t        main_deploy;                    /* 12 Main deploy alt in meters */
1029         uint16_t        flight_log_max;                 /* 14 Maximum flight log size in kB */
1030         char            callsign[AO_MAX_CALLSIGN];      /* 16 Radio operator identity */
1031         char            version[AO_MAX_VERSION];        /* 24 Software version */
1032         /* 32 */
1033 };
1034
1035 #define AO_TELEMETRY_LOCATION           0x05
1036
1037 #define AO_GPS_MODE_NOT_VALID           'N'
1038 #define AO_GPS_MODE_AUTONOMOUS          'A'
1039 #define AO_GPS_MODE_DIFFERENTIAL        'D'
1040 #define AO_GPS_MODE_ESTIMATED           'E'
1041 #define AO_GPS_MODE_MANUAL              'M'
1042 #define AO_GPS_MODE_SIMULATED           'S'
1043
1044 struct ao_telemetry_location {
1045         uint16_t        serial;         /*  0 */
1046         uint16_t        tick;           /*  2 */
1047         uint8_t         type;           /*  4 */
1048
1049         uint8_t         flags;          /*  5 Number of sats and other flags */
1050         int16_t         altitude;       /*  6 GPS reported altitude (m) */
1051         int32_t         latitude;       /*  8 latitude (degrees * 10⁷) */
1052         int32_t         longitude;      /* 12 longitude (degrees * 10⁷) */
1053         uint8_t         year;           /* 16 (- 2000) */
1054         uint8_t         month;          /* 17 (1-12) */
1055         uint8_t         day;            /* 18 (1-31) */
1056         uint8_t         hour;           /* 19 (0-23) */
1057         uint8_t         minute;         /* 20 (0-59) */
1058         uint8_t         second;         /* 21 (0-59) */
1059         uint8_t         pdop;           /* 22 (m * 5) */
1060         uint8_t         hdop;           /* 23 (m * 5) */
1061         uint8_t         vdop;           /* 24 (m * 5) */
1062         uint8_t         mode;           /* 25 */
1063         uint16_t        ground_speed;   /* 26 cm/s */
1064         int16_t         climb_rate;     /* 28 cm/s */
1065         uint8_t         course;         /* 30 degrees / 2 */
1066         uint8_t         unused[1];      /* 31 */
1067         /* 32 */
1068 };
1069
1070 #define AO_TELEMETRY_SATELLITE          0x06
1071
1072 struct ao_telemetry_satellite_info {
1073         uint8_t         svid;
1074         uint8_t         c_n_1;
1075 };
1076
1077 struct ao_telemetry_satellite {
1078         uint16_t                                serial;         /*  0 */
1079         uint16_t                                tick;           /*  2 */
1080         uint8_t                                 type;           /*  4 */
1081         uint8_t                                 channels;       /*  5 number of reported sats */
1082
1083         struct ao_telemetry_satellite_info      sats[12];       /* 6 */
1084         uint8_t                                 unused[2];      /* 30 */
1085         /* 32 */
1086 };
1087
1088 #define AO_TELEMETRY_COMPANION          0x07
1089
1090 #define AO_COMPANION_MAX_CHANNELS       12
1091
1092 struct ao_telemetry_companion {
1093         uint16_t                                serial;         /*  0 */
1094         uint16_t                                tick;           /*  2 */
1095         uint8_t                                 type;           /*  4 */
1096         uint8_t                                 board_id;       /*  5 */
1097
1098         uint8_t                                 update_period;  /*  6 */
1099         uint8_t                                 channels;       /*  7 */
1100         uint16_t                                companion_data[AO_COMPANION_MAX_CHANNELS];      /*  8 */
1101         /* 32 */
1102 };
1103         
1104 union ao_telemetry_all {
1105         struct ao_telemetry_generic             generic;
1106         struct ao_telemetry_sensor              sensor;
1107         struct ao_telemetry_configuration       configuration;
1108         struct ao_telemetry_location            location;
1109         struct ao_telemetry_satellite           satellite;
1110         struct ao_telemetry_companion           companion;
1111 };
1112
1113 /*
1114  * ao_gps.c
1115  */
1116
1117 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
1118 #define AO_GPS_NUM_SAT_SHIFT    (0)
1119
1120 #define AO_GPS_VALID            (1 << 4)
1121 #define AO_GPS_RUNNING          (1 << 5)
1122 #define AO_GPS_DATE_VALID       (1 << 6)
1123 #define AO_GPS_COURSE_VALID     (1 << 7)
1124
1125 extern __pdata uint16_t ao_gps_tick;
1126 extern __xdata uint8_t ao_gps_mutex;
1127 extern __xdata struct ao_telemetry_location ao_gps_data;
1128 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
1129
1130 struct ao_gps_orig {
1131         uint8_t                 year;
1132         uint8_t                 month;
1133         uint8_t                 day;
1134         uint8_t                 hour;
1135         uint8_t                 minute;
1136         uint8_t                 second;
1137         uint8_t                 flags;
1138         int32_t                 latitude;       /* degrees * 10⁷ */
1139         int32_t                 longitude;      /* degrees * 10⁷ */
1140         int16_t                 altitude;       /* m */
1141         uint16_t                ground_speed;   /* cm/s */
1142         uint8_t                 course;         /* degrees / 2 */
1143         uint8_t                 hdop;           /* * 5 */
1144         int16_t                 climb_rate;     /* cm/s */
1145         uint16_t                h_error;        /* m */
1146         uint16_t                v_error;        /* m */
1147 };
1148
1149 struct ao_gps_sat_orig {
1150         uint8_t         svid;
1151         uint8_t         c_n_1;
1152 };
1153
1154 #define AO_MAX_GPS_TRACKING     12
1155
1156 struct ao_gps_tracking_orig {
1157         uint8_t                 channels;
1158         struct ao_gps_sat_orig  sats[AO_MAX_GPS_TRACKING];
1159 };
1160
1161 void
1162 ao_gps(void);
1163
1164 void
1165 ao_gps_print(__xdata struct ao_gps_orig *gps_data);
1166
1167 void
1168 ao_gps_tracking_print(__xdata struct ao_gps_tracking_orig *gps_tracking_data);
1169
1170 void
1171 ao_gps_init(void);
1172
1173 /*
1174  * ao_gps_report.c
1175  */
1176
1177 void
1178 ao_gps_report(void);
1179
1180 void
1181 ao_gps_report_init(void);
1182
1183 /*
1184  * ao_telemetry_orig.c
1185  */
1186
1187 struct ao_telemetry_orig {
1188         uint16_t                serial;
1189         uint16_t                flight;
1190         uint8_t                 flight_state;
1191         int16_t                 accel;
1192         int16_t                 ground_accel;
1193         union {
1194                 struct {
1195                         int16_t                 speed;
1196                         int16_t                 unused;
1197                 } k;
1198                 int32_t         flight_vel;
1199         } u;
1200         int16_t                 height;
1201         int16_t                 ground_pres;
1202         int16_t                 accel_plus_g;
1203         int16_t                 accel_minus_g;
1204         struct ao_adc           adc;
1205         struct ao_gps_orig      gps;
1206         char                    callsign[AO_MAX_CALLSIGN];
1207         struct ao_gps_tracking_orig     gps_tracking;
1208 };
1209
1210 struct ao_telemetry_tiny {
1211         uint16_t                serial;
1212         uint16_t                flight;
1213         uint8_t                 flight_state;
1214         int16_t                 height;         /* AGL in meters */
1215         int16_t                 speed;          /* in m/s * 16 */
1216         int16_t                 accel;          /* in m/s² * 16 */
1217         int16_t                 ground_pres;    /* sensor units */
1218         struct ao_adc           adc;            /* raw ADC readings */
1219         char                    callsign[AO_MAX_CALLSIGN];
1220 };
1221
1222 struct ao_telemetry_orig_recv {
1223         struct ao_telemetry_orig        telemetry_orig;
1224         int8_t                          rssi;
1225         uint8_t                         status;
1226 };
1227
1228 struct ao_telemetry_tiny_recv {
1229         struct ao_telemetry_tiny        telemetry_tiny;
1230         int8_t                          rssi;
1231         uint8_t                         status;
1232 };
1233
1234 /*
1235  * ao_radio_recv tacks on rssi and status bytes
1236  */
1237
1238 struct ao_telemetry_raw_recv {
1239         uint8_t                 packet[AO_MAX_TELEMETRY + 2];
1240 };
1241
1242 /* Set delay between telemetry reports (0 to disable) */
1243
1244 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(1000)
1245 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(100)
1246 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(1000)
1247
1248 void
1249 ao_telemetry_set_interval(uint16_t interval);
1250
1251 void
1252 ao_rdf_set(uint8_t rdf);
1253
1254 void
1255 ao_telemetry_init(void);
1256
1257 void
1258 ao_telemetry_orig_init(void);
1259
1260 void
1261 ao_telemetry_tiny_init(void);
1262
1263 /*
1264  * ao_radio.c
1265  */
1266
1267 extern __xdata uint8_t  ao_radio_dma;
1268 extern __xdata uint8_t ao_radio_dma_done;
1269 extern __xdata uint8_t ao_radio_done;
1270 extern __xdata uint8_t ao_radio_mutex;
1271
1272 void
1273 ao_radio_general_isr(void) ao_arch_interrupt(16);
1274
1275 void
1276 ao_radio_get(uint8_t len);
1277
1278 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
1279
1280 void
1281 ao_radio_set_packet(void);
1282
1283 void
1284 ao_radio_send(__xdata void *data, uint8_t size) __reentrant;
1285
1286 uint8_t
1287 ao_radio_recv(__xdata void *data, uint8_t size) __reentrant;
1288
1289 void
1290 ao_radio_recv_abort(void);
1291
1292 void
1293 ao_radio_rdf(int ms);
1294
1295 void
1296 ao_radio_rdf_abort(void);
1297
1298 void
1299 ao_radio_idle(void);
1300
1301 void
1302 ao_radio_init(void);
1303
1304 /*
1305  * ao_monitor.c
1306  */
1307
1308 extern const char const * const ao_state_names[];
1309
1310 void
1311 ao_monitor(void);
1312
1313 #define AO_MONITORING_OFF       0
1314 #define AO_MONITORING_ORIG      1
1315 #define AO_MONITORING_TINY      2
1316
1317 void
1318 ao_set_monitor(uint8_t monitoring);
1319
1320 void
1321 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
1322
1323 /*
1324  * ao_stdio.c
1325  */
1326
1327 #define AO_READ_AGAIN   ((char) -1)
1328
1329 struct ao_stdio {
1330         char    (*pollchar)(void);
1331         void    (*putchar)(char c) __reentrant;
1332         void    (*flush)(void);
1333         uint8_t echo;
1334 };
1335
1336 extern __xdata struct ao_stdio ao_stdios[];
1337 extern __pdata int8_t ao_cur_stdio;
1338 extern __pdata int8_t ao_num_stdios;
1339
1340 void
1341 flush(void);
1342
1343 extern __xdata uint8_t ao_stdin_ready;
1344
1345 uint8_t
1346 ao_echo(void);
1347
1348 int8_t
1349 ao_add_stdio(char (*pollchar)(void),
1350              void (*putchar)(char) __reentrant,
1351              void (*flush)(void)) __reentrant;
1352
1353 /*
1354  * ao_ignite.c
1355  */
1356
1357 enum ao_igniter {
1358         ao_igniter_drogue = 0,
1359         ao_igniter_main = 1
1360 };
1361
1362 void
1363 ao_ignite(enum ao_igniter igniter);
1364
1365 enum ao_igniter_status {
1366         ao_igniter_unknown,     /* unknown status (ambiguous voltage) */
1367         ao_igniter_ready,       /* continuity detected */
1368         ao_igniter_active,      /* igniter firing */
1369         ao_igniter_open,        /* open circuit detected */
1370 };
1371
1372 enum ao_igniter_status
1373 ao_igniter_status(enum ao_igniter igniter);
1374
1375 void
1376 ao_ignite_set_pins(void);
1377
1378 void
1379 ao_igniter_init(void);
1380
1381 /*
1382  * ao_config.c
1383  */
1384
1385 #define AO_CONFIG_MAJOR 1
1386 #define AO_CONFIG_MINOR 8
1387
1388 struct ao_config {
1389         uint8_t         major;
1390         uint8_t         minor;
1391         uint16_t        main_deploy;
1392         int16_t         accel_plus_g;           /* changed for minor version 2 */
1393         uint8_t         radio_channel;
1394         char            callsign[AO_MAX_CALLSIGN + 1];
1395         uint8_t         apogee_delay;           /* minor version 1 */
1396         int16_t         accel_minus_g;          /* minor version 2 */
1397         uint32_t        radio_cal;              /* minor version 3 */
1398         uint32_t        flight_log_max;         /* minor version 4 */
1399         uint8_t         ignite_mode;            /* minor version 5 */
1400         uint8_t         pad_orientation;        /* minor version 6 */
1401         uint32_t        radio_setting;          /* minor version 7 */
1402         uint8_t         radio_enable;           /* minor version 8 */
1403 };
1404
1405 #define AO_IGNITE_MODE_DUAL             0
1406 #define AO_IGNITE_MODE_APOGEE           1
1407 #define AO_IGNITE_MODE_MAIN             2
1408
1409 #define AO_PAD_ORIENTATION_ANTENNA_UP   0
1410 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
1411
1412 extern __xdata struct ao_config ao_config;
1413
1414 #define AO_CONFIG_MAX_SIZE      128
1415
1416 void
1417 ao_config_get(void);
1418
1419 void
1420 ao_config_put(void);
1421
1422 void
1423 ao_config_init(void);
1424
1425 /*
1426  * ao_rssi.c
1427  */
1428
1429 void
1430 ao_rssi_set(int rssi_value);
1431
1432 void
1433 ao_rssi_init(uint8_t rssi_led);
1434
1435 /*
1436  * ao_product.c
1437  *
1438  * values which need to be defined for
1439  * each instance of a product
1440  */
1441
1442 extern const char ao_version[];
1443 extern const char ao_manufacturer[];
1444 extern const char ao_product[];
1445
1446 /*
1447  * Fifos
1448  */
1449
1450 #define AO_FIFO_SIZE    32
1451
1452 struct ao_fifo {
1453         uint8_t insert;
1454         uint8_t remove;
1455         char    fifo[AO_FIFO_SIZE];
1456 };
1457
1458 #define ao_fifo_insert(f,c) do { \
1459         (f).fifo[(f).insert] = (c); \
1460         (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1461 } while(0)
1462
1463 #define ao_fifo_remove(f,c) do {\
1464         c = (f).fifo[(f).remove]; \
1465         (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1466 } while(0)
1467
1468 #define ao_fifo_full(f)         ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1469 #define ao_fifo_empty(f)        ((f).insert == (f).remove)
1470
1471 /*
1472  * ao_packet.c
1473  *
1474  * Packet-based command interface
1475  */
1476
1477 #define AO_PACKET_MAX           64
1478 #define AO_PACKET_SYN           (uint8_t) 0xff
1479
1480 struct ao_packet {
1481         uint8_t         addr;
1482         uint8_t         len;
1483         uint8_t         seq;
1484         uint8_t         ack;
1485         uint8_t         d[AO_PACKET_MAX];
1486         uint8_t         callsign[AO_MAX_CALLSIGN];
1487 };
1488
1489 struct ao_packet_recv {
1490         struct ao_packet        packet;
1491         int8_t                  rssi;
1492         uint8_t                 status;
1493 };
1494
1495 extern __xdata struct ao_packet_recv ao_rx_packet;
1496 extern __xdata struct ao_packet ao_tx_packet;
1497 extern __xdata struct ao_task   ao_packet_task;
1498 extern __xdata uint8_t ao_packet_enable;
1499 extern __xdata uint8_t ao_packet_master_sleeping;
1500 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1501
1502 void
1503 ao_packet_send(void);
1504
1505 uint8_t
1506 ao_packet_recv(void);
1507
1508 void
1509 ao_packet_flush(void);
1510
1511 void
1512 ao_packet_putchar(char c) __reentrant;
1513
1514 char
1515 ao_packet_pollchar(void) __critical;
1516
1517 /* ao_packet_master.c */
1518
1519 void
1520 ao_packet_master_init(void);
1521
1522 /* ao_packet_slave.c */
1523
1524 void
1525 ao_packet_slave_start(void);
1526
1527 void
1528 ao_packet_slave_stop(void);
1529
1530 void
1531 ao_packet_slave_init(uint8_t enable);
1532
1533 /* ao_btm.c */
1534
1535 /* If bt_link is on P2, this interrupt is shared by USB, so the USB
1536  * code calls this function. Otherwise, it's a regular ISR.
1537  */
1538
1539 void
1540 ao_btm_isr(void)
1541 #if BT_LINK_ON_P1
1542         __interrupt 15
1543 #endif
1544         ;
1545
1546 void
1547 ao_btm_init(void);
1548
1549 /* ao_companion.c */
1550
1551 #define AO_COMPANION_SETUP              1
1552 #define AO_COMPANION_FETCH              2
1553 #define AO_COMPANION_NOTIFY             3
1554
1555 struct ao_companion_command {
1556         uint8_t         command;
1557         uint8_t         flight_state;
1558         uint16_t        tick;
1559         uint16_t        serial;
1560         uint16_t        flight;
1561 };
1562
1563 struct ao_companion_setup {
1564         uint16_t        board_id;
1565         uint16_t        board_id_inverse;
1566         uint8_t         update_period;
1567         uint8_t         channels;
1568 };
1569
1570 extern __pdata uint8_t                          ao_companion_running;
1571 extern __xdata struct ao_companion_setup        ao_companion_setup;
1572 extern __xdata uint8_t                          ao_companion_mutex;
1573 extern __xdata uint16_t                         ao_companion_data[AO_COMPANION_MAX_CHANNELS];
1574
1575 void
1576 ao_companion_init(void);
1577
1578 /* ao_lcd.c */
1579   
1580 void
1581 ao_lcd_init(void);
1582
1583 #endif /* _AO_H_ */