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