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