1 /*
2 * Copyright (c) 2023 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/logging/log.h>
9 #include <zephyr/logging/log_backend.h>
10 #include <zephyr/drivers/uart.h>
11 #include <zephyr/drivers/serial/uart_emul.h>
12
13 LOG_MODULE_REGISTER(test, CONFIG_SAMPLE_MODULE_LOG_LEVEL);
14
15 #define EMUL_UART_NUM DT_NUM_INST_STATUS_OKAY(zephyr_uart_emul)
16 #define EMUL_UART_NODE(i) DT_NODELABEL(euart##i)
17 #define EMUL_UART_DEV_INIT(i, _) DEVICE_DT_GET(EMUL_UART_NODE(i))
18 #define EMUL_UART_TX_FIFO_SIZE(i) DT_PROP(EMUL_UART_NODE(i), tx_fifo_size)
19 #define SAMPLE_DATA_SIZE EMUL_UART_TX_FIFO_SIZE(0)
20
21 #define TEST_DATA "0123456789ABCDEF"
22 /* Assert that size of the test string (without '\0') fits in the SAMPLE_DATA_SIZE */
23 BUILD_ASSERT((sizeof(TEST_DATA) - 1) < SAMPLE_DATA_SIZE);
24
25 struct log_backend_uart_fixture {
26 const struct device *dev[EMUL_UART_NUM];
27 };
28
uart_emul_setup(void)29 static void *uart_emul_setup(void)
30 {
31 static struct log_backend_uart_fixture fixture = {
32 .dev = {LISTIFY(EMUL_UART_NUM, EMUL_UART_DEV_INIT, (,))}};
33
34 for (size_t i = 0; i < EMUL_UART_NUM; i++) {
35 zassert_not_null(fixture.dev[i]);
36 }
37
38 return &fixture;
39 }
40
uart_emul_before(void * f)41 static void uart_emul_before(void *f)
42 {
43 struct log_backend_uart_fixture *fixture = f;
44
45 for (size_t i = 0; i < EMUL_UART_NUM; i++) {
46 uart_irq_tx_disable(fixture->dev[i]);
47 uart_irq_rx_disable(fixture->dev[i]);
48
49 uart_emul_flush_rx_data(fixture->dev[i]);
50 uart_emul_flush_tx_data(fixture->dev[i]);
51
52 uart_err_check(fixture->dev[i]);
53 }
54 }
55
ZTEST_F(log_backend_uart,test_log_backend_uart_multi_instance)56 ZTEST_F(log_backend_uart, test_log_backend_uart_multi_instance)
57 {
58 zassert_equal(log_backend_count_get(), EMUL_UART_NUM, "Unexpected number of instance(s)");
59
60 LOG_RAW(TEST_DATA);
61
62 for (size_t i = 0; i < EMUL_UART_NUM; i++) {
63 uint8_t tx_content[SAMPLE_DATA_SIZE] = {0};
64 size_t tx_len;
65
66 tx_len = uart_emul_get_tx_data(fixture->dev[i], tx_content, sizeof(tx_content));
67 zassert_equal(tx_len, strlen(TEST_DATA),
68 "%d: TX buffer length does not match. Expected %d, got %d",
69 i, strlen(TEST_DATA), tx_len);
70 zassert_mem_equal(tx_content, TEST_DATA, strlen(tx_content));
71 }
72 }
73
74 ZTEST_SUITE(log_backend_uart, NULL, uart_emul_setup, uart_emul_before, NULL, NULL);
75