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 */
32 #define DATA_TO_XDATA(a) ((void __xdata *) ((uint8_t) (a) | 0xff00))
34 /* Convert a __code pointer into an __xdata pointer */
35 #define CODE_TO_XDATA(a) ((void __xdata *) ((uint16_t) (a)))
37 /* Stack runs from above the allocated __data space to 0xfe, which avoids
38 * writing to 0xff as that triggers the stack overflow indicator
40 #define AO_STACK_START 0x80
41 #define AO_STACK_END 0xfe
42 #define AO_STACK_SIZE (AO_STACK_END - AO_STACK_START + 1)
46 __xdata void *wchan; /* current wait channel (NULL if running) */
47 uint16_t alarm; /* abort ao_sleep time */
48 uint8_t stack_count; /* amount of saved stack */
49 uint8_t task_id; /* unique id */
50 __code char *name; /* task name */
51 uint8_t stack[AO_STACK_SIZE]; /* saved stack */
54 extern __xdata struct ao_task *__data ao_cur_task;
56 #define AO_NUM_TASKS 16 /* maximum number of tasks */
57 #define AO_NO_TASK 0 /* no task id */
63 /* Suspend the current task until wchan is awoken.
69 ao_sleep(__xdata void *wchan);
71 /* Wake all tasks sleeping on wchan */
73 ao_wakeup(__xdata void *wchan);
75 /* Wake up a specific task */
77 ao_wake_task(__xdata struct ao_task *task);
79 /* set an alarm to go off in 'delay' ticks */
81 ao_alarm(uint16_t delay);
83 /* Yield the processor to another task */
85 ao_yield(void) __naked;
87 /* Add a task to the run queue */
89 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
91 /* Terminate the current task */
95 /* Dump task info to console */
99 /* Start the scheduler. This will not return */
101 ao_start_scheduler(void);
107 #define AO_PANIC_NO_TASK 1 /* AO_NUM_TASKS is not large enough */
108 #define AO_PANIC_DMA 2 /* Attempt to start DMA while active */
109 #define AO_PANIC_MUTEX 3 /* Mis-using mutex API */
110 #define AO_PANIC_EE 4 /* Mis-using eeprom API */
111 #define AO_PANIC_LOG 5 /* Failing to read/write log data */
112 #define AO_PANIC_CMD 6 /* Too many command sets registered */
113 #define AO_PANIC_STDIO 7 /* Too many stdio handlers registered */
114 #define AO_PANIC_REBOOT 8 /* Reboot failed */
115 #define AO_PANIC_FLASH 9 /* Invalid flash part (or wrong blocksize) */
116 #define AO_PANIC_USB 10 /* Trying to send USB packet while busy */
118 /* Stop the operating system, beeping and blinking the reason */
120 ao_panic(uint8_t reason);
126 /* Our timer runs at 100Hz */
128 #define AO_MS_TO_TICKS(ms) ((ms) / (1000 / AO_HERTZ))
129 #define AO_SEC_TO_TICKS(s) ((s) * AO_HERTZ)
131 /* Returns the current time in ticks */
135 /* Suspend the current task until ticks time has passed */
137 ao_delay(uint16_t ticks);
139 /* Set the ADC interval */
141 ao_timer_set_adc_interval(uint8_t interval) __critical;
143 /* Timer interrupt */
145 ao_timer_isr(void) __interrupt 9;
147 /* Initialize the timer */
151 /* Initialize the hardware clock. Must be called first */
156 * One set of samples read from the A/D converter or telemetry
159 uint16_t tick; /* tick when the sample was read */
160 int16_t accel; /* accelerometer */
161 int16_t pres; /* pressure sensor */
162 int16_t temp; /* temperature sensor */
163 int16_t v_batt; /* battery voltage */
164 int16_t sense_d; /* drogue continuity sense */
165 int16_t sense_m; /* main continuity sense */
169 #error Please define HAS_ADC
177 #define AO_ADC_RING 32
178 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
179 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
183 * A/D data is stored in a ring, with the next sample to be written
186 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
187 extern volatile __data uint8_t ao_adc_head;
189 /* Trigger a conversion sequence (called from the timer interrupt) */
193 /* Suspend the current task until another A/D sample is converted */
197 /* Get a copy of the last complete A/D sample set */
199 ao_adc_get(__xdata struct ao_adc *packet);
201 /* The A/D interrupt handler */
204 ao_adc_isr(void) __interrupt 1;
206 /* Initialize the A/D converter */
217 * Various pre-defined beep frequencies
219 * frequency = 1/2 (24e6/32) / beep
222 #define AO_BEEP_LOW 150 /* 2500Hz */
223 #define AO_BEEP_MID 94 /* 3989Hz */
224 #define AO_BEEP_HIGH 75 /* 5000Hz */
225 #define AO_BEEP_OFF 0 /* off */
227 #define AO_BEEP_g 240 /* 1562.5Hz */
228 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
229 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
230 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
231 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
232 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
233 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
234 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
235 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
236 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
237 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
238 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
239 #define AO_BEEP_gg 120 /* 3125Hz */
240 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
241 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
242 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
243 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
244 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
245 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
246 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
247 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
248 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
249 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
250 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
251 #define AO_BEEP_ggg 60 /* 6250Hz */
253 /* Set the beeper to the specified tone */
255 ao_beep(uint8_t beep);
257 /* Turn on the beeper for the specified time */
259 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
261 /* Initialize the beeper */
269 #define AO_LED_NONE 0
271 /* Turn on the specified LEDs */
273 ao_led_on(uint8_t colors);
275 /* Turn off the specified LEDs */
277 ao_led_off(uint8_t colors);
279 /* Set all of the LEDs to the specified state */
281 ao_led_set(uint8_t colors);
283 /* Toggle the specified LEDs */
285 ao_led_toggle(uint8_t colors);
287 /* Turn on the specified LEDs for the indicated interval */
289 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
291 /* Initialize the LEDs */
293 ao_led_init(uint8_t enable);
299 #define AO_ROMCONFIG_VERSION 2
301 extern __code __at (0x00a0) uint16_t ao_romconfig_version;
302 extern __code __at (0x00a2) uint16_t ao_romconfig_check;
303 extern __code __at (0x00a4) uint16_t ao_serial_number;
304 extern __code __at (0x00a6) uint32_t ao_radio_cal;
305 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
311 /* Put one character to the USB output queue */
313 ao_usb_putchar(char c);
315 /* Get one character from the USB input queue */
317 ao_usb_getchar(void);
319 /* Poll for a charcter on the USB input queue.
320 * returns AO_READ_AGAIN if none are available
323 ao_usb_pollchar(void);
325 /* Flush the USB output queue */
329 /* USB interrupt handler */
331 ao_usb_isr(void) __interrupt 6;
333 /* Enable the USB controller */
337 /* Disable the USB controller */
339 ao_usb_disable(void);
341 /* Initialize the USB system */
351 ao_cmd_lex_error = 1,
352 ao_cmd_syntax_error = 2,
355 extern __xdata uint16_t ao_cmd_lex_i;
356 extern __xdata uint32_t ao_cmd_lex_u32;
357 extern __xdata char ao_cmd_lex_c;
358 extern __xdata enum ao_cmd_status ao_cmd_status;
364 ao_cmd_put8(uint8_t v);
367 ao_cmd_put16(uint16_t v);
376 ao_cmd_decimal(void);
379 ao_match_word(__code char *word);
388 ao_cmd_register(__code struct ao_cmds *cmds);
397 /* Allocate a DMA channel. the 'done' parameter will be set
398 * when the dma is finished or aborted and will be used to
402 #define AO_DMA_DONE 1
403 #define AO_DMA_ABORTED 2
406 ao_dma_alloc(__xdata uint8_t * done);
408 /* Setup a DMA channel */
410 ao_dma_set_transfer(uint8_t id,
411 void __xdata *srcaddr,
412 void __xdata *dstaddr,
417 /* Start a DMA channel */
419 ao_dma_start(uint8_t id);
421 /* Manually trigger a DMA channel */
423 ao_dma_trigger(uint8_t id);
425 /* Abort a running DMA transfer */
427 ao_dma_abort(uint8_t id);
429 /* DMA interrupt routine */
431 ao_dma_isr(void) __interrupt 8;
438 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
441 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
448 * We reserve the last block on the device for
449 * configuration space. Writes and reads in this
450 * area return errors.
453 #define AO_EE_BLOCK_SIZE ((uint16_t) (256))
454 #define AO_EE_DEVICE_SIZE ((uint32_t) 128 * (uint32_t) 1024)
455 #define AO_EE_DATA_SIZE (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
456 #define AO_EE_CONFIG_BLOCK ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
459 ao_ee_flush(void) __reentrant;
461 /* Write to the eeprom */
463 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
465 /* Read from the eeprom */
467 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
469 /* Write the config block (at the end of the eeprom) */
471 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
473 /* Read the config block (at the end of the eeprom) */
475 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
477 /* Initialize the EEPROM code */
486 * The data log is recorded in the eeprom as a sequence
489 * Each packet starts with a 4-byte header that has the
490 * packet type, the packet checksum and the tick count. Then
491 * they all contain 2 16 bit values which hold packet-specific
494 * For each flight, the first packet
495 * is FLIGHT packet, indicating the serial number of the
496 * device and a unique number marking the number of flights
497 * recorded by this device.
499 * During flight, data from the accelerometer and barometer
500 * are recorded in SENSOR packets, using the raw 16-bit values
501 * read from the A/D converter.
503 * Also during flight, but at a lower rate, the deployment
504 * sensors are recorded in DEPLOY packets. The goal here is to
505 * detect failure in the deployment circuits.
507 * STATE packets hold state transitions as the flight computer
508 * transitions through different stages of the flight.
510 #define AO_LOG_FLIGHT 'F'
511 #define AO_LOG_SENSOR 'A'
512 #define AO_LOG_TEMP_VOLT 'T'
513 #define AO_LOG_DEPLOY 'D'
514 #define AO_LOG_STATE 'S'
515 #define AO_LOG_GPS_TIME 'G'
516 #define AO_LOG_GPS_LAT 'N'
517 #define AO_LOG_GPS_LON 'W'
518 #define AO_LOG_GPS_ALT 'H'
519 #define AO_LOG_GPS_SAT 'V'
520 #define AO_LOG_GPS_DATE 'Y'
522 #define AO_LOG_POS_NONE (~0UL)
524 struct ao_log_record {
530 int16_t ground_accel;
555 int32_t gps_latitude;
556 int32_t gps_longitude;
579 /* Write a record to the eeprom log */
581 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
587 /* We record flight numbers in the first record of
588 * the log. Tasks may wait for this to be initialized
589 * by sleeping on this variable.
591 extern __xdata uint16_t ao_flight_number;
593 /* Retrieve first log record for the current flight */
595 ao_log_dump_first(void);
597 /* return next log record for the current flight */
599 ao_log_dump_next(void);
601 /* Logging thread main routine */
605 /* Start logging to eeprom */
613 /* Initialize the logging system */
621 enum ao_flight_state {
622 ao_flight_startup = 0,
628 ao_flight_drogue = 6,
630 ao_flight_landed = 8,
631 ao_flight_invalid = 9
634 extern __xdata struct ao_adc ao_flight_data;
635 extern __pdata enum ao_flight_state ao_flight_state;
636 extern __pdata uint16_t ao_flight_tick;
637 extern __pdata int16_t ao_flight_accel;
638 extern __pdata int16_t ao_flight_pres;
639 extern __pdata int32_t ao_flight_vel;
640 extern __pdata int16_t ao_ground_pres;
641 extern __pdata int16_t ao_ground_accel;
642 extern __pdata int16_t ao_min_pres;
643 extern __pdata uint16_t ao_launch_time;
644 extern __xdata uint8_t ao_flight_force_idle;
650 /* Initialize flight thread */
652 ao_flight_init(void);
659 ao_report_init(void);
664 * Given raw data, convert to SI units
667 /* pressure from the sensor to altitude in meters */
669 ao_pres_to_altitude(int16_t pres) __reentrant;
672 ao_altitude_to_pres(int16_t alt) __reentrant;
675 ao_temp_to_dC(int16_t temp) __reentrant;
680 * debug another telemetrum board
683 /* Send a byte to the dbg target */
685 ao_dbg_send_byte(uint8_t byte);
687 /* Receive a byte from the dbg target */
689 ao_dbg_recv_byte(void);
691 /* Start a bulk transfer to/from dbg target memory */
693 ao_dbg_start_transfer(uint16_t addr);
695 /* End a bulk transfer to/from dbg target memory */
697 ao_dbg_end_transfer(void);
699 /* Write a byte to dbg target memory */
701 ao_dbg_write_byte(uint8_t byte);
703 /* Read a byte from dbg target memory */
705 ao_dbg_read_byte(void);
707 /* Enable dbg mode, switching use of the pins */
709 ao_dbg_debug_mode(void);
711 /* Reset the dbg target */
723 #error Please define HAS_SERIAL_1
728 ao_serial_rx1_isr(void) __interrupt 3;
731 ao_serial_tx1_isr(void) __interrupt 14;
734 ao_serial_getchar(void) __critical;
737 ao_serial_putchar(char c) __critical;
739 #define AO_SERIAL_SPEED_4800 0
740 #define AO_SERIAL_SPEED_9600 1
741 #define AO_SERIAL_SPEED_57600 2
744 ao_serial_set_speed(uint8_t speed);
747 ao_serial_init(void);
754 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
755 #define AO_GPS_NUM_SAT_SHIFT (0)
757 #define AO_GPS_VALID (1 << 4)
758 #define AO_GPS_RUNNING (1 << 5)
759 #define AO_GPS_DATE_VALID (1 << 6)
761 extern __xdata uint16_t ao_gps_tick;
771 int32_t latitude; /* degrees * 10⁷ */
772 int32_t longitude; /* degrees * 10⁷ */
773 int16_t altitude; /* m */
774 uint16_t ground_speed; /* cm/s */
775 uint8_t course; /* degrees / 2 */
776 uint8_t hdop; /* * 5 */
777 int16_t climb_rate; /* cm/s */
778 uint16_t h_error; /* m */
779 uint16_t v_error; /* m */
782 struct ao_gps_sat_data {
787 #define AO_MAX_GPS_TRACKING 12
789 struct ao_gps_tracking_data {
791 struct ao_gps_sat_data sats[AO_MAX_GPS_TRACKING];
794 extern __xdata uint8_t ao_gps_mutex;
795 extern __xdata struct ao_gps_data ao_gps_data;
796 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
802 ao_gps_print(__xdata struct ao_gps_data *gps_data);
805 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
818 ao_gps_report_init(void);
824 #define AO_MAX_CALLSIGN 8
825 #define AO_TELEMETRY_VERSION 3
827 struct ao_telemetry {
830 uint8_t flight_state;
831 int16_t flight_accel;
832 int16_t ground_accel;
836 int16_t accel_plus_g;
837 int16_t accel_minus_g;
839 struct ao_gps_data gps;
840 char callsign[AO_MAX_CALLSIGN];
841 struct ao_gps_tracking_data gps_tracking;
844 /* Set delay between telemetry reports (0 to disable) */
846 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(1000)
847 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(50)
848 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(1000)
851 ao_telemetry_set_interval(uint16_t interval);
854 ao_rdf_set(uint8_t rdf);
857 ao_telemetry_init(void);
863 extern __xdata uint8_t ao_radio_dma;
864 extern __xdata uint8_t ao_radio_dma_done;
865 extern __xdata uint8_t ao_radio_done;
866 extern __xdata uint8_t ao_radio_mutex;
869 ao_radio_general_isr(void) __interrupt 16;
874 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
877 ao_radio_set_telemetry(void);
880 ao_radio_set_packet(void);
883 ao_radio_set_rdf(void);
886 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant;
888 struct ao_radio_recv {
889 struct ao_telemetry telemetry;
895 ao_radio_recv(__xdata struct ao_radio_recv *recv) __reentrant;
898 ao_radio_rdf(int ms);
901 ao_radio_abort(void);
904 ao_radio_rdf_abort(void);
916 extern const char const * const ao_state_names[];
922 ao_set_monitor(uint8_t monitoring);
925 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
931 #define AO_READ_AGAIN ((char) -1)
934 char (*pollchar)(void);
935 void (*putchar)(char c) __reentrant;
942 extern __xdata uint8_t ao_stdin_ready;
945 ao_add_stdio(char (*pollchar)(void),
946 void (*putchar)(char) __reentrant,
947 void (*flush)(void));
954 ao_igniter_drogue = 0,
959 ao_ignite(enum ao_igniter igniter);
961 enum ao_igniter_status {
962 ao_igniter_unknown, /* unknown status (ambiguous voltage) */
963 ao_igniter_ready, /* continuity detected */
964 ao_igniter_active, /* igniter firing */
965 ao_igniter_open, /* open circuit detected */
968 enum ao_igniter_status
969 ao_igniter_status(enum ao_igniter igniter);
972 ao_igniter_init(void);
978 #define AO_CONFIG_MAJOR 1
979 #define AO_CONFIG_MINOR 3
984 uint16_t main_deploy;
985 int16_t accel_plus_g; /* changed for minor version 2 */
986 uint8_t radio_channel;
987 char callsign[AO_MAX_CALLSIGN + 1];
988 uint8_t apogee_delay; /* minor version 1 */
989 int16_t accel_minus_g; /* minor version 2 */
990 uint32_t radio_cal; /* minor version 3 */
993 extern __xdata struct ao_config ao_config;
999 ao_config_init(void);
1006 ao_rssi_set(int rssi_value);
1009 ao_rssi_init(uint8_t rssi_led);
1014 * values which need to be defined for
1015 * each instance of a product
1018 extern __code __at(0x00aa) uint8_t ao_usb_descriptors [];
1019 extern const char ao_version[];
1020 extern const char ao_manufacturer[];
1021 extern const char ao_product[];
1027 #define AO_FIFO_SIZE 32
1032 char fifo[AO_FIFO_SIZE];
1035 #define ao_fifo_insert(f,c) do { \
1036 (f).fifo[(f).insert] = (c); \
1037 (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1040 #define ao_fifo_remove(f,c) do {\
1041 c = (f).fifo[(f).remove]; \
1042 (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1045 #define ao_fifo_full(f) ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1046 #define ao_fifo_empty(f) ((f).insert == (f).remove)
1051 * Packet-based command interface
1054 #define AO_PACKET_MAX 64
1055 #define AO_PACKET_SYN (uint8_t) 0xff
1062 uint8_t d[AO_PACKET_MAX];
1063 uint8_t callsign[AO_MAX_CALLSIGN];
1066 struct ao_packet_recv {
1067 struct ao_packet packet;
1072 extern __xdata struct ao_packet_recv ao_rx_packet;
1073 extern __xdata struct ao_packet ao_tx_packet;
1074 extern __xdata struct ao_task ao_packet_task;
1075 extern __xdata uint8_t ao_packet_enable;
1076 extern __xdata uint8_t ao_packet_master_sleeping;
1077 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1080 ao_packet_send(void);
1083 ao_packet_recv(void);
1086 ao_packet_flush(void);
1089 ao_packet_putchar(char c) __reentrant;
1092 ao_packet_pollchar(void) __critical;
1094 /* ao_packet_master.c */
1097 ao_packet_master_init(void);
1099 /* ao_packet_slave.c */
1102 ao_packet_slave_start(void);
1105 ao_packet_slave_stop(void);
1108 ao_packet_slave_init(void);
1116 ao_terraui_init(void);
1121 ao_audio_test(void);
1124 ao_audio_send(__xdata uint8_t *samples, uint16_t nsamples) __reentrant;
1127 ao_audio_init(void);