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