stm32f1: Get boot loader working
[fw/altos] / src / stm32f1 / ao_arch_funcs.h
1 /*
2  * Copyright © 2023 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, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _AO_ARCH_FUNCS_H_
20 #define _AO_ARCH_FUNCS_H_
21
22 #define AO_EXTI_MODE_RISING     1
23 #define AO_EXTI_MODE_FALLING    2
24 #define AO_EXTI_MODE_PULL_NONE  0
25 #define AO_EXTI_MODE_PULL_UP    4
26 #define AO_EXTI_MODE_PULL_DOWN  8
27 #define AO_EXTI_PRIORITY_LOW    16
28 #define AO_EXTI_PRIORITY_MED    0
29 #define AO_EXTI_PRIORITY_HIGH   32
30 #define AO_EXTI_PIN_NOCONFIGURE 64
31
32 static inline void
33 ao_enable_port(struct stm_gpio *port)
34 {
35         if ((port) == &stm_gpioa)
36                 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_IOPAEN);
37         else if ((port) == &stm_gpiob)
38                 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_IOPBEN);
39         else if ((port) == &stm_gpioc)
40                 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_IOPCEN);
41         else if ((port) == &stm_gpiod)
42                 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_IOPDEN);
43         else if ((port) == &stm_gpioe)
44                 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_IOPEEN);
45 }
46
47 static inline void
48 ao_disable_port(struct stm_gpio *port)
49 {
50         if ((port) == &stm_gpioa)
51                 stm_rcc.apb2enr &= ~(1UL << STM_RCC_APB2ENR_IOPAEN);
52         else if ((port) == &stm_gpiob)
53                 stm_rcc.apb2enr &= ~(1UL << STM_RCC_APB2ENR_IOPBEN);
54         else if ((port) == &stm_gpioc)
55                 stm_rcc.apb2enr &= ~(1UL << STM_RCC_APB2ENR_IOPCEN);
56         else if ((port) == &stm_gpiod)
57                 stm_rcc.apb2enr &= ~(1UL << STM_RCC_APB2ENR_IOPDEN);
58         else if ((port) == &stm_gpioe)
59                 stm_rcc.apb2enr &= ~(1UL << STM_RCC_APB2ENR_IOPEEN);
60 }
61
62 #define ao_gpio_set(port, bit, v) stm_gpio_set(port, bit, v)
63
64 #define ao_gpio_get(port, bit) stm_gpio_get(port, bit)
65
66 #define ao_gpio_set_bits(port, bits) stm_gpio_set_bits(port, bits)
67
68 #define ao_gpio_set_mask(port, bits, mask) stm_gpio_set_mask(port, bits, mask)
69
70 #define ao_gpio_clr_bits(port, bits) stm_gpio_clr_bits(port, bits);
71
72 #define ao_gpio_get_all(port) stm_gpio_get_all(port)
73
74 static inline void
75 ao_enable_output(struct stm_gpio *port, int bit, uint8_t v)
76 {
77         ao_enable_port(port);
78         ao_gpio_set(port, bit, v);
79         stm_gpio_conf(port, bit,
80                       STM_GPIO_CR_MODE_OUTPUT_10MHZ,
81                       STM_GPIO_CR_CNF_OUTPUT_PUSH_PULL);
82 }
83
84 static inline void
85 ao_gpio_set_mode(struct stm_gpio *port, int bit, int mode)
86 {
87         uint8_t cnf;
88
89         if (mode == AO_EXTI_MODE_PULL_NONE)
90                 cnf = STM_GPIO_CR_CNF_INPUT_FLOATING;
91         else {
92                 cnf = STM_GPIO_CR_CNF_INPUT_PULL;
93                 ao_gpio_set(port, bit, mode == AO_EXTI_MODE_PULL_UP);
94         }
95         stm_gpio_conf(port, bit,
96                       STM_GPIO_CR_MODE_INPUT,
97                       cnf);
98 }
99
100 static inline void
101 ao_enable_input(struct stm_gpio *port, int bit, int mode)
102 {
103         ao_enable_port(port);
104         ao_gpio_set_mode(port, bit, mode);
105 }
106
107 #if USE_SERIAL_1_SW_FLOW || USE_SERIAL_2_SW_FLOW || USE_SERIAL_3_SW_FLOW
108 #define HAS_SERIAL_SW_FLOW 1
109 #else
110 #define HAS_SERIAL_SW_FLOW 0
111 #endif
112
113 #if USE_SERIAL_1_FLOW && !USE_SERIAL_1_SW_FLOW || USE_SERIAL_2_FLOW && !USE_SERIAL_2_SW_FLOW || USE_SERIAL_3_FLOW && !USE_SERIAL_3_SW_FLOW
114 #define HAS_SERIAL_HW_FLOW 1
115 #else
116 #define HAS_SERIAL_HW_FLOW 0
117 #endif
118
119 /* ao_serial_stm.c */
120 struct ao_stm_usart {
121         struct ao_fifo          rx_fifo;
122         struct ao_fifo          tx_fifo;
123         struct stm_usart        *reg;
124         uint8_t                 tx_running;
125         uint8_t                 draining;
126 #if HAS_SERIAL_SW_FLOW
127         /* RTS - 0 if we have FIFO space, 1 if not
128          * CTS - 0 if we can send, 0 if not
129          */
130         struct stm_gpio         *gpio_rts;
131         struct stm_gpio         *gpio_cts;
132         uint8_t                 pin_rts;
133         uint8_t                 pin_cts;
134         uint8_t                 rts;
135 #endif
136 };
137
138 void
139 ao_debug_out(char c);
140
141 #if HAS_SERIAL_1
142 extern struct ao_stm_usart      ao_stm_usart1;
143 #endif
144
145 #if HAS_SERIAL_2
146 extern struct ao_stm_usart      ao_stm_usart2;
147 #endif
148
149 #if HAS_SERIAL_3
150 extern struct ao_stm_usart      ao_stm_usart3;
151 #endif
152
153 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
154
155 typedef uint32_t        ao_arch_irq_t;
156
157 static inline void
158 ao_arch_block_interrupts(void) {
159 #ifdef AO_NONMASK_INTERRUPTS
160         asm("msr basepri,%0" : : "r" (AO_STM_NVIC_BASEPRI_MASK));
161 #else
162         asm("cpsid i");
163 #endif
164 }
165
166 static inline void
167 ao_arch_release_interrupts(void) {
168 #ifdef AO_NONMASK_INTERRUPTS
169         asm("msr basepri,%0" : : "r" (0x0));
170 #else
171         asm("cpsie i");
172 #endif
173 }
174
175 static inline uint32_t
176 ao_arch_irqsave(void) {
177         uint32_t        val;
178 #ifdef AO_NONMASK_INTERRUPTS
179         asm("mrs %0,basepri" : "=r" (val));
180 #else
181         asm("mrs %0,primask" : "=r" (val));
182 #endif
183         ao_arch_block_interrupts();
184         return val;
185 }
186
187 static inline void
188 ao_arch_irqrestore(uint32_t basepri) {
189 #ifdef AO_NONMASK_INTERRUPTS
190         asm("msr basepri,%0" : : "r" (basepri));
191 #else
192         asm("msr primask,%0" : : "r" (basepri));
193 #endif
194 }
195
196 static inline void
197 ao_arch_memory_barrier(void) {
198         asm volatile("" ::: "memory");
199 }
200
201 static inline void
202 ao_arch_irq_check(void) {
203 #ifdef AO_NONMASK_INTERRUPTS
204         uint32_t        basepri;
205         asm("mrs %0,basepri" : "=r" (basepri));
206         if (basepri == 0)
207                 ao_panic(AO_PANIC_IRQ);
208 #else
209         uint32_t        primask;
210         asm("mrs %0,primask" : "=r" (primask));
211         if ((primask & 1) == 0)
212                 ao_panic(AO_PANIC_IRQ);
213 #endif
214 }
215
216 #if HAS_TASK
217 static inline void
218 ao_arch_init_stack(struct ao_task *task, uint32_t *sp, void *start)
219 {
220         uint32_t        a = (uint32_t) start;
221         int             i;
222
223         /* Return address (goes into LR) */
224         ARM_PUSH32(sp, a);
225
226         /* Clear register values r0-r12 */
227         i = 13;
228         while (i--)
229                 ARM_PUSH32(sp, 0);
230
231         /* APSR */
232         ARM_PUSH32(sp, 0);
233
234         /* BASEPRI with interrupts enabled */
235         ARM_PUSH32(sp, 0);
236
237         task->sp32 = sp;
238 }
239
240 static inline void ao_arch_save_regs(void) {
241         /* Save general registers */
242         asm("push {r0-r12,lr}\n");
243
244         /* Save APSR */
245         asm("mrs r0,apsr");
246         asm("push {r0}");
247
248 #ifdef AO_NONMASK_INTERRUPTS
249         /* Save BASEPRI */
250         asm("mrs r0,basepri");
251 #else
252         /* Save PRIMASK */
253         asm("mrs r0,primask");
254 #endif
255         asm("push {r0}");
256 }
257
258 static inline void ao_arch_save_stack(void) {
259         uint32_t        *sp;
260         asm("mov %0,sp" : "=&r" (sp) );
261         ao_cur_task->sp32 = (sp);
262 }
263
264 static inline void ao_arch_restore_stack(void) {
265         /* Switch stacks */
266         asm("mov sp, %0" : : "r" (ao_cur_task->sp32) );
267
268 #ifdef AO_NONMASK_INTERRUPTS
269         /* Restore BASEPRI */
270         asm("pop {r0}");
271         asm("msr basepri,r0");
272 #else
273         /* Restore PRIMASK */
274         asm("pop {r0}");
275         asm("msr primask,r0");
276 #endif
277
278         /* Restore APSR */
279         asm("pop {r0}");
280         asm("msr apsr_nczvq,r0");
281
282         /* Restore general registers */
283         asm("pop {r0-r12,lr}\n");
284
285         /* Return to calling function */
286         asm("bx lr");
287 }
288 #define HAS_ARCH_START_SCHEDULER        1
289
290 static inline void ao_arch_start_scheduler(void) {
291         uint32_t        sp;
292         uint32_t        control;
293
294         asm("mrs %0,msp" : "=&r" (sp));
295         asm("msr psp,%0" : : "r" (sp));
296         asm("mrs %0,control" : "=r" (control));
297         control |= (1 << 1);
298         asm("msr control,%0" : : "r" (control));
299         asm("isb");
300 }
301
302 #define ao_arch_isr_stack()
303
304 #endif /* HAS_TASK */
305
306 static inline void
307 ao_arch_wait_interrupt(void) {
308 #ifdef AO_NONMASK_INTERRUPTS
309         asm(
310             "dsb\n"                     /* Serialize data */
311             "isb\n"                     /* Serialize instructions */
312             "cpsid i\n"                 /* Block all interrupts */
313             "msr basepri,%0\n"          /* Allow all interrupts through basepri */
314             "wfi\n"                     /* Wait for an interrupt */
315             "cpsie i\n"                 /* Allow all interrupts */
316             "msr basepri,%1\n"          /* Block interrupts through basepri */
317             : : "r" (0), "r" (AO_STM_NVIC_BASEPRI_MASK));
318 #else
319         asm("\twfi\n");
320         ao_arch_release_interrupts();
321         ao_arch_block_interrupts();
322 #endif
323 }
324
325 #define ao_arch_critical(b) do {                        \
326                 uint32_t __mask = ao_arch_irqsave();    \
327                 do { b } while (0);                     \
328                 ao_arch_irqrestore(__mask);             \
329         } while (0)
330
331 #define ao_arch_reboot() \
332         (stm_scb.aircr = ((STM_SCB_AIRCR_VECTKEY_KEY << STM_SCB_AIRCR_VECTKEY) | \
333                           (1 << STM_SCB_AIRCR_SYSRESETREQ)))
334
335 #endif /* _AO_ARCH_FUNCS_H_ */