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)
35 #ifndef PDATA_TO_XDATA
36 #define PDATA_TO_XDATA(a) (a)
39 #define CODE_TO_XDATA(a) (a)
44 __xdata void *wchan; /* current wait channel (NULL if running) */
45 uint16_t alarm; /* abort ao_sleep time */
46 ao_arch_task_members /* any architecture-specific fields */
47 uint8_t task_id; /* unique id */
48 __code char *name; /* task name */
49 uint8_t stack[AO_STACK_SIZE]; /* saved stack */
52 extern __xdata struct ao_task *__data ao_cur_task;
54 #define AO_NUM_TASKS 16 /* maximum number of tasks */
55 #define AO_NO_TASK 0 /* no task id */
61 /* Suspend the current task until wchan is awoken.
67 ao_sleep(__xdata void *wchan);
69 /* Wake all tasks sleeping on wchan */
71 ao_wakeup(__xdata void *wchan);
73 /* set an alarm to go off in 'delay' ticks */
75 ao_alarm(uint16_t delay);
77 /* Clear any pending alarm */
81 /* Yield the processor to another task */
83 ao_yield(void) ao_arch_naked_declare;
85 /* Add a task to the run queue */
87 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
89 /* Terminate the current task */
93 /* Dump task info to console */
97 /* Start the scheduler. This will not return */
99 ao_start_scheduler(void);
105 #define AO_PANIC_NO_TASK 1 /* AO_NUM_TASKS is not large enough */
106 #define AO_PANIC_DMA 2 /* Attempt to start DMA while active */
107 #define AO_PANIC_MUTEX 3 /* Mis-using mutex API */
108 #define AO_PANIC_EE 4 /* Mis-using eeprom API */
109 #define AO_PANIC_LOG 5 /* Failing to read/write log data */
110 #define AO_PANIC_CMD 6 /* Too many command sets registered */
111 #define AO_PANIC_STDIO 7 /* Too many stdio handlers registered */
112 #define AO_PANIC_REBOOT 8 /* Reboot failed */
113 #define AO_PANIC_FLASH 9 /* Invalid flash part (or wrong blocksize) */
114 #define AO_PANIC_USB 10 /* Trying to send USB packet while busy */
115 #define AO_PANIC_BT 11 /* Communications with bluetooth device failed */
117 /* Stop the operating system, beeping and blinking the reason */
119 ao_panic(uint8_t reason);
125 /* Our timer runs at 100Hz */
127 #define AO_MS_TO_TICKS(ms) ((ms) / (1000 / AO_HERTZ))
128 #define AO_SEC_TO_TICKS(s) ((s) * AO_HERTZ)
130 /* Returns the current time in ticks */
134 /* Suspend the current task until ticks time has passed */
136 ao_delay(uint16_t ticks);
138 /* Set the ADC interval */
140 ao_timer_set_adc_interval(uint8_t interval) __critical;
142 /* Timer interrupt */
144 ao_timer_isr(void) ao_arch_interrupt(9);
146 /* Initialize the timer */
150 /* Initialize the hardware clock. Must be called first */
155 * One set of samples read from the A/D converter or telemetry
164 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
165 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
169 * A/D data is stored in a ring, with the next sample to be written
172 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
173 extern volatile __data uint8_t ao_adc_head;
175 extern volatile __xdata uint16_t ao_accel_ref[AO_ADC_RING];
178 /* Trigger a conversion sequence (called from the timer interrupt) */
182 /* Suspend the current task until another A/D sample is converted */
186 /* Get a copy of the last complete A/D sample set */
188 ao_adc_get(__xdata struct ao_adc *packet);
190 /* The A/D interrupt handler */
193 ao_adc_isr(void) ao_arch_interrupt(1);
195 /* Initialize the A/D converter */
206 * Various pre-defined beep frequencies
208 * frequency = 1/2 (24e6/32) / beep
211 #define AO_BEEP_LOW 150 /* 2500Hz */
212 #define AO_BEEP_MID 94 /* 3989Hz */
213 #define AO_BEEP_HIGH 75 /* 5000Hz */
214 #define AO_BEEP_OFF 0 /* off */
216 #define AO_BEEP_g 240 /* 1562.5Hz */
217 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
218 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
219 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
220 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
221 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
222 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
223 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
224 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
225 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
226 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
227 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
228 #define AO_BEEP_gg 120 /* 3125Hz */
229 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
230 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
231 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
232 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
233 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
234 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
235 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
236 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
237 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
238 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
239 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
240 #define AO_BEEP_ggg 60 /* 6250Hz */
242 /* Set the beeper to the specified tone */
244 ao_beep(uint8_t beep);
246 /* Turn on the beeper for the specified time */
248 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
250 /* Initialize the beeper */
258 #define AO_LED_NONE 0
260 /* Turn on the specified LEDs */
262 ao_led_on(uint8_t colors);
264 /* Turn off the specified LEDs */
266 ao_led_off(uint8_t colors);
268 /* Set all of the LEDs to the specified state */
270 ao_led_set(uint8_t colors);
272 /* Toggle the specified LEDs */
274 ao_led_toggle(uint8_t colors);
276 /* Turn on the specified LEDs for the indicated interval */
278 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
280 /* Initialize the LEDs */
282 ao_led_init(uint8_t enable);
288 /* Put one character to the USB output queue */
290 ao_usb_putchar(char c);
292 /* Get one character from the USB input queue */
294 ao_usb_getchar(void);
296 /* Poll for a charcter on the USB input queue.
297 * returns AO_READ_AGAIN if none are available
300 ao_usb_pollchar(void);
302 /* Flush the USB output queue */
307 /* USB interrupt handler */
309 ao_usb_isr(void) ao_arch_interrupt(6);
312 /* Enable the USB controller */
316 /* Disable the USB controller */
318 ao_usb_disable(void);
320 /* Initialize the USB system */
325 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
334 ao_cmd_lex_error = 1,
335 ao_cmd_syntax_error = 2,
338 extern __pdata uint16_t ao_cmd_lex_i;
339 extern __pdata uint32_t ao_cmd_lex_u32;
340 extern __pdata char ao_cmd_lex_c;
341 extern __pdata enum ao_cmd_status ao_cmd_status;
347 ao_cmd_put8(uint8_t v);
350 ao_cmd_put16(uint16_t v);
356 ao_cmd_hexchar(char c);
359 ao_cmd_hexbyte(void);
365 ao_cmd_decimal(void);
368 ao_match_word(__code char *word);
376 ao_cmd_register(__code struct ao_cmds *cmds);
383 * Provided by an external module to filter raw command lines
393 /* Allocate a DMA channel. the 'done' parameter will be set when the
394 * dma is finished and will be used to wakeup any waiters
398 ao_dma_alloc(__xdata uint8_t * done);
400 /* Setup a DMA channel */
402 ao_dma_set_transfer(uint8_t id,
403 void __xdata *srcaddr,
404 void __xdata *dstaddr,
409 /* Start a DMA channel */
411 ao_dma_start(uint8_t id);
413 /* Manually trigger a DMA channel */
415 ao_dma_trigger(uint8_t id);
417 /* Abort a running DMA transfer */
419 ao_dma_abort(uint8_t id);
421 /* DMA interrupt routine */
423 ao_dma_isr(void) ao_arch_interrupt(8);
430 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
433 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
436 * Storage interface, provided by one of the eeprom or flash
440 /* Total bytes of available storage */
441 extern __pdata uint32_t ao_storage_total;
443 /* Block size - device is erased in these units. At least 256 bytes */
444 extern __pdata uint32_t ao_storage_block;
446 /* Byte offset of config block. Will be ao_storage_block bytes long */
447 extern __pdata uint32_t ao_storage_config;
449 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
450 extern __pdata uint16_t ao_storage_unit;
452 #define AO_STORAGE_ERASE_LOG (ao_storage_config + AO_CONFIG_MAX_SIZE)
454 /* Initialize above values. Can only be called once the OS is running */
456 ao_storage_setup(void) __reentrant;
458 /* Write data. Returns 0 on failure, 1 on success */
460 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
462 /* Read data. Returns 0 on failure, 1 on success */
464 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
466 /* Erase a block of storage. This always clears ao_storage_block bytes */
468 ao_storage_erase(uint32_t pos) __reentrant;
470 /* Flush any pending writes to stable storage */
472 ao_storage_flush(void) __reentrant;
474 /* Initialize the storage code */
476 ao_storage_init(void);
479 * Low-level functions wrapped by ao_storage.c
482 /* Read data within a storage unit */
484 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
486 /* Write data within a storage unit */
488 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
490 /* Initialize low-level device bits */
492 ao_storage_device_init(void);
494 /* Print out information about flash chips */
496 ao_storage_device_info(void) __reentrant;
502 /* We record flight numbers in the first record of
503 * the log. Tasks may wait for this to be initialized
504 * by sleeping on this variable.
506 extern __xdata uint16_t ao_flight_number;
508 extern __pdata uint32_t ao_log_current_pos;
509 extern __pdata uint32_t ao_log_end_pos;
510 extern __pdata uint32_t ao_log_start_pos;
511 extern __xdata uint8_t ao_log_running;
512 extern __pdata enum flight_state ao_log_state;
514 /* required functions from the underlying log system */
516 #define AO_LOG_FORMAT_UNKNOWN 0 /* unknown; altosui will have to guess */
517 #define AO_LOG_FORMAT_FULL 1 /* 8 byte typed log records */
518 #define AO_LOG_FORMAT_TINY 2 /* two byte state/baro records */
519 #define AO_LOG_FORMAT_TELEMETRY 3 /* 32 byte ao_telemetry records */
520 #define AO_LOG_FORMAT_TELESCIENCE 4 /* 32 byte typed telescience records */
521 #define AO_LOG_FORMAT_NONE 127 /* No log at all */
523 extern __code uint8_t ao_log_format;
525 /* Return the flight number from the given log slot, 0 if none */
527 ao_log_flight(uint8_t slot);
533 /* Logging thread main routine */
537 /* functions provided in ao_log.c */
539 /* Figure out the current flight number */
541 ao_log_scan(void) __reentrant;
543 /* Return the position of the start of the given log slot */
545 ao_log_pos(uint8_t slot);
547 /* Start logging to eeprom */
555 /* Initialize the logging system */
559 /* Write out the current flight number to the erase log */
561 ao_log_write_erase(uint8_t pos);
563 /* Returns true if there are any logs stored in eeprom */
565 ao_log_present(void);
567 /* Returns true if there is no more storage space available */
576 * The data log is recorded in the eeprom as a sequence
579 * Each packet starts with a 4-byte header that has the
580 * packet type, the packet checksum and the tick count. Then
581 * they all contain 2 16 bit values which hold packet-specific
584 * For each flight, the first packet
585 * is FLIGHT packet, indicating the serial number of the
586 * device and a unique number marking the number of flights
587 * recorded by this device.
589 * During flight, data from the accelerometer and barometer
590 * are recorded in SENSOR packets, using the raw 16-bit values
591 * read from the A/D converter.
593 * Also during flight, but at a lower rate, the deployment
594 * sensors are recorded in DEPLOY packets. The goal here is to
595 * detect failure in the deployment circuits.
597 * STATE packets hold state transitions as the flight computer
598 * transitions through different stages of the flight.
600 #define AO_LOG_FLIGHT 'F'
601 #define AO_LOG_SENSOR 'A'
602 #define AO_LOG_TEMP_VOLT 'T'
603 #define AO_LOG_DEPLOY 'D'
604 #define AO_LOG_STATE 'S'
605 #define AO_LOG_GPS_TIME 'G'
606 #define AO_LOG_GPS_LAT 'N'
607 #define AO_LOG_GPS_LON 'W'
608 #define AO_LOG_GPS_ALT 'H'
609 #define AO_LOG_GPS_SAT 'V'
610 #define AO_LOG_GPS_DATE 'Y'
612 #define AO_LOG_POS_NONE (~0UL)
614 struct ao_log_record {
620 int16_t ground_accel;
645 int32_t gps_latitude;
646 int32_t gps_longitude;
669 /* Write a record to the eeprom log */
671 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
677 enum ao_flight_state {
678 ao_flight_startup = 0,
684 ao_flight_drogue = 6,
686 ao_flight_landed = 8,
687 ao_flight_invalid = 9
690 extern __pdata enum ao_flight_state ao_flight_state;
692 extern __pdata uint16_t ao_launch_time;
693 extern __pdata uint8_t ao_flight_force_idle;
699 /* Initialize flight thread */
701 ao_flight_init(void);
708 ao_flight_nano_init(void);
715 * Barometer calibration
717 * We directly sample the barometer. The specs say:
719 * Pressure range: 15-115 kPa
720 * Voltage at 115kPa: 2.82
721 * Output scale: 27mV/kPa
723 * If we want to detect launch with the barometer, we need
724 * a large enough bump to not be fooled by noise. At typical
725 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
726 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
727 * As all of our calculations are done in 16 bits, we'll actually see a change
728 * of 16 times this though
730 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
733 /* Accelerometer calibration
735 * We're sampling the accelerometer through a resistor divider which
736 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
737 * That goes into the cc1111 A/D converter, which is running at 11 bits
738 * of precision with the bits in the MSB of the 16 bit value. Only positive
739 * values are used, so values should range from 0-32752 for 0-3.3V. The
740 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
741 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
742 * for a final computation of:
744 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
746 * Zero g was measured at 16000 (we would expect 16384).
747 * Note that this value is only require to tell if the
748 * rocket is standing upright. Once that is determined,
749 * the value of the accelerometer is averaged for 100 samples
750 * to find the resting accelerometer value, which is used
751 * for all further flight computations
754 #define GRAVITY 9.80665
757 * Above this height, the baro sensor doesn't work
759 #define AO_MAX_BARO_HEIGHT 12000
762 * Above this speed, baro measurements are unreliable
764 #define AO_MAX_BARO_SPEED 200
766 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
769 * Speed and acceleration are scaled by 16 to provide a bit more
770 * resolution while still having reasonable range. Note that this
771 * limits speed to 2047m/s (around mach 6) and acceleration to
772 * 2047m/s² (over 200g)
775 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
776 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
777 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
779 extern __pdata uint16_t ao_sample_tick; /* time of last data */
780 extern __pdata int16_t ao_sample_pres; /* most recent pressure sensor reading */
781 extern __pdata int16_t ao_sample_alt; /* MSL of ao_sample_pres */
782 extern __pdata int16_t ao_sample_height; /* AGL of ao_sample_pres */
783 extern __data uint8_t ao_sample_adc; /* Ring position of last processed sample */
786 extern __pdata int16_t ao_sample_accel; /* most recent accel sensor reading */
789 extern __pdata int16_t ao_ground_pres; /* startup pressure */
790 extern __pdata int16_t ao_ground_height; /* MSL of ao_ground_pres */
793 extern __pdata int16_t ao_ground_accel; /* startup acceleration */
794 extern __pdata int16_t ao_accel_2g; /* factory accel calibration */
795 extern __pdata int32_t ao_accel_scale; /* sensor to m/s² conversion */
798 void ao_sample_init(void);
800 /* returns FALSE in preflight mode, TRUE in flight mode */
801 uint8_t ao_sample(void);
807 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
808 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
809 #define from_fix(x) ((x) >> 16)
811 extern __pdata int16_t ao_height; /* meters */
812 extern __pdata int16_t ao_speed; /* m/s * 16 */
813 extern __pdata int16_t ao_accel; /* m/s² * 16 */
814 extern __pdata int16_t ao_max_height; /* max of ao_height */
815 extern __pdata int16_t ao_avg_height; /* running average of height */
817 extern __pdata int16_t ao_error_h;
818 extern __pdata int16_t ao_error_h_sq_avg;
821 extern __pdata int16_t ao_error_a;
824 void ao_kalman(void);
831 ao_report_init(void);
836 * Given raw data, convert to SI units
839 /* pressure from the sensor to altitude in meters */
841 ao_pres_to_altitude(int16_t pres) __reentrant;
844 ao_altitude_to_pres(int16_t alt) __reentrant;
847 ao_temp_to_dC(int16_t temp) __reentrant;
852 * debug another telemetrum board
855 /* Send a byte to the dbg target */
857 ao_dbg_send_byte(uint8_t byte);
859 /* Receive a byte from the dbg target */
861 ao_dbg_recv_byte(void);
863 /* Start a bulk transfer to/from dbg target memory */
865 ao_dbg_start_transfer(uint16_t addr);
867 /* End a bulk transfer to/from dbg target memory */
869 ao_dbg_end_transfer(void);
871 /* Write a byte to dbg target memory */
873 ao_dbg_write_byte(uint8_t byte);
875 /* Read a byte from dbg target memory */
877 ao_dbg_read_byte(void);
879 /* Enable dbg mode, switching use of the pins */
881 ao_dbg_debug_mode(void);
883 /* Reset the dbg target */
895 #error Please define HAS_SERIAL_1
899 #ifndef USE_SERIAL_STDIN
900 #error Please define USE_SERIAL_STDIN
904 ao_serial_rx1_isr(void) ao_arch_interrupt(3);
907 ao_serial_tx1_isr(void) ao_arch_interrupt(14);
910 ao_serial_getchar(void) __critical;
914 ao_serial_pollchar(void) __critical;
917 ao_serial_set_stdin(uint8_t stdin);
921 ao_serial_putchar(char c) __critical;
924 ao_serial_drain(void) __critical;
926 #define AO_SERIAL_SPEED_4800 0
927 #define AO_SERIAL_SPEED_9600 1
928 #define AO_SERIAL_SPEED_19200 2
929 #define AO_SERIAL_SPEED_57600 3
932 ao_serial_set_speed(uint8_t speed);
935 ao_serial_init(void);
942 extern __xdata uint8_t ao_spi_mutex;
944 #define ao_spi_get_mask(reg,mask) do {\
945 ao_mutex_get(&ao_spi_mutex); \
949 #define ao_spi_put_mask(reg,mask) do { \
951 ao_mutex_put(&ao_spi_mutex); \
954 #define ao_spi_get_bit(bit) do {\
955 ao_mutex_get(&ao_spi_mutex); \
959 #define ao_spi_put_bit(bit) do { \
961 ao_mutex_put(&ao_spi_mutex); \
965 * The SPI mutex must be held to call either of these
966 * functions -- this mutex covers the entire SPI operation,
967 * from chip select low to chip select high
971 ao_spi_send(void __xdata *block, uint16_t len) __reentrant;
974 ao_spi_recv(void __xdata *block, uint16_t len) __reentrant;
984 ao_spi_slave_recv(uint8_t *buf, uint8_t len);
987 ao_spi_slave_send(uint8_t *buf, uint8_t len);
990 ao_spi_slave_init(void);
992 /* This must be defined by the product; it will get called when chip
993 * select goes low, at which point it should use ao_spi_read and
994 * ao_spi_write to deal with the request
1003 #define AO_MAX_CALLSIGN 8
1004 #define AO_MAX_VERSION 8
1006 #define AO_MAX_TELEMETRY 128
1008 #define AO_MAX_TELEMETRY 32
1011 struct ao_telemetry_generic {
1012 uint16_t serial; /* 0 */
1013 uint16_t tick; /* 2 */
1014 uint8_t type; /* 4 */
1015 uint8_t payload[27]; /* 5 */
1019 #define AO_TELEMETRY_SENSOR_TELEMETRUM 0x01
1020 #define AO_TELEMETRY_SENSOR_TELEMINI 0x02
1021 #define AO_TELEMETRY_SENSOR_TELENANO 0x03
1023 struct ao_telemetry_sensor {
1024 uint16_t serial; /* 0 */
1025 uint16_t tick; /* 2 */
1026 uint8_t type; /* 4 */
1028 uint8_t state; /* 5 flight state */
1029 int16_t accel; /* 6 accelerometer (TM only) */
1030 int16_t pres; /* 8 pressure sensor */
1031 int16_t temp; /* 10 temperature sensor */
1032 int16_t v_batt; /* 12 battery voltage */
1033 int16_t sense_d; /* 14 drogue continuity sense (TM/Tm) */
1034 int16_t sense_m; /* 16 main continuity sense (TM/Tm) */
1036 int16_t acceleration; /* 18 m/s² * 16 */
1037 int16_t speed; /* 20 m/s * 16 */
1038 int16_t height; /* 22 m */
1040 int16_t ground_pres; /* 24 average pres on pad */
1041 int16_t ground_accel; /* 26 average accel on pad */
1042 int16_t accel_plus_g; /* 28 accel calibration at +1g */
1043 int16_t accel_minus_g; /* 30 accel calibration at -1g */
1047 #define AO_TELEMETRY_CONFIGURATION 0x04
1049 struct ao_telemetry_configuration {
1050 uint16_t serial; /* 0 */
1051 uint16_t tick; /* 2 */
1052 uint8_t type; /* 4 */
1054 uint8_t device; /* 5 device type */
1055 uint16_t flight; /* 6 flight number */
1056 uint8_t config_major; /* 8 Config major version */
1057 uint8_t config_minor; /* 9 Config minor version */
1058 uint16_t apogee_delay; /* 10 Apogee deploy delay in seconds */
1059 uint16_t main_deploy; /* 12 Main deploy alt in meters */
1060 uint16_t flight_log_max; /* 14 Maximum flight log size in kB */
1061 char callsign[AO_MAX_CALLSIGN]; /* 16 Radio operator identity */
1062 char version[AO_MAX_VERSION]; /* 24 Software version */
1066 #define AO_TELEMETRY_LOCATION 0x05
1068 #define AO_GPS_MODE_NOT_VALID 'N'
1069 #define AO_GPS_MODE_AUTONOMOUS 'A'
1070 #define AO_GPS_MODE_DIFFERENTIAL 'D'
1071 #define AO_GPS_MODE_ESTIMATED 'E'
1072 #define AO_GPS_MODE_MANUAL 'M'
1073 #define AO_GPS_MODE_SIMULATED 'S'
1075 struct ao_telemetry_location {
1076 uint16_t serial; /* 0 */
1077 uint16_t tick; /* 2 */
1078 uint8_t type; /* 4 */
1080 uint8_t flags; /* 5 Number of sats and other flags */
1081 int16_t altitude; /* 6 GPS reported altitude (m) */
1082 int32_t latitude; /* 8 latitude (degrees * 10⁷) */
1083 int32_t longitude; /* 12 longitude (degrees * 10⁷) */
1084 uint8_t year; /* 16 (- 2000) */
1085 uint8_t month; /* 17 (1-12) */
1086 uint8_t day; /* 18 (1-31) */
1087 uint8_t hour; /* 19 (0-23) */
1088 uint8_t minute; /* 20 (0-59) */
1089 uint8_t second; /* 21 (0-59) */
1090 uint8_t pdop; /* 22 (m * 5) */
1091 uint8_t hdop; /* 23 (m * 5) */
1092 uint8_t vdop; /* 24 (m * 5) */
1093 uint8_t mode; /* 25 */
1094 uint16_t ground_speed; /* 26 cm/s */
1095 int16_t climb_rate; /* 28 cm/s */
1096 uint8_t course; /* 30 degrees / 2 */
1097 uint8_t unused[1]; /* 31 */
1101 #define AO_TELEMETRY_SATELLITE 0x06
1103 struct ao_telemetry_satellite_info {
1108 struct ao_telemetry_satellite {
1109 uint16_t serial; /* 0 */
1110 uint16_t tick; /* 2 */
1111 uint8_t type; /* 4 */
1112 uint8_t channels; /* 5 number of reported sats */
1114 struct ao_telemetry_satellite_info sats[12]; /* 6 */
1115 uint8_t unused[2]; /* 30 */
1119 #define AO_TELEMETRY_COMPANION 0x07
1121 #define AO_COMPANION_MAX_CHANNELS 12
1123 struct ao_telemetry_companion {
1124 uint16_t serial; /* 0 */
1125 uint16_t tick; /* 2 */
1126 uint8_t type; /* 4 */
1127 uint8_t board_id; /* 5 */
1129 uint8_t update_period; /* 6 */
1130 uint8_t channels; /* 7 */
1131 uint16_t companion_data[AO_COMPANION_MAX_CHANNELS]; /* 8 */
1135 /* #define AO_SEND_ALL_BARO */
1137 #define AO_TELEMETRY_BARO 0x80
1140 * This packet allows the full sampling rate baro
1141 * data to be captured over the RF link so that the
1142 * flight software can be tested using 'real' data.
1144 * Along with this telemetry packet, the flight
1145 * code is modified to send full-rate telemetry all the time
1146 * and never send an RDF tone; this ensure that the full radio
1147 * link is available.
1149 struct ao_telemetry_baro {
1150 uint16_t serial; /* 0 */
1151 uint16_t tick; /* 2 */
1152 uint8_t type; /* 4 */
1153 uint8_t samples; /* 5 number samples */
1155 int16_t baro[12]; /* 6 samples */
1159 union ao_telemetry_all {
1160 struct ao_telemetry_generic generic;
1161 struct ao_telemetry_sensor sensor;
1162 struct ao_telemetry_configuration configuration;
1163 struct ao_telemetry_location location;
1164 struct ao_telemetry_satellite satellite;
1165 struct ao_telemetry_companion companion;
1166 struct ao_telemetry_baro baro;
1169 struct ao_telemetry_all_recv {
1170 union ao_telemetry_all telemetry;
1179 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
1180 #define AO_GPS_NUM_SAT_SHIFT (0)
1182 #define AO_GPS_VALID (1 << 4)
1183 #define AO_GPS_RUNNING (1 << 5)
1184 #define AO_GPS_DATE_VALID (1 << 6)
1185 #define AO_GPS_COURSE_VALID (1 << 7)
1187 extern __pdata uint16_t ao_gps_tick;
1188 extern __xdata uint8_t ao_gps_mutex;
1189 extern __xdata struct ao_telemetry_location ao_gps_data;
1190 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
1192 struct ao_gps_orig {
1200 int32_t latitude; /* degrees * 10⁷ */
1201 int32_t longitude; /* degrees * 10⁷ */
1202 int16_t altitude; /* m */
1203 uint16_t ground_speed; /* cm/s */
1204 uint8_t course; /* degrees / 2 */
1205 uint8_t hdop; /* * 5 */
1206 int16_t climb_rate; /* cm/s */
1207 uint16_t h_error; /* m */
1208 uint16_t v_error; /* m */
1211 struct ao_gps_sat_orig {
1216 #define AO_MAX_GPS_TRACKING 12
1218 struct ao_gps_tracking_orig {
1220 struct ao_gps_sat_orig sats[AO_MAX_GPS_TRACKING];
1227 ao_gps_print(__xdata struct ao_gps_orig *gps_data);
1230 ao_gps_tracking_print(__xdata struct ao_gps_tracking_orig *gps_tracking_data);
1240 ao_gps_report(void);
1243 ao_gps_report_init(void);
1246 * ao_telemetry_orig.c
1249 struct ao_telemetry_orig {
1252 uint8_t flight_state;
1254 int16_t ground_accel;
1263 int16_t ground_pres;
1264 int16_t accel_plus_g;
1265 int16_t accel_minus_g;
1267 struct ao_gps_orig gps;
1268 char callsign[AO_MAX_CALLSIGN];
1269 struct ao_gps_tracking_orig gps_tracking;
1272 struct ao_telemetry_tiny {
1275 uint8_t flight_state;
1276 int16_t height; /* AGL in meters */
1277 int16_t speed; /* in m/s * 16 */
1278 int16_t accel; /* in m/s² * 16 */
1279 int16_t ground_pres; /* sensor units */
1280 struct ao_adc adc; /* raw ADC readings */
1281 char callsign[AO_MAX_CALLSIGN];
1284 struct ao_telemetry_orig_recv {
1285 struct ao_telemetry_orig telemetry_orig;
1290 struct ao_telemetry_tiny_recv {
1291 struct ao_telemetry_tiny telemetry_tiny;
1297 * ao_radio_recv tacks on rssi and status bytes
1300 struct ao_telemetry_raw_recv {
1301 uint8_t packet[AO_MAX_TELEMETRY + 2];
1304 /* Set delay between telemetry reports (0 to disable) */
1306 #ifdef AO_SEND_ALL_BARO
1307 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(100)
1308 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(100)
1309 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(100)
1311 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(1000)
1312 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(100)
1313 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(1000)
1317 ao_telemetry_set_interval(uint16_t interval);
1320 ao_rdf_set(uint8_t rdf);
1323 ao_telemetry_init(void);
1326 ao_telemetry_orig_init(void);
1329 ao_telemetry_tiny_init(void);
1335 extern __xdata uint8_t ao_radio_dma;
1336 extern __xdata uint8_t ao_radio_dma_done;
1337 extern __xdata uint8_t ao_radio_done;
1338 extern __xdata uint8_t ao_radio_mutex;
1341 ao_radio_general_isr(void) ao_arch_interrupt(16);
1344 ao_radio_get(uint8_t len);
1346 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
1349 ao_radio_set_packet(void);
1352 ao_radio_send(__xdata void *data, uint8_t size) __reentrant;
1355 ao_radio_recv(__xdata void *data, uint8_t size) __reentrant;
1358 ao_radio_recv_abort(void);
1361 * Compute the packet length as follows:
1363 * 2000 bps (for a 1kHz tone)
1364 * so, for 'ms' milliseconds, we need
1365 * 2 * ms bits, or ms / 4 bytes
1368 #define AO_MS_TO_RDF_LEN(ms) ((ms) > 255 * 4 ? 255 : ((ms) >> 2))
1371 ao_radio_rdf(uint8_t pkt_len);
1374 ao_radio_rdf_abort(void);
1377 ao_radio_idle(void);
1380 ao_radio_init(void);
1386 extern const char const * const ao_state_names[];
1388 #define AO_MONITOR_RING 8
1391 struct ao_telemetry_raw_recv raw;
1392 struct ao_telemetry_all_recv all;
1393 struct ao_telemetry_orig_recv orig;
1394 struct ao_telemetry_tiny_recv tiny;
1397 extern __xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
1399 #define ao_monitor_ring_next(n) (((n) + 1) & (AO_MONITOR_RING - 1))
1401 extern __data uint8_t ao_monitoring;
1402 extern __data uint8_t ao_monitor_head;
1407 #define AO_MONITORING_OFF 0
1408 #define AO_MONITORING_ORIG 1
1411 ao_monitor_set(uint8_t monitoring);
1414 ao_monitor_disable(void);
1417 ao_monitor_enable(void);
1420 ao_monitor_init(void) __reentrant;
1426 #define AO_READ_AGAIN ((char) -1)
1429 char (*pollchar)(void);
1430 void (*putchar)(char c) __reentrant;
1431 void (*flush)(void);
1435 extern __xdata struct ao_stdio ao_stdios[];
1436 extern __pdata int8_t ao_cur_stdio;
1437 extern __pdata int8_t ao_num_stdios;
1442 extern __xdata uint8_t ao_stdin_ready;
1448 ao_add_stdio(char (*pollchar)(void),
1449 void (*putchar)(char) __reentrant,
1450 void (*flush)(void)) __reentrant;
1457 ao_igniter_drogue = 0,
1462 ao_ignite(enum ao_igniter igniter);
1464 enum ao_igniter_status {
1465 ao_igniter_unknown, /* unknown status (ambiguous voltage) */
1466 ao_igniter_ready, /* continuity detected */
1467 ao_igniter_active, /* igniter firing */
1468 ao_igniter_open, /* open circuit detected */
1471 struct ao_ignition {
1477 extern __xdata struct ao_ignition ao_ignition[2];
1479 enum ao_igniter_status
1480 ao_igniter_status(enum ao_igniter igniter);
1482 extern __pdata uint8_t ao_igniter_present;
1485 ao_ignite_set_pins(void);
1488 ao_igniter_init(void);
1494 #define AO_CONFIG_MAJOR 1
1495 #define AO_CONFIG_MINOR 9
1496 #define AO_AES_LEN 16
1501 uint16_t main_deploy;
1502 int16_t accel_plus_g; /* changed for minor version 2 */
1503 uint8_t radio_channel;
1504 char callsign[AO_MAX_CALLSIGN + 1];
1505 uint8_t apogee_delay; /* minor version 1 */
1506 int16_t accel_minus_g; /* minor version 2 */
1507 uint32_t radio_cal; /* minor version 3 */
1508 uint32_t flight_log_max; /* minor version 4 */
1509 uint8_t ignite_mode; /* minor version 5 */
1510 uint8_t pad_orientation; /* minor version 6 */
1511 uint32_t radio_setting; /* minor version 7 */
1512 uint8_t radio_enable; /* minor version 8 */
1513 uint8_t aes_key[AO_AES_LEN]; /* minor version 9 */
1516 #define AO_IGNITE_MODE_DUAL 0
1517 #define AO_IGNITE_MODE_APOGEE 1
1518 #define AO_IGNITE_MODE_MAIN 2
1520 #define AO_PAD_ORIENTATION_ANTENNA_UP 0
1521 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
1523 extern __xdata struct ao_config ao_config;
1525 #define AO_CONFIG_MAX_SIZE 128
1528 ao_config_get(void);
1531 ao_config_put(void);
1534 ao_config_init(void);
1541 ao_rssi_set(int rssi_value);
1544 ao_rssi_init(uint8_t rssi_led);
1549 * values which need to be defined for
1550 * each instance of a product
1553 extern const char ao_version[];
1554 extern const char ao_manufacturer[];
1555 extern const char ao_product[];
1561 #define AO_FIFO_SIZE 32
1566 char fifo[AO_FIFO_SIZE];
1569 #define ao_fifo_insert(f,c) do { \
1570 (f).fifo[(f).insert] = (c); \
1571 (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1574 #define ao_fifo_remove(f,c) do {\
1575 c = (f).fifo[(f).remove]; \
1576 (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1579 #define ao_fifo_full(f) ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1580 #define ao_fifo_empty(f) ((f).insert == (f).remove)
1585 * Packet-based command interface
1588 #define AO_PACKET_MAX 64
1589 #define AO_PACKET_SYN (uint8_t) 0xff
1596 uint8_t d[AO_PACKET_MAX];
1597 uint8_t callsign[AO_MAX_CALLSIGN];
1600 struct ao_packet_recv {
1601 struct ao_packet packet;
1606 extern __xdata struct ao_packet_recv ao_rx_packet;
1607 extern __xdata struct ao_packet ao_tx_packet;
1608 extern __xdata struct ao_task ao_packet_task;
1609 extern __xdata uint8_t ao_packet_enable;
1610 extern __xdata uint8_t ao_packet_master_sleeping;
1611 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1614 ao_packet_send(void);
1617 ao_packet_recv(void);
1620 ao_packet_flush(void);
1623 ao_packet_putchar(char c) __reentrant;
1626 ao_packet_pollchar(void) __critical;
1628 /* ao_packet_master.c */
1631 ao_packet_master_init(void);
1633 /* ao_packet_slave.c */
1636 ao_packet_slave_start(void);
1639 ao_packet_slave_stop(void);
1642 ao_packet_slave_init(uint8_t enable);
1646 /* If bt_link is on P2, this interrupt is shared by USB, so the USB
1647 * code calls this function. Otherwise, it's a regular ISR.
1660 /* ao_companion.c */
1662 #define AO_COMPANION_SETUP 1
1663 #define AO_COMPANION_FETCH 2
1664 #define AO_COMPANION_NOTIFY 3
1666 struct ao_companion_command {
1668 uint8_t flight_state;
1674 struct ao_companion_setup {
1676 uint16_t board_id_inverse;
1677 uint8_t update_period;
1681 extern __pdata uint8_t ao_companion_running;
1682 extern __xdata uint8_t ao_companion_mutex;
1683 extern __xdata struct ao_companion_command ao_companion_command;
1684 extern __xdata struct ao_companion_setup ao_companion_setup;
1685 extern __xdata uint16_t ao_companion_data[AO_COMPANION_MAX_CHANNELS];
1688 ao_companion_init(void);
1693 ao_lcd_putchar(uint8_t data);
1696 ao_lcd_putstring(char *string);
1699 ao_lcd_contrast_set(uint8_t contrast);
1704 #define AO_LCD_ADDR(row,col) ((row << 6) | (col))
1707 ao_lcd_goto(uint8_t addr);
1718 ao_lcd_port_put_nibble(uint8_t rs, uint8_t data);
1721 ao_lcd_port_init(void);
1725 __xdata uint8_t ao_aes_mutex;
1727 /* AES keys and blocks are 128 bits */
1735 ao_aes_isr(void) __interrupt 4;
1739 ao_aes_set_mode(enum ao_aes_mode mode);
1742 ao_aes_set_key(__xdata uint8_t *in);
1745 ao_aes_zero_iv(void);
1748 ao_aes_run(__xdata uint8_t *in,
1749 __xdata uint8_t *out);
1754 /* ao_radio_cmac.c */
1757 ao_radio_cmac_send(__xdata void *packet, uint8_t len) __reentrant;
1759 #define AO_RADIO_CMAC_OK 0
1760 #define AO_RADIO_CMAC_LEN_ERROR -1
1761 #define AO_RADIO_CMAC_CRC_ERROR -2
1762 #define AO_RADIO_CMAC_MAC_ERROR -3
1763 #define AO_RADIO_CMAC_TIMEOUT -4
1766 ao_radio_cmac_recv(__xdata void *packet, uint8_t len, uint16_t timeout) __reentrant;
1769 ao_radio_cmac_init(void);
1773 struct ao_launch_command {
1781 #define AO_LAUNCH_QUERY 1
1783 struct ao_launch_query {
1789 uint8_t igniter_status;
1792 #define AO_LAUNCH_ARM 2
1793 #define AO_LAUNCH_FIRE 3
1796 ao_launch_init(void);
1802 #define AO_LOG_TELESCIENCE_START ((uint8_t) 's')
1803 #define AO_LOG_TELESCIENCE_DATA ((uint8_t) 'd')
1805 #define AO_LOG_TELESCIENCE_NUM_ADC 12
1807 struct ao_log_telescience {
1814 uint16_t adc[AO_LOG_TELESCIENCE_NUM_ADC];
1817 #define AO_LOG_SINGLE_SIZE 32
1819 union ao_log_single {
1820 struct ao_log_telescience telescience;
1821 union ao_telemetry_all telemetry;
1822 uint8_t bytes[AO_LOG_SINGLE_SIZE];
1825 extern __xdata union ao_log_single ao_log_single_write_data;
1826 extern __xdata union ao_log_single ao_log_single_read_data;
1829 ao_log_single_extra_query(void);
1832 ao_log_single_list(void);
1835 ao_log_single_main(void);
1838 ao_log_single_write(void);
1841 ao_log_single_read(uint32_t pos);
1844 ao_log_single_start(void);
1847 ao_log_single_stop(void);
1850 ao_log_single_restart(void);
1853 ao_log_single_set(void);
1856 ao_log_single_delete(void);
1859 ao_log_single_init(void);
1862 ao_log_single(void);
1868 #define AO_TELEPYRO_NUM_ADC 9
1871 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
1872 #define ao_xmemset(d,v,c) memset(d,v,c)
1873 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
1881 ao_terraui_init(void);
1889 ao_battery_isr(void) ao_arch_interrupt(1);
1892 ao_battery_get(void);
1895 ao_battery_init(void);
1896 #endif /* BATTERY_PIN */
1903 ao_sqrt(uint32_t op);