1 /*
2  * Copyright (c) 2017 comsuisse AG
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 #include <zephyr/kernel.h>
9 #include <zephyr/ztest.h>
10 #include <zephyr/device.h>
11 #include "i2s_api_test.h"
12 
13 ZTEST_DMEM const struct device *dev_i2s_rx =
14 	DEVICE_DT_GET_OR_NULL(I2S_DEV_NODE_RX);
15 ZTEST_DMEM const struct device *dev_i2s_tx =
16 	DEVICE_DT_GET_OR_NULL(I2S_DEV_NODE_TX);
17 ZTEST_DMEM const struct device *dev_i2s =
18 	DEVICE_DT_GET_OR_NULL(I2S_DEV_NODE_RX);
19 ZTEST_DMEM bool dir_both_supported;
20 
setup(void)21 static void *setup(void)
22 {
23 	k_thread_access_grant(k_current_get(),
24 			      &rx_mem_slab, &tx_mem_slab);
25 	k_object_access_grant(dev_i2s_rx, k_current_get());
26 	k_object_access_grant(dev_i2s_tx, k_current_get());
27 
28 	return NULL;
29 }
30 
before(void * fixture)31 static void before(void *fixture)
32 {
33 	ARG_UNUSED(fixture);
34 
35 	int ret;
36 
37 	zassert_not_null(dev_i2s_rx, "RX device not found");
38 	zassert_true(device_is_ready(dev_i2s_rx),
39 		     "device %s is not ready", dev_i2s_rx->name);
40 	zassert_not_null(dev_i2s_tx, "TX device not found");
41 	zassert_true(device_is_ready(dev_i2s_tx),
42 		     "device %s is not ready", dev_i2s_tx->name);
43 
44 	ret = configure_stream(dev_i2s_rx, I2S_DIR_RX);
45 	zassert_equal(ret, TC_PASS);
46 
47 	ret = configure_stream(dev_i2s_tx, I2S_DIR_TX);
48 	zassert_equal(ret, TC_PASS);
49 }
50 
before_dir_both(void * fixture)51 static void before_dir_both(void *fixture)
52 {
53 	ARG_UNUSED(fixture);
54 
55 	int ret;
56 
57 	zassert_not_null(dev_i2s, "TX/RX device not found");
58 	zassert_true(device_is_ready(dev_i2s),
59 		     "device %s is not ready", dev_i2s->name);
60 
61 	ret = configure_stream(dev_i2s, I2S_DIR_BOTH);
62 	zassert_equal(ret, TC_PASS);
63 
64 	/* Check if the tested driver supports the I2S_DIR_BOTH value.
65 	 * Use the DROP trigger for this, as in the current state of the driver
66 	 * (READY, both TX and RX queues empty) it is actually a no-op.
67 	 */
68 	ret = i2s_trigger(dev_i2s, I2S_DIR_BOTH, I2S_TRIGGER_DROP);
69 	dir_both_supported = (ret == 0);
70 
71 	if (IS_ENABLED(CONFIG_I2S_TEST_USE_I2S_DIR_BOTH)) {
72 		zassert_true(dir_both_supported,
73 			     "I2S_DIR_BOTH value is supposed to be supported.");
74 	}
75 }
76 
77 ZTEST_SUITE(i2s_loopback, NULL, setup, before, NULL, NULL);
78 ZTEST_SUITE(i2s_states, NULL, setup, before, NULL, NULL);
79 ZTEST_SUITE(i2s_dir_both_states, NULL, setup, before_dir_both, NULL, NULL);
80 ZTEST_SUITE(i2s_dir_both_loopback, NULL, setup, before_dir_both, NULL, NULL);
81 ZTEST_SUITE(i2s_errors, NULL, setup, before, NULL, NULL);
82