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