altos: Declare port register type only in arch header
[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 <stdio.h>
22 #include <avr/io.h>
23 #include <avr/interrupt.h>
24 #include <avr/sleep.h>
25
26 #ifdef AVR_DEMO
27 #define TEENSY 1
28 #endif
29
30 #if TEENSY
31 #define F_CPU 16000000UL        // 16 MHz
32 #else
33 #define F_CPU  8000000UL        // 8 MHz
34 #endif
35
36 /*
37  * AVR definitions and code fragments for AltOS
38  */
39
40 #ifndef AO_STACK_SIZE
41 #define AO_STACK_SIZE   116
42 #endif
43
44 #define AO_PORT_TYPE    uint8_t
45
46 /* Various definitions to make GCC look more like SDCC */
47
48 #define ao_arch_naked_declare   __attribute__((naked))
49 #define ao_arch_naked_define
50 #define __pdata
51 #define __data
52 #define __xdata
53 #define __code const
54 #define __reentrant
55 #define __critical
56 #define __interrupt(n)
57 #define __at(n)
58
59 #define ao_arch_reboot()        /* XXX */
60
61 #define ao_arch_nop()           asm("nop")
62
63 #define ao_arch_interrupt(n)    /* nothing */
64
65 #undef putchar
66 #undef getchar
67 #define putchar(c)      ao_putchar(c)
68 #define getchar         ao_getchar
69
70 extern void putchar(char c);
71 extern char getchar(void);
72 extern void ao_avr_stdio_init(void);
73
74 #define AO_ROMCONFIG_VERSION    2
75
76 #define AO_ROMCONFIG_SYMBOL(a) const
77
78 extern AO_ROMCONFIG_SYMBOL(0) uint16_t ao_serial_number;
79
80 #define AVR_PUSH8(stack, val)   (*((stack)--) = (val))
81
82 extern uint8_t  ao_cpu_sleep_disable;
83
84 #define ao_arch_task_globals    uint8_t ao_cpu_sleep_disable;
85
86 #define ao_arch_task_members\
87         uint8_t *sp;                    /* saved stack pointer */
88
89 #define ao_arch_init_stack(task, start) do {                    \
90         uint8_t         *sp = task->stack + AO_STACK_SIZE - 1;  \
91         uint16_t        a = (uint16_t) start;                   \
92         int             i;                                      \
93                                                                 \
94         /* Return address */                                    \
95         AVR_PUSH8(sp, a);                                       \
96         AVR_PUSH8(sp, (a >> 8));                                \
97                                                                 \
98         /* Clear register values */                             \
99         i = 32;                                                 \
100         while (i--)                                             \
101                 AVR_PUSH8(sp, 0);                               \
102                                                                 \
103         /* SREG with interrupts enabled */                      \
104         AVR_PUSH8(sp, 0x80);                                    \
105         task->sp = sp;                                          \
106 } while (0);
107         
108 #define ao_arch_save_regs() do {                                        \
109                 asm("push r31" "\n\t" "push r30");                      \
110                 asm("push r29" "\n\t" "push r28" "\n\t" "push r27" "\n\t" "push r26" "\n\t" "push r25"); \
111                 asm("push r24" "\n\t" "push r23" "\n\t" "push r22" "\n\t" "push r21" "\n\t" "push r20"); \
112                 asm("push r19" "\n\t" "push r18" "\n\t" "push r17" "\n\t" "push r16" "\n\t" "push r15"); \
113                 asm("push r14" "\n\t" "push r13" "\n\t" "push r12" "\n\t" "push r11" "\n\t" "push r10"); \
114                 asm("push r9" "\n\t" "push r8" "\n\t" "push r7" "\n\t" "push r6" "\n\t" "push r5"); \
115                 asm("push r4" "\n\t" "push r3" "\n\t" "push r2" "\n\t" "push r1" "\n\t" "push r0"); \
116                 asm("in r0, __SREG__" "\n\t" "push r0");                \
117         } while (0)
118
119 #define ao_arch_save_stack() do {                                       \
120                 uint8_t sp_l, sp_h;                                     \
121                 asm("in %0,__SP_L__" : "=&r" (sp_l) );                  \
122                 asm("in %0,__SP_H__" : "=&r" (sp_h) );                  \
123                 ao_cur_task->sp = (uint8_t *) ((uint16_t) sp_l | ((uint16_t) sp_h << 8)); \
124         } while (0)
125
126 #define ao_arch_isr_stack()     /* nothing */
127
128 /* Idle the CPU (if possible) waiting for an interrupt. Enabling
129  * interrupts and sleeping the CPU must be adjacent to eliminate race
130  * conditions. In all cases, we execute a single nop with interrupts
131  * enabled
132  */
133 #define ao_arch_wait_interrupt() do {           \
134                 if (!ao_cpu_sleep_disable) {    \
135                         sleep_enable();         \
136                         sei();                  \
137                         sleep_cpu();            \
138                         sleep_disable();        \
139                 } else {                        \
140                         sei();                  \
141                 }                               \
142                 ao_arch_nop();                  \
143                 cli();                          \
144         } while (0)
145
146 #define ao_arch_restore_stack() do { \
147                 uint8_t sp_l, sp_h;                                     \
148                 sp_l = (uint16_t) ao_cur_task->sp;                      \
149                 sp_h = ((uint16_t) ao_cur_task->sp) >> 8;               \
150                 asm("out __SP_H__,%0" : : "r" (sp_h) );                 \
151                 asm("out __SP_L__,%0" : : "r" (sp_l) );                 \
152                 asm("pop r0"    "\n\t"                                  \
153                     "out __SREG__, r0");                                \
154                 asm("pop r0" "\n\t" "pop r1" "\n\t" "pop r2" "\n\t" "pop r3" "\n\t" "pop r4"); \
155                 asm("pop r5" "\n\t" "pop r6" "\n\t" "pop r7" "\n\t" "pop r8" "\n\t" "pop r9"); \
156                 asm("pop r10" "\n\t" "pop r11" "\n\t" "pop r12" "\n\t" "pop r13" "\n\t" "pop r14"); \
157                 asm("pop r15" "\n\t" "pop r16" "\n\t" "pop r17" "\n\t" "pop r18" "\n\t" "pop r19"); \
158                 asm("pop r20" "\n\t" "pop r21" "\n\t" "pop r22" "\n\t" "pop r23" "\n\t" "pop r24"); \
159                 asm("pop r25" "\n\t" "pop r26" "\n\t" "pop r27" "\n\t" "pop r28" "\n\t" "pop r29"); \
160                 asm("pop r30" "\n\t" "pop r31");                        \
161                 asm("ret");                                             \
162         } while(0)
163
164 #define ao_arch_critical(b) do { cli(); do { b } while (0); sei(); } while (0)
165
166 #define ao_arch_block_interrupts()      cli()
167 #define ao_arch_release_interrupts()    sei()
168
169 #define AO_TELESCIENCE_NUM_ADC  12
170
171 #endif /* _AO_ARCH_H_ */
172