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