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