From 726628584507d1b043fa28e9adf122acb4b40781 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 28 Mar 2022 16:53:03 +1100 Subject: [PATCH] tests/extmod: Add test for machine.I2S data rate. Signed-off-by: Damien George --- tests/extmod/machine_i2s_rate.py | 80 ++++++++++++++++++++++++++++ tests/extmod/machine_i2s_rate.py.exp | 8 +++ 2 files changed, 88 insertions(+) create mode 100644 tests/extmod/machine_i2s_rate.py create mode 100644 tests/extmod/machine_i2s_rate.py.exp diff --git a/tests/extmod/machine_i2s_rate.py b/tests/extmod/machine_i2s_rate.py new file mode 100644 index 0000000000..46e94585b7 --- /dev/null +++ b/tests/extmod/machine_i2s_rate.py @@ -0,0 +1,80 @@ +# Test machine.I2S data transfer rate, for both TX and RX. + +try: + from machine import Pin, I2S +except ImportError: + print("SKIP") + raise SystemExit + +import time, sys + +# Configure pins based on the board. +if "pyboard" in sys.platform: + i2s_id = 2 + sck_pin = Pin("Y6") + ws_pin = Pin("Y5") + sd_pin = Pin("Y8") +elif "rp2" in sys.platform: + i2s_id = 1 + sck_pin = Pin(0) + ws_pin = Pin(1) + sd_pin = Pin(2) + +TEST_BYTES = b"01234567" +RATE = 11025 # frames/sec + + +def test(mode, bits_per_sample, frame_format): + i2s = I2S( + i2s_id, + sck=sck_pin, + ws=ws_pin, + sd=sd_pin, + mode=mode, + bits=bits_per_sample, + format=frame_format, + rate=RATE, + ibuf=200, + ) + + if frame_format == I2S.MONO: + channels = 1 + else: + channels = 2 + bits_per_frame = bits_per_sample * channels + buf_len_250ms = bits_per_frame // 8 * RATE // 4 + + # Create test data and preload I2S buffers. + if mode == I2S.TX: + mode_str = "TX" + data = TEST_BYTES * (buf_len_250ms // len(TEST_BYTES)) + i2s.write(data) + else: + mode_str = "RX" + data = bytearray(len(TEST_BYTES) * (buf_len_250ms // len(TEST_BYTES))) + i2s.readinto(data) + + # Time how long it takes to read/write 2 lots of data. + t0 = time.ticks_ms() + for i in range(2): + if mode == I2S.TX: + i2s.write(data) + else: + i2s.readinto(data) + t1 = time.ticks_ms() + dt = time.ticks_diff(t1, t0) + + i2s.deinit() + + # Print out test result, time should be in range of 500ms. + print(mode_str, bits_per_sample, channels, abs(dt - 500) <= 4) + + +test(I2S.TX, 16, I2S.MONO) +test(I2S.TX, 16, I2S.STEREO) +test(I2S.TX, 32, I2S.MONO) +test(I2S.TX, 32, I2S.STEREO) +test(I2S.RX, 16, I2S.MONO) +test(I2S.RX, 16, I2S.STEREO) +test(I2S.RX, 32, I2S.MONO) +test(I2S.RX, 32, I2S.STEREO) diff --git a/tests/extmod/machine_i2s_rate.py.exp b/tests/extmod/machine_i2s_rate.py.exp new file mode 100644 index 0000000000..e160d92f6b --- /dev/null +++ b/tests/extmod/machine_i2s_rate.py.exp @@ -0,0 +1,8 @@ +TX 16 1 True +TX 16 2 True +TX 32 1 True +TX 32 2 True +RX 16 1 True +RX 16 2 True +RX 32 1 True +RX 32 2 True