altos: Start work on stm32f1 support
[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 static inline void
23 ao_enable_port(struct stm_gpio *port)
24 {
25         if ((port) == &stm_gpioa)
26                 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_IOPAEN);
27         else if ((port) == &stm_gpiob)
28                 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_IOPBEN);
29         else if ((port) == &stm_gpioc)
30                 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_IOPCEN);
31         else if ((port) == &stm_gpiod)
32                 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_IOPDEN);
33         else if ((port) == &stm_gpioe)
34                 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_IOPEEN);
35 }
36
37 static inline void
38 ao_disable_port(struct stm_gpio *port)
39 {
40         if ((port) == &stm_gpioa)
41                 stm_rcc.apb2enr &= ~(1UL << STM_RCC_APB2ENR_IOPAEN);
42         else if ((port) == &stm_gpiob)
43                 stm_rcc.apb2enr &= ~(1UL << STM_RCC_APB2ENR_IOPBEN);
44         else if ((port) == &stm_gpioc)
45                 stm_rcc.apb2enr &= ~(1UL << STM_RCC_APB2ENR_IOPCEN);
46         else if ((port) == &stm_gpiod)
47                 stm_rcc.apb2enr &= ~(1UL << STM_RCC_APB2ENR_IOPDEN);
48         else if ((port) == &stm_gpioe)
49                 stm_rcc.apb2enr &= ~(1UL << STM_RCC_APB2ENR_IOPEEN);
50 }
51
52 #define ao_gpio_set(port, bit, v) stm_gpio_set(port, bit, v)
53
54 #define ao_gpio_get(port, bit) stm_gpio_get(port, bit)
55
56 #define ao_gpio_set_bits(port, bits) stm_gpio_set_bits(port, bits)
57
58 #define ao_gpio_set_mask(port, bits, mask) stm_gpio_set_mask(port, bits, mask)
59
60 #define ao_gpio_clr_bits(port, bits) stm_gpio_clr_bits(port, bits);
61
62 #define ao_gpio_get_all(port) stm_gpio_get_all(port)
63
64 static inline void
65 ao_enable_output(struct stm_gpio *port, int bit, uint8_t v)
66 {
67         ao_enable_port(port);
68         ao_gpio_set(port, bit, v);
69         stm_gpio_conf(port, bit,
70                       STM_GPIO_CR_MODE_OUTPUT_10MHZ,
71                       STM_GPIO_CR_CNF_OUTPUT_PUSH_PULL);
72 }
73
74 #endif /* _AO_ARCH_FUNCS_H_ */