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 */
116 #define AO_PANIC_STACK 12 /* Stack overflow */
117 #define AO_PANIC_SPI 13 /* SPI communication failure */
119 /* Stop the operating system, beeping and blinking the reason */
121 ao_panic(uint8_t reason);
127 /* Our timer runs at 100Hz */
129 #define AO_MS_TO_TICKS(ms) ((ms) / (1000 / AO_HERTZ))
130 #define AO_SEC_TO_TICKS(s) ((s) * AO_HERTZ)
132 /* Returns the current time in ticks */
136 /* Suspend the current task until ticks time has passed */
138 ao_delay(uint16_t ticks);
140 /* Set the ADC interval */
142 ao_timer_set_adc_interval(uint8_t interval) __critical;
144 /* Timer interrupt */
146 ao_timer_isr(void) ao_arch_interrupt(9);
148 /* Initialize the timer */
152 /* Initialize the hardware clock. Must be called first */
157 * One set of samples read from the A/D converter or telemetry
166 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
167 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
171 * A/D data is stored in a ring, with the next sample to be written
174 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
175 extern volatile __data uint8_t ao_adc_head;
177 extern volatile __xdata uint16_t ao_accel_ref[AO_ADC_RING];
180 /* Trigger a conversion sequence (called from the timer interrupt) */
184 /* Suspend the current task until another A/D sample is converted */
188 /* Get a copy of the last complete A/D sample set */
190 ao_adc_get(__xdata struct ao_adc *packet);
192 /* The A/D interrupt handler */
195 ao_adc_isr(void) ao_arch_interrupt(1);
197 /* Initialize the A/D converter */
208 * Various pre-defined beep frequencies
210 * frequency = 1/2 (24e6/32) / beep
213 #define AO_BEEP_LOW 150 /* 2500Hz */
214 #define AO_BEEP_MID 94 /* 3989Hz */
215 #define AO_BEEP_HIGH 75 /* 5000Hz */
216 #define AO_BEEP_OFF 0 /* off */
218 #define AO_BEEP_g 240 /* 1562.5Hz */
219 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
220 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
221 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
222 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
223 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
224 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
225 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
226 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
227 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
228 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
229 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
230 #define AO_BEEP_gg 120 /* 3125Hz */
231 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
232 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
233 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
234 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
235 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
236 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
237 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
238 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
239 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
240 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
241 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
242 #define AO_BEEP_ggg 60 /* 6250Hz */
244 /* Set the beeper to the specified tone */
246 ao_beep(uint8_t beep);
248 /* Turn on the beeper for the specified time */
250 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
252 /* Initialize the beeper */
260 #define AO_LED_NONE 0
263 #define AO_LED_TYPE uint8_t
266 /* Turn on the specified LEDs */
268 ao_led_on(AO_LED_TYPE colors);
270 /* Turn off the specified LEDs */
272 ao_led_off(AO_LED_TYPE colors);
274 /* Set all of the LEDs to the specified state */
276 ao_led_set(AO_LED_TYPE colors);
278 /* Toggle the specified LEDs */
280 ao_led_toggle(AO_LED_TYPE colors);
282 /* Turn on the specified LEDs for the indicated interval */
284 ao_led_for(AO_LED_TYPE colors, uint16_t ticks) __reentrant;
286 /* Initialize the LEDs */
288 ao_led_init(AO_LED_TYPE enable);
294 /* Put one character to the USB output queue */
296 ao_usb_putchar(char c);
298 /* Get one character from the USB input queue */
300 ao_usb_getchar(void);
302 /* Poll for a charcter on the USB input queue.
303 * returns AO_READ_AGAIN if none are available
306 ao_usb_pollchar(void);
308 /* Flush the USB output queue */
313 /* USB interrupt handler */
315 ao_usb_isr(void) ao_arch_interrupt(6);
318 /* Enable the USB controller */
322 /* Disable the USB controller */
324 ao_usb_disable(void);
326 /* Initialize the USB system */
331 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
340 ao_cmd_lex_error = 1,
341 ao_cmd_syntax_error = 2,
344 extern __pdata uint16_t ao_cmd_lex_i;
345 extern __pdata uint32_t ao_cmd_lex_u32;
346 extern __pdata char ao_cmd_lex_c;
347 extern __pdata enum ao_cmd_status ao_cmd_status;
353 ao_cmd_put8(uint8_t v);
356 ao_cmd_put16(uint16_t v);
359 ao_cmd_is_white(void);
365 ao_cmd_hexchar(char c);
368 ao_cmd_hexbyte(void);
374 ao_cmd_decimal(void);
377 ao_match_word(__code char *word);
385 ao_cmd_register(const __code struct ao_cmds *cmds);
392 * Provided by an external module to filter raw command lines
403 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
406 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
409 * Storage interface, provided by one of the eeprom or flash
413 /* Total bytes of available storage */
414 extern __pdata uint32_t ao_storage_total;
416 /* Block size - device is erased in these units. At least 256 bytes */
417 extern __pdata uint32_t ao_storage_block;
419 /* Byte offset of config block. Will be ao_storage_block bytes long */
420 extern __pdata uint32_t ao_storage_config;
422 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
423 extern __pdata uint16_t ao_storage_unit;
425 #define AO_STORAGE_ERASE_LOG (ao_storage_config + AO_CONFIG_MAX_SIZE)
427 /* Initialize above values. Can only be called once the OS is running */
429 ao_storage_setup(void) __reentrant;
431 /* Write data. Returns 0 on failure, 1 on success */
433 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
435 /* Read data. Returns 0 on failure, 1 on success */
437 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
439 /* Erase a block of storage. This always clears ao_storage_block bytes */
441 ao_storage_erase(uint32_t pos) __reentrant;
443 /* Flush any pending writes to stable storage */
445 ao_storage_flush(void) __reentrant;
447 /* Initialize the storage code */
449 ao_storage_init(void);
452 * Low-level functions wrapped by ao_storage.c
455 /* Read data within a storage unit */
457 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
459 /* Write data within a storage unit */
461 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
463 /* Initialize low-level device bits */
465 ao_storage_device_init(void);
467 /* Print out information about flash chips */
469 ao_storage_device_info(void) __reentrant;
475 /* We record flight numbers in the first record of
476 * the log. Tasks may wait for this to be initialized
477 * by sleeping on this variable.
479 extern __xdata uint16_t ao_flight_number;
481 extern __pdata uint32_t ao_log_current_pos;
482 extern __pdata uint32_t ao_log_end_pos;
483 extern __pdata uint32_t ao_log_start_pos;
484 extern __xdata uint8_t ao_log_running;
485 extern __pdata enum flight_state ao_log_state;
487 /* required functions from the underlying log system */
489 #define AO_LOG_FORMAT_UNKNOWN 0 /* unknown; altosui will have to guess */
490 #define AO_LOG_FORMAT_FULL 1 /* 8 byte typed log records */
491 #define AO_LOG_FORMAT_TINY 2 /* two byte state/baro records */
492 #define AO_LOG_FORMAT_TELEMETRY 3 /* 32 byte ao_telemetry records */
493 #define AO_LOG_FORMAT_TELESCIENCE 4 /* 32 byte typed telescience records */
494 #define AO_LOG_FORMAT_NONE 127 /* No log at all */
496 extern __code uint8_t ao_log_format;
498 /* Return the flight number from the given log slot, 0 if none */
500 ao_log_flight(uint8_t slot);
506 /* Logging thread main routine */
510 /* functions provided in ao_log.c */
512 /* Figure out the current flight number */
514 ao_log_scan(void) __reentrant;
516 /* Return the position of the start of the given log slot */
518 ao_log_pos(uint8_t slot);
520 /* Start logging to eeprom */
528 /* Initialize the logging system */
532 /* Write out the current flight number to the erase log */
534 ao_log_write_erase(uint8_t pos);
536 /* Returns true if there are any logs stored in eeprom */
538 ao_log_present(void);
540 /* Returns true if there is no more storage space available */
549 * The data log is recorded in the eeprom as a sequence
552 * Each packet starts with a 4-byte header that has the
553 * packet type, the packet checksum and the tick count. Then
554 * they all contain 2 16 bit values which hold packet-specific
557 * For each flight, the first packet
558 * is FLIGHT packet, indicating the serial number of the
559 * device and a unique number marking the number of flights
560 * recorded by this device.
562 * During flight, data from the accelerometer and barometer
563 * are recorded in SENSOR packets, using the raw 16-bit values
564 * read from the A/D converter.
566 * Also during flight, but at a lower rate, the deployment
567 * sensors are recorded in DEPLOY packets. The goal here is to
568 * detect failure in the deployment circuits.
570 * STATE packets hold state transitions as the flight computer
571 * transitions through different stages of the flight.
573 #define AO_LOG_FLIGHT 'F'
574 #define AO_LOG_SENSOR 'A'
575 #define AO_LOG_TEMP_VOLT 'T'
576 #define AO_LOG_DEPLOY 'D'
577 #define AO_LOG_STATE 'S'
578 #define AO_LOG_GPS_TIME 'G'
579 #define AO_LOG_GPS_LAT 'N'
580 #define AO_LOG_GPS_LON 'W'
581 #define AO_LOG_GPS_ALT 'H'
582 #define AO_LOG_GPS_SAT 'V'
583 #define AO_LOG_GPS_DATE 'Y'
585 #define AO_LOG_POS_NONE (~0UL)
587 struct ao_log_record {
593 int16_t ground_accel;
618 int32_t gps_latitude;
619 int32_t gps_longitude;
642 /* Write a record to the eeprom log */
644 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
650 enum ao_flight_state {
651 ao_flight_startup = 0,
657 ao_flight_drogue = 6,
659 ao_flight_landed = 8,
660 ao_flight_invalid = 9
663 extern __pdata enum ao_flight_state ao_flight_state;
665 extern __pdata uint16_t ao_launch_time;
666 extern __pdata uint8_t ao_flight_force_idle;
672 /* Initialize flight thread */
674 ao_flight_init(void);
681 ao_flight_nano_init(void);
688 * Barometer calibration
690 * We directly sample the barometer. The specs say:
692 * Pressure range: 15-115 kPa
693 * Voltage at 115kPa: 2.82
694 * Output scale: 27mV/kPa
696 * If we want to detect launch with the barometer, we need
697 * a large enough bump to not be fooled by noise. At typical
698 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
699 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
700 * As all of our calculations are done in 16 bits, we'll actually see a change
701 * of 16 times this though
703 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
706 /* Accelerometer calibration
708 * We're sampling the accelerometer through a resistor divider which
709 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
710 * That goes into the cc1111 A/D converter, which is running at 11 bits
711 * of precision with the bits in the MSB of the 16 bit value. Only positive
712 * values are used, so values should range from 0-32752 for 0-3.3V. The
713 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
714 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
715 * for a final computation of:
717 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
719 * Zero g was measured at 16000 (we would expect 16384).
720 * Note that this value is only require to tell if the
721 * rocket is standing upright. Once that is determined,
722 * the value of the accelerometer is averaged for 100 samples
723 * to find the resting accelerometer value, which is used
724 * for all further flight computations
727 #define GRAVITY 9.80665
730 * Above this height, the baro sensor doesn't work
732 #define AO_MAX_BARO_HEIGHT 12000
735 * Above this speed, baro measurements are unreliable
737 #define AO_MAX_BARO_SPEED 200
739 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
742 * Speed and acceleration are scaled by 16 to provide a bit more
743 * resolution while still having reasonable range. Note that this
744 * limits speed to 2047m/s (around mach 6) and acceleration to
745 * 2047m/s² (over 200g)
748 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
749 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
750 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
752 extern __pdata uint16_t ao_sample_tick; /* time of last data */
753 extern __pdata int16_t ao_sample_pres; /* most recent pressure sensor reading */
754 extern __pdata int16_t ao_sample_alt; /* MSL of ao_sample_pres */
755 extern __pdata int16_t ao_sample_height; /* AGL of ao_sample_pres */
756 extern __data uint8_t ao_sample_adc; /* Ring position of last processed sample */
759 extern __pdata int16_t ao_sample_accel; /* most recent accel sensor reading */
762 extern __pdata int16_t ao_ground_pres; /* startup pressure */
763 extern __pdata int16_t ao_ground_height; /* MSL of ao_ground_pres */
766 extern __pdata int16_t ao_ground_accel; /* startup acceleration */
767 extern __pdata int16_t ao_accel_2g; /* factory accel calibration */
768 extern __pdata int32_t ao_accel_scale; /* sensor to m/s² conversion */
771 void ao_sample_init(void);
773 /* returns FALSE in preflight mode, TRUE in flight mode */
774 uint8_t ao_sample(void);
780 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
781 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
782 #define from_fix(x) ((x) >> 16)
784 extern __pdata int16_t ao_height; /* meters */
785 extern __pdata int16_t ao_speed; /* m/s * 16 */
786 extern __pdata int16_t ao_accel; /* m/s² * 16 */
787 extern __pdata int16_t ao_max_height; /* max of ao_height */
788 extern __pdata int16_t ao_avg_height; /* running average of height */
790 extern __pdata int16_t ao_error_h;
791 extern __pdata int16_t ao_error_h_sq_avg;
794 extern __pdata int16_t ao_error_a;
797 void ao_kalman(void);
804 ao_report_init(void);
809 * Given raw data, convert to SI units
812 /* pressure from the sensor to altitude in meters */
814 ao_pres_to_altitude(int16_t pres) __reentrant;
817 ao_altitude_to_pres(int16_t alt) __reentrant;
820 ao_temp_to_dC(int16_t temp) __reentrant;
825 * debug another telemetrum board
828 /* Send a byte to the dbg target */
830 ao_dbg_send_byte(uint8_t byte);
832 /* Receive a byte from the dbg target */
834 ao_dbg_recv_byte(void);
836 /* Start a bulk transfer to/from dbg target memory */
838 ao_dbg_start_transfer(uint16_t addr);
840 /* End a bulk transfer to/from dbg target memory */
842 ao_dbg_end_transfer(void);
844 /* Write a byte to dbg target memory */
846 ao_dbg_write_byte(uint8_t byte);
848 /* Read a byte from dbg target memory */
850 ao_dbg_read_byte(void);
852 /* Enable dbg mode, switching use of the pins */
854 ao_dbg_debug_mode(void);
856 /* Reset the dbg target */
868 #error Please define HAS_SERIAL_1
871 #if HAS_SERIAL_1 | HAS_SERIAL_2 | HAS_SERIAL_3
872 #ifndef USE_SERIAL_STDIN
873 #error Please define USE_SERIAL_STDIN
877 ao_serial_rx1_isr(void) ao_arch_interrupt(3);
880 ao_serial_tx1_isr(void) ao_arch_interrupt(14);
883 ao_serial_getchar(void) __critical;
887 ao_serial_pollchar(void) __critical;
890 ao_serial_set_stdin(uint8_t in);
894 ao_serial_putchar(char c) __critical;
897 ao_serial_drain(void) __critical;
899 #define AO_SERIAL_SPEED_4800 0
900 #define AO_SERIAL_SPEED_9600 1
901 #define AO_SERIAL_SPEED_19200 2
902 #define AO_SERIAL_SPEED_57600 3
905 ao_serial_set_speed(uint8_t speed);
908 ao_serial_init(void);
912 #define HAS_SERIAL_0 0
917 extern volatile __xdata struct ao_fifo ao_usart0_rx_fifo;
918 extern volatile __xdata struct ao_fifo ao_usart0_tx_fifo;
921 ao_serial0_rx0_isr(void) ao_arch_interrupt(2);
924 ao_serial0_tx0_isr(void) ao_arch_interrupt(7);
927 ao_serial0_getchar(void) __critical;
930 ao_serial0_putchar(char c) __critical;
933 ao_serial0_drain(void) __critical;
936 ao_serial0_set_speed(uint8_t speed);
939 ao_serial0_init(void);
949 ao_spi_slave_recv(uint8_t *buf, uint8_t len);
952 ao_spi_slave_send(uint8_t *buf, uint8_t len);
955 ao_spi_slave_init(void);
957 /* This must be defined by the product; it will get called when chip
958 * select goes low, at which point it should use ao_spi_read and
959 * ao_spi_write to deal with the request
968 #define AO_MAX_CALLSIGN 8
969 #define AO_MAX_VERSION 8
971 #define AO_MAX_TELEMETRY 128
973 #define AO_MAX_TELEMETRY 32
976 struct ao_telemetry_generic {
977 uint16_t serial; /* 0 */
978 uint16_t tick; /* 2 */
979 uint8_t type; /* 4 */
980 uint8_t payload[27]; /* 5 */
984 #define AO_TELEMETRY_SENSOR_TELEMETRUM 0x01
985 #define AO_TELEMETRY_SENSOR_TELEMINI 0x02
986 #define AO_TELEMETRY_SENSOR_TELENANO 0x03
988 struct ao_telemetry_sensor {
989 uint16_t serial; /* 0 */
990 uint16_t tick; /* 2 */
991 uint8_t type; /* 4 */
993 uint8_t state; /* 5 flight state */
994 int16_t accel; /* 6 accelerometer (TM only) */
995 int16_t pres; /* 8 pressure sensor */
996 int16_t temp; /* 10 temperature sensor */
997 int16_t v_batt; /* 12 battery voltage */
998 int16_t sense_d; /* 14 drogue continuity sense (TM/Tm) */
999 int16_t sense_m; /* 16 main continuity sense (TM/Tm) */
1001 int16_t acceleration; /* 18 m/s² * 16 */
1002 int16_t speed; /* 20 m/s * 16 */
1003 int16_t height; /* 22 m */
1005 int16_t ground_pres; /* 24 average pres on pad */
1006 int16_t ground_accel; /* 26 average accel on pad */
1007 int16_t accel_plus_g; /* 28 accel calibration at +1g */
1008 int16_t accel_minus_g; /* 30 accel calibration at -1g */
1012 #define AO_TELEMETRY_CONFIGURATION 0x04
1014 struct ao_telemetry_configuration {
1015 uint16_t serial; /* 0 */
1016 uint16_t tick; /* 2 */
1017 uint8_t type; /* 4 */
1019 uint8_t device; /* 5 device type */
1020 uint16_t flight; /* 6 flight number */
1021 uint8_t config_major; /* 8 Config major version */
1022 uint8_t config_minor; /* 9 Config minor version */
1023 uint16_t apogee_delay; /* 10 Apogee deploy delay in seconds */
1024 uint16_t main_deploy; /* 12 Main deploy alt in meters */
1025 uint16_t flight_log_max; /* 14 Maximum flight log size in kB */
1026 char callsign[AO_MAX_CALLSIGN]; /* 16 Radio operator identity */
1027 char version[AO_MAX_VERSION]; /* 24 Software version */
1031 #define AO_TELEMETRY_LOCATION 0x05
1033 #define AO_GPS_MODE_NOT_VALID 'N'
1034 #define AO_GPS_MODE_AUTONOMOUS 'A'
1035 #define AO_GPS_MODE_DIFFERENTIAL 'D'
1036 #define AO_GPS_MODE_ESTIMATED 'E'
1037 #define AO_GPS_MODE_MANUAL 'M'
1038 #define AO_GPS_MODE_SIMULATED 'S'
1040 struct ao_telemetry_location {
1041 uint16_t serial; /* 0 */
1042 uint16_t tick; /* 2 */
1043 uint8_t type; /* 4 */
1045 uint8_t flags; /* 5 Number of sats and other flags */
1046 int16_t altitude; /* 6 GPS reported altitude (m) */
1047 int32_t latitude; /* 8 latitude (degrees * 10⁷) */
1048 int32_t longitude; /* 12 longitude (degrees * 10⁷) */
1049 uint8_t year; /* 16 (- 2000) */
1050 uint8_t month; /* 17 (1-12) */
1051 uint8_t day; /* 18 (1-31) */
1052 uint8_t hour; /* 19 (0-23) */
1053 uint8_t minute; /* 20 (0-59) */
1054 uint8_t second; /* 21 (0-59) */
1055 uint8_t pdop; /* 22 (m * 5) */
1056 uint8_t hdop; /* 23 (m * 5) */
1057 uint8_t vdop; /* 24 (m * 5) */
1058 uint8_t mode; /* 25 */
1059 uint16_t ground_speed; /* 26 cm/s */
1060 int16_t climb_rate; /* 28 cm/s */
1061 uint8_t course; /* 30 degrees / 2 */
1062 uint8_t unused[1]; /* 31 */
1066 #define AO_TELEMETRY_SATELLITE 0x06
1068 struct ao_telemetry_satellite_info {
1073 struct ao_telemetry_satellite {
1074 uint16_t serial; /* 0 */
1075 uint16_t tick; /* 2 */
1076 uint8_t type; /* 4 */
1077 uint8_t channels; /* 5 number of reported sats */
1079 struct ao_telemetry_satellite_info sats[12]; /* 6 */
1080 uint8_t unused[2]; /* 30 */
1084 #define AO_TELEMETRY_COMPANION 0x07
1086 #define AO_COMPANION_MAX_CHANNELS 12
1088 struct ao_telemetry_companion {
1089 uint16_t serial; /* 0 */
1090 uint16_t tick; /* 2 */
1091 uint8_t type; /* 4 */
1092 uint8_t board_id; /* 5 */
1094 uint8_t update_period; /* 6 */
1095 uint8_t channels; /* 7 */
1096 uint16_t companion_data[AO_COMPANION_MAX_CHANNELS]; /* 8 */
1100 /* #define AO_SEND_ALL_BARO */
1102 #define AO_TELEMETRY_BARO 0x80
1105 * This packet allows the full sampling rate baro
1106 * data to be captured over the RF link so that the
1107 * flight software can be tested using 'real' data.
1109 * Along with this telemetry packet, the flight
1110 * code is modified to send full-rate telemetry all the time
1111 * and never send an RDF tone; this ensure that the full radio
1112 * link is available.
1114 struct ao_telemetry_baro {
1115 uint16_t serial; /* 0 */
1116 uint16_t tick; /* 2 */
1117 uint8_t type; /* 4 */
1118 uint8_t samples; /* 5 number samples */
1120 int16_t baro[12]; /* 6 samples */
1124 union ao_telemetry_all {
1125 struct ao_telemetry_generic generic;
1126 struct ao_telemetry_sensor sensor;
1127 struct ao_telemetry_configuration configuration;
1128 struct ao_telemetry_location location;
1129 struct ao_telemetry_satellite satellite;
1130 struct ao_telemetry_companion companion;
1131 struct ao_telemetry_baro baro;
1134 struct ao_telemetry_all_recv {
1135 union ao_telemetry_all telemetry;
1144 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
1145 #define AO_GPS_NUM_SAT_SHIFT (0)
1147 #define AO_GPS_VALID (1 << 4)
1148 #define AO_GPS_RUNNING (1 << 5)
1149 #define AO_GPS_DATE_VALID (1 << 6)
1150 #define AO_GPS_COURSE_VALID (1 << 7)
1152 extern __pdata uint16_t ao_gps_tick;
1153 extern __xdata uint8_t ao_gps_mutex;
1154 extern __xdata struct ao_telemetry_location ao_gps_data;
1155 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
1157 struct ao_gps_orig {
1165 int32_t latitude; /* degrees * 10⁷ */
1166 int32_t longitude; /* degrees * 10⁷ */
1167 int16_t altitude; /* m */
1168 uint16_t ground_speed; /* cm/s */
1169 uint8_t course; /* degrees / 2 */
1170 uint8_t hdop; /* * 5 */
1171 int16_t climb_rate; /* cm/s */
1172 uint16_t h_error; /* m */
1173 uint16_t v_error; /* m */
1176 struct ao_gps_sat_orig {
1181 #define AO_MAX_GPS_TRACKING 12
1183 struct ao_gps_tracking_orig {
1185 struct ao_gps_sat_orig sats[AO_MAX_GPS_TRACKING];
1192 ao_gps_print(__xdata struct ao_gps_orig *gps_data);
1195 ao_gps_tracking_print(__xdata struct ao_gps_tracking_orig *gps_tracking_data);
1205 ao_gps_report(void);
1208 ao_gps_report_init(void);
1211 * ao_telemetry_orig.c
1214 struct ao_telemetry_orig {
1217 uint8_t flight_state;
1219 int16_t ground_accel;
1228 int16_t ground_pres;
1229 int16_t accel_plus_g;
1230 int16_t accel_minus_g;
1232 struct ao_gps_orig gps;
1233 char callsign[AO_MAX_CALLSIGN];
1234 struct ao_gps_tracking_orig gps_tracking;
1237 struct ao_telemetry_tiny {
1240 uint8_t flight_state;
1241 int16_t height; /* AGL in meters */
1242 int16_t speed; /* in m/s * 16 */
1243 int16_t accel; /* in m/s² * 16 */
1244 int16_t ground_pres; /* sensor units */
1245 struct ao_adc adc; /* raw ADC readings */
1246 char callsign[AO_MAX_CALLSIGN];
1249 struct ao_telemetry_orig_recv {
1250 struct ao_telemetry_orig telemetry_orig;
1255 struct ao_telemetry_tiny_recv {
1256 struct ao_telemetry_tiny telemetry_tiny;
1262 * ao_radio_recv tacks on rssi and status bytes
1265 struct ao_telemetry_raw_recv {
1266 uint8_t packet[AO_MAX_TELEMETRY + 2];
1269 /* Set delay between telemetry reports (0 to disable) */
1271 #ifdef AO_SEND_ALL_BARO
1272 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(100)
1273 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(100)
1274 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(100)
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)
1282 ao_telemetry_set_interval(uint16_t interval);
1285 ao_rdf_set(uint8_t rdf);
1288 ao_telemetry_init(void);
1291 ao_telemetry_orig_init(void);
1294 ao_telemetry_tiny_init(void);
1300 extern __xdata uint8_t ao_radio_dma;
1301 extern __xdata uint8_t ao_radio_dma_done;
1302 extern __xdata uint8_t ao_radio_done;
1303 extern __xdata uint8_t ao_radio_mutex;
1306 ao_radio_general_isr(void) ao_arch_interrupt(16);
1309 ao_radio_get(uint8_t len);
1311 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
1314 ao_radio_set_packet(void);
1317 ao_radio_send(__xdata void *d, uint8_t size) __reentrant;
1320 ao_radio_recv(__xdata void *d, uint8_t size) __reentrant;
1323 ao_radio_recv_abort(void);
1326 * Compute the packet length as follows:
1328 * 2000 bps (for a 1kHz tone)
1329 * so, for 'ms' milliseconds, we need
1330 * 2 * ms bits, or ms / 4 bytes
1333 #define AO_MS_TO_RDF_LEN(ms) ((ms) > 255 * 4 ? 255 : ((ms) >> 2))
1336 ao_radio_rdf(uint8_t pkt_len);
1339 ao_radio_rdf_abort(void);
1342 ao_radio_idle(void);
1345 ao_radio_init(void);
1351 extern const char const * const ao_state_names[];
1353 #define AO_MONITOR_RING 8
1356 struct ao_telemetry_raw_recv raw;
1357 struct ao_telemetry_all_recv all;
1358 struct ao_telemetry_orig_recv orig;
1359 struct ao_telemetry_tiny_recv tiny;
1362 extern __xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
1364 #define ao_monitor_ring_next(n) (((n) + 1) & (AO_MONITOR_RING - 1))
1366 extern __data uint8_t ao_monitoring;
1367 extern __data uint8_t ao_monitor_head;
1372 #define AO_MONITORING_OFF 0
1373 #define AO_MONITORING_ORIG 1
1376 ao_monitor_set(uint8_t monitoring);
1379 ao_monitor_disable(void);
1382 ao_monitor_enable(void);
1385 ao_monitor_init(void) __reentrant;
1391 #define AO_READ_AGAIN ((char) -1)
1394 char (*pollchar)(void);
1395 void (*putchar)(char c) __reentrant;
1396 void (*flush)(void);
1400 extern __xdata struct ao_stdio ao_stdios[];
1401 extern __pdata int8_t ao_cur_stdio;
1402 extern __pdata int8_t ao_num_stdios;
1407 extern __xdata uint8_t ao_stdin_ready;
1413 ao_add_stdio(char (*pollchar)(void),
1414 void (*putchar)(char) __reentrant,
1415 void (*flush)(void)) __reentrant;
1422 ao_igniter_drogue = 0,
1427 ao_ignite(enum ao_igniter igniter);
1429 enum ao_igniter_status {
1430 ao_igniter_unknown, /* unknown status (ambiguous voltage) */
1431 ao_igniter_ready, /* continuity detected */
1432 ao_igniter_active, /* igniter firing */
1433 ao_igniter_open, /* open circuit detected */
1436 struct ao_ignition {
1442 extern __xdata struct ao_ignition ao_ignition[2];
1444 enum ao_igniter_status
1445 ao_igniter_status(enum ao_igniter igniter);
1447 extern __pdata uint8_t ao_igniter_present;
1450 ao_ignite_set_pins(void);
1453 ao_igniter_init(void);
1459 #define AO_CONFIG_MAJOR 1
1460 #define AO_CONFIG_MINOR 10
1461 #define AO_AES_LEN 16
1463 #if HAS_RADIO_CHANNELS
1464 #define AO_CHANNEL_NAME_LEN 10
1466 #define AO_NUM_CHANNELS 10
1468 struct ao_radio_channel {
1469 char name[AO_CHANNEL_NAME_LEN];
1477 uint16_t main_deploy;
1478 int16_t accel_plus_g; /* changed for minor version 2 */
1479 uint8_t radio_channel;
1480 char callsign[AO_MAX_CALLSIGN + 1];
1481 uint8_t apogee_delay; /* minor version 1 */
1482 int16_t accel_minus_g; /* minor version 2 */
1483 uint32_t radio_cal; /* minor version 3 */
1484 uint32_t flight_log_max; /* minor version 4 */
1485 uint8_t ignite_mode; /* minor version 5 */
1486 uint8_t pad_orientation; /* minor version 6 */
1487 uint32_t radio_setting; /* minor version 7 */
1488 uint8_t radio_enable; /* minor version 8 */
1489 uint8_t aes_key[AO_AES_LEN]; /* minor version 9 */
1490 uint32_t frequency; /* minor version 10 */
1491 #if HAS_RADIO_CHANNELS
1492 struct ao_radio_channel radio_channels[AO_NUM_CHANNELS]; /* minor version 10 */
1496 #define AO_IGNITE_MODE_DUAL 0
1497 #define AO_IGNITE_MODE_APOGEE 1
1498 #define AO_IGNITE_MODE_MAIN 2
1500 #define AO_PAD_ORIENTATION_ANTENNA_UP 0
1501 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
1503 extern __xdata struct ao_config ao_config;
1505 #define AO_CONFIG_MAX_SIZE 128
1508 ao_config_get(void);
1511 ao_config_put(void);
1514 ao_config_init(void);
1521 ao_rssi_set(int rssi_value);
1524 ao_rssi_init(uint8_t rssi_led);
1529 * values which need to be defined for
1530 * each instance of a product
1533 extern const char ao_version[];
1534 extern const char ao_manufacturer[];
1535 extern const char ao_product[];
1541 #define AO_FIFO_SIZE 32
1546 char fifo[AO_FIFO_SIZE];
1549 #define ao_fifo_insert(f,c) do { \
1550 (f).fifo[(f).insert] = (c); \
1551 (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1554 #define ao_fifo_remove(f,c) do {\
1555 c = (f).fifo[(f).remove]; \
1556 (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1559 #define ao_fifo_full(f) ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1560 #define ao_fifo_empty(f) ((f).insert == (f).remove)
1565 * Packet-based command interface
1568 #define AO_PACKET_MAX 64
1569 #define AO_PACKET_SYN (uint8_t) 0xff
1576 uint8_t d[AO_PACKET_MAX];
1577 uint8_t callsign[AO_MAX_CALLSIGN];
1580 struct ao_packet_recv {
1581 struct ao_packet packet;
1586 extern __xdata struct ao_packet_recv ao_rx_packet;
1587 extern __xdata struct ao_packet ao_tx_packet;
1588 extern __xdata struct ao_task ao_packet_task;
1589 extern __xdata uint8_t ao_packet_enable;
1590 extern __xdata uint8_t ao_packet_master_sleeping;
1591 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1594 ao_packet_send(void);
1597 ao_packet_recv(void);
1600 ao_packet_flush(void);
1603 ao_packet_putchar(char c) __reentrant;
1606 ao_packet_pollchar(void) __critical;
1608 /* ao_packet_master.c */
1611 ao_packet_master_init(void);
1613 /* ao_packet_slave.c */
1616 ao_packet_slave_start(void);
1619 ao_packet_slave_stop(void);
1622 ao_packet_slave_init(uint8_t enable);
1626 /* If bt_link is on P2, this interrupt is shared by USB, so the USB
1627 * code calls this function. Otherwise, it's a regular ISR.
1640 /* ao_companion.c */
1642 #define AO_COMPANION_SETUP 1
1643 #define AO_COMPANION_FETCH 2
1644 #define AO_COMPANION_NOTIFY 3
1646 struct ao_companion_command {
1648 uint8_t flight_state;
1654 struct ao_companion_setup {
1656 uint16_t board_id_inverse;
1657 uint8_t update_period;
1661 extern __pdata uint8_t ao_companion_running;
1662 extern __xdata uint8_t ao_companion_mutex;
1663 extern __xdata struct ao_companion_command ao_companion_command;
1664 extern __xdata struct ao_companion_setup ao_companion_setup;
1665 extern __xdata uint16_t ao_companion_data[AO_COMPANION_MAX_CHANNELS];
1668 ao_companion_init(void);
1673 ao_lcd_putchar(uint8_t d);
1676 ao_lcd_putstring(char *string);
1679 ao_lcd_contrast_set(uint8_t contrast);
1685 ao_lcd_cursor_on(void);
1688 ao_lcd_cursor_off(void);
1690 #define AO_LCD_ADDR(row,col) ((row << 6) | (col))
1693 ao_lcd_goto(uint8_t addr);
1704 ao_lcd_port_put_nibble(uint8_t rs, uint8_t d);
1707 ao_lcd_port_init(void);
1711 extern __xdata uint8_t ao_aes_mutex;
1713 /* AES keys and blocks are 128 bits */
1721 ao_aes_isr(void) __interrupt 4;
1725 ao_aes_set_mode(enum ao_aes_mode mode);
1728 ao_aes_set_key(__xdata uint8_t *in);
1731 ao_aes_zero_iv(void);
1734 ao_aes_run(__xdata uint8_t *in,
1735 __xdata uint8_t *out);
1740 /* ao_radio_cmac.c */
1743 ao_radio_cmac_send(__xdata void *packet, uint8_t len) __reentrant;
1745 #define AO_RADIO_CMAC_OK 0
1746 #define AO_RADIO_CMAC_LEN_ERROR -1
1747 #define AO_RADIO_CMAC_CRC_ERROR -2
1748 #define AO_RADIO_CMAC_MAC_ERROR -3
1749 #define AO_RADIO_CMAC_TIMEOUT -4
1752 ao_radio_cmac_recv(__xdata void *packet, uint8_t len, uint16_t timeout) __reentrant;
1755 ao_radio_cmac_init(void);
1759 struct ao_launch_command {
1767 #define AO_LAUNCH_QUERY 1
1769 struct ao_launch_query {
1775 uint8_t igniter_status;
1778 #define AO_LAUNCH_ARM 2
1779 #define AO_LAUNCH_FIRE 3
1782 ao_launch_init(void);
1788 #define AO_LOG_TELESCIENCE_START ((uint8_t) 's')
1789 #define AO_LOG_TELESCIENCE_DATA ((uint8_t) 'd')
1791 #define AO_LOG_TELESCIENCE_NUM_ADC 12
1793 struct ao_log_telescience {
1800 uint16_t adc[AO_LOG_TELESCIENCE_NUM_ADC];
1803 #define AO_LOG_SINGLE_SIZE 32
1805 union ao_log_single {
1806 struct ao_log_telescience telescience;
1807 union ao_telemetry_all telemetry;
1808 uint8_t bytes[AO_LOG_SINGLE_SIZE];
1811 extern __xdata union ao_log_single ao_log_single_write_data;
1812 extern __xdata union ao_log_single ao_log_single_read_data;
1815 ao_log_single_extra_query(void);
1818 ao_log_single_list(void);
1821 ao_log_single_main(void);
1824 ao_log_single_write(void);
1827 ao_log_single_read(uint32_t pos);
1830 ao_log_single_start(void);
1833 ao_log_single_stop(void);
1836 ao_log_single_restart(void);
1839 ao_log_single_set(void);
1842 ao_log_single_delete(void);
1845 ao_log_single_init(void);
1848 ao_log_single(void);
1854 #define AO_TELEPYRO_NUM_ADC 9
1857 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
1858 #define ao_xmemset(d,v,c) memset(d,v,c)
1859 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
1867 ao_terraui_init(void);
1875 ao_battery_isr(void) ao_arch_interrupt(1);
1878 ao_battery_get(void);
1881 ao_battery_init(void);
1882 #endif /* BATTERY_PIN */
1889 ao_sqrt(uint32_t op);
1895 int32_t ao_freq_to_set(int32_t freq, int32_t cal);
1897 #include <ao_arch_funcs.h>
1903 void ao_ms5607_init(void);