Skip to content

Commit

Permalink
Merge 5cd0707 into caaaa2b
Browse files Browse the repository at this point in the history
  • Loading branch information
presslab-us committed Jul 24, 2020
2 parents caaaa2b + 5cd0707 commit 1e27052
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/esp32/quickref.rst
Expand Up @@ -148,6 +148,7 @@ Use the :ref:`machine.Pin <machine.Pin>` class::

p4 = Pin(4, Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor
p5 = Pin(5, Pin.OUT, value=1) # set pin high on creation
p6 = Pin(6, Pin.OUT, value=1, drive=Pin.LOW_POWER) # set low drive strength

Available Pins are from the following ranges (inclusive): 0-19, 21-23, 25-27, 32-39.
These correspond to the actual GPIO pin numbers of ESP32 chip. Note that many
Expand Down
20 changes: 18 additions & 2 deletions ports/esp32/machine_pin.c
Expand Up @@ -44,6 +44,10 @@
#define GPIO_PULL_UP (2)
#define GPIO_PULL_HOLD (4)

#define GPIO_LOW_POWER (GPIO_DRIVE_CAP_0)
#define GPIO_MED_POWER (GPIO_DRIVE_CAP_2)
#define GPIO_HIGH_POWER (GPIO_DRIVE_CAP_3)

typedef struct _machine_pin_obj_t {
mp_obj_base_t base;
gpio_num_t id;
Expand Down Expand Up @@ -137,13 +141,14 @@ STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
mp_printf(print, "Pin(%u)", self->id);
}

// pin.init(mode, pull=None, *, value)
// pin.init(mode, pull=None, *, value, drive=None)
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) {
enum { ARG_mode, ARG_pull, ARG_value };
enum { ARG_mode, ARG_pull, ARG_value, ARG_drive };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_mode, MP_ARG_OBJ, {.u_obj = mp_const_none}},
{ MP_QSTR_pull, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)}},
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
{ MP_QSTR_drive, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)}},
};

// parse args
Expand All @@ -158,6 +163,14 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
gpio_set_level(self->id, mp_obj_is_true(args[ARG_value].u_obj));
}

// configure drive strength
if (args[ARG_drive].u_obj != MP_OBJ_NEW_SMALL_INT(-1)) {
if (args[ARG_drive].u_obj != mp_const_none) {
int drive = mp_obj_get_int(args[ARG_drive].u_obj);
gpio_set_drive_capability(self->id, drive);
}
}

// configure mode
if (args[ARG_mode].u_obj != mp_const_none) {
mp_int_t pin_io_mode = mp_obj_get_int(args[ARG_mode].u_obj);
Expand Down Expand Up @@ -340,6 +353,9 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULL_UP) },
{ MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULL_DOWN) },
{ MP_ROM_QSTR(MP_QSTR_PULL_HOLD), MP_ROM_INT(GPIO_PULL_HOLD) },
{ MP_ROM_QSTR(MP_QSTR_LOW_POWER), MP_ROM_INT(GPIO_LOW_POWER) },
{ MP_ROM_QSTR(MP_QSTR_MED_POWER), MP_ROM_INT(GPIO_MED_POWER) },
{ MP_ROM_QSTR(MP_QSTR_HIGH_POWER), MP_ROM_INT(GPIO_HIGH_POWER) },
{ MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_PIN_INTR_POSEDGE) },
{ MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_PIN_INTR_NEGEDGE) },
{ MP_ROM_QSTR(MP_QSTR_WAKE_LOW), MP_ROM_INT(GPIO_PIN_INTR_LOLEVEL) },
Expand Down

0 comments on commit 1e27052

Please sign in to comment.