Skip to content

Commit 45cb9b4

Browse files
Gadgetoiddpgeorge
authored andcommitted
rp2/machine_pin: Fix simulated open drain with more than 32 GPIOs.
Changes are: - Refactor the open-drain macros, add GPIO_ENABLE/DISABLE_OPEN_DRAIN, and move them to `mphalport.h`. - Only use `uint64_t` for the open-drain mask if there are more than 32 GPIOs (saves code size). - Ensure we're shifting a `uint64_t` by using 1ULL constants. Signed-off-by: Phil Howard <[email protected]>
1 parent 28c8fff commit 45cb9b4

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

ports/rp2/machine_pin.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@
4646

4747
#define GPIO_IRQ_ALL (0xf)
4848

49-
// Open drain behaviour is simulated.
50-
#define GPIO_IS_OPEN_DRAIN(id) (machine_pin_open_drain_mask & (1 << (id)))
51-
5249
#ifndef MICROPY_HW_PIN_RESERVED
5350
#define MICROPY_HW_PIN_RESERVED(i) (0)
5451
#endif
@@ -83,7 +80,11 @@ static const mp_irq_methods_t machine_pin_irq_methods;
8380
static const int num_intr_regs = sizeof(iobank0_hw->intr) / sizeof(iobank0_hw->intr[0]);
8481

8582
// Mask with "1" indicating that the corresponding pin is in simulated open-drain mode.
83+
#if NUM_BANK0_GPIOS > 32
8684
uint64_t machine_pin_open_drain_mask;
85+
#else
86+
uint32_t machine_pin_open_drain_mask;
87+
#endif
8788

8889
#if MICROPY_HW_PIN_EXT_COUNT
8990
static inline bool is_ext_pin(__unused const machine_pin_obj_t *self) {
@@ -292,7 +293,7 @@ static mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
292293
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin af: %d"), af);
293294
}
294295
gpio_set_function(self->id, af);
295-
machine_pin_open_drain_mask &= ~(1ULL << self->id);
296+
GPIO_DISABLE_OPEN_DRAIN(self->id);
296297
}
297298
}
298299

ports/rp2/mphalport.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,18 @@ static inline mp_uint_t mp_hal_get_cpu_freq(void) {
123123
#define MP_HAL_PIN_PULL_UP (1)
124124
#define MP_HAL_PIN_PULL_DOWN (2)
125125

126+
// Open drain behaviour is simulated.
127+
#if NUM_BANK0_GPIOS > 32
126128
extern uint64_t machine_pin_open_drain_mask;
129+
#define GPIO_IS_OPEN_DRAIN(id) (machine_pin_open_drain_mask & (1ULL << id))
130+
#define GPIO_ENABLE_OPEN_DRAIN(id) (machine_pin_open_drain_mask |= (1ULL << id))
131+
#define GPIO_DISABLE_OPEN_DRAIN(id) (machine_pin_open_drain_mask &= ~(1ULL << id))
132+
#else
133+
extern uint32_t machine_pin_open_drain_mask;
134+
#define GPIO_IS_OPEN_DRAIN(id) (machine_pin_open_drain_mask & (1U << id))
135+
#define GPIO_ENABLE_OPEN_DRAIN(id) (machine_pin_open_drain_mask |= (1U << id))
136+
#define GPIO_DISABLE_OPEN_DRAIN(id) (machine_pin_open_drain_mask &= ~(1U << id))
137+
#endif
127138

128139
mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t pin_in);
129140

@@ -133,13 +144,13 @@ static inline unsigned int mp_hal_pin_name(mp_hal_pin_obj_t pin) {
133144

134145
static inline void mp_hal_pin_input(mp_hal_pin_obj_t pin) {
135146
gpio_set_dir(pin, GPIO_IN);
136-
machine_pin_open_drain_mask &= ~(1 << pin);
147+
GPIO_DISABLE_OPEN_DRAIN(pin);
137148
gpio_set_function(pin, GPIO_FUNC_SIO);
138149
}
139150

140151
static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) {
141152
gpio_set_dir(pin, GPIO_OUT);
142-
machine_pin_open_drain_mask &= ~(1 << pin);
153+
GPIO_DISABLE_OPEN_DRAIN(pin);
143154
gpio_set_function(pin, GPIO_FUNC_SIO);
144155
}
145156

@@ -151,7 +162,7 @@ static inline void mp_hal_pin_open_drain_with_value(mp_hal_pin_obj_t pin, int v)
151162
gpio_put(pin, 0);
152163
gpio_set_dir(pin, GPIO_OUT);
153164
}
154-
machine_pin_open_drain_mask |= 1 << pin;
165+
GPIO_ENABLE_OPEN_DRAIN(pin);
155166
gpio_set_function(pin, GPIO_FUNC_SIO);
156167
}
157168

0 commit comments

Comments
 (0)