Skip to content

Commit 3d1f651

Browse files
authored
Merge pull request #20236 from maribu/periph/gpio_ll/smaller_type
drivers/periph/gpio_ll: shrink gpio_conf_t
2 parents 504c169 + 9222762 commit 3d1f651

File tree

19 files changed

+762
-510
lines changed

19 files changed

+762
-510
lines changed

cpu/atmega_common/include/periph_cpu_common.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,6 @@ typedef enum {
231231
GPIO_TRIGGER_LEVEL_HIGH = 0xff, /**< not supported */
232232
} gpio_irq_trig_t;
233233

234-
#define HAVE_GPIO_PULL_T
235-
typedef enum {
236-
GPIO_FLOATING = 0,
237-
GPIO_PULL_UP = 1,
238-
GPIO_PULL_DOWN = 0xfe, /*< not supported */
239-
GPIO_PULL_KEEP = 0xff, /*< not supported */
240-
} gpio_pull_t;
241-
242234
#define HAVE_GPIO_LL_PREPARE_WRITE_ALL_PINS
243235
#define HAVE_GPIO_LL_PREPARE_WRITE
244236

cpu/atmega_common/periph/gpio_ll.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,45 +56,42 @@ static void _set_pull_config(gpio_port_t port, uint8_t pin, gpio_pull_t pull)
5656
p->port |= pull << pin;
5757
}
5858

