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