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