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