uart_rxnb_txb.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-06-16 KyleChan the first version
  9. */
  10. #include <rtthread.h>
  11. #include "utest.h"
  12. #include <rtdevice.h>
  13. #include <stdlib.h>
  14. #define TC_UART_DEVICE_NAME "uart2"
  15. #define TC_UART_SEND_TIMES 100
  16. #ifdef UTEST_SERIAL_TC
  17. #define TEST_UART_NAME TC_UART_DEVICE_NAME
  18. static struct rt_serial_device *serial;
  19. static rt_sem_t rx_sem;
  20. static rt_uint8_t uart_over_flag;
  21. static rt_bool_t uart_result = RT_TRUE;
  22. static rt_err_t uart_find(void)
  23. {
  24. serial = (struct rt_serial_device *)rt_device_find(TEST_UART_NAME);
  25. if (serial == RT_NULL)
  26. {
  27. LOG_E("find %s device failed!\n", TEST_UART_NAME);
  28. return -RT_ERROR;
  29. }
  30. return RT_EOK;
  31. }
  32. static rt_err_t uart_rx_indicate(rt_device_t device, rt_size_t size)
  33. {
  34. rt_sem_release(rx_sem);
  35. return RT_EOK;
  36. }
  37. static void uart_send_entry(void *parameter)
  38. {
  39. rt_uint8_t *uart_write_buffer;
  40. rt_uint16_t send_len;
  41. rt_uint32_t i = 0;
  42. send_len = *(rt_uint16_t *)parameter;
  43. /* assign send buffer */
  44. uart_write_buffer = (rt_uint8_t *)rt_malloc(send_len);
  45. if (uart_write_buffer == RT_NULL)
  46. {
  47. LOG_E("Without spare memory for uart dma!");
  48. uart_result = RT_FALSE;
  49. return;
  50. }
  51. rt_memset(uart_write_buffer, 0, send_len);
  52. for (i = 0; i < send_len; i++)
  53. {
  54. uart_write_buffer[i] = (rt_uint8_t)i;
  55. }
  56. /* send buffer */
  57. if (rt_device_write(&serial->parent, 0, uart_write_buffer, send_len) != send_len)
  58. {
  59. LOG_E("device write failed\r\n");
  60. }
  61. rt_free(uart_write_buffer);
  62. }
  63. static void uart_rec_entry(void *parameter)
  64. {
  65. rt_uint16_t rev_len;
  66. rev_len = *(rt_uint16_t *)parameter;
  67. rt_uint8_t *ch;
  68. ch = (rt_uint8_t *)rt_calloc(1, sizeof(rt_uint8_t) * (rev_len + 1));
  69. rt_int32_t cnt, i;
  70. rt_uint8_t last_old_data;
  71. rt_bool_t fisrt_flag = RT_TRUE;
  72. rt_uint32_t all_receive_length = 0;
  73. while (1)
  74. {
  75. rt_err_t result;
  76. result = rt_sem_take(rx_sem, RT_WAITING_FOREVER);
  77. if (result != RT_EOK)
  78. {
  79. LOG_E("take sem err in recv.");
  80. }
  81. cnt = rt_device_read(&serial->parent, 0, (void *)ch, rev_len);
  82. if (cnt == 0)
  83. {
  84. continue;
  85. }
  86. if (fisrt_flag != RT_TRUE)
  87. {
  88. if ((rt_uint8_t)(last_old_data + 1) != ch[0])
  89. {
  90. LOG_E("_Read Different data -> former data: %x, current data: %x.", last_old_data, ch[0]);
  91. uart_result = RT_FALSE;
  92. rt_free(ch);
  93. return;
  94. }
  95. }
  96. else
  97. {
  98. fisrt_flag = RT_FALSE;
  99. }
  100. for (i = 0; i < cnt - 1; i++)
  101. {
  102. if ((rt_uint8_t)(ch[i] + 1) != ch[i + 1])
  103. {
  104. LOG_E("Read Different data -> former data: %x, current data: %x.", ch[i], ch[i + 1]);
  105. uart_result = RT_FALSE;
  106. rt_free(ch);
  107. return;
  108. }
  109. }
  110. all_receive_length += cnt;
  111. if (all_receive_length >= rev_len)
  112. break;
  113. else
  114. last_old_data = ch[cnt - 1];
  115. }
  116. rt_free(ch);
  117. uart_over_flag = RT_TRUE;
  118. }
  119. static rt_err_t uart_api(rt_uint16_t test_buf)
  120. {
  121. rt_thread_t thread_send = RT_NULL;
  122. rt_thread_t thread_recv = RT_NULL;
  123. rt_err_t result = RT_EOK;
  124. result = uart_find();
  125. if (result != RT_EOK)
  126. {
  127. return -RT_ERROR;
  128. }
  129. rx_sem = rt_sem_create("rx_sem", 0, RT_IPC_FLAG_PRIO);
  130. if (rx_sem == RT_NULL)
  131. {
  132. LOG_E("Init sem failed.");
  133. uart_result = RT_FALSE;
  134. return -RT_ERROR;
  135. }
  136. /* reinitialize */
  137. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  138. config.baud_rate = BAUD_RATE_115200;
  139. config.rx_bufsz = BSP_UART2_RX_BUFSIZE;
  140. config.tx_bufsz = BSP_UART2_TX_BUFSIZE;
  141. rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONFIG, &config);
  142. result = rt_device_open(&serial->parent, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
  143. if (result != RT_EOK)
  144. {
  145. LOG_E("Open uart device failed.");
  146. uart_result = RT_FALSE;
  147. rt_sem_delete(rx_sem);
  148. return -RT_ERROR;
  149. }
  150. /* set receive callback function */
  151. result = rt_device_set_rx_indicate(&serial->parent, uart_rx_indicate);
  152. if (result != RT_EOK)
  153. {
  154. goto __exit;
  155. }
  156. thread_recv = rt_thread_create("uart_recv", uart_rec_entry, &test_buf, 1024, RT_THREAD_PRIORITY_MAX - 5, 10);
  157. thread_send = rt_thread_create("uart_send", uart_send_entry, &test_buf, 1024, RT_THREAD_PRIORITY_MAX - 4, 10);
  158. if (thread_send != RT_NULL && thread_recv != RT_NULL)
  159. {
  160. rt_thread_startup(thread_recv);
  161. rt_thread_startup(thread_send);
  162. }
  163. else
  164. {
  165. result = -RT_ERROR;
  166. goto __exit;
  167. }
  168. while (1)
  169. {
  170. if (uart_result != RT_TRUE)
  171. {
  172. LOG_E("The test for uart dma is failure.");
  173. result = -RT_ERROR;
  174. goto __exit;
  175. }
  176. if (uart_over_flag == RT_TRUE)
  177. {
  178. goto __exit;
  179. }
  180. /* waiting for test over */
  181. rt_thread_mdelay(5);
  182. }
  183. __exit:
  184. if (rx_sem)
  185. rt_sem_delete(rx_sem);
  186. rt_device_close(&serial->parent);
  187. uart_over_flag = RT_FALSE;
  188. return result;
  189. }
  190. static void tc_uart_api(void)
  191. {
  192. rt_uint32_t times = 0;
  193. rt_uint16_t num = 0;
  194. while (TC_UART_SEND_TIMES - times)
  195. {
  196. num = (rand() % 1000) + 1;
  197. if(uart_api(num) == RT_EOK)
  198. LOG_I("data_lens [%3d], it is correct to read and write data. [%d] times testing.", num, ++times);
  199. else
  200. {
  201. LOG_E("uart test error");
  202. break;
  203. }
  204. }
  205. uassert_true(uart_result == RT_TRUE);
  206. }
  207. static rt_err_t utest_tc_init(void)
  208. {
  209. LOG_I("UART TEST: Please connect Tx and Rx directly for self testing.");
  210. return RT_EOK;
  211. }
  212. static rt_err_t utest_tc_cleanup(void)
  213. {
  214. rx_sem = RT_NULL;
  215. uart_result = RT_TRUE;
  216. uart_over_flag = RT_FALSE;
  217. return RT_EOK;
  218. }
  219. static void testcase(void)
  220. {
  221. UTEST_UNIT_RUN(tc_uart_api);
  222. }
  223. UTEST_TC_EXPORT(testcase, "testcases.drivers.uart_rxnb_txb", utest_tc_init, utest_tc_cleanup, 30);
  224. #endif /* TC_UART_USING_TC */