Add eeprom driver and command loop
[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  0x75
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
82 /* Stop the operating system, beeping and blinking the reason */
83 void
84 ao_panic(uint8_t reason);
85
86 /*
87  * ao_timer.c
88  */
89
90 /* Our timer runs at 100Hz */
91 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
92 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
93
94 /* Returns the current time in ticks */
95 uint16_t
96 ao_time(void);
97
98 /* Suspend the current task until ticks time has passed */
99 void
100 ao_delay(uint16_t ticks);
101
102 /* Timer interrupt */
103 void
104 ao_timer_isr(void) interrupt 9;
105
106 /* Initialize the timer */
107 void
108 ao_timer_init(void);
109
110 /*
111  * ao_adc.c
112  */
113
114 #define ADC_RING        128
115
116 /*
117  * One set of samples read from the A/D converter
118  */
119 struct ao_adc {
120         uint16_t        tick;           /* tick when the sample was read */
121         int16_t         accel;          /* accelerometer */
122         int16_t         pres;           /* pressure sensor */
123         int16_t         temp;           /* temperature sensor */
124         int16_t         v_batt;         /* battery voltage */
125         int16_t         sense_d;        /* drogue continuity sense */
126         int16_t         sense_m;        /* main continuity sense */
127 };
128
129 /*
130  * A/D data is stored in a ring, with the next sample to be written
131  * at ao_adc_head
132  */
133 extern volatile __xdata struct ao_adc   ao_adc_ring[ADC_RING];
134 extern volatile __data uint8_t          ao_adc_head;
135
136 /* Trigger a conversion sequence (called from the timer interrupt) */
137 void
138 ao_adc_poll(void);
139  
140 /* Suspend the current task until another A/D sample is converted */
141 void
142 ao_adc_sleep(void);
143
144 /* Get a copy of the last complete A/D sample set */
145 void
146 ao_adc_get(__xdata struct ao_adc *packet);
147
148 /* The A/D interrupt handler */
149 void
150 ao_adc_isr(void) interrupt 1;
151
152 /* Initialize the A/D converter */
153 void
154 ao_adc_init(void);
155
156 /*
157  * ao_beep.c
158  */
159
160 /*
161  * Various pre-defined beep frequencies
162  *
163  * frequency = 1/2 (24e6/32) / beep
164  */
165
166 #define AO_BEEP_LOW     150     /* 2500Hz */
167 #define AO_BEEP_MID     94      /* 3989Hz */
168 #define AO_BEEP_HIGH    75      /* 5000Hz */
169 #define AO_BEEP_OFF     0       /* off */
170
171 #define AO_BEEP_g       240     /* 1562.5Hz */
172 #define AO_BEEP_gs      227     /* 1652Hz (1655Hz) */
173 #define AO_BEEP_aa      214     /* 1752Hz (1754Hz) */
174 #define AO_BEEP_bbf     202     /* 1856Hz (1858Hz) */
175 #define AO_BEEP_bb      190     /* 1974Hz (1969Hz) */
176 #define AO_BEEP_cc      180     /* 2083Hz (2086Hz) */
177 #define AO_BEEP_ccs     170     /* 2205Hz (2210Hz) */
178 #define AO_BEEP_dd      160     /* 2344Hz (2341Hz) */
179 #define AO_BEEP_eef     151     /* 2483Hz (2480Hz) */
180 #define AO_BEEP_ee      143     /* 2622Hz (2628Hz) */
181 #define AO_BEEP_ff      135     /* 2778Hz (2784Hz) */
182 #define AO_BEEP_ffs     127     /* 2953Hz (2950Hz) */
183 #define AO_BEEP_gg      120     /* 3125Hz */
184 #define AO_BEEP_ggs     113     /* 3319Hz (3311Hz) */
185 #define AO_BEEP_aaa     107     /* 3504Hz (3508Hz) */
186 #define AO_BEEP_bbbf    101     /* 3713Hz (3716Hz) */
187 #define AO_BEEP_bbb     95      /* 3947Hz (3937Hz) */
188 #define AO_BEEP_ccc     90      /* 4167Hz (4171Hz) */
189 #define AO_BEEP_cccs    85      /* 4412Hz (4419Hz) */
190 #define AO_BEEP_ddd     80      /* 4688Hz (4682Hz) */
191 #define AO_BEEP_eeef    76      /* 4934Hz (4961Hz) */
192 #define AO_BEEP_eee     71      /* 5282Hz (5256Hz) */
193 #define AO_BEEP_fff     67      /* 5597Hz (5568Hz) */
194 #define AO_BEEP_fffs    64      /* 5859Hz (5899Hz) */
195 #define AO_BEEP_ggg     60      /* 6250Hz */
196
197 /* Set the beeper to the specified tone */
198 void
199 ao_beep(uint8_t beep);
200
201 /* Turn on the beeper for the specified time */
202 void
203 ao_beep_for(uint8_t beep, uint16_t ticks);
204
205 /* Initialize the beeper */
206 void
207 ao_beep_init(void);
208
209 /*
210  * ao_led.c
211  */
212
213 #define AO_LED_NONE     0
214 #define AO_LED_GREEN    1
215 #define AO_LED_RED      2
216
217 /* Turn on the specified LEDs */
218 void
219 ao_led_on(uint8_t colors);
220
221 /* Turn off the specified LEDs */
222 void
223 ao_led_off(uint8_t colors);
224
225 /* Set all of the LEDs to the specified state */
226 void
227 ao_led_set(uint8_t colors);
228
229 /* Turn on the specified LEDs for the indicated interval */
230 void
231 ao_led_for(uint8_t colors, uint16_t ticks);
232
233 /* Initialize the LEDs */
234 void
235 ao_led_init(void);
236
237 /*
238  * ao_usb.c
239  */
240
241 /* Put one character to the USB output queue */
242 void
243 ao_usb_putchar(uint8_t c);
244
245 /* Get one character from the USB input queue */
246 uint8_t
247 ao_usb_getchar(void);
248
249 /* Flush the USB output queue */
250 void
251 ao_usb_flush(void);
252
253 /* USB interrupt handler */
254 void
255 ao_usb_isr(void) interrupt 6;
256
257 /* Initialize the USB system */
258 void
259 ao_usb_init(void);
260
261 /*
262  * ao_cmd.c
263  */
264 void
265 ao_cmd_init(void);
266
267 /*
268  * ao_dma.c
269  */
270
271 /* Allocate a DMA channel. the 'done' parameter will be set to 1
272  * when the dma is finished and will be used to wakeup any waiters 
273  */
274 uint8_t
275 ao_dma_alloc(__xdata uint8_t * done);
276
277 /* Setup a DMA channel */
278 void
279 ao_dma_set_transfer(uint8_t id,
280                     void __xdata *srcaddr,
281                     void __xdata *dstaddr,
282                     uint16_t count,
283                     uint8_t cfg0,
284                     uint8_t cfg1);
285
286 /* Start a DMA channel */
287 void
288 ao_dma_start(uint8_t id);
289
290 /* Manually trigger a DMA channel */
291 void
292 ao_dma_trigger(uint8_t id);
293
294 /* Abort a running DMA transfer */
295 void
296 ao_dma_abort(uint8_t id);
297
298 /* DMA interrupt routine */
299 void
300 ao_dma_isr(void) interrupt 8;
301
302 /*
303  * ao_mutex.c
304  */
305
306 void
307 ao_mutex_get(__xdata uint8_t *ao_mutex);
308
309 void
310 ao_mutex_put(__xdata uint8_t *ao_mutex);
311
312 /*
313  * ao_ee.c
314  */
315
316 /*
317  * We reserve the last block on the device for
318  * configuration space. Writes and reads in this
319  * area return errors.
320  */
321
322 #define AO_EE_BLOCK_SIZE        ((uint16_t) (256))
323 #define AO_EE_DEVICE_SIZE       ((uint32_t) 128 * (uint32_t) 1024)
324 #define AO_EE_DATA_SIZE         (AO_EE_DEVICE_SIZE - (uint32_t) AO_EE_BLOCK_SIZE)
325 #define AO_EE_CONFIG_BLOCK      ((uint16_t) (AO_EE_DATA_SIZE / AO_EE_BLOCK_SIZE))
326
327 void
328 ao_ee_flush(void);
329
330 /* Write to the eeprom */
331 uint8_t
332 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len);
333
334 /* Read from the eeprom */
335 uint8_t
336 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len);
337
338 /* Write the config block (at the end of the eeprom) */
339 uint8_t
340 ao_ee_write_config(uint8_t *buf, uint16_t len);
341
342 /* Read the config block (at the end of the eeprom) */
343 uint8_t
344 ao_ee_read_config(uint8_t *buf, uint16_t len);
345
346 /* Initialize the EEPROM code */
347 void
348 ao_ee_init(void);
349
350 #endif /* _AO_H_ */