altos: Split up flight code into separate flight/sample/kalman bits
[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  0x90
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 #if HAS_ACCEL
168 #ifndef HAS_ACCEL_REF
169 #error Please define HAS_ACCEL_REF
170 #endif
171 #else
172 #define HAS_ACCEL_REF 0
173 #endif
174
175 /*
176  * ao_adc.c
177  */
178
179 #define AO_ADC_RING     32
180 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
181 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
182
183
184 /*
185  * A/D data is stored in a ring, with the next sample to be written
186  * at ao_adc_head
187  */
188 extern volatile __xdata struct ao_adc   ao_adc_ring[AO_ADC_RING];
189 extern volatile __data uint8_t          ao_adc_head;
190 #if HAS_ACCEL_REF
191 extern volatile __xdata uint16_t        ao_accel_ref[AO_ADC_RING];
192 #endif
193
194 /* Trigger a conversion sequence (called from the timer interrupt) */
195 void
196 ao_adc_poll(void);
197
198 /* Suspend the current task until another A/D sample is converted */
199 void
200 ao_adc_sleep(void);
201
202 /* Get a copy of the last complete A/D sample set */
203 void
204 ao_adc_get(__xdata struct ao_adc *packet);
205
206 /* The A/D interrupt handler */
207
208 void
209 ao_adc_isr(void) __interrupt 1;
210
211 /* Initialize the A/D converter */
212 void
213 ao_adc_init(void);
214
215 #endif /* HAS_ADC */
216
217 /*
218  * ao_beep.c
219  */
220
221 /*
222  * Various pre-defined beep frequencies
223  *
224  * frequency = 1/2 (24e6/32) / beep
225  */
226
227 #define AO_BEEP_LOW     150     /* 2500Hz */
228 #define AO_BEEP_MID     94      /* 3989Hz */
229 #define AO_BEEP_HIGH    75      /* 5000Hz */
230 #define AO_BEEP_OFF     0       /* off */
231
232 #define AO_BEEP_g       240     /* 1562.5Hz */
233 #define AO_BEEP_gs      227     /* 1652Hz (1655Hz) */
234 #define AO_BEEP_aa      214     /* 1752Hz (1754Hz) */
235 #define AO_BEEP_bbf     202     /* 1856Hz (1858Hz) */
236 #define AO_BEEP_bb      190     /* 1974Hz (1969Hz) */
237 #define AO_BEEP_cc      180     /* 2083Hz (2086Hz) */
238 #define AO_BEEP_ccs     170     /* 2205Hz (2210Hz) */
239 #define AO_BEEP_dd      160     /* 2344Hz (2341Hz) */
240 #define AO_BEEP_eef     151     /* 2483Hz (2480Hz) */
241 #define AO_BEEP_ee      143     /* 2622Hz (2628Hz) */
242 #define AO_BEEP_ff      135     /* 2778Hz (2784Hz) */
243 #define AO_BEEP_ffs     127     /* 2953Hz (2950Hz) */
244 #define AO_BEEP_gg      120     /* 3125Hz */
245 #define AO_BEEP_ggs     113     /* 3319Hz (3311Hz) */
246 #define AO_BEEP_aaa     107     /* 3504Hz (3508Hz) */
247 #define AO_BEEP_bbbf    101     /* 3713Hz (3716Hz) */
248 #define AO_BEEP_bbb     95      /* 3947Hz (3937Hz) */
249 #define AO_BEEP_ccc     90      /* 4167Hz (4171Hz) */
250 #define AO_BEEP_cccs    85      /* 4412Hz (4419Hz) */
251 #define AO_BEEP_ddd     80      /* 4688Hz (4682Hz) */
252 #define AO_BEEP_eeef    76      /* 4934Hz (4961Hz) */
253 #define AO_BEEP_eee     71      /* 5282Hz (5256Hz) */
254 #define AO_BEEP_fff     67      /* 5597Hz (5568Hz) */
255 #define AO_BEEP_fffs    64      /* 5859Hz (5899Hz) */
256 #define AO_BEEP_ggg     60      /* 6250Hz */
257
258 /* Set the beeper to the specified tone */
259 void
260 ao_beep(uint8_t beep);
261
262 /* Turn on the beeper for the specified time */
263 void
264 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
265
266 /* Initialize the beeper */
267 void
268 ao_beep_init(void);
269
270 /*
271  * ao_led.c
272  */
273
274 #define AO_LED_NONE     0
275
276 /* Turn on the specified LEDs */
277 void
278 ao_led_on(uint8_t colors);
279
280 /* Turn off the specified LEDs */
281 void
282 ao_led_off(uint8_t colors);
283
284 /* Set all of the LEDs to the specified state */
285 void
286 ao_led_set(uint8_t colors);
287
288 /* Toggle the specified LEDs */
289 void
290 ao_led_toggle(uint8_t colors);
291
292 /* Turn on the specified LEDs for the indicated interval */
293 void
294 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
295
296 /* Initialize the LEDs */
297 void
298 ao_led_init(uint8_t enable);
299
300 /*
301  * ao_romconfig.c
302  */
303
304 #define AO_ROMCONFIG_VERSION    2
305
306 extern __code __at (0x00a0) uint16_t ao_romconfig_version;
307 extern __code __at (0x00a2) uint16_t ao_romconfig_check;
308 extern __code __at (0x00a4) uint16_t ao_serial_number;
309 extern __code __at (0x00a6) uint32_t ao_radio_cal;
310
311 #ifndef HAS_USB
312 #error Please define HAS_USB
313 #endif
314
315 #if HAS_USB
316 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
317 #endif
318
319 /*
320  * ao_usb.c
321  */
322
323 /* Put one character to the USB output queue */
324 void
325 ao_usb_putchar(char c);
326
327 /* Get one character from the USB input queue */
328 char
329 ao_usb_getchar(void);
330
331 /* Poll for a charcter on the USB input queue.
332  * returns AO_READ_AGAIN if none are available
333  */
334 char
335 ao_usb_pollchar(void);
336
337 /* Flush the USB output queue */
338 void
339 ao_usb_flush(void);
340
341 #if HAS_USB
342 /* USB interrupt handler */
343 void
344 ao_usb_isr(void) __interrupt 6;
345 #endif
346
347 /* Enable the USB controller */
348 void
349 ao_usb_enable(void);
350
351 /* Disable the USB controller */
352 void
353 ao_usb_disable(void);
354
355 /* Initialize the USB system */
356 void
357 ao_usb_init(void);
358
359 /*
360  * ao_cmd.c
361  */
362
363 enum ao_cmd_status {
364         ao_cmd_success = 0,
365         ao_cmd_lex_error = 1,
366         ao_cmd_syntax_error = 2,
367 };
368
369 extern __xdata uint16_t ao_cmd_lex_i;
370 extern __xdata uint32_t ao_cmd_lex_u32;
371 extern __xdata char     ao_cmd_lex_c;
372 extern __xdata enum ao_cmd_status ao_cmd_status;
373
374 void
375 ao_cmd_lex(void);
376
377 void
378 ao_cmd_put8(uint8_t v);
379
380 void
381 ao_cmd_put16(uint16_t v);
382
383 void
384 ao_cmd_white(void);
385
386 void
387 ao_cmd_hex(void);
388
389 void
390 ao_cmd_decimal(void);
391
392 uint8_t
393 ao_match_word(__code char *word);
394
395 struct ao_cmds {
396         void            (*func)(void);
397         const char      *help;
398 };
399
400 void
401 ao_cmd_register(__code struct ao_cmds *cmds);
402
403 void
404 ao_cmd_init(void);
405
406 /*
407  * ao_dma.c
408  */
409
410 /* Allocate a DMA channel. the 'done' parameter will be set when the
411  * dma is finished and will be used to wakeup any waiters
412  */
413
414 uint8_t
415 ao_dma_alloc(__xdata uint8_t * done);
416
417 /* Setup a DMA channel */
418 void
419 ao_dma_set_transfer(uint8_t id,
420                     void __xdata *srcaddr,
421                     void __xdata *dstaddr,
422                     uint16_t count,
423                     uint8_t cfg0,
424                     uint8_t cfg1);
425
426 /* Start a DMA channel */
427 void
428 ao_dma_start(uint8_t id);
429
430 /* Manually trigger a DMA channel */
431 void
432 ao_dma_trigger(uint8_t id);
433
434 /* Abort a running DMA transfer */
435 void
436 ao_dma_abort(uint8_t id);
437
438 /* DMA interrupt routine */
439 void
440 ao_dma_isr(void) __interrupt 8;
441
442 /*
443  * ao_mutex.c
444  */
445
446 void
447 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
448
449 void
450 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
451
452 /*
453  * Storage interface, provided by one of the eeprom or flash
454  * drivers
455  */
456
457 /* Total bytes of available storage */
458 extern __xdata uint32_t ao_storage_total;
459
460 /* Block size - device is erased in these units. At least 256 bytes */
461 extern __xdata uint32_t ao_storage_block;
462
463 /* Byte offset of config block. Will be ao_storage_block bytes long */
464 extern __xdata uint32_t ao_storage_config;
465
466 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
467 extern __xdata uint16_t ao_storage_unit;
468
469 #define AO_STORAGE_ERASE_LOG    (ao_storage_config + AO_CONFIG_MAX_SIZE)
470
471 /* Initialize above values. Can only be called once the OS is running */
472 void
473 ao_storage_setup(void) __reentrant;
474
475 /* Write data. Returns 0 on failure, 1 on success */
476 uint8_t
477 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
478
479 /* Read data. Returns 0 on failure, 1 on success */
480 uint8_t
481 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
482
483 /* Erase a block of storage. This always clears ao_storage_block bytes */
484 uint8_t
485 ao_storage_erase(uint32_t pos) __reentrant;
486
487 /* Flush any pending writes to stable storage */
488 void
489 ao_storage_flush(void) __reentrant;
490
491 /* Initialize the storage code */
492 void
493 ao_storage_init(void);
494
495 /*
496  * Low-level functions wrapped by ao_storage.c
497  */
498
499 /* Read data within a storage unit */
500 uint8_t
501 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
502
503 /* Write data within a storage unit */
504 uint8_t
505 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
506
507 /* Initialize low-level device bits */
508 void
509 ao_storage_device_init(void);
510
511 /* Print out information about flash chips */
512 void
513 ao_storage_device_info(void) __reentrant;
514
515 /*
516  * ao_log.c
517  */
518
519 /* We record flight numbers in the first record of
520  * the log. Tasks may wait for this to be initialized
521  * by sleeping on this variable.
522  */
523 extern __xdata uint16_t ao_flight_number;
524
525 extern __pdata uint32_t ao_log_current_pos;
526 extern __pdata uint32_t ao_log_end_pos;
527 extern __pdata uint32_t ao_log_start_pos;
528 extern __xdata uint8_t  ao_log_running;
529 extern __xdata enum flight_state ao_log_state;
530
531 /* required functions from the underlying log system */
532
533 /* Return the flight number from the given log slot, 0 if none */
534 uint16_t
535 ao_log_flight(uint8_t slot);
536
537 /* Flush the log */
538 void
539 ao_log_flush(void);
540
541 /* Logging thread main routine */
542 void
543 ao_log(void);
544
545 /* functions provided in ao_log.c */
546
547 /* Figure out the current flight number */
548 void
549 ao_log_scan(void) __reentrant;
550
551 /* Return the position of the start of the given log slot */
552 uint32_t
553 ao_log_pos(uint8_t slot);
554
555 /* Start logging to eeprom */
556 void
557 ao_log_start(void);
558
559 /* Stop logging */
560 void
561 ao_log_stop(void);
562
563 /* Initialize the logging system */
564 void
565 ao_log_init(void);
566
567 /* Write out the current flight number to the erase log */
568 void
569 ao_log_write_erase(uint8_t pos);
570
571 /* Returns true if there are any logs stored in eeprom */
572 uint8_t
573 ao_log_present(void);
574
575 /* Returns true if there is no more storage space available */
576 uint8_t
577 ao_log_full(void);
578
579 /*
580  * ao_log_big.c
581  */
582
583 /*
584  * The data log is recorded in the eeprom as a sequence
585  * of data packets.
586  *
587  * Each packet starts with a 4-byte header that has the
588  * packet type, the packet checksum and the tick count. Then
589  * they all contain 2 16 bit values which hold packet-specific
590  * data.
591  *
592  * For each flight, the first packet
593  * is FLIGHT packet, indicating the serial number of the
594  * device and a unique number marking the number of flights
595  * recorded by this device.
596  *
597  * During flight, data from the accelerometer and barometer
598  * are recorded in SENSOR packets, using the raw 16-bit values
599  * read from the A/D converter.
600  *
601  * Also during flight, but at a lower rate, the deployment
602  * sensors are recorded in DEPLOY packets. The goal here is to
603  * detect failure in the deployment circuits.
604  *
605  * STATE packets hold state transitions as the flight computer
606  * transitions through different stages of the flight.
607  */
608 #define AO_LOG_FLIGHT           'F'
609 #define AO_LOG_SENSOR           'A'
610 #define AO_LOG_TEMP_VOLT        'T'
611 #define AO_LOG_DEPLOY           'D'
612 #define AO_LOG_STATE            'S'
613 #define AO_LOG_GPS_TIME         'G'
614 #define AO_LOG_GPS_LAT          'N'
615 #define AO_LOG_GPS_LON          'W'
616 #define AO_LOG_GPS_ALT          'H'
617 #define AO_LOG_GPS_SAT          'V'
618 #define AO_LOG_GPS_DATE         'Y'
619
620 #define AO_LOG_POS_NONE         (~0UL)
621
622 struct ao_log_record {
623         char                    type;
624         uint8_t                 csum;
625         uint16_t                tick;
626         union {
627                 struct {
628                         int16_t         ground_accel;
629                         uint16_t        flight;
630                 } flight;
631                 struct {
632                         int16_t         accel;
633                         int16_t         pres;
634                 } sensor;
635                 struct {
636                         int16_t         temp;
637                         int16_t         v_batt;
638                 } temp_volt;
639                 struct {
640                         int16_t         drogue;
641                         int16_t         main;
642                 } deploy;
643                 struct {
644                         uint16_t        state;
645                         uint16_t        reason;
646                 } state;
647                 struct {
648                         uint8_t         hour;
649                         uint8_t         minute;
650                         uint8_t         second;
651                         uint8_t         flags;
652                 } gps_time;
653                 int32_t         gps_latitude;
654                 int32_t         gps_longitude;
655                 struct {
656                         int16_t         altitude;
657                         uint16_t        unused;
658                 } gps_altitude;
659                 struct {
660                         uint16_t        svid;
661                         uint8_t         unused;
662                         uint8_t         c_n;
663                 } gps_sat;
664                 struct {
665                         uint8_t         year;
666                         uint8_t         month;
667                         uint8_t         day;
668                         uint8_t         extra;
669                 } gps_date;
670                 struct {
671                         uint16_t        d0;
672                         uint16_t        d1;
673                 } anon;
674         } u;
675 };
676
677 /* Write a record to the eeprom log */
678 uint8_t
679 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
680
681 /*
682  * ao_flight.c
683  */
684
685 enum ao_flight_state {
686         ao_flight_startup = 0,
687         ao_flight_idle = 1,
688         ao_flight_pad = 2,
689         ao_flight_boost = 3,
690         ao_flight_fast = 4,
691         ao_flight_coast = 5,
692         ao_flight_drogue = 6,
693         ao_flight_main = 7,
694         ao_flight_landed = 8,
695         ao_flight_invalid = 9
696 };
697
698 extern __pdata enum ao_flight_state     ao_flight_state;
699
700 extern __pdata uint16_t                 ao_launch_time;
701 extern __xdata uint8_t                  ao_flight_force_idle;
702
703 /* Flight thread */
704 void
705 ao_flight(void);
706
707 /* Initialize flight thread */
708 void
709 ao_flight_init(void);
710
711 /*
712  * ao_sample.c
713  */
714
715 /*
716  * Barometer calibration
717  *
718  * We directly sample the barometer. The specs say:
719  *
720  * Pressure range: 15-115 kPa
721  * Voltage at 115kPa: 2.82
722  * Output scale: 27mV/kPa
723  *
724  * If we want to detect launch with the barometer, we need
725  * a large enough bump to not be fooled by noise. At typical
726  * launch elevations (0-2000m), a 200Pa pressure change cooresponds
727  * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
728  * As all of our calculations are done in 16 bits, we'll actually see a change
729  * of 16 times this though
730  *
731  * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
732  */
733
734 /* Accelerometer calibration
735  *
736  * We're sampling the accelerometer through a resistor divider which
737  * consists of 5k and 10k resistors. This multiplies the values by 2/3.
738  * That goes into the cc1111 A/D converter, which is running at 11 bits
739  * of precision with the bits in the MSB of the 16 bit value. Only positive
740  * values are used, so values should range from 0-32752 for 0-3.3V. The
741  * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
742  * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
743  * for a final computation of:
744  *
745  * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
746  *
747  * Zero g was measured at 16000 (we would expect 16384).
748  * Note that this value is only require to tell if the
749  * rocket is standing upright. Once that is determined,
750  * the value of the accelerometer is averaged for 100 samples
751  * to find the resting accelerometer value, which is used
752  * for all further flight computations
753  */
754
755 #define GRAVITY 9.80665
756
757 /*
758  * Above this height, the baro sensor doesn't work
759  */
760 #define AO_MAX_BARO_HEIGHT      12000
761
762 /*
763  * Above this speed, baro measurements are unreliable
764  */
765 #define AO_MAX_BARO_SPEED       200
766
767 #define ACCEL_NOSE_UP   (ao_accel_2g >> 2)
768
769 /*
770  * Speed and acceleration are scaled by 16 to provide a bit more
771  * resolution while still having reasonable range. Note that this
772  * limits speed to 2047m/s (around mach 6) and acceleration to
773  * 2047m/s² (over 200g)
774  */
775
776 #define AO_M_TO_HEIGHT(m)       ((int16_t) (m))
777 #define AO_MS_TO_SPEED(ms)      ((int16_t) ((ms) * 16))
778 #define AO_MSS_TO_ACCEL(mss)    ((int16_t) ((mss) * 16))
779
780 extern __pdata uint16_t ao_sample_tick;         /* time of last data */
781 extern __pdata int16_t  ao_sample_pres;         /* most recent pressure sensor reading */
782 extern __pdata int16_t  ao_sample_alt;          /* MSL of ao_sample_pres */
783 extern __pdata int16_t  ao_sample_height;       /* AGL of ao_sample_pres */
784 extern __data uint8_t   ao_sample_adc;          /* Ring position of last processed sample */
785
786 #if HAS_ACCEL
787 extern __pdata int16_t  ao_sample_accel;        /* most recent accel sensor reading */
788 #endif
789
790 extern __xdata int16_t  ao_ground_pres;         /* startup pressure */
791 extern __xdata int16_t  ao_ground_height;       /* MSL of ao_ground_pres */
792
793 #if HAS_ACCEL
794 extern __xdata int16_t  ao_ground_accel;        /* startup acceleration */
795 extern __xdata int16_t  ao_accel_2g;            /* factory accel calibration */
796 extern __xdata int32_t  ao_accel_scale;         /* sensor to m/s² conversion */
797 #endif
798
799 void ao_sample_init(void);
800
801 /* returns FALSE in preflight mode, TRUE in flight mode */
802 uint8_t ao_sample(void);
803
804 /*
805  * ao_kalman.c
806  */
807
808 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
809 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
810 #define from_fix(x)     ((x) >> 16)
811
812 extern __pdata int16_t                  ao_height;      /* meters */
813 extern __pdata int16_t                  ao_speed;       /* m/s * 16 */
814 extern __pdata int16_t                  ao_accel;       /* m/s² * 16 */
815 extern __pdata int16_t                  ao_max_height;  /* max of ao_height */
816
817 extern __pdata int16_t                  ao_error_h;
818 extern __pdata int16_t                  ao_error_h_sq_avg;
819
820 #if HAS_ACCEL
821 extern __pdata int16_t                  ao_error_a;
822 #endif
823
824 void ao_kalman(void);
825
826 /*
827  * ao_report.c
828  */
829
830 void
831 ao_report_init(void);
832
833 /*
834  * ao_convert.c
835  *
836  * Given raw data, convert to SI units
837  */
838
839 /* pressure from the sensor to altitude in meters */
840 int16_t
841 ao_pres_to_altitude(int16_t pres) __reentrant;
842
843 int16_t
844 ao_altitude_to_pres(int16_t alt) __reentrant;
845
846 int16_t
847 ao_temp_to_dC(int16_t temp) __reentrant;
848
849 /*
850  * ao_dbg.c
851  *
852  * debug another telemetrum board
853  */
854
855 /* Send a byte to the dbg target */
856 void
857 ao_dbg_send_byte(uint8_t byte);
858
859 /* Receive a byte from the dbg target */
860 uint8_t
861 ao_dbg_recv_byte(void);
862
863 /* Start a bulk transfer to/from dbg target memory */
864 void
865 ao_dbg_start_transfer(uint16_t addr);
866
867 /* End a bulk transfer to/from dbg target memory */
868 void
869 ao_dbg_end_transfer(void);
870
871 /* Write a byte to dbg target memory */
872 void
873 ao_dbg_write_byte(uint8_t byte);
874
875 /* Read a byte from dbg target memory */
876 uint8_t
877 ao_dbg_read_byte(void);
878
879 /* Enable dbg mode, switching use of the pins */
880 void
881 ao_dbg_debug_mode(void);
882
883 /* Reset the dbg target */
884 void
885 ao_dbg_reset(void);
886
887 void
888 ao_dbg_init(void);
889
890 /*
891  * ao_serial.c
892  */
893
894 #ifndef HAS_SERIAL_1
895 #error Please define HAS_SERIAL_1
896 #endif
897
898 #if HAS_SERIAL_1
899 void
900 ao_serial_rx1_isr(void) __interrupt 3;
901
902 void
903 ao_serial_tx1_isr(void) __interrupt 14;
904
905 char
906 ao_serial_getchar(void) __critical;
907
908 void
909 ao_serial_putchar(char c) __critical;
910
911 #define AO_SERIAL_SPEED_4800    0
912 #define AO_SERIAL_SPEED_9600    1
913 #define AO_SERIAL_SPEED_57600   2
914
915 void
916 ao_serial_set_speed(uint8_t speed);
917
918 void
919 ao_serial_init(void);
920 #endif
921
922 /*
923  * ao_spi.c
924  */
925
926 void
927 ao_spi_send(void __xdata *block, uint16_t len) __reentrant;
928
929 void
930 ao_spi_recv(void __xdata *block, uint16_t len) __reentrant;
931
932 void
933 ao_spi_init(void);
934
935 /*
936  * ao_gps.c
937  */
938
939 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
940 #define AO_GPS_NUM_SAT_SHIFT    (0)
941
942 #define AO_GPS_VALID            (1 << 4)
943 #define AO_GPS_RUNNING          (1 << 5)
944 #define AO_GPS_DATE_VALID       (1 << 6)
945 #define AO_GPS_COURSE_VALID     (1 << 7)
946
947 extern __xdata uint16_t ao_gps_tick;
948
949 struct ao_gps_data {
950         uint8_t                 year;
951         uint8_t                 month;
952         uint8_t                 day;
953         uint8_t                 hour;
954         uint8_t                 minute;
955         uint8_t                 second;
956         uint8_t                 flags;
957         int32_t                 latitude;       /* degrees * 10⁷ */
958         int32_t                 longitude;      /* degrees * 10⁷ */
959         int16_t                 altitude;       /* m */
960         uint16_t                ground_speed;   /* cm/s */
961         uint8_t                 course;         /* degrees / 2 */
962         uint8_t                 hdop;           /* * 5 */
963         int16_t                 climb_rate;     /* cm/s */
964         uint16_t                h_error;        /* m */
965         uint16_t                v_error;        /* m */
966 };
967
968 struct ao_gps_sat_data {
969         uint8_t         svid;
970         uint8_t         c_n_1;
971 };
972
973 #define AO_MAX_GPS_TRACKING     12
974
975 struct ao_gps_tracking_data {
976         uint8_t                 channels;
977         struct ao_gps_sat_data  sats[AO_MAX_GPS_TRACKING];
978 };
979
980 extern __xdata uint8_t ao_gps_mutex;
981 extern __xdata struct ao_gps_data ao_gps_data;
982 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
983
984 void
985 ao_gps(void);
986
987 void
988 ao_gps_print(__xdata struct ao_gps_data *gps_data);
989
990 void
991 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
992
993 void
994 ao_gps_init(void);
995
996 /*
997  * ao_gps_report.c
998  */
999
1000 void
1001 ao_gps_report(void);
1002
1003 void
1004 ao_gps_report_init(void);
1005
1006 /*
1007  * ao_telemetry.c
1008  */
1009
1010 #define AO_MAX_CALLSIGN                 8
1011
1012 struct ao_telemetry {
1013         uint16_t                serial;
1014         uint16_t                flight;
1015         uint8_t                 flight_state;
1016         int16_t                 accel;
1017         int16_t                 ground_accel;
1018         union {
1019                 struct {
1020                         int16_t                 speed;
1021                         int16_t                 unused;
1022                 } k;
1023                 int32_t         flight_vel;
1024         } u;
1025         int16_t                 height;
1026         int16_t                 ground_pres;
1027         int16_t                 accel_plus_g;
1028         int16_t                 accel_minus_g;
1029         struct ao_adc           adc;
1030         struct ao_gps_data      gps;
1031         char                    callsign[AO_MAX_CALLSIGN];
1032         struct ao_gps_tracking_data     gps_tracking;
1033 };
1034
1035 struct ao_telemetry_tiny {
1036         uint16_t                serial;
1037         uint16_t                flight;
1038         uint8_t                 flight_state;
1039         int16_t                 height;         /* AGL in meters */
1040         int16_t                 speed;          /* in m/s * 16 */
1041         int16_t                 accel;          /* in m/s² * 16 */
1042         int16_t                 ground_pres;    /* sensor units */
1043         struct ao_adc           adc;            /* raw ADC readings */
1044         char                    callsign[AO_MAX_CALLSIGN];
1045 };
1046
1047 /*
1048  * ao_radio_recv tacks on rssi and status bytes
1049  */
1050 struct ao_telemetry_recv {
1051         struct ao_telemetry     telemetry;
1052         int8_t                  rssi;
1053         uint8_t                 status;
1054 };
1055
1056 struct ao_telemetry_tiny_recv {
1057         struct ao_telemetry_tiny        telemetry_tiny;
1058         int8_t                          rssi;
1059         uint8_t                         status;
1060 };
1061
1062 /* Set delay between telemetry reports (0 to disable) */
1063
1064 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(1000)
1065 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(100)
1066 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(1000)
1067
1068 void
1069 ao_telemetry_set_interval(uint16_t interval);
1070
1071 void
1072 ao_rdf_set(uint8_t rdf);
1073
1074 void
1075 ao_telemetry_init(void);
1076
1077 void
1078 ao_telemetry_tiny_init(void);
1079
1080 /*
1081  * ao_radio.c
1082  */
1083
1084 extern __xdata uint8_t  ao_radio_dma;
1085 extern __xdata uint8_t ao_radio_dma_done;
1086 extern __xdata uint8_t ao_radio_done;
1087 extern __xdata uint8_t ao_radio_mutex;
1088
1089 void
1090 ao_radio_general_isr(void) __interrupt 16;
1091
1092 void
1093 ao_radio_get(uint8_t len);
1094
1095 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
1096
1097 void
1098 ao_radio_set_packet(void);
1099
1100 void
1101 ao_radio_send(__xdata void *data, uint8_t size) __reentrant;
1102
1103 uint8_t
1104 ao_radio_recv(__xdata void *data, uint8_t size) __reentrant;
1105
1106 void
1107 ao_radio_recv_abort(void);
1108
1109 void
1110 ao_radio_rdf(int ms);
1111
1112 void
1113 ao_radio_rdf_abort(void);
1114
1115 void
1116 ao_radio_idle(void);
1117
1118 void
1119 ao_radio_init(void);
1120
1121 /*
1122  * ao_monitor.c
1123  */
1124
1125 extern const char const * const ao_state_names[];
1126
1127 void
1128 ao_monitor(void);
1129
1130 #define AO_MONITORING_OFF       0
1131 #define AO_MONITORING_FULL      1
1132 #define AO_MONITORING_TINY      2
1133
1134 void
1135 ao_set_monitor(uint8_t monitoring);
1136
1137 void
1138 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
1139
1140 /*
1141  * ao_stdio.c
1142  */
1143
1144 #define AO_READ_AGAIN   ((char) -1)
1145
1146 struct ao_stdio {
1147         char    (*pollchar)(void);
1148         void    (*putchar)(char c) __reentrant;
1149         void    (*flush)(void);
1150 };
1151
1152 void
1153 flush(void);
1154
1155 extern __xdata uint8_t ao_stdin_ready;
1156
1157 void
1158 ao_add_stdio(char (*pollchar)(void),
1159              void (*putchar)(char) __reentrant,
1160              void (*flush)(void)) __reentrant;
1161
1162 /*
1163  * ao_ignite.c
1164  */
1165
1166 enum ao_igniter {
1167         ao_igniter_drogue = 0,
1168         ao_igniter_main = 1
1169 };
1170
1171 void
1172 ao_ignite(enum ao_igniter igniter);
1173
1174 enum ao_igniter_status {
1175         ao_igniter_unknown,     /* unknown status (ambiguous voltage) */
1176         ao_igniter_ready,       /* continuity detected */
1177         ao_igniter_active,      /* igniter firing */
1178         ao_igniter_open,        /* open circuit detected */
1179 };
1180
1181 enum ao_igniter_status
1182 ao_igniter_status(enum ao_igniter igniter);
1183
1184 void
1185 ao_igniter_init(void);
1186
1187 /*
1188  * ao_config.c
1189  */
1190
1191 #define AO_CONFIG_MAJOR 1
1192 #define AO_CONFIG_MINOR 4
1193
1194 struct ao_config {
1195         uint8_t         major;
1196         uint8_t         minor;
1197         uint16_t        main_deploy;
1198         int16_t         accel_plus_g;           /* changed for minor version 2 */
1199         uint8_t         radio_channel;
1200         char            callsign[AO_MAX_CALLSIGN + 1];
1201         uint8_t         apogee_delay;           /* minor version 1 */
1202         int16_t         accel_minus_g;          /* minor version 2 */
1203         uint32_t        radio_cal;              /* minor version 3 */
1204         uint32_t        flight_log_max;         /* minor version 4 */
1205 };
1206
1207 extern __xdata struct ao_config ao_config;
1208
1209 #define AO_CONFIG_MAX_SIZE      128
1210
1211 void
1212 ao_config_get(void);
1213
1214 void
1215 ao_config_put(void);
1216
1217 void
1218 ao_config_init(void);
1219
1220 /*
1221  * ao_rssi.c
1222  */
1223
1224 void
1225 ao_rssi_set(int rssi_value);
1226
1227 void
1228 ao_rssi_init(uint8_t rssi_led);
1229
1230 /*
1231  * ao_product.c
1232  *
1233  * values which need to be defined for
1234  * each instance of a product
1235  */
1236
1237 extern const char ao_version[];
1238 extern const char ao_manufacturer[];
1239 extern const char ao_product[];
1240
1241 /*
1242  * Fifos
1243  */
1244
1245 #define AO_FIFO_SIZE    32
1246
1247 struct ao_fifo {
1248         uint8_t insert;
1249         uint8_t remove;
1250         char    fifo[AO_FIFO_SIZE];
1251 };
1252
1253 #define ao_fifo_insert(f,c) do { \
1254         (f).fifo[(f).insert] = (c); \
1255         (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1256 } while(0)
1257
1258 #define ao_fifo_remove(f,c) do {\
1259         c = (f).fifo[(f).remove]; \
1260         (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1261 } while(0)
1262
1263 #define ao_fifo_full(f)         ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1264 #define ao_fifo_empty(f)        ((f).insert == (f).remove)
1265
1266 /*
1267  * ao_packet.c
1268  *
1269  * Packet-based command interface
1270  */
1271
1272 #define AO_PACKET_MAX           64
1273 #define AO_PACKET_SYN           (uint8_t) 0xff
1274
1275 struct ao_packet {
1276         uint8_t         addr;
1277         uint8_t         len;
1278         uint8_t         seq;
1279         uint8_t         ack;
1280         uint8_t         d[AO_PACKET_MAX];
1281         uint8_t         callsign[AO_MAX_CALLSIGN];
1282 };
1283
1284 struct ao_packet_recv {
1285         struct ao_packet        packet;
1286         int8_t                  rssi;
1287         uint8_t                 status;
1288 };
1289
1290 extern __xdata struct ao_packet_recv ao_rx_packet;
1291 extern __xdata struct ao_packet ao_tx_packet;
1292 extern __xdata struct ao_task   ao_packet_task;
1293 extern __xdata uint8_t ao_packet_enable;
1294 extern __xdata uint8_t ao_packet_master_sleeping;
1295 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1296
1297 void
1298 ao_packet_send(void);
1299
1300 uint8_t
1301 ao_packet_recv(void);
1302
1303 void
1304 ao_packet_flush(void);
1305
1306 void
1307 ao_packet_putchar(char c) __reentrant;
1308
1309 char
1310 ao_packet_pollchar(void) __critical;
1311
1312 /* ao_packet_master.c */
1313
1314 void
1315 ao_packet_master_init(void);
1316
1317 /* ao_packet_slave.c */
1318
1319 void
1320 ao_packet_slave_start(void);
1321
1322 void
1323 ao_packet_slave_stop(void);
1324
1325 void
1326 ao_packet_slave_init(uint8_t enable);
1327
1328 #endif /* _AO_H_ */