Move LCD string output code to ao_lcd_font.c
[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                 /* APSR */                                              \
74                 ARM_PUSH32(sp, 0);                                      \
75                                                                         \
76                 /* PRIMASK with interrupts enabled */                   \
77                 ARM_PUSH32(sp, 0);                                      \
78                                                                         \
79                 /* Return address (goes into LR) */                     \
80                 ARM_PUSH32(sp, a);                                      \
81                                                                         \
82                 /* Clear register values r0-r12 */                      \
83                 i = 13;                                                 \
84                 while (i--)                                             \
85                         ARM_PUSH32(sp, 0);                              \
86                                                                         \
87                 task->sp = sp;                                          \
88 } while (0);
89         
90 #define ao_arch_save_regs()     do {                                    \
91                 uint32_t        apsr;                                   \
92                 uint32_t        primask;                                \
93                                                                         \
94                 /* Save APSR */                                         \
95                 asm("mrs %0,apsr" : "=&r" (apsr));                      \
96                 asm("push {%0}" : : "r" (apsr));                        \
97                                                                         \
98                 /* Save PRIMASK */                                      \
99                 asm("mrs %0,primask" : "=&r" (primask));                \
100                 asm("push {%0}" : : "r" (primask));                     \
101                                                                         \
102                 /* Save general registers */                            \
103                 asm("push {r0-r12,lr}\n");                              \
104         } while (0)
105
106 #define ao_arch_save_stack() do {                                       \
107                 uint32_t        *sp;                                    \
108                 asm("mov %0,sp" : "=&r" (sp) );                         \
109                 ao_cur_task->sp = (sp);                                 \
110                 if ((uint8_t *) sp < ao_cur_task->stack)                \
111                         ao_panic (AO_PANIC_STACK);                      \
112         } while (0)
113
114 #define ao_arch_isr_stack()     /* nothing */
115
116 #define ao_arch_cpu_idle() do {                 \
117                 asm("wfi");                     \
118         } while (0)
119
120 #define ao_arch_restore_stack() do { \
121                 uint32_t        sp;                                     \
122                 uint32_t        primask;                                \
123                 uint32_t        apsr;                                   \
124                 sp = (uint32_t) ao_cur_task->sp;                        \
125                                                                         \
126                 /* Switch stacks */                                     \
127                 asm("mov sp, %0" : : "r" (sp) );                        \
128                                                                         \
129                 /* Restore general registers */                         \
130                 asm("pop {r0-r12,lr}\n");                               \
131                                                                         \
132                 /* Restore PRIMASK */                                   \
133                 asm("pop {%0}" : "=&r" (primask) );                     \
134                 asm("msr primask,%0" : : "r" (primask) );               \
135                                                                         \
136                 /* Restore APSR */                                      \
137                 asm("pop {%0}" : "=&r" (apsr) );                        \
138                 asm("msr apsr,%0" : : "r" (apsr) );                     \
139                                                                         \
140                 /* Return to calling function */                        \
141                 asm("bx lr");                                           \
142         } while(0)
143
144 #define ao_arch_critical(b) do { cli(); do { b } while (0); sei(); } while (0)
145
146 #define AO_ARM_NUM_ADC  12
147
148 struct ao_adc {
149         uint16_t        tick;                   /* tick when the sample was read */
150         uint16_t        adc[AO_ARM_NUM_ADC];    /* samples */
151 };
152
153 /*
154  * For now, we're running at a weird frequency
155  */
156 #define STM_APB1        (16000000 * 6 / 4)
157
158 void ao_lcd_stm_init(void);
159
160 void ao_lcd_font_init(void);
161
162 void ao_lcd_font_string(char *s);
163
164 #endif /* _AO_ARCH_H_ */
165