8a344ccab2b19847d93da807f2d286db3a9d8d0c
[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  0x4b
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 stack[AO_STACK_SIZE];   /* saved stack */
41 };
42
43 #define AO_NUM_TASKS            10      /* maximum number of tasks */
44
45 /*
46  ao_task.c
47  */
48
49 /* Suspend the current task until wchan is awoken */
50 int
51 ao_sleep(__xdata void *wchan);
52
53 /* Wake all tasks sleeping on wchan */
54 int
55 ao_wakeup(__xdata void *wchan);
56
57 /* Yield the processor to another task */
58 void
59 ao_yield(void) _naked;
60
61 /* Add a task to the run queue */
62 void
63 ao_add_task(__xdata struct ao_task * task, void (*start)(void));
64
65 /* Start the scheduler. This will not return */
66 void
67 ao_start_scheduler(void);
68
69 /*
70  * ao_panic.c
71  */
72
73 #define AO_PANIC_NO_TASK        1       /* AO_NUM_TASKS is not large enough */
74
75 /* Stop the operating system, beeping and blinking the reason */
76 void
77 ao_panic(uint8_t reason);
78
79 /*
80  * ao_timer.c
81  */
82
83 /* Our timer runs at 100Hz */
84 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
85 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
86
87 /* Returns the current time in ticks */
88 uint16_t
89 ao_time(void);
90
91 /* Suspend the current task until ticks time has passed */
92 void
93 ao_delay(uint16_t ticks);
94
95 /* Timer interrupt */
96 void
97 ao_timer_isr(void) interrupt 9;
98
99 /* Initialize the timer */
100 void
101 ao_timer_init(void);
102
103 /*
104  * ao_adc.c
105  */
106
107 #define ADC_RING        128
108
109 /*
110  * One set of samples read from the A/D converter
111  */
112 struct ao_adc {
113         uint16_t        tick;           /* tick when the sample was read */
114         int16_t         accel;          /* accelerometer */
115         int16_t         pres;           /* pressure sensor */
116         int16_t         temp;           /* temperature sensor */
117         int16_t         v_batt;         /* battery voltage */
118         int16_t         sense_d;        /* drogue continuity sense */
119         int16_t         sense_m;        /* main continuity sense */
120 };
121
122 /*
123  * A/D data is stored in a ring, with the next sample to be written
124  * at ao_adc_head
125  */
126 extern volatile __xdata struct ao_adc   ao_adc_ring[ADC_RING];
127 extern volatile __data uint8_t          ao_adc_head;
128
129 /* Trigger a conversion sequence (called from the timer interrupt) */
130 void
131 ao_adc_poll(void);
132  
133 /* Suspend the current task until another A/D sample is converted */
134 void
135 ao_adc_sleep(void);
136
137 /* Get a copy of the last complete A/D sample set */
138 void
139 ao_adc_get(__xdata struct ao_adc *packet);
140
141 /* The A/D interrupt handler */
142 void
143 ao_adc_isr(void) interrupt 1;
144
145 /* Initialize the A/D converter */
146 void
147 ao_adc_init(void);
148
149 /*
150  * ao_beep.c
151  */
152
153 /*
154  * Various pre-defined beep frequencies
155  *
156  * frequency = 1/2 (24e6/32) / beep
157  */
158
159 #define AO_BEEP_LOW     150     /* 2500Hz */
160 #define AO_BEEP_MID     94      /* 3989Hz */
161 #define AO_BEEP_HIGH    75      /* 5000Hz */
162 #define AO_BEEP_OFF     0       /* off */
163
164 #define AO_BEEP_g       240     /* 1562.5Hz */
165 #define AO_BEEP_gs      227     /* 1652Hz (1655Hz) */
166 #define AO_BEEP_aa      214     /* 1752Hz (1754Hz) */
167 #define AO_BEEP_bbf     202     /* 1856Hz (1858Hz) */
168 #define AO_BEEP_bb      190     /* 1974Hz (1969Hz) */
169 #define AO_BEEP_cc      180     /* 2083Hz (2086Hz) */
170 #define AO_BEEP_ccs     170     /* 2205Hz (2210Hz) */
171 #define AO_BEEP_dd      160     /* 2344Hz (2341Hz) */
172 #define AO_BEEP_eef     151     /* 2483Hz (2480Hz) */
173 #define AO_BEEP_ee      143     /* 2622Hz (2628Hz) */
174 #define AO_BEEP_ff      135     /* 2778Hz (2784Hz) */
175 #define AO_BEEP_ffs     127     /* 2953Hz (2950Hz) */
176 #define AO_BEEP_gg      120     /* 3125Hz */
177 #define AO_BEEP_ggs     113     /* 3319Hz (3311Hz) */
178 #define AO_BEEP_aaa     107     /* 3504Hz (3508Hz) */
179 #define AO_BEEP_bbbf    101     /* 3713Hz (3716Hz) */
180 #define AO_BEEP_bbb     95      /* 3947Hz (3937Hz) */
181 #define AO_BEEP_ccc     90      /* 4167Hz (4171Hz) */
182 #define AO_BEEP_cccs    85      /* 4412Hz (4419Hz) */
183 #define AO_BEEP_ddd     80      /* 4688Hz (4682Hz) */
184 #define AO_BEEP_eeef    76      /* 4934Hz (4961Hz) */
185 #define AO_BEEP_eee     71      /* 5282Hz (5256Hz) */
186 #define AO_BEEP_fff     67      /* 5597Hz (5568Hz) */
187 #define AO_BEEP_fffs    64      /* 5859Hz (5899Hz) */
188 #define AO_BEEP_ggg     60      /* 6250Hz */
189
190 /* Set the beeper to the specified tone */
191 void
192 ao_beep(uint8_t beep);
193
194 /* Turn on the beeper for the specified time */
195 void
196 ao_beep_for(uint8_t beep, uint16_t ticks);
197
198 /* Initialize the beeper */
199 void
200 ao_beep_init(void);
201
202 /*
203  * ao_led.c
204  */
205
206 #define AO_LED_NONE     0
207 #define AO_LED_GREEN    1
208 #define AO_LED_RED      2
209
210 /* Turn on the specified LEDs */
211 void
212 ao_led_on(uint8_t colors);
213
214 /* Turn off the specified LEDs */
215 void
216 ao_led_off(uint8_t colors);
217
218 /* Set all of the LEDs to the specified state */
219 void
220 ao_led_set(uint8_t colors);
221
222 /* Turn on the specified LEDs for the indicated interval */
223 void
224 ao_led_for(uint8_t colors, uint16_t ticks);
225
226 /* Initialize the LEDs */
227 void
228 ao_led_init(void);
229
230 /*
231  * ao_usb.c
232  */
233
234 /* Put one character to the USB output queue */
235 void
236 ao_usb_putchar(uint8_t c);
237
238 /* Get one character from the USB input queue */
239 uint8_t
240 ao_usb_getchar(void);
241
242 /* Flush the USB output queue */
243 void
244 ao_usb_flush(void);
245
246 /* USB interrupt handler */
247 void
248 ao_usb_isr(void) interrupt 6;
249
250 /* Initialize the USB system */
251 void
252 ao_usb_init(void);
253
254 /*
255  * ao_cmd.c
256  */
257 void
258 ao_cmd_init(void);
259
260 #endif /* _AO_H_ */