Loading...
Searching...
No Matches
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
30extern "C" {
31#endif
32
33#ifndef DOXYGEN /* hide implementation specific details from Doxygen */
34
35#define GPIO_PORT(num) (num)
36#if GPIO_PORT_NUMOF > 1
37# define GPIO_PORT_NUM(port) (port)
38#else
39# define GPIO_PORT_NUM(port) 0
40#endif
41
42static inline uword_t gpio_ll_read(gpio_port_t port)
43{
44 static_assert(GPIO_PORT_NUMOF < 3);
45 volatile uword_t *in = (uint32_t *)GPIO_IN_REG;
46 /* return 0 for unconfigured pins, the current level at the pin otherwise */
47#if GPIO_PORT_NUM > 1
48 if (GPIO_PORT_NUM(port) != 0) {
49 in = (uint32_t *)GPIO_IN1_REG;
50 }
51#endif
52
53 return *in;
54}
55
56static inline uword_t gpio_ll_read_output(gpio_port_t port)
57{
58 static_assert(GPIO_PORT_NUMOF < 3);
59 volatile uword_t *out = (uint32_t *)GPIO_OUT_REG;
60#if GPIO_PORT_NUM > 1
61 if (GPIO_PORT_NUM(port) != 0) {
62 out = (uint32_t *)GPIO_OUT1_REG;
63 }
64#endif
65
66 return *out;
67}
68
69static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
70{
71 static_assert(GPIO_PORT_NUMOF < 3);
72 volatile uword_t *out_w1ts = (uint32_t *)GPIO_OUT_W1TS_REG;
73 if (GPIO_PORT_NUM(port) != 0) {
74#if GPIO_PORT_NUM > 1
75 out_w1ts = (uint32_t)GPIO_OUT1_W1TS;
76#endif
77 }
78
79 *out_w1ts = mask;
80}
81
82static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
83{
84 static_assert(GPIO_PORT_NUMOF < 3);
85 volatile uword_t *out_w1tc = (uint32_t *)GPIO_OUT_W1TC_REG;
86 if (GPIO_PORT_NUM(port) != 0) {
87#if GPIO_PORT_NUM > 1
88 out_w1tc = (uint32_t)GPIO_OUT1_W1TC;
89#endif
90 }
91
92 *out_w1tc = mask;
93}
94
95static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
96{
97 static_assert(GPIO_PORT_NUMOF < 3);
98 volatile uword_t *out = (uint32_t *)GPIO_OUT_REG;
99#if GPIO_PORT_NUM > 1
100 if (GPIO_PORT_NUM(port) != 0) {
101 out = (uint32_t *)GPIO_OUT1_REG;
102 }
103#endif
104 unsigned irq_state = irq_disable();
105 *out ^= mask;
106 irq_restore(irq_state);
107}
108
109static inline void gpio_ll_write(gpio_port_t port, uword_t value)
110{
111 static_assert(GPIO_PORT_NUMOF < 3);
112 volatile uword_t *out = (uint32_t *)GPIO_OUT_REG;
113#if GPIO_PORT_NUM > 1
114 if (GPIO_PORT_NUM(port) != 0) {
115 out = (uint32_t *)GPIO_OUT1_REG;
116 }
117#endif
118 *out = value;
119}
120
121static inline gpio_port_t gpio_get_port(gpio_t pin)
122{
123 return GPIO_PORT(pin >> 5);
124}
125
126static inline uint8_t gpio_get_pin_num(gpio_t pin)
127{
128 return pin & 0x1f;
129}
130
131static inline gpio_port_t gpio_port_pack_addr(void *addr)
132{
133 return (gpio_port_t)addr;
134}
135
136static inline void * gpio_port_unpack_addr(gpio_port_t port)
137{
138 if (port <= 1) {
139 return NULL;
140 }
141
142 return (void *)port;
143}
144
145static inline bool is_gpio_port_num_valid(uint_fast8_t num)
146{
147 return (num < GPIO_PORT_NUMOF);
148}
149
150#endif /* DOXYGEN */
151
152#ifdef __cplusplus
153}
154#endif
155
156#endif /* GPIO_LL_ARCH_H */
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_ll_set(gpio_port_t port, uword_t mask)
Perform an reg |= mask operation on the I/O register of the port.
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
#define GPIO_PORT_NUM(port)
Get the number of the GPIO port belonging to the given gpio_port_t value.
Definition gpio_ll.h:126
static void * gpio_port_unpack_addr(gpio_port_t port)
Extract a data pointer that was packed by gpio_port_pack_addr.
#define GPIO_PORT(num)
Get the gpio_port_t value of the port identified by num.
Definition gpio_ll.h:106
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.
IRQ driver interface.