uart_flush_rx.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. *
  9. */
  10. #include <rtthread.h>
  11. #include "utest.h"
  12. #include <rtdevice.h>
  13. #include <stdlib.h>
  14. #ifdef UTEST_SERIAL_TC
  15. static struct rt_serial_device *serial;
  16. static rt_err_t uart_find(void)
  17. {
  18. serial = (struct rt_serial_device *)rt_device_find(RT_SERIAL_TC_DEVICE_NAME);
  19. if (serial == RT_NULL)
  20. {
  21. LOG_E("find %s device failed!\n", RT_SERIAL_TC_DEVICE_NAME);
  22. return -RT_ERROR;
  23. }
  24. return RT_EOK;
  25. }
  26. static rt_err_t test_item(rt_uint8_t *uart_write_buffer, rt_uint32_t size)
  27. {
  28. rt_device_write(&serial->parent, 0, uart_write_buffer, size);
  29. rt_thread_mdelay(size * 0.0868 + 5);
  30. if (1 != rt_device_read(&serial->parent, 0, uart_write_buffer, 1))
  31. {
  32. LOG_E("read failed.");
  33. return -RT_ERROR;
  34. }
  35. rt_device_control(&serial->parent, RT_SERIAL_CTRL_RX_FLUSH, RT_NULL);
  36. if (0 != rt_device_read(&serial->parent, 0, uart_write_buffer, 1))
  37. {
  38. LOG_E("read failed.");
  39. return -RT_ERROR;
  40. }
  41. return RT_EOK;
  42. }
  43. static rt_bool_t uart_api()
  44. {
  45. rt_err_t result = RT_EOK;
  46. result = uart_find();
  47. if (result != RT_EOK)
  48. {
  49. return RT_FALSE;
  50. }
  51. /* Reinitialize */
  52. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  53. config.baud_rate = BAUD_RATE_115200;
  54. config.rx_bufsz = RT_SERIAL_TC_RXBUF_SIZE;
  55. config.tx_bufsz = RT_SERIAL_TC_TXBUF_SIZE;
  56. #ifdef RT_SERIAL_USING_DMA
  57. config.dma_ping_bufsz = RT_SERIAL_TC_RXBUF_SIZE / 2;
  58. #endif
  59. rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONFIG, &config);
  60. result = rt_device_open(&serial->parent, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
  61. if (result != RT_EOK)
  62. {
  63. LOG_E("Open uart device failed.");
  64. return RT_FALSE;
  65. }
  66. rt_uint8_t *uart_write_buffer;
  67. rt_uint32_t i;
  68. uart_write_buffer = (rt_uint8_t *)rt_malloc(sizeof(rt_uint8_t) * (RT_SERIAL_TC_TXBUF_SIZE * 5 + 1));
  69. srand(rt_tick_get());
  70. for (i = 0; i < RT_SERIAL_TC_SEND_ITERATIONS; i++)
  71. {
  72. if (RT_EOK != test_item(uart_write_buffer, RT_SERIAL_TC_RXBUF_SIZE + RT_SERIAL_TC_RXBUF_SIZE * (rand() % 5)))
  73. {
  74. LOG_E("test_item failed.");
  75. result = -RT_ERROR;
  76. goto __exit;
  77. }
  78. if (RT_EOK != test_item(uart_write_buffer, rand() % (RT_SERIAL_TC_RXBUF_SIZE * 5)))
  79. {
  80. LOG_E("test_item failed.");
  81. result = -RT_ERROR;
  82. goto __exit;
  83. }
  84. }
  85. __exit:
  86. rt_free(uart_write_buffer);
  87. rt_device_close(&serial->parent);
  88. rt_thread_mdelay(5);
  89. return result == RT_EOK ? RT_TRUE : RT_FALSE;
  90. }
  91. static void tc_uart_api(void)
  92. {
  93. uassert_true(uart_api() == RT_TRUE);
  94. }
  95. static rt_err_t utest_tc_init(void)
  96. {
  97. LOG_I("UART TEST: Please connect Tx and Rx directly for self testing.");
  98. return RT_EOK;
  99. }
  100. static rt_err_t utest_tc_cleanup(void)
  101. {
  102. rt_device_t uart_dev = rt_device_find(RT_SERIAL_TC_DEVICE_NAME);
  103. while (rt_device_close(uart_dev) != -RT_ERROR);
  104. return RT_EOK;
  105. }
  106. static void testcase(void)
  107. {
  108. UTEST_UNIT_RUN(tc_uart_api);
  109. }
  110. UTEST_TC_EXPORT(testcase, "testcases.drivers.uart_flush_rx", utest_tc_init, utest_tc_cleanup, 30);
  111. #endif /* TC_UART_USING_TC */