altos: Make ao_xmem funcs require __xdata void * instead of casting
[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 extern __code __at (0x00a0) uint16_t ao_romconfig_version;
63 extern __code __at (0x00a2) uint16_t ao_romconfig_check;
64 extern __code __at (0x00a4) uint16_t ao_serial_number;
65 extern __code __at (0x00a6) uint32_t ao_radio_cal;
66
67 #ifndef HAS_USB
68 #error Please define HAS_USB
69 #endif
70
71 #define ao_arch_task_members\
72         uint8_t stack_count;            /* amount of saved stack */
73
74 /* Initialize stack */
75 #define ao_arch_init_stack(task, start) {                       \
76         uint8_t __xdata *stack = task->stack;                   \
77         uint8_t t;                                              \
78         *stack++ = ((uint16_t) start);          /* 0 */         \
79         *stack++ = ((uint16_t) start) >> 8;     /* 1 */         \
80                                                                 \
81         /* and the stuff saved by ao_switch */                  \
82         *stack++ = 0;                           /* 2 acc */     \
83         *stack++ = 0x80;                        /* 3 IE */      \
84                                                                 \
85         /*  4 DPL                                               \
86          *  5 DPH                                               \
87          *  6 B                                                 \
88          *  7 R2                                                \
89          *  8 R3                                                \
90          *  9 R4                                                \
91          * 10 R5                                                \
92          * 11 R6                                                \
93          * 12 R7                                                \
94          * 13 R0                                                \
95          * 14 R1                                                \
96          * 15 PSW                                               \
97          * 16 BP                                                \
98          */                                                     \
99         for (t = 0; t < 13; t++)                                \
100                 *stack++ = 0;                                   \
101         task->stack_count = 17;                                 \
102         }
103
104
105   
106 /* Save current context */
107
108 #define ao_arch_save_regs()                                             \
109         _asm                                                            \
110         /* Push ACC first, as when restoring the context it must be restored \
111          * last (it is used to set the IE register). */                 \
112         push    ACC                                                     \
113         /* Store the IE register then enable interrupts. */             \
114         push    _IEN0                                                   \
115         setb    _EA                                                     \
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 #define ao_arch_isr_stack()                                             \
149         /* Empty the stack; might as well let interrupts have the whole thing */ \
150         (SP = AO_STACK_START - 1)
151
152 #define ao_arch_cpu_idle()      (PCON = PCON_IDLE)
153
154 #define ao_arch_restore_stack() {                                       \
155                 uint8_t stack_len;                                      \
156                 __data uint8_t *stack_ptr;                              \
157                 __xdata uint8_t *save_ptr;                              \
158                                                                         \
159                 /* Restore the old stack */                             \
160                 stack_len = ao_cur_task->stack_count;                   \
161                 SP = AO_STACK_START - 1 + stack_len;                    \
162                                                                         \
163                 stack_ptr = (uint8_t __data *) AO_STACK_START;          \
164                 save_ptr = (uint8_t __xdata *) ao_cur_task->stack;      \
165                 do                                                      \
166                         *stack_ptr++ = *save_ptr++;                     \
167                 while (--stack_len);                                    \
168                                                                         \
169                 _asm                                                    \
170                 pop             _bp                                     \
171                 pop             PSW                                     \
172                 pop             ar1                                     \
173                 pop             ar0                                     \
174                 pop             ar7                                     \
175                 pop             ar6                                     \
176                 pop             ar5                                     \
177                 pop             ar4                                     \
178                 pop             ar3                                     \
179                 pop             ar2                                     \
180                 pop             b                                       \
181                 pop             DPH                                     \
182                 pop             DPL                                     \
183                 /* The next byte of the stack is the IE register.  Only the global \
184                    enable bit forms part of the task context.  Pop off the IE then set \
185                    the global enable bit to match that of the stored IE register. */ \
186                 pop             ACC                                     \
187                 JB              ACC.7,0098$                             \
188                 CLR             _EA                                     \
189                 LJMP    0099$                                           \
190                 0098$:                                                  \
191                         SETB            _EA                             \
192                 0099$:                                                  \
193                 /* Finally pop off the ACC, which was the first register saved. */ \
194                 pop             ACC                                     \
195                 ret                                                     \
196                 _endasm;                                                \
197 }
198
199 #define ao_arch_critical(b) __critical { b }
200
201 struct ao_adc {
202         uint16_t        tick;           /* tick when the sample was read */
203         int16_t         accel;          /* accelerometer */
204         int16_t         pres;           /* pressure sensor */
205         int16_t         temp;           /* temperature sensor */
206         int16_t         v_batt;         /* battery voltage */
207         int16_t         sense_d;        /* drogue continuity sense */
208         int16_t         sense_m;        /* main continuity sense */
209 };
210
211 #define AO_ADC_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(void) __critical;
233
234 /* ao_string.c */
235
236 void
237 _ao_xmemcpy(__xdata void *dst, __xdata void *src, uint8_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, uint8_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, uint8_t count);
248
249 #define ao_xmemcmp(d,s,c) _ao_xmemcmp((d), (s), (c))
250
251 #endif /* _AO_ARCH_H_ */