Add stm-demo program
[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   256
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                 uint16_t        a = (uint16_t) start;                   \
71                 int             i;                                      \
72                                                                         \
73                 /* Return address */                                    \
74                 ARM_PUSH32(sp, a);                                      \
75                                                                         \
76                 /* Invalid link register */                             \
77                 ARM_PUSH32(sp, 0xffffffff);                             \
78                                                                         \
79                 /* Clear register values  */                            \
80                 i = 13;                                                 \
81                 while (i--)                                             \
82                         ARM_PUSH32(sp, 0);                              \
83                                                                         \
84                 /* PSR with interrupts enabled */                       \
85                 ARM_PUSH32(sp, 0x01000000);                             \
86                 task->sp = sp;                                          \
87 } while (0);
88         
89 #define ao_arch_save_regs() do {                                        \
90                 asm("push {r0-r12,lr}\n");                              \
91                 cli();                                                  \
92                 asm("mrs r0,psr" "\n\t" "push {r0}");                   \
93                 sei();                                                  \
94         } while (0)
95
96 #define ao_arch_save_stack() do {                                       \
97                 uint32_t        sp;                                     \
98                 asm("mov %0,sp" : "=&r" (sp) );                         \
99                 ao_cur_task->sp = (uint32_t *) (sp);                    \
100         } while (0)
101
102 #define ao_arch_isr_stack()     /* nothing */
103
104 #define ao_arch_cpu_idle() do {                 \
105                 asm("wfi");                     \
106         } while (0)
107
108 #define ao_arch_restore_stack() do { \
109                 uint32_t        sp;                                     \
110                 sp = (uint32_t) ao_cur_task->sp;                        \
111                 cli();                                                  \
112                 asm("mov sp, %0" : : "r" (sp) );                        \
113                 asm("pop {r0}" "\n\t" "msr psr,r0");                    \
114                 asm("pop {r0-r12,lr}\n");                               \
115                 asm("bx lr");                                           \
116         } while(0)
117
118 #define ao_arch_critical(b) do { cli(); do { b } while (0); sei(); } while (0)
119
120 #define AO_ARM_NUM_ADC  12
121
122 struct ao_adc {
123         uint16_t        tick;                   /* tick when the sample was read */
124         uint16_t        adc[AO_ARM_NUM_ADC];    /* samples */
125 };
126
127 /*
128  * For now, we're running at a weird frequency
129  */
130 #define STM_APB1        (16000000 * 6 / 4)
131
132 #endif /* _AO_ARCH_H_ */
133