Add gps, debug dongle support and pressure alt tables
[fw/altos] / ao.h
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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
7  *
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.
12  *
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.
16  */
17
18 #ifndef _AO_H_
19 #define _AO_H_
20
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "cc1111.h"
25
26 /* Convert a __data pointer into an __xdata pointer */
27 #define DATA_TO_XDATA(a)        ((void __xdata *) ((uint8_t) (a) | 0xff00))
28
29 /* Stack runs from above the allocated __data space to 0xfe, which avoids
30  * writing to 0xff as that triggers the stack overflow indicator
31  */
32 #define AO_STACK_START  0x6f
33 #define AO_STACK_END    0xfe
34 #define AO_STACK_SIZE   (AO_STACK_END - AO_STACK_START + 1)
35
36 /* An AltOS task */
37 struct ao_task {
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         uint8_t stack[AO_STACK_SIZE];   /* saved stack */
42 };
43
44 extern __xdata struct ao_task *__data ao_cur_task;
45
46 #define AO_NUM_TASKS            10      /* maximum number of tasks */
47 #define AO_NO_TASK              0       /* no task id */
48
49 /*
50  ao_task.c
51  */
52
53 /* Suspend the current task until wchan is awoken */
54 int
55 ao_sleep(__xdata void *wchan);
56
57 /* Wake all tasks sleeping on wchan */
58 int
59 ao_wakeup(__xdata void *wchan);
60
61 /* Yield the processor to another task */
62 void
63 ao_yield(void) _naked;
64
65 /* Add a task to the run queue */
66 void
67 ao_add_task(__xdata struct ao_task * task, void (*start)(void));
68
69 /* Start the scheduler. This will not return */
70 void
71 ao_start_scheduler(void);
72
73 /*
74  * ao_panic.c
75  */
76
77 #define AO_PANIC_NO_TASK        1       /* AO_NUM_TASKS is not large enough */
78 #define AO_PANIC_DMA            2       /* Attempt to start DMA while active */
79 #define AO_PANIC_MUTEX          3       /* Mis-using mutex API */
80 #define AO_PANIC_EE             4       /* Mis-using eeprom API */
81 #define AO_PANIC_LOG            5       /* Failing to read/write log data */
82
83 /* Stop the operating system, beeping and blinking the reason */
84 void
85 ao_panic(uint8_t reason);
86
87 /*
88  * ao_timer.c
89  */
90
91 /* Our timer runs at 100Hz */
92 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
93 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
94
95 /* Returns the current time in ticks */
96 uint16_t
97 ao_time(void);
98
99 /* Suspend the current task until ticks time has passed */
100 void
101 ao_delay(uint16_t ticks);
102
103 /* Timer interrupt */
104 void
105 ao_timer_isr(void) interrupt 9;
106
107 /* Initialize the timer */
108 void
109 ao_timer_init(void);
110
111 /*
112  * ao_adc.c
113  */
114
115 #define AO_ADC_RING     128
116
117 /*
118  * One set of samples read from the A/D converter
119  */
120 struct ao_adc {
121         uint16_t        tick;           /* tick when the sample was read */
122         int16_t         accel;          /* accelerometer */
123         int16_t         pres;           /* pressure sensor */
124         int16_t         temp;           /* temperature sensor */
125         int16_t         v_batt;         /* battery voltage */
126         int16_t         sense_d;        /* drogue continuity sense */
127         int16_t         sense_m;        /* main continuity sense */
128 };
129
130 /*
131  * A/D data is stored in a ring, with the next sample to be written
132  * at ao_adc_head
133  */
134 extern volatile __xdata struct ao_adc   ao_adc_ring[AO_ADC_RING];
135 extern volatile __data uint8_t          ao_adc_head;
136
137 /* Trigger a conversion sequence (called from the timer interrupt) */
138 void
139 ao_adc_poll(void);
140  
141 /* Suspend the current task until another A/D sample is converted */
142 void
143 ao_adc_sleep(void);
144
145 /* Get a copy of the last complete A/D sample set */
146 void
147 ao_adc_get(__xdata struct ao_adc *packet);
148
149 /* The A/D interrupt handler */
150 void
151 ao_adc_isr(void) interrupt 1;
152
153 /* Initialize the A/D converter */
154 void
155 ao_adc_init(void);
156
157 /*
158  * ao_beep.c
159  */
160
161 /*
162  * Various pre-defined beep frequencies
163  *
164  * frequency = 1/2 (24e6/32) / beep
165  */
166
167 #define AO_BEEP_LOW     150     /* 2500Hz */
168 #define AO_BEEP_MID     94      /* 3989Hz */
169 #define AO_BEEP_HIGH    75      /* 5000Hz */
170 #define AO_BEEP_OFF     0       /* off */
171
172 #define AO_BEEP_g       240     /* 1562.5Hz */
173 #define AO_BEEP_gs      227     /* 1652Hz (1655Hz) */
174 #define AO_BEEP_aa      214     /* 1752Hz (1754Hz) */
175 #define AO_BEEP_bbf     202     /* 1856Hz (1858Hz) */
176 #define AO_BEEP_bb      190     /* 1974Hz (1969Hz) */
177 #define AO_BEEP_cc      180     /* 2083Hz (2086Hz) */
178 #define AO_BEEP_ccs     170     /* 2205Hz (2210Hz) */
179 #define AO_BEEP_dd      160     /* 2344Hz (2341Hz) */
180 #define AO_BEEP_eef     151     /* 2483Hz (2480Hz) */
181 #define AO_BEEP_ee      143     /* 2622Hz (2628Hz) */
182 #define AO_BEEP_ff      135     /* 2778Hz (2784Hz) */
183 #define AO_BEEP_ffs     127     /* 2953Hz (2950Hz) */
184 #define AO_BEEP_gg      120     /* 3125Hz */
185 #define AO_BEEP_ggs     113     /* 3319Hz (3311Hz) */
186 #define AO_BEEP_aaa     107     /* 3504Hz (3508Hz) */
187 #define AO_BEEP_bbbf    101     /* 3713Hz (3716Hz) */
188 #define AO_BEEP_bbb     95      /* 3947Hz (3937Hz) */
189 #define AO_BEEP_ccc     90      /* 4167Hz (4171Hz) */
190 #define AO_BEEP_cccs    85      /* 4412Hz (4419Hz) */
191 #define AO_BEEP_ddd     80      /* 4688Hz (4682Hz) */
192 #define AO_BEEP_eeef    76      /* 4934Hz (4961Hz) */
193 #define AO_BEEP_eee     71      /* 5282Hz (5256Hz) */
194 #define AO_BEEP_fff     67      /* 5597Hz (5568Hz) */
195 #define AO_BEEP_fffs    64      /* 5859Hz (5899Hz) */
196 #define AO_BEEP_ggg     60      /* 6250Hz */
197
198 /* Set the beeper to the specified tone */
199 void
200 ao_beep(uint8_t beep);
201
202 /* Turn on the beeper for the specified time */
203 void
204 ao_beep_for(uint8_t beep, uint16_t ticks);
205
206 /* Initialize the beeper */
207 void
208 ao_beep_init(void);
209
210 /*
211  * ao_led.c
212  */
213
214 #define AO_LED_NONE     0
215 #define AO_LED_GREEN    1
216 #define AO_LED_RED      2
217
218 /* Turn on the specified LEDs */
219 void
220 ao_led_on(uint8_t colors);
221
222 /* Turn off the specified LEDs */
223 void
224 ao_led_off(uint8_t colors);
225
226 /* Set all of the LEDs to the specified state */
227 void
228 ao_led_set(uint8_t colors);
229
230 /* Turn on the specified LEDs for the indicated interval */
231 void
232 ao_led_for(uint8_t colors, uint16_t ticks);
233
234 /* Initialize the LEDs */
235 void
236 ao_led_init(void);
237
238 /*
239  * ao_usb.c
240  */
241
242 /* Put one character to the USB output queue */
243 void
244 ao_usb_putchar(uint8_t c);
245
246 /* Get one character from the USB input queue */
247 uint8_t
248 ao_usb_getchar(void);
249
250 /* Flush the USB output queue */
251 void
252 ao_usb_flush(void);
253
254 /* USB interrupt handler */
255 void
256 ao_usb_isr(void) interrupt 6;
257
258 /* Initialize the USB system */
259 void
260 ao_usb_init(void);
261
262 /*
263  * ao_cmd.c
264  */
265 void
266 ao_cmd_init(void);
267
268 /*
269  * ao_dma.c
270  */
271
272 /* Allocate a DMA channel. the 'done' parameter will be set to 1
273  * when the dma is finished and will be used to wakeup any waiters 
274  */
275 uint8_t
276 ao_dma_alloc(__xdata uint8_t * done);
277
278 /* Setup a DMA channel */
279 void
280 ao_dma_set_transfer(uint8_t id,
281                     void __xdata *srcaddr,
282                     void __xdata *dstaddr,
283                     uint16_t count,
284                     uint8_t cfg0,
285                     uint8_t cfg1);
286
287 /* Start a DMA channel */
288 void
289 ao_dma_start(uint8_t id);
290
291 /* Manually trigger a DMA channel */
292 void
293 ao_dma_trigger(uint8_t id);
294
295 /* Abort a running DMA transfer */
296 void
297 ao_dma_abort(uint8_t id);
298
299 /* DMA interrupt routine */
300 void
301 ao_dma_isr(void) interrupt 8;
302
303 /*
304  * ao_mutex.c
305  */
306
307 void
308 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
309
310 void
311 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
312
313 /*
314  * ao_ee.c
315  */
316
317 /*
318  * We reserve the last block on the device for
319  * configuration space. Writes and reads in this
320  * area return errors.
321  */
322
323 #define AO_EE_BLOCK_SIZE        ((uint16_t) (256))
324 #define AO_EE_DEVICE_SIZE       ((uint32_t) 128 * (uint32_t) 1024)
325 #define AO_EE_DATA_SIZE         (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
326 #define AO_EE_CONFIG_BLOCK      ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
327
328 void
329 ao_ee_flush(void) __reentrant;
330
331 /* Write to the eeprom */
332 uint8_t
333 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
334
335 /* Read from the eeprom */
336 uint8_t
337 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
338
339 /* Write the config block (at the end of the eeprom) */
340 uint8_t
341 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
342
343 /* Read the config block (at the end of the eeprom) */
344 uint8_t
345 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
346
347 /* Initialize the EEPROM code */
348 void
349 ao_ee_init(void);
350
351 /*
352  * ao_log.c
353  */
354
355 /*
356  * The data log is recorded in the eeprom as a sequence
357  * of data packets.
358  *
359  * Each packet starts with a 4-byte header that has the
360  * packet type, the packet checksum and the tick count. Then
361  * they all contain 2 16 bit values which hold packet-specific
362  * data.
363  * 
364  * For each flight, the first packet
365  * is FLIGHT packet, indicating the serial number of the
366  * device and a unique number marking the number of flights
367  * recorded by this device.
368  *
369  * During flight, data from the accelerometer and barometer
370  * are recorded in SENSOR packets, using the raw 16-bit values
371  * read from the A/D converter.
372  *
373  * Also during flight, but at a lower rate, the deployment
374  * sensors are recorded in DEPLOY packets. The goal here is to
375  * detect failure in the deployment circuits.
376  *
377  * STATE packets hold state transitions as the flight computer
378  * transitions through different stages of the flight.
379  */
380 #define AO_LOG_FLIGHT           'F'
381 #define AO_LOG_SENSOR           'A'
382 #define AO_LOG_TEMP_VOLT        'T'
383 #define AO_LOG_DEPLOY           'D'
384 #define AO_LOG_STATE            'S'
385
386 #define AO_LOG_POS_NONE         (~0UL)
387
388 struct ao_log_record {
389         uint8_t                 type;
390         uint8_t                 csum;
391         uint16_t                tick;
392         union {
393                 struct {
394                         uint16_t        serial;
395                         uint16_t        flight;
396                 } flight;
397                 struct {
398                         int16_t         accel;
399                         int16_t         pres;
400                 } sensor;
401                 struct {
402                         int16_t         temp;
403                         int16_t         v_batt;
404                 } temp_volt;
405                 struct {
406                         int16_t         drogue;
407                         int16_t         main;
408                 } deploy;
409                 struct {
410                         uint16_t        state;
411                         uint16_t        reason;
412                 } state;
413                 struct {
414                         uint16_t        d0;
415                         uint16_t        d1;
416                 } anon;
417         } u;
418 };
419
420 /* Write a record to the eeprom log */
421 void
422 ao_log_data(struct ao_log_record *log);
423
424 /* Flush the log */
425 void
426 ao_log_flush(void);
427
428 /* Log dumping API:
429  * ao_log_dump_first() - get first log record
430  * ao_log_dump_next()  - get next log record
431  */
432 extern __xdata struct ao_log_record ao_log_dump;
433
434 /* Retrieve first log record for the current flight */
435 uint8_t
436 ao_log_dump_first(void);
437
438 /* return next log record for the current flight */
439 uint8_t
440 ao_log_dump_next(void);
441
442 /* Logging thread main routine */
443 void
444 ao_log(void);
445
446 /* Start logging to eeprom */
447 void
448 ao_log_start(void);
449
450 /* Stop logging */
451 void
452 ao_log_stop(void);
453
454 /* Initialize the logging system */
455 void
456 ao_log_init(void);
457
458 /*
459  * ao_flight.c
460  */
461
462 enum ao_flight_state {
463         ao_flight_startup,
464         ao_flight_idle,
465         ao_flight_launchpad,
466         ao_flight_boost,
467         ao_flight_coast,
468         ao_flight_apogee,
469         ao_flight_drogue,
470         ao_flight_main,
471         ao_flight_landed,
472         ao_flight_invalid
473 };
474
475 extern __xdata struct ao_adc    ao_flight_data;
476 extern __data enum flight_state ao_flight_state;
477 extern __data uint16_t                  ao_flight_tick;
478 extern __data int16_t                   ao_flight_accel;
479 extern __data int16_t                   ao_flight_pres;
480 extern __data int16_t                   ao_ground_pres;
481 extern __data int16_t                   ao_ground_accel;
482 extern __data int16_t                   ao_min_pres;
483 extern __data uint16_t                  ao_launch_time;
484
485 /* Flight thread */
486 void
487 ao_flight(void);
488
489 /* Initialize flight thread */
490 void
491 ao_flight_init(void);
492
493 /*
494  * ao_report.c
495  */
496
497 void
498 ao_report_notify(void);
499
500 void
501 ao_report_init(void);
502
503 /*
504  * ao_convert.c
505  *
506  * Given raw data, convert to SI units
507  */
508
509 /* pressure from the sensor to altitude in meters */
510 int16_t
511 ao_pres_to_altitude(int16_t pres) __reentrant;
512
513 int16_t
514 ao_temp_to_dC(int16_t temp) __reentrant;
515
516 int16_t
517 ao_accel_to_dm_per_s2(int16_t accel)
518 {
519         return (998 - (accel >> 4)) * 3300 / 2047;
520 }
521
522 /*
523  * ao_dbg.c
524  *
525  * debug another telemetrum board
526  */
527
528 /* Send a byte to the dbg target */
529 void
530 ao_dbg_send_byte(uint8_t byte);
531
532 /* Receive a byte from the dbg target */
533 uint8_t
534 ao_dbg_recv_byte(void);
535
536 /* Start a bulk transfer to/from dbg target memory */
537 void
538 ao_dbg_start_transfer(uint16_t addr);
539
540 /* End a bulk transfer to/from dbg target memory */
541 void
542 ao_dbg_end_transfer(void);
543
544 /* Write a byte to dbg target memory */
545 void
546 ao_dbg_write_byte(uint8_t byte);
547
548 /* Read a byte from dbg target memory */
549 uint8_t
550 ao_dbg_read_byte(void);
551
552 /* Enable dbg mode, switching use of the pins */
553 void
554 ao_dbg_debug_mode(void);
555
556 /* Reset the dbg target */
557 void
558 ao_dbg_reset(void);
559
560 /*
561  * ao_serial.c
562  */
563
564 void
565 ao_serial_rx1_isr(void) interrupt 3;
566
567 void
568 ao_serial_tx1_isr(void) interrupt 14;
569
570 uint8_t
571 ao_serial_getchar(void) __critical;
572
573 void
574 ao_serial_putchar(uint8_t c) __critical;
575
576 void
577 ao_serial_init(void);
578
579 /*
580  * ao_gps.c
581  */
582
583 void
584 ao_gps(void);
585
586 void
587 ao_gps_init(void);
588
589
590 #endif /* _AO_H_ */
591