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