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 /* Yield the processor to another task */
73 ao_yield(void) ao_arch_naked_declare;
75 /* Add a task to the run queue */
77 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
79 /* Terminate the current task */
83 /* Dump task info to console */
87 /* Start the scheduler. This will not return */
89 ao_start_scheduler(void);
95 #define AO_PANIC_NO_TASK 1 /* AO_NUM_TASKS is not large enough */
96 #define AO_PANIC_DMA 2 /* Attempt to start DMA while active */
97 #define AO_PANIC_MUTEX 3 /* Mis-using mutex API */
98 #define AO_PANIC_EE 4 /* Mis-using eeprom API */
99 #define AO_PANIC_LOG 5 /* Failing to read/write log data */
100 #define AO_PANIC_CMD 6 /* Too many command sets registered */
101 #define AO_PANIC_STDIO 7 /* Too many stdio handlers registered */
102 #define AO_PANIC_REBOOT 8 /* Reboot failed */
103 #define AO_PANIC_FLASH 9 /* Invalid flash part (or wrong blocksize) */
104 #define AO_PANIC_USB 10 /* Trying to send USB packet while busy */
105 #define AO_PANIC_BT 11 /* Communications with bluetooth device failed */
107 /* Stop the operating system, beeping and blinking the reason */
109 ao_panic(uint8_t reason);
115 /* Our timer runs at 100Hz */
117 #define AO_MS_TO_TICKS(ms) ((ms) / (1000 / AO_HERTZ))
118 #define AO_SEC_TO_TICKS(s) ((s) * AO_HERTZ)
120 /* Returns the current time in ticks */
124 /* Suspend the current task until ticks time has passed */
126 ao_delay(uint16_t ticks);
128 /* Set the ADC interval */
130 ao_timer_set_adc_interval(uint8_t interval) __critical;
132 /* Timer interrupt */
134 ao_timer_isr(void) ao_arch_interrupt(9);
136 /* Initialize the timer */
140 /* Initialize the hardware clock. Must be called first */
145 * One set of samples read from the A/D converter or telemetry
154 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
155 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
159 * A/D data is stored in a ring, with the next sample to be written
162 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
163 extern volatile __data uint8_t ao_adc_head;
165 extern volatile __xdata uint16_t ao_accel_ref[AO_ADC_RING];
168 /* Trigger a conversion sequence (called from the timer interrupt) */
172 /* Suspend the current task until another A/D sample is converted */
176 /* Get a copy of the last complete A/D sample set */
178 ao_adc_get(__xdata struct ao_adc *packet);
180 /* The A/D interrupt handler */
183 ao_adc_isr(void) ao_arch_interrupt(1);
185 /* Initialize the A/D converter */
196 * Various pre-defined beep frequencies
198 * frequency = 1/2 (24e6/32) / beep
201 #define AO_BEEP_LOW 150 /* 2500Hz */
202 #define AO_BEEP_MID 94 /* 3989Hz */
203 #define AO_BEEP_HIGH 75 /* 5000Hz */
204 #define AO_BEEP_OFF 0 /* off */
206 #define AO_BEEP_g 240 /* 1562.5Hz */
207 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
208 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
209 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
210 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
211 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
212 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
213 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
214 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
215 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
216 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
217 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
218 #define AO_BEEP_gg 120 /* 3125Hz */
219 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
220 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
221 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
222 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
223 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
224 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
225 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
226 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
227 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
228 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
229 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
230 #define AO_BEEP_ggg 60 /* 6250Hz */
232 /* Set the beeper to the specified tone */
234 ao_beep(uint8_t beep);
236 /* Turn on the beeper for the specified time */
238 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
240 /* Initialize the beeper */
248 #define AO_LED_NONE 0
250 /* Turn on the specified LEDs */
252 ao_led_on(uint8_t colors);
254 /* Turn off the specified LEDs */
256 ao_led_off(uint8_t colors);
258 /* Set all of the LEDs to the specified state */
260 ao_led_set(uint8_t colors);
262 /* Toggle the specified LEDs */
264 ao_led_toggle(uint8_t colors);
266 /* Turn on the specified LEDs for the indicated interval */
268 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
270 /* Initialize the LEDs */
272 ao_led_init(uint8_t enable);
278 /* Put one character to the USB output queue */
280 ao_usb_putchar(char c);
282 /* Get one character from the USB input queue */
284 ao_usb_getchar(void);
286 /* Poll for a charcter on the USB input queue.
287 * returns AO_READ_AGAIN if none are available
290 ao_usb_pollchar(void);
292 /* Flush the USB output queue */
297 /* USB interrupt handler */
299 ao_usb_isr(void) ao_arch_interrupt(6);
302 /* Enable the USB controller */
306 /* Disable the USB controller */
308 ao_usb_disable(void);
310 /* Initialize the USB system */
315 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
324 ao_cmd_lex_error = 1,
325 ao_cmd_syntax_error = 2,
328 extern __pdata uint16_t ao_cmd_lex_i;
329 extern __pdata uint32_t ao_cmd_lex_u32;
330 extern __pdata char ao_cmd_lex_c;
331 extern __pdata enum ao_cmd_status ao_cmd_status;
337 ao_cmd_put8(uint8_t v);
340 ao_cmd_put16(uint16_t v);
349 ao_cmd_decimal(void);
352 ao_match_word(__code char *word);
360 ao_cmd_register(__code struct ao_cmds *cmds);
367 * Provided by an external module to filter raw command lines
377 /* Allocate a DMA channel. the 'done' parameter will be set when the
378 * dma is finished and will be used to wakeup any waiters
382 ao_dma_alloc(__xdata uint8_t * done);
384 /* Setup a DMA channel */
386 ao_dma_set_transfer(uint8_t id,
387 void __xdata *srcaddr,
388 void __xdata *dstaddr,
393 /* Start a DMA channel */
395 ao_dma_start(uint8_t id);
397 /* Manually trigger a DMA channel */
399 ao_dma_trigger(uint8_t id);
401 /* Abort a running DMA transfer */
403 ao_dma_abort(uint8_t id);
405 /* DMA interrupt routine */
407 ao_dma_isr(void) ao_arch_interrupt(8);
414 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
417 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
420 * Storage interface, provided by one of the eeprom or flash
424 /* Total bytes of available storage */
425 extern __pdata uint32_t ao_storage_total;
427 /* Block size - device is erased in these units. At least 256 bytes */
428 extern __pdata uint32_t ao_storage_block;
430 /* Byte offset of config block. Will be ao_storage_block bytes long */
431 extern __pdata uint32_t ao_storage_config;
433 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
434 extern __pdata uint16_t ao_storage_unit;
436 #define AO_STORAGE_ERASE_LOG (ao_storage_config + AO_CONFIG_MAX_SIZE)
438 /* Initialize above values. Can only be called once the OS is running */
440 ao_storage_setup(void) __reentrant;
442 /* Write data. Returns 0 on failure, 1 on success */
444 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
446 /* Read data. Returns 0 on failure, 1 on success */
448 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
450 /* Erase a block of storage. This always clears ao_storage_block bytes */
452 ao_storage_erase(uint32_t pos) __reentrant;
454 /* Flush any pending writes to stable storage */
456 ao_storage_flush(void) __reentrant;
458 /* Initialize the storage code */
460 ao_storage_init(void);
463 * Low-level functions wrapped by ao_storage.c
466 /* Read data within a storage unit */
468 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
470 /* Write data within a storage unit */
472 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
474 /* Initialize low-level device bits */
476 ao_storage_device_init(void);
478 /* Print out information about flash chips */
480 ao_storage_device_info(void) __reentrant;
486 /* We record flight numbers in the first record of
487 * the log. Tasks may wait for this to be initialized
488 * by sleeping on this variable.
490 extern __xdata uint16_t ao_flight_number;
492 extern __pdata uint32_t ao_log_current_pos;
493 extern __pdata uint32_t ao_log_end_pos;
494 extern __pdata uint32_t ao_log_start_pos;
495 extern __xdata uint8_t ao_log_running;
496 extern __pdata enum flight_state ao_log_state;
498 #define AO_LOG_TELESCIENCE_START ((uint8_t) 's')
499 #define AO_LOG_TELESCIENCE_DATA ((uint8_t) 'd')
501 #define AO_LOG_TELESCIENCE_NUM_ADC 12
503 struct ao_log_telescience {
510 uint16_t adc[AO_LOG_TELESCIENCE_NUM_ADC];
513 extern struct ao_log_telescience ao_log_store;
515 /* required functions from the underlying log system */
517 #define AO_LOG_FORMAT_UNKNOWN 0 /* unknown; altosui will have to guess */
518 #define AO_LOG_FORMAT_FULL 1 /* 8 byte typed log records */
519 #define AO_LOG_FORMAT_TINY 2 /* two byte state/baro records */
520 #define AO_LOG_FORMAT_TELEMETRY 3 /* 32 byte ao_telemetry records */
521 #define AO_LOG_FORMAT_TELESCIENCE 4 /* 32 byte typed telescience records */
522 #define AO_LOG_FORMAT_NONE 127 /* No log at all */
524 extern __code uint8_t ao_log_format;
526 /* Return the flight number from the given log slot, 0 if none */
528 ao_log_flight(uint8_t slot);
534 /* Logging thread main routine */
538 /* functions provided in ao_log.c */
540 /* Figure out the current flight number */
542 ao_log_scan(void) __reentrant;
544 /* Return the position of the start of the given log slot */
546 ao_log_pos(uint8_t slot);
548 /* Start logging to eeprom */
556 /* Initialize the logging system */
560 /* Write out the current flight number to the erase log */
562 ao_log_write_erase(uint8_t pos);
564 /* Returns true if there are any logs stored in eeprom */
566 ao_log_present(void);
568 /* Returns true if there is no more storage space available */
577 * The data log is recorded in the eeprom as a sequence
580 * Each packet starts with a 4-byte header that has the
581 * packet type, the packet checksum and the tick count. Then
582 * they all contain 2 16 bit values which hold packet-specific
585 * For each flight, the first packet
586 * is FLIGHT packet, indicating the serial number of the
587 * device and a unique number marking the number of flights
588 * recorded by this device.
590 * During flight, data from the accelerometer and barometer
591 * are recorded in SENSOR packets, using the raw 16-bit values
592 * read from the A/D converter.
594 * Also during flight, but at a lower rate, the deployment
595 * sensors are recorded in DEPLOY packets. The goal here is to
596 * detect failure in the deployment circuits.
598 * STATE packets hold state transitions as the flight computer
599 * transitions through different stages of the flight.
601 #define AO_LOG_FLIGHT 'F'
602 #define AO_LOG_SENSOR 'A'
603 #define AO_LOG_TEMP_VOLT 'T'
604 #define AO_LOG_DEPLOY 'D'
605 #define AO_LOG_STATE 'S'
606 #define AO_LOG_GPS_TIME 'G'
607 #define AO_LOG_GPS_LAT 'N'
608 #define AO_LOG_GPS_LON 'W'
609 #define AO_LOG_GPS_ALT 'H'
610 #define AO_LOG_GPS_SAT 'V'
611 #define AO_LOG_GPS_DATE 'Y'
613 #define AO_LOG_POS_NONE (~0UL)
615 struct ao_log_record {
621 int16_t ground_accel;
646 int32_t gps_latitude;
647 int32_t gps_longitude;
670 /* Write a record to the eeprom log */
672 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
678 enum ao_flight_state {
679 ao_flight_startup = 0,
685 ao_flight_drogue = 6,
687 ao_flight_landed = 8,
688 ao_flight_invalid = 9
691 extern __pdata enum ao_flight_state ao_flight_state;
693 extern __pdata uint16_t ao_launch_time;
694 extern __pdata uint8_t ao_flight_force_idle;
700 /* Initialize flight thread */
702 ao_flight_init(void);
709 ao_flight_nano_init(void);
716 * Barometer calibration
718 * We directly sample the barometer. The specs say:
720 * Pressure range: 15-115 kPa
721 * Voltage at 115kPa: 2.82
722 * Output scale: 27mV/kPa
724 * If we want to detect launch with the barometer, we need
725 * a large enough bump to not be fooled by noise. At typical
726 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
727 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
728 * As all of our calculations are done in 16 bits, we'll actually see a change
729 * of 16 times this though
731 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
734 /* Accelerometer calibration
736 * We're sampling the accelerometer through a resistor divider which
737 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
738 * That goes into the cc1111 A/D converter, which is running at 11 bits
739 * of precision with the bits in the MSB of the 16 bit value. Only positive
740 * values are used, so values should range from 0-32752 for 0-3.3V. The
741 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
742 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
743 * for a final computation of:
745 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
747 * Zero g was measured at 16000 (we would expect 16384).
748 * Note that this value is only require to tell if the
749 * rocket is standing upright. Once that is determined,
750 * the value of the accelerometer is averaged for 100 samples
751 * to find the resting accelerometer value, which is used
752 * for all further flight computations
755 #define GRAVITY 9.80665
758 * Above this height, the baro sensor doesn't work
760 #define AO_MAX_BARO_HEIGHT 12000
763 * Above this speed, baro measurements are unreliable
765 #define AO_MAX_BARO_SPEED 200
767 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
770 * Speed and acceleration are scaled by 16 to provide a bit more
771 * resolution while still having reasonable range. Note that this
772 * limits speed to 2047m/s (around mach 6) and acceleration to
773 * 2047m/s² (over 200g)
776 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
777 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
778 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
780 extern __pdata uint16_t ao_sample_tick; /* time of last data */
781 extern __pdata int16_t ao_sample_pres; /* most recent pressure sensor reading */
782 extern __pdata int16_t ao_sample_alt; /* MSL of ao_sample_pres */
783 extern __pdata int16_t ao_sample_height; /* AGL of ao_sample_pres */
784 extern __data uint8_t ao_sample_adc; /* Ring position of last processed sample */
787 extern __pdata int16_t ao_sample_accel; /* most recent accel sensor reading */
790 extern __pdata int16_t ao_ground_pres; /* startup pressure */
791 extern __pdata int16_t ao_ground_height; /* MSL of ao_ground_pres */
794 extern __pdata int16_t ao_ground_accel; /* startup acceleration */
795 extern __pdata int16_t ao_accel_2g; /* factory accel calibration */
796 extern __pdata int32_t ao_accel_scale; /* sensor to m/s² conversion */
799 void ao_sample_init(void);
801 /* returns FALSE in preflight mode, TRUE in flight mode */
802 uint8_t ao_sample(void);
808 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
809 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
810 #define from_fix(x) ((x) >> 16)
812 extern __pdata int16_t ao_height; /* meters */
813 extern __pdata int16_t ao_speed; /* m/s * 16 */
814 extern __pdata int16_t ao_accel; /* m/s² * 16 */
815 extern __pdata int16_t ao_max_height; /* max of ao_height */
816 extern __pdata int16_t ao_avg_height; /* running average of height */
818 extern __pdata int16_t ao_error_h;
819 extern __pdata int16_t ao_error_h_sq_avg;
822 extern __pdata int16_t ao_error_a;
825 void ao_kalman(void);
832 ao_report_init(void);
837 * Given raw data, convert to SI units
840 /* pressure from the sensor to altitude in meters */
842 ao_pres_to_altitude(int16_t pres) __reentrant;
845 ao_altitude_to_pres(int16_t alt) __reentrant;
848 ao_temp_to_dC(int16_t temp) __reentrant;
853 * debug another telemetrum board
856 /* Send a byte to the dbg target */
858 ao_dbg_send_byte(uint8_t byte);
860 /* Receive a byte from the dbg target */
862 ao_dbg_recv_byte(void);
864 /* Start a bulk transfer to/from dbg target memory */
866 ao_dbg_start_transfer(uint16_t addr);
868 /* End a bulk transfer to/from dbg target memory */
870 ao_dbg_end_transfer(void);
872 /* Write a byte to dbg target memory */
874 ao_dbg_write_byte(uint8_t byte);
876 /* Read a byte from dbg target memory */
878 ao_dbg_read_byte(void);
880 /* Enable dbg mode, switching use of the pins */
882 ao_dbg_debug_mode(void);
884 /* Reset the dbg target */
896 #error Please define HAS_SERIAL_1
900 #ifndef USE_SERIAL_STDIN
901 #error Please define USE_SERIAL_STDIN
905 ao_serial_rx1_isr(void) ao_arch_interrupt(3);
908 ao_serial_tx1_isr(void) ao_arch_interrupt(14);
911 ao_serial_getchar(void) __critical;
915 ao_serial_pollchar(void) __critical;
918 ao_serial_set_stdin(uint8_t stdin);
922 ao_serial_putchar(char c) __critical;
925 ao_serial_drain(void) __critical;
927 #define AO_SERIAL_SPEED_4800 0
928 #define AO_SERIAL_SPEED_9600 1
929 #define AO_SERIAL_SPEED_19200 2
930 #define AO_SERIAL_SPEED_57600 3
933 ao_serial_set_speed(uint8_t speed);
936 ao_serial_init(void);
943 extern __xdata uint8_t ao_spi_mutex;
945 #define ao_spi_get_mask(reg,mask) do {\
946 ao_mutex_get(&ao_spi_mutex); \
950 #define ao_spi_put_mask(reg,mask) do { \
952 ao_mutex_put(&ao_spi_mutex); \
955 #define ao_spi_get_bit(bit) do {\
956 ao_mutex_get(&ao_spi_mutex); \
960 #define ao_spi_put_bit(bit) do { \
962 ao_mutex_put(&ao_spi_mutex); \
966 * The SPI mutex must be held to call either of these
967 * functions -- this mutex covers the entire SPI operation,
968 * from chip select low to chip select high
972 ao_spi_send(void __xdata *block, uint16_t len) __reentrant;
975 ao_spi_recv(void __xdata *block, uint16_t len) __reentrant;
985 ao_spi_slave_debug(void);
988 ao_spi_slave_init(void);
993 #define AO_MAX_CALLSIGN 8
994 #define AO_MAX_VERSION 8
995 #define AO_MAX_TELEMETRY 128
997 struct ao_telemetry_generic {
998 uint16_t serial; /* 0 */
999 uint16_t tick; /* 2 */
1000 uint8_t type; /* 4 */
1001 uint8_t payload[27]; /* 5 */
1005 #define AO_TELEMETRY_SENSOR_TELEMETRUM 0x01
1006 #define AO_TELEMETRY_SENSOR_TELEMINI 0x02
1007 #define AO_TELEMETRY_SENSOR_TELENANO 0x03
1009 struct ao_telemetry_sensor {
1010 uint16_t serial; /* 0 */
1011 uint16_t tick; /* 2 */
1012 uint8_t type; /* 4 */
1014 uint8_t state; /* 5 flight state */
1015 int16_t accel; /* 6 accelerometer (TM only) */
1016 int16_t pres; /* 8 pressure sensor */
1017 int16_t temp; /* 10 temperature sensor */
1018 int16_t v_batt; /* 12 battery voltage */
1019 int16_t sense_d; /* 14 drogue continuity sense (TM/Tm) */
1020 int16_t sense_m; /* 16 main continuity sense (TM/Tm) */
1022 int16_t acceleration; /* 18 m/s² * 16 */
1023 int16_t speed; /* 20 m/s * 16 */
1024 int16_t height; /* 22 m */
1026 int16_t ground_pres; /* 24 average pres on pad */
1027 int16_t ground_accel; /* 26 average accel on pad */
1028 int16_t accel_plus_g; /* 28 accel calibration at +1g */
1029 int16_t accel_minus_g; /* 30 accel calibration at -1g */
1033 #define AO_TELEMETRY_CONFIGURATION 0x04
1035 struct ao_telemetry_configuration {
1036 uint16_t serial; /* 0 */
1037 uint16_t tick; /* 2 */
1038 uint8_t type; /* 4 */
1040 uint8_t device; /* 5 device type */
1041 uint16_t flight; /* 6 flight number */
1042 uint8_t config_major; /* 8 Config major version */
1043 uint8_t config_minor; /* 9 Config minor version */
1044 uint16_t apogee_delay; /* 10 Apogee deploy delay in seconds */
1045 uint16_t main_deploy; /* 12 Main deploy alt in meters */
1046 uint16_t flight_log_max; /* 14 Maximum flight log size in kB */
1047 char callsign[AO_MAX_CALLSIGN]; /* 16 Radio operator identity */
1048 char version[AO_MAX_VERSION]; /* 24 Software version */
1052 #define AO_TELEMETRY_LOCATION 0x05
1054 #define AO_GPS_MODE_NOT_VALID 'N'
1055 #define AO_GPS_MODE_AUTONOMOUS 'A'
1056 #define AO_GPS_MODE_DIFFERENTIAL 'D'
1057 #define AO_GPS_MODE_ESTIMATED 'E'
1058 #define AO_GPS_MODE_MANUAL 'M'
1059 #define AO_GPS_MODE_SIMULATED 'S'
1061 struct ao_telemetry_location {
1062 uint16_t serial; /* 0 */
1063 uint16_t tick; /* 2 */
1064 uint8_t type; /* 4 */
1066 uint8_t flags; /* 5 Number of sats and other flags */
1067 int16_t altitude; /* 6 GPS reported altitude (m) */
1068 int32_t latitude; /* 8 latitude (degrees * 10⁷) */
1069 int32_t longitude; /* 12 longitude (degrees * 10⁷) */
1070 uint8_t year; /* 16 (- 2000) */
1071 uint8_t month; /* 17 (1-12) */
1072 uint8_t day; /* 18 (1-31) */
1073 uint8_t hour; /* 19 (0-23) */
1074 uint8_t minute; /* 20 (0-59) */
1075 uint8_t second; /* 21 (0-59) */
1076 uint8_t pdop; /* 22 (m * 5) */
1077 uint8_t hdop; /* 23 (m * 5) */
1078 uint8_t vdop; /* 24 (m * 5) */
1079 uint8_t mode; /* 25 */
1080 uint16_t ground_speed; /* 26 cm/s */
1081 int16_t climb_rate; /* 28 cm/s */
1082 uint8_t course; /* 30 degrees / 2 */
1083 uint8_t unused[1]; /* 31 */
1087 #define AO_TELEMETRY_SATELLITE 0x06
1089 struct ao_telemetry_satellite_info {
1094 struct ao_telemetry_satellite {
1095 uint16_t serial; /* 0 */
1096 uint16_t tick; /* 2 */
1097 uint8_t type; /* 4 */
1098 uint8_t channels; /* 5 number of reported sats */
1100 struct ao_telemetry_satellite_info sats[12]; /* 6 */
1101 uint8_t unused[2]; /* 30 */
1105 #define AO_TELEMETRY_COMPANION 0x07
1107 #define AO_COMPANION_MAX_CHANNELS 12
1109 struct ao_telemetry_companion {
1110 uint16_t serial; /* 0 */
1111 uint16_t tick; /* 2 */
1112 uint8_t type; /* 4 */
1113 uint8_t board_id; /* 5 */
1115 uint8_t update_period; /* 6 */
1116 uint8_t channels; /* 7 */
1117 uint16_t companion_data[AO_COMPANION_MAX_CHANNELS]; /* 8 */
1121 union ao_telemetry_all {
1122 struct ao_telemetry_generic generic;
1123 struct ao_telemetry_sensor sensor;
1124 struct ao_telemetry_configuration configuration;
1125 struct ao_telemetry_location location;
1126 struct ao_telemetry_satellite satellite;
1127 struct ao_telemetry_companion companion;
1134 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
1135 #define AO_GPS_NUM_SAT_SHIFT (0)
1137 #define AO_GPS_VALID (1 << 4)
1138 #define AO_GPS_RUNNING (1 << 5)
1139 #define AO_GPS_DATE_VALID (1 << 6)
1140 #define AO_GPS_COURSE_VALID (1 << 7)
1142 extern __pdata uint16_t ao_gps_tick;
1143 extern __xdata uint8_t ao_gps_mutex;
1144 extern __xdata struct ao_telemetry_location ao_gps_data;
1145 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
1147 struct ao_gps_orig {
1155 int32_t latitude; /* degrees * 10⁷ */
1156 int32_t longitude; /* degrees * 10⁷ */
1157 int16_t altitude; /* m */
1158 uint16_t ground_speed; /* cm/s */
1159 uint8_t course; /* degrees / 2 */
1160 uint8_t hdop; /* * 5 */
1161 int16_t climb_rate; /* cm/s */
1162 uint16_t h_error; /* m */
1163 uint16_t v_error; /* m */
1166 struct ao_gps_sat_orig {
1171 #define AO_MAX_GPS_TRACKING 12
1173 struct ao_gps_tracking_orig {
1175 struct ao_gps_sat_orig sats[AO_MAX_GPS_TRACKING];
1182 ao_gps_print(__xdata struct ao_gps_orig *gps_data);
1185 ao_gps_tracking_print(__xdata struct ao_gps_tracking_orig *gps_tracking_data);
1195 ao_gps_report(void);
1198 ao_gps_report_init(void);
1201 * ao_telemetry_orig.c
1204 struct ao_telemetry_orig {
1207 uint8_t flight_state;
1209 int16_t ground_accel;
1218 int16_t ground_pres;
1219 int16_t accel_plus_g;
1220 int16_t accel_minus_g;
1222 struct ao_gps_orig gps;
1223 char callsign[AO_MAX_CALLSIGN];
1224 struct ao_gps_tracking_orig gps_tracking;
1227 struct ao_telemetry_tiny {
1230 uint8_t flight_state;
1231 int16_t height; /* AGL in meters */
1232 int16_t speed; /* in m/s * 16 */
1233 int16_t accel; /* in m/s² * 16 */
1234 int16_t ground_pres; /* sensor units */
1235 struct ao_adc adc; /* raw ADC readings */
1236 char callsign[AO_MAX_CALLSIGN];
1239 struct ao_telemetry_orig_recv {
1240 struct ao_telemetry_orig telemetry_orig;
1245 struct ao_telemetry_tiny_recv {
1246 struct ao_telemetry_tiny telemetry_tiny;
1252 * ao_radio_recv tacks on rssi and status bytes
1255 struct ao_telemetry_raw_recv {
1256 uint8_t packet[AO_MAX_TELEMETRY + 2];
1259 /* Set delay between telemetry reports (0 to disable) */
1261 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(1000)
1262 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(100)
1263 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(1000)
1266 ao_telemetry_set_interval(uint16_t interval);
1269 ao_rdf_set(uint8_t rdf);
1272 ao_telemetry_init(void);
1275 ao_telemetry_orig_init(void);
1278 ao_telemetry_tiny_init(void);
1284 extern __xdata uint8_t ao_radio_dma;
1285 extern __xdata uint8_t ao_radio_dma_done;
1286 extern __xdata uint8_t ao_radio_done;
1287 extern __xdata uint8_t ao_radio_mutex;
1290 ao_radio_general_isr(void) ao_arch_interrupt(16);
1293 ao_radio_get(uint8_t len);
1295 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
1298 ao_radio_set_packet(void);
1301 ao_radio_send(__xdata void *data, uint8_t size) __reentrant;
1304 ao_radio_recv(__xdata void *data, uint8_t size) __reentrant;
1307 ao_radio_recv_abort(void);
1310 ao_radio_rdf(int ms);
1313 ao_radio_rdf_abort(void);
1316 ao_radio_idle(void);
1319 ao_radio_init(void);
1325 extern const char const * const ao_state_names[];
1330 #define AO_MONITORING_OFF 0
1331 #define AO_MONITORING_ORIG 1
1332 #define AO_MONITORING_TINY 2
1335 ao_set_monitor(uint8_t monitoring);
1338 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
1344 #define AO_READ_AGAIN ((char) -1)
1347 char (*pollchar)(void);
1348 void (*putchar)(char c) __reentrant;
1349 void (*flush)(void);
1353 extern __xdata struct ao_stdio ao_stdios[];
1354 extern __pdata int8_t ao_cur_stdio;
1355 extern __pdata int8_t ao_num_stdios;
1360 extern __xdata uint8_t ao_stdin_ready;
1366 ao_add_stdio(char (*pollchar)(void),
1367 void (*putchar)(char) __reentrant,
1368 void (*flush)(void)) __reentrant;
1375 ao_igniter_drogue = 0,
1380 ao_ignite(enum ao_igniter igniter);
1382 enum ao_igniter_status {
1383 ao_igniter_unknown, /* unknown status (ambiguous voltage) */
1384 ao_igniter_ready, /* continuity detected */
1385 ao_igniter_active, /* igniter firing */
1386 ao_igniter_open, /* open circuit detected */
1389 enum ao_igniter_status
1390 ao_igniter_status(enum ao_igniter igniter);
1393 ao_ignite_set_pins(void);
1396 ao_igniter_init(void);
1402 #define AO_CONFIG_MAJOR 1
1403 #define AO_CONFIG_MINOR 8
1408 uint16_t main_deploy;
1409 int16_t accel_plus_g; /* changed for minor version 2 */
1410 uint8_t radio_channel;
1411 char callsign[AO_MAX_CALLSIGN + 1];
1412 uint8_t apogee_delay; /* minor version 1 */
1413 int16_t accel_minus_g; /* minor version 2 */
1414 uint32_t radio_cal; /* minor version 3 */
1415 uint32_t flight_log_max; /* minor version 4 */
1416 uint8_t ignite_mode; /* minor version 5 */
1417 uint8_t pad_orientation; /* minor version 6 */
1418 uint32_t radio_setting; /* minor version 7 */
1419 uint8_t radio_enable; /* minor version 8 */
1422 #define AO_IGNITE_MODE_DUAL 0
1423 #define AO_IGNITE_MODE_APOGEE 1
1424 #define AO_IGNITE_MODE_MAIN 2
1426 #define AO_PAD_ORIENTATION_ANTENNA_UP 0
1427 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
1429 extern __xdata struct ao_config ao_config;
1431 #define AO_CONFIG_MAX_SIZE 128
1434 ao_config_get(void);
1437 ao_config_put(void);
1440 ao_config_init(void);
1447 ao_rssi_set(int rssi_value);
1450 ao_rssi_init(uint8_t rssi_led);
1455 * values which need to be defined for
1456 * each instance of a product
1459 extern const char ao_version[];
1460 extern const char ao_manufacturer[];
1461 extern const char ao_product[];
1467 #define AO_FIFO_SIZE 32
1472 char fifo[AO_FIFO_SIZE];
1475 #define ao_fifo_insert(f,c) do { \
1476 (f).fifo[(f).insert] = (c); \
1477 (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1480 #define ao_fifo_remove(f,c) do {\
1481 c = (f).fifo[(f).remove]; \
1482 (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1485 #define ao_fifo_full(f) ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1486 #define ao_fifo_empty(f) ((f).insert == (f).remove)
1491 * Packet-based command interface
1494 #define AO_PACKET_MAX 64
1495 #define AO_PACKET_SYN (uint8_t) 0xff
1502 uint8_t d[AO_PACKET_MAX];
1503 uint8_t callsign[AO_MAX_CALLSIGN];
1506 struct ao_packet_recv {
1507 struct ao_packet packet;
1512 extern __xdata struct ao_packet_recv ao_rx_packet;
1513 extern __xdata struct ao_packet ao_tx_packet;
1514 extern __xdata struct ao_task ao_packet_task;
1515 extern __xdata uint8_t ao_packet_enable;
1516 extern __xdata uint8_t ao_packet_master_sleeping;
1517 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1520 ao_packet_send(void);
1523 ao_packet_recv(void);
1526 ao_packet_flush(void);
1529 ao_packet_putchar(char c) __reentrant;
1532 ao_packet_pollchar(void) __critical;
1534 /* ao_packet_master.c */
1537 ao_packet_master_init(void);
1539 /* ao_packet_slave.c */
1542 ao_packet_slave_start(void);
1545 ao_packet_slave_stop(void);
1548 ao_packet_slave_init(uint8_t enable);
1552 /* If bt_link is on P2, this interrupt is shared by USB, so the USB
1553 * code calls this function. Otherwise, it's a regular ISR.
1566 /* ao_companion.c */
1568 #define AO_COMPANION_SETUP 1
1569 #define AO_COMPANION_FETCH 2
1570 #define AO_COMPANION_NOTIFY 3
1572 struct ao_companion_command {
1574 uint8_t flight_state;
1580 struct ao_companion_setup {
1582 uint16_t board_id_inverse;
1583 uint8_t update_period;
1587 extern __pdata uint8_t ao_companion_running;
1588 extern __xdata uint8_t ao_companion_mutex;
1589 extern __xdata struct ao_companion_command ao_companion_command;
1590 extern __xdata struct ao_companion_setup ao_companion_setup;
1591 extern __xdata uint16_t ao_companion_data[AO_COMPANION_MAX_CHANNELS];
1594 ao_companion_init(void);