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