8000 zephyr/machine_pin: Use CONTAINER_OF and don't access pin_mask directly. · micropython/micropython@4fd44e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4fd44e3

Browse files
committed
zephyr/machine_pin: Use CONTAINER_OF and don't access pin_mask directly.
1 parent 4696484 commit 4fd44e3

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

ports/zephyr/machine_pin.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void machine_pin_deinit(void) {
5959
}
6060

6161
STATIC void gpio_callback_handler(struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins) {
62-
machine_pin_irq_obj_t *irq = (void *)((uintptr_t)cb - offsetof(machine_pin_irq_obj_t, callback));
62+
machine_pin_irq_obj_t *irq = CONTAINER_OF(cb, machine_pin_irq_obj_t, callback);
6363

6464
#if MICROPY_STACK_CHECK
6565
// This callback executes in an ISR context so the stack-limit check must be changed to
@@ -219,7 +219,7 @@ STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_
219219
irq->base.handler = mp_const_none;
220220
irq->base.ishard = false;
221221
irq->next = MP_STATE_PORT(machine_pin_irq_list);
222-
gpio_init_callback(&irq->callback, gpio_callback_handler, 0);
222+
gpio_init_callback(&irq->callback, gpio_callback_handler, BIT(self->pin));
223223
int ret = gpio_add_callback(self->port, &irq->callback);
224224
if (ret != 0) {
225225
mp_raise_OSError(-ret);
@@ -231,20 +231,19 @@ STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_
231231

232232
if (n_args > 1 || kw_args->used != 0) {
233233
// configure irq
234-
self->irq->callback.pin_mask &= ~BIT(self->pin);
234+
int ret = gpio_pin_interrupt_configure(self->port, self->pin, GPIO_INT_DISABLE);
235+
if (ret != 0) {
236+
mp_raise_OSError(-ret);
237+
}
235238

236239
self->irq->base.handler = args[ARG_handler].u_obj;
237240
self->irq->base.ishard = args[ARG_hard].u_bool;
238241

239-
int ret;
240-
if (args[ARG_handler].u_obj == mp_const_none) {
241-
ret = gpio_pin_interrupt_configure(self->port, self->pin, GPIO_INT_DISABLE);
242-
} else {
242+
if (args[ARG_handler].u_obj != mp_const_none) {
243243
ret = gpio_pin_interrupt_configure(self->port, self->pin, args[ARG_trigger].u_int);
244-
self->irq->callback.pin_mask |= BIT(self->pin);
245-
}
246-
if (ret != 0) {
247-
mp_raise_OSError(-ret);
244+
if (ret != 0) {
245+
mp_raise_OSError(-ret);
246+
}
248247
}
249248
}
250249

0 commit comments

Comments
 (0)
0