Skip to content

Commit f2f3ef1

Browse files
committed
extmod/machine_i2s: Factor ports' I2S Python bindings to common code.
This factors the basic top-level I2S class code from the ports into extmod/machine_i2s.c: - I2S class definition and method table. - The init and deinit method wrappers. - The make_new code. Further factoring will follow. Signed-off-by: Damien George <damien@micropython.org>
1 parent 7e7af71 commit f2f3ef1

24 files changed

Lines changed: 172 additions & 297 deletions

extmod/extmod.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(MICROPY_SOURCE_EXTMOD
99
${MICROPY_EXTMOD_DIR}/btstack/modbluetooth_btstack.c
1010
${MICROPY_EXTMOD_DIR}/machine_bitstream.c
1111
${MICROPY_EXTMOD_DIR}/machine_i2c.c
12+
${MICROPY_EXTMOD_DIR}/machine_i2s.c
1213
${MICROPY_EXTMOD_DIR}/machine_mem.c
1314
${MICROPY_EXTMOD_DIR}/machine_pulse.c
1415
${MICROPY_EXTMOD_DIR}/machine_pwm.c

extmod/extmod.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
SRC_EXTMOD_C += \
55
extmod/machine_bitstream.c \
66
extmod/machine_i2c.c \
7+
extmod/machine_i2s.c \
78
extmod/machine_mem.c \
89
extmod/machine_pinbase.c \
910
extmod/machine_pulse.c \

extmod/machine_i2s.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Mike Teachman
7+
* Copyright (c) 2023 Damien P. George
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/runtime.h"
29+
30+
#if MICROPY_PY_MACHINE_I2S
31+
32+
#include "extmod/modmachine.h"
33+
34+
// The port must provide implementations of these low-level I2S functions.
35+
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);
36+
STATIC machine_i2s_obj_t *mp_machine_i2s_make_new_instance(mp_int_t i2s_id);
37+
STATIC void mp_machine_i2s_deinit(machine_i2s_obj_t *self);
38+
39+
// The port provides implementations of the above in this file.
40+
#include MICROPY_PY_MACHINE_I2S_INCLUDEFILE
41+
42+
STATIC mp_obj_t machine_i2s_make_new(const mp_obj_type_t *type, size_t n_pos_args, size_t n_kw_args, const mp_obj_t *args) {
43+
mp_arg_check_num(n_pos_args, n_kw_args, 1, MP_OBJ_FUN_ARGS_MAX, true);
44+
mp_int_t i2s_id = mp_obj_get_int(args[0]);
45+
46+
machine_i2s_obj_t *self = mp_machine_i2s_make_new_instance(i2s_id);
47+
48+
mp_map_t kw_args;
49+
mp_map_init_fixed_table(&kw_args, n_kw_args, args + n_pos_args);
50+
mp_machine_i2s_init_helper(self, n_pos_args - 1, args + 1, &kw_args);
51+
52+
return MP_OBJ_FROM_PTR(self);
53+
}
54+
55+
// I2S.init(...)
56+
STATIC mp_obj_t machine_i2s_init(size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
57+
machine_i2s_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
58+
mp_machine_i2s_deinit(self);
59+
mp_machine_i2s_init_helper(self, n_pos_args - 1, pos_args + 1, kw_args);
60+
return mp_const_none;
61+
}
62+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2s_init_obj, 1, machine_i2s_init);
63+
64+
// I2S.deinit()
65+
STATIC mp_obj_t machine_i2s_deinit(mp_obj_t self_in) {
66+
machine_i2s_obj_t *self = MP_OBJ_TO_PTR(self_in);
67+
mp_machine_i2s_deinit(self);
68+
return mp_const_none;
69+
}
70+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_i2s_deinit_obj, machine_i2s_deinit);
71+
72+
STATIC const mp_rom_map_elem_t machine_i2s_locals_dict_table[] = {
73+
// Methods
74+
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_i2s_init_obj) },
75+
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
76+
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
77+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_i2s_deinit_obj) },
78+
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_i2s_irq_obj) },
79+
#if MICROPY_PY_MACHINE_I2S_FINALISER
80+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&machine_i2s_deinit_obj) },
81+
#endif
82+
83+
// Static method
84+
{ MP_ROM_QSTR(MP_QSTR_shift), MP_ROM_PTR(&machine_i2s_shift_obj) },
85+
86+
// Constants
87+
{ MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_INT(MICROPY_PY_MACHINE_I2S_CONSTANT_RX) },
88+
{ MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_INT(MICROPY_PY_MACHINE_I2S_CONSTANT_TX) },
89+
{ MP_ROM_QSTR(MP_QSTR_STEREO), MP_ROM_INT(STEREO) },
90+
{ MP_ROM_QSTR(MP_QSTR_MONO), MP_ROM_INT(MONO) },
91+
};
92+
MP_DEFINE_CONST_DICT(machine_i2s_locals_dict, machine_i2s_locals_dict_table);
93+
94+
STATIC const mp_stream_p_t i2s_stream_p = {
95+
.read = machine_i2s_stream_read,
96+
.write = machine_i2s_stream_write,
97+
.ioctl = machine_i2s_ioctl,
98+
.is_text = false,
99+
};
100+
101+
MP_DEFINE_CONST_OBJ_TYPE(
102+
machine_i2s_type,
103+
MP_QSTR_I2S,
104+
MP_TYPE_FLAG_ITER_IS_STREAM,
105+
make_new, machine_i2s_make_new,
106+
print, machine_i2s_print,
107+
protocol, &i2s_stream_p,
108+
locals_dict, &machine_i2s_locals_dict
109+
);
110+
111+
#endif // MICROPY_PY_MACHINE_I2S

