|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/* |
| 8 | + * @addtogroup t_uart_basic |
| 9 | + * @{ |
| 10 | + * @defgroup t_uart_fifo test_uart_fifo |
| 11 | + * @brief TestPurpose: verify UART works well in fifo mode |
| 12 | + * @details |
| 13 | + * - Test Steps |
| 14 | + * - FIFO Output: |
| 15 | + * -# Set UART IRQ callback using uart_irq_callback_set(). |
| 16 | + * -# Enable UART TX IRQ using uart_irq_tx_enable(). |
| 17 | + * -# Output the prepared data using uart_fifo_fill(). |
| 18 | + * -# Disable UART TX IRQ using uart_irq_tx_disable(). |
| 19 | + * -# Compare the number of characters sent out with the |
| 20 | + * original data size. |
| 21 | + * - FIFO Input: |
| 22 | + * -# Set UART IRQ callback using uart_irq_callback_set(). |
| 23 | + * -# Enable UART RX IRQ using uart_irq_rx_enable(). |
| 24 | + * -# Wait for data sent to UART console and trigger RX IRQ. |
| 25 | + * -# Read data from UART console using uart_fifo_read(). |
| 26 | + * -# Disable UART TX IRQ using uart_irq_rx_disable(). |
| 27 | + * - Expected Results |
| 28 | + * -# When test UART FIFO output, the number of characters actually |
| 29 | + * sent out will be equal to the original length of the characters. |
| 30 | + * -# When test UART FIFO input, the app will wait for input from UART |
| 31 | + * console and exit after receiving one character. |
| 32 | + * @} |
| 33 | + */ |
| 34 | + |
| 35 | +#include <test_uart.h> |
| 36 | + |
| 37 | +static volatile bool data_transmitted; |
| 38 | +static volatile bool data_received; |
| 39 | +static int char_sent; |
| 40 | +static const char *fifo_data = "This is a FIFO test.\r\n"; |
| 41 | + |
| 42 | +#define DATA_SIZE strlen(fifo_data) |
| 43 | + |
| 44 | +static void uart_fifo_callback(struct device *dev) |
| 45 | +{ |
| 46 | + char recvData; |
| 47 | + |
| 48 | + /* Verify uart_irq_update() */ |
| 49 | + uart_irq_update(dev); |
| 50 | + |
| 51 | + /* Verify uart_irq_tx_ready() */ |
| 52 | + if (uart_irq_tx_ready(dev)) { |
| 53 | + data_transmitted = true; |
| 54 | + char_sent++; |
| 55 | + } |
| 56 | + |
| 57 | + /* Verify uart_irq_rx_ready() */ |
| 58 | + if (uart_irq_rx_ready(dev)) { |
| 59 | + /* Verify uart_fifo_read() */ |
| 60 | + uart_fifo_read(dev, &recvData, 1); |
| 61 | + TC_PRINT("%c", recvData); |
| 62 | + |
| 63 | + if (recvData == '\n') { |
| 64 | + data_received = true; |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +static int test_fifo_read(void) |
| 70 | +{ |
| 71 | + struct device *uart_dev = device_get_binding(UART_DEVICE_NAME); |
| 72 | + |
| 73 | + /* Verify uart_irq_callback_set() */ |
| 74 | + uart_irq_callback_set(uart_dev, uart_fifo_callback); |
| 75 | + |
| 76 | + /* Enable Tx/Rx interrupt before using fifo */ |
| 77 | + /* Verify uart_irq_rx_enable() */ |
| 78 | + uart_irq_rx_enable(uart_dev); |
| 79 | + |
| 80 | + TC_PRINT("Please send characters to serial console\n"); |
| 81 | + |
| 82 | + data_received = false; |
| 83 | + while (data_received == false) |
| 84 | + ; |
| 85 | + /* Verify uart_irq_rx_disable() */ |
| 86 | + uart_irq_rx_disable(uart_dev); |
| 87 | + |
| 88 | + return TC_PASS; |
| 89 | +} |
| 90 | + |
| 91 | +static int test_fifo_fill(void) |
| 92 | +{ |
| 93 | + struct device *uart_dev = device_get_binding(UART_DEVICE_NAME); |
| 94 | + |
| 95 | + char_sent = 0; |
| 96 | + |
| 97 | + /* Verify uart_irq_callback_set() */ |
| 98 | + uart_irq_callback_set(uart_dev, uart_fifo_callback); |
| 99 | + |
| 100 | + /* Enable Tx/Rx interrupt before using fifo */ |
| 101 | + /* Verify uart_irq_tx_enable() */ |
| 102 | + uart_irq_tx_enable(uart_dev); |
| 103 | + |
| 104 | + /* Verify uart_fifo_fill() */ |
| 105 | + for (int i = 0; i < DATA_SIZE; i++) { |
| 106 | + data_transmitted = false; |
| 107 | + while (!uart_fifo_fill(uart_dev, &fifo_data[i], 1)) |
| 108 | + ; |
| 109 | + while (data_transmitted == false) |
| 110 | + ; |
| 111 | + } |
| 112 | + |
| 113 | + /* Verify uart_irq_tx_disable() */ |
| 114 | + uart_irq_tx_disable(uart_dev); |
| 115 | + |
| 116 | + /* strlen() doesn't include \r\n*/ |
| 117 | + if (char_sent - 1 == DATA_SIZE) { |
| 118 | + return TC_PASS; |
| 119 | + } else { |
| 120 | + return TC_FAIL; |
| 121 | + } |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | +void test_uart_fifo_fill(void) |
| 126 | +{ |
| 127 | + assert_true(test_fifo_fill() == TC_PASS, NULL); |
| 128 | +} |
| 129 | + |
| 130 | +void test_uart_fifo_read(void) |
| 131 | +{ |
| 132 | + assert_true(test_fifo_read() == TC_PASS, NULL); |
| 133 | +} |
0 commit comments