uart_blocking_rx.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include "utest.h"
  4. #ifdef UTEST_SERIAL_TC
  5. static rt_bool_t block_read(rt_device_t uart_dev)
  6. {
  7. rt_size_t total_length, recv_length;
  8. char uart_read_buffer[1024], log_buffer[64];
  9. /* make sure device is closed and reopen it */
  10. while (rt_device_close(uart_dev) != -RT_ERROR);
  11. rt_device_open(uart_dev, RT_DEVICE_FLAG_TX_BLOCKING | RT_DEVICE_FLAG_RX_BLOCKING);
  12. rt_sprintf(log_buffer, "\nBLOCKING READ BEGIN, PLEASE SEND SOME DATAS\n");
  13. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  14. total_length = 0;
  15. recv_length = 0;
  16. recv_length = rt_device_read(uart_dev, -1, uart_read_buffer, rt_strlen(log_buffer));
  17. total_length += recv_length;
  18. rt_sprintf(log_buffer, "\nblock : %d bytes read, total: %d \n", recv_length, total_length);
  19. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  20. recv_length = 0;
  21. recv_length = rt_device_read(uart_dev, -1, uart_read_buffer, rt_strlen(log_buffer));
  22. total_length += recv_length;
  23. rt_sprintf(log_buffer, "\nblock : %d bytes read , total: %d \n", recv_length, total_length);
  24. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  25. recv_length = 0;
  26. recv_length = rt_device_read(uart_dev, -1, uart_read_buffer, rt_strlen(log_buffer));
  27. total_length += recv_length;
  28. rt_sprintf(log_buffer, "\nblock : %d bytes read , total: %d \n", recv_length, total_length);
  29. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  30. recv_length = 0;
  31. recv_length = rt_device_read(uart_dev, -1, uart_read_buffer, rt_strlen(log_buffer));
  32. total_length += recv_length;
  33. rt_sprintf(log_buffer, "\nblock : %d bytes read , total: %d \n", recv_length, total_length);
  34. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  35. recv_length = 0;
  36. recv_length = rt_device_read(uart_dev, -1, uart_read_buffer, rt_strlen(log_buffer));
  37. total_length += recv_length;
  38. rt_sprintf(log_buffer, "\nblock : %d bytes read , total: %d \n", recv_length, total_length);
  39. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  40. rt_thread_mdelay(1000);
  41. rt_sprintf(log_buffer, "BLOCKING READ END");
  42. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  43. return RT_TRUE;
  44. }
  45. static void uart_test_blocking_rx(void)
  46. {
  47. rt_device_t uart_dev;
  48. uart_dev = rt_device_find(RT_SERIAL_TC_DEVICE_NAME);
  49. uassert_not_null(uart_dev);
  50. uassert_true(block_read(uart_dev));
  51. }
  52. static rt_err_t utest_tc_init(void)
  53. {
  54. return RT_EOK;
  55. }
  56. static rt_err_t utest_tc_cleanup(void)
  57. {
  58. rt_device_t uart_dev = rt_device_find(RT_SERIAL_TC_DEVICE_NAME);
  59. while (rt_device_close(uart_dev) != -RT_ERROR);
  60. return RT_EOK;
  61. }
  62. static void testcase(void)
  63. {
  64. UTEST_UNIT_RUN(uart_test_blocking_rx);
  65. }
  66. UTEST_TC_EXPORT(testcase, "uart_blocking_rx", utest_tc_init, utest_tc_cleanup, 10);
  67. #endif