Switch from GPLv2 to GPLv2+
[fw/altos] / src / cc1111 / ao_arch.h
1 /*
2  * Copyright © 2011 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 /*
20  * CC1111 definitions and code fragments for AltOS
21  */
22
23 #ifndef _AO_ARCH_H_
24 #define _AO_ARCH_H_
25
26 #include "cc1111.h"
27
28 /* Convert a __data pointer into an __xdata pointer */
29 #define DATA_TO_XDATA(a)        ((void __xdata *) ((uint8_t) (a) | 0xff00))
30
31 /* Code and xdata use the same address space */
32 #define CODE_TO_XDATA(a)        ((__xdata void *) ((uint16_t) (a)))
33
34 /* Pdata lives at the start of xdata */
35 #define PDATA_TO_XDATA(a)       ((void __xdata *) ((uint8_t) (a) | 0xf000))
36
37 /* Stack runs from above the allocated __data space to 0xfe, which avoids
38  * writing to 0xff as that triggers the stack overflow indicator
39  */
40 #define AO_STACK_START  0x90
41 #define AO_STACK_END    0xfe
42 #define AO_STACK_SIZE   (AO_STACK_END - AO_STACK_START + 1)
43
44 #define AO_PORT_TYPE    uint8_t
45
46 #define ao_arch_reboot() do {                                   \
47         WDCTL = WDCTL_EN | WDCTL_MODE_WATCHDOG | WDCTL_INT_64;  \
48         ao_delay(AO_SEC_TO_TICKS(2));                           \
49         } while (0)
50         
51 #define ao_arch_nop()   __asm nop __endasm
52 #define ao_arch_interrupt(n)    __interrupt n
53
54 #define ao_arch_naked_declare   __naked
55 #define ao_arch_naked_define    __naked
56
57 /* CC1111-specific drivers */
58
59 /*
60  * ao_romconfig.c
61  */
62
63 #define AO_ROMCONFIG_VERSION    2
64
65 #define AO_ROMCONFIG_SYMBOL(a) __code __at(a)
66
67 extern AO_ROMCONFIG_SYMBOL(0x00a0) uint16_t ao_romconfig_version;
68 extern AO_ROMCONFIG_SYMBOL(0x00a2) uint16_t ao_romconfig_check;
69 extern AO_ROMCONFIG_SYMBOL(0x00a4) uint16_t ao_serial_number;
70 extern AO_ROMCONFIG_SYMBOL(0x00a6) uint32_t ao_radio_cal;
71
72 #ifndef HAS_USB
73 #error Please define HAS_USB
74 #endif
75
76 #define ao_arch_task_members\
77         uint8_t stack_count;            /* amount of saved stack */
78
79 /* Initialize stack */
80 #define ao_arch_init_stack(task, start) {                       \
81         uint8_t __xdata *stack = task->stack;                   \
82         uint8_t t;                                              \
83         *stack++ = ((uint16_t) start);          /* 0 */         \
84         *stack++ = ((uint16_t) start) >> 8;     /* 1 */         \
85                                                                 \
86         /* and the stuff saved by ao_switch */                  \
87         *stack++ = 0;                           /* 2 acc */     \
88         *stack++ = 0x80;                        /* 3 IE */      \
89                                                                 \
90         /*  4 DPL                                               \
91          *  5 DPH                                               \
92          *  6 B                                                 \
93          *  7 R2                                                \
94          *  8 R3                                                \
95          *  9 R4                                                \
96          * 10 R5                                                \
97          * 11 R6                                                \
98          * 12 R7                                                \
99          * 13 R0                                                \
100          * 14 R1                                                \
101          * 15 PSW                                               \
102          * 16 BP                                                \
103          */                                                     \
104         for (t = 0; t < 13; t++)                                \
105                 *stack++ = 0;                                   \
106         task->stack_count = 17;                                 \
107         }
108
109
110   
111 /* Save current context */
112
113 #define ao_arch_save_regs()                                             \
114         __asm                                                           \
115         /* Push ACC first, as when restoring the context it must be restored \
116          * last (it is used to set the IE register). */                 \
117         push    ACC                                                     \
118         push    _IEN0                                                   \
119         push    DPL                                                     \
120         push    DPH                                                     \
121         push    b                                                       \
122         push    ar2                                                     \
123         push    ar3                                                     \
124         push    ar4                                                     \
125         push    ar5                                                     \
126         push    ar6                                                     \
127         push    ar7                                                     \
128         push    ar0                                                     \
129         push    ar1                                                     \
130         push    PSW                                                     \
131         __endasm;                                                       \
132         PSW = 0;                                                        \
133         __asm                                                           \
134         push    _bp                                                     \
135         __endasm
136
137 #define ao_arch_save_stack() {                                          \
138                 uint8_t stack_len;                                      \
139                 __data uint8_t *stack_ptr;                              \
140                 __xdata uint8_t *save_ptr;                              \
141                 /* Save the current stack */                            \
142                 stack_len = SP - (AO_STACK_START - 1);                  \
143                 ao_cur_task->stack_count = stack_len;                   \
144                 stack_ptr = (uint8_t __data *) AO_STACK_START;          \
145                 save_ptr = (uint8_t __xdata *) ao_cur_task->stack;      \
146                 do                                                      \
147                         *save_ptr++ = *stack_ptr++;                     \
148                 while (--stack_len);                                    \
149         }
150
151 /* Empty the stack; might as well let interrupts have the whole thing */
152 #define ao_arch_isr_stack()             (SP = AO_STACK_START - 1)
153
154 #define ao_arch_block_interrupts()      __asm clr _EA __endasm
155 #define ao_arch_release_interrupts()    __asm setb _EA __endasm
156
157 /* Idle the CPU, waking when an interrupt occurs */
158 #define ao_arch_wait_interrupt() do {           \
159                 ao_arch_release_interrupts();   \
160                 (PCON = PCON_IDLE);             \
161                 ao_arch_block_interrupts();     \
162         } while (0)
163
164 #define ao_arch_restore_stack() {                                       \
165                 uint8_t stack_len;                                      \
166                 __data uint8_t *stack_ptr;                              \
167                 __xdata uint8_t *save_ptr;                              \
168                                                                         \
169                 /* Restore the old stack */                             \
170                 stack_len = ao_cur_task->stack_count;                   \
171                 SP = AO_STACK_START - 1 + stack_len;                    \
172                                                                         \
173                 stack_ptr = (uint8_t __data *) AO_STACK_START;          \
174                 save_ptr = (uint8_t __xdata *) ao_cur_task->stack;      \
175                 do                                                      \
176                         *stack_ptr++ = *save_ptr++;                     \
177                 while (--stack_len);                                    \
178                                                                         \
179                 __asm                                                   \
180                 pop             _bp                                     \
181                 pop             PSW                                     \
182                 pop             ar1                                     \
183                 pop             ar0                                     \
184                 pop             ar7                                     \
185                 pop             ar6                                     \
186                 pop             ar5                                     \
187                 pop             ar4                                     \
188                 pop             ar3                                     \
189                 pop             ar2                                     \
190                 pop             b                                       \
191                 pop             DPH                                     \
192                 pop             DPL                                     \
193                 /* The next byte of the stack is the IE register.  Only the global \
194                    enable bit forms part of the task context.  Pop off the IE then set \
195                    the global enable bit to match that of the stored IE register. */ \
196                 pop             ACC                                     \
197                 JB              ACC.7,0098$                             \
198                 CLR             _EA                                     \
199                 LJMP    0099$                                           \
200                 0098$:                                                  \
201                         SETB            _EA                             \
202                 0099$:                                                  \
203                 /* Finally restore ACC, which was the first register saved. */ \
204                 pop             ACC                                     \
205                 ret                                                     \
206                 __endasm;                                               \
207 }
208
209 #define ao_arch_critical(b) __critical { b }
210
211 #define AO_DATA_RING    32
212
213 /* ao_button.c */
214 #ifdef HAS_BUTTON
215 void
216 ao_p0_isr(void) ao_arch_interrupt(13);
217
218 void
219 ao_p1_isr(void) ao_arch_interrupt(15);
220
221 void
222 ao_p2_isr(void);
223
224 #define HAS_P2_ISR      1
225
226 #endif
227
228 void
229 ao_button_init(void);
230
231 char
232 ao_button_get(uint16_t timeout) __critical;
233
234 void
235 ao_button_clear(void) __critical;
236
237 /* ao_string.c */
238
239 void
240 _ao_xmemcpy(__xdata void *dst, __xdata void *src, uint16_t count);
241
242 #define ao_xmemcpy(d,s,c) _ao_xmemcpy(d,s,c)
243
244 void
245 _ao_xmemset(__xdata void *dst, uint8_t value, uint16_t count);
246
247 #define ao_xmemset(d,v,c) _ao_xmemset(d,v,c)
248
249 int8_t
250 _ao_xmemcmp(__xdata void *a, __xdata void *b, uint16_t count);
251
252 #define ao_xmemcmp(d,s,c) _ao_xmemcmp((d), (s), (c))
253
254 struct ao_serial_speed {
255         uint8_t baud;
256         uint8_t gcr;
257 };
258
259 extern const __code struct ao_serial_speed ao_serial_speeds[];
260
261 /*
262  * ao_dma.c
263  */
264
265 /* Allocate a DMA channel. the 'done' parameter will be set when the
266  * dma is finished and will be used to wakeup any waiters
267  */
268
269 uint8_t
270 ao_dma_alloc(__xdata uint8_t * done);
271
272 /* Setup a DMA channel */
273 void
274 ao_dma_set_transfer(uint8_t id,
275                     void __xdata *srcaddr,
276                     void __xdata *dstaddr,
277                     uint16_t count,
278                     uint8_t cfg0,
279                     uint8_t cfg1);
280
281 /* Start a DMA channel */
282 void
283 ao_dma_start(uint8_t id);
284
285 /* Manually trigger a DMA channel */
286 void
287 ao_dma_trigger(uint8_t id);
288
289 /* Abort a running DMA transfer */
290 void
291 ao_dma_abort(uint8_t id);
292
293 /* DMA interrupt routine */
294 void
295 ao_dma_isr(void) ao_arch_interrupt(8);
296
297 /* ao_adc.c */
298
299 #if HAS_ADC
300 /* The A/D interrupt handler */
301 void
302 ao_adc_isr(void) ao_arch_interrupt(1);
303 #endif
304
305 #if HAS_USB
306 /* USB interrupt handler */
307 void
308 ao_usb_isr(void) ao_arch_interrupt(6);
309 #endif
310
311 #if HAS_SERIAL_0
312 void
313 ao_serial0_rx_isr(void) ao_arch_interrupt(2);
314
315 void
316 ao_serial0_tx_isr(void) ao_arch_interrupt(7);
317 #endif
318
319 #if HAS_SERIAL_1
320 void
321 ao_serial1_rx_isr(void) ao_arch_interrupt(3);
322
323 void
324 ao_serial1_tx_isr(void) ao_arch_interrupt(14);
325 #endif
326
327 #if HAS_EXTI_0
328 void
329 ao_p0_isr(void) __interrupt(13);
330 #endif
331
332 #define AO_ADC_MAX      32767
333
334 #endif /* _AO_ARCH_H_ */