Merge remote-tracking branch 'mjb/altosdroid'
[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 #define AO_TICK_SIGNED  int16_t
35 #endif
36
37 /* Various definitions to make GCC look more like SDCC */
38
39 #define ao_arch_naked_declare   __attribute__((naked))
40 #define ao_arch_naked_define
41 #define __pdata
42 #define __data
43 #define __xdata
44 #define __code const
45 #define __reentrant
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 ao_arch_block_interrupts()      asm("cpsid i")
86 #define ao_arch_release_interrupts()    asm("cpsie i")
87
88 #define cli()   ao_arch_block_interrupts()
89 #define sei()   ao_arch_release_interrupts()
90
91 static uint32_t
92 ao_arch_irqsave(void) {
93         uint32_t        primask;
94         asm("mrs %0,primask" : "=&r" (primask));
95         ao_arch_block_interrupts();
96         return primask;
97 }
98
99 static void
100 ao_arch_irqrestore(uint32_t primask) {
101         asm("msr primask,%0" : : "r" (primask));
102 }
103
104 static void
105 ao_arch_memory_barrier() {
106         asm volatile("" ::: "memory");
107 }
108
109 #define ao_arch_init_stack(task, start) do {                            \
110                 uint32_t        *sp = (uint32_t *) (task->stack + AO_STACK_SIZE); \
111                 uint32_t        a = (uint32_t) start;                   \
112                 int             i;                                      \
113                                                                         \
114                 /* Return address (goes into LR) */                     \
115                 ARM_PUSH32(sp, a);                                      \
116                                                                         \
117                 /* Clear register values r0-r12 */                      \
118                 i = 13;                                                 \
119                 while (i--)                                             \
120                         ARM_PUSH32(sp, 0);                              \
121                                                                         \
122                 /* APSR */                                              \
123                 ARM_PUSH32(sp, 0);                                      \
124                                                                         \
125                 /* PRIMASK with interrupts enabled */                   \
126                 ARM_PUSH32(sp, 0);                                      \
127                                                                         \
128                 task->sp = sp;                                          \
129 } while (0);
130         
131 #define ao_arch_save_regs()     do {                                    \
132                 /* Save general registers */                            \
133                 asm("push {r0-r12,lr}\n");                              \
134                                                                         \
135                 /* Save APSR */                                         \
136                 asm("mrs r0,apsr");                                     \
137                 asm("push {r0}");                                       \
138                                                                         \
139                 /* Save PRIMASK */                                      \
140                 asm("mrs r0,primask");                                  \
141                 asm("push {r0}");                                       \
142                                                                         \
143                 /* Enable interrupts */                                 \
144                 sei();                                                  \
145         } while (0)
146
147 #define ao_arch_save_stack() do {                                       \
148                 uint32_t        *sp;                                    \
149                 asm("mov %0,sp" : "=&r" (sp) );                         \
150                 ao_cur_task->sp = (sp);                                 \
151                 if ((uint8_t *) sp < &ao_cur_task->stack[0])            \
152                         ao_panic (AO_PANIC_STACK);                      \
153         } while (0)
154
155 #if 0
156 #define ao_arch_isr_stack() do {                                \
157                 uint32_t        *sp = (uint32_t *) 0x20004000;  \
158                 asm("mov %0,sp" : "=&r" (sp) );                 \
159         } while (0)
160 #else
161 #define ao_arch_isr_stack()
162 #endif
163
164
165 #define ao_arch_cpu_idle() do {                 \
166                 asm(".global ao_idle_loc\n\twfi\nao_idle_loc:");        \
167         } while (0)
168
169 #define ao_arch_restore_stack() do { \
170                 uint32_t        sp;                                     \
171                 sp = (uint32_t) ao_cur_task->sp;                        \
172                                                                         \
173                 /* Switch stacks */                                     \
174                 asm("mov sp, %0" : : "r" (sp) );                        \
175                                                                         \
176                 /* Restore PRIMASK */                                   \
177                 asm("pop {r0}");                                        \
178                 asm("msr primask,r0");                                  \
179                                                                         \
180                 /* Restore APSR */                                      \
181                 asm("pop {r0}");                                        \
182                 asm("msr apsr,r0");                                     \
183                                                                         \
184                 /* Restore general registers */                         \
185                 asm("pop {r0-r12,lr}\n");                               \
186                                                                         \
187                 /* Return to calling function */                        \
188                 asm("bx lr");                                           \
189         } while(0)
190
191 #define ao_arch_critical(b) do { cli(); do { b } while (0); sei(); } while (0)
192
193 /*
194  * For now, we're running at a weird frequency
195  */
196
197 #if AO_HSE
198 #define AO_PLLSRC       AO_HSE
199 #else
200 #define AO_PLLSRC       STM_HSI_FREQ
201 #endif
202
203 #define AO_PLLVCO       (AO_PLLSRC * AO_PLLMUL)
204 #define AO_SYSCLK       (AO_PLLVCO / AO_PLLDIV)
205 #define AO_HCLK         (AO_SYSCLK / AO_AHB_PRESCALER)
206 #define AO_PCLK1        (AO_HCLK / AO_APB1_PRESCALER)
207 #define AO_PCLK2        (AO_HCLK / AO_APB2_PRESCALER)
208
209 #if AO_APB1_PRESCALER == 1
210 #define AO_TIM23467_CLK         AO_PCLK1
211 #else
212 #define AO_TIM23467_CLK         (2 * AO_PCLK1)
213 #endif
214
215 #if AO_APB2_PRESCALER == 1
216 #define AO_TIM91011_CLK         AO_PCLK2
217 #else
218 #define AO_TIM91011_CLK         (2 * AO_PCLK2)
219 #endif
220
221 #define AO_STM_NVIC_HIGH_PRIORITY       4
222 #define AO_STM_NVIC_CLOCK_PRIORITY      6
223 #define AO_STM_NVIC_MED_PRIORITY        8
224 #define AO_STM_NVIC_LOW_PRIORITY        10
225
226 void ao_lcd_stm_init(void);
227
228 void ao_lcd_font_init(void);
229
230 void ao_lcd_font_string(char *s);
231
232 char
233 ao_serial1_getchar(void);
234
235 void
236 ao_serial1_putchar(char c);
237
238 char
239 ao_serial1_pollchar(void);
240
241 void
242 ao_serial1_set_speed(uint8_t speed);
243
244 char
245 ao_serial2_getchar(void);
246
247 void
248 ao_serial2_putchar(char c);
249
250 char
251 ao_serial2_pollchar(void);
252
253 void
254 ao_serial2_set_speed(uint8_t speed);
255
256 char
257 ao_serial3_getchar(void);
258
259 void
260 ao_serial3_putchar(char c);
261
262 char
263 ao_serial3_pollchar(void);
264
265 void
266 ao_serial3_set_speed(uint8_t speed);
267
268 extern const uint32_t   ao_radio_cal;
269
270 void
271 ao_adc_init();
272
273 #endif /* _AO_ARCH_H_ */
274