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