extmod/machine_i2s: Factor I2S.irq method.

Signed-off-by: Damien George <damien@micropython.org>
pull/12630/head
Damien George 2023-10-10 12:13:37 +11:00
rodzic cdd9ad8d62
commit 1477986815
5 zmienionych plików z 30 dodań i 60 usunięć

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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]);

Wyświetl plik

@ -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]);

Wyświetl plik

@ -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]);

Wyświetl plik

@ -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]);