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