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