extmod/modmachine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
#include "py/obj.h"
3131

3232
// A port must provide these types, but they are otherwise opaque.
33+
typedef struct _machine_i2s_obj_t machine_i2s_obj_t;
3334
typedef struct _machine_pwm_obj_t machine_pwm_obj_t;
3435
typedef struct _machine_wdt_obj_t machine_wdt_obj_t;
3536

3637
// These classes correspond to machine.Type entries in the machine module.
3738
// Their Python bindings are implemented in extmod, and their implementation
3839
// is provided by a port.
3940
extern const mp_obj_type_t machine_i2c_type;
41+
extern const mp_obj_type_t machine_i2s_type;
4042
extern const mp_obj_type_t machine_pwm_type;
4143
extern const mp_obj_type_t machine_spi_type;
4244
extern const mp_obj_type_t machine_timer_type;

ports/esp32/esp32_common.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ list(APPEND MICROPY_SOURCE_PORT
7070
machine_adcblock.c
7171
machine_dac.c
7272
machine_i2c.c
73-
machine_i2s.c
7473
machine_uart.c
7574
modmachine.c
7675
network_common.c

ports/esp32/machine_i2s.c

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,11 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include <stdio.h>
28-
#include <stdint.h>
29-
#include <string.h>
30-
#include <stdlib.h>
31-
#include <stdbool.h>
32-
33-
#include "py/obj.h"
34-
#include "py/runtime.h"
35-
#include "py/misc.h"
27+
// This file is never compiled standalone, it's included directly from
28+
// extmod/machine_i2s.c via MICROPY_PY_MACHINE_I2S_INCLUDEFILE.
29+
30+
#include "py/mphal.h"
3631
#include "py/stream.h"
37-
#include "py/objstr.h"
38-
#include "modmachine.h"
39-
#include "mphalport.h"
4032

4133
#if MICROPY_PY_MACHINE_I2S
4234

@@ -360,7 +352,7 @@ STATIC void task_for_non_blocking_mode(void *self_in) {
360352
}
361353
}
362354

