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