Skip to content

Commit

Permalink
Merge branch 'bugfix/uart_fails_1M_baud_rate' into 'master'
Browse files Browse the repository at this point in the history
driver: check UART physical communication on 1M baudrate

Closes IDFGH-7366

See merge request espressif/esp-idf!18211
  • Loading branch information
alisitsyn committed Aug 3, 2022
2 parents 38264f0 + 76a084c commit f352469
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions components/driver/test/test_rs485.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -14,22 +13,17 @@
#include "esp_log.h"
#include "esp_random.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 (40)

// Wait timeout for uart driver
#define PACKET_READ_TICS (1000 / portTICK_PERIOD_MS)
Expand Down Expand Up @@ -158,7 +152,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,
Expand Down Expand Up @@ -210,7 +204,7 @@ static void rs485_slave(void)
unity_wait_for_signal("Master_started");
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);
Expand Down Expand Up @@ -259,7 +253,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
Expand All @@ -280,6 +274,7 @@ 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);
Expand Down

0 comments on commit f352469

Please sign in to comment.