altos: clean up radio abort paths. Share radio code.
[fw/altos] / src / 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 "cc1111.h"
26 #include "ao_pins.h"
27
28 #define TRUE 1
29 #define FALSE 0
30
31 /* Convert a __data pointer into an __xdata pointer */
32 #define DATA_TO_XDATA(a)        ((void __xdata *) ((uint8_t) (a) | 0xff00))
33
34 /* Stack runs from above the allocated __data space to 0xfe, which avoids
35  * writing to 0xff as that triggers the stack overflow indicator
36  */
37 #define AO_STACK_START  0x80
38 #define AO_STACK_END    0xfe
39 #define AO_STACK_SIZE   (AO_STACK_END - AO_STACK_START + 1)
40
41 /* An AltOS task */
42 struct ao_task {
43         __xdata void *wchan;            /* current wait channel (NULL if running) */
44         uint16_t alarm;                 /* abort ao_sleep time */
45         uint8_t stack_count;            /* amount of saved stack */
46         uint8_t task_id;                /* unique id */
47         __code char *name;              /* task name */
48         uint8_t stack[AO_STACK_SIZE];   /* saved stack */
49 };
50
51 extern __xdata struct ao_task *__data ao_cur_task;
52
53 #define AO_NUM_TASKS            16      /* maximum number of tasks */
54 #define AO_NO_TASK              0       /* no task id */
55
56 /*
57  ao_task.c
58  */
59
60 /* Suspend the current task until wchan is awoken.
61  * returns:
62  *  0 on normal wake
63  *  1 on alarm
64  */
65 uint8_t
66 ao_sleep(__xdata void *wchan);
67
68 /* Wake all tasks sleeping on wchan */
69 void
70 ao_wakeup(__xdata void *wchan);
71
72 /* Wake up a specific task */
73 void
74 ao_wake_task(__xdata struct ao_task *task);
75
76 /* set an alarm to go off in 'delay' ticks */
77 void
78 ao_alarm(uint16_t delay);
79
80 /* Yield the processor to another task */
81 void
82 ao_yield(void) __naked;
83
84 /* Add a task to the run queue */
85 void
86 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
87
88 /* Terminate the current task */
89 void
90 ao_exit(void);
91
92 /* Dump task info to console */
93 void
94 ao_task_info(void);
95
96 /* Start the scheduler. This will not return */
97 void
98 ao_start_scheduler(void);
99
100 /*
101  * ao_panic.c
102  */
103
104 #define AO_PANIC_NO_TASK        1       /* AO_NUM_TASKS is not large enough */
105 #define AO_PANIC_DMA            2       /* Attempt to start DMA while active */
106 #define AO_PANIC_MUTEX          3       /* Mis-using mutex API */
107 #define AO_PANIC_EE             4       /* Mis-using eeprom API */
108 #define AO_PANIC_LOG            5       /* Failing to read/write log data */
109 #define AO_PANIC_CMD            6       /* Too many command sets registered */
110 #define AO_PANIC_STDIO          7       /* Too many stdio handlers registered */
111 #define AO_PANIC_REBOOT         8       /* Reboot failed */
112 #define AO_PANIC_FLASH          9       /* Invalid flash part (or wrong blocksize) */
113 #define AO_PANIC_USB            10      /* Trying to send USB packet while busy */
114
115 /* Stop the operating system, beeping and blinking the reason */
116 void
117 ao_panic(uint8_t reason);
118
119 /*
120  * ao_timer.c
121  */
122
123 /* Our timer runs at 100Hz */
124 #define AO_HERTZ                100
125 #define AO_MS_TO_TICKS(ms)      ((ms) / (1000 / AO_HERTZ))
126 #define AO_SEC_TO_TICKS(s)      ((s) * AO_HERTZ)
127
128 /* Returns the current time in ticks */
129 uint16_t
130 ao_time(void);
131
132 /* Suspend the current task until ticks time has passed */
133 void
134 ao_delay(uint16_t ticks);
135
136 /* Set the ADC interval */
137 void
138 ao_timer_set_adc_interval(uint8_t interval) __critical;
139
140 /* Timer interrupt */
141 void
142 ao_timer_isr(void) __interrupt 9;
143
144 /* Initialize the timer */
145 void
146 ao_timer_init(void);
147
148 /* Initialize the hardware clock. Must be called first */
149 void
150 ao_clock_init(void);
151
152 /*
153  * One set of samples read from the A/D converter or telemetry
154  */
155 struct ao_adc {
156         uint16_t        tick;           /* tick when the sample was read */
157         int16_t         accel;          /* accelerometer */
158         int16_t         pres;           /* pressure sensor */
159         int16_t         temp;           /* temperature sensor */
160         int16_t         v_batt;         /* battery voltage */
161         int16_t         sense_d;        /* drogue continuity sense */
162         int16_t         sense_m;        /* main continuity sense */
163 };
164
165 #ifndef HAS_ADC
166 #error Please define HAS_ADC
167 #endif
168
169 #if HAS_ADC
170 /*
171  * ao_adc.c
172  */
173
174 #define AO_ADC_RING     32
175 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
176 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
177
178
179 /*
180  * A/D data is stored in a ring, with the next sample to be written
181  * at ao_adc_head
182  */
183 extern volatile __xdata struct ao_adc   ao_adc_ring[AO_ADC_RING];
184 extern volatile __data uint8_t          ao_adc_head;
185
186 /* Trigger a conversion sequence (called from the timer interrupt) */
187 void
188 ao_adc_poll(void);
189
190 /* Suspend the current task until another A/D sample is converted */
191 void
192 ao_adc_sleep(void);
193
194 /* Get a copy of the last complete A/D sample set */
195 void
196 ao_adc_get(__xdata struct ao_adc *packet);
197
198 /* The A/D interrupt handler */
199
200 void
201 ao_adc_isr(void) __interrupt 1;
202
203 /* Initialize the A/D converter */
204 void
205 ao_adc_init(void);
206
207 #endif /* HAS_ADC */
208
209 /*
210  * ao_beep.c
211  */
212
213 /*
214  * Various pre-defined beep frequencies
215  *
216  * frequency = 1/2 (24e6/32) / beep
217  */
218
219 #define AO_BEEP_LOW     150     /* 2500Hz */
220 #define AO_BEEP_MID     94      /* 3989Hz */
221 #define AO_BEEP_HIGH    75      /* 5000Hz */
222 #define AO_BEEP_OFF     0       /* off */
223
224 #define AO_BEEP_g       240     /* 1562.5Hz */
225 #define AO_BEEP_gs      227     /* 1652Hz (1655Hz) */
226 #define AO_BEEP_aa      214     /* 1752Hz (1754Hz) */
227 #define AO_BEEP_bbf     202     /* 1856Hz (1858Hz) */
228 #define AO_BEEP_bb      190     /* 1974Hz (1969Hz) */
229 #define AO_BEEP_cc      180     /* 2083Hz (2086Hz) */
230 #define AO_BEEP_ccs     170     /* 2205Hz (2210Hz) */
231 #define AO_BEEP_dd      160     /* 2344Hz (2341Hz) */
232 #define AO_BEEP_eef     151     /* 2483Hz (2480Hz) */
233 #define AO_BEEP_ee      143     /* 2622Hz (2628Hz) */
234 #define AO_BEEP_ff      135     /* 2778Hz (2784Hz) */
235 #define AO_BEEP_ffs     127     /* 2953Hz (2950Hz) */
236 #define AO_BEEP_gg      120     /* 3125Hz */
237 #define AO_BEEP_ggs     113     /* 3319Hz (3311Hz) */
238 #define AO_BEEP_aaa     107     /* 3504Hz (3508Hz) */
239 #define AO_BEEP_bbbf    101     /* 3713Hz (3716Hz) */
240 #define AO_BEEP_bbb     95      /* 3947Hz (3937Hz) */
241 #define AO_BEEP_ccc     90      /* 4167Hz (4171Hz) */
242 #define AO_BEEP_cccs    85      /* 4412Hz (4419Hz) */
243 #define AO_BEEP_ddd     80      /* 4688Hz (4682Hz) */
244 #define AO_BEEP_eeef    76      /* 4934Hz (4961Hz) */
245 #define AO_BEEP_eee     71      /* 5282Hz (5256Hz) */
246 #define AO_BEEP_fff     67      /* 5597Hz (5568Hz) */
247 #define AO_BEEP_fffs    64      /* 5859Hz (5899Hz) */
248 #define AO_BEEP_ggg     60      /* 6250Hz */
249
250 /* Set the beeper to the specified tone */
251 void
252 ao_beep(uint8_t beep);
253
254 /* Turn on the beeper for the specified time */
255 void
256 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
257
258 /* Initialize the beeper */
259 void
260 ao_beep_init(void);
261
262 /*
263  * ao_led.c
264  */
265
266 #define AO_LED_NONE     0
267
268 /* Turn on the specified LEDs */
269 void
270 ao_led_on(uint8_t colors);
271
272 /* Turn off the specified LEDs */
273 void
274 ao_led_off(uint8_t colors);
275
276 /* Set all of the LEDs to the specified state */
277 void
278 ao_led_set(uint8_t colors);
279
280 /* Toggle the specified LEDs */
281 void
282 ao_led_toggle(uint8_t colors);
283
284 /* Turn on the specified LEDs for the indicated interval */
285 void
286 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
287
288 /* Initialize the LEDs */
289 void
290 ao_led_init(uint8_t enable);
291
292 /*
293  * ao_romconfig.c
294  */
295
296 #define AO_ROMCONFIG_VERSION    2
297
298 extern __code __at (0x00a0) uint16_t ao_romconfig_version;
299 extern __code __at (0x00a2) uint16_t ao_romconfig_check;
300 extern __code __at (0x00a4) uint16_t ao_serial_number;
301 extern __code __at (0x00a6) uint32_t ao_radio_cal;
302 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
303
304 /*
305  * ao_usb.c
306  */
307
308 /* Put one character to the USB output queue */
309 void
310 ao_usb_putchar(char c);
311
312 /* Get one character from the USB input queue */
313 char
314 ao_usb_getchar(void);
315
316 /* Poll for a charcter on the USB input queue.
317  * returns AO_READ_AGAIN if none are available
318  */
319 char
320 ao_usb_pollchar(void);
321
322 /* Flush the USB output queue */
323 void
324 ao_usb_flush(void);
325
326 /* USB interrupt handler */
327 void
328 ao_usb_isr(void) __interrupt 6;
329
330 /* Enable the USB controller */
331 void
332 ao_usb_enable(void);
333
334 /* Disable the USB controller */
335 void
336 ao_usb_disable(void);
337
338 /* Initialize the USB system */
339 void
340 ao_usb_init(void);
341
342 /*
343  * ao_cmd.c
344  */
345
346 enum ao_cmd_status {
347         ao_cmd_success = 0,
348         ao_cmd_lex_error = 1,
349         ao_cmd_syntax_error = 2,
350 };
351
352 extern __xdata uint16_t ao_cmd_lex_i;
353 extern __xdata uint32_t ao_cmd_lex_u32;
354 extern __xdata char     ao_cmd_lex_c;
355 extern __xdata enum ao_cmd_status ao_cmd_status;
356
357 void
358 ao_cmd_lex(void);
359
360 void
361 ao_cmd_put8(uint8_t v);
362
363 void
364 ao_cmd_put16(uint16_t v);
365
366 void
367 ao_cmd_white(void);
368
369 void
370 ao_cmd_hex(void);
371
372 void
373 ao_cmd_decimal(void);
374
375 uint8_t
376 ao_match_word(__code char *word);
377
378 struct ao_cmds {
379         char            cmd;
380         void            (*func)(void);
381         const char      *help;
382 };
383
384 void
385 ao_cmd_register(__code struct ao_cmds *cmds);
386
387 void
388 ao_cmd_init(void);
389
390 /*
391  * ao_dma.c
392  */
393
394 /* Allocate a DMA channel. the 'done' parameter will be set when the
395  * dma is finished and will be used to wakeup any waiters
396  */
397
398 uint8_t
399 ao_dma_alloc(__xdata uint8_t * done);
400
401 /* Setup a DMA channel */
402 void
403 ao_dma_set_transfer(uint8_t id,
404                     void __xdata *srcaddr,
405                     void __xdata *dstaddr,
406                     uint16_t count,
407                     uint8_t cfg0,
408                     uint8_t cfg1);
409
410 /* Start a DMA channel */
411 void
412 ao_dma_start(uint8_t id);
413
414 /* Manually trigger a DMA channel */
415 void
416 ao_dma_trigger(uint8_t id);
417
418 /* Abort a running DMA transfer */
419 void
420 ao_dma_abort(uint8_t id);
421
422 /* DMA interrupt routine */
423 void
424 ao_dma_isr(void) __interrupt 8;
425
426 /*
427  * ao_mutex.c
428  */
429
430 void
431 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
432
433 void
434 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
435
436 /*
437  * ao_ee.c
438  */
439
440 /*
441  * We reserve the last block on the device for
442  * configuration space. Writes and reads in this
443  * area return errors.
444  */
445
446 #define AO_EE_BLOCK_SIZE        ((uint16_t) (256))
447 #define AO_EE_DEVICE_SIZE       ((uint32_t) 128 * (uint32_t) 1024)
448 #define AO_EE_DATA_SIZE         (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
449 #define AO_EE_CONFIG_BLOCK      ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
450
451 void
452 ao_ee_flush(void) __reentrant;
453
454 /* Write to the eeprom */
455 uint8_t
456 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
457
458 /* Read from the eeprom */
459 uint8_t
460 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
461
462 /* Write the config block (at the end of the eeprom) */
463 uint8_t
464 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
465
466 /* Read the config block (at the end of the eeprom) */
467 uint8_t
468 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
469
470 /* Initialize the EEPROM code */
471 void
472 ao_ee_init(void);
473
474 /*
475  * ao_log.c
476  */
477
478 /*
479  * The data log is recorded in the eeprom as a sequence
480  * of data packets.
481  *
482  * Each packet starts with a 4-byte header that has the
483  * packet type, the packet checksum and the tick count. Then
484  * they all contain 2 16 bit values which hold packet-specific
485  * data.
486  *
487  * For each flight, the first packet
488  * is FLIGHT packet, indicating the serial number of the
489  * device and a unique number marking the number of flights
490  * recorded by this device.
491  *
492  * During flight, data from the accelerometer and barometer
493  * are recorded in SENSOR packets, using the raw 16-bit values
494  * read from the A/D converter.
495  *
496  * Also during flight, but at a lower rate, the deployment
497  * sensors are recorded in DEPLOY packets. The goal here is to
498  * detect failure in the deployment circuits.
499  *
500  * STATE packets hold state transitions as the flight computer
501  * transitions through different stages of the flight.
502  */
503 #define AO_LOG_FLIGHT           'F'
504 #define AO_LOG_SENSOR           'A'
505 #define AO_LOG_TEMP_VOLT        'T'
506 #define AO_LOG_DEPLOY           'D'
507 #define AO_LOG_STATE            'S'
508 #define AO_LOG_GPS_TIME         'G'
509 #define AO_LOG_GPS_LAT          'N'
510 #define AO_LOG_GPS_LON          'W'
511 #define AO_LOG_GPS_ALT          'H'
512 #define AO_LOG_GPS_SAT          'V'
513 #define AO_LOG_GPS_DATE         'Y'
514
515 #define AO_LOG_POS_NONE         (~0UL)
516
517 struct ao_log_record {
518         char                    type;
519         uint8_t                 csum;
520         uint16_t                tick;
521         union {
522                 struct {
523                         int16_t         ground_accel;
524                         uint16_t        flight;
525                 } flight;
526                 struct {
527                         int16_t         accel;
528                         int16_t         pres;
529                 } sensor;
530                 struct {
531                         int16_t         temp;
532                         int16_t         v_batt;
533                 } temp_volt;
534                 struct {
535                         int16_t         drogue;
536                         int16_t         main;
537                 } deploy;
538                 struct {
539                         uint16_t        state;
540                         uint16_t        reason;
541                 } state;
542                 struct {
543                         uint8_t         hour;
544                         uint8_t         minute;
545                         uint8_t         second;
546                         uint8_t         flags;
547                 } gps_time;
548                 int32_t         gps_latitude;
549                 int32_t         gps_longitude;
550                 struct {
551                         int16_t         altitude;
552                         uint16_t        unused;
553                 } gps_altitude;
554                 struct {
555                         uint16_t        svid;
556                         uint8_t         unused;
557                         uint8_t         c_n;
558                 } gps_sat;
559                 struct {
560                         uint8_t         year;
561                         uint8_t         month;
562                         uint8_t         day;
563                         uint8_t         extra;
564                 } gps_date;
565                 struct {
566                         uint16_t        d0;
567                         uint16_t        d1;
568                 } anon;
569         } u;
570 };
571
572 /* Write a record to the eeprom log */
573 uint8_t
574 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
575
576 /* Flush the log */
577 void
578 ao_log_flush(void);
579
580 /* We record flight numbers in the first record of
581  * the log. Tasks may wait for this to be initialized
582  * by sleeping on this variable.
583  */
584 extern __xdata uint16_t ao_flight_number;
585
586 /* Retrieve first log record for the current flight */
587 uint8_t
588 ao_log_dump_first(void);
589
590 /* return next log record for the current flight */
591 uint8_t
592 ao_log_dump_next(void);
593
594 /* Logging thread main routine */
595 void
596 ao_log(void);
597
598 /* Start logging to eeprom */
599 void
600 ao_log_start(void);
601
602 /* Stop logging */
603 void
604 ao_log_stop(void);
605
606 /* Initialize the logging system */
607 void
608 ao_log_init(void);
609
610 /*
611  * ao_flight.c
612  */
613
614 enum ao_flight_state {
615         ao_flight_startup = 0,
616         ao_flight_idle = 1,
617         ao_flight_pad = 2,
618         ao_flight_boost = 3,
619         ao_flight_fast = 4,
620         ao_flight_coast = 5,
621         ao_flight_drogue = 6,
622         ao_flight_main = 7,
623         ao_flight_landed = 8,
624         ao_flight_invalid = 9
625 };
626
627 extern __xdata struct ao_adc            ao_flight_data;
628 extern __pdata enum ao_flight_state     ao_flight_state;
629 extern __pdata uint16_t                 ao_flight_tick;
630 extern __pdata int16_t                  ao_flight_accel;
631 extern __pdata int16_t                  ao_flight_pres;
632 extern __pdata int32_t                  ao_flight_vel;
633 extern __pdata int16_t                  ao_ground_pres;
634 extern __pdata int16_t                  ao_ground_accel;
635 extern __pdata int16_t                  ao_min_pres;
636 extern __pdata uint16_t                 ao_launch_time;
637 extern __xdata uint8_t                  ao_flight_force_idle;
638
639 /* Flight thread */
640 void
641 ao_flight(void);
642
643 /* Initialize flight thread */
644 void
645 ao_flight_init(void);
646
647 /*
648  * ao_report.c
649  */
650
651 void
652 ao_report_init(void);
653
654 /*
655  * ao_convert.c
656  *
657  * Given raw data, convert to SI units
658  */
659
660 /* pressure from the sensor to altitude in meters */
661 int16_t
662 ao_pres_to_altitude(int16_t pres) __reentrant;
663
664 int16_t
665 ao_altitude_to_pres(int16_t alt) __reentrant;
666
667 int16_t
668 ao_temp_to_dC(int16_t temp) __reentrant;
669
670 /*
671  * ao_dbg.c
672  *
673  * debug another telemetrum board
674  */
675
676 /* Send a byte to the dbg target */
677 void
678 ao_dbg_send_byte(uint8_t byte);
679
680 /* Receive a byte from the dbg target */
681 uint8_t
682 ao_dbg_recv_byte(void);
683
684 /* Start a bulk transfer to/from dbg target memory */
685 void
686 ao_dbg_start_transfer(uint16_t addr);
687
688 /* End a bulk transfer to/from dbg target memory */
689 void
690 ao_dbg_end_transfer(void);
691
692 /* Write a byte to dbg target memory */
693 void
694 ao_dbg_write_byte(uint8_t byte);
695
696 /* Read a byte from dbg target memory */
697 uint8_t
698 ao_dbg_read_byte(void);
699
700 /* Enable dbg mode, switching use of the pins */
701 void
702 ao_dbg_debug_mode(void);
703
704 /* Reset the dbg target */
705 void
706 ao_dbg_reset(void);
707
708 void
709 ao_dbg_init(void);
710
711 /*
712  * ao_serial.c
713  */
714
715 #ifndef HAS_SERIAL_1
716 #error Please define HAS_SERIAL_1
717 #endif
718
719 #if HAS_SERIAL_1
720 void
721 ao_serial_rx1_isr(void) __interrupt 3;
722
723 void
724 ao_serial_tx1_isr(void) __interrupt 14;
725
726 char
727 ao_serial_getchar(void) __critical;
728
729 void
730 ao_serial_putchar(char c) __critical;
731
732 #define AO_SERIAL_SPEED_4800    0
733 #define AO_SERIAL_SPEED_9600    1
734 #define AO_SERIAL_SPEED_57600   2
735
736 void
737 ao_serial_set_speed(uint8_t speed);
738
739 void
740 ao_serial_init(void);
741 #endif
742
743 /*
744  * ao_gps.c
745  */
746
747 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
748 #define AO_GPS_NUM_SAT_SHIFT    (0)
749
750 #define AO_GPS_VALID            (1 << 4)
751 #define AO_GPS_RUNNING          (1 << 5)
752 #define AO_GPS_DATE_VALID       (1 << 6)
753
754 extern __xdata uint16_t ao_gps_tick;
755
756 struct ao_gps_data {
757         uint8_t                 year;
758         uint8_t                 month;
759         uint8_t                 day;
760         uint8_t                 hour;
761         uint8_t                 minute;
762         uint8_t                 second;
763         uint8_t                 flags;
764         int32_t                 latitude;       /* degrees * 10⁷ */
765         int32_t                 longitude;      /* degrees * 10⁷ */
766         int16_t                 altitude;       /* m */
767         uint16_t                ground_speed;   /* cm/s */
768         uint8_t                 course;         /* degrees / 2 */
769         uint8_t                 hdop;           /* * 5 */
770         int16_t                 climb_rate;     /* cm/s */
771         uint16_t                h_error;        /* m */
772         uint16_t                v_error;        /* m */
773 };
774
775 struct ao_gps_sat_data {
776         uint8_t         svid;
777         uint8_t         c_n_1;
778 };
779
780 #define AO_MAX_GPS_TRACKING     12
781
782 struct ao_gps_tracking_data {
783         uint8_t                 channels;
784         struct ao_gps_sat_data  sats[AO_MAX_GPS_TRACKING];
785 };
786
787 extern __xdata uint8_t ao_gps_mutex;
788 extern __xdata struct ao_gps_data ao_gps_data;
789 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
790
791 void
792 ao_gps(void);
793
794 void
795 ao_gps_print(__xdata struct ao_gps_data *gps_data);
796
797 void
798 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
799
800 void
801 ao_gps_init(void);
802
803 /*
804  * ao_gps_report.c
805  */
806
807 void
808 ao_gps_report(void);
809
810 void
811 ao_gps_report_init(void);
812
813 /*
814  * ao_telemetry.c
815  */
816
817 #define AO_MAX_CALLSIGN         8
818 #define AO_TELEMETRY_VERSION    3
819
820 struct ao_telemetry {
821         uint8_t                 addr;
822         uint16_t                flight;
823         uint8_t                 flight_state;
824         int16_t                 flight_accel;
825         int16_t                 ground_accel;
826         int32_t                 flight_vel;
827         int16_t                 flight_pres;
828         int16_t                 ground_pres;
829         int16_t                 accel_plus_g;
830         int16_t                 accel_minus_g;
831         struct ao_adc           adc;
832         struct ao_gps_data      gps;
833         char                    callsign[AO_MAX_CALLSIGN];
834         struct ao_gps_tracking_data     gps_tracking;
835 };
836
837 /*
838  * ao_radio_recv tacks on rssi and status bytes
839  */
840 struct ao_telemetry_recv {
841         struct ao_telemetry     telemetry;
842         int8_t                  rssi;
843         uint8_t                 status;
844 };
845
846 /* Set delay between telemetry reports (0 to disable) */
847
848 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(1000)
849 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(50)
850 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(1000)
851
852 void
853 ao_telemetry_set_interval(uint16_t interval);
854
855 void
856 ao_rdf_set(uint8_t rdf);
857
858 void
859 ao_telemetry_init(void);
860
861 /*
862  * ao_radio.c
863  */
864
865 extern __xdata uint8_t  ao_radio_dma;
866 extern __xdata uint8_t ao_radio_dma_done;
867 extern __xdata uint8_t ao_radio_done;
868 extern __xdata uint8_t ao_radio_mutex;
869
870 void
871 ao_radio_general_isr(void) __interrupt 16;
872
873 void
874 ao_radio_get(void);
875
876 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
877
878 void
879 ao_radio_set_telemetry(void);
880
881 void
882 ao_radio_set_packet(void);
883
884 void
885 ao_radio_set_rdf(void);
886
887 void
888 ao_radio_send(__xdata void *data, uint8_t size) __reentrant;
889
890 uint8_t
891 ao_radio_recv(__xdata void *data, uint8_t size) __reentrant;
892
893 void
894 ao_radio_recv_abort(void);
895
896 void
897 ao_radio_rdf(int ms);
898
899 void
900 ao_radio_rdf_abort(void);
901
902 void
903 ao_radio_idle(void);
904
905 void
906 ao_radio_init(void);
907
908 /*
909  * ao_monitor.c
910  */
911
912 extern const char const * const ao_state_names[];
913
914 void
915 ao_monitor(void);
916
917 void
918 ao_set_monitor(uint8_t monitoring);
919
920 void
921 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
922
923 /*
924  * ao_stdio.c
925  */
926
927 #define AO_READ_AGAIN   ((char) -1)
928
929 struct ao_stdio {
930         char    (*pollchar)(void);
931         void    (*putchar)(char c) __reentrant;
932         void    (*flush)(void);
933 };
934
935 void
936 flush(void);
937
938 extern __xdata uint8_t ao_stdin_ready;
939
940 void
941 ao_add_stdio(char (*pollchar)(void),
942              void (*putchar)(char) __reentrant,
943              void (*flush)(void));
944
945 /*
946  * ao_ignite.c
947  */
948
949 enum ao_igniter {
950         ao_igniter_drogue = 0,
951         ao_igniter_main = 1
952 };
953
954 void
955 ao_ignite(enum ao_igniter igniter);
956
957 enum ao_igniter_status {
958         ao_igniter_unknown,     /* unknown status (ambiguous voltage) */
959         ao_igniter_ready,       /* continuity detected */
960         ao_igniter_active,      /* igniter firing */
961         ao_igniter_open,        /* open circuit detected */
962 };
963
964 enum ao_igniter_status
965 ao_igniter_status(enum ao_igniter igniter);
966
967 void
968 ao_igniter_init(void);
969
970 /*
971  * ao_config.c
972  */
973
974 #define AO_CONFIG_MAJOR 1
975 #define AO_CONFIG_MINOR 3
976
977 struct ao_config {
978         uint8_t         major;
979         uint8_t         minor;
980         uint16_t        main_deploy;
981         int16_t         accel_plus_g;           /* changed for minor version 2 */
982         uint8_t         radio_channel;
983         char            callsign[AO_MAX_CALLSIGN + 1];
984         uint8_t         apogee_delay;           /* minor version 1 */
985         int16_t         accel_minus_g;          /* minor version 2 */
986         uint32_t        radio_cal;              /* minor version 3 */
987 };
988
989 extern __xdata struct ao_config ao_config;
990
991 void
992 ao_config_get(void);
993
994 void
995 ao_config_init(void);
996
997 /*
998  * ao_rssi.c
999  */
1000
1001 void
1002 ao_rssi_set(int rssi_value);
1003
1004 void
1005 ao_rssi_init(uint8_t rssi_led);
1006
1007 /*
1008  * ao_product.c
1009  *
1010  * values which need to be defined for
1011  * each instance of a product
1012  */
1013
1014 extern __code __at(0x00aa) uint8_t ao_usb_descriptors [];
1015 extern const char ao_version[];
1016 extern const char ao_manufacturer[];
1017 extern const char ao_product[];
1018
1019 /*
1020  * Fifos
1021  */
1022
1023 #define AO_FIFO_SIZE    32
1024
1025 struct ao_fifo {
1026         uint8_t insert;
1027         uint8_t remove;
1028         char    fifo[AO_FIFO_SIZE];
1029 };
1030
1031 #define ao_fifo_insert(f,c) do { \
1032         (f).fifo[(f).insert] = (c); \
1033         (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1034 } while(0)
1035
1036 #define ao_fifo_remove(f,c) do {\
1037         c = (f).fifo[(f).remove]; \
1038         (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1039 } while(0)
1040
1041 #define ao_fifo_full(f)         ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1042 #define ao_fifo_empty(f)        ((f).insert == (f).remove)
1043
1044 /*
1045  * ao_packet.c
1046  *
1047  * Packet-based command interface
1048  */
1049
1050 #define AO_PACKET_MAX           64
1051 #define AO_PACKET_SYN           (uint8_t) 0xff
1052
1053 struct ao_packet {
1054         uint8_t         addr;
1055         uint8_t         len;
1056         uint8_t         seq;
1057         uint8_t         ack;
1058         uint8_t         d[AO_PACKET_MAX];
1059         uint8_t         callsign[AO_MAX_CALLSIGN];
1060 };
1061
1062 struct ao_packet_recv {
1063         struct ao_packet        packet;
1064         int8_t                  rssi;
1065         uint8_t                 status;
1066 };
1067
1068 extern __xdata struct ao_packet_recv ao_rx_packet;
1069 extern __xdata struct ao_packet ao_tx_packet;
1070 extern __xdata struct ao_task   ao_packet_task;
1071 extern __xdata uint8_t ao_packet_enable;
1072 extern __xdata uint8_t ao_packet_master_sleeping;
1073 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1074
1075 void
1076 ao_packet_send(void);
1077
1078 uint8_t
1079 ao_packet_recv(void);
1080
1081 void
1082 ao_packet_flush(void);
1083
1084 void
1085 ao_packet_putchar(char c) __reentrant;
1086
1087 char
1088 ao_packet_pollchar(void) __critical;
1089
1090 /* ao_packet_master.c */
1091
1092 void
1093 ao_packet_master_init(void);
1094
1095 /* ao_packet_slave.c */
1096
1097 void
1098 ao_packet_slave_start(void);
1099
1100 void
1101 ao_packet_slave_stop(void);
1102
1103 void
1104 ao_packet_slave_init(void);
1105
1106 #endif /* _AO_H_ */