diff --git a/extmod/machine_i2s.c b/extmod/machine_i2s.c index e101d70ddf..ea2a0e370a 100644 --- a/extmod/machine_i2s.c +++ b/extmod/machine_i2s.c @@ -61,6 +61,7 @@ STATIC void copy_appbuf_to_ringbuf_non_blocking(machine_i2s_obj_t *self); STATIC void mp_machine_i2s_init_helper(machine_i2s_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); STATIC machine_i2s_obj_t *mp_machine_i2s_make_new_instance(mp_int_t i2s_id); STATIC void mp_machine_i2s_deinit(machine_i2s_obj_t *self); +STATIC void mp_machine_i2s_irq_update(machine_i2s_obj_t *self); // The port provides implementations of the above in this file. #include MICROPY_PY_MACHINE_I2S_INCLUDEFILE @@ -309,6 +310,27 @@ STATIC mp_obj_t machine_i2s_deinit(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_i2s_deinit_obj, machine_i2s_deinit); +// I2S.irq(handler) +STATIC mp_obj_t machine_i2s_irq(mp_obj_t self_in, mp_obj_t handler) { + machine_i2s_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (handler != mp_const_none && !mp_obj_is_callable(handler)) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid callback")); + } + + if (handler != mp_const_none) { + self->io_mode = NON_BLOCKING; + } else { + self->io_mode = BLOCKING; + } + + self->callback_for_non_blocking = handler; + + mp_machine_i2s_irq_update(self); + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_i2s_irq_obj, machine_i2s_irq); + // Shift() is typically used as a volume control. // shift=1 increases volume by 6dB, shift=-1 decreases volume by 6dB STATIC mp_obj_t machine_i2s_shift(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { diff --git a/ports/esp32/machine_i2s.c b/ports/esp32/machine_i2s.c index 054ddb90cb..c7a53461c6 100644 --- a/ports/esp32/machine_i2s.c +++ b/ports/esp32/machine_i2s.c @@ -528,15 +528,8 @@ STATIC void mp_machine_i2s_deinit(machine_i2s_obj_t *self) { self->i2s_event_queue = NULL; } -STATIC mp_obj_t machine_i2s_irq(mp_obj_t self_in, mp_obj_t handler) { - machine_i2s_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (handler != mp_const_none && !mp_obj_is_callable(handler)) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid callback")); - } - - if (handler != mp_const_none) { - self->io_mode = NON_BLOCKING; - +STATIC void mp_machine_i2s_irq_update(machine_i2s_obj_t *self) { + if (self->io_mode == NON_BLOCKING) { // create a queue linking the MicroPython task to a FreeRTOS task // that manages the non blocking mode of operation self->non_blocking_mode_queue = xQueueCreate(1, sizeof(non_blocking_descriptor_t)); @@ -563,14 +556,8 @@ STATIC mp_obj_t machine_i2s_irq(mp_obj_t self_in, mp_obj_t handler) { vQueueDelete(self->non_blocking_mode_queue); self->non_blocking_mode_queue = NULL; } - - self->io_mode = BLOCKING; } - - self->callback_for_non_blocking = handler; - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_i2s_irq_obj, machine_i2s_irq); MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[I2S_NUM_AUTO]); diff --git a/ports/mimxrt/machine_i2s.c b/ports/mimxrt/machine_i2s.c index 5ed6588bb1..5556404f25 100644 --- a/ports/mimxrt/machine_i2s.c +++ b/ports/mimxrt/machine_i2s.c @@ -813,22 +813,9 @@ STATIC void mp_machine_i2s_deinit(machine_i2s_obj_t *self) { } } -STATIC mp_obj_t machine_i2s_irq(mp_obj_t self_in, mp_obj_t handler) { - machine_i2s_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (handler != mp_const_none && !mp_obj_is_callable(handler)) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid callback")); - } - - if (handler != mp_const_none) { - self->io_mode = NON_BLOCKING; - } else { - self->io_mode = BLOCKING; - } - - self->callback_for_non_blocking = handler; - return mp_const_none; +STATIC void mp_machine_i2s_irq_update(machine_i2s_obj_t *self) { + (void)self; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_i2s_irq_obj, machine_i2s_irq); MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[MICROPY_HW_I2S_NUM]); diff --git a/ports/rp2/machine_i2s.c b/ports/rp2/machine_i2s.c index 524139012f..4e49ad3590 100644 --- a/ports/rp2/machine_i2s.c +++ b/ports/rp2/machine_i2s.c @@ -686,21 +686,8 @@ STATIC void mp_machine_i2s_deinit(machine_i2s_obj_t *self) { } } -STATIC mp_obj_t machine_i2s_irq(mp_obj_t self_in, mp_obj_t handler) { - machine_i2s_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (handler != mp_const_none && !mp_obj_is_callable(handler)) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid callback")); - } - - if (handler != mp_const_none) { - self->io_mode = NON_BLOCKING; - } else { - self->io_mode = BLOCKING; - } - - self->callback_for_non_blocking = handler; - return mp_const_none; +STATIC void mp_machine_i2s_irq_update(machine_i2s_obj_t *self) { + (void)self; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_i2s_irq_obj, machine_i2s_irq); MP_REGISTER_ROOT_POINTER(void *machine_i2s_obj[2]); diff --git a/ports/stm32/machine_i2s.c b/ports/stm32/machine_i2s.c index b42120e1be..f83ab4ea1a 100644 --- a/ports/stm32/machine_i2s.c +++ b/ports/stm32/machine_i2s.c @@ -662,22 +662,9 @@ STATIC void mp_machine_i2s_deinit(machine_i2s_obj_t *self) { } } -STATIC mp_obj_t machine_i2s_irq(mp_obj_t self_in, mp_obj_t handler) { - machine_i2s_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (handler != mp_const_none && !mp_obj_is_callable(handler)) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid callback")); - } - - if (handler != mp_const_none) { - self->io_mode = NON_BLOCKING; - } else { - self->io_mode = BLOCKING; - } - - self->callback_for_non_blocking = handler; - return mp_const_none; +STATIC void mp_machine_i2s_irq_update(machine_i2s_obj_t *self) { + (void)self; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_i2s_irq_obj, machine_i2s_irq); MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[MICROPY_HW_MAX_I2S]);