Skip to content

Commit b8e8ffa

Browse files
authored
Expose gpio_set_drive_capability API to Lua (#3099)
1 parent 189fe96 commit b8e8ffa

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

components/modules/gpio.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@ static int lgpio_write (lua_State *L)
211211
}
212212

213213

214+
// Lua: gpio.set_drive(gpio, gpio.DRIVE_x)
215+
static int lgpio_set_drive (lua_State *L)
216+
{
217+
int gpio = luaL_checkint (L, 1);
218+
int strength = luaL_checkint (L, 2);
219+
luaL_argcheck (L, strength >= GPIO_DRIVE_CAP_0 && strength < GPIO_DRIVE_CAP_MAX,
220+
2, "pad strength must be between gpio.DRIVE_0 and gpio.DRIVE_3");
221+
check_err (L, gpio_set_drive_capability(gpio, (gpio_drive_cap_t)strength));
222+
return 0;
223+
}
224+
214225

215226
static void nodemcu_gpio_callback_task (task_param_t param, task_prio_t prio)
216227
{
@@ -245,6 +256,7 @@ LROT_BEGIN(lgpio)
245256
LROT_FUNCENTRY( trig, lgpio_trig )
246257
LROT_FUNCENTRY( wakeup, lgpio_wakeup )
247258
LROT_FUNCENTRY( write, lgpio_write )
259+
LROT_FUNCENTRY( set_drive, lgpio_set_drive )
248260

249261

250262
LROT_NUMENTRY ( OUT, GPIO_MODE_OUTPUT )
@@ -262,6 +274,12 @@ LROT_BEGIN(lgpio)
262274
LROT_NUMENTRY ( INTR_UP_DOWN, GPIO_INTR_ANYEDGE )
263275
LROT_NUMENTRY ( INTR_LOW, GPIO_INTR_LOW_LEVEL )
264276
LROT_NUMENTRY ( INTR_HIGH, GPIO_INTR_HIGH_LEVEL )
277+
278+
LROT_NUMENTRY ( DRIVE_0, GPIO_DRIVE_CAP_0 )
279+
LROT_NUMENTRY ( DRIVE_1, GPIO_DRIVE_CAP_1 )
280+
LROT_NUMENTRY ( DRIVE_2, GPIO_DRIVE_CAP_2 )
281+
LROT_NUMENTRY ( DRIVE_DEFAULT,GPIO_DRIVE_CAP_DEFAULT )
282+
LROT_NUMENTRY ( DRIVE_3, GPIO_DRIVE_CAP_3 )
265283
LROT_END(lgpio, NULL, 0)
266284

267285
NODEMCU_MODULE(GPIO, "gpio", lgpio, nodemcu_gpio_init);

docs/modules/gpio.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ Read digital GPIO pin value.
6363
0 = low, 1 = high
6464

6565

66+
## gpio.set_drive()
67+
Set the drive strength of a given GPIO pin. The higher the drive strength, the more current can be sourced/sunk from the pin. The exact maximum depends on the power domain of the pin and how much current other pins in that domain are consuming.
68+
69+
#### Syntax
70+
`gpio.set_drive(pin, strength)`
71+
72+
#### Parameters
73+
- `pin`, a valid GPIO pin number.
74+
- `strength` the drive strength to set, one of
75+
- `gpio.DRIVE_0` weakest drive strength
76+
- `gpio.DRIVE_1` stronger drive strength
77+
- `gpio.DRIVE_2` default drive strength
78+
- `gpio.DRIVE_DEFAULT` default drive strength (same as `DRIVE_2`)
79+
- `gpio.DRIVE_3` maximum drive strength
80+
81+
#### Returns
82+
`nil`
83+
84+
#### Example
85+
```lua
86+
gpio.set_drive(4, gpio.DRIVE_3)
87+
```
88+
89+
6690
## gpio.trig()
6791
Establish or clear a callback function to run on interrupt for a GPIO.
6892

0 commit comments

Comments
 (0)