ad9ab2a6620407096ccd6b0186a1927a1b198720
[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 static inline void
108 ao_enable_cs(struct stm_gpio *port, int bit)
109 {
110         ao_enable_output(port, bit, 1);
111 }
112
113 #if USE_SERIAL_1_SW_FLOW || USE_SERIAL_2_SW_FLOW || USE_SERIAL_3_SW_FLOW
114 #define HAS_SERIAL_SW_FLOW 1
115 #else
116 #define HAS_SERIAL_SW_FLOW 0
117 #endif
118
119 #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
120 #define HAS_SERIAL_HW_FLOW 1
121 #else
122 #define HAS_SERIAL_HW_FLOW 0
123 #endif
124
125 /* ao_serial_stm.c */
126 struct ao_stm_usart {
127         struct ao_fifo          rx_fifo;
128         struct ao_fifo          tx_fifo;
129         struct stm_usart        *reg;
130         uint8_t                 tx_running;
131         uint8_t                 draining;
132 #if HAS_SERIAL_SW_FLOW
133         /* RTS - 0 if we have FIFO space, 1 if not
134          * CTS - 0 if we can send, 0 if not
135          */
136         struct stm_gpio         *gpio_rts;
137         struct stm_gpio         *gpio_cts;
138         uint8_t                 pin_rts;
139         uint8_t                 pin_cts;
140         uint8_t                 rts;
141 #endif
142 };
143
144 void
145 ao_debug_out(char c);
146
147 #if HAS_SERIAL_1
148 extern struct ao_stm_usart      ao_stm_usart1;
149 #endif
150
151 #if HAS_SERIAL_2
152 extern struct ao_stm_usart      ao_stm_usart2;
153 #endif
154
155 #if HAS_SERIAL_3
156 extern struct ao_stm_usart      ao_stm_usart3;
157 #endif
158
159 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
160
161 typedef uint32_t        ao_arch_irq_t;
162
163 static inline void
164 ao_arch_block_interrupts(void) {
165 #ifdef AO_NONMASK_INTERRUPTS
166         asm("msr basepri,%0" : : "r" (AO_STM_NVIC_BASEPRI_MASK));
167 #else
168         asm("cpsid i");
169 #endif
170 }
171
172 static inline void
173 ao_arch_release_interrupts(void) {
174 #ifdef AO_NONMASK_INTERRUPTS
175         asm("msr basepri,%0" : : "r" (0x0));
176 #else
177         asm("cpsie i");
178 #endif
179 }
180
181 static inline uint32_t
182 ao_arch_irqsave(void) {
183         uint32_t        val;
184 #ifdef AO_NONMASK_INTERRUPTS
185         asm("mrs %0,basepri" : "=r" (val));
186 #else
187         asm("mrs %0,primask" : "=r" (val));
188 #endif
189         ao_arch_block_interrupts();
190         return val;
191 }
192
193 static inline void
194 ao_arch_irqrestore(uint32_t basepri) {
195 #ifdef AO_NONMASK_INTERRUPTS
196         asm("msr basepri,%0" : : "r" (basepri));
197 #else
198         asm("msr primask,%0" : : "r" (basepri));
199 #endif
200 }
201
202 static inline void
203 ao_arch_memory_barrier(void) {
204         asm volatile("" ::: "memory");
205 }
206
207 static inline void
208 ao_arch_irq_check(void) {
209 #ifdef AO_NONMASK_INTERRUPTS
210         uint32_t        basepri;
211         asm("mrs %0,basepri" : "=r" (basepri));
212         if (basepri == 0)
213                 ao_panic(AO_PANIC_IRQ);
214 #else
215         uint32_t        primask;
216         asm("mrs %0,primask" : "=r" (primask));
217         if ((primask & 1) == 0)
218                 ao_panic(AO_PANIC_IRQ);
219 #endif
220 }
221
222 #if HAS_TASK
223 static inline void
224 ao_arch_init_stack(struct ao_task *task, uint32_t *sp, void *start)
225 {
226         uint32_t        a = (uint32_t) start;
227         int             i;
228
229         /* Return address (goes into LR) */
230         ARM_PUSH32(sp, a);
231
232         /* Clear register values r0-r12 */
233         i = 13;
234         while (i--)
235                 ARM_PUSH32(sp, 0);
236
237         /* APSR */
238         ARM_PUSH32(sp, 0);
239
240         /* BASEPRI with interrupts enabled */
241         ARM_PUSH32(sp, 0);
242
243         task->sp32 = sp;
244 }
245
246 static inline void ao_arch_save_regs(void) {
247         /* Save general registers */
248         asm("push {r0-r12,lr}\n");
249
250         /* Save APSR */
251         asm("mrs r0,apsr");
252         asm("push {r0}");
253
254 #ifdef AO_NONMASK_INTERRUPTS
255         /* Save BASEPRI */
256         asm("mrs r0,basepri");
257 #else
258         /* Save PRIMASK */
259         asm("mrs r0,primask");
260 #endif
261         asm("push {r0}");
262 }
263
264 static inline void ao_arch_save_stack(void) {
265         uint32_t        *sp;
266         asm("mov %0,sp" : "=&r" (sp) );
267         ao_cur_task->sp32 = (sp);
268 }
269
270 static inline void ao_arch_restore_stack(void) {
271         /* Switch stacks */
272         asm("mov sp, %0" : : "r" (ao_cur_task->sp32) );
273
274 #ifdef AO_NONMASK_INTERRUPTS
275         /* Restore BASEPRI */
276         asm("pop {r0}");
277         asm("msr basepri,r0");
278 #else
279         /* Restore PRIMASK */
280         asm("pop {r0}");
281         asm("msr primask,r0");
282 #endif
283
284         /* Restore APSR */
285         asm("pop {r0}");
286         asm("msr apsr_nczvq,r0");
287
288         /* Restore general registers */
289         asm("pop {r0-r12,lr}\n");
290
291         /* Return to calling function */
292         asm("bx lr");
293 }
294 #define HAS_ARCH_START_SCHEDULER        1
295
296 static inline void ao_arch_start_scheduler(void) {
297         uint32_t        sp;
298         uint32_t        control;
299
300         asm("mrs %0,msp" : "=&r" (sp));
301         asm("msr psp,%0" : : "r" (sp));
302         asm("mrs %0,control" : "=r" (control));
303         control |= (1 << 1);
304         asm("msr control,%0" : : "r" (control));
305         asm("isb");
306 }
307
308 #define ao_arch_isr_stack()
309
310 #endif /* HAS_TASK */
311
312 static inline void
313 ao_arch_wait_interrupt(void) {
314 #ifdef AO_NONMASK_INTERRUPTS
315         asm(
316             "dsb\n"                     /* Serialize data */
317             "isb\n"                     /* Serialize instructions */
318             "cpsid i\n"                 /* Block all interrupts */
319             "msr basepri,%0\n"          /* Allow all interrupts through basepri */
320             "wfi\n"                     /* Wait for an interrupt */
321             "cpsie i\n"                 /* Allow all interrupts */
322             "msr basepri,%1\n"          /* Block interrupts through basepri */
323             : : "r" (0), "r" (AO_STM_NVIC_BASEPRI_MASK));
324 #else
325         asm("\twfi\n");
326         ao_arch_release_interrupts();
327         ao_arch_block_interrupts();
328 #endif
329 }
330
331 #define ao_arch_critical(b) do {                        \
332                 uint32_t __mask = ao_arch_irqsave();    \
333                 do { b } while (0);                     \
334                 ao_arch_irqrestore(__mask);             \
335         } while (0)
336
337 #define ao_arch_reboot() \
338         (stm_scb.aircr = ((STM_SCB_AIRCR_VECTKEY_KEY << STM_SCB_AIRCR_VECTKEY) | \
339                           (1 << STM_SCB_AIRCR_SYSRESETREQ)))
340
341 /* ao_dma_stm.c
342  */
343
344 extern uint8_t ao_dma_done[STM_NUM_DMA];
345
346 void
347 ao_dma_set_transfer(uint8_t             index,
348                     volatile void       *peripheral,
349                     void                *memory,
350                     uint16_t            count,
351                     uint32_t            ccr);
352
353 void
354 ao_dma_set_isr(uint8_t index, void (*isr)(int index));
355
356 void
357 ao_dma_start(uint8_t index);
358
359 void
360 ao_dma_done_transfer(uint8_t index);
361
362 void
363 ao_dma_alloc(uint8_t index);
364
365 void
366 ao_dma_init(void);
367
368 /* ao_spi_stm.c
369  */
370
371 /* PCLK is set to 16MHz (HCLK 32MHz, APB prescaler 2) */
372
373 #define _AO_SPI_SPEED_8MHz      STM_SPI_CR1_BR_PCLK_2
374 #define _AO_SPI_SPEED_4MHz      STM_SPI_CR1_BR_PCLK_4
375 #define _AO_SPI_SPEED_2MHz      STM_SPI_CR1_BR_PCLK_8
376 #define _AO_SPI_SPEED_1MHz      STM_SPI_CR1_BR_PCLK_16
377 #define _AO_SPI_SPEED_500kHz    STM_SPI_CR1_BR_PCLK_32
378 #define _AO_SPI_SPEED_250kHz    STM_SPI_CR1_BR_PCLK_64
379 #define _AO_SPI_SPEED_125kHz    STM_SPI_CR1_BR_PCLK_128
380 #define _AO_SPI_SPEED_62500Hz   STM_SPI_CR1_BR_PCLK_256
381
382 static inline uint32_t
383 ao_spi_speed(uint32_t hz)
384 {
385         if (hz >= 8000000) return _AO_SPI_SPEED_8MHz;
386         if (hz >= 4000000) return _AO_SPI_SPEED_4MHz;
387         if (hz >= 2000000) return _AO_SPI_SPEED_2MHz;
388         if (hz >= 1000000) return _AO_SPI_SPEED_1MHz;
389         if (hz >=  500000) return _AO_SPI_SPEED_500kHz;
390         if (hz >=  250000) return _AO_SPI_SPEED_250kHz;
391         if (hz >=  125000) return _AO_SPI_SPEED_125kHz;
392         return _AO_SPI_SPEED_62500Hz;
393 }
394
395 #define AO_SPI_CPOL_BIT         4
396 #define AO_SPI_CPHA_BIT         5
397
398 #define AO_SPI_CONFIG_1         0x00
399 #define AO_SPI_1_CONFIG_PA5_PA6_PA7     AO_SPI_CONFIG_1
400 #define AO_SPI_2_CONFIG_PB13_PB14_PB15  AO_SPI_CONFIG_1
401
402 #define AO_SPI_CONFIG_2         0x04
403 #define AO_SPI_1_CONFIG_PB3_PB4_PB5     AO_SPI_CONFIG_2
404 #define AO_SPI_2_CONFIG_PD1_PD3_PD4     AO_SPI_CONFIG_2
405
406 #define AO_SPI_CONFIG_3         0x08
407 #define AO_SPI_1_CONFIG_PE13_PE14_PE15  AO_SPI_CONFIG_3
408
409 #define AO_SPI_CONFIG_NONE      0x0c
410
411 #define AO_SPI_INDEX_MASK       0x01
412 #define AO_SPI_CONFIG_MASK      0x0c
413
414 #define AO_SPI_1_PA5_PA6_PA7    (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PA5_PA6_PA7)
415 #define AO_SPI_1_PB3_PB4_PB5    (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PB3_PB4_PB5)
416 #define AO_SPI_1_PE13_PE14_PE15 (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PE13_PE14_PE15)
417
418 #define AO_SPI_2_PB13_PB14_PB15 (STM_SPI_INDEX(2) | AO_SPI_2_CONFIG_PB13_PB14_PB15)
419 #define AO_SPI_2_PD1_PD3_PD4    (STM_SPI_INDEX(2) | AO_SPI_2_CONFIG_PD1_PD3_PD4)
420
421 #define AO_SPI_INDEX(id)        ((id) & AO_SPI_INDEX_MASK)
422 #define AO_SPI_CONFIG(id)       ((id) & AO_SPI_CONFIG_MASK)
423 #define AO_SPI_PIN_CONFIG(id)   ((id) & (AO_SPI_INDEX_MASK | AO_SPI_CONFIG_MASK))
424 #define AO_SPI_CPOL(id)         ((uint32_t) (((id) >> AO_SPI_CPOL_BIT) & 1))
425 #define AO_SPI_CPHA(id)         ((uint32_t) (((id) >> AO_SPI_CPHA_BIT) & 1))
426
427 #define AO_SPI_MAKE_MODE(pol,pha)       (((pol) << AO_SPI_CPOL_BIT) | ((pha) << AO_SPI_CPHA_BIT))
428 #define AO_SPI_MODE_0           AO_SPI_MAKE_MODE(0,0)
429 #define AO_SPI_MODE_1           AO_SPI_MAKE_MODE(0,1)
430 #define AO_SPI_MODE_2           AO_SPI_MAKE_MODE(1,0)
431 #define AO_SPI_MODE_3           AO_SPI_MAKE_MODE(1,1)
432
433 uint8_t
434 ao_spi_try_get(uint8_t spi_index, uint32_t speed, uint8_t task_id);
435
436 void
437 ao_spi_get(uint8_t spi_index, uint32_t speed);
438
439 void
440 ao_spi_put(uint8_t spi_index);
441
442 void
443 ao_spi_put_pins(uint8_t spi_index);
444
445 void
446 ao_spi_send(const void *block, uint16_t len, uint8_t spi_index);
447
448 void
449 ao_spi_send_fixed(uint8_t value, uint16_t len, uint8_t spi_index);
450
451 void
452 ao_spi_send_sync(const void *block, uint16_t len, uint8_t spi_index);
453
454 void
455 ao_spi_start_bytes(uint8_t spi_index);
456
457 void
458 ao_spi_stop_bytes(uint8_t spi_index);
459
460 static inline void
461 ao_spi_send_byte(uint8_t byte, uint8_t spi_index)
462 {
463         struct stm_spi  *stm_spi;
464
465         switch (AO_SPI_INDEX(spi_index)) {
466         case 0:
467                 stm_spi = &stm_spi1;
468                 break;
469         case 1:
470                 stm_spi = &stm_spi2;
471                 break;
472         }
473
474         while (!(stm_spi->sr & (1 << STM_SPI_SR_TXE)))
475                 ;
476         stm_spi->dr = byte;
477         while (!(stm_spi->sr & (1 << STM_SPI_SR_RXNE)))
478                 ;
479         (void) stm_spi->dr;
480 }
481
482 static inline uint8_t
483 ao_spi_recv_byte(uint8_t spi_index)
484 {
485         struct stm_spi  *stm_spi;
486
487         switch (AO_SPI_INDEX(spi_index)) {
488         case 0:
489                 stm_spi = &stm_spi1;
490                 break;
491         case 1:
492                 stm_spi = &stm_spi2;
493                 break;
494         }
495
496         while (!(stm_spi->sr & (1 << STM_SPI_SR_TXE)))
497                 ;
498         stm_spi->dr = 0xff;
499         while (!(stm_spi->sr & (1 << STM_SPI_SR_RXNE)))
500                 ;
501         return (uint8_t) stm_spi->dr;
502 }
503
504 void
505 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
506
507 void
508 ao_spi_duplex(const void *out, void *in, uint16_t len, uint8_t spi_index);
509
510 void
511 ao_spi_init(void);
512
513 #define ao_spi_set_cs(reg,mask) ((reg)->bsrr = ((uint32_t) (mask)) << 16)
514 #define ao_spi_clr_cs(reg,mask) ((reg)->bsrr = (mask))
515
516 #define ao_spi_get_mask(reg,mask,bus, speed) do {               \
517                 ao_spi_get(bus, speed);                         \
518                 ao_spi_set_cs(reg,mask);                        \
519         } while (0)
520
521 static inline uint8_t
522 ao_spi_try_get_mask(struct stm_gpio *reg, uint16_t mask, uint8_t bus, uint32_t speed, uint8_t task_id)
523 {
524         if (!ao_spi_try_get(bus, speed, task_id))
525                 return 0;
526         ao_spi_set_cs(reg, mask);
527         return 1;
528 }
529
530 #define ao_spi_put_mask(reg,mask,bus) do {      \
531                 ao_spi_clr_cs(reg,mask);        \
532                 ao_spi_put(bus);                \
533         } while (0)
534
535 #define ao_spi_get_bit(reg,bit,bus,speed) ao_spi_get_mask(reg,1<<(bit),bus,speed)
536 #define ao_spi_put_bit(reg,bit,bus) ao_spi_put_mask(reg,1<<(bit),bus)
537
538 #endif /* _AO_ARCH_FUNCS_H_ */