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 
9 #pragma once
10 
22 #include "architecture.h"
23 #include "periph_cpu.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #ifndef DOXYGEN /* hide implementation specific details from Doxygen */
30 
34 #define GPIO_PORT_NUMOF 5
35 
36 #define GPIO_PORT_NUMBERING_ALPHABETIC 1
37 
38 #ifdef GPIOA_BASE
39 # define GPIO_PORT_0 GPIOA_BASE
40 #endif
41 
42 #ifdef GPIOB_BASE
43 # define GPIO_PORT_1 GPIOB_BASE
44 #endif
45 
46 #ifdef GPIOC_BASE
47 # define GPIO_PORT_2 GPIOC_BASE
48 #endif
49 
50 #ifdef GPIOD_BASE
51 # define GPIO_PORT_3 GPIOD_BASE
52 #endif
53 
54 #ifdef GPIOE_BASE
55 # define GPIO_PORT_4 GPIOE_BASE
56 #endif
57 
58 #ifdef GPIOF_BASE
59 # define GPIO_PORT_5 GPIOF_BASE
60 #endif
61 
62 #ifdef GPIOG_BASE
63 # define GPIO_PORT_6 GPIOG_BASE
64 #endif
65 
66 #ifdef GPIOH_BASE
67 # define GPIO_PORT_7 GPIOH_BASE
68 #endif
69 
70 #ifdef GPIOI_BASE
71 # define GPIO_PORT_8 GPIOI_BASE
72 #endif
73 
74 #ifdef GPIOJ_BASE
75 # define GPIO_PORT_9 GPIOJ_BASE
76 #endif
77 
78 #ifdef GPIOK_BASE
79 # define GPIO_PORT_10 GPIOK_BASE
80 #endif
81 
82 static inline gpio_port_t gpio_port(uword_t num)
83 {
84 #if defined(CPU_FAM_STM32MP1)
85  return GPIOA_BASE + (num << 12);
86 #else
87  return GPIOA_BASE + (num << 10);
88 #endif
89 }
90 
91 static inline uword_t gpio_port_num(gpio_port_t port)
92 {
93 #if defined(CPU_FAM_STM32MP1)
94  return (port - GPIOA_BASE) >> 12;
95 #else
96  return (port - GPIOA_BASE) >> 10;
97 #endif
98 }
99 
100 static inline uword_t gpio_ll_read(gpio_port_t port)
101 {
102  return ((GPIO_Type *)port)->ISTAT;
103 }
104 
105 static inline uword_t gpio_ll_read_output(gpio_port_t port)
106 {
107  return ((GPIO_Type *)port)->OCTL;
108 }
109 
110 static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
111 {
112  ((GPIO_Type *)port)->BOP = mask;
113 }
114 
115 static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
116 {
117  ((GPIO_Type *)port)->BOP = mask << 16;
118 }
119 
120 static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
121 {
122  unsigned irq_state = irq_disable();
123  ((GPIO_Type *)port)->OCTL ^= mask;
124  irq_restore(irq_state);
125 }
126 
127 static inline void gpio_ll_write(gpio_port_t port, uword_t value)
128 {
129  ((GPIO_Type *)port)->OCTL = value;
130 }
131 
132 static inline gpio_port_t gpio_get_port(gpio_t pin)
133 {
134  return pin & 0xfffffff0UL;
135 }
136 
137 static inline uint8_t gpio_get_pin_num(gpio_t pin)
138 {
139  return pin & 0xfUL;
140 }
141 
142 static inline gpio_port_t gpio_port_pack_addr(void *addr)
143 {
144  return (gpio_port_t)addr;
145 }
146 
147 static inline void * gpio_port_unpack_addr(gpio_port_t port)
148 {
149  if (port < GPIOA_BASE) {
150  return (void *)port;
151  }
152 
153  return NULL;
154 }
155 
156 static inline bool is_gpio_port_num_valid(uint_fast8_t num)
157 {
158  return num < GPIO_PORT_NUMOF;
159 }
160 
161 #endif /* DOXYGEN */
162 
163 #ifdef __cplusplus
164 }
165 #endif
166 
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:95
uint< NUM > _t uword_t
Word sized unsigned integer.
Definition: architecture.h:69
Shared CPU specific definitions for the STM32 family.