Add two-point accelerometer calibration.
[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 struct ao_cmds {
354         char            cmd;
355         void            (*func)(void);
356         const char      *help;
357 };
358
359 void
360 ao_cmd_register(__code struct ao_cmds *cmds);
361
362 void
363 ao_cmd_init(void);
364
365 /*
366  * ao_dma.c
367  */
368
369 /* Allocate a DMA channel. the 'done' parameter will be set
370  * when the dma is finished or aborted and will be used to
371  * wakeup any waiters
372  */
373
374 #define AO_DMA_DONE     1
375 #define AO_DMA_ABORTED  2
376
377 uint8_t
378 ao_dma_alloc(__xdata uint8_t * done);
379
380 /* Setup a DMA channel */
381 void
382 ao_dma_set_transfer(uint8_t id,
383                     void __xdata *srcaddr,
384                     void __xdata *dstaddr,
385                     uint16_t count,
386                     uint8_t cfg0,
387                     uint8_t cfg1);
388
389 /* Start a DMA channel */
390 void
391 ao_dma_start(uint8_t id);
392
393 /* Manually trigger a DMA channel */
394 void
395 ao_dma_trigger(uint8_t id);
396
397 /* Abort a running DMA transfer */
398 void
399 ao_dma_abort(uint8_t id);
400
401 /* DMA interrupt routine */
402 void
403 ao_dma_isr(void) interrupt 8;
404
405 /*
406  * ao_mutex.c
407  */
408
409 void
410 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
411
412 void
413 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
414
415 /*
416  * ao_ee.c
417  */
418
419 /*
420  * We reserve the last block on the device for
421  * configuration space. Writes and reads in this
422  * area return errors.
423  */
424
425 #define AO_EE_BLOCK_SIZE        ((uint16_t) (256))
426 #define AO_EE_DEVICE_SIZE       ((uint32_t) 128 * (uint32_t) 1024)
427 #define AO_EE_DATA_SIZE         (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
428 #define AO_EE_CONFIG_BLOCK      ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
429
430 void
431 ao_ee_flush(void) __reentrant;
432
433 /* Write to the eeprom */
434 uint8_t
435 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
436
437 /* Read from the eeprom */
438 uint8_t
439 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
440
441 /* Write the config block (at the end of the eeprom) */
442 uint8_t
443 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
444
445 /* Read the config block (at the end of the eeprom) */
446 uint8_t
447 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
448
449 /* Initialize the EEPROM code */
450 void
451 ao_ee_init(void);
452
453 /*
454  * ao_log.c
455  */
456
457 /*
458  * The data log is recorded in the eeprom as a sequence
459  * of data packets.
460  *
461  * Each packet starts with a 4-byte header that has the
462  * packet type, the packet checksum and the tick count. Then
463  * they all contain 2 16 bit values which hold packet-specific
464  * data.
465  *
466  * For each flight, the first packet
467  * is FLIGHT packet, indicating the serial number of the
468  * device and a unique number marking the number of flights
469  * recorded by this device.
470  *
471  * During flight, data from the accelerometer and barometer
472  * are recorded in SENSOR packets, using the raw 16-bit values
473  * read from the A/D converter.
474  *
475  * Also during flight, but at a lower rate, the deployment
476  * sensors are recorded in DEPLOY packets. The goal here is to
477  * detect failure in the deployment circuits.
478  *
479  * STATE packets hold state transitions as the flight computer
480  * transitions through different stages of the flight.
481  */
482 #define AO_LOG_FLIGHT           'F'
483 #define AO_LOG_SENSOR           'A'
484 #define AO_LOG_TEMP_VOLT        'T'
485 #define AO_LOG_DEPLOY           'D'
486 #define AO_LOG_STATE            'S'
487 #define AO_LOG_GPS_TIME         'G'
488 #define AO_LOG_GPS_LAT          'N'
489 #define AO_LOG_GPS_LON          'W'
490 #define AO_LOG_GPS_ALT          'H'
491 #define AO_LOG_GPS_SAT          'V'
492
493 #define AO_LOG_POS_NONE         (~0UL)
494
495 struct ao_log_record {
496         char                    type;
497         uint8_t                 csum;
498         uint16_t                tick;
499         union {
500                 struct {
501                         int16_t         ground_accel;
502                         uint16_t        flight;
503                 } flight;
504                 struct {
505                         int16_t         accel;
506                         int16_t         pres;
507                 } sensor;
508                 struct {
509                         int16_t         temp;
510                         int16_t         v_batt;
511                 } temp_volt;
512                 struct {
513                         int16_t         drogue;
514                         int16_t         main;
515                 } deploy;
516                 struct {
517                         uint16_t        state;
518                         uint16_t        reason;
519                 } state;
520                 struct {
521                         uint8_t         hour;
522                         uint8_t         minute;
523                         uint8_t         second;
524                         uint8_t         flags;
525                 } gps_time;
526                 int32_t         gps_latitude;
527                 int32_t         gps_longitude;
528                 struct {
529                         int16_t         altitude;
530                         uint16_t        unused;
531                 } gps_altitude;
532                 struct {
533                         uint16_t        svid;
534                         uint8_t         state;
535                         uint8_t         c_n;
536                 } gps_sat;
537                 struct {
538                         uint16_t        d0;
539                         uint16_t        d1;
540                 } anon;
541         } u;
542 };
543
544 /* Write a record to the eeprom log */
545 void
546 ao_log_data(struct ao_log_record *log);
547
548 /* Flush the log */
549 void
550 ao_log_flush(void);
551
552 /* Log dumping API:
553  * ao_log_dump_first() - get first log record
554  * ao_log_dump_next()  - get next log record
555  */
556 extern __xdata struct ao_log_record ao_log_dump;
557
558 /* Retrieve first log record for the current flight */
559 uint8_t
560 ao_log_dump_first(void);
561
562 /* return next log record for the current flight */
563 uint8_t
564 ao_log_dump_next(void);
565
566 /* Logging thread main routine */
567 void
568 ao_log(void);
569
570 /* Start logging to eeprom */
571 void
572 ao_log_start(void);
573
574 /* Stop logging */
575 void
576 ao_log_stop(void);
577
578 /* Initialize the logging system */
579 void
580 ao_log_init(void);
581
582 /*
583  * ao_flight.c
584  */
585
586 enum ao_flight_state {
587         ao_flight_startup = 0,
588         ao_flight_idle = 1,
589         ao_flight_pad = 2,
590         ao_flight_boost = 3,
591         ao_flight_fast = 4,
592         ao_flight_coast = 5,
593         ao_flight_drogue = 6,
594         ao_flight_main = 7,
595         ao_flight_landed = 8,
596         ao_flight_invalid = 9
597 };
598
599 extern __xdata struct ao_adc            ao_flight_data;
600 extern __pdata enum ao_flight_state     ao_flight_state;
601 extern __pdata uint16_t                 ao_flight_tick;
602 extern __pdata int16_t                  ao_flight_accel;
603 extern __pdata int16_t                  ao_flight_pres;
604 extern __pdata int32_t                  ao_flight_vel;
605 extern __pdata int16_t                  ao_ground_pres;
606 extern __pdata int16_t                  ao_ground_accel;
607 extern __pdata int16_t                  ao_min_pres;
608 extern __pdata uint16_t                 ao_launch_time;
609
610 /* Flight thread */
611 void
612 ao_flight(void);
613
614 /* Initialize flight thread */
615 void
616 ao_flight_init(void);
617
618 /*
619  * ao_report.c
620  */
621
622 void
623 ao_report_init(void);
624
625 /*
626  * ao_convert.c
627  *
628  * Given raw data, convert to SI units
629  */
630
631 /* pressure from the sensor to altitude in meters */
632 int16_t
633 ao_pres_to_altitude(int16_t pres) __reentrant;
634
635 int16_t
636 ao_altitude_to_pres(int16_t alt) __reentrant;
637
638 int16_t
639 ao_temp_to_dC(int16_t temp) __reentrant;
640
641 /*
642  * ao_dbg.c
643  *
644  * debug another telemetrum board
645  */
646
647 /* Send a byte to the dbg target */
648 void
649 ao_dbg_send_byte(uint8_t byte);
650
651 /* Receive a byte from the dbg target */
652 uint8_t
653 ao_dbg_recv_byte(void);
654
655 /* Start a bulk transfer to/from dbg target memory */
656 void
657 ao_dbg_start_transfer(uint16_t addr);
658
659 /* End a bulk transfer to/from dbg target memory */
660 void
661 ao_dbg_end_transfer(void);
662
663 /* Write a byte to dbg target memory */
664 void
665 ao_dbg_write_byte(uint8_t byte);
666
667 /* Read a byte from dbg target memory */
668 uint8_t
669 ao_dbg_read_byte(void);
670
671 /* Enable dbg mode, switching use of the pins */
672 void
673 ao_dbg_debug_mode(void);
674
675 /* Reset the dbg target */
676 void
677 ao_dbg_reset(void);
678
679 void
680 ao_dbg_init(void);
681
682 /*
683  * ao_serial.c
684  */
685
686 #if !AO_NO_SERIAL_ISR
687 void
688 ao_serial_rx1_isr(void) interrupt 3;
689
690 void
691 ao_serial_tx1_isr(void) interrupt 14;
692 #endif
693
694 char
695 ao_serial_getchar(void) __critical;
696
697 void
698 ao_serial_putchar(char c) __critical;
699
700 #define AO_SERIAL_SPEED_4800    0
701 #define AO_SERIAL_SPEED_9600    1
702 #define AO_SERIAL_SPEED_57600   2
703
704 void
705 ao_serial_set_speed(uint8_t speed);
706
707 void
708 ao_serial_init(void);
709
710 /*
711  * ao_gps.c
712  */
713
714 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
715 #define AO_GPS_NUM_SAT_SHIFT    (0)
716
717 #define AO_GPS_VALID            (1 << 4)
718 #define AO_GPS_RUNNING          (1 << 5)
719
720 struct ao_gps_data {
721         uint8_t                 hour;
722         uint8_t                 minute;
723         uint8_t                 second;
724         uint8_t                 flags;
725         int32_t                 latitude;       /* degrees * 10⁷ */
726         int32_t                 longitude;      /* degrees * 10⁷ */
727         int16_t                 altitude;       /* m */
728         uint16_t                ground_speed;   /* cm/s */
729         uint8_t                 course;         /* degrees / 2 */
730         uint8_t                 hdop;           /* * 5 */
731         int16_t                 climb_rate;     /* cm/s */
732         uint16_t                h_error;        /* m */
733         uint16_t                v_error;        /* m */
734 };
735
736 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
737 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
738 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
739 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
740 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
741 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
742 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
743 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
744
745 struct ao_gps_sat_data {
746         uint8_t         svid;
747         uint8_t         state;
748         uint8_t         c_n_1;
749 };
750
751 struct ao_gps_tracking_data {
752         uint8_t                 channels;
753         struct ao_gps_sat_data  sats[12];
754 };
755
756 extern __xdata uint8_t ao_gps_mutex;
757 extern __xdata struct ao_gps_data ao_gps_data;
758 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
759
760 void
761 ao_gps(void);
762
763 void
764 ao_gps_print(__xdata struct ao_gps_data *gps_data);
765
766 void
767 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
768
769 void
770 ao_gps_init(void);
771
772 /*
773  * ao_gps_report.c
774  */
775
776 void
777 ao_gps_report(void);
778
779 void
780 ao_gps_report_init(void);
781
782 /*
783  * ao_telemetry.c
784  */
785
786 #define AO_MAX_CALLSIGN         8
787
788 struct ao_telemetry {
789         uint8_t                 addr;
790         uint8_t                 flight_state;
791         int16_t                 flight_accel;
792         int16_t                 ground_accel;
793         int32_t                 flight_vel;
794         int16_t                 flight_pres;
795         int16_t                 ground_pres;
796         struct ao_adc           adc;
797         struct ao_gps_data      gps;
798         char                    callsign[AO_MAX_CALLSIGN];
799         struct ao_gps_tracking_data     gps_tracking;
800 };
801
802 /* Set delay between telemetry reports (0 to disable) */
803
804 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(1000)
805 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(50)
806 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(1000)
807
808 void
809 ao_telemetry_set_interval(uint16_t interval);
810
811 void
812 ao_rdf_set(uint8_t rdf);
813
814 void
815 ao_telemetry_init(void);
816
817 /*
818  * ao_radio.c
819  */
820
821 extern __xdata uint8_t  ao_radio_dma;
822 extern __xdata uint8_t ao_radio_dma_done;
823 extern __xdata uint8_t ao_radio_done;
824 extern __xdata uint8_t ao_radio_mutex;
825
826 void
827 ao_radio_general_isr(void) interrupt 16;
828
829 void
830 ao_radio_set_telemetry(void);
831
832 void
833 ao_radio_set_packet(void);
834
835 void
836 ao_radio_set_rdf(void);
837
838 void
839 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant;
840
841 struct ao_radio_recv {
842         struct ao_telemetry     telemetry;
843         int8_t                  rssi;
844         uint8_t                 status;
845 };
846
847 uint8_t
848 ao_radio_recv(__xdata struct ao_radio_recv *recv) __reentrant;
849
850 void
851 ao_radio_rdf(int ms);
852
853 void
854 ao_radio_abort(void);
855
856 void
857 ao_radio_rdf_abort(void);
858
859 void
860 ao_radio_idle(void);
861
862 void
863 ao_radio_init(void);
864
865 /*
866  * ao_monitor.c
867  */
868
869 extern const char const * const ao_state_names[];
870
871 void
872 ao_monitor(void);
873
874 void
875 ao_set_monitor(uint8_t monitoring);
876
877 void
878 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
879
880 /*
881  * ao_stdio.c
882  */
883
884 #define AO_READ_AGAIN   ((char) -1)
885
886 struct ao_stdio {
887         char    (*pollchar)(void);
888         void    (*putchar)(char c) __reentrant;
889         void    (*flush)(void);
890 };
891
892 void
893 flush(void);
894
895 extern __xdata uint8_t ao_stdin_ready;
896
897 void
898 ao_add_stdio(char (*pollchar)(void),
899              void (*putchar)(char) __reentrant,
900              void (*flush)(void));
901
902 /*
903  * ao_ignite.c
904  */
905
906 enum ao_igniter {
907         ao_igniter_drogue = 0,
908         ao_igniter_main = 1
909 };
910
911 void
912 ao_ignite(enum ao_igniter igniter);
913
914 enum ao_igniter_status {
915         ao_igniter_unknown,     /* unknown status (ambiguous voltage) */
916         ao_igniter_ready,       /* continuity detected */
917         ao_igniter_active,      /* igniter firing */
918         ao_igniter_open,        /* open circuit detected */
919 };
920
921 enum ao_igniter_status
922 ao_igniter_status(enum ao_igniter igniter);
923
924 void
925 ao_igniter_init(void);
926
927 /*
928  * ao_config.c
929  */
930
931 #define AO_CONFIG_MAJOR 1
932 #define AO_CONFIG_MINOR 2
933
934 struct ao_config {
935         uint8_t         major;
936         uint8_t         minor;
937         uint16_t        main_deploy;
938         int16_t         accel_plus_g;
939         uint8_t         radio_channel;
940         char            callsign[AO_MAX_CALLSIGN + 1];
941         uint8_t         apogee_delay;
942         int16_t         accel_minus_g;
943 };
944
945 extern __xdata struct ao_config ao_config;
946
947 void
948 ao_config_get(void);
949
950 void
951 ao_config_init(void);
952
953 /*
954  * ao_rssi.c
955  */
956
957 void
958 ao_rssi_set(int rssi_value);
959
960 void
961 ao_rssi_init(uint8_t rssi_led);
962
963 /*
964  * ao_product.c
965  *
966  * values which need to be defined for
967  * each instance of a product
968  */
969
970 extern const uint8_t ao_usb_descriptors [];
971 extern const uint16_t ao_serial_number;
972 extern const char ao_version[];
973 extern const char ao_manufacturer[];
974 extern const char ao_product[];
975
976 /*
977  * Fifos
978  */
979
980 #define AO_FIFO_SIZE    32
981
982 struct ao_fifo {
983         uint8_t insert;
984         uint8_t remove;
985         char    fifo[AO_FIFO_SIZE];
986 };
987
988 #define ao_fifo_insert(f,c) do { \
989         (f).fifo[(f).insert] = (c); \
990         (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
991 } while(0)
992
993 #define ao_fifo_remove(f,c) do {\
994         c = (f).fifo[(f).remove]; \
995         (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
996 } while(0)
997
998 #define ao_fifo_full(f)         ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
999 #define ao_fifo_empty(f)        ((f).insert == (f).remove)
1000
1001 /*
1002  * ao_packet.c
1003  *
1004  * Packet-based command interface
1005  */
1006
1007 #define AO_PACKET_MAX   8
1008 #define AO_PACKET_SYN           (uint8_t) 0xff
1009
1010 struct ao_packet {
1011         uint8_t         addr;
1012         uint8_t         len;
1013         uint8_t         seq;
1014         uint8_t         ack;
1015         uint8_t         d[AO_PACKET_MAX];
1016 };
1017
1018 struct ao_packet_recv {
1019         struct ao_packet        packet;
1020         int8_t                  rssi;
1021         uint8_t                 status;
1022 };
1023
1024 extern __xdata struct ao_packet_recv ao_rx_packet;
1025 extern __xdata struct ao_packet ao_tx_packet;
1026 extern __xdata struct ao_task   ao_packet_task;
1027 extern __xdata uint8_t ao_packet_enable;
1028 extern __xdata uint8_t ao_packet_master_sleeping;
1029 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1030
1031 void
1032 ao_packet_send(void);
1033
1034 uint8_t
1035 ao_packet_recv(void);
1036
1037 void
1038 ao_packet_flush(void);
1039
1040 void
1041 ao_packet_putchar(char c) __reentrant;
1042
1043 char
1044 ao_packet_pollchar(void) __critical;
1045
1046 /* ao_packet_master.c */
1047
1048 void
1049 ao_packet_master_init(void);
1050
1051 /* ao_packet_slave.c */
1052
1053 void
1054 ao_packet_slave_start(void);
1055
1056 void
1057 ao_packet_slave_stop(void);
1058
1059 void
1060 ao_packet_slave_init(void);
1061
1062 #endif /* _AO_H_ */