Skip to content

Commit

Permalink
[nrf fromlist] tests: drivers: i2s: Add tests at typical audio sample…
Browse files Browse the repository at this point in the history
… rates

Change frame clock frequency to a global variable so it can be
changed inside a test.

Add short transfer test at 16000, 32000, 44000, 44100, 48000
and 96000 frame clock frequency.

Add configuration for nrf5340dk where i2s peripheral is clocked
from ACLK.

Upstream PR #: 85126

Signed-off-by: Sebastian Głąb <[email protected]>
  • Loading branch information
nordic-segl committed Feb 4, 2025
1 parent 7cbe361 commit 2b270c6
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Drive i2s peripheral from ACLK. */

&clock {
hfclkaudio-frequency = <11289600>;
};

&i2s0 {
clock-source = "ACLK";
};
240 changes: 220 additions & 20 deletions tests/drivers/i2s/i2s_speed/src/test_i2s_speed.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ static const struct device *dev_i2s_rx;
static const struct device *dev_i2s_tx;
static const struct device *dev_i2s_rxtx;
static bool dir_both_supported;
static uint32_t frame_clk_freq = 44000;

static void fill_buf(int16_t *tx_block, int att)
{
Expand Down Expand Up @@ -125,7 +126,6 @@ static int verify_buf(int16_t *rx_block, int att)
}

#define TIMEOUT 2000
#define FRAME_CLK_FREQ 44000

static int configure_stream(const struct device *dev_i2s, enum i2s_dir dir)
{
Expand All @@ -135,7 +135,7 @@ static int configure_stream(const struct device *dev_i2s, enum i2s_dir dir)
i2s_cfg.word_size = 16U;
i2s_cfg.channels = 2U;
i2s_cfg.format = I2S_FMT_DATA_FORMAT_I2S;
i2s_cfg.frame_clk_freq = FRAME_CLK_FREQ;
i2s_cfg.frame_clk_freq = frame_clk_freq;
i2s_cfg.block_size = BLOCK_SIZE;
i2s_cfg.timeout = TIMEOUT;

Expand Down Expand Up @@ -179,16 +179,7 @@ static int configure_stream(const struct device *dev_i2s, enum i2s_dir dir)
return TC_PASS;
}


