Add task names and 'T' command to show task status.
[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  0x62
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         __code char *name;              /* task name */
42         uint8_t stack[AO_STACK_SIZE];   /* saved stack */
43 };
44
45 extern __xdata struct ao_task *__data ao_cur_task;
46
47 #define AO_NUM_TASKS            10      /* maximum number of tasks */
48 #define AO_NO_TASK              0       /* no task id */
49
50 /*
51  ao_task.c
52  */
53
54 /* Suspend the current task until wchan is awoken */
55 int
56 ao_sleep(__xdata void *wchan);
57
58 /* Wake all tasks sleeping on wchan */
59 int
60 ao_wakeup(__xdata void *wchan);
61
62 /* Yield the processor to another task */
63 void
64 ao_yield(void) _naked;
65
66 /* Add a task to the run queue */
67 void
68 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name);
69
70 /* Dump task info to console */
71 void
72 ao_task_info(void);
73
74 /* Start the scheduler. This will not return */
75 void
76 ao_start_scheduler(void);
77
78 /*
79  * ao_panic.c
80  */
81
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
88 /* Stop the operating system, beeping and blinking the reason */
89 void
90 ao_panic(uint8_t reason);
91
92 /*
93  * ao_timer.c
94  */
95
96 /* Our timer runs at 100Hz */
97 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
98 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
99
100 /* Returns the current time in ticks */
101 uint16_t
102 ao_time(void);
103
104 /* Suspend the current task until ticks time has passed */
105 void
106 ao_delay(uint16_t ticks);
107
108 /* Timer interrupt */
109 void
110 ao_timer_isr(void) interrupt 9;
111
112 /* Initialize the timer */
113 void
114 ao_timer_init(void);
115
116 /*
117  * ao_adc.c
118  */
119
120 #define AO_ADC_RING     128
121
122 /*
123  * One set of samples read from the A/D converter
124  */
125 struct ao_adc {
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 */
133 };
134
135 /*
136  * A/D data is stored in a ring, with the next sample to be written
137  * at ao_adc_head
138  */
139 extern volatile __xdata struct ao_adc   ao_adc_ring[AO_ADC_RING];
140 extern volatile __data uint8_t          ao_adc_head;
141
142 /* Trigger a conversion sequence (called from the timer interrupt) */
143 void
144 ao_adc_poll(void);
145  
146 /* Suspend the current task until another A/D sample is converted */
147 void
148 ao_adc_sleep(void);
149
150 /* Get a copy of the last complete A/D sample set */
151 void
152 ao_adc_get(__xdata struct ao_adc *packet);
153
154 /* The A/D interrupt handler */
155 void
156 ao_adc_isr(void) interrupt 1;
157
158 /* Initialize the A/D converter */
159 void
160 ao_adc_init(void);
161
162 /*
163  * ao_beep.c
164  */
165
166 /*
167  * Various pre-defined beep frequencies
168  *
169  * frequency = 1/2 (24e6/32) / beep
170  */
171
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 */
176
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 */
202
203 /* Set the beeper to the specified tone */
204 void
205 ao_beep(uint8_t beep);
206
207 /* Turn on the beeper for the specified time */
208 void
209 ao_beep_for(uint8_t beep, uint16_t ticks);
210
211 /* Initialize the beeper */
212 void
213 ao_beep_init(void);
214
215 /*
216  * ao_led.c
217  */
218
219 #define AO_LED_NONE     0
220 #define AO_LED_GREEN    1
221 #define AO_LED_RED      2
222
223 /* Turn on the specified LEDs */
224 void
225 ao_led_on(uint8_t colors);
226
227 /* Turn off the specified LEDs */
228 void
229 ao_led_off(uint8_t colors);
230
231 /* Set all of the LEDs to the specified state */
232 void
233 ao_led_set(uint8_t colors);
234
235 /* Turn on the specified LEDs for the indicated interval */
236 void
237 ao_led_for(uint8_t colors, uint16_t ticks);
238
239 /* Initialize the LEDs */
240 void
241 ao_led_init(void);
242
243 /*
244  * ao_usb.c
245  */
246
247 /* Put one character to the USB output queue */
248 void
249 ao_usb_putchar(uint8_t c);
250
251 /* Get one character from the USB input queue */
252 uint8_t
253 ao_usb_getchar(void);
254
255 /* Flush the USB output queue */
256 void
257 ao_usb_flush(void);
258
259 /* USB interrupt handler */
260 void
261 ao_usb_isr(void) interrupt 6;
262
263 /* Initialize the USB system */
264 void
265 ao_usb_init(void);
266
267 /*
268  * ao_cmd.c
269  */
270 void
271 ao_cmd_init(void);
272
273 /*
274  * ao_dma.c
275  */
276
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 
279  */
280 uint8_t
281 ao_dma_alloc(__xdata uint8_t * done);
282
283 /* Setup a DMA channel */
284 void
285 ao_dma_set_transfer(uint8_t id,
286                     void __xdata *srcaddr,
287                     void __xdata *dstaddr,
288                     uint16_t count,
289                     uint8_t cfg0,
290                     uint8_t cfg1);
291
292 /* Start a DMA channel */
293 void
294 ao_dma_start(uint8_t id);
295
296 /* Manually trigger a DMA channel */
297 void
298 ao_dma_trigger(uint8_t id);
299
300 /* Abort a running DMA transfer */
301 void
302 ao_dma_abort(uint8_t id);
303
304 /* DMA interrupt routine */
305 void
306 ao_dma_isr(void) interrupt 8;
307
308 /*
309  * ao_mutex.c
310  */
311
312 void
313 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
314
315 void
316 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
317
318 /*
319  * ao_ee.c
320  */
321
322 /*
323  * We reserve the last block on the device for
324  * configuration space. Writes and reads in this
325  * area return errors.
326  */
327
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))
332
333 void
334 ao_ee_flush(void) __reentrant;
335
336 /* Write to the eeprom */
337 uint8_t
338 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
339
340 /* Read from the eeprom */
341 uint8_t
342 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
343
344 /* Write the config block (at the end of the eeprom) */
345 uint8_t
346 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
347
348 /* Read the config block (at the end of the eeprom) */
349 uint8_t
350 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
351
352 /* Initialize the EEPROM code */
353 void
354 ao_ee_init(void);
355
356 /*
357  * ao_log.c
358  */
359
360 /*
361  * The data log is recorded in the eeprom as a sequence
362  * of data packets.
363  *
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
367  * data.
368  * 
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.
373  *
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.
377  *
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.
381  *
382  * STATE packets hold state transitions as the flight computer
383  * transitions through different stages of the flight.
384  */
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'
390
391 #define AO_LOG_POS_NONE         (~0UL)
392
393 struct ao_log_record {
394         uint8_t                 type;
395         uint8_t                 csum;
396         uint16_t                tick;
397         union {
398                 struct {
399                         uint16_t        serial;
400                         uint16_t        flight;
401                 } flight;
402                 struct {
403                         int16_t         accel;
404                         int16_t         pres;
405                 } sensor;
406                 struct {
407                         int16_t         temp;
408                         int16_t         v_batt;
409                 } temp_volt;
410                 struct {
411                         int16_t         drogue;
412                         int16_t         main;
413                 } deploy;
414                 struct {
415                         uint16_t        state;
416                         uint16_t        reason;
417                 } state;
418                 struct {
419                         uint16_t        d0;
420                         uint16_t        d1;
421                 } anon;
422         } u;
423 };
424
425 /* Write a record to the eeprom log */
426 void
427 ao_log_data(struct ao_log_record *log);
428
429 /* Flush the log */
430 void
431 ao_log_flush(void);
432
433 /* Log dumping API:
434  * ao_log_dump_first() - get first log record
435  * ao_log_dump_next()  - get next log record
436  */
437 extern __xdata struct ao_log_record ao_log_dump;
438
439 /* Retrieve first log record for the current flight */
440 uint8_t
441 ao_log_dump_first(void);
442
443 /* return next log record for the current flight */
444 uint8_t
445 ao_log_dump_next(void);
446
447 /* Logging thread main routine */
448 void
449 ao_log(void);
450
451 /* Start logging to eeprom */
452 void
453 ao_log_start(void);
454
455 /* Stop logging */
456 void
457 ao_log_stop(void);
458
459 /* Initialize the logging system */
460 void
461 ao_log_init(void);
462
463 /*
464  * ao_flight.c
465  */
466
467 enum ao_flight_state {
468         ao_flight_startup,
469         ao_flight_idle,
470         ao_flight_launchpad,
471         ao_flight_boost,
472         ao_flight_coast,
473         ao_flight_apogee,
474         ao_flight_drogue,
475         ao_flight_main,
476         ao_flight_landed,
477         ao_flight_invalid
478 };
479
480 extern __xdata struct ao_adc    ao_flight_data;
481 extern __data enum flight_state ao_flight_state;
482 extern __data uint16_t                  ao_flight_tick;
483 extern __data int16_t                   ao_flight_accel;
484 extern __data int16_t                   ao_flight_pres;
485 extern __data int16_t                   ao_ground_pres;
486 extern __data int16_t                   ao_ground_accel;
487 extern __data int16_t                   ao_min_pres;
488 extern __data uint16_t                  ao_launch_time;
489
490 /* Flight thread */
491 void
492 ao_flight(void);
493
494 /* Initialize flight thread */
495 void
496 ao_flight_init(void);
497
498 /*
499  * ao_report.c
500  */
501
502 void
503 ao_report_notify(void);
504
505 void
506 ao_report_init(void);
507
508 /*
509  * ao_convert.c
510  *
511  * Given raw data, convert to SI units
512  */
513
514 /* pressure from the sensor to altitude in meters */
515 int16_t
516 ao_pres_to_altitude(int16_t pres) __reentrant;
517
518 int16_t
519 ao_temp_to_dC(int16_t temp) __reentrant;
520
521 int16_t
522 ao_accel_to_dm_per_s2(int16_t accel)
523 {
524         return (998 - (accel >> 4)) * 3300 / 2047;
525 }
526
527 /*
528  * ao_dbg.c
529  *
530  * debug another telemetrum board
531  */
532
533 /* Send a byte to the dbg target */
534 void
535 ao_dbg_send_byte(uint8_t byte);
536
537 /* Receive a byte from the dbg target */
538 uint8_t
539 ao_dbg_recv_byte(void);
540
541 /* Start a bulk transfer to/from dbg target memory */
542 void
543 ao_dbg_start_transfer(uint16_t addr);
544
545 /* End a bulk transfer to/from dbg target memory */
546 void
547 ao_dbg_end_transfer(void);
548
549 /* Write a byte to dbg target memory */
550 void
551 ao_dbg_write_byte(uint8_t byte);
552
553 /* Read a byte from dbg target memory */
554 uint8_t
555 ao_dbg_read_byte(void);
556
557 /* Enable dbg mode, switching use of the pins */
558 void
559 ao_dbg_debug_mode(void);
560
561 /* Reset the dbg target */
562 void
563 ao_dbg_reset(void);
564
565 /*
566  * ao_serial.c
567  */
568
569 void
570 ao_serial_rx1_isr(void) interrupt 3;
571
572 void
573 ao_serial_tx1_isr(void) interrupt 14;
574
575 uint8_t
576 ao_serial_getchar(void) __critical;
577
578 void
579 ao_serial_putchar(uint8_t c) __critical;
580
581 void
582 ao_serial_init(void);
583
584 /*
585  * ao_gps.c
586  */
587
588 void
589 ao_gps(void);
590
591 void
592 ao_gps_init(void);
593
594
595 #endif /* _AO_H_ */
596