altos: add GCC/SDCC compat macros, init_stack, save_context and GCC stdio hooks
[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 #if HAS_USB
66 extern __code __at (0x00aa) uint8_t ao_usb_descriptors [];
67 #endif
68
69 /* Initialize stack */
70 #define ao_arch_init_stack(task, start) {                       \
71         uint8_t __xdata *stack = task->stack;                   \
72         uint8_t t;                                              \
73         *stack++ = ((uint16_t) start);          /* 0 */         \
74         *stack++ = ((uint16_t) start) >> 8;     /* 1 */         \
75                                                                 \
76         /* and the stuff saved by ao_switch */                  \
77         *stack++ = 0;                           /* 2 acc */     \
78         *stack++ = 0x80;                        /* 3 IE */      \
79                                                                 \
80         /*  4 DPL                                               \
81          *  5 DPH                                               \
82          *  6 B                                                 \
83          *  7 R2                                                \
84          *  8 R3                                                \
85          *  9 R4                                                \
86          * 10 R5                                                \
87          * 11 R6                                                \
88          * 12 R7                                                \
89          * 13 R0                                                \
90          * 14 R1                                                \
91          * 15 PSW                                               \
92          * 16 BP                                                \
93          */                                                     \
94         for (t = 0; t < 13; t++)                                \
95                 *stack++ = 0;                                   \
96         task->stack_count = 17;                                 \
97         }
98
99
100   
101 /* Save current context */
102
103 #define ao_arch_save_context() \
104         _asm \
105                 /* Push ACC first, as when restoring the context it must be restored \
106                  * last (it is used to set the IE register). */ \
107                 push    ACC \
108                 /* Store the IE register then enable interrupts. */ \
109                 push    _IEN0 \
110                 setb    _EA \
111                 push    DPL \
112                 push    DPH \
113                 push    b \
114                 push    ar2 \
115                 push    ar3 \
116                 push    ar4 \
117                 push    ar5 \
118                 push    ar6 \
119                 push    ar7 \
120                 push    ar0 \
121                 push    ar1 \
122                 push    PSW \
123         _endasm; \
124         PSW = 0; \
125         _asm \
126                 push    _bp \
127         _endasm
128
129
130
131 #endif /* _AO_ARCH_H_ */