/** @brief Short I2S transfer.
*
* - TX stream START trigger starts transmission.
* - RX stream START trigger starts reception.
* - sending / receiving a short sequence of data returns success.
* - TX stream DRAIN trigger empties the transmit queue.
* - RX stream STOP trigger stops reception.
*/
ZTEST(drivers_i2s_speed, test_i2s_transfer_short)
static void i2s_transfer_short(void)
{
if (IS_ENABLED(CONFIG_I2S_TEST_USE_I2S_DIR_BOTH)) {
TC_PRINT("RX/TX transfer requires use of I2S_DIR_BOTH.\n");
Expand Down Expand Up @@ -258,6 +249,126 @@ ZTEST(drivers_i2s_speed, test_i2s_transfer_short)
TC_PRINT("%d<-OK\n", 3);
}

/** @brief Short I2S transfer at 16000 samples per second.
*
* - TX stream START trigger starts transmission.
* - RX stream START trigger starts reception.
* - sending / receiving a short sequence of data returns success.
* - TX stream DRAIN trigger empties the transmit queue.
* - RX stream STOP trigger stops reception.
*/
ZTEST(drivers_i2s_speed, test_i2s_transfer_short_16000)
{
int ret;

frame_clk_freq = 16000;
ret = configure_stream(dev_i2s_tx, I2S_DIR_TX);
zassert_equal(ret, TC_PASS);
ret = configure_stream(dev_i2s_rx, I2S_DIR_RX);
zassert_equal(ret, TC_PASS);
i2s_transfer_short();
}

/** @brief Short I2S transfer at 32000 samples per second.
*
* - TX stream START trigger starts transmission.
* - RX stream START trigger starts reception.
* - sending / receiving a short sequence of data returns success.
* - TX stream DRAIN trigger empties the transmit queue.
* - RX stream STOP trigger stops reception.
*/
ZTEST(drivers_i2s_speed, test_i2s_transfer_short_32000)
{
int ret;

frame_clk_freq = 32000;
ret = configure_stream(dev_i2s_tx, I2S_DIR_TX);
zassert_equal(ret, TC_PASS);
ret = configure_stream(dev_i2s_rx, I2S_DIR_RX);
zassert_equal(ret, TC_PASS);
i2s_transfer_short();
}

/** @brief Short I2S transfer at 44000 samples per second.
*
* - TX stream START trigger starts transmission.
* - RX stream START trigger starts reception.
* - sending / receiving a short sequence of data returns success.
* - TX stream DRAIN trigger empties the transmit queue.
* - RX stream STOP trigger stops reception.
*/
ZTEST(drivers_i2s_speed, test_i2s_transfer_short_44000)
{
int ret;

frame_clk_freq = 44000;
ret = configure_stream(dev_i2s_tx, I2S_DIR_TX);
zassert_equal(ret, TC_PASS);
ret = configure_stream(dev_i2s_rx, I2S_DIR_RX);
zassert_equal(ret, TC_PASS);
i2s_transfer_short();
}

/** @brief Short I2S transfer at 44100 samples per second.
*
* - TX stream START trigger starts transmission.
* - RX stream START trigger starts reception.
* - sending / receiving a short sequence of data returns success.
* - TX stream DRAIN trigger empties the transmit queue.
* - RX stream STOP trigger stops reception.
*/
ZTEST(drivers_i2s_speed, test_i2s_transfer_short_44100)
{
int ret;

frame_clk_freq = 44100;
ret = configure_stream(dev_i2s_tx, I2S_DIR_TX);
zassert_equal(ret, TC_PASS);
ret = configure_stream(dev_i2s_rx, I2S_DIR_RX);
zassert_equal(ret, TC_PASS);
i2s_transfer_short();
}

/** @brief Short I2S transfer at 48000 samples per second.
*
* - TX stream START trigger starts transmission.
* - RX stream START trigger starts reception.
* - sending / receiving a short sequence of data returns success.
* - TX stream DRAIN trigger empties the transmit queue.
* - RX stream STOP trigger stops reception.
*/
ZTEST(drivers_i2s_speed, test_i2s_transfer_short_48000)
{
int ret;

frame_clk_freq = 48000;
ret = configure_stream(dev_i2s_tx, I2S_DIR_TX);
zassert_equal(ret, TC_PASS);
ret = configure_stream(dev_i2s_rx, I2S_DIR_RX);
zassert_equal(ret, TC_PASS);
i2s_transfer_short();
}

/** @brief Short I2S transfer at 96000 samples per second.
*
* - TX stream START trigger starts transmission.
* - RX stream START trigger starts reception.
* - sending / receiving a short sequence of data returns success.
* - TX stream DRAIN trigger empties the transmit queue.
* - RX stream STOP trigger stops reception.
*/
ZTEST(drivers_i2s_speed, test_i2s_transfer_short_96000)
{
int ret;

frame_clk_freq = 96000;
ret = configure_stream(dev_i2s_tx, I2S_DIR_TX);
zassert_equal(ret, TC_PASS);
ret = configure_stream(dev_i2s_rx, I2S_DIR_RX);
zassert_equal(ret, TC_PASS);
i2s_transfer_short();
}

/** @brief Long I2S transfer.
*
* - TX stream START trigger starts transmission.
Expand Down Expand Up @@ -349,14 +460,7 @@ ZTEST(drivers_i2s_speed, test_i2s_transfer_long)
zassert_equal(num_verified, NUM_BLOCKS, "Invalid RX blocks received");
}


/** @brief Short I2S transfer using I2S_DIR_BOTH.
*
* - START trigger starts both the transmission and reception.
* - Sending / receiving a short sequence of data returns success.
* - DRAIN trigger empties the transmit queue and stops both streams.
*/
ZTEST(drivers_i2s_speed_both_rxtx, test_i2s_dir_both_transfer_short)
static void i2s_dir_both_transfer_short(void)
{
if (!dir_both_supported) {
TC_PRINT("I2S_DIR_BOTH value is not supported.\n");
Expand Down Expand Up @@ -417,6 +521,102 @@ ZTEST(drivers_i2s_speed_both_rxtx, test_i2s_dir_both_transfer_short)
TC_PRINT("%d<-OK\n", 3);
}

/** @brief Short I2S transfer using I2S_DIR_BOTH and sample rate of 16000.
*
* - START trigger starts both the transmission and reception.
* - Sending / receiving a short sequence of data returns success.
* - DRAIN trigger empties the transmit queue and stops both streams.
*/
ZTEST(drivers_i2s_speed_both_rxtx, test_i2s_dir_both_transfer_short_16000)
{
int ret;

frame_clk_freq = 16000;
ret = configure_stream(dev_i2s_rxtx, I2S_DIR_BOTH);
zassert_equal(ret, TC_PASS);
i2s_dir_both_transfer_short();
}

/** @brief Short I2S transfer using I2S_DIR_BOTH and sample rate of 32000.
*
* - START trigger starts both the transmission and reception.
* - Sending / receiving a short sequence of data returns success.
* - DRAIN trigger empties the transmit queue and stops both streams.
*/
ZTEST(drivers_i2s_speed_both_rxtx, test_i2s_dir_both_transfer_short_32000)
{
int ret;

frame_clk_freq = 32000;
ret = configure_stream(dev_i2s_rxtx, I2S_DIR_BOTH);
zassert_equal(ret, TC_PASS);
i2s_dir_both_transfer_short();
}

/** @brief Short I2S transfer using I2S_DIR_BOTH and sample rate of 44000.
*
* - START trigger starts both the transmission and reception.
* - Sending / receiving a short sequence of data returns success.
* - DRAIN trigger empties the transmit queue and stops both streams.
*/
ZTEST(drivers_i2s_speed_both_rxtx, test_i2s_dir_both_transfer_short_44000)
{
int ret;

frame_clk_freq = 44000;
ret = configure_stream(dev_i2s_rxtx, I2S_DIR_BOTH);
zassert_equal(ret, TC_PASS);
i2s_dir_both_transfer_short();
}

/** @brief Short I2S transfer using I2S_DIR_BOTH and sample rate of 44100.
*
* - START trigger starts both the transmission and reception.
* - Sending / receiving a short sequence of data returns success.
* - DRAIN trigger empties the transmit queue and stops both streams.
*/
ZTEST(drivers_i2s_speed_both_rxtx, test_i2s_dir_both_transfer_short_44100)
{
int ret;

frame_clk_freq = 44100;
ret = configure_stream(dev_i2s_rxtx, I2S_DIR_BOTH);
zassert_equal(ret, TC_PASS);
i2s_dir_both_transfer_short();
}

/** @brief Short I2S transfer using I2S_DIR_BOTH and sample rate of 48000.
*
* - START trigger starts both the transmission and reception.
* - Sending / receiving a short sequence of data returns success.
* - DRAIN trigger empties the transmit queue and stops both streams.
*/
ZTEST(drivers_i2s_speed_both_rxtx, test_i2s_dir_both_transfer_short_48000)
{
int ret;

frame_clk_freq = 48000;
ret = configure_stream(dev_i2s_rxtx, I2S_DIR_BOTH);
zassert_equal(ret, TC_PASS);
i2s_dir_both_transfer_short();
}

/** @brief Short I2S transfer using I2S_DIR_BOTH and sample rate of 96000.
*
* - START trigger starts both the transmission and reception.
* - Sending / receiving a short sequence of data returns success.
* - DRAIN trigger empties the transmit queue and stops both streams.
*/
ZTEST(drivers_i2s_speed_both_rxtx, test_i2s_dir_both_transfer_short_96000)
{
int ret;

frame_clk_freq = 96000;
ret = configure_stream(dev_i2s_rxtx, I2S_DIR_BOTH);
zassert_equal(ret, TC_PASS);
i2s_dir_both_transfer_short();
}

/** @brief Long I2S transfer using I2S_DIR_BOTH.
*
* - START trigger starts both the transmission and reception.
Expand Down
13 changes: 13 additions & 0 deletions tests/drivers/i2s/i2s_speed/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,16 @@ tests:
harness: ztest
harness_config:
fixture: gpio_loopback
drivers.i2s.speed.gpio_loopback.aclk:
depends_on:
- i2s
- gpio
tags:
- drivers
- i2s
filter: CONFIG_I2S_TEST_USE_GPIO_LOOPBACK
harness: ztest
harness_config:
fixture: gpio_loopback
extra_args: EXTRA_DTC_OVERLAY_FILE="boards/nrf5340dk_nrf5340_cpuapp_aclk.overlay"
platform_allow: nrf5340dk/nrf5340/cpuapp

0 comments on commit 2b270c6

Please sign in to comment.