Move ao_led_init to end of file to be consistent with other files
[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  0x7e
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 void
56 ao_sleep(__xdata void *wchan);
57
58 /* Wake all tasks sleeping on wchan */
59 void
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 #define AO_PANIC_CMD            6       /* Too many command sets registered */
88
89 /* Stop the operating system, beeping and blinking the reason */
90 void
91 ao_panic(uint8_t reason);
92
93 /*
94  * ao_timer.c
95  */
96
97 /* Our timer runs at 100Hz */
98 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
99 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
100
101 /* Returns the current time in ticks */
102 uint16_t
103 ao_time(void);
104
105 /* Suspend the current task until ticks time has passed */
106 void
107 ao_delay(uint16_t ticks);
108
109 /* Set the ADC interval */
110 void
111 ao_timer_set_adc_interval(uint8_t interval) __critical;
112
113 /* Timer interrupt */
114 void
115 ao_timer_isr(void) interrupt 9;
116
117 /* Initialize the timer */
118 void
119 ao_timer_init(void);
120
121 /*
122  * ao_adc.c
123  */
124
125 #define AO_ADC_RING     64
126 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
127 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
128
129 /*
130  * One set of samples read from the A/D converter
131  */
132 struct ao_adc {
133         uint16_t        tick;           /* tick when the sample was read */
134         int16_t         accel;          /* accelerometer */
135         int16_t         pres;           /* pressure sensor */
136         int16_t         temp;           /* temperature sensor */
137         int16_t         v_batt;         /* battery voltage */
138         int16_t         sense_d;        /* drogue continuity sense */
139         int16_t         sense_m;        /* main continuity sense */
140 };
141
142 /*
143  * A/D data is stored in a ring, with the next sample to be written
144  * at ao_adc_head
145  */
146 extern volatile __xdata struct ao_adc   ao_adc_ring[AO_ADC_RING];
147 extern volatile __data uint8_t          ao_adc_head;
148
149 /* Trigger a conversion sequence (called from the timer interrupt) */
150 void
151 ao_adc_poll(void);
152
153 /* Suspend the current task until another A/D sample is converted */
154 void
155 ao_adc_sleep(void);
156
157 /* Get a copy of the last complete A/D sample set */
158 void
159 ao_adc_get(__xdata struct ao_adc *packet);
160
161 /* The A/D interrupt handler */
162 #if !AO_NO_ADC_ISR
163 void
164 ao_adc_isr(void) interrupt 1;
165 #endif
166
167 /* Initialize the A/D converter */
168 void
169 ao_adc_init(void);
170
171 /*
172  * ao_beep.c
173  */
174
175 /*
176  * Various pre-defined beep frequencies
177  *
178  * frequency = 1/2 (24e6/32) / beep
179  */
180
181 #define AO_BEEP_LOW     150     /* 2500Hz */
182 #define AO_BEEP_MID     94      /* 3989Hz */
183 #define AO_BEEP_HIGH    75      /* 5000Hz */
184 #define AO_BEEP_OFF     0       /* off */
185
186 #define AO_BEEP_g       240     /* 1562.5Hz */
187 #define AO_BEEP_gs      227     /* 1652Hz (1655Hz) */
188 #define AO_BEEP_aa      214     /* 1752Hz (1754Hz) */
189 #define AO_BEEP_bbf     202     /* 1856Hz (1858Hz) */
190 #define AO_BEEP_bb      190     /* 1974Hz (1969Hz) */
191 #define AO_BEEP_cc      180     /* 2083Hz (2086Hz) */
192 #define AO_BEEP_ccs     170     /* 2205Hz (2210Hz) */
193 #define AO_BEEP_dd      160     /* 2344Hz (2341Hz) */
194 #define AO_BEEP_eef     151     /* 2483Hz (2480Hz) */
195 #define AO_BEEP_ee      143     /* 2622Hz (2628Hz) */
196 #define AO_BEEP_ff      135     /* 2778Hz (2784Hz) */
197 #define AO_BEEP_ffs     127     /* 2953Hz (2950Hz) */
198 #define AO_BEEP_gg      120     /* 3125Hz */
199 #define AO_BEEP_ggs     113     /* 3319Hz (3311Hz) */
200 #define AO_BEEP_aaa     107     /* 3504Hz (3508Hz) */
201 #define AO_BEEP_bbbf    101     /* 3713Hz (3716Hz) */
202 #define AO_BEEP_bbb     95      /* 3947Hz (3937Hz) */
203 #define AO_BEEP_ccc     90      /* 4167Hz (4171Hz) */
204 #define AO_BEEP_cccs    85      /* 4412Hz (4419Hz) */
205 #define AO_BEEP_ddd     80      /* 4688Hz (4682Hz) */
206 #define AO_BEEP_eeef    76      /* 4934Hz (4961Hz) */
207 #define AO_BEEP_eee     71      /* 5282Hz (5256Hz) */
208 #define AO_BEEP_fff     67      /* 5597Hz (5568Hz) */
209 #define AO_BEEP_fffs    64      /* 5859Hz (5899Hz) */
210 #define AO_BEEP_ggg     60      /* 6250Hz */
211
212 /* Set the beeper to the specified tone */
213 void
214 ao_beep(uint8_t beep);
215
216 /* Turn on the beeper for the specified time */
217 void
218 ao_beep_for(uint8_t beep, uint16_t ticks);
219
220 /* Initialize the beeper */
221 void
222 ao_beep_init(void);
223
224 /*
225  * ao_led.c
226  */
227
228 #define AO_LED_NONE     0
229 #define AO_LED_GREEN    1
230 #define AO_LED_RED      2
231
232 /* Turn on the specified LEDs */
233 void
234 ao_led_on(uint8_t colors);
235
236 /* Turn off the specified LEDs */
237 void
238 ao_led_off(uint8_t colors);
239
240 /* Set all of the LEDs to the specified state */
241 void
242 ao_led_set(uint8_t colors);
243
244 /* Turn on the specified LEDs for the indicated interval */
245 void
246 ao_led_for(uint8_t colors, uint16_t ticks);
247
248 /* Initialize the LEDs */
249 void
250 ao_led_init(void);
251
252 /*
253  * ao_usb.c
254  */
255
256 /* Put one character to the USB output queue */
257 void
258 ao_usb_putchar(uint8_t c);
259
260 /* Get one character from the USB input queue */
261 uint8_t
262 ao_usb_getchar(void);
263
264 /* Flush the USB output queue */
265 void
266 ao_usb_flush(void);
267
268 /* USB interrupt handler */
269 void
270 ao_usb_isr(void) interrupt 6;
271
272 /* Initialize the USB system */
273 void
274 ao_usb_init(void);
275
276 /*
277  * ao_cmd.c
278  */
279
280 enum ao_cmd_status {
281         ao_cmd_success = 0,
282         ao_cmd_lex_error = 1,
283         ao_cmd_syntax_error = 2,
284 };
285
286 extern __xdata uint16_t ao_cmd_lex_i;
287 extern __xdata uint8_t  ao_cmd_lex_c;
288 extern __xdata enum ao_cmd_status ao_cmd_status;
289
290 void
291 ao_cmd_lex(void);
292
293 void
294 ao_cmd_put8(uint8_t v);
295
296 void
297 ao_cmd_put16(uint16_t v);
298
299 void
300 ao_cmd_white(void);
301
302 void
303 ao_cmd_hex(void);
304
305 struct ao_cmds {
306         uint8_t         cmd;
307         void            (*func)(void);
308         const char      *help;
309 };
310
311 void
312 ao_cmd_register(__code struct ao_cmds *cmds);
313
314 void
315 ao_cmd_init(void);
316
317 /*
318  * ao_dma.c
319  */
320
321 /* Allocate a DMA channel. the 'done' parameter will be set to 1
322  * when the dma is finished and will be used to wakeup any waiters
323  */
324 uint8_t
325 ao_dma_alloc(__xdata uint8_t * done);
326
327 /* Setup a DMA channel */
328 void
329 ao_dma_set_transfer(uint8_t id,
330                     void __xdata *srcaddr,
331                     void __xdata *dstaddr,
332                     uint16_t count,
333                     uint8_t cfg0,
334                     uint8_t cfg1);
335
336 /* Start a DMA channel */
337 void
338 ao_dma_start(uint8_t id);
339
340 /* Manually trigger a DMA channel */
341 void
342 ao_dma_trigger(uint8_t id);
343
344 /* Abort a running DMA transfer */
345 void
346 ao_dma_abort(uint8_t id);
347
348 /* DMA interrupt routine */
349 void
350 ao_dma_isr(void) interrupt 8;
351
352 /*
353  * ao_mutex.c
354  */
355
356 void
357 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
358
359 void
360 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
361
362 /*
363  * ao_ee.c
364  */
365
366 /*
367  * We reserve the last block on the device for
368  * configuration space. Writes and reads in this
369  * area return errors.
370  */
371
372 #define AO_EE_BLOCK_SIZE        ((uint16_t) (256))
373 #define AO_EE_DEVICE_SIZE       ((uint32_t) 128 * (uint32_t) 1024)
374 #define AO_EE_DATA_SIZE         (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
375 #define AO_EE_CONFIG_BLOCK      ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
376
377 void
378 ao_ee_flush(void) __reentrant;
379
380 /* Write to the eeprom */
381 uint8_t
382 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
383
384 /* Read from the eeprom */
385 uint8_t
386 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
387
388 /* Write the config block (at the end of the eeprom) */
389 uint8_t
390 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
391
392 /* Read the config block (at the end of the eeprom) */
393 uint8_t
394 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
395
396 /* Initialize the EEPROM code */
397 void
398 ao_ee_init(void);
399
400 /*
401  * ao_log.c
402  */
403
404 /* Structure containing GPS position, either lat or lon */
405
406 struct ao_gps_pos {
407         uint8_t degrees;
408         uint8_t minutes;
409         uint16_t minutes_fraction;      /* in units of 1/10000 minutes */
410 };
411
412 /*
413  * The data log is recorded in the eeprom as a sequence
414  * of data packets.
415  *
416  * Each packet starts with a 4-byte header that has the
417  * packet type, the packet checksum and the tick count. Then
418  * they all contain 2 16 bit values which hold packet-specific
419  * data.
420  *
421  * For each flight, the first packet
422  * is FLIGHT packet, indicating the serial number of the
423  * device and a unique number marking the number of flights
424  * recorded by this device.
425  *
426  * During flight, data from the accelerometer and barometer
427  * are recorded in SENSOR packets, using the raw 16-bit values
428  * read from the A/D converter.
429  *
430  * Also during flight, but at a lower rate, the deployment
431  * sensors are recorded in DEPLOY packets. The goal here is to
432  * detect failure in the deployment circuits.
433  *
434  * STATE packets hold state transitions as the flight computer
435  * transitions through different stages of the flight.
436  */
437 #define AO_LOG_FLIGHT           'F'
438 #define AO_LOG_SENSOR           'A'
439 #define AO_LOG_TEMP_VOLT        'T'
440 #define AO_LOG_DEPLOY           'D'
441 #define AO_LOG_STATE            'S'
442 #define AO_LOG_GPS_TIME         'G'
443 #define AO_LOG_GPS_LAT          'N'
444 #define AO_LOG_GPS_LON          'W'
445 #define AO_LOG_GPS_ALT          'H'
446
447 #define AO_LOG_POS_NONE         (~0UL)
448
449 struct ao_log_record {
450         uint8_t                 type;
451         uint8_t                 csum;
452         uint16_t                tick;
453         union {
454                 struct {
455                         uint16_t        serial;
456                         uint16_t        flight;
457                 } flight;
458                 struct {
459                         int16_t         accel;
460                         int16_t         pres;
461                 } sensor;
462                 struct {
463                         int16_t         temp;
464                         int16_t         v_batt;
465                 } temp_volt;
466                 struct {
467                         int16_t         drogue;
468                         int16_t         main;
469                 } deploy;
470                 struct {
471                         uint16_t        state;
472                         uint16_t        reason;
473                 } state;
474                 struct {
475                         uint8_t         hour;
476                         uint8_t         minute;
477                         uint8_t         second;
478                         uint8_t         flags;
479                 } gps_time;
480                 struct ao_gps_pos gps_latitude;
481                 struct ao_gps_pos gps_longitude;
482                 struct {
483                         int16_t         altitude;
484                         uint16_t        unused;
485                 } gps_altitude;
486                 struct {
487                         uint16_t        d0;
488                         uint16_t        d1;
489                 } anon;
490         } u;
491 };
492
493 /* Write a record to the eeprom log */
494 void
495 ao_log_data(struct ao_log_record *log);
496
497 /* Flush the log */
498 void
499 ao_log_flush(void);
500
501 /* Log dumping API:
502  * ao_log_dump_first() - get first log record
503  * ao_log_dump_next()  - get next log record
504  */
505 extern __xdata struct ao_log_record ao_log_dump;
506
507 /* Retrieve first log record for the current flight */
508 uint8_t
509 ao_log_dump_first(void);
510
511 /* return next log record for the current flight */
512 uint8_t
513 ao_log_dump_next(void);
514
515 /* Logging thread main routine */
516 void
517 ao_log(void);
518
519 /* Start logging to eeprom */
520 void
521 ao_log_start(void);
522
523 /* Stop logging */
524 void
525 ao_log_stop(void);
526
527 /* Initialize the logging system */
528 void
529 ao_log_init(void);
530
531 /*
532  * ao_flight.c
533  */
534
535 enum ao_flight_state {
536         ao_flight_startup = 0,
537         ao_flight_idle = 1,
538         ao_flight_launchpad = 2,
539         ao_flight_boost = 3,
540         ao_flight_coast = 4,
541         ao_flight_apogee = 5,
542         ao_flight_drogue = 6,
543         ao_flight_main = 7,
544         ao_flight_landed = 8,
545         ao_flight_invalid = 9
546 };
547
548 extern __xdata struct ao_adc            ao_flight_data;
549 extern __pdata enum ao_flight_state     ao_flight_state;
550 extern __pdata uint16_t                 ao_flight_tick;
551 extern __pdata int16_t                  ao_flight_accel;
552 extern __pdata int16_t                  ao_flight_pres;
553 extern __pdata int16_t                  ao_ground_pres;
554 extern __pdata int16_t                  ao_ground_accel;
555 extern __pdata int16_t                  ao_min_pres;
556 extern __pdata uint16_t                 ao_launch_time;
557
558 /* Flight thread */
559 void
560 ao_flight(void);
561
562 /* Initialize flight thread */
563 void
564 ao_flight_init(void);
565
566 /*
567  * ao_report.c
568  */
569
570 void
571 ao_report_init(void);
572
573 /*
574  * ao_convert.c
575  *
576  * Given raw data, convert to SI units
577  */
578
579 /* pressure from the sensor to altitude in meters */
580 int16_t
581 ao_pres_to_altitude(int16_t pres) __reentrant;
582
583 int16_t
584 ao_temp_to_dC(int16_t temp) __reentrant;
585
586 int16_t
587 ao_accel_to_cm_per_s2(int16_t accel) __reentrant;
588
589 /*
590  * ao_dbg.c
591  *
592  * debug another telemetrum board
593  */
594
595 /* Send a byte to the dbg target */
596 void
597 ao_dbg_send_byte(uint8_t byte);
598
599 /* Receive a byte from the dbg target */
600 uint8_t
601 ao_dbg_recv_byte(void);
602
603 /* Start a bulk transfer to/from dbg target memory */
604 void
605 ao_dbg_start_transfer(uint16_t addr);
606
607 /* End a bulk transfer to/from dbg target memory */
608 void
609 ao_dbg_end_transfer(void);
610
611 /* Write a byte to dbg target memory */
612 void
613 ao_dbg_write_byte(uint8_t byte);
614
615 /* Read a byte from dbg target memory */
616 uint8_t
617 ao_dbg_read_byte(void);
618
619 /* Enable dbg mode, switching use of the pins */
620 void
621 ao_dbg_debug_mode(void);
622
623 /* Reset the dbg target */
624 void
625 ao_dbg_reset(void);
626
627 void
628 ao_dbg_init(void);
629
630 /*
631  * ao_serial.c
632  */
633
634 #if !AO_NO_SERIAL_ISR
635 void
636 ao_serial_rx1_isr(void) interrupt 3;
637
638 void
639 ao_serial_tx1_isr(void) interrupt 14;
640 #endif
641
642 uint8_t
643 ao_serial_getchar(void) __critical;
644
645 void
646 ao_serial_putchar(uint8_t c) __critical;
647
648 void
649 ao_serial_init(void);
650
651 /*
652  * ao_gps.c
653  */
654
655 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
656 #define AO_GPS_NUM_SAT_SHIFT    (0)
657
658 #define AO_GPS_VALID            (1 << 4)
659 #define AO_GPS_LONGITUDE_MASK   (1 << 5)
660 #define AO_GPS_LONGITUDE_EAST   (0 << 5)
661 #define AO_GPS_LONGITUDE_WEST   (1 << 5)
662
663 #define AO_GPS_LATITUDE_MASK    (1 << 6)
664 #define AO_GPS_LATITUDE_NORTH   (0 << 6)
665 #define AO_GPS_LATITUDE_SOUTH   (1 << 6)
666
667 struct ao_gps_data {
668         uint8_t                 hour;
669         uint8_t                 minute;
670         uint8_t                 second;
671         uint8_t                 flags;
672         struct ao_gps_pos       latitude;
673         struct ao_gps_pos       longitude;
674         int16_t                 altitude;
675 };
676
677 extern __xdata uint8_t ao_gps_mutex;
678 extern __xdata struct ao_gps_data ao_gps_data;
679
680 void
681 ao_gps(void);
682
683 void
684 ao_gps_print(__xdata struct ao_gps_data *gps_data);
685
686 void
687 ao_gps_init(void);
688
689 /*
690  * ao_telemetry.c
691  */
692
693 struct ao_telemetry {
694         uint8_t                 addr;
695         uint8_t                 flight_state;
696         struct ao_adc           adc;
697         struct ao_gps_data      gps;
698 };
699
700 void
701 ao_telemetry_init(void);
702
703 /*
704  * ao_radio.c
705  */
706
707 void
708 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant;
709
710 struct ao_radio_recv {
711         struct ao_telemetry     telemetry;
712         uint8_t                 rssi;
713         uint8_t                 status;
714 };
715
716 void
717 ao_radio_recv(__xdata struct ao_radio_recv *recv) __reentrant;
718
719 void
720 ao_radio_init(void);
721
722 /*
723  * ao_monitor.c
724  */
725
726 extern const char const * const ao_state_names[];
727
728 void
729 ao_monitor(void);
730
731 void
732 ao_monitor_init(void);
733
734 /*
735  * ao_stdio.c
736  */
737
738 void
739 flush(void);
740
741 /*
742  * ao_ignite.c
743  */
744
745 enum ao_igniter {
746         ao_igniter_drogue = 0,
747         ao_igniter_main = 1
748 };
749
750 void
751 ao_ignite(enum ao_igniter igniter);
752
753 enum ao_igniter_status {
754         ao_igniter_unknown,     /* unknown status (ambiguous voltage) */
755         ao_igniter_ready,       /* continuity detected */
756         ao_igniter_active,      /* igniter firing */
757         ao_igniter_open,        /* open circuit detected */
758 };
759
760 enum ao_igniter_status
761 ao_igniter_status(enum ao_igniter igniter);
762
763 void
764 ao_igniter_init(void);
765
766 #endif /* _AO_H_ */
767