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.
26 /* Convert a __data pointer into an __xdata pointer */
27 #define DATA_TO_XDATA(a) ((void __xdata *) ((uint8_t) (a) | 0xff00))
29 /* Stack runs from above the allocated __data space to 0xfe, which avoids
30 * writing to 0xff as that triggers the stack overflow indicator
32 #define AO_STACK_START 0x68
33 #define AO_STACK_END 0xfe
34 #define AO_STACK_SIZE (AO_STACK_END - AO_STACK_START + 1)
38 __xdata void *wchan; /* current wait channel (NULL if running) */
39 uint8_t stack_count; /* amount of saved stack */
40 uint8_t task_id; /* index in the task array */
41 __code char *name; /* task name */
42 uint8_t stack[AO_STACK_SIZE]; /* saved stack */
45 extern __xdata struct ao_task *__data ao_cur_task;
47 #define AO_NUM_TASKS 10 /* maximum number of tasks */
48 #define AO_NO_TASK 0 /* no task id */
54 /* Suspend the current task until wchan is awoken */
56 ao_sleep(__xdata void *wchan);
58 /* Wake all tasks sleeping on wchan */
60 ao_wakeup(__xdata void *wchan);
62 /* Yield the processor to another task */
64 ao_yield(void) _naked;
66 /* Add a task to the run queue */
68 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name);
70 /* Dump task info to console */
74 /* Start the scheduler. This will not return */
76 ao_start_scheduler(void);
82 #define AO_PANIC_NO_TASK 1 /* AO_NUM_TASKS is not large enough */
83 #define AO_PANIC_DMA 2 /* Attempt to start DMA while active */
84 #define AO_PANIC_MUTEX 3 /* Mis-using mutex API */
85 #define AO_PANIC_EE 4 /* Mis-using eeprom API */
86 #define AO_PANIC_LOG 5 /* Failing to read/write log data */
87 #define AO_PANIC_CMD 6 /* Too many command sets registered */
89 /* Stop the operating system, beeping and blinking the reason */
91 ao_panic(uint8_t reason);
97 /* Our timer runs at 100Hz */
98 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
99 #define AO_SEC_TO_TICKS(s) ((s) * 100)
101 /* Returns the current time in ticks */
105 /* Suspend the current task until ticks time has passed */
107 ao_delay(uint16_t ticks);
109 /* Set the ADC interval */
111 ao_timer_set_adc_interval(uint8_t interval) __critical;
113 /* Timer interrupt */
115 ao_timer_isr(void) interrupt 9;
117 /* Initialize the timer */
125 #define AO_ADC_RING 128
128 * One set of samples read from the A/D converter
131 uint16_t tick; /* tick when the sample was read */
132 int16_t accel; /* accelerometer */
133 int16_t pres; /* pressure sensor */
134 int16_t temp; /* temperature sensor */
135 int16_t v_batt; /* battery voltage */
136 int16_t sense_d; /* drogue continuity sense */
137 int16_t sense_m; /* main continuity sense */
141 * A/D data is stored in a ring, with the next sample to be written
144 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
145 extern volatile __data uint8_t ao_adc_head;
147 /* Trigger a conversion sequence (called from the timer interrupt) */
151 /* Suspend the current task until another A/D sample is converted */
155 /* Get a copy of the last complete A/D sample set */
157 ao_adc_get(__xdata struct ao_adc *packet);
159 /* The A/D interrupt handler */
162 ao_adc_isr(void) interrupt 1;
165 /* Initialize the A/D converter */
174 * Various pre-defined beep frequencies
176 * frequency = 1/2 (24e6/32) / beep
179 #define AO_BEEP_LOW 150 /* 2500Hz */
180 #define AO_BEEP_MID 94 /* 3989Hz */
181 #define AO_BEEP_HIGH 75 /* 5000Hz */
182 #define AO_BEEP_OFF 0 /* off */
184 #define AO_BEEP_g 240 /* 1562.5Hz */
185 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
186 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
187 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
188 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
189 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
190 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
191 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
192 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
193 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
194 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
195 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
196 #define AO_BEEP_gg 120 /* 3125Hz */
197 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
198 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
199 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
200 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
201 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
202 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
203 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
204 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
205 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
206 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
207 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
208 #define AO_BEEP_ggg 60 /* 6250Hz */
210 /* Set the beeper to the specified tone */
212 ao_beep(uint8_t beep);
214 /* Turn on the beeper for the specified time */
216 ao_beep_for(uint8_t beep, uint16_t ticks);
218 /* Initialize the beeper */
226 #define AO_LED_NONE 0
227 #define AO_LED_GREEN 1
230 /* Turn on the specified LEDs */
232 ao_led_on(uint8_t colors);
234 /* Turn off the specified LEDs */
236 ao_led_off(uint8_t colors);
238 /* Set all of the LEDs to the specified state */
240 ao_led_set(uint8_t colors);
242 /* Turn on the specified LEDs for the indicated interval */
244 ao_led_for(uint8_t colors, uint16_t ticks);
246 /* Initialize the LEDs */
254 /* Put one character to the USB output queue */
256 ao_usb_putchar(uint8_t c);
258 /* Get one character from the USB input queue */
260 ao_usb_getchar(void);
262 /* Flush the USB output queue */
266 /* USB interrupt handler */
268 ao_usb_isr(void) interrupt 6;
270 /* Initialize the USB system */
280 ao_cmd_lex_error = 1,
281 ao_cmd_syntax_error = 2,
284 extern __xdata uint16_t ao_cmd_lex_i;
285 extern __xdata uint8_t ao_cmd_lex_c;
286 extern __xdata enum ao_cmd_status ao_cmd_status;
292 ao_cmd_put8(uint8_t v);
295 ao_cmd_put16(uint16_t v);
310 ao_cmd_register(__code struct ao_cmds *cmds);
319 /* Allocate a DMA channel. the 'done' parameter will be set to 1
320 * when the dma is finished and will be used to wakeup any waiters
323 ao_dma_alloc(__xdata uint8_t * done);
325 /* Setup a DMA channel */
327 ao_dma_set_transfer(uint8_t id,
328 void __xdata *srcaddr,
329 void __xdata *dstaddr,
334 /* Start a DMA channel */
336 ao_dma_start(uint8_t id);
338 /* Manually trigger a DMA channel */
340 ao_dma_trigger(uint8_t id);
342 /* Abort a running DMA transfer */
344 ao_dma_abort(uint8_t id);
346 /* DMA interrupt routine */
348 ao_dma_isr(void) interrupt 8;
355 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
358 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
365 * We reserve the last block on the device for
366 * configuration space. Writes and reads in this
367 * area return errors.
370 #define AO_EE_BLOCK_SIZE ((uint16_t) (256))
371 #define AO_EE_DEVICE_SIZE ((uint32_t) 128 * (uint32_t) 1024)
372 #define AO_EE_DATA_SIZE (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
373 #define AO_EE_CONFIG_BLOCK ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
376 ao_ee_flush(void) __reentrant;
378 /* Write to the eeprom */
380 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
382 /* Read from the eeprom */
384 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
386 /* Write the config block (at the end of the eeprom) */
388 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
390 /* Read the config block (at the end of the eeprom) */
392 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
394 /* Initialize the EEPROM code */
402 /* Structure containing GPS position, either lat or lon */
407 uint16_t minutes_fraction; /* in units of 1/10000 minutes */
411 * The data log is recorded in the eeprom as a sequence
414 * Each packet starts with a 4-byte header that has the
415 * packet type, the packet checksum and the tick count. Then
416 * they all contain 2 16 bit values which hold packet-specific
419 * For each flight, the first packet
420 * is FLIGHT packet, indicating the serial number of the
421 * device and a unique number marking the number of flights
422 * recorded by this device.
424 * During flight, data from the accelerometer and barometer
425 * are recorded in SENSOR packets, using the raw 16-bit values
426 * read from the A/D converter.
428 * Also during flight, but at a lower rate, the deployment
429 * sensors are recorded in DEPLOY packets. The goal here is to
430 * detect failure in the deployment circuits.
432 * STATE packets hold state transitions as the flight computer
433 * transitions through different stages of the flight.
435 #define AO_LOG_FLIGHT 'F'
436 #define AO_LOG_SENSOR 'A'
437 #define AO_LOG_TEMP_VOLT 'T'
438 #define AO_LOG_DEPLOY 'D'
439 #define AO_LOG_STATE 'S'
440 #define AO_LOG_GPS_TIME 'G'
441 #define AO_LOG_GPS_LAT 'N'
442 #define AO_LOG_GPS_LON 'W'
443 #define AO_LOG_GPS_ALT 'H'
445 #define AO_LOG_POS_NONE (~0UL)
447 struct ao_log_record {
478 struct ao_gps_pos gps_latitude;
479 struct ao_gps_pos gps_longitude;
491 /* Write a record to the eeprom log */
493 ao_log_data(struct ao_log_record *log);
500 * ao_log_dump_first() - get first log record
501 * ao_log_dump_next() - get next log record
503 extern __xdata struct ao_log_record ao_log_dump;
505 /* Retrieve first log record for the current flight */
507 ao_log_dump_first(void);
509 /* return next log record for the current flight */
511 ao_log_dump_next(void);
513 /* Logging thread main routine */
517 /* Start logging to eeprom */
525 /* Initialize the logging system */
533 enum ao_flight_state {
534 ao_flight_startup = 0,
536 ao_flight_launchpad = 2,
539 ao_flight_apogee = 5,
540 ao_flight_drogue = 6,
542 ao_flight_landed = 8,
543 ao_flight_invalid = 9
546 extern __xdata struct ao_adc ao_flight_data;
547 extern __pdata enum flight_state ao_flight_state;
548 extern __pdata uint16_t ao_flight_tick;
549 extern __pdata int16_t ao_flight_accel;
550 extern __pdata int16_t ao_flight_pres;
551 extern __pdata int16_t ao_ground_pres;
552 extern __pdata int16_t ao_ground_accel;
553 extern __pdata int16_t ao_min_pres;
554 extern __pdata uint16_t ao_launch_time;
560 /* Initialize flight thread */
562 ao_flight_init(void);
569 ao_report_init(void);
574 * Given raw data, convert to SI units
577 /* pressure from the sensor to altitude in meters */
579 ao_pres_to_altitude(int16_t pres) __reentrant;
582 ao_temp_to_dC(int16_t temp) __reentrant;
585 ao_accel_to_cm_per_s2(int16_t accel) __reentrant;
590 * debug another telemetrum board
593 /* Send a byte to the dbg target */
595 ao_dbg_send_byte(uint8_t byte);
597 /* Receive a byte from the dbg target */
599 ao_dbg_recv_byte(void);
601 /* Start a bulk transfer to/from dbg target memory */
603 ao_dbg_start_transfer(uint16_t addr);
605 /* End a bulk transfer to/from dbg target memory */
607 ao_dbg_end_transfer(void);
609 /* Write a byte to dbg target memory */
611 ao_dbg_write_byte(uint8_t byte);
613 /* Read a byte from dbg target memory */
615 ao_dbg_read_byte(void);
617 /* Enable dbg mode, switching use of the pins */
619 ao_dbg_debug_mode(void);
621 /* Reset the dbg target */
632 #if !AO_NO_SERIAL_ISR
634 ao_serial_rx1_isr(void) interrupt 3;
637 ao_serial_tx1_isr(void) interrupt 14;
641 ao_serial_getchar(void) __critical;
644 ao_serial_putchar(uint8_t c) __critical;
647 ao_serial_init(void);
653 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
654 #define AO_GPS_NUM_SAT_SHIFT (0)
656 #define AO_GPS_VALID (1 << 4)
657 #define AO_GPS_LONGITUDE_MASK (1 << 5)
658 #define AO_GPS_LONGITUDE_EAST (0 << 5)
659 #define AO_GPS_LONGITUDE_WEST (1 << 5)
661 #define AO_GPS_LATITUDE_MASK (1 << 6)
662 #define AO_GPS_LATITUDE_NORTH (0 << 6)
663 #define AO_GPS_LATITUDE_SOUTH (1 << 6)
670 struct ao_gps_pos latitude;
671 struct ao_gps_pos longitude;
675 extern __xdata uint8_t ao_gps_mutex;
676 extern __xdata struct ao_gps_data ao_gps_data;
682 ao_gps_print(__xdata struct ao_gps_data *gps_data);
691 struct ao_telemetry {
693 uint8_t flight_state;
695 struct ao_gps_data gps;
699 ao_telemetry_init(void);
706 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant;
708 struct ao_radio_recv {
709 struct ao_telemetry telemetry;
715 ao_radio_recv(__xdata struct ao_radio_recv *recv) __reentrant;
728 ao_monitor_init(void);