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