8000 rp2/machine_pin: Fix configuring OPEN_DRAIN with initial value. · micropython/micropython@67fac4e · GitHub
[go: up one dir, main page]

Skip to content

Commit 67fac4e

Browse files
committed
rp2/machine_pin: Fix configuring OPEN_DRAIN with initial value.
Prior to this commit, Pin(Pin.OPEN_DRAIN, value=0) would not set the initial value of the open-drain pin to low, instead it would be high. Signed-off-by: Damien George <damien@micropython.org>
1 parent 8a03535 commit 67fac4e

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

ports/rp2/machine_pin.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,10 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
236236
mp_raise_ValueError("alternate functions are not supported for external pins");
237237
}
238238

239+
// get initial value of pin (only valid for OUT and OPEN_DRAIN modes)
239240
int value = -1;
240241
if (args[ARG_value].u_obj != mp_const_none) {
241242
value = mp_obj_is_true(args[ARG_value].u_obj);
242-
// set initial value (do this before configuring mode/pull)
243-
if (!is_ext_pin(self)) {
244-
gpio_put(self->id, value);
245-
}
246243
}
247244

248245
// configure mode
@@ -257,9 +254,13 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
257254
} else if (mode == MACHINE_PIN_MODE_IN) {
258255
mp_hal_pin_input(self->id);
259256
} else if (mode == MACHINE_PIN_MODE_OUT) {
257+
if (value != -1) {
258+
// set initial output value before configuring mode
259+
gpio_put(self->id, value);
260+
}
260261
mp_hal_pin_output(self->id);
261262
} else if (mode == MACHINE_PIN_MODE_OPEN_DRAIN) {
262-
mp_hal_pin_open_drain(self->id);
263+
mp_hal_pin_open_drain_with_value(self->id, value == -1 ? 1 : value);
263264
} else {
264265
// Configure alternate function.
265266
mp_uint_t af = args[ARG_alt].u_int;

ports/rp2/mphalport.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,22 @@ static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) {
102102
gpio_set_function(pin, GPIO_FUNC_SIO);
103103
}
104104

105-
static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) {
106-
gpio_set_dir(pin, GPIO_IN);
107-
gpio_put(pin, 0);
105+
static inline void mp_hal_pin_open_drain_with_value(mp_hal_pin_obj_t pin, int v) {
106+
if (v) {
107+
gpio_set_dir(pin, GPIO_IN);
108+
gpio_put(pin, 0);
109+
} else {
110+
gpio_put(pin, 0);
111+
gpio_set_dir(pin, GPIO_OUT);
112+
}
108113
machine_pin_open_drain_mask |= 1 << pin;
109114
gpio_set_function(pin, GPIO_FUNC_SIO);
110115
}
111116

117+
static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) {
118+
mp_hal_pin_open_drain_with_value(pin, 1);
119+
}
120+
112121
static inline void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt) {
113122
assert((mode == MP_HAL_PIN_MODE_INPUT || mode == MP_HAL_PIN_MODE_OUTPUT) && alt == 0);
114123
gpio_set_dir(pin, mode);

0 commit comments

Comments
 (0)
0