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 /* Stack runs from above the allocated __data space to 0xfe, which avoids
35 * writing to 0xff as that triggers the stack overflow indicator
37 #define AO_STACK_START 0x80
38 #define AO_STACK_END 0xfe
39 #define AO_STACK_SIZE (AO_STACK_END - AO_STACK_START + 1)
43 __xdata void *wchan; /* current wait channel (NULL if running) */
44 uint16_t alarm; /* abort ao_sleep time */
45 uint8_t stack_count; /* amount of saved stack */
46 uint8_t task_id; /* index in the task array */
47 __code char *name; /* task name */
48 uint8_t stack[AO_STACK_SIZE]; /* saved stack */
51 extern __xdata struct ao_task *__data ao_cur_task;
53 #define AO_NUM_TASKS 16 /* maximum number of tasks */
54 #define AO_NO_TASK 0 /* no task id */
60 /* Suspend the current task until wchan is awoken.
66 ao_sleep(__xdata void *wchan);
68 /* Wake all tasks sleeping on wchan */
70 ao_wakeup(__xdata void *wchan);
72 /* Wake up a specific task */
74 ao_wake_task(__xdata struct ao_task *task);
76 /* set an alarm to go off in 'delay' ticks */
78 ao_alarm(uint16_t delay);
80 /* Yield the processor to another task */
82 ao_yield(void) _naked;
84 /* Add a task to the run queue */
86 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
88 /* Terminate the current task */
92 /* Dump task info to console */
96 /* Start the scheduler. This will not return */
98 ao_start_scheduler(void);
104 #define AO_PANIC_NO_TASK 1 /* AO_NUM_TASKS is not large enough */
105 #define AO_PANIC_DMA 2 /* Attempt to start DMA while active */
106 #define AO_PANIC_MUTEX 3 /* Mis-using mutex API */
107 #define AO_PANIC_EE 4 /* Mis-using eeprom API */
108 #define AO_PANIC_LOG 5 /* Failing to read/write log data */
109 #define AO_PANIC_CMD 6 /* Too many command sets registered */
110 #define AO_PANIC_STDIO 7 /* Too many stdio handlers registered */
111 #define AO_PANIC_REBOOT 8 /* Reboot failed */
112 #define AO_PANIC_FLASH 9 /* Invalid flash part (or wrong blocksize) */
114 /* Stop the operating system, beeping and blinking the reason */
116 ao_panic(uint8_t reason);
122 /* Our timer runs at 100Hz */
124 #define AO_MS_TO_TICKS(ms) ((ms) / (1000 / AO_HERTZ))
125 #define AO_SEC_TO_TICKS(s) ((s) * AO_HERTZ)
127 /* Returns the current time in ticks */
131 /* Suspend the current task until ticks time has passed */
133 ao_delay(uint16_t ticks);
135 /* Set the ADC interval */
137 ao_timer_set_adc_interval(uint8_t interval) __critical;
139 /* Timer interrupt */
141 ao_timer_isr(void) interrupt 9;
143 /* Initialize the timer */
147 /* Initialize the hardware clock. Must be called first */
152 * One set of samples read from the A/D converter or telemetry
155 uint16_t tick; /* tick when the sample was read */
156 int16_t accel; /* accelerometer */
157 int16_t pres; /* pressure sensor */
158 int16_t temp; /* temperature sensor */
159 int16_t v_batt; /* battery voltage */
160 int16_t sense_d; /* drogue continuity sense */
161 int16_t sense_m; /* main continuity sense */
165 #error Please define HAS_ADC
173 #define AO_ADC_RING 32
174 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
175 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
179 * A/D data is stored in a ring, with the next sample to be written
182 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
183 extern volatile __data uint8_t ao_adc_head;
185 /* Trigger a conversion sequence (called from the timer interrupt) */
189 /* Suspend the current task until another A/D sample is converted */
193 /* Get a copy of the last complete A/D sample set */
195 ao_adc_get(__xdata struct ao_adc *packet);
197 /* The A/D interrupt handler */
200 ao_adc_isr(void) interrupt 1;
202 /* Initialize the A/D converter */
213 * Various pre-defined beep frequencies
215 * frequency = 1/2 (24e6/32) / beep
218 #define AO_BEEP_LOW 150 /* 2500Hz */
219 #define AO_BEEP_MID 94 /* 3989Hz */
220 #define AO_BEEP_HIGH 75 /* 5000Hz */
221 #define AO_BEEP_OFF 0 /* off */
223 #define AO_BEEP_g 240 /* 1562.5Hz */
224 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
225 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
226 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
227 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
228 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
229 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
230 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
231 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
232 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
233 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
234 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
235 #define AO_BEEP_gg 120 /* 3125Hz */
236 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
237 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
238 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
239 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
240 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
241 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
242 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
243 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
244 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
245 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
246 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
247 #define AO_BEEP_ggg 60 /* 6250Hz */
249 /* Set the beeper to the specified tone */
251 ao_beep(uint8_t beep);
253 /* Turn on the beeper for the specified time */
255 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
257 /* Initialize the beeper */
265 #define AO_LED_NONE 0
266 #define AO_LED_GREEN 1
269 /* Turn on the specified LEDs */
271 ao_led_on(uint8_t colors);
273 /* Turn off the specified LEDs */
275 ao_led_off(uint8_t colors);
277 /* Set all of the LEDs to the specified state */
279 ao_led_set(uint8_t colors);
281 /* Toggle the specified LEDs */
283 ao_led_toggle(uint8_t colors);
285 /* Turn on the specified LEDs for the indicated interval */
287 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
289 /* Initialize the LEDs */
291 ao_led_init(uint8_t enable);
297 /* Put one character to the USB output queue */
299 ao_usb_putchar(char c);
301 /* Get one character from the USB input queue */
303 ao_usb_getchar(void);
305 /* Poll for a charcter on the USB input queue.
306 * returns AO_READ_AGAIN if none are available
309 ao_usb_pollchar(void);
311 /* Flush the USB output queue */
315 /* USB interrupt handler */
317 ao_usb_isr(void) interrupt 6;
319 /* Enable the USB controller */
323 /* Disable the USB controller */
325 ao_usb_disable(void);
327 /* Initialize the USB system */
337 ao_cmd_lex_error = 1,
338 ao_cmd_syntax_error = 2,
341 extern __xdata uint16_t ao_cmd_lex_i;
342 extern __xdata uint32_t ao_cmd_lex_u32;
343 extern __xdata char ao_cmd_lex_c;
344 extern __xdata enum ao_cmd_status ao_cmd_status;
350 ao_cmd_put8(uint8_t v);
353 ao_cmd_put16(uint16_t v);
362 ao_cmd_decimal(void);
365 ao_match_word(__code char *word);
374 ao_cmd_register(__code struct ao_cmds *cmds);
383 /* Allocate a DMA channel. the 'done' parameter will be set
384 * when the dma is finished or aborted and will be used to
388 #define AO_DMA_DONE 1
389 #define AO_DMA_ABORTED 2
392 ao_dma_alloc(__xdata uint8_t * done);
394 /* Setup a DMA channel */
396 ao_dma_set_transfer(uint8_t id,
397 void __xdata *srcaddr,
398 void __xdata *dstaddr,
403 /* Start a DMA channel */
405 ao_dma_start(uint8_t id);
407 /* Manually trigger a DMA channel */
409 ao_dma_trigger(uint8_t id);
411 /* Abort a running DMA transfer */
413 ao_dma_abort(uint8_t id);
415 /* DMA interrupt routine */
417 ao_dma_isr(void) interrupt 8;
424 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
427 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
434 * We reserve the last block on the device for
435 * configuration space. Writes and reads in this
436 * area return errors.
439 #define AO_EE_BLOCK_SIZE ((uint16_t) (256))
440 #define AO_EE_DEVICE_SIZE ((uint32_t) 128 * (uint32_t) 1024)
441 #define AO_EE_DATA_SIZE (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
442 #define AO_EE_CONFIG_BLOCK ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
445 ao_ee_flush(void) __reentrant;
447 /* Write to the eeprom */
449 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
451 /* Read from the eeprom */
453 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
455 /* Write the config block (at the end of the eeprom) */
457 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
459 /* Read the config block (at the end of the eeprom) */
461 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
463 /* Initialize the EEPROM code */
472 * The data log is recorded in the eeprom as a sequence
475 * Each packet starts with a 4-byte header that has the
476 * packet type, the packet checksum and the tick count. Then
477 * they all contain 2 16 bit values which hold packet-specific
480 * For each flight, the first packet
481 * is FLIGHT packet, indicating the serial number of the
482 * device and a unique number marking the number of flights
483 * recorded by this device.
485 * During flight, data from the accelerometer and barometer
486 * are recorded in SENSOR packets, using the raw 16-bit values
487 * read from the A/D converter.
489 * Also during flight, but at a lower rate, the deployment
490 * sensors are recorded in DEPLOY packets. The goal here is to
491 * detect failure in the deployment circuits.
493 * STATE packets hold state transitions as the flight computer
494 * transitions through different stages of the flight.
496 #define AO_LOG_FLIGHT 'F'
497 #define AO_LOG_SENSOR 'A'
498 #define AO_LOG_TEMP_VOLT 'T'
499 #define AO_LOG_DEPLOY 'D'
500 #define AO_LOG_STATE 'S'
501 #define AO_LOG_GPS_TIME 'G'
502 #define AO_LOG_GPS_LAT 'N'
503 #define AO_LOG_GPS_LON 'W'
504 #define AO_LOG_GPS_ALT 'H'
505 #define AO_LOG_GPS_SAT 'V'
506 #define AO_LOG_GPS_DATE 'Y'
508 #define AO_LOG_POS_NONE (~0UL)
510 struct ao_log_record {
516 int16_t ground_accel;
541 int32_t gps_latitude;
542 int32_t gps_longitude;
564 /* Write a record to the eeprom log */
566 ao_log_data(struct ao_log_record *log);
572 /* We record flight numbers in the first record of
573 * the log. Tasks may wait for this to be initialized
574 * by sleeping on this variable.
576 extern __xdata uint16_t ao_flight_number;
578 /* Retrieve first log record for the current flight */
580 ao_log_dump_first(void);
582 /* return next log record for the current flight */
584 ao_log_dump_next(void);
586 /* Logging thread main routine */
590 /* Start logging to eeprom */
598 /* Initialize the logging system */
606 enum ao_flight_state {
607 ao_flight_startup = 0,
613 ao_flight_drogue = 6,
615 ao_flight_landed = 8,
616 ao_flight_invalid = 9
619 extern __xdata struct ao_adc ao_flight_data;
620 extern __pdata enum ao_flight_state ao_flight_state;
621 extern __pdata uint16_t ao_flight_tick;
622 extern __pdata int16_t ao_flight_accel;
623 extern __pdata int16_t ao_flight_pres;
624 extern __pdata int32_t ao_flight_vel;
625 extern __pdata int16_t ao_ground_pres;
626 extern __pdata int16_t ao_ground_accel;
627 extern __pdata int16_t ao_min_pres;
628 extern __pdata uint16_t ao_launch_time;
629 extern __xdata uint8_t ao_flight_force_idle;
635 /* Initialize flight thread */
637 ao_flight_init(void);
644 ao_report_init(void);
649 * Given raw data, convert to SI units
652 /* pressure from the sensor to altitude in meters */
654 ao_pres_to_altitude(int16_t pres) __reentrant;
657 ao_altitude_to_pres(int16_t alt) __reentrant;
660 ao_temp_to_dC(int16_t temp) __reentrant;
665 * debug another telemetrum board
668 /* Send a byte to the dbg target */
670 ao_dbg_send_byte(uint8_t byte);
672 /* Receive a byte from the dbg target */
674 ao_dbg_recv_byte(void);
676 /* Start a bulk transfer to/from dbg target memory */
678 ao_dbg_start_transfer(uint16_t addr);
680 /* End a bulk transfer to/from dbg target memory */
682 ao_dbg_end_transfer(void);
684 /* Write a byte to dbg target memory */
686 ao_dbg_write_byte(uint8_t byte);
688 /* Read a byte from dbg target memory */
690 ao_dbg_read_byte(void);
692 /* Enable dbg mode, switching use of the pins */
694 ao_dbg_debug_mode(void);
696 /* Reset the dbg target */
708 #error Please define HAS_SERIAL_1
713 ao_serial_rx1_isr(void) interrupt 3;
716 ao_serial_tx1_isr(void) interrupt 14;
719 ao_serial_getchar(void) __critical;
722 ao_serial_putchar(char c) __critical;
724 #define AO_SERIAL_SPEED_4800 0
725 #define AO_SERIAL_SPEED_9600 1
726 #define AO_SERIAL_SPEED_57600 2
729 ao_serial_set_speed(uint8_t speed);
732 ao_serial_init(void);
739 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
740 #define AO_GPS_NUM_SAT_SHIFT (0)
742 #define AO_GPS_VALID (1 << 4)
743 #define AO_GPS_RUNNING (1 << 5)
744 #define AO_GPS_DATE_VALID (1 << 6)
746 extern __xdata uint16_t ao_gps_tick;
756 int32_t latitude; /* degrees * 10⁷ */
757 int32_t longitude; /* degrees * 10⁷ */
758 int16_t altitude; /* m */
759 uint16_t ground_speed; /* cm/s */
760 uint8_t course; /* degrees / 2 */
761 uint8_t hdop; /* * 5 */
762 int16_t climb_rate; /* cm/s */
763 uint16_t h_error; /* m */
764 uint16_t v_error; /* m */
767 struct ao_gps_sat_data {
772 struct ao_gps_tracking_data {
774 struct ao_gps_sat_data sats[12];
777 extern __xdata uint8_t ao_gps_mutex;
778 extern __xdata struct ao_gps_data ao_gps_data;
779 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
785 ao_gps_print(__xdata struct ao_gps_data *gps_data);
788 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
801 ao_gps_report_init(void);
807 #define AO_MAX_CALLSIGN 8
808 #define AO_TELEMETRY_VERSION 2
810 struct ao_telemetry {
813 uint8_t flight_state;
814 int16_t flight_accel;
815 int16_t ground_accel;
819 int16_t accel_plus_g;
820 int16_t accel_minus_g;
822 struct ao_gps_data gps;
823 char callsign[AO_MAX_CALLSIGN];
824 struct ao_gps_tracking_data gps_tracking;
827 /* Set delay between telemetry reports (0 to disable) */
829 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(1000)
830 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(50)
831 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(1000)
834 ao_telemetry_set_interval(uint16_t interval);
837 ao_rdf_set(uint8_t rdf);
840 ao_telemetry_init(void);
846 extern __xdata uint8_t ao_radio_dma;
847 extern __xdata uint8_t ao_radio_dma_done;
848 extern __xdata uint8_t ao_radio_done;
849 extern __xdata uint8_t ao_radio_mutex;
852 ao_radio_general_isr(void) interrupt 16;
857 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
860 ao_radio_set_telemetry(void);
863 ao_radio_set_packet(void);
866 ao_radio_set_rdf(void);
869 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant;
871 struct ao_radio_recv {
872 struct ao_telemetry telemetry;
878 ao_radio_recv(__xdata struct ao_radio_recv *recv) __reentrant;
881 ao_radio_rdf(int ms);
884 ao_radio_abort(void);
887 ao_radio_rdf_abort(void);
899 extern const char const * const ao_state_names[];
905 ao_set_monitor(uint8_t monitoring);
908 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
914 #define AO_READ_AGAIN ((char) -1)
917 char (*pollchar)(void);
918 void (*putchar)(char c) __reentrant;
925 extern __xdata uint8_t ao_stdin_ready;
928 ao_add_stdio(char (*pollchar)(void),
929 void (*putchar)(char) __reentrant,
930 void (*flush)(void));
937 ao_igniter_drogue = 0,
942 ao_ignite(enum ao_igniter igniter);
944 enum ao_igniter_status {
945 ao_igniter_unknown, /* unknown status (ambiguous voltage) */
946 ao_igniter_ready, /* continuity detected */
947 ao_igniter_active, /* igniter firing */
948 ao_igniter_open, /* open circuit detected */
951 enum ao_igniter_status
952 ao_igniter_status(enum ao_igniter igniter);
955 ao_igniter_init(void);
961 #define AO_CONFIG_MAJOR 1
962 #define AO_CONFIG_MINOR 3
967 uint16_t main_deploy;
968 int16_t accel_plus_g; /* changed for minor version 2 */
969 uint8_t radio_channel;
970 char callsign[AO_MAX_CALLSIGN + 1];
971 uint8_t apogee_delay; /* minor version 1 */
972 int16_t accel_minus_g; /* minor version 2 */
973 uint32_t radio_cal; /* minor version 3 */
976 extern __xdata struct ao_config ao_config;
982 ao_config_init(void);
989 ao_rssi_set(int rssi_value);
992 ao_rssi_init(uint8_t rssi_led);
997 * values which need to be defined for
998 * each instance of a product
1001 extern const uint8_t ao_usb_descriptors [];
1002 extern const uint16_t ao_serial_number;
1003 extern const char ao_version[];
1004 extern const char ao_manufacturer[];
1005 extern const char ao_product[];
1011 #define AO_FIFO_SIZE 32
1016 char fifo[AO_FIFO_SIZE];
1019 #define ao_fifo_insert(f,c) do { \
1020 (f).fifo[(f).insert] = (c); \
1021 (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1024 #define ao_fifo_remove(f,c) do {\
1025 c = (f).fifo[(f).remove]; \
1026 (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1029 #define ao_fifo_full(f) ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1030 #define ao_fifo_empty(f) ((f).insert == (f).remove)
1035 * Packet-based command interface
1038 #define AO_PACKET_MAX 8
1039 #define AO_PACKET_SYN (uint8_t) 0xff
1046 uint8_t d[AO_PACKET_MAX];
1049 struct ao_packet_recv {
1050 struct ao_packet packet;
1055 extern __xdata struct ao_packet_recv ao_rx_packet;
1056 extern __xdata struct ao_packet ao_tx_packet;
1057 extern __xdata struct ao_task ao_packet_task;
1058 extern __xdata uint8_t ao_packet_enable;
1059 extern __xdata uint8_t ao_packet_master_sleeping;
1060 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1063 ao_packet_send(void);
1066 ao_packet_recv(void);
1069 ao_packet_flush(void);
1072 ao_packet_putchar(char c) __reentrant;
1075 ao_packet_pollchar(void) __critical;
1077 /* ao_packet_master.c */
1080 ao_packet_master_init(void);
1082 /* ao_packet_slave.c */
1085 ao_packet_slave_start(void);
1088 ao_packet_slave_stop(void);
1091 ao_packet_slave_init(void);