mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Merge branch 'bugfix/driver_rs485_fix_test_sync_issues_v44' into 'release/v4.4'
driver: uart rs485 fix test sync issues and fail threshold (backport v4.4) See merge request espressif/esp-idf!19610
This commit is contained in:
commit
e3c1fde6c4
@ -5,7 +5,6 @@
|
||||
*/
|
||||
/* This file is from test_uart.c, but mainly about RS485 */
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/param.h>
|
||||
#include "unity.h"
|
||||
@ -14,29 +13,30 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h" // for uint32_t esp_random()
|
||||
|
||||
#define UART_TAG "Uart"
|
||||
#define UART_NUM1 (UART_NUM_1)
|
||||
#define BUF_SIZE (100)
|
||||
#define UART_BAUD_RATE (115200 * 10)
|
||||
#define BUF_SIZE (512)
|
||||
#define UART1_RX_PIN (22)
|
||||
#define UART1_TX_PIN (23)
|
||||
#define UART_BAUD_11520 (11520)
|
||||
#define UART_BAUD_115200 (115200)
|
||||
#define TOLERANCE (0.02) //baud rate error tolerance 2%.
|
||||
|
||||
#define UART_TOLERANCE_CHECK(val, uper_limit, lower_limit) ( (val) <= (uper_limit) && (val) >= (lower_limit) )
|
||||
|
||||
// RTS for RS485 Half-Duplex Mode manages DE/~RE
|
||||
#define UART1_RTS_PIN (18)
|
||||
|
||||
// Number of packets to be send during test
|
||||
#define PACKETS_NUMBER (10)
|
||||
#define PACKETS_NUMBER (30)
|
||||
|
||||
// Wait timeout for uart driver
|
||||
#define PACKET_READ_TICS (1000 / portTICK_RATE_MS)
|
||||
#define PACKET_READ_TICS (2000 / portTICK_PERIOD_MS)
|
||||
// This is for workarond to avoid master-slave syncronization issues
|
||||
// when slave gets the "Master_started" signal with delay ~2-3 seconds
|
||||
#define TEST_ALLOW_PROC_FAIL (10)
|
||||
#define TEST_CHECK_PROC_FAIL(fails, threshold) TEST_ASSERT((fails * 100 / PACKETS_NUMBER) <= threshold)
|
||||
|
||||
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C3)
|
||||
//No runners
|
||||
|
||||
static const char *TAG = "rs485_test";
|
||||
|
||||
// The table for fast CRC16 calculation
|
||||
static const uint8_t crc_hi[] = {
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
@ -158,7 +158,7 @@ static uint16_t buffer_fill_random(uint8_t *buffer, size_t length)
|
||||
static void rs485_init(void)
|
||||
{
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = UART_BAUD_115200,
|
||||
.baud_rate = UART_BAUD_RATE,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
@ -166,7 +166,7 @@ static void rs485_init(void)
|
||||
.rx_flow_ctrl_thresh = 120,
|
||||
.source_clk = UART_SCLK_APB,
|
||||
};
|
||||
printf("RS485 port initialization...\r\n");
|
||||
ESP_LOGI(TAG, "RS485 port initialization...");
|
||||
TEST_ESP_OK(uart_wait_tx_idle_polling(UART_NUM1));
|
||||
// Configure UART1 parameters
|
||||
TEST_ESP_OK(uart_param_config(UART_NUM1, &uart_config));
|
||||
@ -205,12 +205,12 @@ static void rs485_slave(void)
|
||||
rs485_init();
|
||||
uint8_t* slave_data = (uint8_t*) malloc(BUF_SIZE);
|
||||
uint16_t err_count = 0, good_count = 0;
|
||||
printf("Start recieve loop.\r\n");
|
||||
unity_send_signal("Slave_ready");
|
||||
unity_wait_for_signal("Master_started");
|
||||
ESP_LOGI(TAG, "Start recieve loop.");
|
||||
for(int pack_count = 0; pack_count < PACKETS_NUMBER; pack_count++) {
|
||||
//Read slave_data from UART
|
||||
int len = uart_read_bytes(UART_NUM1, slave_data, BUF_SIZE, (PACKET_READ_TICS * 2));
|
||||
int len = uart_read_bytes(UART_NUM1, slave_data, BUF_SIZE, PACKET_READ_TICS);
|
||||
//Write slave_data back to UART
|
||||
if (len > 2) {
|
||||
esp_err_t status = print_packet_data("Received ", slave_data, len);
|
||||
@ -229,12 +229,12 @@ static void rs485_slave(void)
|
||||
err_count++;
|
||||
}
|
||||
}
|
||||
printf("Test completed. Received packets = %d, errors = %d\r\n", good_count, err_count);
|
||||
ESP_LOGI(TAG, "Test completed. Received packets = %d, errors = %d", good_count, err_count);
|
||||
// Wait for packet to be sent
|
||||
uart_wait_tx_done(UART_NUM1, PACKET_READ_TICS);
|
||||
free(slave_data);
|
||||
uart_driver_delete(UART_NUM1);
|
||||
TEST_ASSERT(err_count < 2);
|
||||
TEST_CHECK_PROC_FAIL(err_count, TEST_ALLOW_PROC_FAIL);
|
||||
}
|
||||
|
||||
// Master test of multi device test case.
|
||||
@ -249,7 +249,7 @@ static void rs485_master(void)
|
||||
// The master test case should be synchronized with slave
|
||||
unity_wait_for_signal("Slave_ready");
|
||||
unity_send_signal("Master_started");
|
||||
printf("Start recieve loop.\r\n");
|
||||
ESP_LOGI(TAG, "Start recieve loop.");
|
||||
for(int i = 0; i < PACKETS_NUMBER; i++) {
|
||||
// Form random buffer with CRC16
|
||||
buffer_fill_random(master_buffer, BUF_SIZE);
|
||||
@ -259,7 +259,7 @@ static void rs485_master(void)
|
||||
uart_write_bytes(UART_NUM1, (char*)master_buffer, BUF_SIZE);
|
||||
uart_wait_tx_idle_polling(UART_NUM1);
|
||||
// Read translated packet from slave
|
||||
int len = uart_read_bytes(UART_NUM1, slave_buffer, BUF_SIZE, (PACKET_READ_TICS * 2));
|
||||
int len = uart_read_bytes(UART_NUM1, slave_buffer, BUF_SIZE, PACKET_READ_TICS);
|
||||
// Check if the received packet is too short
|
||||
if (len > 2) {
|
||||
// Print received packet and check checksum
|
||||
@ -280,9 +280,10 @@ static void rs485_master(void)
|
||||
uart_wait_tx_done(UART_NUM1, PACKET_READ_TICS);
|
||||
// Free the buffer and delete driver at the end
|
||||
free(master_buffer);
|
||||
free(slave_buffer);
|
||||
uart_driver_delete(UART_NUM1);
|
||||
TEST_ASSERT(err_count <= 1);
|
||||
printf("Test completed. Received packets = %d, errors = %d\r\n", (uint16_t)good_count, (uint16_t)err_count);
|
||||
ESP_LOGI(TAG, "Test completed. Received packets = %d, errors = %d", good_count, err_count);
|
||||
TEST_CHECK_PROC_FAIL(err_count, TEST_ALLOW_PROC_FAIL);
|
||||
}
|
||||
|
||||
/*
|
||||
|
Loading…
Reference in New Issue
Block a user