ce3a22e262e6ce8c7a7c330c3aa66b732b9b4620
[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 ao_arch_reboot()        /* XXX */
46
47 #define ao_arch_nop()           asm("nop")
48
49 #define ao_arch_interrupt(n)    /* nothing */
50
51 #undef putchar
52 #undef getchar
53 #define putchar(c)      ao_putchar(c)
54 #define getchar         ao_getchar
55
56 extern void putchar(char c);
57 extern char getchar(void);
58 extern void ao_avr_stdio_init(void);
59
60 extern const uint16_t ao_serial_number;
61
62 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
63
64 #define ao_arch_task_members\
65         uint32_t *sp;                   /* saved stack pointer */
66
67 #define cli()   asm("cpsid i")
68 #define sei()   asm("cpsie i")
69
70 #define ao_arch_init_stack(task, start) do {                            \
71                 uint32_t        *sp = (uint32_t *) (task->stack + AO_STACK_SIZE); \
72                 uint32_t        a = (uint32_t) start;                   \
73                 int             i;                                      \
74                                                                         \
75                 /* APSR */                                              \
76                 ARM_PUSH32(sp, 0);                                      \
77                                                                         \
78                 /* PRIMASK with interrupts enabled */                   \
79                 ARM_PUSH32(sp, 0);                                      \
80                                                                         \
81                 /* Return address (goes into LR) */                     \
82                 ARM_PUSH32(sp, a);                                      \
83                                                                         \
84                 /* Clear register values r0-r12 */                      \
85                 i = 13;                                                 \
86                 while (i--)                                             \
87                         ARM_PUSH32(sp, 0);                              \
88                                                                         \
89                 task->sp = sp;                                          \
90 } while (0);
91         
92 #define ao_arch_save_regs()     do {                                    \
93                 uint32_t        apsr;                                   \
94                 uint32_t        primask;                                \
95                                                                         \
96                 /* Save APSR */                                         \
97                 asm("mrs %0,apsr" : "=&r" (apsr));                      \
98                 asm("push {%0}" : : "r" (apsr));                        \
99                                                                         \
100                 /* Save PRIMASK */                                      \
101                 asm("mrs %0,primask" : "=&r" (primask));                \
102                 asm("push {%0}" : : "r" (primask));                     \
103                                                                         \
104                 /* Save general registers */                            \
105                 asm("push {r0-r12,lr}\n");                              \
106         } while (0)
107
108 #define ao_arch_save_stack() do {                                       \
109                 uint32_t        *sp;                                    \
110                 asm("mov %0,sp" : "=&r" (sp) );                         \
111                 ao_cur_task->sp = (sp);                                 \
112                 if ((uint8_t *) sp < ao_cur_task->stack)                \
113                         ao_panic (AO_PANIC_STACK);                      \
114         } while (0)
115
116 #define ao_arch_isr_stack()     /* nothing */
117
118 #define ao_arch_cpu_idle() do {                 \
119                 asm("wfi");                     \
120         } while (0)
121
122 #define ao_arch_restore_stack() do { \
123                 uint32_t        sp;                                     \
124                 uint32_t        primask;                                \
125                 uint32_t        apsr;                                   \
126                 sp = (uint32_t) ao_cur_task->sp;                        \
127                                                                         \
128                 /* Switch stacks */                                     \
129                 asm("mov sp, %0" : : "r" (sp) );                        \
130                                                                         \
131                 /* Restore general registers */                         \
132                 asm("pop {r0-r12,lr}\n");                               \
133                                                                         \
134                 /* Restore PRIMASK */                                   \
135                 asm("pop {%0}" : "=&r" (primask) );                     \
136                 asm("msr primask,%0" : : "r" (primask) );               \
137                                                                         \
138                 /* Restore APSR */                                      \
139                 asm("pop {%0}" : "=&r" (apsr) );                        \
140                 asm("msr apsr,%0" : : "r" (apsr) );                     \
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 #define STM_APB1        (16000000 * 6 / 4)
152
153 void ao_lcd_stm_init(void);
154
155 void ao_lcd_font_init(void);
156
157 void ao_lcd_font_string(char *s);
158
159 #define USE_SERIAL_STDIN        (USE_SERIAL_1_STDIN + USE_SERIAL_2_STDIN + USE_SERIAL_3_STDIN)
160
161 char
162 ao_serial1_getchar(void);
163
164 void
165 ao_serial1_putchar(char c);
166
167 char
168 ao_serial1_pollchar(void);
169
170 void
171 ao_serial1_set_speed(uint8_t speed);
172
173 char
174 ao_serial2_getchar(void);
175
176 void
177 ao_serial2_putchar(char c);
178
179 char
180 ao_serial2_pollchar(void);
181
182 void
183 ao_serial2_set_speed(uint8_t speed);
184
185 char
186 ao_serial3_getchar(void);
187
188 void
189 ao_serial3_putchar(char c);
190
191 char
192 ao_serial3_pollchar(void);
193
194 void
195 ao_serial3_set_speed(uint8_t speed);
196
197 extern uint32_t ao_radio_cal;
198
199 void
200 ao_adc_init();
201
202 #endif /* _AO_ARCH_H_ */
203