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 */
33 #define DATA_TO_XDATA(a) (a)
38 __xdata void *wchan; /* current wait channel (NULL if running) */
39 uint16_t alarm; /* abort ao_sleep time */
40 ao_arch_task_members /* any architecture-specific fields */
41 uint8_t task_id; /* unique id */
42 __code char *name; /* task name */
43 uint8_t stack[AO_STACK_SIZE]; /* saved stack */
46 extern __xdata struct ao_task *__data ao_cur_task;
48 #define AO_NUM_TASKS 16 /* maximum number of tasks */
49 #define AO_NO_TASK 0 /* no task id */
55 /* Suspend the current task until wchan is awoken.
61 ao_sleep(__xdata void *wchan);
63 /* Wake all tasks sleeping on wchan */
65 ao_wakeup(__xdata void *wchan);
67 /* set an alarm to go off in 'delay' ticks */
69 ao_alarm(uint16_t delay);
71 /* Clear any pending alarm */
75 /* Yield the processor to another task */
77 ao_yield(void) ao_arch_naked_declare;
79 /* Add a task to the run queue */
81 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
83 /* Terminate the current task */
87 /* Dump task info to console */
91 /* Start the scheduler. This will not return */
93 ao_start_scheduler(void);
99 #define AO_PANIC_NO_TASK 1 /* AO_NUM_TASKS is not large enough */
100 #define AO_PANIC_DMA 2 /* Attempt to start DMA while active */
101 #define AO_PANIC_MUTEX 3 /* Mis-using mutex API */
102 #define AO_PANIC_EE 4 /* Mis-using eeprom API */
103 #define AO_PANIC_LOG 5 /* Failing to read/write log data */
104 #define AO_PANIC_CMD 6 /* Too many command sets registered */
105 #define AO_PANIC_STDIO 7 /* Too many stdio handlers registered */
106 #define AO_PANIC_REBOOT 8 /* Reboot failed */
107 #define AO_PANIC_FLASH 9 /* Invalid flash part (or wrong blocksize) */
108 #define AO_PANIC_USB 10 /* Trying to send USB packet while busy */
109 #define AO_PANIC_BT 11 /* Communications with bluetooth device failed */
111 /* Stop the operating system, beeping and blinking the reason */
113 ao_panic(uint8_t reason);
119 /* Our timer runs at 100Hz */
121 #define AO_MS_TO_TICKS(ms) ((ms) / (1000 / AO_HERTZ))
122 #define AO_SEC_TO_TICKS(s) ((s) * AO_HERTZ)
124 /* Returns the current time in ticks */
128 /* Suspend the current task until ticks time has passed */
130 ao_delay(uint16_t ticks);
132 /* Set the ADC interval */
134 ao_timer_set_adc_interval(uint8_t interval) __critical;
136 /* Timer interrupt */
138 ao_timer_isr(void) ao_arch_interrupt(9);
140 /* Initialize the timer */
144 /* Initialize the hardware clock. Must be called first */
149 * One set of samples read from the A/D converter or telemetry
158 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
159 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
163 * A/D data is stored in a ring, with the next sample to be written
166 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
167 extern volatile __data uint8_t ao_adc_head;
169 extern volatile __xdata uint16_t ao_accel_ref[AO_ADC_RING];
172 /* Trigger a conversion sequence (called from the timer interrupt) */
176 /* Suspend the current task until another A/D sample is converted */
180 /* Get a copy of the last complete A/D sample set */
182 ao_adc_get(__xdata struct ao_adc *packet);
184 /* The A/D interrupt handler */
187 ao_adc_isr(void) ao_arch_interrupt(1);
189 /* Initialize the A/D converter */
200 * Various pre-defined beep frequencies
202 * frequency = 1/2 (24e6/32) / beep
205 #define AO_BEEP_LOW 150 /* 2500Hz */
206 #define AO_BEEP_MID 94 /* 3989Hz */
207 #define AO_BEEP_HIGH 75 /* 5000Hz */
208 #define AO_BEEP_OFF 0 /* off */
210 #define AO_BEEP_g 240 /* 1562.5Hz */
211 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
212 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
213 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
214 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
215 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
216 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
217 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
218 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
219 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
220 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
221 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
222 #define AO_BEEP_gg 120 /* 3125Hz */
223 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
224 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
225 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
226 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
227 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
228 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
229 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
230 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
231 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
232 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
233 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
234 #define AO_BEEP_ggg 60 /* 6250Hz */
236 /* Set the beeper to the specified tone */
238 ao_beep(uint8_t beep);
240 /* Turn on the beeper for the specified time */
242 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
244 /* Initialize the beeper */
252 #define AO_LED_NONE 0
254 /* Turn on the specified LEDs */
256 ao_led_on(uint8_t colors);
258 /* Turn off the specified LEDs */
260 ao_led_off(uint8_t colors);
262 /* Set all of the LEDs to the specified state */
264 ao_led_set(uint8_t colors);
266 /* Toggle the specified LEDs */
268 ao_led_toggle(uint8_t colors);
270 /* Turn on the specified LEDs for the indicated interval */
272 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
274 /* Initialize the LEDs */
276 ao_led_init(uint8_t enable);
282 /* Put one character to the USB output queue */
284 ao_usb_putchar(char c);
286 /* Get one character from the USB input queue */
288 ao_usb_getchar(void);
290 /* Poll for a charcter on the USB input queue.
291 * returns AO_READ_AGAIN if none are available
294 ao_usb_pollchar(void);
296 /* Flush the USB output queue */
301 /* USB interrupt handler */
303 ao_usb_isr(void) ao_arch_interrupt(6);
306 /* Enable the USB controller */
310 /* Disable the USB controller */
312 ao_usb_disable(void);
314 /* Initialize the USB system */
319 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
328 ao_cmd_lex_error = 1,
329 ao_cmd_syntax_error = 2,
332 extern __pdata uint16_t ao_cmd_lex_i;
333 extern __pdata uint32_t ao_cmd_lex_u32;
334 extern __pdata char ao_cmd_lex_c;
335 extern __pdata enum ao_cmd_status ao_cmd_status;
341 ao_cmd_put8(uint8_t v);
344 ao_cmd_put16(uint16_t v);
350 ao_cmd_hexchar(char c);
353 ao_cmd_hexbyte(void);
359 ao_cmd_decimal(void);
362 ao_match_word(__code char *word);
370 ao_cmd_register(__code struct ao_cmds *cmds);
377 * Provided by an external module to filter raw command lines
387 /* Allocate a DMA channel. the 'done' parameter will be set when the
388 * dma is finished and will be used to wakeup any waiters
392 ao_dma_alloc(__xdata uint8_t * done);
394 /* Setup a DMA channel */
396 ao_dma_set_transfer(uint8_t id,
397 void __xdata *srcaddr,
398 void __xdata *dstaddr,
403 /* Start a DMA channel */
405 ao_dma_start(uint8_t id);
407 /* Manually trigger a DMA channel */
409 ao_dma_trigger(uint8_t id);
411 /* Abort a running DMA transfer */
413 ao_dma_abort(uint8_t id);
415 /* DMA interrupt routine */
417 ao_dma_isr(void) ao_arch_interrupt(8);
424 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
427 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
430 * Storage interface, provided by one of the eeprom or flash
434 /* Total bytes of available storage */
435 extern __pdata uint32_t ao_storage_total;
437 /* Block size - device is erased in these units. At least 256 bytes */
438 extern __pdata uint32_t ao_storage_block;
440 /* Byte offset of config block. Will be ao_storage_block bytes long */
441 extern __pdata uint32_t ao_storage_config;
443 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
444 extern __pdata uint16_t ao_storage_unit;
446 #define AO_STORAGE_ERASE_LOG (ao_storage_config + AO_CONFIG_MAX_SIZE)
448 /* Initialize above values. Can only be called once the OS is running */
450 ao_storage_setup(void) __reentrant;
452 /* Write data. Returns 0 on failure, 1 on success */
454 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
456 /* Read data. Returns 0 on failure, 1 on success */
458 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
460 /* Erase a block of storage. This always clears ao_storage_block bytes */
462 ao_storage_erase(uint32_t pos) __reentrant;
464 /* Flush any pending writes to stable storage */
466 ao_storage_flush(void) __reentrant;
468 /* Initialize the storage code */
470 ao_storage_init(void);
473 * Low-level functions wrapped by ao_storage.c
476 /* Read data within a storage unit */
478 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
480 /* Write data within a storage unit */
482 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
484 /* Initialize low-level device bits */
486 ao_storage_device_init(void);
488 /* Print out information about flash chips */
490 ao_storage_device_info(void) __reentrant;
496 /* We record flight numbers in the first record of
497 * the log. Tasks may wait for this to be initialized
498 * by sleeping on this variable.
500 extern __xdata uint16_t ao_flight_number;
502 extern __pdata uint32_t ao_log_current_pos;
503 extern __pdata uint32_t ao_log_end_pos;
504 extern __pdata uint32_t ao_log_start_pos;
505 extern __xdata uint8_t ao_log_running;
506 extern __pdata enum flight_state ao_log_state;
508 /* required functions from the underlying log system */
510 #define AO_LOG_FORMAT_UNKNOWN 0 /* unknown; altosui will have to guess */
511 #define AO_LOG_FORMAT_FULL 1 /* 8 byte typed log records */
512 #define AO_LOG_FORMAT_TINY 2 /* two byte state/baro records */
513 #define AO_LOG_FORMAT_TELEMETRY 3 /* 32 byte ao_telemetry records */
514 #define AO_LOG_FORMAT_TELESCIENCE 4 /* 32 byte typed telescience records */
515 #define AO_LOG_FORMAT_NONE 127 /* No log at all */
517 extern __code uint8_t ao_log_format;
519 /* Return the flight number from the given log slot, 0 if none */
521 ao_log_flight(uint8_t slot);
527 /* Logging thread main routine */
531 /* functions provided in ao_log.c */
533 /* Figure out the current flight number */
535 ao_log_scan(void) __reentrant;
537 /* Return the position of the start of the given log slot */
539 ao_log_pos(uint8_t slot);
541 /* Start logging to eeprom */
549 /* Initialize the logging system */
553 /* Write out the current flight number to the erase log */
555 ao_log_write_erase(uint8_t pos);
557 /* Returns true if there are any logs stored in eeprom */
559 ao_log_present(void);
561 /* Returns true if there is no more storage space available */
570 * The data log is recorded in the eeprom as a sequence
573 * Each packet starts with a 4-byte header that has the
574 * packet type, the packet checksum and the tick count. Then
575 * they all contain 2 16 bit values which hold packet-specific
578 * For each flight, the first packet
579 * is FLIGHT packet, indicating the serial number of the
580 * device and a unique number marking the number of flights
581 * recorded by this device.
583 * During flight, data from the accelerometer and barometer
584 * are recorded in SENSOR packets, using the raw 16-bit values
585 * read from the A/D converter.
587 * Also during flight, but at a lower rate, the deployment
588 * sensors are recorded in DEPLOY packets. The goal here is to
589 * detect failure in the deployment circuits.
591 * STATE packets hold state transitions as the flight computer
592 * transitions through different stages of the flight.
594 #define AO_LOG_FLIGHT 'F'
595 #define AO_LOG_SENSOR 'A'
596 #define AO_LOG_TEMP_VOLT 'T'
597 #define AO_LOG_DEPLOY 'D'
598 #define AO_LOG_STATE 'S'
599 #define AO_LOG_GPS_TIME 'G'
600 #define AO_LOG_GPS_LAT 'N'
601 #define AO_LOG_GPS_LON 'W'
602 #define AO_LOG_GPS_ALT 'H'
603 #define AO_LOG_GPS_SAT 'V'
604 #define AO_LOG_GPS_DATE 'Y'
606 #define AO_LOG_POS_NONE (~0UL)
608 struct ao_log_record {
614 int16_t ground_accel;
639 int32_t gps_latitude;
640 int32_t gps_longitude;
663 /* Write a record to the eeprom log */
665 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
671 enum ao_flight_state {
672 ao_flight_startup = 0,
678 ao_flight_drogue = 6,
680 ao_flight_landed = 8,
681 ao_flight_invalid = 9
684 extern __pdata enum ao_flight_state ao_flight_state;
686 extern __pdata uint16_t ao_launch_time;
687 extern __pdata uint8_t ao_flight_force_idle;
693 /* Initialize flight thread */
695 ao_flight_init(void);
702 ao_flight_nano_init(void);
709 * Barometer calibration
711 * We directly sample the barometer. The specs say:
713 * Pressure range: 15-115 kPa
714 * Voltage at 115kPa: 2.82
715 * Output scale: 27mV/kPa
717 * If we want to detect launch with the barometer, we need
718 * a large enough bump to not be fooled by noise. At typical
719 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
720 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
721 * As all of our calculations are done in 16 bits, we'll actually see a change
722 * of 16 times this though
724 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
727 /* Accelerometer calibration
729 * We're sampling the accelerometer through a resistor divider which
730 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
731 * That goes into the cc1111 A/D converter, which is running at 11 bits
732 * of precision with the bits in the MSB of the 16 bit value. Only positive
733 * values are used, so values should range from 0-32752 for 0-3.3V. The
734 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
735 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
736 * for a final computation of:
738 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
740 * Zero g was measured at 16000 (we would expect 16384).
741 * Note that this value is only require to tell if the
742 * rocket is standing upright. Once that is determined,
743 * the value of the accelerometer is averaged for 100 samples
744 * to find the resting accelerometer value, which is used
745 * for all further flight computations
748 #define GRAVITY 9.80665
751 * Above this height, the baro sensor doesn't work
753 #define AO_MAX_BARO_HEIGHT 12000
756 * Above this speed, baro measurements are unreliable
758 #define AO_MAX_BARO_SPEED 200
760 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
763 * Speed and acceleration are scaled by 16 to provide a bit more
764 * resolution while still having reasonable range. Note that this
765 * limits speed to 2047m/s (around mach 6) and acceleration to
766 * 2047m/s² (over 200g)
769 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
770 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
771 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
773 extern __pdata uint16_t ao_sample_tick; /* time of last data */
774 extern __pdata int16_t ao_sample_pres; /* most recent pressure sensor reading */
775 extern __pdata int16_t ao_sample_alt; /* MSL of ao_sample_pres */
776 extern __pdata int16_t ao_sample_height; /* AGL of ao_sample_pres */
777 extern __data uint8_t ao_sample_adc; /* Ring position of last processed sample */
780 extern __pdata int16_t ao_sample_accel; /* most recent accel sensor reading */
783 extern __pdata int16_t ao_ground_pres; /* startup pressure */
784 extern __pdata int16_t ao_ground_height; /* MSL of ao_ground_pres */
787 extern __pdata int16_t ao_ground_accel; /* startup acceleration */
788 extern __pdata int16_t ao_accel_2g; /* factory accel calibration */
789 extern __pdata int32_t ao_accel_scale; /* sensor to m/s² conversion */
792 void ao_sample_init(void);
794 /* returns FALSE in preflight mode, TRUE in flight mode */
795 uint8_t ao_sample(void);
801 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
802 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
803 #define from_fix(x) ((x) >> 16)
805 extern __pdata int16_t ao_height; /* meters */
806 extern __pdata int16_t ao_speed; /* m/s * 16 */
807 extern __pdata int16_t ao_accel; /* m/s² * 16 */
808 extern __pdata int16_t ao_max_height; /* max of ao_height */
809 extern __pdata int16_t ao_avg_height; /* running average of height */
811 extern __pdata int16_t ao_error_h;
812 extern __pdata int16_t ao_error_h_sq_avg;
815 extern __pdata int16_t ao_error_a;
818 void ao_kalman(void);
825 ao_report_init(void);
830 * Given raw data, convert to SI units
833 /* pressure from the sensor to altitude in meters */
835 ao_pres_to_altitude(int16_t pres) __reentrant;
838 ao_altitude_to_pres(int16_t alt) __reentrant;
841 ao_temp_to_dC(int16_t temp) __reentrant;
846 * debug another telemetrum board
849 /* Send a byte to the dbg target */
851 ao_dbg_send_byte(uint8_t byte);
853 /* Receive a byte from the dbg target */
855 ao_dbg_recv_byte(void);
857 /* Start a bulk transfer to/from dbg target memory */
859 ao_dbg_start_transfer(uint16_t addr);
861 /* End a bulk transfer to/from dbg target memory */
863 ao_dbg_end_transfer(void);
865 /* Write a byte to dbg target memory */
867 ao_dbg_write_byte(uint8_t byte);
869 /* Read a byte from dbg target memory */
871 ao_dbg_read_byte(void);
873 /* Enable dbg mode, switching use of the pins */
875 ao_dbg_debug_mode(void);
877 /* Reset the dbg target */
889 #error Please define HAS_SERIAL_1
893 #ifndef USE_SERIAL_STDIN
894 #error Please define USE_SERIAL_STDIN
898 ao_serial_rx1_isr(void) ao_arch_interrupt(3);
901 ao_serial_tx1_isr(void) ao_arch_interrupt(14);
904 ao_serial_getchar(void) __critical;
908 ao_serial_pollchar(void) __critical;
911 ao_serial_set_stdin(uint8_t stdin);
915 ao_serial_putchar(char c) __critical;
918 ao_serial_drain(void) __critical;
920 #define AO_SERIAL_SPEED_4800 0
921 #define AO_SERIAL_SPEED_9600 1
922 #define AO_SERIAL_SPEED_19200 2
923 #define AO_SERIAL_SPEED_57600 3
926 ao_serial_set_speed(uint8_t speed);
929 ao_serial_init(void);
936 extern __xdata uint8_t ao_spi_mutex;
938 #define ao_spi_get_mask(reg,mask) do {\
939 ao_mutex_get(&ao_spi_mutex); \
943 #define ao_spi_put_mask(reg,mask) do { \
945 ao_mutex_put(&ao_spi_mutex); \
948 #define ao_spi_get_bit(bit) do {\
949 ao_mutex_get(&ao_spi_mutex); \
953 #define ao_spi_put_bit(bit) do { \
955 ao_mutex_put(&ao_spi_mutex); \
959 * The SPI mutex must be held to call either of these
960 * functions -- this mutex covers the entire SPI operation,
961 * from chip select low to chip select high
965 ao_spi_send(void __xdata *block, uint16_t len) __reentrant;
968 ao_spi_recv(void __xdata *block, uint16_t len) __reentrant;
978 ao_spi_read(uint8_t *buf, uint8_t len);
981 ao_spi_write(uint8_t *buf, uint8_t len);
984 ao_spi_slave_init(void);
986 /* This must be defined by the product; it will get called when chip
987 * select goes low, at which point it should use ao_spi_read and
988 * ao_spi_write to deal with the request
997 #define AO_MAX_CALLSIGN 8
998 #define AO_MAX_VERSION 8
999 #define AO_MAX_TELEMETRY 128
1001 struct ao_telemetry_generic {
1002 uint16_t serial; /* 0 */
1003 uint16_t tick; /* 2 */
1004 uint8_t type; /* 4 */
1005 uint8_t payload[27]; /* 5 */
1009 #define AO_TELEMETRY_SENSOR_TELEMETRUM 0x01
1010 #define AO_TELEMETRY_SENSOR_TELEMINI 0x02
1011 #define AO_TELEMETRY_SENSOR_TELENANO 0x03
1013 struct ao_telemetry_sensor {
1014 uint16_t serial; /* 0 */
1015 uint16_t tick; /* 2 */
1016 uint8_t type; /* 4 */
1018 uint8_t state; /* 5 flight state */
1019 int16_t accel; /* 6 accelerometer (TM only) */
1020 int16_t pres; /* 8 pressure sensor */
1021 int16_t temp; /* 10 temperature sensor */
1022 int16_t v_batt; /* 12 battery voltage */
1023 int16_t sense_d; /* 14 drogue continuity sense (TM/Tm) */
1024 int16_t sense_m; /* 16 main continuity sense (TM/Tm) */
1026 int16_t acceleration; /* 18 m/s² * 16 */
1027 int16_t speed; /* 20 m/s * 16 */
1028 int16_t height; /* 22 m */
1030 int16_t ground_pres; /* 24 average pres on pad */
1031 int16_t ground_accel; /* 26 average accel on pad */
1032 int16_t accel_plus_g; /* 28 accel calibration at +1g */
1033 int16_t accel_minus_g; /* 30 accel calibration at -1g */
1037 #define AO_TELEMETRY_CONFIGURATION 0x04
1039 struct ao_telemetry_configuration {
1040 uint16_t serial; /* 0 */
1041 uint16_t tick; /* 2 */
1042 uint8_t type; /* 4 */
1044 uint8_t device; /* 5 device type */
1045 uint16_t flight; /* 6 flight number */
1046 uint8_t config_major; /* 8 Config major version */
1047 uint8_t config_minor; /* 9 Config minor version */
1048 uint16_t apogee_delay; /* 10 Apogee deploy delay in seconds */
1049 uint16_t main_deploy; /* 12 Main deploy alt in meters */
1050 uint16_t flight_log_max; /* 14 Maximum flight log size in kB */
1051 char callsign[AO_MAX_CALLSIGN]; /* 16 Radio operator identity */
1052 char version[AO_MAX_VERSION]; /* 24 Software version */
1056 #define AO_TELEMETRY_LOCATION 0x05
1058 #define AO_GPS_MODE_NOT_VALID 'N'
1059 #define AO_GPS_MODE_AUTONOMOUS 'A'
1060 #define AO_GPS_MODE_DIFFERENTIAL 'D'
1061 #define AO_GPS_MODE_ESTIMATED 'E'
1062 #define AO_GPS_MODE_MANUAL 'M'
1063 #define AO_GPS_MODE_SIMULATED 'S'
1065 struct ao_telemetry_location {
1066 uint16_t serial; /* 0 */
1067 uint16_t tick; /* 2 */
1068 uint8_t type; /* 4 */
1070 uint8_t flags; /* 5 Number of sats and other flags */
1071 int16_t altitude; /* 6 GPS reported altitude (m) */
1072 int32_t latitude; /* 8 latitude (degrees * 10⁷) */
1073 int32_t longitude; /* 12 longitude (degrees * 10⁷) */
1074 uint8_t year; /* 16 (- 2000) */
1075 uint8_t month; /* 17 (1-12) */
1076 uint8_t day; /* 18 (1-31) */
1077 uint8_t hour; /* 19 (0-23) */
1078 uint8_t minute; /* 20 (0-59) */
1079 uint8_t second; /* 21 (0-59) */
1080 uint8_t pdop; /* 22 (m * 5) */
1081 uint8_t hdop; /* 23 (m * 5) */
1082 uint8_t vdop; /* 24 (m * 5) */
1083 uint8_t mode; /* 25 */
1084 uint16_t ground_speed; /* 26 cm/s */
1085 int16_t climb_rate; /* 28 cm/s */
1086 uint8_t course; /* 30 degrees / 2 */
1087 uint8_t unused[1]; /* 31 */
1091 #define AO_TELEMETRY_SATELLITE 0x06
1093 struct ao_telemetry_satellite_info {
1098 struct ao_telemetry_satellite {
1099 uint16_t serial; /* 0 */
1100 uint16_t tick; /* 2 */
1101 uint8_t type; /* 4 */
1102 uint8_t channels; /* 5 number of reported sats */
1104 struct ao_telemetry_satellite_info sats[12]; /* 6 */
1105 uint8_t unused[2]; /* 30 */
1109 #define AO_TELEMETRY_COMPANION 0x07
1111 #define AO_COMPANION_MAX_CHANNELS 12
1113 struct ao_telemetry_companion {
1114 uint16_t serial; /* 0 */
1115 uint16_t tick; /* 2 */
1116 uint8_t type; /* 4 */
1117 uint8_t board_id; /* 5 */
1119 uint8_t update_period; /* 6 */
1120 uint8_t channels; /* 7 */
1121 uint16_t companion_data[AO_COMPANION_MAX_CHANNELS]; /* 8 */
1125 /* #define AO_SEND_ALL_BARO */
1127 #define AO_TELEMETRY_BARO 0x80
1130 * This packet allows the full sampling rate baro
1131 * data to be captured over the RF link so that the
1132 * flight software can be tested using 'real' data.
1134 * Along with this telemetry packet, the flight
1135 * code is modified to send full-rate telemetry all the time
1136 * and never send an RDF tone; this ensure that the full radio
1137 * link is available.
1139 struct ao_telemetry_baro {
1140 uint16_t serial; /* 0 */
1141 uint16_t tick; /* 2 */
1142 uint8_t type; /* 4 */
1143 uint8_t samples; /* 5 number samples */
1145 int16_t baro[12]; /* 6 samples */
1149 union ao_telemetry_all {
1150 struct ao_telemetry_generic generic;
1151 struct ao_telemetry_sensor sensor;
1152 struct ao_telemetry_configuration configuration;
1153 struct ao_telemetry_location location;
1154 struct ao_telemetry_satellite satellite;
1155 struct ao_telemetry_companion companion;
1156 struct ao_telemetry_baro baro;
1163 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
1164 #define AO_GPS_NUM_SAT_SHIFT (0)
1166 #define AO_GPS_VALID (1 << 4)
1167 #define AO_GPS_RUNNING (1 << 5)
1168 #define AO_GPS_DATE_VALID (1 << 6)
1169 #define AO_GPS_COURSE_VALID (1 << 7)
1171 extern __pdata uint16_t ao_gps_tick;
1172 extern __xdata uint8_t ao_gps_mutex;
1173 extern __xdata struct ao_telemetry_location ao_gps_data;
1174 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
1176 struct ao_gps_orig {
1184 int32_t latitude; /* degrees * 10⁷ */
1185 int32_t longitude; /* degrees * 10⁷ */
1186 int16_t altitude; /* m */
1187 uint16_t ground_speed; /* cm/s */
1188 uint8_t course; /* degrees / 2 */
1189 uint8_t hdop; /* * 5 */
1190 int16_t climb_rate; /* cm/s */
1191 uint16_t h_error; /* m */
1192 uint16_t v_error; /* m */
1195 struct ao_gps_sat_orig {
1200 #define AO_MAX_GPS_TRACKING 12
1202 struct ao_gps_tracking_orig {
1204 struct ao_gps_sat_orig sats[AO_MAX_GPS_TRACKING];
1211 ao_gps_print(__xdata struct ao_gps_orig *gps_data);
1214 ao_gps_tracking_print(__xdata struct ao_gps_tracking_orig *gps_tracking_data);
1224 ao_gps_report(void);
1227 ao_gps_report_init(void);
1230 * ao_telemetry_orig.c
1233 struct ao_telemetry_orig {
1236 uint8_t flight_state;
1238 int16_t ground_accel;
1247 int16_t ground_pres;
1248 int16_t accel_plus_g;
1249 int16_t accel_minus_g;
1251 struct ao_gps_orig gps;
1252 char callsign[AO_MAX_CALLSIGN];
1253 struct ao_gps_tracking_orig gps_tracking;
1256 struct ao_telemetry_tiny {
1259 uint8_t flight_state;
1260 int16_t height; /* AGL in meters */
1261 int16_t speed; /* in m/s * 16 */
1262 int16_t accel; /* in m/s² * 16 */
1263 int16_t ground_pres; /* sensor units */
1264 struct ao_adc adc; /* raw ADC readings */
1265 char callsign[AO_MAX_CALLSIGN];
1268 struct ao_telemetry_orig_recv {
1269 struct ao_telemetry_orig telemetry_orig;
1274 struct ao_telemetry_tiny_recv {
1275 struct ao_telemetry_tiny telemetry_tiny;
1281 * ao_radio_recv tacks on rssi and status bytes
1284 struct ao_telemetry_raw_recv {
1285 uint8_t packet[AO_MAX_TELEMETRY + 2];
1288 /* Set delay between telemetry reports (0 to disable) */
1290 #ifdef AO_SEND_ALL_BARO
1291 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(100)
1292 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(100)
1293 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(100)
1295 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(1000)
1296 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(100)
1297 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(1000)
1301 ao_telemetry_set_interval(uint16_t interval);
1304 ao_rdf_set(uint8_t rdf);
1307 ao_telemetry_init(void);
1310 ao_telemetry_orig_init(void);
1313 ao_telemetry_tiny_init(void);
1319 extern __xdata uint8_t ao_radio_dma;
1320 extern __xdata uint8_t ao_radio_dma_done;
1321 extern __xdata uint8_t ao_radio_done;
1322 extern __xdata uint8_t ao_radio_mutex;
1325 ao_radio_general_isr(void) ao_arch_interrupt(16);
1328 ao_radio_get(uint8_t len);
1330 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
1333 ao_radio_set_packet(void);
1336 ao_radio_send(__xdata void *data, uint8_t size) __reentrant;
1339 ao_radio_recv(__xdata void *data, uint8_t size) __reentrant;
1342 ao_radio_recv_abort(void);
1345 * Compute the packet length as follows:
1347 * 2000 bps (for a 1kHz tone)
1348 * so, for 'ms' milliseconds, we need
1349 * 2 * ms bits, or ms / 4 bytes
1352 #define AO_MS_TO_RDF_LEN(ms) ((ms) > 255 * 4 ? 255 : ((ms) >> 2))
1355 ao_radio_rdf(uint8_t pkt_len);
1358 ao_radio_rdf_abort(void);
1361 ao_radio_idle(void);
1364 ao_radio_init(void);
1370 extern const char const * const ao_state_names[];
1372 #define AO_MONITOR_RING 8
1375 struct ao_telemetry_raw_recv raw;
1376 struct ao_telemetry_orig_recv orig;
1377 struct ao_telemetry_tiny_recv tiny;
1380 extern __xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
1382 #define ao_monitor_ring_next(n) (((n) + 1) & (AO_MONITOR_RING - 1))
1384 extern __data uint8_t ao_monitoring;
1385 extern __data uint8_t ao_monitor_head;
1390 #define AO_MONITORING_OFF 0
1391 #define AO_MONITORING_ORIG 1
1392 #define AO_MONITORING_TINY 2
1395 ao_set_monitor(uint8_t monitoring);
1398 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
1404 #define AO_READ_AGAIN ((char) -1)
1407 char (*pollchar)(void);
1408 void (*putchar)(char c) __reentrant;
1409 void (*flush)(void);
1413 extern __xdata struct ao_stdio ao_stdios[];
1414 extern __pdata int8_t ao_cur_stdio;
1415 extern __pdata int8_t ao_num_stdios;
1420 extern __xdata uint8_t ao_stdin_ready;
1426 ao_add_stdio(char (*pollchar)(void),
1427 void (*putchar)(char) __reentrant,
1428 void (*flush)(void)) __reentrant;
1435 ao_igniter_drogue = 0,
1440 ao_ignite(enum ao_igniter igniter);
1442 enum ao_igniter_status {
1443 ao_igniter_unknown, /* unknown status (ambiguous voltage) */
1444 ao_igniter_ready, /* continuity detected */
1445 ao_igniter_active, /* igniter firing */
1446 ao_igniter_open, /* open circuit detected */
1449 struct ao_ignition {
1455 extern __xdata struct ao_ignition ao_ignition[2];
1457 enum ao_igniter_status
1458 ao_igniter_status(enum ao_igniter igniter);
1460 extern __pdata uint8_t ao_igniter_present;
1463 ao_ignite_set_pins(void);
1466 ao_igniter_init(void);
1472 #define AO_CONFIG_MAJOR 1
1473 #define AO_CONFIG_MINOR 9
1474 #define AO_AES_LEN 16
1479 uint16_t main_deploy;
1480 int16_t accel_plus_g; /* changed for minor version 2 */
1481 uint8_t radio_channel;
1482 char callsign[AO_MAX_CALLSIGN + 1];
1483 uint8_t apogee_delay; /* minor version 1 */
1484 int16_t accel_minus_g; /* minor version 2 */
1485 uint32_t radio_cal; /* minor version 3 */
1486 uint32_t flight_log_max; /* minor version 4 */
1487 uint8_t ignite_mode; /* minor version 5 */
1488 uint8_t pad_orientation; /* minor version 6 */
1489 uint32_t radio_setting; /* minor version 7 */
1490 uint8_t radio_enable; /* minor version 8 */
1491 uint8_t aes_key[AO_AES_LEN]; /* minor version 9 */
1494 #define AO_IGNITE_MODE_DUAL 0
1495 #define AO_IGNITE_MODE_APOGEE 1
1496 #define AO_IGNITE_MODE_MAIN 2
1498 #define AO_PAD_ORIENTATION_ANTENNA_UP 0
1499 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
1501 extern __xdata struct ao_config ao_config;
1503 #define AO_CONFIG_MAX_SIZE 128
1506 ao_config_get(void);
1509 ao_config_put(void);
1512 ao_config_init(void);
1519 ao_rssi_set(int rssi_value);
1522 ao_rssi_init(uint8_t rssi_led);
1527 * values which need to be defined for
1528 * each instance of a product
1531 extern const char ao_version[];
1532 extern const char ao_manufacturer[];
1533 extern const char ao_product[];
1539 #define AO_FIFO_SIZE 32
1544 char fifo[AO_FIFO_SIZE];
1547 #define ao_fifo_insert(f,c) do { \
1548 (f).fifo[(f).insert] = (c); \
1549 (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1552 #define ao_fifo_remove(f,c) do {\
1553 c = (f).fifo[(f).remove]; \
1554 (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1557 #define ao_fifo_full(f) ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1558 #define ao_fifo_empty(f) ((f).insert == (f).remove)
1563 * Packet-based command interface
1566 #define AO_PACKET_MAX 64
1567 #define AO_PACKET_SYN (uint8_t) 0xff
1574 uint8_t d[AO_PACKET_MAX];
1575 uint8_t callsign[AO_MAX_CALLSIGN];
1578 struct ao_packet_recv {
1579 struct ao_packet packet;
1584 extern __xdata struct ao_packet_recv ao_rx_packet;
1585 extern __xdata struct ao_packet ao_tx_packet;
1586 extern __xdata struct ao_task ao_packet_task;
1587 extern __xdata uint8_t ao_packet_enable;
1588 extern __xdata uint8_t ao_packet_master_sleeping;
1589 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1592 ao_packet_send(void);
1595 ao_packet_recv(void);
1598 ao_packet_flush(void);
1601 ao_packet_putchar(char c) __reentrant;
1604 ao_packet_pollchar(void) __critical;
1606 /* ao_packet_master.c */
1609 ao_packet_master_init(void);
1611 /* ao_packet_slave.c */
1614 ao_packet_slave_start(void);
1617 ao_packet_slave_stop(void);
1620 ao_packet_slave_init(uint8_t enable);
1624 /* If bt_link is on P2, this interrupt is shared by USB, so the USB
1625 * code calls this function. Otherwise, it's a regular ISR.
1638 /* ao_companion.c */
1640 #define AO_COMPANION_SETUP 1
1641 #define AO_COMPANION_FETCH 2
1642 #define AO_COMPANION_NOTIFY 3
1644 struct ao_companion_command {
1646 uint8_t flight_state;
1652 struct ao_companion_setup {
1654 uint16_t board_id_inverse;
1655 uint8_t update_period;
1659 extern __pdata uint8_t ao_companion_running;
1660 extern __xdata uint8_t ao_companion_mutex;
1661 extern __xdata struct ao_companion_command ao_companion_command;
1662 extern __xdata struct ao_companion_setup ao_companion_setup;
1663 extern __xdata uint16_t ao_companion_data[AO_COMPANION_MAX_CHANNELS];
1666 ao_companion_init(void);
1675 __xdata uint8_t ao_aes_mutex;
1677 /* AES keys and blocks are 128 bits */
1685 ao_aes_isr(void) __interrupt 4;
1689 ao_aes_set_mode(enum ao_aes_mode mode);
1692 ao_aes_set_key(__xdata uint8_t *in);
1695 ao_aes_zero_iv(void);
1698 ao_aes_run(__xdata uint8_t *in,
1699 __xdata uint8_t *out);
1704 /* ao_radio_cmac.c */
1707 ao_radio_cmac_send(__xdata void *packet, uint8_t len) __reentrant;
1709 #define AO_RADIO_CMAC_OK 0
1710 #define AO_RADIO_CMAC_LEN_ERROR -1
1711 #define AO_RADIO_CMAC_CRC_ERROR -2
1712 #define AO_RADIO_CMAC_MAC_ERROR -3
1713 #define AO_RADIO_CMAC_TIMEOUT -4
1716 ao_radio_cmac_recv(__xdata void *packet, uint8_t len, uint16_t timeout) __reentrant;
1719 ao_radio_cmac_init(void);
1723 struct ao_launch_command {
1731 #define AO_LAUNCH_QUERY 1
1733 struct ao_launch_query {
1739 uint8_t igniter_status;
1742 #define AO_LAUNCH_ARM 2
1743 #define AO_LAUNCH_FIRE 3
1746 ao_launch_init(void);
1752 #define AO_LOG_TELESCIENCE_START ((uint8_t) 's')
1753 #define AO_LOG_TELESCIENCE_DATA ((uint8_t) 'd')
1755 #define AO_LOG_TELESCIENCE_NUM_ADC 12
1757 struct ao_log_telescience {
1764 uint16_t adc[AO_LOG_TELESCIENCE_NUM_ADC];
1767 #define AO_LOG_SINGLE_SIZE 32
1769 union ao_log_single {
1770 struct ao_log_telescience telescience;
1771 union ao_telemetry_all telemetry;
1772 uint8_t bytes[AO_LOG_SINGLE_SIZE];
1775 extern __xdata union ao_log_single ao_log_single_write_data;
1776 extern __xdata union ao_log_single ao_log_single_read_data;
1779 ao_log_single_extra_query(void);
1782 ao_log_single_list(void);
1785 ao_log_single_main(void);
1788 ao_log_single_write(void);
1791 ao_log_single_read(uint32_t pos);
1794 ao_log_single_start(void);
1797 ao_log_single_stop(void);
1800 ao_log_single_restart(void);
1803 ao_log_single_set(void);
1806 ao_log_single_delete(void);
1809 ao_log_single_init(void);
1812 ao_log_single(void);
1818 #define AO_TELEPYRO_NUM_ADC 9
1821 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
1822 #define ao_xmemset(d,v,c) memset(d,v,c)
1823 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)