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.
30 /* Convert a __data pointer into an __xdata pointer */
31 #define DATA_TO_XDATA(a) ((void __xdata *) ((uint8_t) (a) | 0xff00))
33 /* Stack runs from above the allocated __data space to 0xfe, which avoids
34 * writing to 0xff as that triggers the stack overflow indicator
36 #define AO_STACK_START 0x80
37 #define AO_STACK_END 0xfe
38 #define AO_STACK_SIZE (AO_STACK_END - AO_STACK_START + 1)
42 __xdata void *wchan; /* current wait channel (NULL if running) */
43 uint8_t stack_count; /* amount of saved stack */
44 uint8_t task_id; /* index in the task array */
45 __code char *name; /* task name */
46 uint8_t stack[AO_STACK_SIZE]; /* saved stack */
49 extern __xdata struct ao_task *__data ao_cur_task;
51 #define AO_NUM_TASKS 16 /* maximum number of tasks */
52 #define AO_NO_TASK 0 /* no task id */
58 /* Suspend the current task until wchan is awoken */
60 ao_sleep(__xdata void *wchan);
62 /* Wake all tasks sleeping on wchan */
64 ao_wakeup(__xdata void *wchan);
66 /* Wake up a specific task */
68 ao_wake_task(__xdata struct ao_task *task);
70 /* Yield the processor to another task */
72 ao_yield(void) _naked;
74 /* Add a task to the run queue */
76 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
78 /* Terminate the current task */
82 /* Dump task info to console */
86 /* Start the scheduler. This will not return */
88 ao_start_scheduler(void);
94 #define AO_PANIC_NO_TASK 1 /* AO_NUM_TASKS is not large enough */
95 #define AO_PANIC_DMA 2 /* Attempt to start DMA while active */
96 #define AO_PANIC_MUTEX 3 /* Mis-using mutex API */
97 #define AO_PANIC_EE 4 /* Mis-using eeprom API */
98 #define AO_PANIC_LOG 5 /* Failing to read/write log data */
99 #define AO_PANIC_CMD 6 /* Too many command sets registered */
101 /* Stop the operating system, beeping and blinking the reason */
103 ao_panic(uint8_t reason);
109 /* Our timer runs at 100Hz */
110 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
111 #define AO_SEC_TO_TICKS(s) ((s) * 100)
113 /* Returns the current time in ticks */
117 /* Suspend the current task until ticks time has passed */
119 ao_delay(uint16_t ticks);
121 /* Set the ADC interval */
123 ao_timer_set_adc_interval(uint8_t interval) __critical;
125 /* Timer interrupt */
127 ao_timer_isr(void) interrupt 9;
129 /* Initialize the timer */
133 /* Initialize the hardware clock. Must be called first */
141 #define AO_ADC_RING 64
142 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
143 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
146 * One set of samples read from the A/D converter
149 uint16_t tick; /* tick when the sample was read */
150 int16_t accel; /* accelerometer */
151 int16_t pres; /* pressure sensor */
152 int16_t temp; /* temperature sensor */
153 int16_t v_batt; /* battery voltage */
154 int16_t sense_d; /* drogue continuity sense */
155 int16_t sense_m; /* main continuity sense */
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 /* Trigger a conversion sequence (called from the timer interrupt) */
169 /* Suspend the current task until another A/D sample is converted */
173 /* Get a copy of the last complete A/D sample set */
175 ao_adc_get(__xdata struct ao_adc *packet);
177 /* The A/D interrupt handler */
180 ao_adc_isr(void) interrupt 1;
183 /* Initialize the A/D converter */
192 * Various pre-defined beep frequencies
194 * frequency = 1/2 (24e6/32) / beep
197 #define AO_BEEP_LOW 150 /* 2500Hz */
198 #define AO_BEEP_MID 94 /* 3989Hz */
199 #define AO_BEEP_HIGH 75 /* 5000Hz */
200 #define AO_BEEP_OFF 0 /* off */
202 #define AO_BEEP_g 240 /* 1562.5Hz */
203 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
204 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
205 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
206 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
207 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
208 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
209 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
210 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
211 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
212 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
213 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
214 #define AO_BEEP_gg 120 /* 3125Hz */
215 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
216 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
217 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
218 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
219 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
220 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
221 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
222 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
223 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
224 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
225 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
226 #define AO_BEEP_ggg 60 /* 6250Hz */
228 /* Set the beeper to the specified tone */
230 ao_beep(uint8_t beep);
232 /* Turn on the beeper for the specified time */
234 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
236 /* Initialize the beeper */
244 #define AO_LED_NONE 0
245 #define AO_LED_GREEN 1
248 /* Turn on the specified LEDs */
250 ao_led_on(uint8_t colors);
252 /* Turn off the specified LEDs */
254 ao_led_off(uint8_t colors);
256 /* Set all of the LEDs to the specified state */
258 ao_led_set(uint8_t colors);
260 /* Toggle the specified LEDs */
262 ao_led_toggle(uint8_t colors);
264 /* Turn on the specified LEDs for the indicated interval */
266 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
268 /* Initialize the LEDs */
270 ao_led_init(uint8_t enable);
276 /* Put one character to the USB output queue */
278 ao_usb_putchar(char c);
280 /* Get one character from the USB input queue */
282 ao_usb_getchar(void);
284 /* Flush the USB output queue */
288 /* USB interrupt handler */
290 ao_usb_isr(void) interrupt 6;
292 /* Enable the USB controller */
296 /* Disable the USB controller */
298 ao_usb_disable(void);
300 /* Initialize the USB system */
310 ao_cmd_lex_error = 1,
311 ao_cmd_syntax_error = 2,
314 extern __xdata uint16_t ao_cmd_lex_i;
315 extern __xdata char ao_cmd_lex_c;
316 extern __xdata enum ao_cmd_status ao_cmd_status;
322 ao_cmd_put8(uint8_t v);
325 ao_cmd_put16(uint16_t v);
334 ao_cmd_decimal(void);
343 ao_cmd_register(__code struct ao_cmds *cmds);
352 /* Allocate a DMA channel. the 'done' parameter will be set to 1
353 * when the dma is finished and will be used to wakeup any waiters
356 ao_dma_alloc(__xdata uint8_t * done);
358 /* Setup a DMA channel */
360 ao_dma_set_transfer(uint8_t id,
361 void __xdata *srcaddr,
362 void __xdata *dstaddr,
367 /* Start a DMA channel */
369 ao_dma_start(uint8_t id);
371 /* Manually trigger a DMA channel */
373 ao_dma_trigger(uint8_t id);
375 /* Abort a running DMA transfer */
377 ao_dma_abort(uint8_t id);
379 /* DMA interrupt routine */
381 ao_dma_isr(void) interrupt 8;
388 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
391 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
398 * We reserve the last block on the device for
399 * configuration space. Writes and reads in this
400 * area return errors.
403 #define AO_EE_BLOCK_SIZE ((uint16_t) (256))
404 #define AO_EE_DEVICE_SIZE ((uint32_t) 128 * (uint32_t) 1024)
405 #define AO_EE_DATA_SIZE (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
406 #define AO_EE_CONFIG_BLOCK ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
409 ao_ee_flush(void) __reentrant;
411 /* Write to the eeprom */
413 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
415 /* Read from the eeprom */
417 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
419 /* Write the config block (at the end of the eeprom) */
421 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
423 /* Read the config block (at the end of the eeprom) */
425 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
427 /* Initialize the EEPROM code */
436 * The data log is recorded in the eeprom as a sequence
439 * Each packet starts with a 4-byte header that has the
440 * packet type, the packet checksum and the tick count. Then
441 * they all contain 2 16 bit values which hold packet-specific
444 * For each flight, the first packet
445 * is FLIGHT packet, indicating the serial number of the
446 * device and a unique number marking the number of flights
447 * recorded by this device.
449 * During flight, data from the accelerometer and barometer
450 * are recorded in SENSOR packets, using the raw 16-bit values
451 * read from the A/D converter.
453 * Also during flight, but at a lower rate, the deployment
454 * sensors are recorded in DEPLOY packets. The goal here is to
455 * detect failure in the deployment circuits.
457 * STATE packets hold state transitions as the flight computer
458 * transitions through different stages of the flight.
460 #define AO_LOG_FLIGHT 'F'
461 #define AO_LOG_SENSOR 'A'
462 #define AO_LOG_TEMP_VOLT 'T'
463 #define AO_LOG_DEPLOY 'D'
464 #define AO_LOG_STATE 'S'
465 #define AO_LOG_GPS_TIME 'G'
466 #define AO_LOG_GPS_LAT 'N'
467 #define AO_LOG_GPS_LON 'W'
468 #define AO_LOG_GPS_ALT 'H'
469 #define AO_LOG_GPS_SAT 'V'
471 #define AO_LOG_POS_NONE (~0UL)
473 struct ao_log_record {
479 int16_t ground_accel;
504 int32_t gps_latitude;
505 int32_t gps_longitude;
522 /* Write a record to the eeprom log */
524 ao_log_data(struct ao_log_record *log);
531 * ao_log_dump_first() - get first log record
532 * ao_log_dump_next() - get next log record
534 extern __xdata struct ao_log_record ao_log_dump;
536 /* Retrieve first log record for the current flight */
538 ao_log_dump_first(void);
540 /* return next log record for the current flight */
542 ao_log_dump_next(void);
544 /* Logging thread main routine */
548 /* Start logging to eeprom */
556 /* Initialize the logging system */
564 enum ao_flight_state {
565 ao_flight_startup = 0,
571 ao_flight_drogue = 6,
573 ao_flight_landed = 8,
574 ao_flight_invalid = 9
577 extern __xdata struct ao_adc ao_flight_data;
578 extern __pdata enum ao_flight_state ao_flight_state;
579 extern __pdata uint16_t ao_flight_tick;
580 extern __pdata int16_t ao_flight_accel;
581 extern __pdata int16_t ao_flight_pres;
582 extern __pdata int32_t ao_flight_vel;
583 extern __pdata int16_t ao_ground_pres;
584 extern __pdata int16_t ao_ground_accel;
585 extern __pdata int16_t ao_min_pres;
586 extern __pdata uint16_t ao_launch_time;
592 /* Initialize flight thread */
594 ao_flight_init(void);
601 ao_report_init(void);
606 * Given raw data, convert to SI units
609 /* pressure from the sensor to altitude in meters */
611 ao_pres_to_altitude(int16_t pres) __reentrant;
614 ao_altitude_to_pres(int16_t alt) __reentrant;
617 ao_temp_to_dC(int16_t temp) __reentrant;
622 * debug another telemetrum board
625 /* Send a byte to the dbg target */
627 ao_dbg_send_byte(uint8_t byte);
629 /* Receive a byte from the dbg target */
631 ao_dbg_recv_byte(void);
633 /* Start a bulk transfer to/from dbg target memory */
635 ao_dbg_start_transfer(uint16_t addr);
637 /* End a bulk transfer to/from dbg target memory */
639 ao_dbg_end_transfer(void);
641 /* Write a byte to dbg target memory */
643 ao_dbg_write_byte(uint8_t byte);
645 /* Read a byte from dbg target memory */
647 ao_dbg_read_byte(void);
649 /* Enable dbg mode, switching use of the pins */
651 ao_dbg_debug_mode(void);
653 /* Reset the dbg target */
664 #if !AO_NO_SERIAL_ISR
666 ao_serial_rx1_isr(void) interrupt 3;
669 ao_serial_tx1_isr(void) interrupt 14;
673 ao_serial_getchar(void) __critical;
676 ao_serial_putchar(char c) __critical;
678 #define AO_SERIAL_SPEED_4800 0
679 #define AO_SERIAL_SPEED_9600 1
680 #define AO_SERIAL_SPEED_57600 2
683 ao_serial_set_speed(uint8_t speed);
686 ao_serial_init(void);
692 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
693 #define AO_GPS_NUM_SAT_SHIFT (0)
695 #define AO_GPS_VALID (1 << 4)
696 #define AO_GPS_RUNNING (1 << 5)
703 int32_t latitude; /* degrees * 10⁷ */
704 int32_t longitude; /* degrees * 10⁷ */
705 int16_t altitude; /* m */
706 uint16_t ground_speed; /* cm/s */
707 uint8_t course; /* degrees / 2 */
708 uint8_t hdop; /* * 5 */
709 int16_t climb_rate; /* cm/s */
710 uint16_t h_error; /* m */
711 uint16_t v_error; /* m */
714 #define SIRF_SAT_STATE_ACQUIRED (1 << 0)
715 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID (1 << 1)
716 #define SIRF_SAT_BIT_SYNC_COMPLETE (1 << 2)
717 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE (1 << 3)
718 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE (1 << 4)
719 #define SIRF_SAT_CODE_LOCKED (1 << 5)
720 #define SIRF_SAT_ACQUISITION_FAILED (1 << 6)
721 #define SIRF_SAT_EPHEMERIS_AVAILABLE (1 << 7)
723 struct ao_gps_sat_data {
729 struct ao_gps_tracking_data {
731 struct ao_gps_sat_data sats[12];
734 extern __xdata uint8_t ao_gps_mutex;
735 extern __xdata struct ao_gps_data ao_gps_data;
736 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
742 ao_gps_print(__xdata struct ao_gps_data *gps_data);
745 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
758 ao_gps_report_init(void);
764 #define AO_MAX_CALLSIGN 8
766 struct ao_telemetry {
768 uint8_t flight_state;
769 int16_t flight_accel;
770 int16_t ground_accel;
775 struct ao_gps_data gps;
776 char callsign[AO_MAX_CALLSIGN];
777 struct ao_gps_tracking_data gps_tracking;
780 /* Set delay between telemetry reports (0 to disable) */
782 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(1000)
783 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(50)
784 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(1000)
787 ao_telemetry_set_interval(uint16_t interval);
790 ao_rdf_set(uint8_t rdf);
793 ao_telemetry_init(void);
800 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant;
802 struct ao_radio_recv {
803 struct ao_telemetry telemetry;
809 ao_radio_recv(__xdata struct ao_radio_recv *recv) __reentrant;
812 ao_radio_rdf(int ms);
815 ao_radio_rdf_abort(void);
824 extern const char const * const ao_state_names[];
830 ao_set_monitor(uint8_t monitoring);
833 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
847 ao_igniter_drogue = 0,
852 ao_ignite(enum ao_igniter igniter);
854 enum ao_igniter_status {
855 ao_igniter_unknown, /* unknown status (ambiguous voltage) */
856 ao_igniter_ready, /* continuity detected */
857 ao_igniter_active, /* igniter firing */
858 ao_igniter_open, /* open circuit detected */
861 enum ao_igniter_status
862 ao_igniter_status(enum ao_igniter igniter);
865 ao_igniter_init(void);
871 #define AO_CONFIG_MAJOR 1
872 #define AO_CONFIG_MINOR 1
877 uint16_t main_deploy;
878 int16_t accel_zero_g;
879 uint8_t radio_channel;
880 char callsign[AO_MAX_CALLSIGN + 1];
881 uint8_t apogee_delay;
884 extern __xdata struct ao_config ao_config;
890 ao_config_init(void);
897 ao_rssi_set(int rssi_value);
900 ao_rssi_init(uint8_t rssi_led);
905 * values which need to be defined for
906 * each instance of a product
909 extern const uint8_t ao_usb_descriptors [];
910 extern const uint16_t ao_serial_number;
911 extern const char ao_version[];
912 extern const char ao_manufacturer[];
913 extern const char ao_product[];
919 #define AO_FIFO_SIZE 32
924 char fifo[AO_FIFO_SIZE];
927 #define ao_fifo_insert(f,c) do { \
928 (f).fifo[(f).insert] = (c); \
929 (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
932 #define ao_fifo_remove(f,c) do {\
933 c = (f).fifo[(f).remove]; \
934 (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
937 #define ao_fifo_full(f) ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
938 #define ao_fifo_empty(f) ((f).insert == (f).remove)
943 * Packet-based command interface
946 #define AO_PACKET_MAX 32
947 #define AO_PACKET_WIN 256
949 #define AO_PACKET_FIN (1 << 0)
950 #define AO_PACKET_SYN (1 << 1)
951 #define AO_PACKET_RST (1 << 2)
952 #define AO_PACKET_ACK (1 << 3)
961 uint8_t d[AO_PACKET_MAX];
965 ao_packet_connect(uint8_t dest);
968 ao_packet_accept(void);
971 ao_packet_send(uint8_t *data, int len);
974 ao_packet_recv(uint8_t *data, int len);
977 ao_packet_init(void);