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