1 /*
2 * Copyright (c) 2024 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/ztest.h>
9 #include <zephyr/drivers/i2s.h>
10 #include "i2s_api_test.h"
11
12 #define INVALID_TRIGGER_SETTING 7
13
ZTEST_USER(i2s_errors,test_i2s_improper_configuration)14 ZTEST_USER(i2s_errors, test_i2s_improper_configuration)
15 {
16 int err;
17 struct i2s_config invalid_config = { .word_size = 16U,
18 .channels = 2U,
19 .format = I2S_FMT_DATA_FORMAT_I2S,
20 .frame_clk_freq = FRAME_CLK_FREQ,
21 .block_size = BLOCK_SIZE,
22 .timeout = TIMEOUT,
23 .options = I2S_OPT_FRAME_CLK_MASTER |
24 I2S_OPT_BIT_CLK_MASTER,
25 .mem_slab = &tx_mem_slab };
26
27
28 invalid_config.format =
29 I2S_FMT_DATA_FORMAT_LEFT_JUSTIFIED | I2S_FMT_DATA_FORMAT_RIGHT_JUSTIFIED;
30
31 err = i2s_configure(dev_i2s, I2S_DIR_TX, &invalid_config);
32 zassert_not_equal(
33 err, 0,
34 "I2S configuration did not detect improper data format (I2S_FMT_DATA_FORMAT_LEFT_JUSTIFIED | I2S_FMT_DATA_FORMAT_RIGHT_JUSTIFIED)");
35
36 invalid_config.format = I2S_FMT_DATA_FORMAT_I2S | I2S_FMT_DATA_ORDER_LSB;
37
38 err = i2s_configure(dev_i2s, I2S_DIR_TX, &invalid_config);
39 zassert_not_equal(
40 err, 0,
41 "I2S configuration did not detect improper stream format (I2S_FMT_DATA_ORDER_LSB)");
42
43 invalid_config.format = I2S_FMT_DATA_FORMAT_I2S;
44 invalid_config.channels = 3U;
45 err = i2s_configure(dev_i2s, I2S_DIR_TX, &invalid_config);
46 zassert_not_equal(err, 0,
47 "I2S configuration did not detect improper channels configuration (3)");
48 }
49
ZTEST_USER(i2s_errors,test_i2s_config_attempt_in_wrong_state)50 ZTEST_USER(i2s_errors, test_i2s_config_attempt_in_wrong_state)
51 {
52 int err;
53 int config_err;
54 char tx_data[BLOCK_SIZE] = {0};
55 struct i2s_config inactive_config = { .word_size = 16U,
56 .channels = 2U,
57 .format = I2S_FMT_DATA_FORMAT_I2S,
58 .frame_clk_freq = FRAME_CLK_FREQ,
59 .block_size = BLOCK_SIZE,
60 .timeout = TIMEOUT,
61 .options = I2S_OPT_FRAME_CLK_MASTER |
62 I2S_OPT_BIT_CLK_MASTER,
63 .mem_slab = &tx_mem_slab };
64
65 err = i2s_configure(dev_i2s, I2S_DIR_TX, &inactive_config);
66 zassert_equal(err, 0, "I2S interface configuration failed, err=%d", err);
67
68 err = i2s_buf_write(dev_i2s, tx_data, BLOCK_SIZE);
69 zassert_equal(err, 0, "I2S buffer write unexpected error: %d", err);
70
71 err = i2s_trigger(dev_i2s, I2S_DIR_TX, I2S_TRIGGER_START);
72 zassert_equal(err, 0, "I2S_TRIGGER_START unexpected error: %d", err);
73
74 config_err = i2s_configure(dev_i2s, I2S_DIR_TX, &inactive_config);
75
76 err = i2s_trigger(dev_i2s, I2S_DIR_TX, I2S_TRIGGER_STOP);
77 zassert_equal(err, 0, "I2S_TRIGGER_STOP unexpected error: %d", err);
78
79 zassert_not_equal(
80 config_err, 0,
81 "I2S configuration should not be possible in states other than I2S_STATE_READY");
82 }
83
ZTEST_USER(i2s_errors,test_i2s_incorrect_trigger)84 ZTEST_USER(i2s_errors, test_i2s_incorrect_trigger)
85 {
86 int err;
87 char tx_data[BLOCK_SIZE] = {0};
88 struct i2s_config test_config = { .word_size = 16U,
89 .channels = 2U,
90 .format = I2S_FMT_DATA_FORMAT_I2S,
91 .frame_clk_freq = FRAME_CLK_FREQ,
92 .block_size = BLOCK_SIZE,
93 .timeout = TIMEOUT,
94 .options =
95 I2S_OPT_FRAME_CLK_MASTER | I2S_OPT_BIT_CLK_MASTER,
96 .mem_slab = &tx_mem_slab };
97
98 err = i2s_configure(dev_i2s, I2S_DIR_TX, &test_config);
99 zassert_equal(err, 0, "CFG err=%d", err);
100
101 err = i2s_buf_write(dev_i2s, tx_data, BLOCK_SIZE);
102 zassert_equal(err, 0, "I2S buffer write unexpected error: %d", err);
103
104 err = i2s_trigger(dev_i2s, I2S_DIR_TX, INVALID_TRIGGER_SETTING);
105 zassert_equal(err, -EINVAL, "I2S invalid trigger setting not detected: err=%d", err);
106 }
107
ZTEST_USER(i2s_errors,test_i2s_unconfigured_access)108 ZTEST_USER(i2s_errors, test_i2s_unconfigured_access)
109 {
110 int err;
111 char tx_data[BLOCK_SIZE] = {0};
112 struct i2s_config inactive_config = { .word_size = 16U,
113 .channels = 2U,
114 .format = I2S_FMT_DATA_FORMAT_I2S,
115 .frame_clk_freq = 0,
116 .block_size = BLOCK_SIZE,
117 .timeout = TIMEOUT,
118 .options = I2S_OPT_FRAME_CLK_MASTER |
119 I2S_OPT_BIT_CLK_MASTER,
120 .mem_slab = &tx_mem_slab };
121
122 err = i2s_configure(dev_i2s, I2S_DIR_TX, &inactive_config);
123 zassert_equal(err, 0, "I2S interface NOT_READY state transition failed. err=%d", err);
124
125 err = i2s_buf_write(dev_i2s, tx_data, BLOCK_SIZE);
126 zassert_equal(
127 err, -EIO,
128 "I2S attempting unconfigured interface access did not raise I/O error, err=%d",
129 err);
130 }
131
ZTEST_USER(i2s_errors,test_i2s_improper_block_size_write)132 ZTEST_USER(i2s_errors, test_i2s_improper_block_size_write)
133 {
134 int err;
135 char tx_data[BLOCK_SIZE] = {0};
136 struct i2s_config test_config = { .word_size = 16U,
137 .channels = 2U,
138 .format = I2S_FMT_DATA_FORMAT_I2S,
139 .frame_clk_freq = FRAME_CLK_FREQ,
140 .block_size = BLOCK_SIZE,
141 .timeout = TIMEOUT,
142 .options =
143 I2S_OPT_FRAME_CLK_MASTER | I2S_OPT_BIT_CLK_MASTER,
144 .mem_slab = &tx_mem_slab };
145
146 err = i2s_configure(dev_i2s, I2S_DIR_TX, &test_config);
147 zassert_equal(err, 0, "Unexpected error when configuring I2S interface: %d", err);
148
149 err = i2s_buf_write(dev_i2s, tx_data, sizeof(uint16_t) + BLOCK_SIZE);
150 zassert_not_equal(
151 err, 0,
152 "I2S attempting write with incorrect block size did not raise error, err=%d", err);
153 }
154