driver: check UART physical communication on 1M baudrate
*/
/* This file is from test_uart.c, but mainly about RS485 */
-
#include <string.h>
#include <sys/param.h>
#include "unity.h"
#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 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 (40)
// Wait timeout for uart driver
#define PACKET_READ_TICS (1000 / portTICK_PERIOD_MS)
@@ -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,
@@ -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);
@@ -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
@@ -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);
