uart_qemu_echo.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include "utest.h"
  4. #define UART_SEND_TIMES 100
  5. #define UART_TEST_NUMBER 6
  6. #ifdef UTEST_SERIAL_TC
  7. #define echo_test_buffer_size (1024)
  8. static rt_device_t u1serial;
  9. static rt_device_t u2serial;
  10. static rt_uint32_t u2rx_length = 0;
  11. static rt_uint32_t u2tx_length = 0;
  12. static rt_uint32_t u1rx_length = 0;
  13. static rt_uint32_t u1tx_length = 0;
  14. static rt_uint8_t uart_over_flag = RT_FALSE;
  15. static void echo_test_u2_thread_entry(void *parameter)
  16. {
  17. char *uart_name = "uart2";
  18. u2serial = rt_device_find(uart_name);
  19. if (!u2serial)
  20. {
  21. LOG_I("find %s failed!\n", uart_name);
  22. return;
  23. }
  24. rt_uint8_t *rx_buffer = rt_malloc(echo_test_buffer_size);
  25. rt_device_open(u2serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
  26. rt_ssize_t buf_datalen = 0;
  27. while (1)
  28. {
  29. rt_device_control(u2serial, RT_SERIAL_CTRL_GET_UNREAD_BYTES_COUNT, (void *)&buf_datalen);
  30. int32_t recbLen = rt_device_read(u2serial, 0, rx_buffer, buf_datalen > 0 ? buf_datalen : 1);
  31. if (recbLen > 0)
  32. {
  33. u2rx_length += recbLen;
  34. u2tx_length += rt_device_write(u2serial, 0, rx_buffer, recbLen);
  35. if (uart_over_flag)
  36. break;
  37. }
  38. }
  39. rt_free(rx_buffer);
  40. }
  41. static void echo_test_u1_thread_entry(void *parameter)
  42. {
  43. rt_uint8_t *rx_buffer = rt_malloc(echo_test_buffer_size);
  44. rt_ssize_t buf_datalen = 0;
  45. while (1)
  46. {
  47. rt_device_control(u1serial, RT_SERIAL_CTRL_GET_UNREAD_BYTES_COUNT, (void *)&buf_datalen);
  48. int32_t recbLen = rt_device_read(u1serial, 0, rx_buffer, buf_datalen > 0 ? buf_datalen : 1);
  49. if (recbLen > 0)
  50. {
  51. u1rx_length += recbLen;
  52. if (uart_over_flag)
  53. break;
  54. }
  55. }
  56. rt_free(rx_buffer);
  57. }
  58. static rt_bool_t echo_test()
  59. {
  60. rt_bool_t result = RT_TRUE;
  61. char *uart_name = "uart1";
  62. u1serial = rt_device_find(uart_name);
  63. if (!u1serial)
  64. {
  65. LOG_I("find %s failed!\n", uart_name);
  66. return RT_FALSE;
  67. }
  68. rt_uint8_t *tx_buffer = rt_malloc(echo_test_buffer_size);
  69. rt_device_open(u1serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
  70. rt_thread_startup(rt_thread_create("serial2", echo_test_u2_thread_entry, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX - 4, 5));
  71. rt_thread_startup(rt_thread_create("serial1", echo_test_u1_thread_entry, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX - 5, 5));
  72. uint32_t sendTotalCount = 0;
  73. srand(rt_tick_get());
  74. for (uint32_t count = 0; count < 1000; count++)
  75. {
  76. // Indefinite length of data is sent
  77. uint32_t sendCount = rand() % echo_test_buffer_size;
  78. u1tx_length += rt_device_write(u1serial, 0, tx_buffer, sendCount);
  79. sendTotalCount += sendCount;
  80. // Wait for the cross-send to complete
  81. rt_thread_mdelay(UART_SEND_TIMES);
  82. if (count % 50 == 0)
  83. {
  84. LOG_I("echo, uart2: tx: %ld, rx: %ld", u2tx_length, u2rx_length);
  85. LOG_I("echo, uart1: tx: %ld, rx: %ld", u1tx_length, u1rx_length);
  86. if (u2tx_length != u2rx_length || u1tx_length != u1rx_length || u2tx_length != u1tx_length)
  87. {
  88. LOG_I("echo test error!!!");
  89. result = RT_FALSE;
  90. break;
  91. }
  92. if (u2tx_length != sendTotalCount)
  93. {
  94. LOG_I("u2tx_length != sendTotalCount echo test error!!!");
  95. result = RT_FALSE;
  96. break;
  97. }
  98. }
  99. }
  100. uart_over_flag = RT_TRUE;
  101. // Notify the thread to exit
  102. rt_device_write(u1serial, 0, tx_buffer, echo_test_buffer_size);
  103. rt_thread_mdelay(30);
  104. {
  105. rt_device_t uart_dev = rt_device_find("uart2");
  106. while (rt_device_close(uart_dev) != -RT_ERROR);
  107. }
  108. {
  109. rt_device_t uart_dev = rt_device_find("uart1");
  110. while (rt_device_close(uart_dev) != -RT_ERROR);
  111. }
  112. rt_free(tx_buffer);
  113. return result;
  114. }
  115. static void uart_test_nonblocking_tx(void)
  116. {
  117. uassert_true(echo_test());
  118. }
  119. static rt_err_t utest_tc_init(void)
  120. {
  121. return RT_EOK;
  122. }
  123. static rt_err_t utest_tc_cleanup(void)
  124. {
  125. u1serial = RT_NULL;
  126. u2serial = RT_NULL;
  127. u2rx_length = 0;
  128. u2tx_length = 0;
  129. u1rx_length = 0;
  130. u1tx_length = 0;
  131. uart_over_flag = RT_FALSE;
  132. return RT_EOK;
  133. }
  134. static void testcase(void)
  135. {
  136. UTEST_UNIT_RUN(uart_test_nonblocking_tx);
  137. }
  138. UTEST_TC_EXPORT(testcase, "testcases.drivers.uart_qemu_echo", utest_tc_init, utest_tc_cleanup, 10);
  139. #endif