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