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