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