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; /* unique id */
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) */
113 #define AO_PANIC_USB 10 /* Trying to send USB packet while busy */
115 /* Stop the operating system, beeping and blinking the reason */
117 ao_panic(uint8_t reason);
123 /* Our timer runs at 100Hz */
125 #define AO_MS_TO_TICKS(ms) ((ms) / (1000 / AO_HERTZ))
126 #define AO_SEC_TO_TICKS(s) ((s) * AO_HERTZ)
128 /* Returns the current time in ticks */
132 /* Suspend the current task until ticks time has passed */
134 ao_delay(uint16_t ticks);
136 /* Set the ADC interval */
138 ao_timer_set_adc_interval(uint8_t interval) __critical;
140 /* Timer interrupt */
142 ao_timer_isr(void) interrupt 9;
144 /* Initialize the timer */
148 /* Initialize the hardware clock. Must be called first */
153 * One set of samples read from the A/D converter or telemetry
156 uint16_t tick; /* tick when the sample was read */
157 int16_t accel; /* accelerometer */
158 int16_t pres; /* pressure sensor */
159 int16_t temp; /* temperature sensor */
160 int16_t v_batt; /* battery voltage */
161 int16_t sense_d; /* drogue continuity sense */
162 int16_t sense_m; /* main continuity sense */
166 #error Please define HAS_ADC
174 #define AO_ADC_RING 32
175 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
176 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
180 * A/D data is stored in a ring, with the next sample to be written
183 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
184 extern volatile __data uint8_t ao_adc_head;
186 /* Trigger a conversion sequence (called from the timer interrupt) */
190 /* Suspend the current task until another A/D sample is converted */
194 /* Get a copy of the last complete A/D sample set */
196 ao_adc_get(__xdata struct ao_adc *packet);
198 /* The A/D interrupt handler */
201 ao_adc_isr(void) interrupt 1;
203 /* Initialize the A/D converter */
214 * Various pre-defined beep frequencies
216 * frequency = 1/2 (24e6/32) / beep
219 #define AO_BEEP_LOW 150 /* 2500Hz */
220 #define AO_BEEP_MID 94 /* 3989Hz */
221 #define AO_BEEP_HIGH 75 /* 5000Hz */
222 #define AO_BEEP_OFF 0 /* off */
224 #define AO_BEEP_g 240 /* 1562.5Hz */
225 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
226 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
227 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
228 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
229 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
230 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
231 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
232 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
233 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
234 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
235 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
236 #define AO_BEEP_gg 120 /* 3125Hz */
237 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
238 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
239 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
240 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
241 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
242 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
243 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
244 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
245 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
246 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
247 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
248 #define AO_BEEP_ggg 60 /* 6250Hz */
250 /* Set the beeper to the specified tone */
252 ao_beep(uint8_t beep);
254 /* Turn on the beeper for the specified time */
256 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
258 /* Initialize the beeper */
266 #define AO_LED_NONE 0
268 /* Turn on the specified LEDs */
270 ao_led_on(uint8_t colors);
272 /* Turn off the specified LEDs */
274 ao_led_off(uint8_t colors);
276 /* Set all of the LEDs to the specified state */
278 ao_led_set(uint8_t colors);
280 /* Toggle the specified LEDs */
282 ao_led_toggle(uint8_t colors);
284 /* Turn on the specified LEDs for the indicated interval */
286 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
288 /* Initialize the LEDs */
290 ao_led_init(uint8_t enable);
296 #define AO_ROMCONFIG_VERSION 1
298 extern __code __at (0x00a0) uint16_t ao_romconfig_version;
299 extern __code __at (0x00a2) uint16_t ao_romconfig_check;
300 extern __code __at (0x00a4) uint16_t ao_serial_number;
301 extern __code __at (0x00a6) uint32_t ao_radio_cal;
307 /* Put one character to the USB output queue */
309 ao_usb_putchar(char c);
311 /* Get one character from the USB input queue */
313 ao_usb_getchar(void);
315 /* Poll for a charcter on the USB input queue.
316 * returns AO_READ_AGAIN if none are available
319 ao_usb_pollchar(void);
321 /* Flush the USB output queue */
325 /* USB interrupt handler */
327 ao_usb_isr(void) interrupt 6;
329 /* Enable the USB controller */
333 /* Disable the USB controller */
335 ao_usb_disable(void);
337 /* Initialize the USB system */
347 ao_cmd_lex_error = 1,
348 ao_cmd_syntax_error = 2,
351 extern __xdata uint16_t ao_cmd_lex_i;
352 extern __xdata uint32_t ao_cmd_lex_u32;
353 extern __xdata char ao_cmd_lex_c;
354 extern __xdata enum ao_cmd_status ao_cmd_status;
360 ao_cmd_put8(uint8_t v);
363 ao_cmd_put16(uint16_t v);
372 ao_cmd_decimal(void);
375 ao_match_word(__code char *word);
384 ao_cmd_register(__code struct ao_cmds *cmds);
393 /* Allocate a DMA channel. the 'done' parameter will be set
394 * when the dma is finished or aborted and will be used to
398 #define AO_DMA_DONE 1
399 #define AO_DMA_ABORTED 2
402 ao_dma_alloc(__xdata uint8_t * done);
404 /* Setup a DMA channel */
406 ao_dma_set_transfer(uint8_t id,
407 void __xdata *srcaddr,
408 void __xdata *dstaddr,
413 /* Start a DMA channel */
415 ao_dma_start(uint8_t id);
417 /* Manually trigger a DMA channel */
419 ao_dma_trigger(uint8_t id);
421 /* Abort a running DMA transfer */
423 ao_dma_abort(uint8_t id);
425 /* DMA interrupt routine */
427 ao_dma_isr(void) interrupt 8;
434 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
437 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
444 * We reserve the last block on the device for
445 * configuration space. Writes and reads in this
446 * area return errors.
449 #define AO_EE_BLOCK_SIZE ((uint16_t) (256))
450 #define AO_EE_DEVICE_SIZE ((uint32_t) 128 * (uint32_t) 1024)
451 #define AO_EE_DATA_SIZE (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
452 #define AO_EE_CONFIG_BLOCK ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
455 ao_ee_flush(void) __reentrant;
457 /* Write to the eeprom */
459 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
461 /* Read from the eeprom */
463 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
465 /* Write the config block (at the end of the eeprom) */
467 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
469 /* Read the config block (at the end of the eeprom) */
471 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
473 /* Initialize the EEPROM code */
482 * The data log is recorded in the eeprom as a sequence
485 * Each packet starts with a 4-byte header that has the
486 * packet type, the packet checksum and the tick count. Then
487 * they all contain 2 16 bit values which hold packet-specific
490 * For each flight, the first packet
491 * is FLIGHT packet, indicating the serial number of the
492 * device and a unique number marking the number of flights
493 * recorded by this device.
495 * During flight, data from the accelerometer and barometer
496 * are recorded in SENSOR packets, using the raw 16-bit values
497 * read from the A/D converter.
499 * Also during flight, but at a lower rate, the deployment
500 * sensors are recorded in DEPLOY packets. The goal here is to
501 * detect failure in the deployment circuits.
503 * STATE packets hold state transitions as the flight computer
504 * transitions through different stages of the flight.
506 #define AO_LOG_FLIGHT 'F'
507 #define AO_LOG_SENSOR 'A'
508 #define AO_LOG_TEMP_VOLT 'T'
509 #define AO_LOG_DEPLOY 'D'
510 #define AO_LOG_STATE 'S'
511 #define AO_LOG_GPS_TIME 'G'
512 #define AO_LOG_GPS_LAT 'N'
513 #define AO_LOG_GPS_LON 'W'
514 #define AO_LOG_GPS_ALT 'H'
515 #define AO_LOG_GPS_SAT 'V'
516 #define AO_LOG_GPS_DATE 'Y'
518 #define AO_LOG_POS_NONE (~0UL)
520 struct ao_log_record {
526 int16_t ground_accel;
551 int32_t gps_latitude;
552 int32_t gps_longitude;
574 /* Write a record to the eeprom log */
576 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
582 /* We record flight numbers in the first record of
583 * the log. Tasks may wait for this to be initialized
584 * by sleeping on this variable.
586 extern __xdata uint16_t ao_flight_number;
588 /* Retrieve first log record for the current flight */
590 ao_log_dump_first(void);
592 /* return next log record for the current flight */
594 ao_log_dump_next(void);
596 /* Logging thread main routine */
600 /* Start logging to eeprom */
608 /* Initialize the logging system */
616 enum ao_flight_state {
617 ao_flight_startup = 0,
623 ao_flight_drogue = 6,
625 ao_flight_landed = 8,
626 ao_flight_invalid = 9
629 extern __xdata struct ao_adc ao_flight_data;
630 extern __pdata enum ao_flight_state ao_flight_state;
631 extern __pdata uint16_t ao_flight_tick;
632 extern __pdata int16_t ao_flight_accel;
633 extern __pdata int16_t ao_flight_pres;
634 extern __pdata int32_t ao_flight_vel;
635 extern __pdata int16_t ao_ground_pres;
636 extern __pdata int16_t ao_ground_accel;
637 extern __pdata int16_t ao_min_pres;
638 extern __pdata uint16_t ao_launch_time;
639 extern __xdata uint8_t ao_flight_force_idle;
645 /* Initialize flight thread */
647 ao_flight_init(void);
654 ao_report_init(void);
659 * Given raw data, convert to SI units
662 /* pressure from the sensor to altitude in meters */
664 ao_pres_to_altitude(int16_t pres) __reentrant;
667 ao_altitude_to_pres(int16_t alt) __reentrant;
670 ao_temp_to_dC(int16_t temp) __reentrant;
675 * debug another telemetrum board
678 /* Send a byte to the dbg target */
680 ao_dbg_send_byte(uint8_t byte);
682 /* Receive a byte from the dbg target */
684 ao_dbg_recv_byte(void);
686 /* Start a bulk transfer to/from dbg target memory */
688 ao_dbg_start_transfer(uint16_t addr);
690 /* End a bulk transfer to/from dbg target memory */
692 ao_dbg_end_transfer(void);
694 /* Write a byte to dbg target memory */
696 ao_dbg_write_byte(uint8_t byte);
698 /* Read a byte from dbg target memory */
700 ao_dbg_read_byte(void);
702 /* Enable dbg mode, switching use of the pins */
704 ao_dbg_debug_mode(void);
706 /* Reset the dbg target */
718 #error Please define HAS_SERIAL_1
723 ao_serial_rx1_isr(void) interrupt 3;
726 ao_serial_tx1_isr(void) interrupt 14;
729 ao_serial_getchar(void) __critical;
732 ao_serial_putchar(char c) __critical;
734 #define AO_SERIAL_SPEED_4800 0
735 #define AO_SERIAL_SPEED_9600 1
736 #define AO_SERIAL_SPEED_57600 2
739 ao_serial_set_speed(uint8_t speed);
742 ao_serial_init(void);
749 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
750 #define AO_GPS_NUM_SAT_SHIFT (0)
752 #define AO_GPS_VALID (1 << 4)
753 #define AO_GPS_RUNNING (1 << 5)
754 #define AO_GPS_DATE_VALID (1 << 6)
756 extern __xdata uint16_t ao_gps_tick;
766 int32_t latitude; /* degrees * 10⁷ */
767 int32_t longitude; /* degrees * 10⁷ */
768 int16_t altitude; /* m */
769 uint16_t ground_speed; /* cm/s */
770 uint8_t course; /* degrees / 2 */
771 uint8_t hdop; /* * 5 */
772 int16_t climb_rate; /* cm/s */
773 uint16_t h_error; /* m */
774 uint16_t v_error; /* m */
777 struct ao_gps_sat_data {
782 struct ao_gps_tracking_data {
784 struct ao_gps_sat_data sats[12];
787 extern __xdata uint8_t ao_gps_mutex;
788 extern __xdata struct ao_gps_data ao_gps_data;
789 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
795 ao_gps_print(__xdata struct ao_gps_data *gps_data);
798 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
811 ao_gps_report_init(void);
817 #define AO_MAX_CALLSIGN 8
818 #define AO_TELEMETRY_VERSION 3
820 struct ao_telemetry {
823 uint8_t flight_state;
824 int16_t flight_accel;
825 int16_t ground_accel;
829 int16_t accel_plus_g;
830 int16_t accel_minus_g;
832 struct ao_gps_data gps;
833 char callsign[AO_MAX_CALLSIGN];
834 struct ao_gps_tracking_data gps_tracking;
837 /* Set delay between telemetry reports (0 to disable) */
839 #define AO_TELEMETRY_INTERVAL_PAD AO_MS_TO_TICKS(1000)
840 #define AO_TELEMETRY_INTERVAL_FLIGHT AO_MS_TO_TICKS(50)
841 #define AO_TELEMETRY_INTERVAL_RECOVER AO_MS_TO_TICKS(1000)
844 ao_telemetry_set_interval(uint16_t interval);
847 ao_rdf_set(uint8_t rdf);
850 ao_telemetry_init(void);
856 extern __xdata uint8_t ao_radio_dma;
857 extern __xdata uint8_t ao_radio_dma_done;
858 extern __xdata uint8_t ao_radio_done;
859 extern __xdata uint8_t ao_radio_mutex;
862 ao_radio_general_isr(void) interrupt 16;
867 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
870 ao_radio_set_telemetry(void);
873 ao_radio_set_packet(void);
876 ao_radio_set_rdf(void);
879 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant;
881 struct ao_radio_recv {
882 struct ao_telemetry telemetry;
888 ao_radio_recv(__xdata struct ao_radio_recv *recv) __reentrant;
891 ao_radio_rdf(int ms);
894 ao_radio_abort(void);
897 ao_radio_rdf_abort(void);
909 extern const char const * const ao_state_names[];
915 ao_set_monitor(uint8_t monitoring);
918 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
924 #define AO_READ_AGAIN ((char) -1)
927 char (*pollchar)(void);
928 void (*putchar)(char c) __reentrant;
935 extern __xdata uint8_t ao_stdin_ready;
938 ao_add_stdio(char (*pollchar)(void),
939 void (*putchar)(char) __reentrant,
940 void (*flush)(void));
947 ao_igniter_drogue = 0,
952 ao_ignite(enum ao_igniter igniter);
954 enum ao_igniter_status {
955 ao_igniter_unknown, /* unknown status (ambiguous voltage) */
956 ao_igniter_ready, /* continuity detected */
957 ao_igniter_active, /* igniter firing */
958 ao_igniter_open, /* open circuit detected */
961 enum ao_igniter_status
962 ao_igniter_status(enum ao_igniter igniter);
965 ao_igniter_init(void);
971 #define AO_CONFIG_MAJOR 1
972 #define AO_CONFIG_MINOR 3
977 uint16_t main_deploy;
978 int16_t accel_plus_g; /* changed for minor version 2 */
979 uint8_t radio_channel;
980 char callsign[AO_MAX_CALLSIGN + 1];
981 uint8_t apogee_delay; /* minor version 1 */
982 int16_t accel_minus_g; /* minor version 2 */
983 uint32_t radio_cal; /* minor version 3 */
986 extern __xdata struct ao_config ao_config;
992 ao_config_init(void);
999 ao_rssi_set(int rssi_value);
1002 ao_rssi_init(uint8_t rssi_led);
1007 * values which need to be defined for
1008 * each instance of a product
1011 extern const uint8_t ao_usb_descriptors [];
1012 extern const char ao_version[];
1013 extern const char ao_manufacturer[];
1014 extern const char ao_product[];
1020 #define AO_FIFO_SIZE 32
1025 char fifo[AO_FIFO_SIZE];
1028 #define ao_fifo_insert(f,c) do { \
1029 (f).fifo[(f).insert] = (c); \
1030 (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1033 #define ao_fifo_remove(f,c) do {\
1034 c = (f).fifo[(f).remove]; \
1035 (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1038 #define ao_fifo_full(f) ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1039 #define ao_fifo_empty(f) ((f).insert == (f).remove)
1044 * Packet-based command interface
1047 #define AO_PACKET_MAX 64
1048 #define AO_PACKET_SYN (uint8_t) 0xff
1055 uint8_t d[AO_PACKET_MAX];
1056 uint8_t callsign[AO_MAX_CALLSIGN];
1059 struct ao_packet_recv {
1060 struct ao_packet packet;
1065 extern __xdata struct ao_packet_recv ao_rx_packet;
1066 extern __xdata struct ao_packet ao_tx_packet;
1067 extern __xdata struct ao_task ao_packet_task;
1068 extern __xdata uint8_t ao_packet_enable;
1069 extern __xdata uint8_t ao_packet_master_sleeping;
1070 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1073 ao_packet_send(void);
1076 ao_packet_recv(void);
1079 ao_packet_flush(void);
1082 ao_packet_putchar(char c) __reentrant;
1085 ao_packet_pollchar(void) __critical;
1087 /* ao_packet_master.c */
1090 ao_packet_master_init(void);
1092 /* ao_packet_slave.c */
1095 ao_packet_slave_start(void);
1098 ao_packet_slave_stop(void);
1101 ao_packet_slave_init(void);