tests/extmod/machine_i2s_rate.py: Test multiple I2S instances.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2024-01-18 16:36:47 +11:00
parent 50b809c8e8
commit 51fbec2780

View File

@@ -9,28 +9,28 @@ except ImportError:
import time, sys import time, sys
# Configure pins based on the board. # Configure pins based on the board.
# A board must have at least one instance to test, both TX and RX mode.
if "pyboard" in sys.platform: if "pyboard" in sys.platform:
i2s_id = 2 i2s_instances = ((2, Pin("Y6"), Pin("Y5"), Pin("Y8"), Pin("Y8")),)
sck_pin = Pin("Y6")
ws_pin = Pin("Y5")
sd_tx_pin = sd_rx_pin = Pin("Y8")
elif "rp2" in sys.platform: elif "rp2" in sys.platform:
i2s_id = 1 i2s_instances = (
sck_pin = Pin(0) (0, Pin(0), Pin(1), Pin(2), Pin(2)),
ws_pin = Pin(1) (1, Pin(0), Pin(1), Pin(2), Pin(2)),
sd_tx_pin = sd_rx_pin = Pin(2) )
elif "mimxrt" in sys.platform: elif "mimxrt" in sys.platform:
i2s_id = 1 i2s_instances = (
sck_pin = Pin(26) (1, Pin("D26"), Pin("D27"), Pin("D7"), Pin("D8")),
ws_pin = Pin(27) (2, Pin("D4"), Pin("D3"), Pin("D2"), None),
sd_tx_pin = Pin(7) )
sd_rx_pin = Pin(8)
TEST_BYTES = b"01234567" TEST_BYTES = b"01234567"
RATE = 11025 # frames/sec RATE = 11025 # frames/sec
def test(mode, sd_pin, bits_per_sample, frame_format): def test(i2s_id, sck_pin, ws_pin, sd_pin, mode, bits_per_sample, frame_format):
if sd_pin is None:
return
i2s = I2S( i2s = I2S(
i2s_id, i2s_id,
sck=sck_pin, sck=sck_pin,
@@ -48,16 +48,16 @@ def test(mode, sd_pin, bits_per_sample, frame_format):
else: else:
channels = 2 channels = 2
bits_per_frame = bits_per_sample * channels bits_per_frame = bits_per_sample * channels
buf_len_250ms = bits_per_frame // 8 * RATE // 4 buf_len_200ms = bits_per_frame // 8 * RATE // 5
# Create test data and preload I2S buffers. # Create test data and preload I2S buffers.
if mode == I2S.TX: if mode == I2S.TX:
mode_str = "TX" mode_str = "TX"
data = TEST_BYTES * (buf_len_250ms // len(TEST_BYTES)) data = TEST_BYTES * (buf_len_200ms // len(TEST_BYTES))
i2s.write(data) i2s.write(data)
else: else:
mode_str = "RX" mode_str = "RX"
data = bytearray(len(TEST_BYTES) * (buf_len_250ms // len(TEST_BYTES))) data = bytearray(len(TEST_BYTES) * (buf_len_200ms // len(TEST_BYTES)))
i2s.readinto(data) i2s.readinto(data)
# Time how long it takes to read/write 2 lots of data. # Time how long it takes to read/write 2 lots of data.
@@ -72,15 +72,27 @@ def test(mode, sd_pin, bits_per_sample, frame_format):
i2s.deinit() i2s.deinit()
# Print out test result, time should be in range of 500ms. # Time should be in range of 400ms.
print(mode_str, bits_per_sample, channels, abs(dt - 500) <= 4) time_in_range = abs(dt - 400) <= 4
# Print out test result if requested, or if time not in range.
if print_results or not time_in_range:
print(mode_str, bits_per_sample, channels, time_in_range)
test(I2S.TX, sd_tx_pin, 16, I2S.MONO) print_results = True
test(I2S.TX, sd_tx_pin, 16, I2S.STEREO)
test(I2S.TX, sd_tx_pin, 32, I2S.MONO) for i2s_id, sck_pin, ws_pin, sd_tx_pin, sd_rx_pin in i2s_instances:
test(I2S.TX, sd_tx_pin, 32, I2S.STEREO) test(i2s_id, sck_pin, ws_pin, sd_tx_pin, I2S.TX, 16, I2S.MONO)
test(I2S.RX, sd_rx_pin, 16, I2S.MONO) test(i2s_id, sck_pin, ws_pin, sd_tx_pin, I2S.TX, 16, I2S.STEREO)
test(I2S.RX, sd_rx_pin, 16, I2S.STEREO) test(i2s_id, sck_pin, ws_pin, sd_tx_pin, I2S.TX, 32, I2S.MONO)
test(I2S.RX, sd_rx_pin, 32, I2S.MONO) test(i2s_id, sck_pin, ws_pin, sd_tx_pin, I2S.TX, 32, I2S.STEREO)
test(I2S.RX, sd_rx_pin, 32, I2S.STEREO) test(i2s_id, sck_pin, ws_pin, sd_rx_pin, I2S.RX, 16, I2S.MONO)
test(i2s_id, sck_pin, ws_pin, sd_rx_pin, I2S.RX, 16, I2S.STEREO)
test(i2s_id, sck_pin, ws_pin, sd_rx_pin, I2S.RX, 32, I2S.MONO)
test(i2s_id, sck_pin, ws_pin, sd_rx_pin, I2S.RX, 32, I2S.STEREO)
# For any remaining tests, don't print the results if they pass.
# This is to have the same output on all boards regardless of
# how many I2S instances they test.
print_results = False