10000 esp32/machine_pin: Add new hold keyword argument and remove PULL_HOLD. · micropython/micropython@7684c99 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7684c99

Browse files
jonathanhoggdpgeorge
authored andcommitted
esp32/machine_pin: Add new hold keyword argument and remove PULL_HOLD.
The current pull=Pin.PULL_HOLD argument doesn't make a lot of sense in the context of what it actually does vs what the ESP32 quickref document says it does. This commit removes PULL_HOLD and adds a new hold=True|False keyword argument to Pin()/Pin.init(). Setting this to True will cause the ESP32 to lock the configuration of the pin – including direction, output value, drive strength, pull-up/-down – such that it can't be accidentally changed and will be retained through a watchdog or internal reset. Fixes issue #8283, and see also #8284.
1 parent 5887dfe commit 7684c99

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

docs/esp32/quickref.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ safe maximum source/sink currents and approximate internal driver resistances:
177177
- ``Pin.DRIVE_2``: 20mA / 30 ohm (default strength if not configured)
178178
- ``Pin.DRIVE_3``: 40mA / 15 ohm
179179

180+
The ``hold=`` keyword argument to ``Pin()`` and ``Pin.init()`` will enable the
181+
ESP32 "pad hold" feature. When set to ``True``, the pin configuration
182+
(direction, pull resistors and output value) will be held and any further
183+
changes (including changing the output level) will not be applied. Setting
184+
``hold=False`` will immediately apply any outstanding pin configuration changes
185+
and release the pin. Using ``hold=True`` while a pin is already held will apply
186+
any configuration changes and then immediately reapply the hold.
187+
180188
Notes:
181189

182190
* Pins 1 and 3 are REPL UART TX and RX respectively
@@ -549,6 +557,10 @@ deep-sleep mode::
549557
pin.init(pull=None)
550558
machine.deepsleep(10000)
551559

560+
Output-configured RTC pins will also retain their output direction and level in
561+
deep-sleep if pad hold is enabled with the ``hold=True`` argument to
562+
``Pin.init()``.
563+
552564
Non-RTC GPIO pins will be disconnected by default on entering deep-sleep.
553565

554566
SD card

ports/esp32/machine_pin.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
// Used to implement a range of pull capabilities
4848
#define GPIO_PULL_DOWN (1)
4949
#define GPIO_PULL_UP (2)
50-
#define GPIO_PULL_HOLD (4)
5150

5251
#if CONFIG_IDF_TARGET_ESP32
5352
#define GPIO_FIRST_NON_OUTPUT (34)
@@ -253,14 +252,15 @@ STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
253252
mp_printf(print, "Pin(%u)", self->id);
254253
}
255254

256-
// pin.init(mode=None, pull=-1, *, value, drive)
255+
// pin.init(mode=None, pull=-1, *, value, drive, hold)
257256
STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
258-
enum { ARG_mode, ARG_pull, ARG_value, ARG_drive };
257+
enum { ARG_mode, ARG_pull, ARG_value, ARG_drive, ARG_hold };
259258
static const mp_arg_t allowed_args[] = {
260259
{ MP_QSTR_mode, MP_ARG_OBJ, {.u_obj = mp_const_none}},
261260
{ MP_QSTR_pull, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)}},
262261
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
263262
{ MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
263+
{ MP_QSTR_hold, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
264264
};
265265

266266
// parse args
@@ -325,10 +325,15 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
325325
} else {
326326
gpio_pullup_dis(self->id);
327327
}
328-
if (mode & GPIO_PULL_HOLD) {
328+
}
329+
330+
// configure pad hold
331+
if (args[ARG_hold].u_obj != MP_OBJ_NULL && GPIO_IS_VALID_OUTPUT_GPIO(self->id)) {
332+
// always disable pad hold to apply outstanding config changes
333+
gpio_hold_dis(self->id);
334+
// (re-)enable pad hold if requested
335+
if (mp_obj_is_true(args[ARG_hold].u_obj)) {
329336
gpio_hold_en(self->id);
330-
} else if (GPIO_IS_VALID_OUTPUT_GPIO(self->id)) {
331-
gpio_hold_dis(self->id);
332337
}
333338
}
334339

@@ -480,7 +485,6 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
480485
{ MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_INPUT_OUTPUT_OD) },
481486
{ MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULL_UP) },
482487
{ MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULL_DOWN) },
483-
{ MP_ROM_QSTR(MP_QSTR_PULL_HOLD), MP_ROM_INT(GPIO_PULL_HOLD) },
484488
{ MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_PIN_INTR_POSEDGE) },
485489
{ MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_PIN_INTR_NEGEDGE) },
486490
{ MP_ROM_QSTR(MP_QSTR_WAKE_LOW), MP_ROM_INT(GPIO_PIN_INTR_LOLEVEL) },

0 commit comments

Comments
 (0)
0