altos: add GCC/SDCC compat macros, init_stack, save_context and GCC stdio hooks
[fw/altos] / src / avr / 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 #ifndef _AO_ARCH_H_
19 #define _AO_ARCH_H_
20
21 #include <avr/io.h>
22 #include <avr/interrupt.h>
23
24 #ifdef AVR_DEMO
25 #define TEENSY 1
26 #endif
27
28 #if TEENSY
29 #define F_CPU 16000000UL        // 16 MHz
30 #else
31 #define F_CPU  8000000UL        // 8 MHz
32 #endif
33
34 /*
35  * AVR definitions and code fragments for AltOS
36  */
37
38 #define AO_STACK_SIZE   128
39
40 /* Various definitions to make GCC look more like SDCC */
41
42 #define ao_arch_naked_declare   __attribute__((naked))
43 #define ao_arch_naked_define
44 #define __pdata
45 #define __data
46 #define __xdata
47 #define __code const
48 #define __reentrant
49 #define __critical
50 #define __interrupt(n)
51
52 #define ao_arch_reboot()        /* XXX */
53
54 #define ao_arch_nop()           asm("nop")
55
56 #define ao_arch_interrupt(n)    /* nothing */
57
58 #undef putchar
59 #undef getchar
60 #define putchar(c)      ao_putchar(c)
61 #define getchar         ao_getchar
62
63 extern void putchar(char c);
64 extern char getchar(void);
65
66 extern int ao_serial_number;
67
68 #define ao_arch_init_stack(task, start) do {                    \
69         uint8_t         *sp = task->stack + AO_STACK_SIZE - 1;  \
70         uint16_t        a = (uint16_t) start;                   \
71         int             i;                                      \
72                                                                 \
73         /* Return address */                                    \
74         PUSH8(sp, a);                                           \
75         PUSH8(sp, (a >> 8));                                    \
76                                                                 \
77         /* Clear register values */                             \
78         i = 32;                                                 \
79         while (i--)                                             \
80                 PUSH8(sp, 0);                                   \
81                                                                 \
82         /* SREG with interrupts enabled */                      \
83         PUSH8(sp, 0x80);                                        \
84         task->sp = sp;                                          \
85 } while (0);
86         
87 #define ao_arch_save_context() do {                     \
88         asm("push r31" "\n\t" "push r30"); \
89         asm("push r29" "\n\t" "push r28" "\n\t" "push r27" "\n\t" "push r26" "\n\t" "push r25"); \
90         asm("push r24" "\n\t" "push r23" "\n\t" "push r22" "\n\t" "push r21" "\n\t" "push r20"); \
91         asm("push r19" "\n\t" "push r18" "\n\t" "push r17" "\n\t" "push r16" "\n\t" "push r15"); \
92         asm("push r14" "\n\t" "push r13" "\n\t" "push r12" "\n\t" "push r11" "\n\t" "push r10"); \
93         asm("push r9" "\n\t" "push r8" "\n\t" "push r7" "\n\t" "push r6" "\n\t" "push r5"); \
94         asm("push r4" "\n\t" "push r3" "\n\t" "push r2" "\n\t" "push r1" "\n\t" "push r0"); \
95         asm("in r0, __SREG__" "\n\t" "push r0"); \
96         sei(); \
97         } while (0)
98
99 #endif /* _AO_ARCH_H_ */