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_v42' into 'release/v4.2'
driver: uart rs485 fix test sync issues and fail threshold (backport v4.2) See merge request espressif/esp-idf!19615
This commit is contained in:
commit
949d7da779
@ -9,29 +9,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 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) )
|
||||
#define UART_NUM1 (UART_NUM_1)
|
||||
#define UART_BAUD_RATE (115200 * 10)
|
||||
#define BUF_SIZE (512)
|
||||
#define UART1_RX_PIN (22)
|
||||
#define UART1_TX_PIN (23)
|
||||
|
||||
// RTS for RS485 Half-Duplex Mode manages DE/~RE
|
||||
#define UART1_RTS_PIN (18)
|
||||
#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)
|
||||
//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,
|
||||
@ -153,23 +154,23 @@ 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,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.rx_flow_ctrl_thresh = 122,
|
||||
};
|
||||
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
|
||||
uart_param_config(UART_NUM1, &uart_config);
|
||||
// Set UART1 pins(TX: IO4, RX: I05, RTS: IO18, CTS: IO19)
|
||||
uart_set_pin(UART_NUM1, UART1_TX_PIN, UART1_RX_PIN, UART1_RTS_PIN, UART_PIN_NO_CHANGE);
|
||||
TEST_ESP_OK(uart_param_config(UART_NUM1, &uart_config));
|
||||
// Set UART1 pins
|
||||
TEST_ESP_OK(uart_set_pin(UART_NUM1, UART1_TX_PIN, UART1_RX_PIN, UART1_RTS_PIN, UART_PIN_NO_CHANGE));
|
||||
// Install UART driver (we don't need an event queue here)
|
||||
uart_driver_install(UART_NUM1, BUF_SIZE * 2, 0, 0, NULL, 0);
|
||||
TEST_ESP_OK(uart_driver_install(UART_NUM1, BUF_SIZE * 2, 0, 0, NULL, 0));
|
||||
// Setup rs485 half duplex mode
|
||||
//uart_set_rs485_hd_mode(uart_num, true);
|
||||
uart_set_mode(UART_NUM1, UART_MODE_RS485_HALF_DUPLEX);
|
||||
TEST_ESP_OK(uart_set_mode(UART_NUM1, UART_MODE_RS485_HALF_DUPLEX));
|
||||
}
|
||||
|
||||
static esp_err_t print_packet_data(const char *str, uint8_t *buffer, uint16_t buffer_size)
|
||||
@ -199,12 +200,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);
|
||||
@ -212,22 +213,23 @@ static void rs485_slave(void)
|
||||
// If received packet is correct then send it back
|
||||
if (status == ESP_OK) {
|
||||
uart_write_bytes(UART_NUM1, (char*)slave_data, len);
|
||||
uart_wait_tx_idle_polling(UART_NUM1);
|
||||
good_count++;
|
||||
} else {
|
||||
printf("Incorrect packet received.\r\n");
|
||||
err_count++;
|
||||
}
|
||||
} else {
|
||||
printf("Incorrect data packet[%d] received.\r\n", pack_count);
|
||||
printf("Incorrect data packet[%d], data length: %d, received.\r\n", pack_count, len);
|
||||
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.
|
||||
@ -242,7 +244,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);
|
||||
@ -250,8 +252,9 @@ static void rs485_master(void)
|
||||
esp_err_t status = print_packet_data("Send ", master_buffer, BUF_SIZE);
|
||||
TEST_ASSERT(status == ESP_OK);
|
||||
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
|
||||
@ -269,11 +272,13 @@ static void rs485_master(void)
|
||||
err_count++;
|
||||
}
|
||||
}
|
||||
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