altos: Bring up basic TeleTerra v0.2 UI
[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 uint8_t
978 ao_spi_slave_recv(uint8_t *buf, uint8_t len);
979
980 void
981 ao_spi_slave_send(uint8_t *buf, uint8_t len);
982
983 void
984 ao_spi_slave_init(void);
985
986 /* This must be defined by the product; it will get called when chip
987  * select goes low, at which point it should use ao_spi_read and
988  * ao_spi_write to deal with the request
989  */
990
991 void
992 ao_spi_slave(void);
993
994 /*
995  * ao_telemetry.c
996  */
997 #define AO_MAX_CALLSIGN                 8
998 #define AO_MAX_VERSION                  8
999 #if LEGACY_MONITOR
1000 #define AO_MAX_TELEMETRY                128
1001 #else
1002 #define AO_MAX_TELEMETRY                32
1003 #endif
1004
1005 struct ao_telemetry_generic {
1006         uint16_t        serial;         /* 0 */
1007         uint16_t        tick;           /* 2 */
1008         uint8_t         type;           /* 4 */
1009         uint8_t         payload[27];    /* 5 */
1010         /* 32 */
1011 };
1012
1013 #define AO_TELEMETRY_SENSOR_TELEMETRUM  0x01
1014 #define AO_TELEMETRY_SENSOR_TELEMINI    0x02
1015 #define AO_TELEMETRY_SENSOR_TELENANO    0x03
1016
1017 struct ao_telemetry_sensor {
1018         uint16_t        serial;         /*  0 */
1019         uint16_t        tick;           /*  2 */
1020         uint8_t         type;           /*  4 */
1021
1022         uint8_t         state;          /*  5 flight state */
1023         int16_t         accel;          /*  6 accelerometer (TM only) */
1024         int16_t         pres;           /*  8 pressure sensor */
1025         int16_t         temp;           /* 10 temperature sensor */
1026         int16_t         v_batt;         /* 12 battery voltage */
1027         int16_t         sense_d;        /* 14 drogue continuity sense (TM/Tm) */
1028         int16_t         sense_m;        /* 16 main continuity sense (TM/Tm) */
1029
1030         int16_t         acceleration;   /* 18 m/s² * 16 */
1031         int16_t         speed;          /* 20 m/s * 16 */
1032         int16_t         height;         /* 22 m */
1033
1034         int16_t         ground_pres;    /* 24 average pres on pad */
1035         int16_t         ground_accel;   /* 26 average accel on pad */
1036         int16_t         accel_plus_g;   /* 28 accel calibration at +1g */
1037         int16_t         accel_minus_g;  /* 30 accel calibration at -1g */
1038         /* 32 */
1039 };
1040
1041 #define AO_TELEMETRY_CONFIGURATION      0x04
1042
1043 struct ao_telemetry_configuration {
1044         uint16_t        serial;                         /*  0 */
1045         uint16_t        tick;                           /*  2 */
1046         uint8_t         type;                           /*  4 */
1047
1048         uint8_t         device;                         /*  5 device type */
1049         uint16_t        flight;                         /*  6 flight number */
1050         uint8_t         config_major;                   /*  8 Config major version */
1051         uint8_t         config_minor;                   /*  9 Config minor version */
1052         uint16_t        apogee_delay;                   /* 10 Apogee deploy delay in seconds */
1053         uint16_t        main_deploy;                    /* 12 Main deploy alt in meters */
1054         uint16_t        flight_log_max;                 /* 14 Maximum flight log size in kB */
1055         char            callsign[AO_MAX_CALLSIGN];      /* 16 Radio operator identity */
1056         char            version[AO_MAX_VERSION];        /* 24 Software version */
1057         /* 32 */
1058 };
1059
1060 #define AO_TELEMETRY_LOCATION           0x05
1061
1062 #define AO_GPS_MODE_NOT_VALID           'N'
1063 #define AO_GPS_MODE_AUTONOMOUS          'A'
1064 #define AO_GPS_MODE_DIFFERENTIAL        'D'
1065 #define AO_GPS_MODE_ESTIMATED           'E'
1066 #define AO_GPS_MODE_MANUAL              'M'
1067 #define AO_GPS_MODE_SIMULATED           'S'
1068
1069 struct ao_telemetry_location {
1070         uint16_t        serial;         /*  0 */
1071         uint16_t        tick;           /*  2 */
1072         uint8_t         type;           /*  4 */
1073
1074         uint8_t         flags;          /*  5 Number of sats and other flags */
1075         int16_t         altitude;       /*  6 GPS reported altitude (m) */
1076         int32_t         latitude;       /*  8 latitude (degrees * 10⁷) */
1077         int32_t         longitude;      /* 12 longitude (degrees * 10⁷) */
1078         uint8_t         year;           /* 16 (- 2000) */
1079         uint8_t         month;          /* 17 (1-12) */
1080         uint8_t         day;            /* 18 (1-31) */
1081         uint8_t         hour;           /* 19 (0-23) */
1082         uint8_t         minute;         /* 20 (0-59) */
1083         uint8_t         second;         /* 21 (0-59) */
1084         uint8_t         pdop;           /* 22 (m * 5) */
1085         uint8_t         hdop;           /* 23 (m * 5) */
1086         uint8_t         vdop;           /* 24 (m * 5) */
1087         uint8_t         mode;           /* 25 */
1088         uint16_t        ground_speed;   /* 26 cm/s */
1089         int16_t         climb_rate;     /* 28 cm/s */
1090         uint8_t         course;         /* 30 degrees / 2 */
1091         uint8_t         unused[1];      /* 31 */
1092         /* 32 */
1093 };
1094
1095 #define AO_TELEMETRY_SATELLITE          0x06
1096
1097 struct ao_telemetry_satellite_info {
1098         uint8_t         svid;
1099         uint8_t         c_n_1;
1100 };
1101
1102 struct ao_telemetry_satellite {
1103         uint16_t                                serial;         /*  0 */
1104         uint16_t                                tick;           /*  2 */
1105         uint8_t                                 type;           /*  4 */
1106         uint8_t                                 channels;       /*  5 number of reported sats */
1107
1108         struct ao_telemetry_satellite_info      sats[12];       /* 6 */
1109         uint8_t                                 unused[2];      /* 30 */
1110         /* 32 */
1111 };
1112
1113 #define AO_TELEMETRY_COMPANION          0x07
1114
1115 #define AO_COMPANION_MAX_CHANNELS       12
1116
1117 struct ao_telemetry_companion {
1118         uint16_t                                serial;         /*  0 */
1119         uint16_t                                tick;           /*  2 */
1120         uint8_t                                 type;           /*  4 */
1121         uint8_t                                 board_id;       /*  5 */
1122
1123         uint8_t                                 update_period;  /*  6 */
1124         uint8_t                                 channels;       /*  7 */
1125         uint16_t                                companion_data[AO_COMPANION_MAX_CHANNELS];      /*  8 */
1126         /* 32 */
1127 };
1128         
1129 /* #define AO_SEND_ALL_BARO */
1130
1131 #define AO_TELEMETRY_BARO               0x80
1132
1133 /*
1134  * This packet allows the full sampling rate baro
1135  * data to be captured over the RF link so that the
1136  * flight software can be tested using 'real' data.
1137  *
1138  * Along with this telemetry packet, the flight
1139  * code is modified to send full-rate telemetry all the time
1140  * and never send an RDF tone; this ensure that the full radio
1141  * link is available.
1142  */
1143 struct ao_telemetry_baro {
1144         uint16_t                                serial;         /*  0 */
1145         uint16_t                                tick;           /*  2 */
1146         uint8_t                                 type;           /*  4 */
1147         uint8_t                                 samples;        /*  5 number samples */
1148
1149         int16_t                                 baro[12];       /* 6 samples */
1150         /* 32 */
1151 };
1152
1153 union ao_telemetry_all {
1154         struct ao_telemetry_generic             generic;
1155         struct ao_telemetry_sensor              sensor;
1156         struct ao_telemetry_configuration       configuration;
1157         struct ao_telemetry_location            location;
1158         struct ao_telemetry_satellite           satellite;
1159         struct ao_telemetry_companion           companion;
1160         struct ao_telemetry_baro                baro;
1161 };
1162
1163 struct ao_telemetry_all_recv {
1164         union ao_telemetry_all          telemetry;
1165         int8_t                          rssi;
1166         uint8_t                         status;
1167 };
1168
1169 /*
1170  * ao_gps.c
1171  */
1172
1173 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
1174 #define AO_GPS_NUM_SAT_SHIFT    (0)
1175
1176 #define AO_GPS_VALID            (1 << 4)
1177 #define AO_GPS_RUNNING          (1 << 5)
1178 #define AO_GPS_DATE_VALID       (1 << 6)
1179 #define AO_GPS_COURSE_VALID     (1 << 7)
1180
1181 extern __pdata uint16_t ao_gps_tick;
1182 extern __xdata uint8_t ao_gps_mutex;
1183 extern __xdata struct ao_telemetry_location ao_gps_data;
1184 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
1185
1186 struct ao_gps_orig {
1187         uint8_t                 year;
1188         uint8_t                 month;
1189         uint8_t                 day;
1190         uint8_t                 hour;
1191         uint8_t                 minute;
1192         uint8_t                 second;
1193         uint8_t                 flags;
1194         int32_t                 latitude;       /* degrees * 10⁷ */
1195         int32_t                 longitude;      /* degrees * 10⁷ */
1196         int16_t                 altitude;       /* m */
1197         uint16_t                ground_speed;   /* cm/s */
1198         uint8_t                 course;         /* degrees / 2 */
1199         uint8_t                 hdop;           /* * 5 */
1200         int16_t                 climb_rate;     /* cm/s */
1201         uint16_t                h_error;        /* m */
1202         uint16_t                v_error;        /* m */
1203 };
1204
1205 struct ao_gps_sat_orig {
1206         uint8_t         svid;
1207         uint8_t         c_n_1;
1208 };
1209
1210 #define AO_MAX_GPS_TRACKING     12
1211
1212 struct ao_gps_tracking_orig {
1213         uint8_t                 channels;
1214         struct ao_gps_sat_orig  sats[AO_MAX_GPS_TRACKING];
1215 };
1216
1217 void
1218 ao_gps(void);
1219
1220 void
1221 ao_gps_print(__xdata struct ao_gps_orig *gps_data);
1222
1223 void
1224 ao_gps_tracking_print(__xdata struct ao_gps_tracking_orig *gps_tracking_data);
1225
1226 void
1227 ao_gps_init(void);
1228
1229 /*
1230  * ao_gps_report.c
1231  */
1232
1233 void
1234 ao_gps_report(void);
1235
1236 void
1237 ao_gps_report_init(void);
1238
1239 /*
1240  * ao_telemetry_orig.c
1241  */
1242
1243 struct ao_telemetry_orig {
1244         uint16_t                serial;
1245         uint16_t                flight;
1246         uint8_t                 flight_state;
1247         int16_t                 accel;
1248         int16_t                 ground_accel;
1249         union {
1250                 struct {
1251                         int16_t                 speed;
1252                         int16_t                 unused;
1253                 } k;
1254                 int32_t         flight_vel;
1255         } u;
1256         int16_t                 height;
1257         int16_t                 ground_pres;
1258         int16_t                 accel_plus_g;
1259         int16_t                 accel_minus_g;
1260         struct ao_adc           adc;
1261         struct ao_gps_orig      gps;
1262         char                    callsign[AO_MAX_CALLSIGN];
1263         struct ao_gps_tracking_orig     gps_tracking;
1264 };
1265
1266 struct ao_telemetry_tiny {
1267         uint16_t                serial;
1268         uint16_t                flight;
1269         uint8_t                 flight_state;
1270         int16_t                 height;         /* AGL in meters */
1271         int16_t                 speed;          /* in m/s * 16 */
1272         int16_t                 accel;          /* in m/s² * 16 */
1273         int16_t                 ground_pres;    /* sensor units */
1274         struct ao_adc           adc;            /* raw ADC readings */
1275         char                    callsign[AO_MAX_CALLSIGN];
1276 };
1277
1278 struct ao_telemetry_orig_recv {
1279         struct ao_telemetry_orig        telemetry_orig;
1280         int8_t                          rssi;
1281         uint8_t                         status;
1282 };
1283
1284 struct ao_telemetry_tiny_recv {
1285         struct ao_telemetry_tiny        telemetry_tiny;
1286         int8_t                          rssi;
1287         uint8_t                         status;
1288 };
1289
1290 /*
1291  * ao_radio_recv tacks on rssi and status bytes
1292  */
1293
1294 struct ao_telemetry_raw_recv {
1295         uint8_t                 packet[AO_MAX_TELEMETRY + 2];
1296 };
1297
1298 /* Set delay between telemetry reports (0 to disable) */
1299
1300 #ifdef AO_SEND_ALL_BARO
1301 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(100)
1302 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(100)
1303 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(100)
1304 #else
1305 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(1000)
1306 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(100)
1307 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(1000)
1308 #endif
1309
1310 void
1311 ao_telemetry_set_interval(uint16_t interval);
1312
1313 void
1314 ao_rdf_set(uint8_t rdf);
1315
1316 void
1317 ao_telemetry_init(void);
1318
1319 void
1320 ao_telemetry_orig_init(void);
1321
1322 void
1323 ao_telemetry_tiny_init(void);
1324
1325 /*
1326  * ao_radio.c
1327  */
1328
1329 extern __xdata uint8_t  ao_radio_dma;
1330 extern __xdata uint8_t ao_radio_dma_done;
1331 extern __xdata uint8_t ao_radio_done;
1332 extern __xdata uint8_t ao_radio_mutex;
1333
1334 void
1335 ao_radio_general_isr(void) ao_arch_interrupt(16);
1336
1337 void
1338 ao_radio_get(uint8_t len);
1339
1340 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
1341
1342 void
1343 ao_radio_set_packet(void);
1344
1345 void
1346 ao_radio_send(__xdata void *data, uint8_t size) __reentrant;
1347
1348 uint8_t
1349 ao_radio_recv(__xdata void *data, uint8_t size) __reentrant;
1350
1351 void
1352 ao_radio_recv_abort(void);
1353
1354 /*
1355  * Compute the packet length as follows:
1356  *
1357  * 2000 bps (for a 1kHz tone)
1358  * so, for 'ms' milliseconds, we need
1359  * 2 * ms bits, or ms / 4 bytes
1360  */
1361
1362 #define AO_MS_TO_RDF_LEN(ms) ((ms) > 255 * 4 ? 255 : ((ms) >> 2))
1363
1364 void
1365 ao_radio_rdf(uint8_t pkt_len);
1366
1367 void
1368 ao_radio_rdf_abort(void);
1369
1370 void
1371 ao_radio_idle(void);
1372
1373 void
1374 ao_radio_init(void);
1375
1376 /*
1377  * ao_monitor.c
1378  */
1379
1380 extern const char const * const ao_state_names[];
1381
1382 #define AO_MONITOR_RING 8
1383
1384 union ao_monitor {
1385         struct ao_telemetry_raw_recv    raw;
1386         struct ao_telemetry_all_recv    all;
1387         struct ao_telemetry_orig_recv   orig;
1388         struct ao_telemetry_tiny_recv   tiny;
1389 };
1390
1391 extern __xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
1392
1393 #define ao_monitor_ring_next(n) (((n) + 1) & (AO_MONITOR_RING - 1))
1394
1395 extern __data uint8_t ao_monitoring;
1396 extern __data uint8_t ao_monitor_head;
1397
1398 void
1399 ao_monitor(void);
1400
1401 #define AO_MONITORING_OFF       0
1402 #define AO_MONITORING_ORIG      1
1403 #define AO_MONITORING_TINY      2
1404
1405 void
1406 ao_set_monitor(uint8_t monitoring);
1407
1408 void
1409 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
1410
1411 /*
1412  * ao_stdio.c
1413  */
1414
1415 #define AO_READ_AGAIN   ((char) -1)
1416
1417 struct ao_stdio {
1418         char    (*pollchar)(void);
1419         void    (*putchar)(char c) __reentrant;
1420         void    (*flush)(void);
1421         uint8_t echo;
1422 };
1423
1424 extern __xdata struct ao_stdio ao_stdios[];
1425 extern __pdata int8_t ao_cur_stdio;
1426 extern __pdata int8_t ao_num_stdios;
1427
1428 void
1429 flush(void);
1430
1431 extern __xdata uint8_t ao_stdin_ready;
1432
1433 uint8_t
1434 ao_echo(void);
1435
1436 int8_t
1437 ao_add_stdio(char (*pollchar)(void),
1438              void (*putchar)(char) __reentrant,
1439              void (*flush)(void)) __reentrant;
1440
1441 /*
1442  * ao_ignite.c
1443  */
1444
1445 enum ao_igniter {
1446         ao_igniter_drogue = 0,
1447         ao_igniter_main = 1
1448 };
1449
1450 void
1451 ao_ignite(enum ao_igniter igniter);
1452
1453 enum ao_igniter_status {
1454         ao_igniter_unknown,     /* unknown status (ambiguous voltage) */
1455         ao_igniter_ready,       /* continuity detected */
1456         ao_igniter_active,      /* igniter firing */
1457         ao_igniter_open,        /* open circuit detected */
1458 };
1459
1460 struct ao_ignition {
1461         uint8_t request;
1462         uint8_t fired;
1463         uint8_t firing;
1464 };
1465
1466 extern __xdata struct ao_ignition ao_ignition[2];
1467
1468 enum ao_igniter_status
1469 ao_igniter_status(enum ao_igniter igniter);
1470
1471 extern __pdata uint8_t ao_igniter_present;
1472
1473 void
1474 ao_ignite_set_pins(void);
1475
1476 void
1477 ao_igniter_init(void);
1478
1479 /*
1480  * ao_config.c
1481  */
1482
1483 #define AO_CONFIG_MAJOR 1
1484 #define AO_CONFIG_MINOR 9
1485 #define AO_AES_LEN 16
1486
1487 struct ao_config {
1488         uint8_t         major;
1489         uint8_t         minor;
1490         uint16_t        main_deploy;
1491         int16_t         accel_plus_g;           /* changed for minor version 2 */
1492         uint8_t         radio_channel;
1493         char            callsign[AO_MAX_CALLSIGN + 1];
1494         uint8_t         apogee_delay;           /* minor version 1 */
1495         int16_t         accel_minus_g;          /* minor version 2 */
1496         uint32_t        radio_cal;              /* minor version 3 */
1497         uint32_t        flight_log_max;         /* minor version 4 */
1498         uint8_t         ignite_mode;            /* minor version 5 */
1499         uint8_t         pad_orientation;        /* minor version 6 */
1500         uint32_t        radio_setting;          /* minor version 7 */
1501         uint8_t         radio_enable;           /* minor version 8 */
1502         uint8_t         aes_key[AO_AES_LEN];    /* minor version 9 */
1503 };
1504
1505 #define AO_IGNITE_MODE_DUAL             0
1506 #define AO_IGNITE_MODE_APOGEE           1
1507 #define AO_IGNITE_MODE_MAIN             2
1508
1509 #define AO_PAD_ORIENTATION_ANTENNA_UP   0
1510 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
1511
1512 extern __xdata struct ao_config ao_config;
1513
1514 #define AO_CONFIG_MAX_SIZE      128
1515
1516 void
1517 ao_config_get(void);
1518
1519 void
1520 ao_config_put(void);
1521
1522 void
1523 ao_config_init(void);
1524
1525 /*
1526  * ao_rssi.c
1527  */
1528
1529 void
1530 ao_rssi_set(int rssi_value);
1531
1532 void
1533 ao_rssi_init(uint8_t rssi_led);
1534
1535 /*
1536  * ao_product.c
1537  *
1538  * values which need to be defined for
1539  * each instance of a product
1540  */
1541
1542 extern const char ao_version[];
1543 extern const char ao_manufacturer[];
1544 extern const char ao_product[];
1545
1546 /*
1547  * Fifos
1548  */
1549
1550 #define AO_FIFO_SIZE    32
1551
1552 struct ao_fifo {
1553         uint8_t insert;
1554         uint8_t remove;
1555         char    fifo[AO_FIFO_SIZE];
1556 };
1557
1558 #define ao_fifo_insert(f,c) do { \
1559         (f).fifo[(f).insert] = (c); \
1560         (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1561 } while(0)
1562
1563 #define ao_fifo_remove(f,c) do {\
1564         c = (f).fifo[(f).remove]; \
1565         (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1566 } while(0)
1567
1568 #define ao_fifo_full(f)         ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1569 #define ao_fifo_empty(f)        ((f).insert == (f).remove)
1570
1571 /*
1572  * ao_packet.c
1573  *
1574  * Packet-based command interface
1575  */
1576
1577 #define AO_PACKET_MAX           64
1578 #define AO_PACKET_SYN           (uint8_t) 0xff
1579
1580 struct ao_packet {
1581         uint8_t         addr;
1582         uint8_t         len;
1583         uint8_t         seq;
1584         uint8_t         ack;
1585         uint8_t         d[AO_PACKET_MAX];
1586         uint8_t         callsign[AO_MAX_CALLSIGN];
1587 };
1588
1589 struct ao_packet_recv {
1590         struct ao_packet        packet;
1591         int8_t                  rssi;
1592         uint8_t                 status;
1593 };
1594
1595 extern __xdata struct ao_packet_recv ao_rx_packet;
1596 extern __xdata struct ao_packet ao_tx_packet;
1597 extern __xdata struct ao_task   ao_packet_task;
1598 extern __xdata uint8_t ao_packet_enable;
1599 extern __xdata uint8_t ao_packet_master_sleeping;
1600 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1601
1602 void
1603 ao_packet_send(void);
1604
1605 uint8_t
1606 ao_packet_recv(void);
1607
1608 void
1609 ao_packet_flush(void);
1610
1611 void
1612 ao_packet_putchar(char c) __reentrant;
1613
1614 char
1615 ao_packet_pollchar(void) __critical;
1616
1617 /* ao_packet_master.c */
1618
1619 void
1620 ao_packet_master_init(void);
1621
1622 /* ao_packet_slave.c */
1623
1624 void
1625 ao_packet_slave_start(void);
1626
1627 void
1628 ao_packet_slave_stop(void);
1629
1630 void
1631 ao_packet_slave_init(uint8_t enable);
1632
1633 /* ao_btm.c */
1634
1635 /* If bt_link is on P2, this interrupt is shared by USB, so the USB
1636  * code calls this function. Otherwise, it's a regular ISR.
1637  */
1638
1639 void
1640 ao_btm_isr(void)
1641 #if BT_LINK_ON_P1
1642         __interrupt 15
1643 #endif
1644         ;
1645
1646 void
1647 ao_btm_init(void);
1648
1649 /* ao_companion.c */
1650
1651 #define AO_COMPANION_SETUP              1
1652 #define AO_COMPANION_FETCH              2
1653 #define AO_COMPANION_NOTIFY             3
1654
1655 struct ao_companion_command {
1656         uint8_t         command;
1657         uint8_t         flight_state;
1658         uint16_t        tick;
1659         uint16_t        serial;
1660         uint16_t        flight;
1661 };
1662
1663 struct ao_companion_setup {
1664         uint16_t        board_id;
1665         uint16_t        board_id_inverse;
1666         uint8_t         update_period;
1667         uint8_t         channels;
1668 };
1669
1670 extern __pdata uint8_t                          ao_companion_running;
1671 extern __xdata uint8_t                          ao_companion_mutex;
1672 extern __xdata struct ao_companion_command      ao_companion_command;
1673 extern __xdata struct ao_companion_setup        ao_companion_setup;
1674 extern __xdata uint16_t                         ao_companion_data[AO_COMPANION_MAX_CHANNELS];
1675
1676 void
1677 ao_companion_init(void);
1678
1679 /* ao_lcd.c */
1680   
1681 void
1682 ao_lcd_putchar(uint8_t data);
1683
1684 void
1685 ao_lcd_putstring(char *string);
1686
1687 void
1688 ao_lcd_contrast_set(uint8_t contrast);
1689
1690 void
1691 ao_lcd_clear(void);
1692
1693 #define AO_LCD_ADDR(row,col)    ((row << 6) | (col))
1694
1695 void
1696 ao_lcd_goto(uint8_t addr);
1697
1698 void
1699 ao_lcd_start(void);
1700
1701 void
1702 ao_lcd_init(void);
1703
1704 /* ao_lcd_port.c */
1705
1706 void
1707 ao_lcd_port_put_nibble(uint8_t rs, uint8_t data);
1708
1709 void
1710 ao_lcd_port_init(void);
1711
1712 /* ao_aes.c */
1713
1714 __xdata uint8_t ao_aes_mutex;
1715
1716 /* AES keys and blocks are 128 bits */
1717
1718 enum ao_aes_mode {
1719         ao_aes_mode_cbc_mac
1720 };
1721
1722 #if HAS_AES
1723 void
1724 ao_aes_isr(void) __interrupt 4;
1725 #endif
1726
1727 void
1728 ao_aes_set_mode(enum ao_aes_mode mode);
1729
1730 void
1731 ao_aes_set_key(__xdata uint8_t *in);
1732
1733 void
1734 ao_aes_zero_iv(void);
1735
1736 void
1737 ao_aes_run(__xdata uint8_t *in,
1738            __xdata uint8_t *out);
1739
1740 void
1741 ao_aes_init(void);
1742
1743 /* ao_radio_cmac.c */
1744
1745 int8_t
1746 ao_radio_cmac_send(__xdata void *packet, uint8_t len) __reentrant;
1747
1748 #define AO_RADIO_CMAC_OK        0
1749 #define AO_RADIO_CMAC_LEN_ERROR -1
1750 #define AO_RADIO_CMAC_CRC_ERROR -2
1751 #define AO_RADIO_CMAC_MAC_ERROR -3
1752 #define AO_RADIO_CMAC_TIMEOUT   -4
1753
1754 int8_t
1755 ao_radio_cmac_recv(__xdata void *packet, uint8_t len, uint16_t timeout) __reentrant;
1756
1757 void
1758 ao_radio_cmac_init(void);
1759
1760 /* ao_launch.c */
1761
1762 struct ao_launch_command {
1763         uint16_t        tick;
1764         uint16_t        serial;
1765         uint8_t         cmd;
1766         uint8_t         channel;
1767         uint16_t        unused;
1768 };
1769
1770 #define AO_LAUNCH_QUERY         1
1771
1772 struct ao_launch_query {
1773         uint16_t        tick;
1774         uint16_t        serial;
1775         uint8_t         channel;
1776         uint8_t         valid;
1777         uint8_t         arm_status;
1778         uint8_t         igniter_status;
1779 };
1780
1781 #define AO_LAUNCH_ARM           2
1782 #define AO_LAUNCH_FIRE          3
1783
1784 void
1785 ao_launch_init(void);
1786
1787 /*
1788  * ao_log_single.c
1789  */
1790
1791 #define AO_LOG_TELESCIENCE_START        ((uint8_t) 's')
1792 #define AO_LOG_TELESCIENCE_DATA         ((uint8_t) 'd')
1793
1794 #define AO_LOG_TELESCIENCE_NUM_ADC      12
1795
1796 struct ao_log_telescience {
1797         uint8_t         type;
1798         uint8_t         csum;
1799         uint16_t        tick;
1800         uint16_t        tm_tick;
1801         uint8_t         tm_state;
1802         uint8_t         unused;
1803         uint16_t        adc[AO_LOG_TELESCIENCE_NUM_ADC];
1804 };
1805
1806 #define AO_LOG_SINGLE_SIZE              32
1807
1808 union ao_log_single {
1809         struct ao_log_telescience       telescience;
1810         union ao_telemetry_all          telemetry;
1811         uint8_t                         bytes[AO_LOG_SINGLE_SIZE];
1812 };
1813
1814 extern __xdata union ao_log_single      ao_log_single_write_data;
1815 extern __xdata union ao_log_single      ao_log_single_read_data;
1816
1817 void
1818 ao_log_single_extra_query(void);
1819
1820 void
1821 ao_log_single_list(void);
1822
1823 void
1824 ao_log_single_main(void);
1825
1826 uint8_t
1827 ao_log_single_write(void);
1828
1829 uint8_t
1830 ao_log_single_read(uint32_t pos);
1831
1832 void
1833 ao_log_single_start(void);
1834
1835 void
1836 ao_log_single_stop(void);
1837
1838 void
1839 ao_log_single_restart(void);
1840
1841 void
1842 ao_log_single_set(void);
1843
1844 void
1845 ao_log_single_delete(void);
1846
1847 void
1848 ao_log_single_init(void);
1849
1850 void
1851 ao_log_single(void);
1852
1853 /*
1854  * ao_pyro_slave.c
1855  */
1856
1857 #define AO_TELEPYRO_NUM_ADC     9
1858
1859 #ifndef ao_xmemcpy
1860 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
1861 #define ao_xmemset(d,v,c) memset(d,v,c)
1862 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
1863 #endif
1864
1865 /*
1866  * ao_terraui.c
1867  */
1868
1869 void
1870 ao_terraui_init(void);
1871
1872 /*
1873  * ao_battery.c
1874  */
1875
1876 #ifdef BATTERY_PIN
1877 void
1878 ao_battery_isr(void) ao_arch_interrupt(1);
1879
1880 uint16_t
1881 ao_battery_get(void);
1882
1883 void
1884 ao_battery_init(void);
1885 #endif /* BATTERY_PIN */
1886
1887 /*
1888  * ao_sqrt.c
1889  */
1890
1891 uint32_t
1892 ao_sqrt(uint32_t op);
1893
1894 #endif /* _AO_H_ */