Skip to content

Commit 7d08c3c

Browse files
committed
drivers/periph/gpio_ll: pass gpio_conf_t by value
Now that `gpio_conf_t` is on all implemented platforms no larger than a register, we can more efficiently pass it by value rather than via pointer.
1 parent 9ac55bd commit 7d08c3c

File tree

13 files changed

+287
-322
lines changed

13 files changed

+287
-322
lines changed

cpu/atmega_common/periph/gpio_ll.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,41 +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));
85+
gpio_conf_t result = { 0 };
8786
if (_is_output(port, pin)) {
88-
dest->state = GPIO_OUTPUT_PUSH_PULL;
89-
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;
9089
}
9190
else {
92-
dest->state = GPIO_INPUT;
93-
dest->pull = (gpio_ll_read_output(port) >> pin) & 1U;
94-
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;
9594
}
95+
96+
return result;
9697
}

cpu/efm32/periph/gpio_ll.c

Lines changed: 23 additions & 24 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,47 +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-
dest->initial_value = (gpio_ll_read_output(port) >> pin) & 1;
141+
result.initial_value = (gpio_ll_read_output(port) >> pin) & 1;
142+
143+
return result;
145144
}

cpu/esp32/periph/gpio_ll.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ const _esp32_port_t _esp32_ports[GPIO_PORT_NUMOF] = {
8080
#endif
8181
};
8282

83-
int gpio_ll_init(gpio_port_t port, uint8_t pin, const gpio_conf_t *conf)
83+
int gpio_ll_init(gpio_port_t port, uint8_t pin, gpio_conf_t conf)
8484
{
8585
assert(port);
86-
assert(conf);
8786
assert(GPIO_PORT_NUM(port) < GPIO_PORT_NUMOF);
8887
assert(pin < GPIO_PORT_PIN_NUMOF(port));
8988

@@ -96,7 +95,7 @@ int gpio_ll_init(gpio_port_t port, uint8_t pin, const gpio_conf_t *conf)
9695
.pull_down_en = false,
9796
};
9897

99-
switch (conf->state) {
98+
switch (conf.state) {
10099
case GPIO_OUTPUT_PUSH_PULL:
101100
cfg.mode = GPIO_MODE_DEF_OUTPUT;
102101
break;
@@ -113,7 +112,7 @@ int gpio_ll_init(gpio_port_t port, uint8_t pin, const gpio_conf_t *conf)
113112
return -ENOTSUP;
114113
}
115114

116-
switch (conf->pull) {
115+
switch (conf.pull) {
117116
case GPIO_FLOATING:
118117
break;
119118
case GPIO_PULL_UP:
@@ -143,22 +142,22 @@ int gpio_ll_init(gpio_port_t port, uint8_t pin, const gpio_conf_t *conf)
143142
_gpio_pin_pd[pin] = cfg.pull_down_en;
144143
#endif
145144

146-
if (conf->state == GPIO_DISCONNECT) {
145+
if (conf.state == GPIO_DISCONNECT) {
147146
/* reset the pin to disconnects any other peripheral output configured
148147
via GPIO Matrix, the pin is reconfigured according to given conf */
149148
esp_idf_gpio_reset_pin(gpio);
150149
}
151150

152151
/* since we can't read back the configuration, we have to save it */
153-
_gpio_conf[gpio] = *conf;
152+
_gpio_conf[gpio] = conf;
154153

155154
if (esp_idf_gpio_config(&cfg) != ESP_OK) {
156155
return -ENOTSUP;
157156
}
158157

159158
/* if output pin, try to set drive strength */
160159
gpio_drive_cap_t strength;
161-
switch (conf->drive_strength) {
160+
switch (conf.drive_strength) {
162161
case GPIO_DRIVE_WEAKEST:
163162
strength = GPIO_DRIVE_CAP_0;
164163
break;
@@ -179,7 +178,7 @@ int gpio_ll_init(gpio_port_t port, uint8_t pin, const gpio_conf_t *conf)
179178
return -ENOTSUP;
180179
}
181180

182-
if (conf->initial_value) {
181+
if (conf.initial_value) {
183182
gpio_ll_set(port, 1UL << pin);
184183
}
185184
else {
@@ -189,23 +188,25 @@ int gpio_ll_init(gpio_port_t port, uint8_t pin, const gpio_conf_t *conf)
189188
return 0;
190189
}
191190

192-
void gpio_ll_query_conf(gpio_conf_t *dest, gpio_port_t port, uint8_t pin)
191+
gpio_conf_t gpio_ll_query_conf(gpio_port_t port, uint8_t pin)
193192
{
194-
assert(dest);
193+
gpio_conf_t result;
195194

196195
unsigned state = irq_disable();
197196

198-
*dest = _gpio_conf[GPIO_PIN(GPIO_PORT_NUM(port), pin)];
199-
if (dest->state == GPIO_INPUT) {
200-
dest->initial_value = (gpio_ll_read(port) >> pin) & 1UL;
197+
result = _gpio_conf[GPIO_PIN(GPIO_PORT_NUM(port), pin)];
198+
if (result.state == GPIO_INPUT) {
199+
result.initial_value = (gpio_ll_read(port) >> pin) & 1UL;
201200
}
202201
else {
203-
dest->initial_value = (gpio_ll_read_output(port) >> pin) & 1UL;
202+
result.initial_value = (gpio_ll_read_output(port) >> pin) & 1UL;
204203
}
205204
irq_restore(state);
205+
206+
return result;
206207
}
207208

208-
void gpio_ll_print_conf(const gpio_conf_t *conf)
209+
void gpio_ll_print_conf(gpio_conf_t conf)
209210
{
210211
static const char *drive_strs[] = {
211212
[GPIO_DRIVE_WEAKEST] = "weakest",
@@ -216,5 +217,5 @@ void gpio_ll_print_conf(const gpio_conf_t *conf)
216217

217218
gpio_ll_print_conf_common(conf);
218219
print_str(", drive: ");
219-
print_str(drive_strs[conf->drive_strength]);
220+
print_str(drive_strs[conf.drive_strength]);
220221
}

0 commit comments

Comments
 (0)