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.
35 /* Convert a __data pointer into an __xdata pointer */
36 #define DATA_TO_XDATA(a) ((void __xdata *) ((uint8_t) (a) | 0xff00))
38 /* Stack runs from above the allocated __data space to 0xfe, which avoids
39 * writing to 0xff as that triggers the stack overflow indicator
42 #define AO_STACK_SIZE 128
44 #define AO_STACK_START 0x90
45 #define AO_STACK_END 0xfe
46 #define AO_STACK_SIZE (AO_STACK_END - AO_STACK_START + 1)
51 __xdata void *wchan; /* current wait channel (NULL if running) */
52 uint16_t alarm; /* abort ao_sleep time */
54 uint8_t *sp; /* saved stack pointer */
56 uint8_t stack_count; /* amount of saved stack */
58 uint8_t task_id; /* unique id */
59 __code char *name; /* task name */
60 uint8_t stack[AO_STACK_SIZE]; /* saved stack */
63 extern __xdata struct ao_task *__data ao_cur_task;
65 #define AO_NUM_TASKS 16 /* maximum number of tasks */
66 #define AO_NO_TASK 0 /* no task id */
69 extern uint8_t ao_cpu_sleep_disable;
76 /* Suspend the current task until wchan is awoken.
82 ao_sleep(__xdata void *wchan);
84 /* Wake all tasks sleeping on wchan */
86 ao_wakeup(__xdata void *wchan);
88 /* set an alarm to go off in 'delay' ticks */
90 ao_alarm(uint16_t delay);
92 /* Yield the processor to another task */
94 void ao_yield(void) __attribute__((naked));
97 ao_yield(void) __naked;
100 /* Add a task to the run queue */
102 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
104 /* Terminate the current task */
108 /* Dump task info to console */
112 /* Start the scheduler. This will not return */
114 ao_start_scheduler(void);
120 #define AO_PANIC_NO_TASK 1 /* AO_NUM_TASKS is not large enough */
121 #define AO_PANIC_DMA 2 /* Attempt to start DMA while active */
122 #define AO_PANIC_MUTEX 3 /* Mis-using mutex API */
123 #define AO_PANIC_EE 4 /* Mis-using eeprom API */
124 #define AO_PANIC_LOG 5 /* Failing to read/write log data */
125 #define AO_PANIC_CMD 6 /* Too many command sets registered */
126 #define AO_PANIC_STDIO 7 /* Too many stdio handlers registered */
127 #define AO_PANIC_REBOOT 8 /* Reboot failed */
128 #define AO_PANIC_FLASH 9 /* Invalid flash part (or wrong blocksize) */
129 #define AO_PANIC_USB 10 /* Trying to send USB packet while busy */
130 #define AO_PANIC_BT 11 /* Communications with bluetooth device failed */
132 /* Stop the operating system, beeping and blinking the reason */
134 ao_panic(uint8_t reason);
140 /* Our timer runs at 100Hz */
142 #define AO_MS_TO_TICKS(ms) ((ms) / (1000 / AO_HERTZ))
143 #define AO_SEC_TO_TICKS(s) ((s) * AO_HERTZ)
145 /* Returns the current time in ticks */
149 /* Suspend the current task until ticks time has passed */
151 ao_delay(uint16_t ticks);
153 /* Set the ADC interval */
155 ao_timer_set_adc_interval(uint8_t interval) __critical;
157 /* Timer interrupt */
159 ao_timer_isr(void) __interrupt(9);
161 /* Initialize the timer */
165 /* Initialize the hardware clock. Must be called first */
170 * One set of samples read from the A/D converter or telemetry
177 uint16_t tick; /* tick when the sample was read */
179 uint16_t adc[NUM_ADC]; /* samples */
181 int16_t accel; /* accelerometer */
182 int16_t pres; /* pressure sensor */
183 int16_t temp; /* temperature sensor */
184 int16_t v_batt; /* battery voltage */
185 int16_t sense_d; /* drogue continuity sense */
186 int16_t sense_m; /* main continuity sense */
193 #ifndef HAS_ACCEL_REF
194 #error Please define HAS_ACCEL_REF
197 #define HAS_ACCEL_REF 0
204 #define AO_ADC_RING 8
206 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
207 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
211 * A/D data is stored in a ring, with the next sample to be written
214 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
215 extern volatile __data uint8_t ao_adc_head;
217 extern volatile __xdata uint16_t ao_accel_ref[AO_ADC_RING];
220 /* Trigger a conversion sequence (called from the timer interrupt) */
224 /* Suspend the current task until another A/D sample is converted */
228 /* Get a copy of the last complete A/D sample set */
230 ao_adc_get(__xdata struct ao_adc *packet);
232 /* The A/D interrupt handler */
235 ao_adc_isr(void) __interrupt(1);
237 /* Initialize the A/D converter */
248 * Various pre-defined beep frequencies
250 * frequency = 1/2 (24e6/32) / beep
253 #define AO_BEEP_LOW 150 /* 2500Hz */
254 #define AO_BEEP_MID 94 /* 3989Hz */
255 #define AO_BEEP_HIGH 75 /* 5000Hz */
256 #define AO_BEEP_OFF 0 /* off */
258 #define AO_BEEP_g 240 /* 1562.5Hz */
259 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
260 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
261 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
262 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
263 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
264 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
265 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
266 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
267 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
268 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
269 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
270 #define AO_BEEP_gg 120 /* 3125Hz */
271 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
272 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
273 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
274 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
275 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
276 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
277 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
278 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
279 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
280 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
281 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
282 #define AO_BEEP_ggg 60 /* 6250Hz */
284 /* Set the beeper to the specified tone */
286 ao_beep(uint8_t beep);
288 /* Turn on the beeper for the specified time */
290 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
292 /* Initialize the beeper */
300 #define AO_LED_NONE 0
302 /* Turn on the specified LEDs */
304 ao_led_on(uint8_t colors);
306 /* Turn off the specified LEDs */
308 ao_led_off(uint8_t colors);
310 /* Set all of the LEDs to the specified state */
312 ao_led_set(uint8_t colors);
314 /* Toggle the specified LEDs */
316 ao_led_toggle(uint8_t colors);
318 /* Turn on the specified LEDs for the indicated interval */
320 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
322 /* Initialize the LEDs */
324 ao_led_init(uint8_t enable);
337 #define AO_ROMCONFIG_VERSION 2
339 struct ao_romconfig {
342 uint16_t serial_number;
347 extern __code struct ao_romconfig ao_romconfig __attribute__ ((section("romconfig")));
349 extern __code __at (0x00a0) struct ao_romconfig ao_romconfig;
354 extern const uint8_t ao_usb_descriptors [];
356 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
364 /* Put one character to the USB output queue */
366 ao_usb_putchar(char c);
368 /* Get one character from the USB input queue */
370 ao_usb_getchar(void);
372 /* Poll for a charcter on the USB input queue.
373 * returns AO_READ_AGAIN if none are available
376 ao_usb_pollchar(void);
378 /* Flush the USB output queue */
383 /* USB interrupt handler */
385 ao_usb_isr(void) __interrupt(6);
388 /* Enable the USB controller */
392 /* Disable the USB controller */
394 ao_usb_disable(void);
396 /* Initialize the USB system */
406 ao_cmd_lex_error = 1,
407 ao_cmd_syntax_error = 2,
410 extern __xdata uint16_t ao_cmd_lex_i;
411 extern __xdata uint32_t ao_cmd_lex_u32;
412 extern __xdata char ao_cmd_lex_c;
413 extern __xdata enum ao_cmd_status ao_cmd_status;
419 ao_cmd_put8(uint8_t v);
422 ao_cmd_put16(uint16_t v);
431 ao_cmd_decimal(void);
434 ao_match_word(__code char *word);
442 ao_cmd_register(__code struct ao_cmds *cmds);
449 * Provided by an external module to filter raw command lines
459 /* Allocate a DMA channel. the 'done' parameter will be set when the
460 * dma is finished and will be used to wakeup any waiters
464 ao_dma_alloc(__xdata uint8_t * done);
466 /* Setup a DMA channel */
468 ao_dma_set_transfer(uint8_t id,
469 void __xdata *srcaddr,
470 void __xdata *dstaddr,
475 /* Start a DMA channel */
477 ao_dma_start(uint8_t id);
479 /* Manually trigger a DMA channel */
481 ao_dma_trigger(uint8_t id);
483 /* Abort a running DMA transfer */
485 ao_dma_abort(uint8_t id);
487 /* DMA interrupt routine */
489 ao_dma_isr(void) __interrupt(8);
496 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
499 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
502 * Storage interface, provided by one of the eeprom or flash
506 /* Total bytes of available storage */
507 extern __xdata uint32_t ao_storage_total;
509 /* Block size - device is erased in these units. At least 256 bytes */
510 extern __xdata uint32_t ao_storage_block;
512 /* Byte offset of config block. Will be ao_storage_block bytes long */
513 extern __xdata uint32_t ao_storage_config;
515 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
516 extern __xdata uint16_t ao_storage_unit;
518 #define AO_STORAGE_ERASE_LOG (ao_storage_config + AO_CONFIG_MAX_SIZE)
520 /* Initialize above values. Can only be called once the OS is running */
522 ao_storage_setup(void) __reentrant;
524 /* Write data. Returns 0 on failure, 1 on success */
526 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
528 /* Read data. Returns 0 on failure, 1 on success */
530 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
532 /* Erase a block of storage. This always clears ao_storage_block bytes */
534 ao_storage_erase(uint32_t pos) __reentrant;
536 /* Flush any pending writes to stable storage */
538 ao_storage_flush(void) __reentrant;
540 /* Initialize the storage code */
542 ao_storage_init(void);
545 * Low-level functions wrapped by ao_storage.c
548 /* Read data within a storage unit */
550 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
552 /* Write data within a storage unit */
554 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant;
556 /* Initialize low-level device bits */
558 ao_storage_device_init(void);
560 /* Print out information about flash chips */
562 ao_storage_device_info(void) __reentrant;
568 /* We record flight numbers in the first record of
569 * the log. Tasks may wait for this to be initialized
570 * by sleeping on this variable.
572 extern __xdata uint16_t ao_flight_number;
574 extern __pdata uint32_t ao_log_current_pos;
575 extern __pdata uint32_t ao_log_end_pos;
576 extern __pdata uint32_t ao_log_start_pos;
577 extern __xdata uint8_t ao_log_running;
578 extern __xdata enum flight_state ao_log_state;
580 #define AO_LOG_TELESCIENCE_START ((uint8_t) 's')
581 #define AO_LOG_TELESCIENCE_DATA ((uint8_t) 'd')
583 struct ao_log_telescience {
590 uint16_t adc[NUM_ADC];
593 extern struct ao_log_telescience ao_log_store;
595 /* required functions from the underlying log system */
597 #define AO_LOG_FORMAT_UNKNOWN 0 /* unknown; altosui will have to guess */
598 #define AO_LOG_FORMAT_FULL 1 /* 8 byte typed log records */
599 #define AO_LOG_FORMAT_TINY 2 /* two byte state/baro records */
600 #define AO_LOG_FORMAT_TELEMETRY 3 /* 32 byte ao_telemetry records */
601 #define AO_LOG_FORMAT_TELESCIENCE 4 /* 32 byte typed telescience records */
602 #define AO_LOG_FORMAT_NONE 127 /* No log at all */
604 extern __code uint8_t ao_log_format;
606 /* Return the flight number from the given log slot, 0 if none */
608 ao_log_flight(uint8_t slot);
610 /* Flash has been erased, go find the start of storage */
612 ao_log_restart(void);
618 /* Logging thread main routine */
622 /* functions provided in ao_log.c */
624 /* Figure out the current flight number */
626 ao_log_scan(void) __reentrant;
628 /* Return the position of the start of the given log slot */
630 ao_log_pos(uint8_t slot);
632 /* Start logging to eeprom */
640 /* Initialize the logging system */
644 /* Write out the current flight number to the erase log */
646 ao_log_write_erase(uint8_t pos);
648 /* Returns true if there are any logs stored in eeprom */
650 ao_log_present(void);
652 /* Returns true if there is no more storage space available */
661 * The data log is recorded in the eeprom as a sequence
664 * Each packet starts with a 4-byte header that has the
665 * packet type, the packet checksum and the tick count. Then
666 * they all contain 2 16 bit values which hold packet-specific
669 * For each flight, the first packet
670 * is FLIGHT packet, indicating the serial number of the
671 * device and a unique number marking the number of flights
672 * recorded by this device.
674 * During flight, data from the accelerometer and barometer
675 * are recorded in SENSOR packets, using the raw 16-bit values
676 * read from the A/D converter.
678 * Also during flight, but at a lower rate, the deployment
679 * sensors are recorded in DEPLOY packets. The goal here is to
680 * detect failure in the deployment circuits.
682 * STATE packets hold state transitions as the flight computer
683 * transitions through different stages of the flight.
685 #define AO_LOG_FLIGHT 'F'
686 #define AO_LOG_SENSOR 'A'
687 #define AO_LOG_TEMP_VOLT 'T'
688 #define AO_LOG_DEPLOY 'D'
689 #define AO_LOG_STATE 'S'
690 #define AO_LOG_GPS_TIME 'G'
691 #define AO_LOG_GPS_LAT 'N'
692 #define AO_LOG_GPS_LON 'W'
693 #define AO_LOG_GPS_ALT 'H'
694 #define AO_LOG_GPS_SAT 'V'
695 #define AO_LOG_GPS_DATE 'Y'
697 #define AO_LOG_POS_NONE (~0UL)
699 struct ao_log_record {
705 int16_t ground_accel;
730 int32_t gps_latitude;
731 int32_t gps_longitude;
754 /* Write a record to the eeprom log */
756 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
762 enum ao_flight_state {
763 ao_flight_startup = 0,
769 ao_flight_drogue = 6,
771 ao_flight_landed = 8,
772 ao_flight_invalid = 9
775 extern __pdata enum ao_flight_state ao_flight_state;
777 extern __pdata uint16_t ao_launch_time;
778 extern __xdata uint8_t ao_flight_force_idle;
784 /* Initialize flight thread */
786 ao_flight_init(void);
793 ao_flight_nano_init(void);
800 * Barometer calibration
802 * We directly sample the barometer. The specs say:
804 * Pressure range: 15-115 kPa
805 * Voltage at 115kPa: 2.82
806 * Output scale: 27mV/kPa
808 * If we want to detect launch with the barometer, we need
809 * a large enough bump to not be fooled by noise. At typical
810 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
811 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
812 * As all of our calculations are done in 16 bits, we'll actually see a change
813 * of 16 times this though
815 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
818 /* Accelerometer calibration
820 * We're sampling the accelerometer through a resistor divider which
821 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
822 * That goes into the cc1111 A/D converter, which is running at 11 bits
823 * of precision with the bits in the MSB of the 16 bit value. Only positive
824 * values are used, so values should range from 0-32752 for 0-3.3V. The
825 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
826 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
827 * for a final computation of:
829 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
831 * Zero g was measured at 16000 (we would expect 16384).
832 * Note that this value is only require to tell if the
833 * rocket is standing upright. Once that is determined,
834 * the value of the accelerometer is averaged for 100 samples
835 * to find the resting accelerometer value, which is used
836 * for all further flight computations
839 #define GRAVITY 9.80665
842 * Above this height, the baro sensor doesn't work
844 #define AO_MAX_BARO_HEIGHT 12000
847 * Above this speed, baro measurements are unreliable
849 #define AO_MAX_BARO_SPEED 200
851 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
854 * Speed and acceleration are scaled by 16 to provide a bit more
855 * resolution while still having reasonable range. Note that this
856 * limits speed to 2047m/s (around mach 6) and acceleration to
857 * 2047m/s² (over 200g)
860 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
861 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
862 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
864 extern __pdata uint16_t ao_sample_tick; /* time of last data */
865 extern __pdata int16_t ao_sample_pres; /* most recent pressure sensor reading */
866 extern __pdata int16_t ao_sample_alt; /* MSL of ao_sample_pres */
867 extern __pdata int16_t ao_sample_height; /* AGL of ao_sample_pres */
868 extern __data uint8_t ao_sample_adc; /* Ring position of last processed sample */
871 extern __pdata int16_t ao_sample_accel; /* most recent accel sensor reading */
874 extern __xdata int16_t ao_ground_pres; /* startup pressure */
875 extern __xdata int16_t ao_ground_height; /* MSL of ao_ground_pres */
878 extern __xdata int16_t ao_ground_accel; /* startup acceleration */
879 extern __xdata int16_t ao_accel_2g; /* factory accel calibration */
880 extern __xdata int32_t ao_accel_scale; /* sensor to m/s² conversion */
883 void ao_sample_init(void);
885 /* returns FALSE in preflight mode, TRUE in flight mode */
886 uint8_t ao_sample(void);
892 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
893 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
894 #define from_fix(x) ((x) >> 16)
896 extern __pdata int16_t ao_height; /* meters */
897 extern __pdata int16_t ao_speed; /* m/s * 16 */
898 extern __pdata int16_t ao_accel; /* m/s² * 16 */
899 extern __pdata int16_t ao_max_height; /* max of ao_height */
901 extern __pdata int16_t ao_error_h;
902 extern __pdata int16_t ao_error_h_sq_avg;
905 extern __pdata int16_t ao_error_a;
908 void ao_kalman(void);
915 ao_report_init(void);
920 * Given raw data, convert to SI units
923 /* pressure from the sensor to altitude in meters */
925 ao_pres_to_altitude(int16_t pres) __reentrant;
928 ao_altitude_to_pres(int16_t alt) __reentrant;
931 ao_temp_to_dC(int16_t temp) __reentrant;
936 * debug another telemetrum board
939 /* Send a byte to the dbg target */
941 ao_dbg_send_byte(uint8_t byte);
943 /* Receive a byte from the dbg target */
945 ao_dbg_recv_byte(void);
947 /* Start a bulk transfer to/from dbg target memory */
949 ao_dbg_start_transfer(uint16_t addr);
951 /* End a bulk transfer to/from dbg target memory */
953 ao_dbg_end_transfer(void);
955 /* Write a byte to dbg target memory */
957 ao_dbg_write_byte(uint8_t byte);
959 /* Read a byte from dbg target memory */
961 ao_dbg_read_byte(void);
963 /* Enable dbg mode, switching use of the pins */
965 ao_dbg_debug_mode(void);
967 /* Reset the dbg target */
979 #ifndef USE_SERIAL_STDIN
980 #error Please define USE_SERIAL_STDIN
984 ao_serial_rx1_isr(void) __interrupt(3);
987 ao_serial_tx1_isr(void) __interrupt(14);
990 ao_serial_getchar(void) __critical;
994 ao_serial_pollchar(void) __critical;
997 ao_serial_set_stdin(uint8_t stdin);
1001 ao_serial_putchar(char c) __critical;
1004 ao_serial_drain(void) __critical;
1006 #define AO_SERIAL_SPEED_4800 0
1007 #define AO_SERIAL_SPEED_9600 1
1008 #define AO_SERIAL_SPEED_19200 2
1009 #define AO_SERIAL_SPEED_57600 3
1012 ao_serial_set_speed(uint8_t speed);
1015 ao_serial_init(void);
1023 ao_spi_send(void __xdata *block, uint16_t len) __reentrant;
1026 ao_spi_recv(void __xdata *block, uint16_t len) __reentrant;
1035 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
1036 #define AO_GPS_NUM_SAT_SHIFT (0)
1038 #define AO_GPS_VALID (1 << 4)
1039 #define AO_GPS_RUNNING (1 << 5)
1040 #define AO_GPS_DATE_VALID (1 << 6)
1041 #define AO_GPS_COURSE_VALID (1 << 7)
1043 extern __xdata uint16_t ao_gps_tick;
1045 struct ao_gps_data {
1053 int32_t latitude; /* degrees * 10⁷ */
1054 int32_t longitude; /* degrees * 10⁷ */
1055 int16_t altitude; /* m */
1056 uint16_t ground_speed; /* cm/s */
1057 uint8_t course; /* degrees / 2 */
1058 uint8_t hdop; /* * 5 */
1059 int16_t climb_rate; /* cm/s */
1060 uint16_t h_error; /* m */
1061 uint16_t v_error; /* m */
1064 struct ao_gps_sat_data {
1069 #define AO_MAX_GPS_TRACKING 12
1071 struct ao_gps_tracking_data {
1073 struct ao_gps_sat_data sats[AO_MAX_GPS_TRACKING];
1076 extern __xdata uint8_t ao_gps_mutex;
1077 extern __xdata struct ao_gps_data ao_gps_data;
1078 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
1084 ao_gps_print(__xdata struct ao_gps_data *gps_data);
1087 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
1097 ao_gps_report(void);
1100 ao_gps_report_init(void);
1106 #define AO_MAX_CALLSIGN 8
1108 struct ao_telemetry {
1111 uint8_t flight_state;
1113 int16_t ground_accel;
1122 int16_t ground_pres;
1123 int16_t accel_plus_g;
1124 int16_t accel_minus_g;
1126 struct ao_gps_data gps;
1127 char callsign[AO_MAX_CALLSIGN];
1128 struct ao_gps_tracking_data gps_tracking;
1131 struct ao_telemetry_tiny {
1134 uint8_t flight_state;
1135 int16_t height; /* AGL in meters */
1136 int16_t speed; /* in m/s * 16 */
1137 int16_t accel; /* in m/s² * 16 */
1138 int16_t ground_pres; /* sensor units */
1139 struct ao_adc adc; /* raw ADC readings */
1140 char callsign[AO_MAX_CALLSIGN];
1144 * ao_radio_recv tacks on rssi and status bytes
1146 struct ao_telemetry_recv {
1147 struct ao_telemetry telemetry;
1152 struct ao_telemetry_tiny_recv {
1153 struct ao_telemetry_tiny telemetry_tiny;
1158 /* Set delay between telemetry reports (0 to disable) */
1160 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(1000)
1161 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(100)
1162 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(1000)
1165 ao_telemetry_set_interval(uint16_t interval);
1168 ao_rdf_set(uint8_t rdf);
1171 ao_telemetry_init(void);
1174 ao_telemetry_tiny_init(void);
1180 extern __xdata uint8_t ao_radio_dma;
1181 extern __xdata uint8_t ao_radio_dma_done;
1182 extern __xdata uint8_t ao_radio_done;
1183 extern __xdata uint8_t ao_radio_mutex;
1186 ao_radio_general_isr(void) __interrupt(16);
1189 ao_radio_get(uint8_t len);
1191 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
1194 ao_radio_set_packet(void);
1197 ao_radio_send(__xdata void *data, uint8_t size) __reentrant;
1200 ao_radio_recv(__xdata void *data, uint8_t size) __reentrant;
1203 ao_radio_recv_abort(void);
1206 ao_radio_rdf(int ms);
1209 ao_radio_rdf_abort(void);
1212 ao_radio_idle(void);
1215 ao_radio_init(void);
1221 extern const char const * const ao_state_names[];
1226 #define AO_MONITORING_OFF 0
1227 #define AO_MONITORING_FULL 1
1228 #define AO_MONITORING_TINY 2
1231 ao_set_monitor(uint8_t monitoring);
1234 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
1240 #define AO_READ_AGAIN ((char) -1)
1243 char (*pollchar)(void);
1244 void (*putchar)(char c) __reentrant;
1245 void (*flush)(void);
1249 extern __xdata struct ao_stdio ao_stdios[];
1250 extern __data int8_t ao_cur_stdio;
1251 extern __data int8_t ao_num_stdios;
1256 extern __xdata uint8_t ao_stdin_ready;
1262 ao_add_stdio(char (*pollchar)(void),
1263 void (*putchar)(char) __reentrant,
1264 void (*flush)(void)) __reentrant;
1268 ao_stdio_init(void);
1270 #define ao_stdio_init()
1278 ao_igniter_drogue = 0,
1283 ao_ignite(enum ao_igniter igniter);
1285 enum ao_igniter_status {
1286 ao_igniter_unknown, /* unknown status (ambiguous voltage) */
1287 ao_igniter_ready, /* continuity detected */
1288 ao_igniter_active, /* igniter firing */
1289 ao_igniter_open, /* open circuit detected */
1292 enum ao_igniter_status
1293 ao_igniter_status(enum ao_igniter igniter);
1296 ao_igniter_init(void);
1302 #define AO_CONFIG_MAJOR 1
1303 #define AO_CONFIG_MINOR 4
1308 uint16_t main_deploy;
1309 int16_t accel_plus_g; /* changed for minor version 2 */
1310 uint8_t radio_channel;
1311 char callsign[AO_MAX_CALLSIGN + 1];
1312 uint8_t apogee_delay; /* minor version 1 */
1313 int16_t accel_minus_g; /* minor version 2 */
1314 uint32_t radio_cal; /* minor version 3 */
1315 uint32_t flight_log_max; /* minor version 4 */
1318 extern __xdata struct ao_config ao_config;
1320 #define AO_CONFIG_MAX_SIZE 128
1323 ao_config_get(void);
1326 ao_config_put(void);
1329 ao_config_init(void);
1336 ao_rssi_set(int rssi_value);
1339 ao_rssi_init(uint8_t rssi_led);
1344 * values which need to be defined for
1345 * each instance of a product
1348 extern const char ao_version[];
1349 extern const char ao_manufacturer[];
1350 extern const char ao_product[];
1351 extern const int ao_serial_number;
1357 #define AO_FIFO_SIZE 32
1362 char fifo[AO_FIFO_SIZE];
1365 #define ao_fifo_insert(f,c) do { \
1366 (f).fifo[(f).insert] = (c); \
1367 (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1370 #define ao_fifo_remove(f,c) do {\
1371 c = (f).fifo[(f).remove]; \
1372 (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1375 #define ao_fifo_reset(f) ((f).insert = (f).remove = 0)
1376 #define ao_fifo_full(f) ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1377 #define ao_fifo_empty(f) ((f).insert == (f).remove)
1382 * Packet-based command interface
1385 #define AO_PACKET_MAX 64
1386 #define AO_PACKET_SYN (uint8_t) 0xff
1393 uint8_t d[AO_PACKET_MAX];
1394 uint8_t callsign[AO_MAX_CALLSIGN];
1397 struct ao_packet_recv {
1398 struct ao_packet packet;
1403 extern __xdata struct ao_packet_recv ao_rx_packet;
1404 extern __xdata struct ao_packet ao_tx_packet;
1405 extern __xdata struct ao_task ao_packet_task;
1406 extern __xdata uint8_t ao_packet_enable;
1407 extern __xdata uint8_t ao_packet_master_sleeping;
1408 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1411 ao_packet_send(void);
1414 ao_packet_recv(void);
1417 ao_packet_flush(void);
1420 ao_packet_putchar(char c) __reentrant;
1423 ao_packet_pollchar(void) __critical;
1425 /* ao_packet_master.c */
1428 ao_packet_master_init(void);
1430 /* ao_packet_slave.c */
1433 ao_packet_slave_start(void);
1436 ao_packet_slave_stop(void);
1439 ao_packet_slave_init(uint8_t enable);
1443 /* Shared by USB, so the USB code calls this function */
1450 /* ao_debug_avr.c */
1452 ao_debug_init(void);
1454 /* ao_spi_slave.c */
1457 ao_spi_slave_read(uint8_t *data, int len);
1460 ao_spi_slave_write(uint8_t *data, int len);
1463 ao_spi_slave_debug(void);
1466 ao_spi_slave_init(void);
1468 /* ao_companion.c */
1470 #define AO_COMPANION_SETUP 1
1471 #define AO_COMPANION_FETCH 2
1472 #define AO_COMPANION_STATE 3
1474 struct ao_companion_command {
1476 uint8_t flight_state;
1482 extern struct ao_companion_command ao_companion_command;
1484 struct ao_companion_setup {
1486 uint16_t board_id_inverse;
1487 uint8_t update_period;