Add date to GPS data, captured from GPRMC packet.
[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 #define AO_LOG_GPS_DATE         'Y'
497
498 #define AO_LOG_POS_NONE         (~0UL)
499
500 struct ao_log_record {
501         char                    type;
502         uint8_t                 csum;
503         uint16_t                tick;
504         union {
505                 struct {
506                         int16_t         ground_accel;
507                         uint16_t        flight;
508                 } flight;
509                 struct {
510                         int16_t         accel;
511                         int16_t         pres;
512                 } sensor;
513                 struct {
514                         int16_t         temp;
515                         int16_t         v_batt;
516                 } temp_volt;
517                 struct {
518                         int16_t         drogue;
519                         int16_t         main;
520                 } deploy;
521                 struct {
522                         uint16_t        state;
523                         uint16_t        reason;
524                 } state;
525                 struct {
526                         uint8_t         hour;
527                         uint8_t         minute;
528                         uint8_t         second;
529                         uint8_t         flags;
530                 } gps_time;
531                 int32_t         gps_latitude;
532                 int32_t         gps_longitude;
533                 struct {
534                         int16_t         altitude;
535                         uint16_t        unused;
536                 } gps_altitude;
537                 struct {
538                         uint16_t        svid;
539                         uint8_t         state;
540                         uint8_t         c_n;
541                 } gps_sat;
542                 struct {
543                         uint8_t         year;
544                         uint8_t         month;
545                         uint8_t         day;
546                 } gps_date;
547                 struct {
548                         uint16_t        d0;
549                         uint16_t        d1;
550                 } anon;
551         } u;
552 };
553
554 /* Write a record to the eeprom log */
555 void
556 ao_log_data(struct ao_log_record *log);
557
558 /* Flush the log */
559 void
560 ao_log_flush(void);
561
562 /* We record flight numbers in the first record of
563  * the log. Tasks may wait for this to be initialized
564  * by sleeping on this variable.
565  */
566 extern __xdata uint16_t ao_flight_number;
567
568 /* Retrieve first log record for the current flight */
569 uint8_t
570 ao_log_dump_first(void);
571
572 /* return next log record for the current flight */
573 uint8_t
574 ao_log_dump_next(void);
575
576 /* Logging thread main routine */
577 void
578 ao_log(void);
579
580 /* Start logging to eeprom */
581 void
582 ao_log_start(void);
583
584 /* Stop logging */
585 void
586 ao_log_stop(void);
587
588 /* Initialize the logging system */
589 void
590 ao_log_init(void);
591
592 /*
593  * ao_flight.c
594  */
595
596 enum ao_flight_state {
597         ao_flight_startup = 0,
598         ao_flight_idle = 1,
599         ao_flight_pad = 2,
600         ao_flight_boost = 3,
601         ao_flight_fast = 4,
602         ao_flight_coast = 5,
603         ao_flight_drogue = 6,
604         ao_flight_main = 7,
605         ao_flight_landed = 8,
606         ao_flight_invalid = 9
607 };
608
609 extern __xdata struct ao_adc            ao_flight_data;
610 extern __pdata enum ao_flight_state     ao_flight_state;
611 extern __pdata uint16_t                 ao_flight_tick;
612 extern __pdata int16_t                  ao_flight_accel;
613 extern __pdata int16_t                  ao_flight_pres;
614 extern __pdata int32_t                  ao_flight_vel;
615 extern __pdata int16_t                  ao_ground_pres;
616 extern __pdata int16_t                  ao_ground_accel;
617 extern __pdata int16_t                  ao_min_pres;
618 extern __pdata uint16_t                 ao_launch_time;
619
620 /* Flight thread */
621 void
622 ao_flight(void);
623
624 /* Initialize flight thread */
625 void
626 ao_flight_init(void);
627
628 /*
629  * ao_report.c
630  */
631
632 void
633 ao_report_init(void);
634
635 /*
636  * ao_convert.c
637  *
638  * Given raw data, convert to SI units
639  */
640
641 /* pressure from the sensor to altitude in meters */
642 int16_t
643 ao_pres_to_altitude(int16_t pres) __reentrant;
644
645 int16_t
646 ao_altitude_to_pres(int16_t alt) __reentrant;
647
648 int16_t
649 ao_temp_to_dC(int16_t temp) __reentrant;
650
651 /*
652  * ao_dbg.c
653  *
654  * debug another telemetrum board
655  */
656
657 /* Send a byte to the dbg target */
658 void
659 ao_dbg_send_byte(uint8_t byte);
660
661 /* Receive a byte from the dbg target */
662 uint8_t
663 ao_dbg_recv_byte(void);
664
665 /* Start a bulk transfer to/from dbg target memory */
666 void
667 ao_dbg_start_transfer(uint16_t addr);
668
669 /* End a bulk transfer to/from dbg target memory */
670 void
671 ao_dbg_end_transfer(void);
672
673 /* Write a byte to dbg target memory */
674 void
675 ao_dbg_write_byte(uint8_t byte);
676
677 /* Read a byte from dbg target memory */
678 uint8_t
679 ao_dbg_read_byte(void);
680
681 /* Enable dbg mode, switching use of the pins */
682 void
683 ao_dbg_debug_mode(void);
684
685 /* Reset the dbg target */
686 void
687 ao_dbg_reset(void);
688
689 void
690 ao_dbg_init(void);
691
692 /*
693  * ao_serial.c
694  */
695
696 #if !AO_NO_SERIAL_ISR
697 void
698 ao_serial_rx1_isr(void) interrupt 3;
699
700 void
701 ao_serial_tx1_isr(void) interrupt 14;
702 #endif
703
704 char
705 ao_serial_getchar(void) __critical;
706
707 void
708 ao_serial_putchar(char c) __critical;
709
710 #define AO_SERIAL_SPEED_4800    0
711 #define AO_SERIAL_SPEED_9600    1
712 #define AO_SERIAL_SPEED_57600   2
713
714 void
715 ao_serial_set_speed(uint8_t speed);
716
717 void
718 ao_serial_init(void);
719
720 /*
721  * ao_gps.c
722  */
723
724 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
725 #define AO_GPS_NUM_SAT_SHIFT    (0)
726
727 #define AO_GPS_VALID            (1 << 4)
728 #define AO_GPS_RUNNING          (1 << 5)
729 #define AO_GPS_DATE_VALID       (1 << 6)
730
731 struct ao_gps_data {
732         uint8_t                 year;
733         uint8_t                 month;
734         uint8_t                 day;
735         uint8_t                 hour;
736         uint8_t                 minute;
737         uint8_t                 second;
738         uint8_t                 flags;
739         int32_t                 latitude;       /* degrees * 10⁷ */
740         int32_t                 longitude;      /* degrees * 10⁷ */
741         int16_t                 altitude;       /* m */
742         uint16_t                ground_speed;   /* cm/s */
743         uint8_t                 course;         /* degrees / 2 */
744         uint8_t                 hdop;           /* * 5 */
745         int16_t                 climb_rate;     /* cm/s */
746         uint16_t                h_error;        /* m */
747         uint16_t                v_error;        /* m */
748 };
749
750 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
751 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
752 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
753 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
754 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
755 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
756 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
757 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
758
759 struct ao_gps_sat_data {
760         uint8_t         svid;
761         uint8_t         state;
762         uint8_t         c_n_1;
763 };
764
765 struct ao_gps_tracking_data {
766         uint8_t                 channels;
767         struct ao_gps_sat_data  sats[12];
768 };
769
770 extern __xdata uint8_t ao_gps_mutex;
771 extern __xdata struct ao_gps_data ao_gps_data;
772 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
773
774 void
775 ao_gps(void);
776
777 void
778 ao_gps_print(__xdata struct ao_gps_data *gps_data);
779
780 void
781 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
782
783 void
784 ao_gps_init(void);
785
786 /*
787  * ao_gps_report.c
788  */
789
790 void
791 ao_gps_report(void);
792
793 void
794 ao_gps_report_init(void);
795
796 /*
797  * ao_telemetry.c
798  */
799
800 #define AO_MAX_CALLSIGN         8
801 #define AO_TELEMETRY_VERSION    2
802
803 struct ao_telemetry {
804         uint8_t                 addr;
805         uint16_t                flight;
806         uint8_t                 flight_state;
807         int16_t                 flight_accel;
808         int16_t                 ground_accel;
809         int32_t                 flight_vel;
810         int16_t                 flight_pres;
811         int16_t                 ground_pres;
812         int16_t                 accel_plus_g;
813         int16_t                 accel_minus_g;
814         struct ao_adc           adc;
815         struct ao_gps_data      gps;
816         char                    callsign[AO_MAX_CALLSIGN];
817         struct ao_gps_tracking_data     gps_tracking;
818 };
819
820 /* Set delay between telemetry reports (0 to disable) */
821
822 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(1000)
823 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(50)
824 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(1000)
825
826 void
827 ao_telemetry_set_interval(uint16_t interval);
828
829 void
830 ao_rdf_set(uint8_t rdf);
831
832 void
833 ao_telemetry_init(void);
834
835 /*
836  * ao_radio.c
837  */
838
839 extern __xdata uint8_t  ao_radio_dma;
840 extern __xdata uint8_t ao_radio_dma_done;
841 extern __xdata uint8_t ao_radio_done;
842 extern __xdata uint8_t ao_radio_mutex;
843
844 void
845 ao_radio_general_isr(void) interrupt 16;
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 2
951
952 struct ao_config {
953         uint8_t         major;
954         uint8_t         minor;
955         uint16_t        main_deploy;
956         int16_t         accel_plus_g;
957         uint8_t         radio_channel;
958         char            callsign[AO_MAX_CALLSIGN + 1];
959         uint8_t         apogee_delay;
960         int16_t         accel_minus_g;
961 };
962
963 extern __xdata struct ao_config ao_config;
964
965 void
966 ao_config_get(void);
967
968 void
969 ao_config_init(void);
970
971 /*
972  * ao_rssi.c
973  */
974
975 void
976 ao_rssi_set(int rssi_value);
977
978 void
979 ao_rssi_init(uint8_t rssi_led);
980
981 /*
982  * ao_product.c
983  *
984  * values which need to be defined for
985  * each instance of a product
986  */
987
988 extern const uint8_t ao_usb_descriptors [];
989 extern const uint16_t ao_serial_number;
990 extern const char ao_version[];
991 extern const char ao_manufacturer[];
992 extern const char ao_product[];
993
994 /*
995  * Fifos
996  */
997
998 #define AO_FIFO_SIZE    32
999
1000 struct ao_fifo {
1001         uint8_t insert;
1002         uint8_t remove;
1003         char    fifo[AO_FIFO_SIZE];
1004 };
1005
1006 #define ao_fifo_insert(f,c) do { \
1007         (f).fifo[(f).insert] = (c); \
1008         (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1009 } while(0)
1010
1011 #define ao_fifo_remove(f,c) do {\
1012         c = (f).fifo[(f).remove]; \
1013         (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1014 } while(0)
1015
1016 #define ao_fifo_full(f)         ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1017 #define ao_fifo_empty(f)        ((f).insert == (f).remove)
1018
1019 /*
1020  * ao_packet.c
1021  *
1022  * Packet-based command interface
1023  */
1024
1025 #define AO_PACKET_MAX   8
1026 #define AO_PACKET_SYN           (uint8_t) 0xff
1027
1028 struct ao_packet {
1029         uint8_t         addr;
1030         uint8_t         len;
1031         uint8_t         seq;
1032         uint8_t         ack;
1033         uint8_t         d[AO_PACKET_MAX];
1034 };
1035
1036 struct ao_packet_recv {
1037         struct ao_packet        packet;
1038         int8_t                  rssi;
1039         uint8_t                 status;
1040 };
1041
1042 extern __xdata struct ao_packet_recv ao_rx_packet;
1043 extern __xdata struct ao_packet ao_tx_packet;
1044 extern __xdata struct ao_task   ao_packet_task;
1045 extern __xdata uint8_t ao_packet_enable;
1046 extern __xdata uint8_t ao_packet_master_sleeping;
1047 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1048
1049 void
1050 ao_packet_send(void);
1051
1052 uint8_t
1053 ao_packet_recv(void);
1054
1055 void
1056 ao_packet_flush(void);
1057
1058 void
1059 ao_packet_putchar(char c) __reentrant;
1060
1061 char
1062 ao_packet_pollchar(void) __critical;
1063
1064 /* ao_packet_master.c */
1065
1066 void
1067 ao_packet_master_init(void);
1068
1069 /* ao_packet_slave.c */
1070
1071 void
1072 ao_packet_slave_start(void);
1073
1074 void
1075 ao_packet_slave_stop(void);
1076
1077 void
1078 ao_packet_slave_init(void);
1079
1080 #endif /* _AO_H_ */