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