gpio_ll_arch.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Gunar Schorcht
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 
21 #ifndef GPIO_LL_ARCH_H
22 #define GPIO_LL_ARCH_H
23 
24 #include "gpio_arch.h"
25 #include "irq.h"
26 #include "soc/gpio_reg.h"
27 #include "soc/soc.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #ifndef DOXYGEN /* hide implementation specific details from Doxygen */
34 
35 static inline gpio_port_t gpio_port(uword_t num)
36 {
37  return num;
38 }
39 
40 static inline uword_t gpio_port_num(gpio_port_t port)
41 {
42  if (GPIO_PORT_NUMOF == 1) {
43  return 0;
44  }
45 
46  return port;
47 }
48 
49 static inline uword_t gpio_ll_read(gpio_port_t port)
50 {
51  static_assert(GPIO_PORT_NUMOF < 3);
52  volatile uword_t *in = (uint32_t *)GPIO_IN_REG;
53  /* return 0 for unconfigured pins, the current level at the pin otherwise */
54 #if GPIO_PORT_NUM > 1
55  if (gpio_port_num(port) != 0) {
56  in = (uint32_t *)GPIO_IN1_REG;
57  }
58 #endif
59 
60  return *in;
61 }
62 
63 static inline uword_t gpio_ll_read_output(gpio_port_t port)
64 {
65  static_assert(GPIO_PORT_NUMOF < 3);
66  volatile uword_t *out = (uint32_t *)GPIO_OUT_REG;
67 #if GPIO_PORT_NUM > 1
68  if (gpio_port_num(port) != 0) {
69  out = (uint32_t *)GPIO_OUT1_REG;
70  }
71 #endif
72 
73  return *out;
74 }
75 
76 static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
77 {
78  static_assert(GPIO_PORT_NUMOF < 3);
79  volatile uword_t *out_w1ts = (uint32_t *)GPIO_OUT_W1TS_REG;
80  if (gpio_port_num(port) != 0) {
81 #if GPIO_PORT_NUM > 1
82  out_w1ts = (uint32_t)GPIO_OUT1_W1TS;
83 #endif
84  }
85 
86  *out_w1ts = mask;
87 }
88 
89 static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
90 {
91  static_assert(GPIO_PORT_NUMOF < 3);
92  volatile uword_t *out_w1tc = (uint32_t *)GPIO_OUT_W1TC_REG;
93  if (gpio_port_num(port) != 0) {
94 #if GPIO_PORT_NUM > 1
95  out_w1tc = (uint32_t)GPIO_OUT1_W1TC;
96 #endif
97  }
98 
99  *out_w1tc = mask;
100 }
101 
102 static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
103 {
104  static_assert(GPIO_PORT_NUMOF < 3);
105  volatile uword_t *out = (uint32_t *)GPIO_OUT_REG;
106 #if GPIO_PORT_NUM > 1
107  if (gpio_port_num(port) != 0) {
108  out = (uint32_t *)GPIO_OUT1_REG;
109  }
110 #endif
111  unsigned irq_state = irq_disable();
112  *out ^= mask;
113  irq_restore(irq_state);
114 }
115 
116 static inline void gpio_ll_write(gpio_port_t port, uword_t value)
117 {
118  static_assert(GPIO_PORT_NUMOF < 3);
119  volatile uword_t *out = (uint32_t *)GPIO_OUT_REG;
120 #if GPIO_PORT_NUM > 1
121  if (gpio_port_num(port) != 0) {
122  out = (uint32_t *)GPIO_OUT1_REG;
123  }
124 #endif
125  *out = value;
126 }
127 
128 static inline gpio_port_t gpio_get_port(gpio_t pin)
129 {
130  return gpio_port(pin >> 5);
131 }
132 
133 static inline uint8_t gpio_get_pin_num(gpio_t pin)
134 {
135  return pin & 0x1f;
136 }
137 
138 static inline gpio_port_t gpio_port_pack_addr(void *addr)
139 {
140  return (gpio_port_t)addr;
141 }
142 
143 static inline void * gpio_port_unpack_addr(gpio_port_t port)
144 {
145  if (port <= 1) {
146  return NULL;
147  }
148 
149  return (void *)port;
150 }
151 
152 static inline bool is_gpio_port_num_valid(uint_fast8_t num)
153 {
154  return (num < GPIO_PORT_NUMOF);
155 }
156 
157 #endif /* DOXYGEN */
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif /* GPIO_LL_ARCH_H */
#define static_assert(cond,...)
static_assert for c-version < c11
Definition: assert.h:154
Architecture specific GPIO functions for ESP8266.
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
IRQ driver interface.