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