Basic OS running on STM32L
[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 /* Various definitions to make GCC look more like SDCC */
31
32 #define ao_arch_naked_declare   __attribute__((naked))
33 #define ao_arch_naked_define
34 #define __pdata
35 #define __data
36 #define __xdata
37 #define __code const
38 #define __reentrant
39 #define __critical
40 #define __interrupt(n)
41 #define __at(n)
42
43 #define ao_arch_reboot()        /* XXX */
44
45 #define ao_arch_nop()           asm("nop")
46
47 #define ao_arch_interrupt(n)    /* nothing */
48
49 #undef putchar
50 #undef getchar
51 #define putchar(c)      ao_putchar(c)
52 #define getchar         ao_getchar
53
54 extern void putchar(char c);
55 extern char getchar(void);
56 extern void ao_avr_stdio_init(void);
57
58 extern const uint16_t ao_serial_number;
59
60 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
61
62 #define ao_arch_task_members\
63         uint32_t *sp;                   /* saved stack pointer */
64
65 #define cli()   asm("cpsid i")
66 #define sei()   asm("cpsie i")
67
68 #define ao_arch_init_stack(task, start) do {                            \
69                 uint32_t        *sp = (uint32_t *) (task->stack + AO_STACK_SIZE); \
70                 uint32_t        a = (uint32_t) start;                   \
71                 int             i;                                      \
72                                                                         \
73                 /* Return address (goes into LR) */                     \
74                 ARM_PUSH32(sp, a);                                      \
75                                                                         \
76                 /* Clear register values  */                            \
77                 i = 13;                                                 \
78                 while (i--)                                             \
79                         ARM_PUSH32(sp, 0);                              \
80                                                                         \
81                 /* APSR */                                              \
82                 ARM_PUSH32(sp, 0);                                      \
83                 task->sp = sp;                                          \
84 } while (0);
85         
86 #define ao_arch_save_regs() do {                                        \
87                 asm("push {r0-r12,lr}\n");                              \
88                 cli();                                                  \
89                 asm("mrs r0,apsr" "\n\t" "push {r0}");                  \
90                 sei();                                                  \
91         } while (0)
92
93 #define ao_arch_save_stack() do {                                       \
94                 uint32_t        sp;                                     \
95                 asm("mov %0,sp" : "=&r" (sp) );                         \
96                 ao_cur_task->sp = (uint32_t *) (sp);                    \
97                 if ((uint8_t *) ao_cur_task->sp < ao_cur_task->stack)   \
98                         ao_panic (AO_PANIC_STACK);                      \
99         } while (0)
100
101 #define ao_arch_isr_stack()     /* nothing */
102
103 #define ao_arch_cpu_idle() do {                 \
104                 asm("wfi");                     \
105         } while (0)
106
107 #define ao_arch_restore_stack() do { \
108                 uint32_t        sp;                                     \
109                 sp = (uint32_t) ao_cur_task->sp;                        \
110                 cli();                                                  \
111                 asm("mov sp, %0" : : "r" (sp) );                        \
112                 asm("pop {r0}" "\n\t" "msr apsr,r0");                   \
113                 asm("pop {r0-r12,lr}\n");                               \
114                 asm("bx lr");                                           \
115         } while(0)
116
117 #define ao_arch_critical(b) do { cli(); do { b } while (0); sei(); } while (0)
118
119 #define AO_ARM_NUM_ADC  12
120
121 struct ao_adc {
122         uint16_t        tick;                   /* tick when the sample was read */
123         uint16_t        adc[AO_ARM_NUM_ADC];    /* samples */
124 };
125
126 /*
127  * For now, we're running at a weird frequency
128  */
129 #define STM_APB1        (16000000 * 6 / 4)
130
131 #endif /* _AO_ARCH_H_ */
132