altos: Write xdata versions of memory functions
[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 /* Stack runs from above the allocated __data space to 0xfe, which avoids
31  * writing to 0xff as that triggers the stack overflow indicator
32  */
33 #define AO_STACK_START  0x90
34 #define AO_STACK_END    0xfe
35 #define AO_STACK_SIZE   (AO_STACK_END - AO_STACK_START + 1)
36
37 #define ao_arch_reboot() do {                                   \
38         WDCTL = WDCTL_EN | WDCTL_MODE_WATCHDOG | WDCTL_INT_64;  \
39         ao_delay(AO_SEC_TO_TICKS(2));                           \
40         } while (0)
41         
42 #define ao_arch_nop()   _asm nop _endasm
43 #define ao_arch_interrupt(n)    __interrupt n
44
45 #define ao_arch_naked_declare   __naked
46 #define ao_arch_naked_define    __naked
47
48 /* CC1111-specific drivers */
49
50 /*
51  * ao_romconfig.c
52  */
53
54 #define AO_ROMCONFIG_VERSION    2
55
56 extern __code __at (0x00a0) uint16_t ao_romconfig_version;
57 extern __code __at (0x00a2) uint16_t ao_romconfig_check;
58 extern __code __at (0x00a4) uint16_t ao_serial_number;
59 extern __code __at (0x00a6) uint32_t ao_radio_cal;
60
61 #ifndef HAS_USB
62 #error Please define HAS_USB
63 #endif
64
65 #define ao_arch_task_members\
66         uint8_t stack_count;            /* amount of saved stack */
67
68 /* Initialize stack */
69 #define ao_arch_init_stack(task, start) {                       \
70         uint8_t __xdata *stack = task->stack;                   \
71         uint8_t t;                                              \
72         *stack++ = ((uint16_t) start);          /* 0 */         \
73         *stack++ = ((uint16_t) start) >> 8;     /* 1 */         \
74                                                                 \
75         /* and the stuff saved by ao_switch */                  \
76         *stack++ = 0;                           /* 2 acc */     \
77         *stack++ = 0x80;                        /* 3 IE */      \
78                                                                 \
79         /*  4 DPL                                               \
80          *  5 DPH                                               \
81          *  6 B                                                 \
82          *  7 R2                                                \
83          *  8 R3                                                \
84          *  9 R4                                                \
85          * 10 R5                                                \
86          * 11 R6                                                \
87          * 12 R7                                                \
88          * 13 R0                                                \
89          * 14 R1                                                \
90          * 15 PSW                                               \
91          * 16 BP                                                \
92          */                                                     \
93         for (t = 0; t < 13; t++)                                \
94                 *stack++ = 0;                                   \
95         task->stack_count = 17;                                 \
96         }
97
98
99   
100 /* Save current context */
101
102 #define ao_arch_save_regs()                                             \
103         _asm                                                            \
104         /* Push ACC first, as when restoring the context it must be restored \
105          * last (it is used to set the IE register). */                 \
106         push    ACC                                                     \
107         /* Store the IE register then enable interrupts. */             \
108         push    _IEN0                                                   \
109         setb    _EA                                                     \
110         push    DPL                                                     \
111         push    DPH                                                     \
112         push    b                                                       \
113         push    ar2                                                     \
114         push    ar3                                                     \
115         push    ar4                                                     \
116         push    ar5                                                     \
117         push    ar6                                                     \
118         push    ar7                                                     \
119         push    ar0                                                     \
120         push    ar1                                                     \
121         push    PSW                                                     \
122         _endasm;                                                        \
123         PSW = 0;                                                        \
124         _asm                                                            \
125         push    _bp                                                     \
126         _endasm
127
128 #define ao_arch_save_stack() {                                          \
129                 uint8_t stack_len;                                      \
130                 __data uint8_t *stack_ptr;                              \
131                 __xdata uint8_t *save_ptr;                              \
132                 /* Save the current stack */                            \
133                 stack_len = SP - (AO_STACK_START - 1);                  \
134                 ao_cur_task->stack_count = stack_len;                   \
135                 stack_ptr = (uint8_t __data *) AO_STACK_START;          \
136                 save_ptr = (uint8_t __xdata *) ao_cur_task->stack;      \
137                 do                                                      \
138                         *save_ptr++ = *stack_ptr++;                     \
139                 while (--stack_len);                                    \
140         }
141
142 #define ao_arch_isr_stack()                                             \
143         /* Empty the stack; might as well let interrupts have the whole thing */ \
144         (SP = AO_STACK_START - 1)
145
146 #define ao_arch_cpu_idle()      (PCON = PCON_IDLE)
147
148 #define ao_arch_restore_stack() {                                       \
149                 uint8_t stack_len;                                      \
150                 __data uint8_t *stack_ptr;                              \
151                 __xdata uint8_t *save_ptr;                              \
152                                                                         \
153                 /* Restore the old stack */                             \
154                 stack_len = ao_cur_task->stack_count;                   \
155                 SP = AO_STACK_START - 1 + stack_len;                    \
156                                                                         \
157                 stack_ptr = (uint8_t __data *) AO_STACK_START;          \
158                 save_ptr = (uint8_t __xdata *) ao_cur_task->stack;      \
159                 do                                                      \
160                         *stack_ptr++ = *save_ptr++;                     \
161                 while (--stack_len);                                    \
162                                                                         \
163                 _asm                                                    \
164                 pop             _bp                                     \
165                 pop             PSW                                     \
166                 pop             ar1                                     \
167                 pop             ar0                                     \
168                 pop             ar7                                     \
169                 pop             ar6                                     \
170                 pop             ar5                                     \
171                 pop             ar4                                     \
172                 pop             ar3                                     \
173                 pop             ar2                                     \
174                 pop             b                                       \
175                 pop             DPH                                     \
176                 pop             DPL                                     \
177                 /* The next byte of the stack is the IE register.  Only the global \
178                    enable bit forms part of the task context.  Pop off the IE then set \
179                    the global enable bit to match that of the stored IE register. */ \
180                 pop             ACC                                     \
181                 JB              ACC.7,0098$                             \
182                 CLR             _EA                                     \
183                 LJMP    0099$                                           \
184                 0098$:                                                  \
185                         SETB            _EA                             \
186                 0099$:                                                  \
187                 /* Finally pop off the ACC, which was the first register saved. */ \
188                 pop             ACC                                     \
189                 ret                                                     \
190                 _endasm;                                                \
191 }
192
193 #define ao_arch_critical(b) __critical { b }
194
195 struct ao_adc {
196         uint16_t        tick;           /* tick when the sample was read */
197         int16_t         accel;          /* accelerometer */
198         int16_t         pres;           /* pressure sensor */
199         int16_t         temp;           /* temperature sensor */
200         int16_t         v_batt;         /* battery voltage */
201         int16_t         sense_d;        /* drogue continuity sense */
202         int16_t         sense_m;        /* main continuity sense */
203 };
204
205 #define AO_ADC_RING     32
206
207 /* ao_string.c */
208
209 void
210 _ao_xmemcpy(__xdata uint8_t *dst, __xdata uint8_t *src, uint8_t count);
211
212 #define ao_xmemcpy(d,s,c) _ao_xmemcpy((__xdata uint8_t *) (d), (__xdata uint8_t *) (s), (c))
213
214 void
215 _ao_xmemset(__xdata uint8_t *dst, uint8_t value, uint8_t count);
216
217 #define ao_xmemset(d,v,c) _ao_xmemset((__xdata uint8_t *) (d), (v), (c))
218
219 int8_t
220 _ao_xmemcmp(__xdata uint8_t *a, __xdata uint8_t *b, uint8_t count);
221
222 #define ao_xmemcmp(d,s,c) _ao_xmemcmp((__xdata uint8_t *) (d), (__xdata uint8_t *) (s), (c))
223
224 #endif /* _AO_ARCH_H_ */