altos: Be more careful about register save/restore in ao_yield
[fw/altos] / src / stm / ao_arch.h
1 /*
2  * Copyright © 2012 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 <stm32l.h>
23
24 /*
25  * STM32L definitions and code fragments for AltOS
26  */
27
28 #define AO_STACK_SIZE   1024
29
30 #define AO_LED_TYPE     uint16_t
31
32 /* Various definitions to make GCC look more like SDCC */
33
34 #define ao_arch_naked_declare   __attribute__((naked))
35 #define ao_arch_naked_define
36 #define __pdata
37 #define __data
38 #define __xdata
39 #define __code const
40 #define __reentrant
41 #define __critical
42 #define __interrupt(n)
43 #define __at(n)
44
45 #define CORTEX_M3_AIRCR         ((uint32_t *) 0xe000ed0c)
46
47 #define ao_arch_reboot()        (*((uint32_t *) 0xe000ed0c) = 0x05fa0004)
48
49 #define ao_arch_nop()           asm("nop")
50
51 #define ao_arch_interrupt(n)    /* nothing */
52
53 #undef putchar
54 #undef getchar
55 #define putchar(c)      ao_putchar(c)
56 #define getchar         ao_getchar
57
58 extern void putchar(char c);
59 extern char getchar(void);
60 extern void ao_avr_stdio_init(void);
61
62 extern const uint16_t ao_serial_number;
63
64 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
65
66 #define ao_arch_task_members\
67         uint32_t *sp;                   /* saved stack pointer */
68
69 #define cli()   asm("cpsid i")
70 #define sei()   asm("cpsie i")
71
72 #define ao_arch_init_stack(task, start) do {                            \
73                 uint32_t        *sp = (uint32_t *) (task->stack + AO_STACK_SIZE); \
74                 uint32_t        a = (uint32_t) start;                   \
75                 int             i;                                      \
76                                                                         \
77                 /* Return address (goes into LR) */                     \
78                 ARM_PUSH32(sp, a);                                      \
79                                                                         \
80                 /* Clear register values r0-r12 */                      \
81                 i = 13;                                                 \
82                 while (i--)                                             \
83                         ARM_PUSH32(sp, 0);                              \
84                                                                         \
85                 /* APSR */                                              \
86                 ARM_PUSH32(sp, 0);                                      \
87                                                                         \
88                 /* PRIMASK with interrupts enabled */                   \
89                 ARM_PUSH32(sp, 0);                                      \
90                                                                         \
91                 task->sp = sp;                                          \
92 } while (0);
93         
94 #define ao_arch_save_regs()     do {                                    \
95                 /* Save general registers */                            \
96                 asm("push {r0-r12,lr}\n");                              \
97                                                                         \
98                 /* Save APSR */                                         \
99                 asm("mrs r0,apsr");                                     \
100                 asm("push {r0}");                                       \
101                                                                         \
102                 /* Save PRIMASK */                                      \
103                 asm("mrs r0,primask");                                  \
104                 asm("push {r0}");                                       \
105                                                                         \
106                 /* Enable interrupts */                                 \
107                 sei();                                                  \
108         } while (0)
109
110 #define ao_arch_save_stack() do {                                       \
111                 uint32_t        *sp;                                    \
112                 asm("mov %0,sp" : "=&r" (sp) );                         \
113                 ao_cur_task->sp = (sp);                                 \
114                 if ((uint8_t *) sp < ao_cur_task->stack)                \
115                         ao_panic (AO_PANIC_STACK);                      \
116         } while (0)
117
118 #define ao_arch_isr_stack()     /* nothing */
119
120 #define ao_arch_cpu_idle() do {                 \
121                 asm("wfi");                     \
122         } while (0)
123
124 #define ao_arch_restore_stack() do { \
125                 uint32_t        sp;                                     \
126                 sp = (uint32_t) ao_cur_task->sp;                        \
127                                                                         \
128                 /* Switch stacks */                                     \
129                 asm("mov sp, %0" : : "r" (sp) );                        \
130                                                                         \
131                 /* Restore PRIMASK */                                   \
132                 asm("pop {r0}");                                        \
133                 asm("msr primask,r0");                                  \
134                                                                         \
135                 /* Restore APSR */                                      \
136                 asm("pop {r0}");                                        \
137                 asm("msr apsr,r0");                                     \
138                                                                         \
139                 /* Restore general registers */                         \
140                 asm("pop {r0-r12,lr}\n");                               \
141                                                                         \
142                 /* Return to calling function */                        \
143                 asm("bx lr");                                           \
144         } while(0)
145
146 #define ao_arch_critical(b) do { cli(); do { b } while (0); sei(); } while (0)
147
148 /*
149  * For now, we're running at a weird frequency
150  */
151
152 #if AO_HSE
153 #define AO_PLLSRC       AO_HSE
154 #else
155 #define AO_PLLSRC       STM_HSI_FREQ
156 #endif
157
158 #define AO_PLLVCO       (AO_PLLSRC * AO_PLLMUL)
159 #define AO_SYSCLK       (AO_PLLVCO / AO_PLLDIV)
160 #define AO_HCLK         (AO_SYSCLK / AO_AHB_PRESCALER)
161 #define AO_PCLK1        (AO_HCLK / AO_APB1_PRESCALER)
162 #define AO_PCLK2        (AO_HCLK / AO_APB2_PRESCALER)
163
164 #if AO_APB1_PRESCALER == 1
165 #define AO_TIM23467_CLK         AO_PCLK1
166 #else
167 #define AO_TIM23467_CLK         (2 * AO_PCLK1)
168 #endif
169
170 #if AO_APB2_PRESCALER == 1
171 #define AO_TIM91011_CLK         AO_PCLK2
172 #else
173 #define AO_TIM91011_CLK         (2 * AO_PCLK2)
174 #endif
175
176 void ao_lcd_stm_init(void);
177
178 void ao_lcd_font_init(void);
179
180 void ao_lcd_font_string(char *s);
181
182 char
183 ao_serial1_getchar(void);
184
185 void
186 ao_serial1_putchar(char c);
187
188 char
189 ao_serial1_pollchar(void);
190
191 void
192 ao_serial1_set_speed(uint8_t speed);
193
194 char
195 ao_serial2_getchar(void);
196
197 void
198 ao_serial2_putchar(char c);
199
200 char
201 ao_serial2_pollchar(void);
202
203 void
204 ao_serial2_set_speed(uint8_t speed);
205
206 char
207 ao_serial3_getchar(void);
208
209 void
210 ao_serial3_putchar(char c);
211
212 char
213 ao_serial3_pollchar(void);
214
215 void
216 ao_serial3_set_speed(uint8_t speed);
217
218 extern uint32_t ao_radio_cal;
219
220 void
221 ao_adc_init();
222
223 #endif /* _AO_ARCH_H_ */
224