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 uint16_t alarm; /* abort ao_sleep time */
44 uint8_t stack_count; /* amount of saved stack */
45 uint8_t task_id; /* index in the task array */
46 __code char *name; /* task name */
47 uint8_t stack[AO_STACK_SIZE]; /* saved stack */
50 extern __xdata struct ao_task *__data ao_cur_task;
52 #define AO_NUM_TASKS 16 /* maximum number of tasks */
53 #define AO_NO_TASK 0 /* no task id */
59 /* Suspend the current task until wchan is awoken.
65 ao_sleep(__xdata void *wchan);
67 /* Wake all tasks sleeping on wchan */
69 ao_wakeup(__xdata void *wchan);
71 /* Wake up a specific task */
73 ao_wake_task(__xdata struct ao_task *task);
75 /* set an alarm to go off in 'delay' ticks */
77 ao_alarm(uint16_t delay);
79 /* Yield the processor to another task */
81 ao_yield(void) _naked;
83 /* Add a task to the run queue */
85 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
87 /* Terminate the current task */
91 /* Dump task info to console */
95 /* Start the scheduler. This will not return */
97 ao_start_scheduler(void);
103 #define AO_PANIC_NO_TASK 1 /* AO_NUM_TASKS is not large enough */
104 #define AO_PANIC_DMA 2 /* Attempt to start DMA while active */
105 #define AO_PANIC_MUTEX 3 /* Mis-using mutex API */
106 #define AO_PANIC_EE 4 /* Mis-using eeprom API */
107 #define AO_PANIC_LOG 5 /* Failing to read/write log data */
108 #define AO_PANIC_CMD 6 /* Too many command sets registered */
109 #define AO_PANIC_STDIO 7 /* Too many stdio handlers registered */
111 /* Stop the operating system, beeping and blinking the reason */
113 ao_panic(uint8_t reason);
119 /* Our timer runs at 100Hz */
121 #define AO_MS_TO_TICKS(ms) ((ms) / (1000 / AO_HERTZ))
122 #define AO_SEC_TO_TICKS(s) ((s) * AO_HERTZ)
124 /* Returns the current time in ticks */
128 /* Suspend the current task until ticks time has passed */
130 ao_delay(uint16_t ticks);
132 /* Set the ADC interval */
134 ao_timer_set_adc_interval(uint8_t interval) __critical;
136 /* Timer interrupt */
138 ao_timer_isr(void) interrupt 9;
140 /* Initialize the timer */
144 /* Initialize the hardware clock. Must be called first */
152 #define AO_ADC_RING 32
153 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
154 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
157 * One set of samples read from the A/D converter
160 uint16_t tick; /* tick when the sample was read */
161 int16_t accel; /* accelerometer */
162 int16_t pres; /* pressure sensor */
163 int16_t temp; /* temperature sensor */
164 int16_t v_batt; /* battery voltage */
165 int16_t sense_d; /* drogue continuity sense */
166 int16_t sense_m; /* main continuity sense */
170 * A/D data is stored in a ring, with the next sample to be written
173 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
174 extern volatile __data uint8_t ao_adc_head;
176 /* Trigger a conversion sequence (called from the timer interrupt) */
180 /* Suspend the current task until another A/D sample is converted */
184 /* Get a copy of the last complete A/D sample set */
186 ao_adc_get(__xdata struct ao_adc *packet);
188 /* The A/D interrupt handler */
191 ao_adc_isr(void) interrupt 1;
194 /* Initialize the A/D converter */
203 * Various pre-defined beep frequencies
205 * frequency = 1/2 (24e6/32) / beep
208 #define AO_BEEP_LOW 150 /* 2500Hz */
209 #define AO_BEEP_MID 94 /* 3989Hz */
210 #define AO_BEEP_HIGH 75 /* 5000Hz */
211 #define AO_BEEP_OFF 0 /* off */
213 #define AO_BEEP_g 240 /* 1562.5Hz */
214 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
215 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
216 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
217 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
218 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
219 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
220 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
221 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
222 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
223 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
224 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
225 #define AO_BEEP_gg 120 /* 3125Hz */
226 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
227 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
228 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
229 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
230 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
231 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
232 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
233 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
234 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
235 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
236 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
237 #define AO_BEEP_ggg 60 /* 6250Hz */
239 /* Set the beeper to the specified tone */
241 ao_beep(uint8_t beep);
243 /* Turn on the beeper for the specified time */
245 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
247 /* Initialize the beeper */
255 #define AO_LED_NONE 0
256 #define AO_LED_GREEN 1
259 /* Turn on the specified LEDs */
261 ao_led_on(uint8_t colors);
263 /* Turn off the specified LEDs */
265 ao_led_off(uint8_t colors);
267 /* Set all of the LEDs to the specified state */
269 ao_led_set(uint8_t colors);
271 /* Toggle the specified LEDs */
273 ao_led_toggle(uint8_t colors);
275 /* Turn on the specified LEDs for the indicated interval */
277 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
279 /* Initialize the LEDs */
281 ao_led_init(uint8_t enable);
287 /* Put one character to the USB output queue */
289 ao_usb_putchar(char c);
291 /* Get one character from the USB input queue */
293 ao_usb_getchar(void);
295 /* Poll for a charcter on the USB input queue.
296 * returns AO_READ_AGAIN if none are available
299 ao_usb_pollchar(void);
301 /* Flush the USB output queue */
305 /* USB interrupt handler */
307 ao_usb_isr(void) interrupt 6;
309 /* Enable the USB controller */
313 /* Disable the USB controller */
315 ao_usb_disable(void);
317 /* Initialize the USB system */
327 ao_cmd_lex_error = 1,
328 ao_cmd_syntax_error = 2,
331 extern __xdata uint16_t ao_cmd_lex_i;
332 extern __xdata char ao_cmd_lex_c;
333 extern __xdata enum ao_cmd_status ao_cmd_status;
339 ao_cmd_put8(uint8_t v);
342 ao_cmd_put16(uint16_t v);
351 ao_cmd_decimal(void);
360 ao_cmd_register(__code struct ao_cmds *cmds);
369 /* Allocate a DMA channel. the 'done' parameter will be set
370 * when the dma is finished or aborted and will be used to
374 #define AO_DMA_DONE 1
375 #define AO_DMA_ABORTED 2
378 ao_dma_alloc(__xdata uint8_t * done);
380 /* Setup a DMA channel */
382 ao_dma_set_transfer(uint8_t id,
383 void __xdata *srcaddr,
384 void __xdata *dstaddr,
389 /* Start a DMA channel */
391 ao_dma_start(uint8_t id);
393 /* Manually trigger a DMA channel */
395 ao_dma_trigger(uint8_t id);
397 /* Abort a running DMA transfer */
399 ao_dma_abort(uint8_t id);
401 /* DMA interrupt routine */
403 ao_dma_isr(void) interrupt 8;
410 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
413 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
420 * We reserve the last block on the device for
421 * configuration space. Writes and reads in this
422 * area return errors.
425 #define AO_EE_BLOCK_SIZE ((uint16_t) (256))
426 #define AO_EE_DEVICE_SIZE ((uint32_t) 128 * (uint32_t) 1024)
427 #define AO_EE_DATA_SIZE (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
428 #define AO_EE_CONFIG_BLOCK ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
431 ao_ee_flush(void) __reentrant;
433 /* Write to the eeprom */
435 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
437 /* Read from the eeprom */
439 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
441 /* Write the config block (at the end of the eeprom) */
443 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
445 /* Read the config block (at the end of the eeprom) */
447 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
449 /* Initialize the EEPROM code */
458 * The data log is recorded in the eeprom as a sequence
461 * Each packet starts with a 4-byte header that has the
462 * packet type, the packet checksum and the tick count. Then
463 * they all contain 2 16 bit values which hold packet-specific
466 * For each flight, the first packet
467 * is FLIGHT packet, indicating the serial number of the
468 * device and a unique number marking the number of flights
469 * recorded by this device.
471 * During flight, data from the accelerometer and barometer
472 * are recorded in SENSOR packets, using the raw 16-bit values
473 * read from the A/D converter.
475 * Also during flight, but at a lower rate, the deployment
476 * sensors are recorded in DEPLOY packets. The goal here is to
477 * detect failure in the deployment circuits.
479 * STATE packets hold state transitions as the flight computer
480 * transitions through different stages of the flight.
482 #define AO_LOG_FLIGHT 'F'
483 #define AO_LOG_SENSOR 'A'
484 #define AO_LOG_TEMP_VOLT 'T'
485 #define AO_LOG_DEPLOY 'D'
486 #define AO_LOG_STATE 'S'
487 #define AO_LOG_GPS_TIME 'G'
488 #define AO_LOG_GPS_LAT 'N'
489 #define AO_LOG_GPS_LON 'W'
490 #define AO_LOG_GPS_ALT 'H'
491 #define AO_LOG_GPS_SAT 'V'
493 #define AO_LOG_POS_NONE (~0UL)
495 struct ao_log_record {
501 int16_t ground_accel;
526 int32_t gps_latitude;
527 int32_t gps_longitude;
544 /* Write a record to the eeprom log */
546 ao_log_data(struct ao_log_record *log);
553 * ao_log_dump_first() - get first log record
554 * ao_log_dump_next() - get next log record
556 extern __xdata struct ao_log_record ao_log_dump;
558 /* Retrieve first log record for the current flight */
560 ao_log_dump_first(void);
562 /* return next log record for the current flight */
564 ao_log_dump_next(void);
566 /* Logging thread main routine */
570 /* Start logging to eeprom */
578 /* Initialize the logging system */
586 enum ao_flight_state {
587 ao_flight_startup = 0,
593 ao_flight_drogue = 6,
595 ao_flight_landed = 8,
596 ao_flight_invalid = 9
599 extern __xdata struct ao_adc ao_flight_data;
600 extern __pdata enum ao_flight_state ao_flight_state;
601 extern __pdata uint16_t ao_flight_tick;
602 extern __pdata int16_t ao_flight_accel;
603 extern __pdata int16_t ao_flight_pres;
604 extern __pdata int32_t ao_flight_vel;
605 extern __pdata int16_t ao_ground_pres;
606 extern __pdata int16_t ao_ground_accel;
607 extern __pdata int16_t ao_min_pres;
608 extern __pdata uint16_t ao_launch_time;
614 /* Initialize flight thread */
616 ao_flight_init(void);
623 ao_report_init(void);
628 * Given raw data, convert to SI units
631 /* pressure from the sensor to altitude in meters */
633 ao_pres_to_altitude(int16_t pres) __reentrant;
636 ao_altitude_to_pres(int16_t alt) __reentrant;
639 ao_temp_to_dC(int16_t temp) __reentrant;
644 * debug another telemetrum board
647 /* Send a byte to the dbg target */
649 ao_dbg_send_byte(uint8_t byte);
651 /* Receive a byte from the dbg target */
653 ao_dbg_recv_byte(void);
655 /* Start a bulk transfer to/from dbg target memory */
657 ao_dbg_start_transfer(uint16_t addr);
659 /* End a bulk transfer to/from dbg target memory */
661 ao_dbg_end_transfer(void);
663 /* Write a byte to dbg target memory */
665 ao_dbg_write_byte(uint8_t byte);
667 /* Read a byte from dbg target memory */
669 ao_dbg_read_byte(void);
671 /* Enable dbg mode, switching use of the pins */
673 ao_dbg_debug_mode(void);
675 /* Reset the dbg target */
686 #if !AO_NO_SERIAL_ISR
688 ao_serial_rx1_isr(void) interrupt 3;
691 ao_serial_tx1_isr(void) interrupt 14;
695 ao_serial_getchar(void) __critical;
698 ao_serial_putchar(char c) __critical;
700 #define AO_SERIAL_SPEED_4800 0
701 #define AO_SERIAL_SPEED_9600 1
702 #define AO_SERIAL_SPEED_57600 2
705 ao_serial_set_speed(uint8_t speed);
708 ao_serial_init(void);
714 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
715 #define AO_GPS_NUM_SAT_SHIFT (0)
717 #define AO_GPS_VALID (1 << 4)
718 #define AO_GPS_RUNNING (1 << 5)
725 int32_t latitude; /* degrees * 10⁷ */
726 int32_t longitude; /* degrees * 10⁷ */
727 int16_t altitude; /* m */
728 uint16_t ground_speed; /* cm/s */
729 uint8_t course; /* degrees / 2 */
730 uint8_t hdop; /* * 5 */
731 int16_t climb_rate; /* cm/s */
732 uint16_t h_error; /* m */
733 uint16_t v_error; /* m */
736 #define SIRF_SAT_STATE_ACQUIRED (1 << 0)
737 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID (1 << 1)
738 #define SIRF_SAT_BIT_SYNC_COMPLETE (1 << 2)
739 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE (1 << 3)
740 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE (1 << 4)
741 #define SIRF_SAT_CODE_LOCKED (1 << 5)
742 #define SIRF_SAT_ACQUISITION_FAILED (1 << 6)
743 #define SIRF_SAT_EPHEMERIS_AVAILABLE (1 << 7)
745 struct ao_gps_sat_data {
751 struct ao_gps_tracking_data {
753 struct ao_gps_sat_data sats[12];
756 extern __xdata uint8_t ao_gps_mutex;
757 extern __xdata struct ao_gps_data ao_gps_data;
758 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
764 ao_gps_print(__xdata struct ao_gps_data *gps_data);
767 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
780 ao_gps_report_init(void);
786 #define AO_MAX_CALLSIGN 8
787 #define AO_TELEMETRY_VERSION 1
789 struct ao_telemetry {
791 uint8_t flight_state;
792 int16_t flight_accel;
793 int16_t ground_accel;
797 int16_t accel_plus_g;
798 int16_t accel_minus_g;
800 struct ao_gps_data gps;
801 char callsign[AO_MAX_CALLSIGN];
802 struct ao_gps_tracking_data gps_tracking;
805 /* Set delay between telemetry reports (0 to disable) */
807 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(1000)
808 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(50)
809 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(1000)
812 ao_telemetry_set_interval(uint16_t interval);
815 ao_rdf_set(uint8_t rdf);
818 ao_telemetry_init(void);
824 extern __xdata uint8_t ao_radio_dma;
825 extern __xdata uint8_t ao_radio_dma_done;
826 extern __xdata uint8_t ao_radio_done;
827 extern __xdata uint8_t ao_radio_mutex;
830 ao_radio_general_isr(void) interrupt 16;
833 ao_radio_set_telemetry(void);
836 ao_radio_set_packet(void);
839 ao_radio_set_rdf(void);
842 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant;
844 struct ao_radio_recv {
845 struct ao_telemetry telemetry;
851 ao_radio_recv(__xdata struct ao_radio_recv *recv) __reentrant;
854 ao_radio_rdf(int ms);
857 ao_radio_abort(void);
860 ao_radio_rdf_abort(void);
872 extern const char const * const ao_state_names[];
878 ao_set_monitor(uint8_t monitoring);
881 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
887 #define AO_READ_AGAIN ((char) -1)
890 char (*pollchar)(void);
891 void (*putchar)(char c) __reentrant;
898 extern __xdata uint8_t ao_stdin_ready;
901 ao_add_stdio(char (*pollchar)(void),
902 void (*putchar)(char) __reentrant,
903 void (*flush)(void));
910 ao_igniter_drogue = 0,
915 ao_ignite(enum ao_igniter igniter);
917 enum ao_igniter_status {
918 ao_igniter_unknown, /* unknown status (ambiguous voltage) */
919 ao_igniter_ready, /* continuity detected */
920 ao_igniter_active, /* igniter firing */
921 ao_igniter_open, /* open circuit detected */
924 enum ao_igniter_status
925 ao_igniter_status(enum ao_igniter igniter);
928 ao_igniter_init(void);
934 #define AO_CONFIG_MAJOR 1
935 #define AO_CONFIG_MINOR 2
940 uint16_t main_deploy;
941 int16_t accel_plus_g;
942 uint8_t radio_channel;
943 char callsign[AO_MAX_CALLSIGN + 1];
944 uint8_t apogee_delay;
945 int16_t accel_minus_g;
948 extern __xdata struct ao_config ao_config;
954 ao_config_init(void);
961 ao_rssi_set(int rssi_value);
964 ao_rssi_init(uint8_t rssi_led);
969 * values which need to be defined for
970 * each instance of a product
973 extern const uint8_t ao_usb_descriptors [];
974 extern const uint16_t ao_serial_number;
975 extern const char ao_version[];
976 extern const char ao_manufacturer[];
977 extern const char ao_product[];
983 #define AO_FIFO_SIZE 32
988 char fifo[AO_FIFO_SIZE];
991 #define ao_fifo_insert(f,c) do { \
992 (f).fifo[(f).insert] = (c); \
993 (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
996 #define ao_fifo_remove(f,c) do {\
997 c = (f).fifo[(f).remove]; \
998 (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1001 #define ao_fifo_full(f) ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1002 #define ao_fifo_empty(f) ((f).insert == (f).remove)
1007 * Packet-based command interface
1010 #define AO_PACKET_MAX 8
1011 #define AO_PACKET_SYN (uint8_t) 0xff
1018 uint8_t d[AO_PACKET_MAX];
1021 struct ao_packet_recv {
1022 struct ao_packet packet;
1027 extern __xdata struct ao_packet_recv ao_rx_packet;
1028 extern __xdata struct ao_packet ao_tx_packet;
1029 extern __xdata struct ao_task ao_packet_task;
1030 extern __xdata uint8_t ao_packet_enable;
1031 extern __xdata uint8_t ao_packet_master_sleeping;
1032 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1035 ao_packet_send(void);
1038 ao_packet_recv(void);
1041 ao_packet_flush(void);
1044 ao_packet_putchar(char c) __reentrant;
1047 ao_packet_pollchar(void) __critical;
1049 /* ao_packet_master.c */
1052 ao_packet_master_init(void);
1054 /* ao_packet_slave.c */
1057 ao_packet_slave_start(void);
1060 ao_packet_slave_stop(void);
1063 ao_packet_slave_init(void);