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 0x62
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 */
88 /* Stop the operating system, beeping and blinking the reason */
90 ao_panic(uint8_t reason);
96 /* Our timer runs at 100Hz */
97 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
98 #define AO_SEC_TO_TICKS(s) ((s) * 100)
100 /* Returns the current time in ticks */
104 /* Suspend the current task until ticks time has passed */
106 ao_delay(uint16_t ticks);
108 /* Timer interrupt */
110 ao_timer_isr(void) interrupt 9;
112 /* Initialize the timer */
120 #define AO_ADC_RING 128
123 * One set of samples read from the A/D converter
126 uint16_t tick; /* tick when the sample was read */
127 int16_t accel; /* accelerometer */
128 int16_t pres; /* pressure sensor */
129 int16_t temp; /* temperature sensor */
130 int16_t v_batt; /* battery voltage */
131 int16_t sense_d; /* drogue continuity sense */
132 int16_t sense_m; /* main continuity sense */
136 * A/D data is stored in a ring, with the next sample to be written
139 extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING];
140 extern volatile __data uint8_t ao_adc_head;
142 /* Trigger a conversion sequence (called from the timer interrupt) */
146 /* Suspend the current task until another A/D sample is converted */
150 /* Get a copy of the last complete A/D sample set */
152 ao_adc_get(__xdata struct ao_adc *packet);
154 /* The A/D interrupt handler */
156 ao_adc_isr(void) interrupt 1;
158 /* Initialize the A/D converter */
167 * Various pre-defined beep frequencies
169 * frequency = 1/2 (24e6/32) / beep
172 #define AO_BEEP_LOW 150 /* 2500Hz */
173 #define AO_BEEP_MID 94 /* 3989Hz */
174 #define AO_BEEP_HIGH 75 /* 5000Hz */
175 #define AO_BEEP_OFF 0 /* off */
177 #define AO_BEEP_g 240 /* 1562.5Hz */
178 #define AO_BEEP_gs 227 /* 1652Hz (1655Hz) */
179 #define AO_BEEP_aa 214 /* 1752Hz (1754Hz) */
180 #define AO_BEEP_bbf 202 /* 1856Hz (1858Hz) */
181 #define AO_BEEP_bb 190 /* 1974Hz (1969Hz) */
182 #define AO_BEEP_cc 180 /* 2083Hz (2086Hz) */
183 #define AO_BEEP_ccs 170 /* 2205Hz (2210Hz) */
184 #define AO_BEEP_dd 160 /* 2344Hz (2341Hz) */
185 #define AO_BEEP_eef 151 /* 2483Hz (2480Hz) */
186 #define AO_BEEP_ee 143 /* 2622Hz (2628Hz) */
187 #define AO_BEEP_ff 135 /* 2778Hz (2784Hz) */
188 #define AO_BEEP_ffs 127 /* 2953Hz (2950Hz) */
189 #define AO_BEEP_gg 120 /* 3125Hz */
190 #define AO_BEEP_ggs 113 /* 3319Hz (3311Hz) */
191 #define AO_BEEP_aaa 107 /* 3504Hz (3508Hz) */
192 #define AO_BEEP_bbbf 101 /* 3713Hz (3716Hz) */
193 #define AO_BEEP_bbb 95 /* 3947Hz (3937Hz) */
194 #define AO_BEEP_ccc 90 /* 4167Hz (4171Hz) */
195 #define AO_BEEP_cccs 85 /* 4412Hz (4419Hz) */
196 #define AO_BEEP_ddd 80 /* 4688Hz (4682Hz) */
197 #define AO_BEEP_eeef 76 /* 4934Hz (4961Hz) */
198 #define AO_BEEP_eee 71 /* 5282Hz (5256Hz) */
199 #define AO_BEEP_fff 67 /* 5597Hz (5568Hz) */
200 #define AO_BEEP_fffs 64 /* 5859Hz (5899Hz) */
201 #define AO_BEEP_ggg 60 /* 6250Hz */
203 /* Set the beeper to the specified tone */
205 ao_beep(uint8_t beep);
207 /* Turn on the beeper for the specified time */
209 ao_beep_for(uint8_t beep, uint16_t ticks);
211 /* Initialize the beeper */
219 #define AO_LED_NONE 0
220 #define AO_LED_GREEN 1
223 /* Turn on the specified LEDs */
225 ao_led_on(uint8_t colors);
227 /* Turn off the specified LEDs */
229 ao_led_off(uint8_t colors);
231 /* Set all of the LEDs to the specified state */
233 ao_led_set(uint8_t colors);
235 /* Turn on the specified LEDs for the indicated interval */
237 ao_led_for(uint8_t colors, uint16_t ticks);
239 /* Initialize the LEDs */
247 /* Put one character to the USB output queue */
249 ao_usb_putchar(uint8_t c);
251 /* Get one character from the USB input queue */
253 ao_usb_getchar(void);
255 /* Flush the USB output queue */
259 /* USB interrupt handler */
261 ao_usb_isr(void) interrupt 6;
263 /* Initialize the USB system */
277 /* Allocate a DMA channel. the 'done' parameter will be set to 1
278 * when the dma is finished and will be used to wakeup any waiters
281 ao_dma_alloc(__xdata uint8_t * done);
283 /* Setup a DMA channel */
285 ao_dma_set_transfer(uint8_t id,
286 void __xdata *srcaddr,
287 void __xdata *dstaddr,
292 /* Start a DMA channel */
294 ao_dma_start(uint8_t id);
296 /* Manually trigger a DMA channel */
298 ao_dma_trigger(uint8_t id);
300 /* Abort a running DMA transfer */
302 ao_dma_abort(uint8_t id);
304 /* DMA interrupt routine */
306 ao_dma_isr(void) interrupt 8;
313 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
316 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
323 * We reserve the last block on the device for
324 * configuration space. Writes and reads in this
325 * area return errors.
328 #define AO_EE_BLOCK_SIZE ((uint16_t) (256))
329 #define AO_EE_DEVICE_SIZE ((uint32_t) 128 * (uint32_t) 1024)
330 #define AO_EE_DATA_SIZE (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
331 #define AO_EE_CONFIG_BLOCK ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
334 ao_ee_flush(void) __reentrant;
336 /* Write to the eeprom */
338 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
340 /* Read from the eeprom */
342 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
344 /* Write the config block (at the end of the eeprom) */
346 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
348 /* Read the config block (at the end of the eeprom) */
350 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
352 /* Initialize the EEPROM code */
361 * The data log is recorded in the eeprom as a sequence
364 * Each packet starts with a 4-byte header that has the
365 * packet type, the packet checksum and the tick count. Then
366 * they all contain 2 16 bit values which hold packet-specific
369 * For each flight, the first packet
370 * is FLIGHT packet, indicating the serial number of the
371 * device and a unique number marking the number of flights
372 * recorded by this device.
374 * During flight, data from the accelerometer and barometer
375 * are recorded in SENSOR packets, using the raw 16-bit values
376 * read from the A/D converter.
378 * Also during flight, but at a lower rate, the deployment
379 * sensors are recorded in DEPLOY packets. The goal here is to
380 * detect failure in the deployment circuits.
382 * STATE packets hold state transitions as the flight computer
383 * transitions through different stages of the flight.
385 #define AO_LOG_FLIGHT 'F'
386 #define AO_LOG_SENSOR 'A'
387 #define AO_LOG_TEMP_VOLT 'T'
388 #define AO_LOG_DEPLOY 'D'
389 #define AO_LOG_STATE 'S'
391 #define AO_LOG_POS_NONE (~0UL)
393 struct ao_log_record {
425 /* Write a record to the eeprom log */
427 ao_log_data(struct ao_log_record *log);
434 * ao_log_dump_first() - get first log record
435 * ao_log_dump_next() - get next log record
437 extern __xdata struct ao_log_record ao_log_dump;
439 /* Retrieve first log record for the current flight */
441 ao_log_dump_first(void);
443 /* return next log record for the current flight */
445 ao_log_dump_next(void);
447 /* Logging thread main routine */
451 /* Start logging to eeprom */
459 /* Initialize the logging system */
467 enum ao_flight_state {
480 extern __xdata struct ao_adc ao_flight_data;
481 extern __pdata enum flight_state ao_flight_state;
482 extern __pdata uint16_t ao_flight_tick;
483 extern __pdata int16_t ao_flight_accel;
484 extern __pdata int16_t ao_flight_pres;
485 extern __pdata int16_t ao_ground_pres;
486 extern __pdata int16_t ao_ground_accel;
487 extern __pdata int16_t ao_min_pres;
488 extern __pdata uint16_t ao_launch_time;
494 /* Initialize flight thread */
496 ao_flight_init(void);
503 ao_report_notify(void);
506 ao_report_init(void);
511 * Given raw data, convert to SI units
514 /* pressure from the sensor to altitude in meters */
516 ao_pres_to_altitude(int16_t pres) __reentrant;
519 ao_temp_to_dC(int16_t temp) __reentrant;
522 ao_accel_to_dm_per_s2(int16_t accel)
524 return (998 - (accel >> 4)) * 3300 / 2047;
530 * debug another telemetrum board
533 /* Send a byte to the dbg target */
535 ao_dbg_send_byte(uint8_t byte);
537 /* Receive a byte from the dbg target */
539 ao_dbg_recv_byte(void);
541 /* Start a bulk transfer to/from dbg target memory */
543 ao_dbg_start_transfer(uint16_t addr);
545 /* End a bulk transfer to/from dbg target memory */
547 ao_dbg_end_transfer(void);
549 /* Write a byte to dbg target memory */
551 ao_dbg_write_byte(uint8_t byte);
553 /* Read a byte from dbg target memory */
555 ao_dbg_read_byte(void);
557 /* Enable dbg mode, switching use of the pins */
559 ao_dbg_debug_mode(void);
561 /* Reset the dbg target */
570 ao_serial_rx1_isr(void) interrupt 3;
573 ao_serial_tx1_isr(void) interrupt 14;
576 ao_serial_getchar(void) __critical;
579 ao_serial_putchar(uint8_t c) __critical;
582 ao_serial_init(void);
591 uint16_t minutes_fraction; /* in units of 1/10000 minutes */
594 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
595 #define AO_GPS_NUM_SAT_SHIFT (0)
597 #define AO_GPS_VALID (1 << 4)
598 #define AO_GPS_LONGITUDE_MASK (1 << 5)
599 #define AO_GPS_LONGITUDE_EAST (0 << 5)
600 #define AO_GPS_LONGITUDE_WEST (1 << 5)
602 #define AO_GPS_LATITUDE_MASK (1 << 6)
603 #define AO_GPS_LATITUDE_NORTH (0 << 6)
604 #define AO_GPS_LATITUDE_SOUTH (1 << 6)
611 struct ao_gps_pos latitude;
612 struct ao_gps_pos longitude;
616 extern __xdata uint8_t ao_gps_mutex;
617 extern __xdata struct ao_gps_data ao_gps_data;