forked from nrfconnect/sdk-nrf
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
314 lines (260 loc) · 8 KB
/
Copy pathmain.c
File metadata and controls
314 lines (260 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
#include <zephyr/drivers/gpio.h>
#include <hal/nrf_gpio.h>
#include <stdlib.h>
#include <math.h>
static NRF_GPIO_Type *gpio_port =
((NRF_GPIO_Type *)DT_REG_ADDR(DT_GPIO_CTLR(DT_PATH(zephyr_user), gpios)));
static uint32_t pin_mask = BIT(DT_GPIO_PIN(DT_PATH(zephyr_user), gpios));
static const struct gpio_dt_spec gpio_spec =
GPIO_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), gpios, 0);
static const struct device *const uart_dev = DEVICE_DT_GET(DT_NODELABEL(dut));
static const uint8_t tx_buf[] = {0x00, 0x00};
#define PIN_STATE_SIZE 32768
static uint8_t pin_state[PIN_STATE_SIZE] = {};
#define REPEAT_NUMBER 3
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void uart_fifo_callback(const struct device *dev, void *user_data)
{
int ret;
ARG_UNUSED(user_data);
ret = uart_irq_update(dev);
zassert_true(ret >= 0, "uart_irq_update: %d\n", ret);
ret = uart_irq_tx_ready(dev);
zassert_true(ret >= 0, "uart_irq_tx_ready: %d\n", ret);
ret = uart_fifo_fill(dev, (uint8_t *)&tx_buf, sizeof(tx_buf));
zassert_true(ret == sizeof(tx_buf), "uart_fifo_fill: %d\n", ret);
uart_irq_tx_disable(dev);
}
#endif
static void check_timing(uint32_t baudrate)
{
uint64_t cycles_s_sys;
struct uart_config test_uart_config;
int ret;
uint32_t cycle_start_time;
uint32_t cycle_stop_time;
double gpio_read_time_us_mean;
int32_t start_index;
int32_t stop_index;
int32_t idle_found;
int32_t start_index_count_zero;
double bit_diviation_mean;
double symbol_diviation_mean;
bool once = true;
int key;
ret = uart_config_get(uart_dev, &test_uart_config);
zassert_equal(ret, 0, "uart_config_get: %d\n", ret);
test_uart_config.parity = UART_CFG_PARITY_EVEN;
test_uart_config.stop_bits = UART_CFG_STOP_BITS_1;
test_uart_config.flow_ctrl = UART_CFG_FLOW_CTRL_NONE;
test_uart_config.baudrate = baudrate;
ret = uart_configure(uart_dev, &test_uart_config);
if (ret == -ENOTSUP) {
TC_PRINT("[%d] Not supported\n", baudrate);
ztest_test_skip();
}
zassert_equal(ret, 0, "uart_configure: %d\n", ret);
cycles_s_sys = (uint64_t)sys_clock_hw_cycles_per_sec();
TC_PRINT("Cycles: %llu cycles\n", cycles_s_sys);
double expected_bit_period_us = 1e6 / (double)baudrate;
double number_of_bits = 8;
number_of_bits += 1;
/* Stop bit is 1 so it is not counted. */
if (test_uart_config.parity != UART_CFG_PARITY_NONE) {
number_of_bits += 1;
}
double expected_symbol_period_us = number_of_bits * expected_bit_period_us;
TC_PRINT("[%d] Expected symbol time: %.2f us, expected bit time: %.2f us\n", baudrate,
expected_symbol_period_us, expected_bit_period_us);
start_index_count_zero = 0;
bit_diviation_mean = 0;
symbol_diviation_mean = 0;
for (uint32_t t = 0; t < REPEAT_NUMBER; ++t) {
/*
* Send character
*/
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
uart_irq_tx_enable(uart_dev);
#else
uart_tx(uart_dev, tx_buf, sizeof(tx_buf), 0);
#endif
/*
* Check gpio
*/
key = irq_lock();
cycle_start_time = k_cycle_get_32();
for (uint32_t i = 0; i < PIN_STATE_SIZE; ++i) {
pin_state[i] = nrf_gpio_port_in_read(gpio_port) & pin_mask ? 1 : 0;
}
cycle_stop_time = k_cycle_get_32();
irq_unlock(key);
if (once) {
/* Calculate only for the first iteration. */
uint32_t t_us = k_cyc_to_us_ceil32(cycle_stop_time - cycle_start_time);
gpio_read_time_us_mean = (double)t_us / PIN_STATE_SIZE;
once = false;
TC_PRINT("GPIO get takes: %.2f us\n", gpio_read_time_us_mean);
}
if (expected_bit_period_us < gpio_read_time_us_mean) {
TC_PRINT("[%d] Not supported - gpio measurement is too slow.\n", baudrate);
ztest_test_skip();
}
/*
* Find start of start bit and end of stop bit. For higher baudrates it is
* possible that first byte is already being transferred. In search for
* byte start and end, start from searching for idle state between byte 0 and 1.
*/
start_index = -1;
stop_index = -1;
idle_found = -1;
for (uint32_t i = 0; i < PIN_STATE_SIZE; ++i) {
if (-1 == start_index) {
if (1 == pin_state[i]) {
idle_found = 0;
} else if ((idle_found == 0) && (0 == pin_state[i])) {
start_index = i;
}
} else {
if (-1 == stop_index) {
if (1 == pin_state[i]) {
stop_index = i;
break;
}
} else {
zassert_true(1 == pin_state[i], "Unexpected low at %d\n",
i);
}
}
}
TC_PRINT("Start index: %d, stop index: %d\n", start_index, stop_index);
zassert_true(start_index != -1, "Missing start_index\n");
zassert_true(stop_index != -1, "Missing stop_index\n");
double measured_period_us = (stop_index - start_index) * gpio_read_time_us_mean;
double measured_bit_us = measured_period_us / (double)number_of_bits;
TC_PRINT("[%d][%s][%d] Measured symbol period: %.2f us, measured bit time: %.2f "
"us\n",
t, uart_dev->name, baudrate, measured_period_us, measured_bit_us);
double symbol_diviation = 100 *
fabs(measured_period_us - expected_symbol_period_us) /
expected_symbol_period_us;
double bit_diviation = 100 * fabs(measured_bit_us - expected_bit_period_us) /
expected_bit_period_us;
TC_PRINT("[%d][%s][%d] Symbol diviation: %.2f%%, bit diviation: %.2f%%\n", t,
uart_dev->name, baudrate, symbol_diviation, bit_diviation);
if (start_index == 0) {
start_index_count_zero++;
}
symbol_diviation_mean += symbol_diviation;
bit_diviation_mean += bit_diviation;
}
symbol_diviation_mean /= (double)REPEAT_NUMBER;
bit_diviation_mean /= (double)REPEAT_NUMBER;
TC_PRINT("[%s][%d] Mean symbol diviation: %.2f%%, mean bit diviation: %.2f%%\n",
uart_dev->name, baudrate, symbol_diviation_mean, bit_diviation_mean);
if (start_index_count_zero == 0) {
zassert_true(symbol_diviation_mean <= (double)CONFIG_TEST_ALLOWED_DEVIATION,
"Symbol diviation %0.f%% higher than %d%%\n", symbol_diviation_mean,
CONFIG_TEST_ALLOWED_DEVIATION);
zassert_true(bit_diviation_mean <= (double)CONFIG_TEST_ALLOWED_DEVIATION,
"Bit diviation %0.f%% higher than %d%%\n", bit_diviation_mean,
CONFIG_TEST_ALLOWED_DEVIATION);
} else {
TC_PRINT("Not checking diviation due to lost start of start bit\n");
}
}
ZTEST(uart_baudrate_test, test_08_2400)
{
check_timing(2400);
}
ZTEST(uart_baudrate_test, test_09_4800)
{
check_timing(4800);
}
ZTEST(uart_baudrate_test, test_10_9600)
{
check_timing(9600);
}
ZTEST(uart_baudrate_test, test_11_14400)
{
check_timing(14400);
}
ZTEST(uart_baudrate_test, test_12_19200)
{
check_timing(19200);
}
ZTEST(uart_baudrate_test, test_13_38400)
{
check_timing(38400);
}
ZTEST(uart_baudrate_test, test_14_57600)
{
check_timing(57600);
}
ZTEST(uart_baudrate_test, test_15_115200)
{
check_timing(115200);
}
ZTEST(uart_baudrate_test, test_16_230400)
{
check_timing(230400);
}
ZTEST(uart_baudrate_test, test_17_460800)
{
check_timing(460800);
}
ZTEST(uart_baudrate_test, test_18_576000)
{
check_timing(576000);
}
ZTEST(uart_baudrate_test, test_19_921600)
{
check_timing(921600);
}
ZTEST(uart_baudrate_test, test_20_1000000)
{
check_timing(1000000);
}
ZTEST(uart_baudrate_test, test_21_2000000)
{
check_timing(2000000);
}
ZTEST(uart_baudrate_test, test_22_3000000)
{
check_timing(3000000);
}
ZTEST(uart_baudrate_test, test_23_4000000)
{
check_timing(4000000);
}
ZTEST(uart_baudrate_test, test_24_8000000)
{
check_timing(8000000);
}
static void *uart_baudrate_test_setup(void)
{
int ret;
/*
* Wait for any initialization to finish
*/
k_msleep(1000);
zassert_true(gpio_is_ready_dt(&gpio_spec), "GPIO is not ready");
ret = gpio_pin_configure_dt(&gpio_spec, GPIO_INPUT);
zassert_true(ret == 0, "gpio_pin_configure_dt: %d", ret);
zassert_true(device_is_ready(uart_dev), "UART device is not ready");
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
ret = uart_irq_callback_set(uart_dev, uart_fifo_callback);
zassert_true(ret == 0, "uart_irq_callback_set: %d", ret);
#endif
return NULL;
}
ZTEST_SUITE(uart_baudrate_test, NULL, uart_baudrate_test_setup, NULL, NULL, NULL);