2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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
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.
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.
31 /* Convert a __data pointer into an __xdata pointer */
32 #define DATA_TO_XDATA(a) ((void __xdata *) ((uint8_t) (a) | 0xff00))
34 /* Stack runs from above the allocated __data space to 0xfe, which avoids
35 * writing to 0xff as that triggers the stack overflow indicator
37 #define AO_STACK_START 0x90
38 #define AO_STACK_END 0xfe
39 #define AO_STACK_SIZE (AO_STACK_END - AO_STACK_START + 1)
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 */
51 extern __xdata struct ao_task *__data ao_cur_task;
53 #define AO_NUM_TASKS 16 /* maximum number of tasks */
54 #define AO_NO_TASK 0 /* no task id */
60 /* Suspend the current task until wchan is awoken.
66 ao_sleep(__xdata void *wchan);
68 /* Wake all tasks sleeping on wchan */
70 ao_wakeup(__xdata void *wchan);
72 /* set an alarm to go off in 'delay' ticks */
74 ao_alarm(uint16_t delay);
76 /* Yield the processor to another task */
78 ao_yield(void) __naked;
80 /* Add a task to the run queue */
82 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
84 /* Terminate the current task */
88 /* Dump task info to console */
92 /* Start the scheduler. This will not return */
94 ao_start_scheduler(void);
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 #define AO_PANIC_BT 11 /* Communications with bluetooth device failed */
112 /* Stop the operating system, beeping and blinking the reason */
114 ao_panic(uint8_t reason);
120 /* Our timer runs at 100Hz */
122 #define AO_MS_TO_TICKS(ms) ((ms) / (1000 / AO_HERTZ))
123 #define AO_SEC_TO_TICKS(s) ((s) * AO_HERTZ)
125 /* Returns the current time in ticks */
129 /* Suspend the current task until ticks time has passed */
131 ao_delay(uint16_t ticks);
133 /* Set the ADC interval */
135 ao_timer_set_adc_interval(uint8_t interval) __critical;
137 /* Timer interrupt */
139 ao_timer_isr(void) __interrupt 9;
141 /* Initialize the timer */
145 /* Initialize the hardware clock. Must be called first */
150 * One set of samples read from the A/D converter or telemetry
153 uint16_t tick; /* tick when the sample was read */
154 int16_t accel; /* accelerometer */
155 int16_t pres; /* pressure sensor */
156 int16_t temp; /* temperature sensor */
157 int16_t v_batt; /* battery voltage */
158 int16_t sense_d; /* drogue continuity sense */
159 int16_t sense_m; /* main continuity sense */
163 #error Please define HAS_ADC
169 #ifndef HAS_ACCEL_REF
170 #error Please define HAS_ACCEL_REF
173 #define HAS_ACCEL_REF 0
180 #define AO_ADC_RING 32
181 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
182 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
186 * A/D data is stored in a ring, with the next sample to be written
189 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
190 extern volatile __data uint8_t ao_adc_head;
192 extern volatile __xdata uint16_t ao_accel_ref[AO_ADC_RING];
195 /* Trigger a conversion sequence (called from the timer interrupt) */
199 /* Suspend the current task until another A/D sample is converted */
203 /* Get a copy of the last complete A/D sample set */
205 ao_adc_get(__xdata struct ao_adc *packet);
207 /* The A/D interrupt handler */
210 ao_adc_isr(void) __interrupt 1;
212 /* Initialize the A/D converter */
223 * Various pre-defined beep frequencies
225 * frequency = 1/2 (24e6/32) / beep
228 #define AO_BEEP_LOW 150 /* 2500Hz */
229 #define AO_BEEP_MID 94 /* 3989Hz */
230 #define AO_BEEP_HIGH 75 /* 5000Hz */
231 #define AO_BEEP_OFF 0 /* off */
233 #define AO_BEEP_g 240 /* 1562.5Hz */
234 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
235 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
236 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
237 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
238 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
239 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
240 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
241 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
242 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
243 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
244 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
245 #define AO_BEEP_gg 120 /* 3125Hz */
246 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
247 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
248 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
249 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
250 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
251 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
252 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
253 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
254 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
255 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
256 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
257 #define AO_BEEP_ggg 60 /* 6250Hz */
259 /* Set the beeper to the specified tone */
261 ao_beep(uint8_t beep);
263 /* Turn on the beeper for the specified time */
265 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
267 /* Initialize the beeper */
275 #define AO_LED_NONE 0
277 /* Turn on the specified LEDs */
279 ao_led_on(uint8_t colors);
281 /* Turn off the specified LEDs */
283 ao_led_off(uint8_t colors);
285 /* Set all of the LEDs to the specified state */
287 ao_led_set(uint8_t colors);
289 /* Toggle the specified LEDs */
291 ao_led_toggle(uint8_t colors);
293 /* Turn on the specified LEDs for the indicated interval */
295 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
297 /* Initialize the LEDs */
299 ao_led_init(uint8_t enable);
305 #define AO_ROMCONFIG_VERSION 2
307 extern __code __at (0x00a0) uint16_t ao_romconfig_version;
308 extern __code __at (0x00a2) uint16_t ao_romconfig_check;
309 extern __code __at (0x00a4) uint16_t ao_serial_number;
310 extern __code __at (0x00a6) uint32_t ao_radio_cal;
313 #error Please define HAS_USB
317 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
324 /* Put one character to the USB output queue */
326 ao_usb_putchar(char c);
328 /* Get one character from the USB input queue */
330 ao_usb_getchar(void);
332 /* Poll for a charcter on the USB input queue.
333 * returns AO_READ_AGAIN if none are available
336 ao_usb_pollchar(void);
338 /* Flush the USB output queue */
343 /* USB interrupt handler */
345 ao_usb_isr(void) __interrupt 6;
348 /* Enable the USB controller */
352 /* Disable the USB controller */
354 ao_usb_disable(void);
356 /* Initialize the USB system */
366 ao_cmd_lex_error = 1,
367 ao_cmd_syntax_error = 2,
370 extern __pdata uint16_t ao_cmd_lex_i;
371 extern __pdata uint32_t ao_cmd_lex_u32;
372 extern __pdata char ao_cmd_lex_c;
373 extern __pdata enum ao_cmd_status ao_cmd_status;
379 ao_cmd_put8(uint8_t v);
382 ao_cmd_put16(uint16_t v);
391 ao_cmd_decimal(void);
394 ao_match_word(__code char *word);
402 ao_cmd_register(__code struct ao_cmds *cmds);
409 * Provided by an external module to filter raw command lines
419 /* Allocate a DMA channel. the 'done' parameter will be set when the
420 * dma is finished and will be used to wakeup any waiters
424 ao_dma_alloc(__xdata uint8_t * done);
426 /* Setup a DMA channel */
428 ao_dma_set_transfer(uint8_t id,
429 void __xdata *srcaddr,
430 void __xdata *dstaddr,
435 /* Start a DMA channel */
437 ao_dma_start(uint8_t id);
439 /* Manually trigger a DMA channel */
441 ao_dma_trigger(uint8_t id);
443 /* Abort a running DMA transfer */
445 ao_dma_abort(uint8_t id);
447 /* DMA interrupt routine */
449 ao_dma_isr(void) __interrupt 8;
456 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
459 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
462 * Storage interface, provided by one of the eeprom or flash
466 /* Total bytes of available storage */
467 extern __pdata uint32_t ao_storage_total;
469 /* Block size - device is erased in these units. At least 256 bytes */
470 extern __pdata uint32_t ao_storage_block;
472 /* Byte offset of config block. Will be ao_storage_block bytes long */
473 extern __pdata uint32_t ao_storage_config;
475 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
476 extern __pdata uint16_t ao_storage_unit;
478 #define AO_STORAGE_ERASE_LOG (ao_storage_config + AO_CONFIG_MAX_SIZE)
480 /* Initialize above values. Can only be called once the OS is running */
482 ao_storage_setup(void) __reentrant;
484 /* Write data. Returns 0 on failure, 1 on success */
486 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
488 /* Read data. Returns 0 on failure, 1 on success */
490 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
492 /* Erase a block of storage. This always clears ao_storage_block bytes */
494 ao_storage_erase(uint32_t pos) __reentrant;
496 /* Flush any pending writes to stable storage */
498 ao_storage_flush(void) __reentrant;
500 /* Initialize the storage code */
502 ao_storage_init(void);
505 * Low-level functions wrapped by ao_storage.c
508 /* Read data within a storage unit */
510 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
512 /* Write data within a storage unit */
514 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
516 /* Initialize low-level device bits */
518 ao_storage_device_init(void);
520 /* Print out information about flash chips */
522 ao_storage_device_info(void) __reentrant;
528 /* We record flight numbers in the first record of
529 * the log. Tasks may wait for this to be initialized
530 * by sleeping on this variable.
532 extern __xdata uint16_t ao_flight_number;
534 extern __pdata uint32_t ao_log_current_pos;
535 extern __pdata uint32_t ao_log_end_pos;
536 extern __pdata uint32_t ao_log_start_pos;
537 extern __xdata uint8_t ao_log_running;
538 extern __pdata enum flight_state ao_log_state;
540 /* required functions from the underlying log system */
542 #define AO_LOG_FORMAT_UNKNOWN 0 /* unknown; altosui will have to guess */
543 #define AO_LOG_FORMAT_FULL 1 /* 8 byte typed log records */
544 #define AO_LOG_FORMAT_TINY 2 /* two byte state/baro records */
545 #define AO_LOG_FORMAT_TELEMETRY 3 /* 32 byte ao_telemetry records */
546 #define AO_LOG_FORMAT_TELESCIENCE 4 /* 32 byte typed telescience records */
547 #define AO_LOG_FORMAT_NONE 127 /* No log at all */
549 extern __code uint8_t ao_log_format;
551 /* Return the flight number from the given log slot, 0 if none */
553 ao_log_flight(uint8_t slot);
559 /* Logging thread main routine */
563 /* functions provided in ao_log.c */
565 /* Figure out the current flight number */
567 ao_log_scan(void) __reentrant;
569 /* Return the position of the start of the given log slot */
571 ao_log_pos(uint8_t slot);
573 /* Start logging to eeprom */
581 /* Initialize the logging system */
585 /* Write out the current flight number to the erase log */
587 ao_log_write_erase(uint8_t pos);
589 /* Returns true if there are any logs stored in eeprom */
591 ao_log_present(void);
593 /* Returns true if there is no more storage space available */
602 * The data log is recorded in the eeprom as a sequence
605 * Each packet starts with a 4-byte header that has the
606 * packet type, the packet checksum and the tick count. Then
607 * they all contain 2 16 bit values which hold packet-specific
610 * For each flight, the first packet
611 * is FLIGHT packet, indicating the serial number of the
612 * device and a unique number marking the number of flights
613 * recorded by this device.
615 * During flight, data from the accelerometer and barometer
616 * are recorded in SENSOR packets, using the raw 16-bit values
617 * read from the A/D converter.
619 * Also during flight, but at a lower rate, the deployment
620 * sensors are recorded in DEPLOY packets. The goal here is to
621 * detect failure in the deployment circuits.
623 * STATE packets hold state transitions as the flight computer
624 * transitions through different stages of the flight.
626 #define AO_LOG_FLIGHT 'F'
627 #define AO_LOG_SENSOR 'A'
628 #define AO_LOG_TEMP_VOLT 'T'
629 #define AO_LOG_DEPLOY 'D'
630 #define AO_LOG_STATE 'S'
631 #define AO_LOG_GPS_TIME 'G'
632 #define AO_LOG_GPS_LAT 'N'
633 #define AO_LOG_GPS_LON 'W'
634 #define AO_LOG_GPS_ALT 'H'
635 #define AO_LOG_GPS_SAT 'V'
636 #define AO_LOG_GPS_DATE 'Y'
638 #define AO_LOG_POS_NONE (~0UL)
640 struct ao_log_record {
646 int16_t ground_accel;
671 int32_t gps_latitude;
672 int32_t gps_longitude;
695 /* Write a record to the eeprom log */
697 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
703 enum ao_flight_state {
704 ao_flight_startup = 0,
710 ao_flight_drogue = 6,
712 ao_flight_landed = 8,
713 ao_flight_invalid = 9
716 extern __pdata enum ao_flight_state ao_flight_state;
718 extern __pdata uint16_t ao_launch_time;
719 extern __pdata uint8_t ao_flight_force_idle;
725 /* Initialize flight thread */
727 ao_flight_init(void);
734 ao_flight_nano_init(void);
741 * Barometer calibration
743 * We directly sample the barometer. The specs say:
745 * Pressure range: 15-115 kPa
746 * Voltage at 115kPa: 2.82
747 * Output scale: 27mV/kPa
749 * If we want to detect launch with the barometer, we need
750 * a large enough bump to not be fooled by noise. At typical
751 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
752 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
753 * As all of our calculations are done in 16 bits, we'll actually see a change
754 * of 16 times this though
756 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
759 /* Accelerometer calibration
761 * We're sampling the accelerometer through a resistor divider which
762 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
763 * That goes into the cc1111 A/D converter, which is running at 11 bits
764 * of precision with the bits in the MSB of the 16 bit value. Only positive
765 * values are used, so values should range from 0-32752 for 0-3.3V. The
766 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
767 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
768 * for a final computation of:
770 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
772 * Zero g was measured at 16000 (we would expect 16384).
773 * Note that this value is only require to tell if the
774 * rocket is standing upright. Once that is determined,
775 * the value of the accelerometer is averaged for 100 samples
776 * to find the resting accelerometer value, which is used
777 * for all further flight computations
780 #define GRAVITY 9.80665
783 * Above this height, the baro sensor doesn't work
785 #define AO_MAX_BARO_HEIGHT 12000
788 * Above this speed, baro measurements are unreliable
790 #define AO_MAX_BARO_SPEED 200
792 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
795 * Speed and acceleration are scaled by 16 to provide a bit more
796 * resolution while still having reasonable range. Note that this
797 * limits speed to 2047m/s (around mach 6) and acceleration to
798 * 2047m/s² (over 200g)
801 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
802 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
803 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
805 extern __pdata uint16_t ao_sample_tick; /* time of last data */
806 extern __pdata int16_t ao_sample_pres; /* most recent pressure sensor reading */
807 extern __pdata int16_t ao_sample_alt; /* MSL of ao_sample_pres */
808 extern __pdata int16_t ao_sample_height; /* AGL of ao_sample_pres */
809 extern __data uint8_t ao_sample_adc; /* Ring position of last processed sample */
812 extern __pdata int16_t ao_sample_accel; /* most recent accel sensor reading */
815 extern __pdata int16_t ao_ground_pres; /* startup pressure */
816 extern __pdata int16_t ao_ground_height; /* MSL of ao_ground_pres */
819 extern __pdata int16_t ao_ground_accel; /* startup acceleration */
820 extern __pdata int16_t ao_accel_2g; /* factory accel calibration */
821 extern __pdata int32_t ao_accel_scale; /* sensor to m/s² conversion */
824 void ao_sample_init(void);
826 /* returns FALSE in preflight mode, TRUE in flight mode */
827 uint8_t ao_sample(void);
833 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
834 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
835 #define from_fix(x) ((x) >> 16)
837 extern __pdata int16_t ao_height; /* meters */
838 extern __pdata int16_t ao_speed; /* m/s * 16 */
839 extern __pdata int16_t ao_accel; /* m/s² * 16 */
840 extern __pdata int16_t ao_max_height; /* max of ao_height */
841 extern __pdata int16_t ao_avg_height; /* running average of height */
843 extern __pdata int16_t ao_error_h;
844 extern __pdata int16_t ao_error_h_sq_avg;
847 extern __pdata int16_t ao_error_a;
850 void ao_kalman(void);
857 ao_report_init(void);
862 * Given raw data, convert to SI units
865 /* pressure from the sensor to altitude in meters */
867 ao_pres_to_altitude(int16_t pres) __reentrant;
870 ao_altitude_to_pres(int16_t alt) __reentrant;
873 ao_temp_to_dC(int16_t temp) __reentrant;
878 * debug another telemetrum board
881 /* Send a byte to the dbg target */
883 ao_dbg_send_byte(uint8_t byte);
885 /* Receive a byte from the dbg target */
887 ao_dbg_recv_byte(void);
889 /* Start a bulk transfer to/from dbg target memory */
891 ao_dbg_start_transfer(uint16_t addr);
893 /* End a bulk transfer to/from dbg target memory */
895 ao_dbg_end_transfer(void);
897 /* Write a byte to dbg target memory */
899 ao_dbg_write_byte(uint8_t byte);
901 /* Read a byte from dbg target memory */
903 ao_dbg_read_byte(void);
905 /* Enable dbg mode, switching use of the pins */
907 ao_dbg_debug_mode(void);
909 /* Reset the dbg target */
921 #error Please define HAS_SERIAL_1
925 #ifndef USE_SERIAL_STDIN
926 #error Please define USE_SERIAL_STDIN
930 ao_serial_rx1_isr(void) __interrupt 3;
933 ao_serial_tx1_isr(void) __interrupt 14;
936 ao_serial_getchar(void) __critical;
940 ao_serial_pollchar(void) __critical;
943 ao_serial_set_stdin(uint8_t stdin);
947 ao_serial_putchar(char c) __critical;
950 ao_serial_drain(void) __critical;
952 #define AO_SERIAL_SPEED_4800 0
953 #define AO_SERIAL_SPEED_9600 1
954 #define AO_SERIAL_SPEED_19200 2
955 #define AO_SERIAL_SPEED_57600 3
958 ao_serial_set_speed(uint8_t speed);
961 ao_serial_init(void);
968 extern __xdata uint8_t ao_spi_mutex;
970 #define ao_spi_get_mask(reg,mask) do {\
971 ao_mutex_get(&ao_spi_mutex); \
975 #define ao_spi_put_mask(reg,mask) do { \
977 ao_mutex_put(&ao_spi_mutex); \
980 #define ao_spi_get_bit(bit) do {\
981 ao_mutex_get(&ao_spi_mutex); \
985 #define ao_spi_put_bit(bit) do { \
987 ao_mutex_put(&ao_spi_mutex); \
991 * The SPI mutex must be held to call either of these
992 * functions -- this mutex covers the entire SPI operation,
993 * from chip select low to chip select high
997 ao_spi_send(void __xdata *block, uint16_t len) __reentrant;
1000 ao_spi_recv(void __xdata *block, uint16_t len) __reentrant;
1008 #define AO_MAX_CALLSIGN 8
1009 #define AO_MAX_VERSION 8
1010 #define AO_MAX_TELEMETRY 128
1012 struct ao_telemetry_generic {
1013 uint16_t serial; /* 0 */
1014 uint16_t tick; /* 2 */
1015 uint8_t type; /* 4 */
1016 uint8_t payload[27]; /* 5 */
1020 #define AO_TELEMETRY_SENSOR_TELEMETRUM 0x01
1021 #define AO_TELEMETRY_SENSOR_TELEMINI 0x02
1022 #define AO_TELEMETRY_SENSOR_TELENANO 0x03
1024 struct ao_telemetry_sensor {
1025 uint16_t serial; /* 0 */
1026 uint16_t tick; /* 2 */
1027 uint8_t type; /* 4 */
1029 uint8_t state; /* 5 flight state */
1030 int16_t accel; /* 6 accelerometer (TM only) */
1031 int16_t pres; /* 8 pressure sensor */
1032 int16_t temp; /* 10 temperature sensor */
1033 int16_t v_batt; /* 12 battery voltage */
1034 int16_t sense_d; /* 14 drogue continuity sense (TM/Tm) */
1035 int16_t sense_m; /* 16 main continuity sense (TM/Tm) */
1037 int16_t acceleration; /* 18 m/s² * 16 */
1038 int16_t speed; /* 20 m/s * 16 */
1039 int16_t height; /* 22 m */
1041 int16_t ground_pres; /* 24 average pres on pad */
1042 int16_t ground_accel; /* 26 average accel on pad */
1043 int16_t accel_plus_g; /* 28 accel calibration at +1g */
1044 int16_t accel_minus_g; /* 30 accel calibration at -1g */
1048 #define AO_TELEMETRY_CONFIGURATION 0x04
1050 struct ao_telemetry_configuration {
1051 uint16_t serial; /* 0 */
1052 uint16_t tick; /* 2 */
1053 uint8_t type; /* 4 */
1055 uint8_t device; /* 5 device type */
1056 uint16_t flight; /* 6 flight number */
1057 uint8_t config_major; /* 8 Config major version */
1058 uint8_t config_minor; /* 9 Config minor version */
1059 uint16_t apogee_delay; /* 10 Apogee deploy delay in seconds */
1060 uint16_t main_deploy; /* 12 Main deploy alt in meters */
1061 uint16_t flight_log_max; /* 14 Maximum flight log size in kB */
1062 char callsign[AO_MAX_CALLSIGN]; /* 16 Radio operator identity */
1063 char version[AO_MAX_VERSION]; /* 24 Software version */
1067 #define AO_TELEMETRY_LOCATION 0x05
1069 #define AO_GPS_MODE_NOT_VALID 'N'
1070 #define AO_GPS_MODE_AUTONOMOUS 'A'
1071 #define AO_GPS_MODE_DIFFERENTIAL 'D'
1072 #define AO_GPS_MODE_ESTIMATED 'E'
1073 #define AO_GPS_MODE_MANUAL 'M'
1074 #define AO_GPS_MODE_SIMULATED 'S'
1076 struct ao_telemetry_location {
1077 uint16_t serial; /* 0 */
1078 uint16_t tick; /* 2 */
1079 uint8_t type; /* 4 */
1081 uint8_t flags; /* 5 Number of sats and other flags */
1082 int16_t altitude; /* 6 GPS reported altitude (m) */
1083 int32_t latitude; /* 8 latitude (degrees * 10⁷) */
1084 int32_t longitude; /* 12 longitude (degrees * 10⁷) */
1085 uint8_t year; /* 16 (- 2000) */
1086 uint8_t month; /* 17 (1-12) */
1087 uint8_t day; /* 18 (1-31) */
1088 uint8_t hour; /* 19 (0-23) */
1089 uint8_t minute; /* 20 (0-59) */
1090 uint8_t second; /* 21 (0-59) */
1091 uint8_t pdop; /* 22 (m * 5) */
1092 uint8_t hdop; /* 23 (m * 5) */
1093 uint8_t vdop; /* 24 (m * 5) */
1094 uint8_t mode; /* 25 */
1095 uint16_t ground_speed; /* 26 cm/s */
1096 int16_t climb_rate; /* 28 cm/s */
1097 uint8_t course; /* 30 degrees / 2 */
1098 uint8_t unused[1]; /* 31 */
1102 #define AO_TELEMETRY_SATELLITE 0x06
1104 struct ao_telemetry_satellite_info {
1109 struct ao_telemetry_satellite {
1110 uint16_t serial; /* 0 */
1111 uint16_t tick; /* 2 */
1112 uint8_t type; /* 4 */
1113 uint8_t channels; /* 5 number of reported sats */
1115 struct ao_telemetry_satellite_info sats[12]; /* 6 */
1116 uint8_t unused[2]; /* 30 */
1120 #define AO_TELEMETRY_COMPANION 0x07
1122 #define AO_COMPANION_MAX_CHANNELS 12
1124 struct ao_telemetry_companion {
1125 uint16_t serial; /* 0 */
1126 uint16_t tick; /* 2 */
1127 uint8_t type; /* 4 */
1128 uint8_t board_id; /* 5 */
1130 uint8_t update_period; /* 6 */
1131 uint8_t channels; /* 7 */
1132 uint16_t companion_data[AO_COMPANION_MAX_CHANNELS]; /* 8 */
1136 union ao_telemetry_all {
1137 struct ao_telemetry_generic generic;
1138 struct ao_telemetry_sensor sensor;
1139 struct ao_telemetry_configuration configuration;
1140 struct ao_telemetry_location location;
1141 struct ao_telemetry_satellite satellite;
1142 struct ao_telemetry_companion companion;
1149 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
1150 #define AO_GPS_NUM_SAT_SHIFT (0)
1152 #define AO_GPS_VALID (1 << 4)
1153 #define AO_GPS_RUNNING (1 << 5)
1154 #define AO_GPS_DATE_VALID (1 << 6)
1155 #define AO_GPS_COURSE_VALID (1 << 7)
1157 extern __pdata uint16_t ao_gps_tick;
1158 extern __xdata uint8_t ao_gps_mutex;
1159 extern __xdata struct ao_telemetry_location ao_gps_data;
1160 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
1162 struct ao_gps_orig {
1170 int32_t latitude; /* degrees * 10⁷ */
1171 int32_t longitude; /* degrees * 10⁷ */
1172 int16_t altitude; /* m */
1173 uint16_t ground_speed; /* cm/s */
1174 uint8_t course; /* degrees / 2 */
1175 uint8_t hdop; /* * 5 */
1176 int16_t climb_rate; /* cm/s */
1177 uint16_t h_error; /* m */
1178 uint16_t v_error; /* m */
1181 struct ao_gps_sat_orig {
1186 #define AO_MAX_GPS_TRACKING 12
1188 struct ao_gps_tracking_orig {
1190 struct ao_gps_sat_orig sats[AO_MAX_GPS_TRACKING];
1197 ao_gps_print(__xdata struct ao_gps_orig *gps_data);
1200 ao_gps_tracking_print(__xdata struct ao_gps_tracking_orig *gps_tracking_data);
1210 ao_gps_report(void);
1213 ao_gps_report_init(void);
1216 * ao_telemetry_orig.c
1219 struct ao_telemetry_orig {
1222 uint8_t flight_state;
1224 int16_t ground_accel;
1233 int16_t ground_pres;
1234 int16_t accel_plus_g;
1235 int16_t accel_minus_g;
1237 struct ao_gps_orig gps;
1238 char callsign[AO_MAX_CALLSIGN];
1239 struct ao_gps_tracking_orig gps_tracking;
1242 struct ao_telemetry_tiny {
1245 uint8_t flight_state;
1246 int16_t height; /* AGL in meters */
1247 int16_t speed; /* in m/s * 16 */
1248 int16_t accel; /* in m/s² * 16 */
1249 int16_t ground_pres; /* sensor units */
1250 struct ao_adc adc; /* raw ADC readings */
1251 char callsign[AO_MAX_CALLSIGN];
1255 * ao_radio_recv tacks on rssi and status bytes
1258 struct ao_telemetry_raw_recv {
1259 uint8_t packet[AO_MAX_TELEMETRY + 2];
1262 struct ao_telemetry_orig_recv {
1263 struct ao_telemetry_orig telemetry_orig;
1268 struct ao_telemetry_tiny_recv {
1269 struct ao_telemetry_tiny telemetry_tiny;
1274 /* Set delay between telemetry reports (0 to disable) */
1276 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(1000)
1277 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(100)
1278 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(1000)
1281 ao_telemetry_set_interval(uint16_t interval);
1284 ao_rdf_set(uint8_t rdf);
1287 ao_telemetry_init(void);
1290 ao_telemetry_orig_init(void);
1293 ao_telemetry_tiny_init(void);
1299 extern __xdata uint8_t ao_radio_dma;
1300 extern __xdata uint8_t ao_radio_dma_done;
1301 extern __xdata uint8_t ao_radio_done;
1302 extern __xdata uint8_t ao_radio_mutex;
1305 ao_radio_general_isr(void) __interrupt 16;
1308 ao_radio_get(uint8_t len);
1310 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
1313 ao_radio_set_packet(void);
1316 ao_radio_send(__xdata void *data, uint8_t size) __reentrant;
1319 ao_radio_recv(__xdata void *data, uint8_t size) __reentrant;
1322 ao_radio_recv_abort(void);
1325 ao_radio_rdf(int ms);
1328 ao_radio_rdf_abort(void);
1331 ao_radio_idle(void);
1334 ao_radio_init(void);
1340 extern const char const * const ao_state_names[];
1345 #define AO_MONITORING_OFF 0
1346 #define AO_MONITORING_ORIG 1
1347 #define AO_MONITORING_TINY 2
1350 ao_set_monitor(uint8_t monitoring);
1353 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
1359 #define AO_READ_AGAIN ((char) -1)
1362 char (*pollchar)(void);
1363 void (*putchar)(char c) __reentrant;
1364 void (*flush)(void);
1368 extern __xdata struct ao_stdio ao_stdios[];
1369 extern __pdata int8_t ao_cur_stdio;
1370 extern __pdata int8_t ao_num_stdios;
1375 extern __xdata uint8_t ao_stdin_ready;
1381 ao_add_stdio(char (*pollchar)(void),
1382 void (*putchar)(char) __reentrant,
1383 void (*flush)(void)) __reentrant;
1390 ao_igniter_drogue = 0,
1395 ao_ignite(enum ao_igniter igniter);
1397 enum ao_igniter_status {
1398 ao_igniter_unknown, /* unknown status (ambiguous voltage) */
1399 ao_igniter_ready, /* continuity detected */
1400 ao_igniter_active, /* igniter firing */
1401 ao_igniter_open, /* open circuit detected */
1404 enum ao_igniter_status
1405 ao_igniter_status(enum ao_igniter igniter);
1408 ao_ignite_set_pins(void);
1411 ao_igniter_init(void);
1417 #define AO_CONFIG_MAJOR 1
1418 #define AO_CONFIG_MINOR 6
1423 uint16_t main_deploy;
1424 int16_t accel_plus_g; /* changed for minor version 2 */
1425 uint8_t radio_channel;
1426 char callsign[AO_MAX_CALLSIGN + 1];
1427 uint8_t apogee_delay; /* minor version 1 */
1428 int16_t accel_minus_g; /* minor version 2 */
1429 uint32_t radio_cal; /* minor version 3 */
1430 uint32_t flight_log_max; /* minor version 4 */
1431 uint8_t ignite_mode; /* minor version 5 */
1432 uint8_t pad_orientation; /* minor version 6 */
1433 uint32_t radio_setting; /* minor version 7 */
1436 #define AO_IGNITE_MODE_DUAL 0
1437 #define AO_IGNITE_MODE_APOGEE 1
1438 #define AO_IGNITE_MODE_MAIN 2
1440 #define AO_PAD_ORIENTATION_ANTENNA_UP 0
1441 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
1443 extern __xdata struct ao_config ao_config;
1445 #define AO_CONFIG_MAX_SIZE 128
1448 ao_config_get(void);
1451 ao_config_put(void);
1454 ao_config_init(void);
1461 ao_rssi_set(int rssi_value);
1464 ao_rssi_init(uint8_t rssi_led);
1469 * values which need to be defined for
1470 * each instance of a product
1473 extern const char ao_version[];
1474 extern const char ao_manufacturer[];
1475 extern const char ao_product[];
1481 #define AO_FIFO_SIZE 32
1486 char fifo[AO_FIFO_SIZE];
1489 #define ao_fifo_insert(f,c) do { \
1490 (f).fifo[(f).insert] = (c); \
1491 (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1494 #define ao_fifo_remove(f,c) do {\
1495 c = (f).fifo[(f).remove]; \
1496 (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1499 #define ao_fifo_full(f) ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1500 #define ao_fifo_empty(f) ((f).insert == (f).remove)
1505 * Packet-based command interface
1508 #define AO_PACKET_MAX 64
1509 #define AO_PACKET_SYN (uint8_t) 0xff
1516 uint8_t d[AO_PACKET_MAX];
1517 uint8_t callsign[AO_MAX_CALLSIGN];
1520 struct ao_packet_recv {
1521 struct ao_packet packet;
1526 extern __xdata struct ao_packet_recv ao_rx_packet;
1527 extern __xdata struct ao_packet ao_tx_packet;
1528 extern __xdata struct ao_task ao_packet_task;
1529 extern __xdata uint8_t ao_packet_enable;
1530 extern __xdata uint8_t ao_packet_master_sleeping;
1531 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1534 ao_packet_send(void);
1537 ao_packet_recv(void);
1540 ao_packet_flush(void);
1543 ao_packet_putchar(char c) __reentrant;
1546 ao_packet_pollchar(void) __critical;
1548 /* ao_packet_master.c */
1551 ao_packet_master_init(void);
1553 /* ao_packet_slave.c */
1556 ao_packet_slave_start(void);
1559 ao_packet_slave_stop(void);
1562 ao_packet_slave_init(uint8_t enable);
1566 /* If bt_link is on P2, this interrupt is shared by USB, so the USB
1567 * code calls this function. Otherwise, it's a regular ISR.
1580 /* ao_companion.c */
1582 #define AO_COMPANION_SETUP 1
1583 #define AO_COMPANION_FETCH 2
1585 struct ao_companion_command {
1587 uint8_t flight_state;
1591 struct ao_companion_setup {
1593 uint16_t board_id_inverse;
1594 uint8_t update_period;
1598 extern __pdata uint8_t ao_companion_running;
1599 extern __xdata struct ao_companion_setup ao_companion_setup;
1600 extern __xdata uint8_t ao_companion_mutex;
1601 extern __xdata uint16_t ao_companion_data[AO_COMPANION_MAX_CHANNELS];
1604 ao_companion_init(void);