test_rs485.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /* This file is from test_uart.c, but mainly about RS485 */
  7. #include <string.h>
  8. #include <sys/param.h>
  9. #include "unity.h"
  10. #include "test_utils.h" // unity_send_signal
  11. #include "driver/uart.h" // for the uart driver access
  12. #include "esp_log.h"
  13. #include "esp_random.h" // for uint32_t esp_random()
  14. #define UART_NUM1 (UART_NUM_1)
  15. #define UART_BAUD_RATE (115200 * 10)
  16. #define BUF_SIZE (512)
  17. #define UART1_RX_PIN (22)
  18. #define UART1_TX_PIN (23)
  19. // RTS for RS485 Half-Duplex Mode manages DE/~RE
  20. #define UART1_RTS_PIN (18)
  21. // Number of packets to be send during test
  22. #define PACKETS_NUMBER (30)
  23. // Wait timeout for uart driver
  24. #define PACKET_READ_TICS (2000 / portTICK_PERIOD_MS)
  25. // This is for workarond to avoid master-slave syncronization issues
  26. // when slave gets the "Master_started" signal with delay ~2-3 seconds
  27. #define TEST_ALLOW_PROC_FAIL (10)
  28. #define TEST_CHECK_PROC_FAIL(fails, threshold) TEST_ASSERT((fails * 100 / PACKETS_NUMBER) <= threshold)
  29. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C3, ESP32C2, ESP32H2)
  30. //No runners
  31. static const char *TAG = "rs485_test";
  32. // The table for fast CRC16 calculation
  33. static const uint8_t crc_hi[] = {
  34. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  35. 0x00, 0xC1, 0x81,
  36. 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
  37. 0x40, 0x01, 0xC0,
  38. 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1,
  39. 0x81, 0x40, 0x01,
  40. 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
  41. 0xC0, 0x80, 0x41,
  42. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  43. 0x00, 0xC1, 0x81,
  44. 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80,
  45. 0x41, 0x01, 0xC0,
  46. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  47. 0x80, 0x41, 0x01,
  48. 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00,
  49. 0xC1, 0x81, 0x40,
  50. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  51. 0x00, 0xC1, 0x81,
  52. 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
  53. 0x40, 0x01, 0xC0,
  54. 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1,
  55. 0x81, 0x40, 0x01,
  56. 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01,
  57. 0xC0, 0x80, 0x41,
  58. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  59. 0x00, 0xC1, 0x81,
  60. 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
  61. 0x40, 0x01, 0xC0,
  62. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  63. 0x80, 0x41, 0x01,
  64. 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
  65. 0xC0, 0x80, 0x41,
  66. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  67. 0x00, 0xC1, 0x81,
  68. 0x40
  69. };
  70. static const uint8_t crc_low[] = {
  71. 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
  72. 0x05, 0xC5, 0xC4,
  73. 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB,
  74. 0x0B, 0xC9, 0x09,
  75. 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE,
  76. 0xDF, 0x1F, 0xDD,
  77. 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2,
  78. 0x12, 0x13, 0xD3,
  79. 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
  80. 0x36, 0xF6, 0xF7,
  81. 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E,
  82. 0xFE, 0xFA, 0x3A,
  83. 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B,
  84. 0x2A, 0xEA, 0xEE,
  85. 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27,
  86. 0xE7, 0xE6, 0x26,
  87. 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
  88. 0x63, 0xA3, 0xA2,
  89. 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD,
  90. 0x6D, 0xAF, 0x6F,
  91. 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8,
  92. 0xB9, 0x79, 0xBB,
  93. 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4,
  94. 0x74, 0x75, 0xB5,
  95. 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
  96. 0x50, 0x90, 0x91,
  97. 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94,
  98. 0x54, 0x9C, 0x5C,
  99. 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59,
  100. 0x58, 0x98, 0x88,
  101. 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D,
  102. 0x4D, 0x4C, 0x8C,
  103. 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
  104. 0x41, 0x81, 0x80,
  105. 0x40
  106. };
  107. // Calculate buffer checksum using tables
  108. // The checksum CRC16 algorithm is specific
  109. // for Modbus standard and uses polynomial value = 0xA001
  110. static uint16_t get_buffer_crc16( uint8_t * frame_ptr, uint16_t length )
  111. {
  112. TEST_ASSERT( frame_ptr != NULL);
  113. uint8_t crc_hi_byte = 0xFF;
  114. uint8_t crc_low_byte = 0xFF;
  115. int index;
  116. while ( length-- )
  117. {
  118. index = crc_low_byte ^ *(frame_ptr++);
  119. crc_low_byte = crc_hi_byte ^ crc_hi[index];
  120. crc_hi_byte = crc_low[index];
  121. }
  122. return ((crc_hi_byte << 8) | crc_low_byte);
  123. }
  124. // Fill the buffer with random numbers and apply CRC16 at the end
  125. static uint16_t buffer_fill_random(uint8_t *buffer, size_t length)
  126. {
  127. TEST_ASSERT( buffer != NULL);
  128. // Packet is too short
  129. if (length < 4) {
  130. return 0;
  131. }
  132. for (int i = 0; i < length; i += 4) {
  133. uint32_t random = esp_random();
  134. memcpy(buffer + i, &random, MIN(length - i, 4));
  135. }
  136. // Get checksum of the buffer
  137. uint16_t crc = get_buffer_crc16((uint8_t*)buffer, (length - 2));
  138. // Apply checksum bytes into packet
  139. buffer[length - 2] = (uint8_t)(crc & 0xFF); // Set Low byte CRC
  140. buffer[length - 1] = (uint8_t)(crc >> 8); // Set High byte CRC
  141. return crc;
  142. }
  143. static void rs485_init(void)
  144. {
  145. uart_config_t uart_config = {
  146. .baud_rate = UART_BAUD_RATE,
  147. .data_bits = UART_DATA_8_BITS,
  148. .parity = UART_PARITY_DISABLE,
  149. .stop_bits = UART_STOP_BITS_1,
  150. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  151. .rx_flow_ctrl_thresh = 120,
  152. .source_clk = UART_SCLK_DEFAULT,
  153. };
  154. ESP_LOGI(TAG, "RS485 port initialization...");
  155. TEST_ESP_OK(uart_wait_tx_idle_polling(UART_NUM1));
  156. // Configure UART1 parameters
  157. TEST_ESP_OK(uart_param_config(UART_NUM1, &uart_config));
  158. // Set UART1 pins
  159. TEST_ESP_OK(uart_set_pin(UART_NUM1, UART1_TX_PIN, UART1_RX_PIN, UART1_RTS_PIN, UART_PIN_NO_CHANGE));
  160. // Install UART driver (we don't need an event queue here)
  161. TEST_ESP_OK(uart_driver_install(UART_NUM1, BUF_SIZE * 2, 0, 0, NULL, 0));
  162. // Setup rs485 half duplex mode
  163. TEST_ESP_OK(uart_set_mode(UART_NUM1, UART_MODE_RS485_HALF_DUPLEX));
  164. }
  165. static esp_err_t print_packet_data(const char *str, uint8_t *buffer, uint16_t buffer_size)
  166. {
  167. TEST_ASSERT( buffer != NULL);
  168. TEST_ASSERT( str != NULL);
  169. // Calculate the checksum of the buffer
  170. uint16_t crc16_calc = get_buffer_crc16(buffer, (buffer_size - 2));
  171. uint16_t crc16_in = ((uint16_t)(buffer[buffer_size - 1]) << 8) | buffer[buffer_size - 2];
  172. const char* state_str = (crc16_in != crc16_calc) ? "incorrect " : "correct ";
  173. // Print an array of data
  174. printf("%s%s RS485 packet = [ ", str, state_str);
  175. for (int i = 0; i < buffer_size; i++) {
  176. printf("0x%.2X ", (uint8_t)buffer[i]);
  177. }
  178. printf(" ]\r\n");
  179. printf("crc_in = 0x%.4X\r\n", (uint16_t)crc16_in);
  180. printf("crc_calc = 0x%.4X\r\n", (uint16_t)crc16_calc);
  181. esp_err_t result = (crc16_in != crc16_calc) ? ESP_ERR_INVALID_CRC : ESP_OK;
  182. return result;
  183. }
  184. // Slave test case for multi device
  185. static void rs485_slave(void)
  186. {
  187. rs485_init();
  188. uint8_t* slave_data = (uint8_t*) malloc(BUF_SIZE);
  189. uint16_t err_count = 0, good_count = 0;
  190. unity_send_signal("Slave_ready");
  191. unity_wait_for_signal("Master_started");
  192. ESP_LOGI(TAG, "Start recieve loop.");
  193. for(int pack_count = 0; pack_count < PACKETS_NUMBER; pack_count++) {
  194. //Read slave_data from UART
  195. int len = uart_read_bytes(UART_NUM1, slave_data, BUF_SIZE, PACKET_READ_TICS);
  196. //Write slave_data back to UART
  197. if (len > 2) {
  198. esp_err_t status = print_packet_data("Received ", slave_data, len);
  199. // If received packet is correct then send it back
  200. if (status == ESP_OK) {
  201. uart_write_bytes(UART_NUM1, (char*)slave_data, len);
  202. uart_wait_tx_idle_polling(UART_NUM1);
  203. good_count++;
  204. } else {
  205. printf("Incorrect packet received.\r\n");
  206. err_count++;
  207. }
  208. } else {
  209. printf("Incorrect data packet[%d], data length: %d, received.\r\n", pack_count, len);
  210. err_count++;
  211. }
  212. }
  213. ESP_LOGI(TAG, "Test completed. Received packets = %d, errors = %d", good_count, err_count);
  214. // Wait for packet to be sent
  215. uart_wait_tx_done(UART_NUM1, PACKET_READ_TICS);
  216. free(slave_data);
  217. uart_driver_delete(UART_NUM1);
  218. TEST_CHECK_PROC_FAIL(err_count, TEST_ALLOW_PROC_FAIL);
  219. }
  220. // Master test of multi device test case.
  221. // It forms packet with random data, apply generated CRC16 and sends to slave.
  222. // If response recieved correctly from slave means RS485 channel works.
  223. static void rs485_master(void)
  224. {
  225. uint16_t err_count = 0, good_count = 0;
  226. rs485_init();
  227. uint8_t* master_buffer = (uint8_t*) malloc(BUF_SIZE);
  228. uint8_t* slave_buffer = (uint8_t*) malloc(BUF_SIZE);
  229. // The master test case should be synchronized with slave
  230. unity_wait_for_signal("Slave_ready");
  231. unity_send_signal("Master_started");
  232. ESP_LOGI(TAG, "Start recieve loop.");
  233. for(int i = 0; i < PACKETS_NUMBER; i++) {
  234. // Form random buffer with CRC16
  235. buffer_fill_random(master_buffer, BUF_SIZE);
  236. // Print created packet for debugging
  237. esp_err_t status = print_packet_data("Send ", master_buffer, BUF_SIZE);
  238. TEST_ASSERT(status == ESP_OK);
  239. uart_write_bytes(UART_NUM1, (char*)master_buffer, BUF_SIZE);
  240. uart_wait_tx_idle_polling(UART_NUM1);
  241. // Read translated packet from slave
  242. int len = uart_read_bytes(UART_NUM1, slave_buffer, BUF_SIZE, PACKET_READ_TICS);
  243. // Check if the received packet is too short
  244. if (len > 2) {
  245. // Print received packet and check checksum
  246. esp_err_t status = print_packet_data("Received ", slave_buffer, len);
  247. if (status == ESP_OK) {
  248. good_count++;
  249. printf("Received: %d\r\n", good_count);
  250. } else {
  251. err_count++;
  252. printf("Errors: %d\r\n", err_count);
  253. }
  254. }
  255. else {
  256. printf("Incorrect answer from slave, length = %d.\r\n", len);
  257. err_count++;
  258. }
  259. }
  260. uart_wait_tx_done(UART_NUM1, PACKET_READ_TICS);
  261. // Free the buffer and delete driver at the end
  262. free(master_buffer);
  263. free(slave_buffer);
  264. uart_driver_delete(UART_NUM1);
  265. ESP_LOGI(TAG, "Test completed. Received packets = %d, errors = %d", good_count, err_count);
  266. TEST_CHECK_PROC_FAIL(err_count, TEST_ALLOW_PROC_FAIL);
  267. }
  268. /*
  269. * This multi devices test case verifies RS485 mode of the uart driver and checks
  270. * correctness of RS485 interface channel communication. It requires
  271. * RS485 bus driver hardware to be connected to boards.
  272. */
  273. TEST_CASE_MULTIPLE_DEVICES("RS485 half duplex uart multiple devices test.", "[driver_RS485][test_env=UT_T2_RS485]", rs485_master, rs485_slave);
  274. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(..)