363-
STATIC void machine_i2s_init_helper(machine_i2s_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
355+
STATIC void mp_machine_i2s_init_helper(machine_i2s_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
364356

365357
enum {
366358
ARG_sck,
@@ -501,10 +493,8 @@ STATIC void machine_i2s_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
501493
);
502494
}
503495

504-
STATIC mp_obj_t machine_i2s_make_new(const mp_obj_type_t *type, size_t n_pos_args, size_t n_kw_args, const mp_obj_t *args) {
505-
mp_arg_check_num(n_pos_args, n_kw_args, 1, MP_OBJ_FUN_ARGS_MAX, true);
506-
507-
i2s_port_t port = mp_obj_get_int(args[0]);
496+
STATIC machine_i2s_obj_t *mp_machine_i2s_make_new_instance(mp_int_t i2s_id) {
497+
i2s_port_t port = i2s_id;
508498
if (port < 0 || port >= I2S_NUM_AUTO) {
509499
mp_raise_ValueError(MP_ERROR_TEXT("invalid id"));
510500
}
@@ -520,23 +510,10 @@ STATIC mp_obj_t machine_i2s_make_new(const mp_obj_type_t *type, size_t n_pos_arg
520510
machine_i2s_deinit(self);
521511
}
522512

523-
mp_map_t kw_args;
524-
mp_map_init_fixed_table(&kw_args, n_kw_args, args + n_pos_args);
525-
machine_i2s_init_helper(self, n_pos_args - 1, args + 1, &kw_args);
526-
527-
return MP_OBJ_FROM_PTR(self);
513+
return self;
528514
}
529515

530-
STATIC mp_obj_t machine_i2s_obj_init(size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
531-
machine_i2s_obj_t *self = pos_args[0];
532-
machine_i2s_deinit(self);
533-
machine_i2s_init_helper(self, n_pos_args - 1, pos_args + 1, kw_args);
534-
return mp_const_none;
535-
}
536-
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2s_init_obj, 1, machine_i2s_obj_init);
537-
538-
STATIC mp_obj_t machine_i2s_deinit(mp_obj_t self_in) {
539-
machine_i2s_obj_t *self = MP_OBJ_TO_PTR(self_in);
516+
STATIC void mp_machine_i2s_deinit(machine_i2s_obj_t *self) {
540517
i2s_driver_uninstall(self->port);
541518

542519
if (self->non_blocking_mode_task != NULL) {
@@ -550,9 +527,7 @@ STATIC mp_obj_t machine_i2s_deinit(mp_obj_t self_in) {
550527
}
551528

552529
self->i2s_event_queue = NULL;
553-
return mp_const_none;
554530
}
555-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_i2s_deinit_obj, machine_i2s_deinit);
556531

557532
STATIC mp_obj_t machine_i2s_irq(mp_obj_t self_in, mp_obj_t handler) {
558533
machine_i2s_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -660,26 +635,6 @@ STATIC mp_obj_t machine_i2s_shift(size_t n_args, const mp_obj_t *pos_args, mp_ma
660635
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2s_shift_fun_obj, 0, machine_i2s_shift);
661636
STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(machine_i2s_shift_obj, MP_ROM_PTR(&machine_i2s_shift_fun_obj));
662637

663-
STATIC const mp_rom_map_elem_t machine_i2s_locals_dict_table[] = {
664-
// Methods
665-
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_i2s_init_obj) },
666-
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
667-
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
668-
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_i2s_deinit_obj) },
669-
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_i2s_irq_obj) },
670-
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&machine_i2s_deinit_obj) },
671-
672-
// Static method
673-
{ MP_ROM_QSTR(MP_QSTR_shift), MP_ROM_PTR(&machine_i2s_shift_obj) },
674-
675-
// Constants
676-
{ MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_INT(I2S_MODE_MASTER | I2S_MODE_RX) },
677-
{ MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_INT(I2S_MODE_MASTER | I2S_MODE_TX) },
678-
{ MP_ROM_QSTR(MP_QSTR_STEREO), MP_ROM_INT(STEREO) },
679-
{ MP_ROM_QSTR(MP_QSTR_MONO), MP_ROM_INT(MONO) },
680-
};
681-
MP_DEFINE_CONST_DICT(machine_i2s_locals_dict, machine_i2s_locals_dict_table);
682-
683638
STATIC mp_uint_t machine_i2s_stream_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
684639
machine_i2s_obj_t *self = MP_OBJ_TO_PTR(self_in);
685640

@@ -802,23 +757,6 @@ STATIC mp_uint_t machine_i2s_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_
802757
return ret;
803758
}
804759

805-
STATIC const mp_stream_p_t i2s_stream_p = {
806-
.read = machine_i2s_stream_read,
807-
.write = machine_i2s_stream_write,
808-
.ioctl = machine_i2s_ioctl,
809-
.is_text = false,
810-
};
811-
812-
MP_DEFINE_CONST_OBJ_TYPE(
813-
machine_i2s_type,
814-
MP_QSTR_I2S,
815-
MP_TYPE_FLAG_ITER_IS_STREAM,
816-
make_new, machine_i2s_make_new,
817-
print, machine_i2s_print,
818-
protocol, &i2s_stream_p,
819-
locals_dict, &machine_i2s_locals_dict
820-
);
821-
822760
MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[I2S_NUM_AUTO]);
823761

824762
#endif // MICROPY_PY_MACHINE_I2S

ports/esp32/modmachine.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ extern const mp_obj_type_t machine_adcblock_type;
1717
extern const mp_obj_type_t machine_dac_type;
1818
extern const mp_obj_type_t machine_i2c_type;
1919
extern const mp_obj_type_t machine_spi_type;
20-
extern const mp_obj_type_t machine_i2s_type;
2120
extern const mp_obj_type_t machine_uart_type;
2221
extern const mp_obj_type_t machine_rtc_type;
2322
extern const mp_obj_type_t machine_sdcard_type;

ports/esp32/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115
#ifndef MICROPY_PY_MACHINE_I2S
116116
#define MICROPY_PY_MACHINE_I2S (1)
117117
#endif
118+
#define MICROPY_PY_MACHINE_I2S_INCLUDEFILE "ports/esp32/machine_i2s.c"
119+
#define MICROPY_PY_MACHINE_I2S_FINALISER (1)
120+
#define MICROPY_PY_MACHINE_I2S_CONSTANT_RX (I2S_MODE_MASTER | I2S_MODE_RX)
121+
#define MICROPY_PY_MACHINE_I2S_CONSTANT_TX (I2S_MODE_MASTER | I2S_MODE_TX)
118122
#define MICROPY_PY_MACHINE_WDT (1)
119123
#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/esp32/machine_wdt.c"
120124
#define MICROPY_PY_NETWORK (1)

ports/mimxrt/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ SRC_C += \
198198
machine_adc.c \
199199
machine_bitstream.c \
200200
machine_i2c.c \
201-
machine_i2s.c \
202201
machine_led.c \
203202
machine_pin.c \
204203
machine_rtc.c \

0 commit comments

Comments
 (0)