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