59-
int gpio_ll_init(gpio_port_t port, uint8_t pin, const gpio_conf_t *conf)
59+
int gpio_ll_init(gpio_port_t port, uint8_t pin, gpio_conf_t conf)
6060
{
61-
if ((conf->pull > GPIO_PULL_UP)
62-
|| (conf->state == GPIO_OUTPUT_OPEN_DRAIN)
63-
|| (conf->state == GPIO_OUTPUT_OPEN_SOURCE)) {
61+
if ((conf.pull > GPIO_PULL_UP)
62+
|| (conf.state == GPIO_OUTPUT_OPEN_DRAIN)
63+
|| (conf.state == GPIO_OUTPUT_OPEN_SOURCE)) {
6464
return -ENOTSUP;
6565
}
6666

6767
unsigned state = irq_disable();
68-
if (conf->initial_value) {
68+
if (conf.initial_value) {
6969
gpio_ll_set(port, 1UL << pin);
7070
}
7171
else {
7272
gpio_ll_clear(port, 1UL << pin);
7373
}
74-
_set_dir(port, pin, conf->state == GPIO_OUTPUT_PUSH_PULL);
75-
if (conf->state == GPIO_INPUT) {
76-
_set_pull_config(port, pin, conf->pull);
74+
_set_dir(port, pin, conf.state == GPIO_OUTPUT_PUSH_PULL);
75+
if (conf.state == GPIO_INPUT) {
76+
_set_pull_config(port, pin, conf.pull);
7777
}
7878
irq_restore(state);
7979

8080
return 0;
8181
}
8282

83-
void gpio_ll_query_conf(gpio_conf_t *dest, gpio_port_t port, uint8_t pin)
83+
gpio_conf_t gpio_ll_query_conf(gpio_port_t port, uint8_t pin)
8484
{
85-
assert(dest);
86-
memset(dest, 0, sizeof(*dest));
87-
/* E.g. the schematics in figure 14-5 in the ATmega328P datasheet shows that
88-
* a Schmitt Trigger is always connected before the digital input signal.
89-
* Let's assume this is also true for all other ATmegas */
90-
dest->schmitt_trigger = true;
85+
gpio_conf_t result = { 0 };
9186
if (_is_output(port, pin)) {
92-
dest->state = GPIO_OUTPUT_PUSH_PULL;
93-
dest->initial_value = (gpio_ll_read_output(port) >> pin) & 1U;
87+
result.state = GPIO_OUTPUT_PUSH_PULL;
88+
result.initial_value = (gpio_ll_read_output(port) >> pin) & 1U;
9489
}
9590
else {
96-
dest->state = GPIO_INPUT;
97-
dest->pull = (gpio_ll_read_output(port) >> pin) & 1U;
98-
dest->initial_value = (gpio_ll_read(port) >> pin) & 1U;
91+
result.state = GPIO_INPUT;
92+
result.pull = (gpio_ll_read_output(port) >> pin) & 1U;
93+
result.initial_value = (gpio_ll_read(port) >> pin) & 1U;
9994
}
95+
96+
return result;
10097
}

cpu/efm32/periph/gpio_ll.c

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,26 @@
1919
* @}
2020
*/
2121

22-
#include <assert.h>
2322
#include <errno.h>
24-
#include <string.h>
2523

2624
#include "cpu.h"
2725
#include "periph/gpio_ll.h"
2826
#include "periph_cpu.h"
2927
#include "periph_conf.h"
3028

31-
int gpio_ll_init(gpio_port_t port, uint8_t pin, const gpio_conf_t *conf)
29+
int gpio_ll_init(gpio_port_t port, uint8_t pin, gpio_conf_t conf)
3230
{
3331
GPIO_Mode_TypeDef mode;
3432

35-
bool initial = conf->initial_value;
33+
bool initial = conf.initial_value;
3634

37-
switch (conf->state) {
35+
switch (conf.state) {
3836
case GPIO_DISCONNECT:
3937
/* ignoring pull */
4038
mode = gpioModeDisabled;
4139
break;
4240
case GPIO_INPUT:
43-
switch (conf->pull) {
41+
switch (conf.pull) {
4442
case GPIO_FLOATING:
4543
mode = gpioModeInput;
4644
break;
@@ -61,7 +59,7 @@ int gpio_ll_init(gpio_port_t port, uint8_t pin, const gpio_conf_t *conf)
6159
mode = gpioModePushPull;
6260
break;
6361
case GPIO_OUTPUT_OPEN_DRAIN:
64-
switch (conf->pull) {
62+
switch (conf.pull) {
6563
case GPIO_FLOATING:
6664
mode = gpioModeWiredAnd;
6765
break;
@@ -73,7 +71,7 @@ int gpio_ll_init(gpio_port_t port, uint8_t pin, const gpio_conf_t *conf)
7371
}
7472
break;
7573
case GPIO_OUTPUT_OPEN_SOURCE:
76-
switch (conf->pull) {
74+
switch (conf.pull) {
7775
case GPIO_FLOATING:
7876
mode = gpioModeWiredOr;
7977
break;
@@ -99,58 +97,48 @@ int gpio_ll_init(gpio_port_t port, uint8_t pin, const gpio_conf_t *conf)
9997
return 0;
10098
}
10199

102-
void gpio_ll_query_conf(gpio_conf_t *dest, gpio_port_t port, uint8_t pin)
100+
gpio_conf_t gpio_ll_query_conf(gpio_port_t port, uint8_t pin)
103101
{
104-
memset(dest, 0, sizeof(*dest));
105-
102+
gpio_conf_t result = { 0 };
106103
GPIO_Mode_TypeDef mode = GPIO_PinModeGet(port, pin);
107104

108-
dest->pull = GPIO_FLOATING;
105+
result.pull = GPIO_FLOATING;
109106

110107
switch (mode) {
111108
case gpioModePushPull:
112-
dest->state = GPIO_OUTPUT_PUSH_PULL;
109+
result.state = GPIO_OUTPUT_PUSH_PULL;
113110
break;
114111
case gpioModeWiredOr:
115-
dest->state = GPIO_OUTPUT_OPEN_SOURCE;
112+
result.state = GPIO_OUTPUT_OPEN_SOURCE;
116113
break;
117114
case gpioModeWiredOrPullDown:
118-
dest->state = GPIO_OUTPUT_OPEN_SOURCE;
119-
dest->pull = GPIO_PULL_DOWN;
115+
result.state = GPIO_OUTPUT_OPEN_SOURCE;
116+
result.pull = GPIO_PULL_DOWN;
120117
break;
121118
case gpioModeWiredAnd:
122-
dest->state = GPIO_OUTPUT_OPEN_DRAIN;
119+
result.state = GPIO_OUTPUT_OPEN_DRAIN;
123120
break;
124121
case gpioModeWiredAndPullUp:
125-
dest->state = GPIO_OUTPUT_OPEN_DRAIN;
126-
dest->pull = GPIO_PULL_UP;
122+
result.state = GPIO_OUTPUT_OPEN_DRAIN;
123+
result.pull = GPIO_PULL_UP;
127124
break;
128125
case gpioModeInput:
129-
dest->state = GPIO_INPUT;
126+
result.state = GPIO_INPUT;
130127
break;
131128
case gpioModeInputPull:
132-
dest->state = GPIO_INPUT;
133-
dest->pull = GPIO_PinOutGet(port, pin) ?
129+
result.state = GPIO_INPUT;
130+
result.pull = GPIO_PinOutGet(port, pin) ?
134131
GPIO_PULL_UP :
135132
GPIO_PULL_DOWN;
136133
break;
137134
case gpioModeDisabled:
138135
/* Fall-through: There is no error reporting here */
139136
default:
140-
dest->state = GPIO_DISCONNECT;
137+
result.state = GPIO_DISCONNECT;
141138
break;
142139
}
143140

144-
/* as good as any */
145-
dest->slew_rate = GPIO_SLEW_FAST;
146-
147-
/* It's always on as long as they're in a mode in which it matters, judging
148-
* from https://www.silabs.com/documents/public/application-notes/an0027.pdf */
149-
dest->schmitt_trigger = true;
150-
151-
dest->initial_value = (gpio_ll_read_output(port) >> pin) & 1;
141+
result.initial_value = (gpio_ll_read_output(port) >> pin) & 1;
152142

153-
/* Using 'strong' her already as that fits with what the hardware has
154-
* (lowest, low, standard, high) */
155-
dest->drive_strength = GPIO_DRIVE_STRONG;
143+
return result;
156144
}

cpu/esp32/include/periph_cpu.h

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ typedef enum {
190190
GPIO_FLOATING = 0,
191191
GPIO_PULL_UP = 1,
192192
GPIO_PULL_DOWN = 2,
193-
GPIO_PULL_KEEP = 0xff /*< not supported */
193+
GPIO_PULL_KEEP = 3 /*< not supported */
194194
} gpio_pull_t;
195195

196196
/**
@@ -212,9 +212,64 @@ typedef enum {
212212
#define GPIO_DRIVE_20 GPIO_DRIVE_STRONG /**< 20 mA (default) */
213213
#define GPIO_DRIVE_30 GPIO_DRIVE_STRONGEST /**< 30 mA */
214214

215-
/* END: GPIO LL overwrites */
215+
#define HAVE_GPIO_STATE_T
216+
typedef enum {
217+
GPIO_OUTPUT_PUSH_PULL,
218+
GPIO_OUTPUT_OPEN_DRAIN,
219+
GPIO_OUTPUT_OPEN_SOURCE,
220+
GPIO_INPUT,
221+
GPIO_USED_BY_PERIPHERAL,
222+
GPIO_DISCONNECT,
223+
} gpio_state_t;
224+
225+
#define HAVE_GPIO_CONF_T
226+
typedef union gpio_conf_esp32 gpio_conf_t;
216227

217228
#endif /* ndef DOXYGEN */
229+
230+
/**
231+
* @brief GPIO pin configuration for ESP32/ESP32Cx/ESP32Sx MCUs
232+
* @ingroup drivers_periph_gpio_ll
233+
*/
234+
union gpio_conf_esp32 {
235+
uint8_t bits; /**< the raw bits */
236+
struct {
237+
/**
238+
* @brief State of the pin
239+
*/
240+
gpio_state_t state : 3;
241+
/**
242+
* @brief Pull resistor configuration
243+
*/
244+
gpio_pull_t pull : 2;
245+
/**
246+
* @brief Drive strength of the GPIO
247+
*
248+
* @warning If the requested drive strength is not available, the closest
249+
* fit supported will be configured instead.
250+
*
251+
* This value is ignored when @ref gpio_conf_esp32::state is configured
252+
* to @ref GPIO_INPUT or @ref GPIO_DISCONNECT.
253+
*/
254+
gpio_drive_strength_t drive_strength : 2;
255+
/**
256+
* @brief Initial value of the output
257+
*
258+
* Ignored if @ref gpio_conf_esp32::state is set to @ref GPIO_INPUT or
259+
* @ref GPIO_DISCONNECT. If the pin was previously in a high impedance
260+
* state, it is guaranteed to directly transition to the given initial
261+
* value.
262+
*
263+
* @ref gpio_ll_query_conf will write the current value of the specified
264+
* pin here, which is read from the input register when the state is
265+
* @ref GPIO_INPUT, otherwise the state from the output register is
266+
* consulted.
267+
*/
268+
bool initial_value : 1;
269+
};
270+
};
271+
272+
/* END: GPIO LL overwrites */
218273
/** @} */
219274

220275
/**

0 commit comments

Comments
 (0)