diff options
| author | Scott Shawcroft <scott@adafruit.com> | 2021-03-01 13:14:13 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-01 13:14:13 -0800 |
| commit | 532e7db293a041fb695ce2455ffdb6ac3a3bbf2d (patch) | |
| tree | 274ac62d26efebe21c46affa0f5887364d3d6228 /ports | |
| parent | 58de61121777b836d525ea5e24973909bdb8a7fa (diff) | |
| parent | d9234ffa8276bfc7f9e516a3c801994901e0cb86 (diff) | |
Merge pull request #4267 from dhalbert/rp2040-digitalinout-fixes
RP2040: change DigitalInOut direction only when necessary; strong drive strength
Diffstat (limited to 'ports')
| -rw-r--r-- | ports/raspberrypi/common-hal/digitalio/DigitalInOut.c | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c b/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c index b0bc1b96e..a1a23b9ab 100644 --- a/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c +++ b/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c @@ -43,6 +43,7 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct( self->output = false; self->open_drain = false; + // Set to input. No output value. gpio_init(pin->number); return DIGITALINOUT_OK; } @@ -75,11 +76,17 @@ digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( digitalio_digitalinout_obj_t* self, bool value, digitalio_drive_mode_t drive_mode) { const uint8_t pin = self->pin->number; - gpio_set_dir(pin, GPIO_OUT); - // TODO: Turn on "strong" pin driving (more current available). + gpio_disable_pulls(pin); + + // Turn on "strong" pin driving (more current available). + hw_write_masked(&padsbank0_hw->io[pin], + PADS_BANK0_GPIO0_DRIVE_VALUE_12MA << PADS_BANK0_GPIO0_DRIVE_LSB, + PADS_BANK0_GPIO0_DRIVE_BITS); self->output = true; - common_hal_digitalio_digitalinout_set_drive_mode(self, drive_mode); + self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; + + // Pin direction is ultimately set in set_value. We don't need to do it here. common_hal_digitalio_digitalinout_set_value(self, value); return DIGITALINOUT_OK; } @@ -92,10 +99,18 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( void common_hal_digitalio_digitalinout_set_value( digitalio_digitalinout_obj_t* self, bool value) { const uint8_t pin = self->pin->number; - if (self->open_drain) { - gpio_set_dir(pin, value ? GPIO_IN : GPIO_OUT); + if (self->open_drain && value) { + // If true and open-drain, set the direction -before- setting + // the pin value, to to avoid a high glitch on the pin before + // switching from output to input for open-drain. + gpio_set_dir(pin, GPIO_IN); + gpio_put(pin, value); } else { + // Otherwise set the direction -after- setting the pin value, + // to avoid a glitch which might occur if the old value was + // different and the pin was previously set to input. gpio_put(pin, value); + gpio_set_dir(pin, GPIO_OUT); } } @@ -110,9 +125,6 @@ digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode( const uint8_t pin = self->pin->number; bool value = common_hal_digitalio_digitalinout_get_value(self); self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; - if (self->open_drain) { - gpio_put(pin, false); - } // True is implemented differently between modes so reset the value to make // sure it's correct for the new mode. if (value) { |
