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