altos/stm32f1: Grab both TX/RX DMA mutexes while doing I2C
[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_mutex_get(uint8_t index);
355
356 void
357 ao_dma_mutex_put(uint8_t index);
358
359 void
360 ao_dma_set_isr(uint8_t index, void (*isr)(int index));
361
362 void
363 ao_dma_start(uint8_t index);
364
365 void
366 ao_dma_done_transfer(uint8_t index);
367
368 void
369 ao_dma_alloc(uint8_t index);
370
371 void
372 ao_dma_init(void);
373
374 /* ao_spi_stm.c
375  */
376
377 #define AO_SPI_CPOL_BIT         4
378 #define AO_SPI_CPHA_BIT         5
379
380 #define AO_SPI_CONFIG_1         0x00
381 #define AO_SPI_1_CONFIG_PA5_PA6_PA7     AO_SPI_CONFIG_1
382 #define AO_SPI_2_CONFIG_PB13_PB14_PB15  AO_SPI_CONFIG_1
383
384 #define AO_SPI_CONFIG_2         0x04
385 #define AO_SPI_1_CONFIG_PB3_PB4_PB5     AO_SPI_CONFIG_2
386 #define AO_SPI_2_CONFIG_PD1_PD3_PD4     AO_SPI_CONFIG_2
387
388 #define AO_SPI_CONFIG_3         0x08
389 #define AO_SPI_1_CONFIG_PE13_PE14_PE15  AO_SPI_CONFIG_3
390
391 #define AO_SPI_CONFIG_NONE      0x0c
392
393 #define AO_SPI_INDEX_MASK       0x01
394 #define AO_SPI_CONFIG_MASK      0x0c
395
396 #define AO_SPI_1_PA5_PA6_PA7    (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PA5_PA6_PA7)
397 #define AO_SPI_1_PB3_PB4_PB5    (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PB3_PB4_PB5)
398 #define AO_SPI_1_PE13_PE14_PE15 (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PE13_PE14_PE15)
399
400 #define AO_SPI_2_PB13_PB14_PB15 (STM_SPI_INDEX(2) | AO_SPI_2_CONFIG_PB13_PB14_PB15)
401 #define AO_SPI_2_PD1_PD3_PD4    (STM_SPI_INDEX(2) | AO_SPI_2_CONFIG_PD1_PD3_PD4)
402
403 #define AO_SPI_INDEX(id)        ((id) & AO_SPI_INDEX_MASK)
404 #define AO_SPI_CONFIG(id)       ((id) & AO_SPI_CONFIG_MASK)
405 #define AO_SPI_PIN_CONFIG(id)   ((id) & (AO_SPI_INDEX_MASK | AO_SPI_CONFIG_MASK))
406 #define AO_SPI_CPOL(id)         ((uint32_t) (((id) >> AO_SPI_CPOL_BIT) & 1))
407 #define AO_SPI_CPHA(id)         ((uint32_t) (((id) >> AO_SPI_CPHA_BIT) & 1))
408
409 #define AO_SPI_MAKE_MODE(pol,pha)       (((pol) << AO_SPI_CPOL_BIT) | ((pha) << AO_SPI_CPHA_BIT))
410 #define AO_SPI_MODE_0           AO_SPI_MAKE_MODE(0,0)
411 #define AO_SPI_MODE_1           AO_SPI_MAKE_MODE(0,1)
412 #define AO_SPI_MODE_2           AO_SPI_MAKE_MODE(1,0)
413 #define AO_SPI_MODE_3           AO_SPI_MAKE_MODE(1,1)
414
415 /* SPI1 is on APB2, SPI2 is on APB1 */
416 #define AO_SPI_FREQ(bus, div)   ((AO_SPI_INDEX(bus) == 0 ? AO_APB2CLK : AO_APB1CLK) / (div))
417
418 static inline uint32_t
419 ao_spi_speed(int num, uint32_t hz)
420 {
421         if (hz >= AO_SPI_FREQ(num, 2)) return STM_SPI_CR1_BR_PCLK_2;
422         if (hz >= AO_SPI_FREQ(num, 4)) return STM_SPI_CR1_BR_PCLK_4;
423         if (hz >= AO_SPI_FREQ(num, 8)) return STM_SPI_CR1_BR_PCLK_8;
424         if (hz >= AO_SPI_FREQ(num, 16)) return STM_SPI_CR1_BR_PCLK_16;
425         if (hz >= AO_SPI_FREQ(num, 32)) return STM_SPI_CR1_BR_PCLK_32;
426         if (hz >= AO_SPI_FREQ(num, 64)) return STM_SPI_CR1_BR_PCLK_64;
427         if (hz >= AO_SPI_FREQ(num, 128)) return STM_SPI_CR1_BR_PCLK_128;
428         return STM_SPI_CR1_BR_PCLK_256;
429 }
430
431 uint8_t
432 ao_spi_try_get(uint8_t spi_index, uint32_t speed, uint8_t task_id);
433
434 void
435 ao_spi_get(uint8_t spi_index, uint32_t speed);
436
437 void
438 ao_spi_put(uint8_t spi_index);
439
440 void
441 ao_spi_put_pins(uint8_t spi_index);
442
443 void
444 ao_spi_send(const void *block, uint16_t len, uint8_t spi_index);
445
446 void
447 ao_spi_send_fixed(uint8_t value, uint16_t len, uint8_t spi_index);
448
449 void
450 ao_spi_send_sync(const void *block, uint16_t len, uint8_t spi_index);
451
452 void
453 ao_spi_start_bytes(uint8_t spi_index);
454
455 void
456 ao_spi_stop_bytes(uint8_t spi_index);
457
458 static inline void
459 ao_spi_send_byte(uint8_t byte, uint8_t spi_index)
460 {
461         struct stm_spi  *stm_spi;
462
463         switch (AO_SPI_INDEX(spi_index)) {
464         case 0:
465                 stm_spi = &stm_spi1;
466                 break;
467         case 1:
468                 stm_spi = &stm_spi2;
469                 break;
470         }
471
472         while (!(stm_spi->sr & (1 << STM_SPI_SR_TXE)))
473                 ;
474         stm_spi->dr = byte;
475         while (!(stm_spi->sr & (1 << STM_SPI_SR_RXNE)))
476                 ;
477         (void) stm_spi->dr;
478 }
479
480 static inline uint8_t
481 ao_spi_recv_byte(uint8_t spi_index)
482 {
483         struct stm_spi  *stm_spi;
484
485         switch (AO_SPI_INDEX(spi_index)) {
486         case 0:
487                 stm_spi = &stm_spi1;
488                 break;
489         case 1:
490                 stm_spi = &stm_spi2;
491                 break;
492         }
493
494         while (!(stm_spi->sr & (1 << STM_SPI_SR_TXE)))
495                 ;
496         stm_spi->dr = 0xff;
497         while (!(stm_spi->sr & (1 << STM_SPI_SR_RXNE)))
498                 ;
499         return (uint8_t) stm_spi->dr;
500 }
501
502 void
503 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
504
505 void
506 ao_spi_duplex(const void *out, void *in, uint16_t len, uint8_t spi_index);
507
508 void
509 ao_spi_init(void);
510
511 #define ao_spi_init_cs(port, mask) do {                                 \
512                 uint8_t __bit__;                                        \
513                 for (__bit__ = 0; __bit__ < 32; __bit__++) {            \
514                         if (mask & (1 << __bit__))                      \
515                                 ao_enable_output(port, __bit__, 1); \
516                 }                                                       \
517         } while (0)
518
519 #define ao_spi_set_cs(reg,mask) ((reg)->bsrr = ((uint32_t) (mask)) << 16)
520 #define ao_spi_clr_cs(reg,mask) ((reg)->bsrr = (mask))
521
522 #define ao_spi_get_mask(reg,mask,bus, speed) do {               \
523                 ao_spi_get(bus, speed);                         \
524                 ao_spi_set_cs(reg,mask);                        \
525         } while (0)
526
527 static inline uint8_t
528 ao_spi_try_get_mask(struct stm_gpio *reg, uint16_t mask, uint8_t bus, uint32_t speed, uint8_t task_id)
529 {
530         if (!ao_spi_try_get(bus, speed, task_id))
531                 return 0;
532         ao_spi_set_cs(reg, mask);
533         return 1;
534 }
535
536 #define ao_spi_put_mask(reg,mask,bus) do {      \
537                 ao_spi_clr_cs(reg,mask);        \
538                 ao_spi_put(bus);                \
539         } while (0)
540
541 #define ao_spi_get_bit(reg,bit,bus,speed) ao_spi_get_mask(reg,1<<(bit),bus,speed)
542 #define ao_spi_put_bit(reg,bit,bus) ao_spi_put_mask(reg,1<<(bit),bus)
543
544 void
545 ao_i2c_get(uint8_t i2c_index);
546
547 uint8_t
548 ao_i2c_start(uint8_t i2c_index, uint16_t address);
549
550 void
551 ao_i2c_put(uint8_t i2c_index);
552
553 uint8_t
554 ao_i2c_send(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
555
556 uint8_t
557 ao_i2c_recv(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
558
559 void
560 ao_i2c_init(void);
561
562 #endif /* _AO_ARCH_FUNCS_H_ */