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