altos: Declare task stack as union of uint8_t and uint32_t
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _AO_ARCH_H_
20 #define _AO_ARCH_H_
21
22 #include <stdio.h>
23 #include <avr/io.h>
24 #include <avr/interrupt.h>
25 #include <avr/sleep.h>
26
27 #ifdef AVR_DEMO
28 #define TEENSY 1
29 #endif
30
31 #if TEENSY
32 #define F_CPU 16000000UL        // 16 MHz
33 #else
34 #define F_CPU  8000000UL        // 8 MHz
35 #endif
36
37 /*
38  * AVR definitions and code fragments for AltOS
39  */
40
41 #ifndef AO_STACK_SIZE
42 #define AO_STACK_SIZE   116
43 #endif
44
45 #define AO_PORT_TYPE    uint8_t
46
47 /* Various definitions to make GCC look more like SDCC */
48
49 #define ao_arch_naked_declare   __attribute__((naked))
50 #define ao_arch_naked_define
51 #define __interrupt(n)
52 #define __at(n)
53
54 #define ao_arch_reboot()        /* XXX */
55
56 #define ao_arch_nop()           asm("nop")
57
58 #define ao_arch_interrupt(n)    /* nothing */
59
60 #undef putchar
61 #undef getchar
62 #define putchar(c)      ao_putchar(c)
63 #define getchar         ao_getchar
64
65 extern void putchar(char c);
66 extern char getchar(void);
67 extern void ao_avr_stdio_init(void);
68
69 #define AO_ROMCONFIG_VERSION    2
70
71 #define AO_ROMCONFIG_SYMBOL(a) const
72
73 extern AO_ROMCONFIG_SYMBOL(0) uint16_t ao_serial_number;
74
75 #define AVR_PUSH8(stack, val)   (*((stack)--) = (val))
76
77 extern uint8_t  ao_cpu_sleep_disable;
78
79 #define ao_arch_task_globals    uint8_t ao_cpu_sleep_disable;
80
81 #define ao_arch_init_stack(task, start) \
82         do {                                                            \
83                 uint8_t         *sp = task->stack8 + AO_STACK_SIZE - 1; \
84                 uint16_t        a = (uint16_t) start;                   \
85                 int             i;                                      \
86                                                                         \
87                 /* Return address */                                    \
88                 AVR_PUSH8(sp, a);                                       \
89                 AVR_PUSH8(sp, (a >> 8));                                \
90                                                                         \
91                 /* Clear register values */                             \
92                 i = 32;                                                 \
93                 while (i--)                                             \
94                         AVR_PUSH8(sp, 0);                               \
95                                                                         \
96                 /* SREG with interrupts enabled */                      \
97                 AVR_PUSH8(sp, 0x80);                                    \
98                 task->sp8 = sp;                                         \
99         } while (0);
100         
101 #define ao_arch_save_regs() do {                                        \
102                 asm("push r31" "\n\t" "push r30");                      \
103                 asm("push r29" "\n\t" "push r28" "\n\t" "push r27" "\n\t" "push r26" "\n\t" "push r25"); \
104                 asm("push r24" "\n\t" "push r23" "\n\t" "push r22" "\n\t" "push r21" "\n\t" "push r20"); \
105                 asm("push r19" "\n\t" "push r18" "\n\t" "push r17" "\n\t" "push r16" "\n\t" "push r15"); \
106                 asm("push r14" "\n\t" "push r13" "\n\t" "push r12" "\n\t" "push r11" "\n\t" "push r10"); \
107                 asm("push r9" "\n\t" "push r8" "\n\t" "push r7" "\n\t" "push r6" "\n\t" "push r5"); \
108                 asm("push r4" "\n\t" "push r3" "\n\t" "push r2" "\n\t" "push r1" "\n\t" "push r0"); \
109                 asm("in r0, __SREG__" "\n\t" "push r0");                \
110         } while (0)
111
112 #define ao_arch_save_stack() do {                                       \
113                 uint8_t sp_l, sp_h;                                     \
114                 asm("in %0,__SP_L__" : "=&r" (sp_l) );                  \
115                 asm("in %0,__SP_H__" : "=&r" (sp_h) );                  \
116                 ao_cur_task->sp8 = (uint8_t *) ((uint16_t) sp_l | ((uint16_t) sp_h << 8)); \
117         } while (0)
118
119 #define ao_arch_isr_stack()     /* nothing */
120
121 /* Idle the CPU (if possible) waiting for an interrupt. Enabling
122  * interrupts and sleeping the CPU must be adjacent to eliminate race
123  * conditions. In all cases, we execute a single nop with interrupts
124  * enabled
125  */
126 #define ao_arch_wait_interrupt() do {           \
127                 if (!ao_cpu_sleep_disable) {    \
128                         sleep_enable();         \
129                         sei();                  \
130                         sleep_cpu();            \
131                         sleep_disable();        \
132                 } else {                        \
133                         sei();                  \
134                 }                               \
135                 ao_arch_nop();                  \
136                 cli();                          \
137         } while (0)
138
139 #define ao_arch_restore_stack() do { \
140                 uint8_t sp_l, sp_h;                                     \
141                 sp_l = (uint16_t) ao_cur_task->sp8;                     \
142                 sp_h = ((uint16_t) ao_cur_task->sp8) >> 8;              \
143                 asm("out __SP_H__,%0" : : "r" (sp_h) );                 \
144                 asm("out __SP_L__,%0" : : "r" (sp_l) );                 \
145                 asm("pop r0"    "\n\t"                                  \
146                     "out __SREG__, r0");                                \
147                 asm("pop r0" "\n\t" "pop r1" "\n\t" "pop r2" "\n\t" "pop r3" "\n\t" "pop r4"); \
148                 asm("pop r5" "\n\t" "pop r6" "\n\t" "pop r7" "\n\t" "pop r8" "\n\t" "pop r9"); \
149                 asm("pop r10" "\n\t" "pop r11" "\n\t" "pop r12" "\n\t" "pop r13" "\n\t" "pop r14"); \
150                 asm("pop r15" "\n\t" "pop r16" "\n\t" "pop r17" "\n\t" "pop r18" "\n\t" "pop r19"); \
151                 asm("pop r20" "\n\t" "pop r21" "\n\t" "pop r22" "\n\t" "pop r23" "\n\t" "pop r24"); \
152                 asm("pop r25" "\n\t" "pop r26" "\n\t" "pop r27" "\n\t" "pop r28" "\n\t" "pop r29"); \
153                 asm("pop r30" "\n\t" "pop r31");                        \
154                 asm("ret");                                             \
155         } while(0)
156
157 #define ao_arch_critical(b) do { cli(); do { b } while (0); sei(); } while (0)
158
159 #define ao_arch_block_interrupts()      cli()
160 #define ao_arch_release_interrupts()    sei()
161
162 #define AO_TELESCIENCE_NUM_ADC  12
163
164 #endif /* _AO_ARCH_H_ */
165