All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gpio_ll_arch.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Gunar Schorcht <gunar@schorcht.net>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
20 #ifndef GPIO_LL_ARCH_H
21 #define GPIO_LL_ARCH_H
22 
23 #include "architecture.h"
24 #include "periph_cpu.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #ifndef DOXYGEN /* hide implementation specific details from Doxygen */
31 
35 #define GPIO_PORT_NUMOF 5
36 
37 #define GPIO_PORT_NUMBERING_ALPHABETIC 1
38 
39 #ifdef GPIOA_BASE
40 # define GPIO_PORT_0 GPIOA_BASE
41 #endif
42 
43 #ifdef GPIOB_BASE
44 # define GPIO_PORT_1 GPIOB_BASE
45 #endif
46 
47 #ifdef GPIOC_BASE
48 # define GPIO_PORT_2 GPIOC_BASE
49 #endif
50 
51 #ifdef GPIOD_BASE
52 # define GPIO_PORT_3 GPIOD_BASE
53 #endif
54 
55 #ifdef GPIOE_BASE
56 # define GPIO_PORT_4 GPIOE_BASE
57 #endif
58 
59 #ifdef GPIOF_BASE
60 # define GPIO_PORT_5 GPIOF_BASE
61 #endif
62 
63 #ifdef GPIOG_BASE
64 # define GPIO_PORT_6 GPIOG_BASE
65 #endif
66 
67 #ifdef GPIOH_BASE
68 # define GPIO_PORT_7 GPIOH_BASE
69 #endif
70 
71 #ifdef GPIOI_BASE
72 # define GPIO_PORT_8 GPIOI_BASE
73 #endif
74 
75 #ifdef GPIOJ_BASE
76 # define GPIO_PORT_9 GPIOJ_BASE
77 #endif
78 
79 #ifdef GPIOK_BASE
80 # define GPIO_PORT_10 GPIOK_BASE
81 #endif
82 
83 static inline gpio_port_t gpio_port(uword_t num)
84 {
85 #if defined(CPU_FAM_STM32MP1)
86  return GPIOA_BASE + (num << 12);
87 #else
88  return GPIOA_BASE + (num << 10);
89 #endif
90 }
91 
92 static inline uword_t gpio_port_num(gpio_port_t port)
93 {
94 #if defined(CPU_FAM_STM32MP1)
95  return (port - GPIOA_BASE) >> 12;
96 #else
97  return (port - GPIOA_BASE) >> 10;
98 #endif
99 }
100 
101 static inline uword_t gpio_ll_read(gpio_port_t port)
102 {
103  return ((GPIO_Type *)port)->ISTAT;
104 }
105 
106 static inline uword_t gpio_ll_read_output(gpio_port_t port)
107 {
108  return ((GPIO_Type *)port)->OCTL;
109 }
110 
111 static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
112 {
113  ((GPIO_Type *)port)->BOP = mask;
114 }
115 
116 static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
117 {
118  ((GPIO_Type *)port)->BOP = mask << 16;
119 }
120 
121 static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
122 {
123  unsigned irq_state = irq_disable();
124  ((GPIO_Type *)port)->OCTL ^= mask;
125  irq_restore(irq_state);
126 }
127 
128 static inline void gpio_ll_write(gpio_port_t port, uword_t value)
129 {
130  ((GPIO_Type *)port)->OCTL = value;
131 }
132 
133 static inline gpio_port_t gpio_get_port(gpio_t pin)
134 {
135  return pin & 0xfffffff0UL;
136 }
137 
138 static inline uint8_t gpio_get_pin_num(gpio_t pin)
139 {
140  return pin & 0xfUL;
141 }
142 
143 static inline gpio_port_t gpio_port_pack_addr(void *addr)
144 {
145  return (gpio_port_t)addr;
146 }
147 
148 static inline void * gpio_port_unpack_addr(gpio_port_t port)
149 {
150  if (port < GPIOA_BASE) {
151  return (void *)port;
152  }
153 
154  return NULL;
155 }
156 
157 static inline bool is_gpio_port_num_valid(uint_fast8_t num)
158 {
159  return num < GPIO_PORT_NUMOF;
160 }
161 
162 #endif /* DOXYGEN */
163 
164 #ifdef __cplusplus
165 }
166 #endif
167 
168 #endif /* GPIO_LL_ARCH_H */
Platform-independent access to architecture details.
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
static uint8_t gpio_get_pin_num(gpio_t pin)
Extract the pin number from a gpio_t
static void * gpio_port_unpack_addr(gpio_port_t port)
Extract a data pointer that was packed by gpio_port_pack_addr.
static void gpio_ll_set(gpio_port_t port, uword_t mask)
Perform an reg |= mask operation on the I/O register of the port.
gpio_port_t gpio_port(uword_t num)
Get the gpio_port_t value of the port number num.
static gpio_port_t gpio_port_pack_addr(void *addr)
Pack a pointer into a gpio_port_t.
static uword_t gpio_ll_read(gpio_port_t port)
Get the current input value of all GPIO pins of the given port as bitmask.
static gpio_port_t gpio_get_port(gpio_t pin)
Extract the gpio_port_t from a gpio_t
uword_t gpio_port_num(gpio_port_t port)
Get the number of the GPIO port port refers to.
static bool is_gpio_port_num_valid(uint_fast8_t num)
Check if the given number is a valid argument for gpio_port.
static uword_t gpio_ll_read_output(gpio_port_t port)
Get the current output value of all GPIO pins of the given port as bitmask.
static void gpio_ll_clear(gpio_port_t port, uword_t mask)
Perform an reg &= ~mask operation on the I/O register of the port.
static void gpio_ll_toggle(gpio_port_t port, uword_t mask)
Perform an reg ^= mask operation on the I/O register of the port.
static void gpio_ll_write(gpio_port_t port, uword_t state)
Perform a masked write operation on the I/O register of the port.
uintptr_t gpio_port_t
GPIO port type.
Definition: gpio_ll.h:87
uint< NUM > _t uword_t
Word sized unsigned integer.
Definition: architecture.h:70
Shared CPU specific definitions for the STM32 family.