summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ports/rp2/machine_pin.c11
-rw-r--r--ports/rp2/mphalport.h15
2 files changed, 18 insertions, 8 deletions
diff --git a/ports/rp2/machine_pin.c b/ports/rp2/machine_pin.c
index 3c7cd86ee..71eda316d 100644
--- a/ports/rp2/machine_pin.c
+++ b/ports/rp2/machine_pin.c
@@ -236,13 +236,10 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
mp_raise_ValueError("alternate functions are not supported for external pins");
}
+ // get initial value of pin (only valid for OUT and OPEN_DRAIN modes)
int value = -1;
if (args[ARG_value].u_obj != mp_const_none) {
value = mp_obj_is_true(args[ARG_value].u_obj);
- // set initial value (do this before configuring mode/pull)
- if (!is_ext_pin(self)) {
- gpio_put(self->id, value);
- }
}
// configure mode
@@ -257,9 +254,13 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
} else if (mode == MACHINE_PIN_MODE_IN) {
mp_hal_pin_input(self->id);
} else if (mode == MACHINE_PIN_MODE_OUT) {
+ if (value != -1) {
+ // set initial output value before configuring mode
+ gpio_put(self->id, value);
+ }
mp_hal_pin_output(self->id);
} else if (mode == MACHINE_PIN_MODE_OPEN_DRAIN) {
- mp_hal_pin_open_drain(self->id);
+ mp_hal_pin_open_drain_with_value(self->id, value == -1 ? 1 : value);
} else {
// Configure alternate function.
mp_uint_t af = args[ARG_alt].u_int;
diff --git a/ports/rp2/mphalport.h b/ports/rp2/mphalport.h
index 2cb9121fa..8b4a5b609 100644
--- a/ports/rp2/mphalport.h
+++ b/ports/rp2/mphalport.h
@@ -102,13 +102,22 @@ static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) {
gpio_set_function(pin, GPIO_FUNC_SIO);
}
-static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) {
- gpio_set_dir(pin, GPIO_IN);
- gpio_put(pin, 0);
+static inline void mp_hal_pin_open_drain_with_value(mp_hal_pin_obj_t pin, int v) {
+ if (v) {
+ gpio_set_dir(pin, GPIO_IN);
+ gpio_put(pin, 0);
+ } else {
+ gpio_put(pin, 0);
+ gpio_set_dir(pin, GPIO_OUT);
+ }
machine_pin_open_drain_mask |= 1 << pin;
gpio_set_function(pin, GPIO_FUNC_SIO);
}
+static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) {
+ mp_hal_pin_open_drain_with_value(pin, 1);
+}
+
static inline void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt) {
assert((mode == MP_HAL_PIN_MODE_INPUT || mode == MP_HAL_PIN_MODE_OUTPUT) && alt == 0);
gpio_set_dir(pin, mode);