Add optional 's' command to packet slave to enable/disable slave mode
[fw/altos] / src / 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 <stddef.h>
25 #include "cc1111.h"
26 #include "ao_pins.h"
27
28 #define TRUE 1
29 #define FALSE 0
30
31 /* Convert a __data pointer into an __xdata pointer */
32 #define DATA_TO_XDATA(a)        ((void __xdata *) ((uint8_t) (a) | 0xff00))
33
34 /* Stack runs from above the allocated __data space to 0xfe, which avoids
35  * writing to 0xff as that triggers the stack overflow indicator
36  */
37 #define AO_STACK_START  0x80
38 #define AO_STACK_END    0xfe
39 #define AO_STACK_SIZE   (AO_STACK_END - AO_STACK_START + 1)
40
41 /* An AltOS task */
42 struct ao_task {
43         __xdata void *wchan;            /* current wait channel (NULL if running) */
44         uint16_t alarm;                 /* abort ao_sleep time */
45         uint8_t stack_count;            /* amount of saved stack */
46         uint8_t task_id;                /* unique id */
47         __code char *name;              /* task name */
48         uint8_t stack[AO_STACK_SIZE];   /* saved stack */
49 };
50
51 extern __xdata struct ao_task *__data ao_cur_task;
52
53 #define AO_NUM_TASKS            16      /* maximum number of tasks */
54 #define AO_NO_TASK              0       /* no task id */
55
56 /*
57  ao_task.c
58  */
59
60 /* Suspend the current task until wchan is awoken.
61  * returns:
62  *  0 on normal wake
63  *  1 on alarm
64  */
65 uint8_t
66 ao_sleep(__xdata void *wchan);
67
68 /* Wake all tasks sleeping on wchan */
69 void
70 ao_wakeup(__xdata void *wchan);
71
72 /* Wake up a specific task */
73 void
74 ao_wake_task(__xdata struct ao_task *task);
75
76 /* set an alarm to go off in 'delay' ticks */
77 void
78 ao_alarm(uint16_t delay);
79
80 /* Yield the processor to another task */
81 void
82 ao_yield(void) _naked;
83
84 /* Add a task to the run queue */
85 void
86 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
87
88 /* Terminate the current task */
89 void
90 ao_exit(void);
91
92 /* Dump task info to console */
93 void
94 ao_task_info(void);
95
96 /* Start the scheduler. This will not return */
97 void
98 ao_start_scheduler(void);
99
100 /*
101  * ao_panic.c
102  */
103
104 #define AO_PANIC_NO_TASK        1       /* AO_NUM_TASKS is not large enough */
105 #define AO_PANIC_DMA            2       /* Attempt to start DMA while active */
106 #define AO_PANIC_MUTEX          3       /* Mis-using mutex API */
107 #define AO_PANIC_EE             4       /* Mis-using eeprom API */
108 #define AO_PANIC_LOG            5       /* Failing to read/write log data */
109 #define AO_PANIC_CMD            6       /* Too many command sets registered */
110 #define AO_PANIC_STDIO          7       /* Too many stdio handlers registered */
111 #define AO_PANIC_REBOOT         8       /* Reboot failed */
112 #define AO_PANIC_FLASH          9       /* Invalid flash part (or wrong blocksize) */
113
114 /* Stop the operating system, beeping and blinking the reason */
115 void
116 ao_panic(uint8_t reason);
117
118 /*
119  * ao_timer.c
120  */
121
122 /* Our timer runs at 100Hz */
123 #define AO_HERTZ                100
124 #define AO_MS_TO_TICKS(ms)      ((ms) / (1000 / AO_HERTZ))
125 #define AO_SEC_TO_TICKS(s)      ((s) * AO_HERTZ)
126
127 /* Returns the current time in ticks */
128 uint16_t
129 ao_time(void);
130
131 /* Suspend the current task until ticks time has passed */
132 void
133 ao_delay(uint16_t ticks);
134
135 /* Set the ADC interval */
136 void
137 ao_timer_set_adc_interval(uint8_t interval) __critical;
138
139 /* Timer interrupt */
140 void
141 ao_timer_isr(void) interrupt 9;
142
143 /* Initialize the timer */
144 void
145 ao_timer_init(void);
146
147 /* Initialize the hardware clock. Must be called first */
148 void
149 ao_clock_init(void);
150
151 /*
152  * One set of samples read from the A/D converter or telemetry
153  */
154 struct ao_adc {
155         uint16_t        tick;           /* tick when the sample was read */
156         int16_t         accel;          /* accelerometer */
157         int16_t         pres;           /* pressure sensor */
158         int16_t         temp;           /* temperature sensor */
159         int16_t         v_batt;         /* battery voltage */
160         int16_t         sense_d;        /* drogue continuity sense */
161         int16_t         sense_m;        /* main continuity sense */
162 };
163
164 #ifndef HAS_ADC
165 #error Please define HAS_ADC
166 #endif
167
168 #if HAS_ADC
169 /*
170  * ao_adc.c
171  */
172
173 #define AO_ADC_RING     32
174 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
175 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
176
177
178 /*
179  * A/D data is stored in a ring, with the next sample to be written
180  * at ao_adc_head
181  */
182 extern volatile __xdata struct ao_adc   ao_adc_ring[AO_ADC_RING];
183 extern volatile __data uint8_t          ao_adc_head;
184
185 /* Trigger a conversion sequence (called from the timer interrupt) */
186 void
187 ao_adc_poll(void);
188
189 /* Suspend the current task until another A/D sample is converted */
190 void
191 ao_adc_sleep(void);
192
193 /* Get a copy of the last complete A/D sample set */
194 void
195 ao_adc_get(__xdata struct ao_adc *packet);
196
197 /* The A/D interrupt handler */
198
199 void
200 ao_adc_isr(void) interrupt 1;
201
202 /* Initialize the A/D converter */
203 void
204 ao_adc_init(void);
205
206 #endif /* HAS_ADC */
207
208 /*
209  * ao_beep.c
210  */
211
212 /*
213  * Various pre-defined beep frequencies
214  *
215  * frequency = 1/2 (24e6/32) / beep
216  */
217
218 #define AO_BEEP_LOW     150     /* 2500Hz */
219 #define AO_BEEP_MID     94      /* 3989Hz */
220 #define AO_BEEP_HIGH    75      /* 5000Hz */
221 #define AO_BEEP_OFF     0       /* off */
222
223 #define AO_BEEP_g       240     /* 1562.5Hz */
224 #define AO_BEEP_gs      227     /* 1652Hz (1655Hz) */
225 #define AO_BEEP_aa      214     /* 1752Hz (1754Hz) */
226 #define AO_BEEP_bbf     202     /* 1856Hz (1858Hz) */
227 #define AO_BEEP_bb      190     /* 1974Hz (1969Hz) */
228 #define AO_BEEP_cc      180     /* 2083Hz (2086Hz) */
229 #define AO_BEEP_ccs     170     /* 2205Hz (2210Hz) */
230 #define AO_BEEP_dd      160     /* 2344Hz (2341Hz) */
231 #define AO_BEEP_eef     151     /* 2483Hz (2480Hz) */
232 #define AO_BEEP_ee      143     /* 2622Hz (2628Hz) */
233 #define AO_BEEP_ff      135     /* 2778Hz (2784Hz) */
234 #define AO_BEEP_ffs     127     /* 2953Hz (2950Hz) */
235 #define AO_BEEP_gg      120     /* 3125Hz */
236 #define AO_BEEP_ggs     113     /* 3319Hz (3311Hz) */
237 #define AO_BEEP_aaa     107     /* 3504Hz (3508Hz) */
238 #define AO_BEEP_bbbf    101     /* 3713Hz (3716Hz) */
239 #define AO_BEEP_bbb     95      /* 3947Hz (3937Hz) */
240 #define AO_BEEP_ccc     90      /* 4167Hz (4171Hz) */
241 #define AO_BEEP_cccs    85      /* 4412Hz (4419Hz) */
242 #define AO_BEEP_ddd     80      /* 4688Hz (4682Hz) */
243 #define AO_BEEP_eeef    76      /* 4934Hz (4961Hz) */
244 #define AO_BEEP_eee     71      /* 5282Hz (5256Hz) */
245 #define AO_BEEP_fff     67      /* 5597Hz (5568Hz) */
246 #define AO_BEEP_fffs    64      /* 5859Hz (5899Hz) */
247 #define AO_BEEP_ggg     60      /* 6250Hz */
248
249 /* Set the beeper to the specified tone */
250 void
251 ao_beep(uint8_t beep);
252
253 /* Turn on the beeper for the specified time */
254 void
255 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant;
256
257 /* Initialize the beeper */
258 void
259 ao_beep_init(void);
260
261 /*
262  * ao_led.c
263  */
264
265 #define AO_LED_NONE     0
266
267 /* Turn on the specified LEDs */
268 void
269 ao_led_on(uint8_t colors);
270
271 /* Turn off the specified LEDs */
272 void
273 ao_led_off(uint8_t colors);
274
275 /* Set all of the LEDs to the specified state */
276 void
277 ao_led_set(uint8_t colors);
278
279 /* Toggle the specified LEDs */
280 void
281 ao_led_toggle(uint8_t colors);
282
283 /* Turn on the specified LEDs for the indicated interval */
284 void
285 ao_led_for(uint8_t colors, uint16_t ticks) __reentrant;
286
287 /* Initialize the LEDs */
288 void
289 ao_led_init(uint8_t enable);
290
291 /*
292  * ao_usb.c
293  */
294
295 /* Put one character to the USB output queue */
296 void
297 ao_usb_putchar(char c);
298
299 /* Get one character from the USB input queue */
300 char
301 ao_usb_getchar(void);
302
303 /* Poll for a charcter on the USB input queue.
304  * returns AO_READ_AGAIN if none are available
305  */
306 char
307 ao_usb_pollchar(void);
308
309 /* Flush the USB output queue */
310 void
311 ao_usb_flush(void);
312
313 /* USB interrupt handler */
314 void
315 ao_usb_isr(void) interrupt 6;
316
317 /* Enable the USB controller */
318 void
319 ao_usb_enable(void);
320
321 /* Disable the USB controller */
322 void
323 ao_usb_disable(void);
324
325 /* Initialize the USB system */
326 void
327 ao_usb_init(void);
328
329 /*
330  * ao_cmd.c
331  */
332
333 enum ao_cmd_status {
334         ao_cmd_success = 0,
335         ao_cmd_lex_error = 1,
336         ao_cmd_syntax_error = 2,
337 };
338
339 extern __xdata uint16_t ao_cmd_lex_i;
340 extern __xdata uint32_t ao_cmd_lex_u32;
341 extern __xdata char     ao_cmd_lex_c;
342 extern __xdata enum ao_cmd_status ao_cmd_status;
343
344 void
345 ao_cmd_lex(void);
346
347 void
348 ao_cmd_put8(uint8_t v);
349
350 void
351 ao_cmd_put16(uint16_t v);
352
353 void
354 ao_cmd_white(void);
355
356 void
357 ao_cmd_hex(void);
358
359 void
360 ao_cmd_decimal(void);
361
362 uint8_t
363 ao_match_word(__code char *word);
364
365 struct ao_cmds {
366         char            cmd;
367         void            (*func)(void);
368         const char      *help;
369 };
370
371 void
372 ao_cmd_register(__code struct ao_cmds *cmds);
373
374 void
375 ao_cmd_init(void);
376
377 /*
378  * ao_dma.c
379  */
380
381 /* Allocate a DMA channel. the 'done' parameter will be set
382  * when the dma is finished or aborted and will be used to
383  * wakeup any waiters
384  */
385
386 #define AO_DMA_DONE     1
387 #define AO_DMA_ABORTED  2
388
389 uint8_t
390 ao_dma_alloc(__xdata uint8_t * done);
391
392 /* Setup a DMA channel */
393 void
394 ao_dma_set_transfer(uint8_t id,
395                     void __xdata *srcaddr,
396                     void __xdata *dstaddr,
397                     uint16_t count,
398                     uint8_t cfg0,
399                     uint8_t cfg1);
400
401 /* Start a DMA channel */
402 void
403 ao_dma_start(uint8_t id);
404
405 /* Manually trigger a DMA channel */
406 void
407 ao_dma_trigger(uint8_t id);
408
409 /* Abort a running DMA transfer */
410 void
411 ao_dma_abort(uint8_t id);
412
413 /* DMA interrupt routine */
414 void
415 ao_dma_isr(void) interrupt 8;
416
417 /*
418  * ao_mutex.c
419  */
420
421 void
422 ao_mutex_get(__xdata uint8_t *ao_mutex) __reentrant;
423
424 void
425 ao_mutex_put(__xdata uint8_t *ao_mutex) __reentrant;
426
427 /*
428  * ao_ee.c
429  */
430
431 /*
432  * We reserve the last block on the device for
433  * configuration space. Writes and reads in this
434  * area return errors.
435  */
436
437 #define AO_EE_BLOCK_SIZE        ((uint16_t) (256))
438 #define AO_EE_DEVICE_SIZE       ((uint32_t) 128 * (uint32_t) 1024)
439 #define AO_EE_DATA_SIZE         (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
440 #define AO_EE_CONFIG_BLOCK      ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
441
442 void
443 ao_ee_flush(void) __reentrant;
444
445 /* Write to the eeprom */
446 uint8_t
447 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
448
449 /* Read from the eeprom */
450 uint8_t
451 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant;
452
453 /* Write the config block (at the end of the eeprom) */
454 uint8_t
455 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant;
456
457 /* Read the config block (at the end of the eeprom) */
458 uint8_t
459 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant;
460
461 /* Initialize the EEPROM code */
462 void
463 ao_ee_init(void);
464
465 /*
466  * ao_log.c
467  */
468
469 /*
470  * The data log is recorded in the eeprom as a sequence
471  * of data packets.
472  *
473  * Each packet starts with a 4-byte header that has the
474  * packet type, the packet checksum and the tick count. Then
475  * they all contain 2 16 bit values which hold packet-specific
476  * data.
477  *
478  * For each flight, the first packet
479  * is FLIGHT packet, indicating the serial number of the
480  * device and a unique number marking the number of flights
481  * recorded by this device.
482  *
483  * During flight, data from the accelerometer and barometer
484  * are recorded in SENSOR packets, using the raw 16-bit values
485  * read from the A/D converter.
486  *
487  * Also during flight, but at a lower rate, the deployment
488  * sensors are recorded in DEPLOY packets. The goal here is to
489  * detect failure in the deployment circuits.
490  *
491  * STATE packets hold state transitions as the flight computer
492  * transitions through different stages of the flight.
493  */
494 #define AO_LOG_FLIGHT           'F'
495 #define AO_LOG_SENSOR           'A'
496 #define AO_LOG_TEMP_VOLT        'T'
497 #define AO_LOG_DEPLOY           'D'
498 #define AO_LOG_STATE            'S'
499 #define AO_LOG_GPS_TIME         'G'
500 #define AO_LOG_GPS_LAT          'N'
501 #define AO_LOG_GPS_LON          'W'
502 #define AO_LOG_GPS_ALT          'H'
503 #define AO_LOG_GPS_SAT          'V'
504 #define AO_LOG_GPS_DATE         'Y'
505
506 #define AO_LOG_POS_NONE         (~0UL)
507
508 struct ao_log_record {
509         char                    type;
510         uint8_t                 csum;
511         uint16_t                tick;
512         union {
513                 struct {
514                         int16_t         ground_accel;
515                         uint16_t        flight;
516                 } flight;
517                 struct {
518                         int16_t         accel;
519                         int16_t         pres;
520                 } sensor;
521                 struct {
522                         int16_t         temp;
523                         int16_t         v_batt;
524                 } temp_volt;
525                 struct {
526                         int16_t         drogue;
527                         int16_t         main;
528                 } deploy;
529                 struct {
530                         uint16_t        state;
531                         uint16_t        reason;
532                 } state;
533                 struct {
534                         uint8_t         hour;
535                         uint8_t         minute;
536                         uint8_t         second;
537                         uint8_t         flags;
538                 } gps_time;
539                 int32_t         gps_latitude;
540                 int32_t         gps_longitude;
541                 struct {
542                         int16_t         altitude;
543                         uint16_t        unused;
544                 } gps_altitude;
545                 struct {
546                         uint16_t        svid;
547                         uint8_t         unused;
548                         uint8_t         c_n;
549                 } gps_sat;
550                 struct {
551                         uint8_t         year;
552                         uint8_t         month;
553                         uint8_t         day;
554                 } gps_date;
555                 struct {
556                         uint16_t        d0;
557                         uint16_t        d1;
558                 } anon;
559         } u;
560 };
561
562 /* Write a record to the eeprom log */
563 void
564 ao_log_data(struct ao_log_record *log);
565
566 /* Flush the log */
567 void
568 ao_log_flush(void);
569
570 /* We record flight numbers in the first record of
571  * the log. Tasks may wait for this to be initialized
572  * by sleeping on this variable.
573  */
574 extern __xdata uint16_t ao_flight_number;
575
576 /* Retrieve first log record for the current flight */
577 uint8_t
578 ao_log_dump_first(void);
579
580 /* return next log record for the current flight */
581 uint8_t
582 ao_log_dump_next(void);
583
584 /* Logging thread main routine */
585 void
586 ao_log(void);
587
588 /* Start logging to eeprom */
589 void
590 ao_log_start(void);
591
592 /* Stop logging */
593 void
594 ao_log_stop(void);
595
596 /* Initialize the logging system */
597 void
598 ao_log_init(void);
599
600 /*
601  * ao_flight.c
602  */
603
604 enum ao_flight_state {
605         ao_flight_startup = 0,
606         ao_flight_idle = 1,
607         ao_flight_pad = 2,
608         ao_flight_boost = 3,
609         ao_flight_fast = 4,
610         ao_flight_coast = 5,
611         ao_flight_drogue = 6,
612         ao_flight_main = 7,
613         ao_flight_landed = 8,
614         ao_flight_invalid = 9
615 };
616
617 extern __xdata struct ao_adc            ao_flight_data;
618 extern __pdata enum ao_flight_state     ao_flight_state;
619 extern __pdata uint16_t                 ao_flight_tick;
620 extern __pdata int16_t                  ao_flight_accel;
621 extern __pdata int16_t                  ao_flight_pres;
622 extern __pdata int32_t                  ao_flight_vel;
623 extern __pdata int16_t                  ao_ground_pres;
624 extern __pdata int16_t                  ao_ground_accel;
625 extern __pdata int16_t                  ao_min_pres;
626 extern __pdata uint16_t                 ao_launch_time;
627 extern __xdata uint8_t                  ao_flight_force_idle;
628
629 /* Flight thread */
630 void
631 ao_flight(void);
632
633 /* Initialize flight thread */
634 void
635 ao_flight_init(void);
636
637 /*
638  * ao_report.c
639  */
640
641 void
642 ao_report_init(void);
643
644 /*
645  * ao_convert.c
646  *
647  * Given raw data, convert to SI units
648  */
649
650 /* pressure from the sensor to altitude in meters */
651 int16_t
652 ao_pres_to_altitude(int16_t pres) __reentrant;
653
654 int16_t
655 ao_altitude_to_pres(int16_t alt) __reentrant;
656
657 int16_t
658 ao_temp_to_dC(int16_t temp) __reentrant;
659
660 /*
661  * ao_dbg.c
662  *
663  * debug another telemetrum board
664  */
665
666 /* Send a byte to the dbg target */
667 void
668 ao_dbg_send_byte(uint8_t byte);
669
670 /* Receive a byte from the dbg target */
671 uint8_t
672 ao_dbg_recv_byte(void);
673
674 /* Start a bulk transfer to/from dbg target memory */
675 void
676 ao_dbg_start_transfer(uint16_t addr);
677
678 /* End a bulk transfer to/from dbg target memory */
679 void
680 ao_dbg_end_transfer(void);
681
682 /* Write a byte to dbg target memory */
683 void
684 ao_dbg_write_byte(uint8_t byte);
685
686 /* Read a byte from dbg target memory */
687 uint8_t
688 ao_dbg_read_byte(void);
689
690 /* Enable dbg mode, switching use of the pins */
691 void
692 ao_dbg_debug_mode(void);
693
694 /* Reset the dbg target */
695 void
696 ao_dbg_reset(void);
697
698 void
699 ao_dbg_init(void);
700
701 /*
702  * ao_serial.c
703  */
704
705 #ifndef HAS_SERIAL_1
706 #error Please define HAS_SERIAL_1
707 #endif
708
709 #if HAS_SERIAL_1
710 void
711 ao_serial_rx1_isr(void) interrupt 3;
712
713 void
714 ao_serial_tx1_isr(void) interrupt 14;
715
716 char
717 ao_serial_getchar(void) __critical;
718
719 void
720 ao_serial_putchar(char c) __critical;
721
722 #define AO_SERIAL_SPEED_4800    0
723 #define AO_SERIAL_SPEED_9600    1
724 #define AO_SERIAL_SPEED_57600   2
725
726 void
727 ao_serial_set_speed(uint8_t speed);
728
729 void
730 ao_serial_init(void);
731 #endif
732
733 /*
734  * ao_gps.c
735  */
736
737 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
738 #define AO_GPS_NUM_SAT_SHIFT    (0)
739
740 #define AO_GPS_VALID            (1 << 4)
741 #define AO_GPS_RUNNING          (1 << 5)
742 #define AO_GPS_DATE_VALID       (1 << 6)
743
744 extern __xdata uint16_t ao_gps_tick;
745
746 struct ao_gps_data {
747         uint8_t                 year;
748         uint8_t                 month;
749         uint8_t                 day;
750         uint8_t                 hour;
751         uint8_t                 minute;
752         uint8_t                 second;
753         uint8_t                 flags;
754         int32_t                 latitude;       /* degrees * 10⁷ */
755         int32_t                 longitude;      /* degrees * 10⁷ */
756         int16_t                 altitude;       /* m */
757         uint16_t                ground_speed;   /* cm/s */
758         uint8_t                 course;         /* degrees / 2 */
759         uint8_t                 hdop;           /* * 5 */
760         int16_t                 climb_rate;     /* cm/s */
761         uint16_t                h_error;        /* m */
762         uint16_t                v_error;        /* m */
763 };
764
765 struct ao_gps_sat_data {
766         uint8_t         svid;
767         uint8_t         c_n_1;
768 };
769
770 struct ao_gps_tracking_data {
771         uint8_t                 channels;
772         struct ao_gps_sat_data  sats[12];
773 };
774
775 extern __xdata uint8_t ao_gps_mutex;
776 extern __xdata struct ao_gps_data ao_gps_data;
777 extern __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
778
779 void
780 ao_gps(void);
781
782 void
783 ao_gps_print(__xdata struct ao_gps_data *gps_data);
784
785 void
786 ao_gps_tracking_print(__xdata struct ao_gps_tracking_data *gps_tracking_data);
787
788 void
789 ao_gps_init(void);
790
791 /*
792  * ao_gps_report.c
793  */
794
795 void
796 ao_gps_report(void);
797
798 void
799 ao_gps_report_init(void);
800
801 /*
802  * ao_telemetry.c
803  */
804
805 #define AO_MAX_CALLSIGN         8
806 #define AO_TELEMETRY_VERSION    2
807
808 struct ao_telemetry {
809         uint8_t                 addr;
810         uint16_t                flight;
811         uint8_t                 flight_state;
812         int16_t                 flight_accel;
813         int16_t                 ground_accel;
814         int32_t                 flight_vel;
815         int16_t                 flight_pres;
816         int16_t                 ground_pres;
817         int16_t                 accel_plus_g;
818         int16_t                 accel_minus_g;
819         struct ao_adc           adc;
820         struct ao_gps_data      gps;
821         char                    callsign[AO_MAX_CALLSIGN];
822         struct ao_gps_tracking_data     gps_tracking;
823 };
824
825 /* Set delay between telemetry reports (0 to disable) */
826
827 #define AO_TELEMETRY_INTERVAL_PAD       AO_MS_TO_TICKS(1000)
828 #define AO_TELEMETRY_INTERVAL_FLIGHT    AO_MS_TO_TICKS(50)
829 #define AO_TELEMETRY_INTERVAL_RECOVER   AO_MS_TO_TICKS(1000)
830
831 void
832 ao_telemetry_set_interval(uint16_t interval);
833
834 void
835 ao_rdf_set(uint8_t rdf);
836
837 void
838 ao_telemetry_init(void);
839
840 /*
841  * ao_radio.c
842  */
843
844 extern __xdata uint8_t  ao_radio_dma;
845 extern __xdata uint8_t ao_radio_dma_done;
846 extern __xdata uint8_t ao_radio_done;
847 extern __xdata uint8_t ao_radio_mutex;
848
849 void
850 ao_radio_general_isr(void) interrupt 16;
851
852 void
853 ao_radio_get(void);
854
855 #define ao_radio_put() ao_mutex_put(&ao_radio_mutex)
856
857 void
858 ao_radio_set_telemetry(void);
859
860 void
861 ao_radio_set_packet(void);
862
863 void
864 ao_radio_set_rdf(void);
865
866 void
867 ao_radio_send(__xdata struct ao_telemetry *telemetry) __reentrant;
868
869 struct ao_radio_recv {
870         struct ao_telemetry     telemetry;
871         int8_t                  rssi;
872         uint8_t                 status;
873 };
874
875 uint8_t
876 ao_radio_recv(__xdata struct ao_radio_recv *recv) __reentrant;
877
878 void
879 ao_radio_rdf(int ms);
880
881 void
882 ao_radio_abort(void);
883
884 void
885 ao_radio_rdf_abort(void);
886
887 void
888 ao_radio_idle(void);
889
890 void
891 ao_radio_init(void);
892
893 /*
894  * ao_monitor.c
895  */
896
897 extern const char const * const ao_state_names[];
898
899 void
900 ao_monitor(void);
901
902 void
903 ao_set_monitor(uint8_t monitoring);
904
905 void
906 ao_monitor_init(uint8_t led, uint8_t monitoring) __reentrant;
907
908 /*
909  * ao_stdio.c
910  */
911
912 #define AO_READ_AGAIN   ((char) -1)
913
914 struct ao_stdio {
915         char    (*pollchar)(void);
916         void    (*putchar)(char c) __reentrant;
917         void    (*flush)(void);
918 };
919
920 void
921 flush(void);
922
923 extern __xdata uint8_t ao_stdin_ready;
924
925 void
926 ao_add_stdio(char (*pollchar)(void),
927              void (*putchar)(char) __reentrant,
928              void (*flush)(void));
929
930 /*
931  * ao_ignite.c
932  */
933
934 enum ao_igniter {
935         ao_igniter_drogue = 0,
936         ao_igniter_main = 1
937 };
938
939 void
940 ao_ignite(enum ao_igniter igniter);
941
942 enum ao_igniter_status {
943         ao_igniter_unknown,     /* unknown status (ambiguous voltage) */
944         ao_igniter_ready,       /* continuity detected */
945         ao_igniter_active,      /* igniter firing */
946         ao_igniter_open,        /* open circuit detected */
947 };
948
949 enum ao_igniter_status
950 ao_igniter_status(enum ao_igniter igniter);
951
952 void
953 ao_igniter_init(void);
954
955 /*
956  * ao_config.c
957  */
958
959 #define AO_CONFIG_MAJOR 1
960 #define AO_CONFIG_MINOR 3
961
962 struct ao_config {
963         uint8_t         major;
964         uint8_t         minor;
965         uint16_t        main_deploy;
966         int16_t         accel_plus_g;           /* changed for minor version 2 */
967         uint8_t         radio_channel;
968         char            callsign[AO_MAX_CALLSIGN + 1];
969         uint8_t         apogee_delay;           /* minor version 1 */
970         int16_t         accel_minus_g;          /* minor version 2 */
971         uint32_t        radio_cal;              /* minor version 3 */
972 };
973
974 extern __xdata struct ao_config ao_config;
975
976 void
977 ao_config_get(void);
978
979 void
980 ao_config_init(void);
981
982 /*
983  * ao_rssi.c
984  */
985
986 void
987 ao_rssi_set(int rssi_value);
988
989 void
990 ao_rssi_init(uint8_t rssi_led);
991
992 /*
993  * ao_product.c
994  *
995  * values which need to be defined for
996  * each instance of a product
997  */
998
999 extern const uint8_t ao_usb_descriptors [];
1000 extern const uint16_t ao_serial_number;
1001 extern const char ao_version[];
1002 extern const char ao_manufacturer[];
1003 extern const char ao_product[];
1004
1005 /*
1006  * Fifos
1007  */
1008
1009 #define AO_FIFO_SIZE    32
1010
1011 struct ao_fifo {
1012         uint8_t insert;
1013         uint8_t remove;
1014         char    fifo[AO_FIFO_SIZE];
1015 };
1016
1017 #define ao_fifo_insert(f,c) do { \
1018         (f).fifo[(f).insert] = (c); \
1019         (f).insert = ((f).insert + 1) & (AO_FIFO_SIZE-1); \
1020 } while(0)
1021
1022 #define ao_fifo_remove(f,c) do {\
1023         c = (f).fifo[(f).remove]; \
1024         (f).remove = ((f).remove + 1) & (AO_FIFO_SIZE-1); \
1025 } while(0)
1026
1027 #define ao_fifo_full(f)         ((((f).insert + 1) & (AO_FIFO_SIZE-1)) == (f).remove)
1028 #define ao_fifo_empty(f)        ((f).insert == (f).remove)
1029
1030 /*
1031  * ao_packet.c
1032  *
1033  * Packet-based command interface
1034  */
1035
1036 #define AO_PACKET_MAX   8
1037 #define AO_PACKET_SYN           (uint8_t) 0xff
1038
1039 struct ao_packet {
1040         uint8_t         addr;
1041         uint8_t         len;
1042         uint8_t         seq;
1043         uint8_t         ack;
1044         uint8_t         d[AO_PACKET_MAX];
1045 };
1046
1047 struct ao_packet_recv {
1048         struct ao_packet        packet;
1049         int8_t                  rssi;
1050         uint8_t                 status;
1051 };
1052
1053 extern __xdata struct ao_packet_recv ao_rx_packet;
1054 extern __xdata struct ao_packet ao_tx_packet;
1055 extern __xdata struct ao_task   ao_packet_task;
1056 extern __xdata uint8_t ao_packet_enable;
1057 extern __xdata uint8_t ao_packet_master_sleeping;
1058 extern __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
1059
1060 void
1061 ao_packet_send(void);
1062
1063 uint8_t
1064 ao_packet_recv(void);
1065
1066 void
1067 ao_packet_flush(void);
1068
1069 void
1070 ao_packet_putchar(char c) __reentrant;
1071
1072 char
1073 ao_packet_pollchar(void) __critical;
1074
1075 /* ao_packet_master.c */
1076
1077 void
1078 ao_packet_master_init(void);
1079
1080 /* ao_packet_slave.c */
1081
1082 void
1083 ao_packet_slave_start(void);
1084
1085 void
1086 ao_packet_slave_stop(void);
1087
1088 void
1089 ao_packet_slave_init(void);
1090
1091 #endif /* _AO_H_ */