UART 1 and 2 communication

Palonso
Posts: 95
Joined: Tue Sep 24, 2019 8:43 pm

UART 1 and 2 communication

Postby Palonso » Tue Oct 01, 2019 10:35 pm

Hi, I'm trying to set two UART port on my ESP32-WROOM-32D, so I can use it as a bridge to communicate with another device.

The thing is that I modified the UART_echo example code from Github and added the new port. when I'm sending data I recieve it correctly on the other side (the device side) but when the device answers I only read part of the data.

Here is the code I use:

Code: Select all

/* UART Echo Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"

/**
 * This is an example which echos any data it receives on UART1 back to the sender,
 * with hardware flow control turned off. It does not use UART driver event queue.
 *
 * - Port: UART1
 * - Receive (Rx) buffer: on
 * - Transmit (Tx) buffer: off
 * - Flow control: off
 * - Event queue: off
 * - Pin assignment: see defines below
 */

//Definitions UART to PC
#define UART_NUM_PC UART_NUM_1
#define ECHO_TEST_TXD  (GPIO_NUM_1)
#define ECHO_TEST_RXD  (GPIO_NUM_3)
#define ECHO_TEST_RTS  (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS  (UART_PIN_NO_CHANGE)

//Definitions UART to Device
#define UART_NUM_DEV UART_NUM_2
#define UART2_TEST_TXD  (GPIO_NUM_26)
#define UART2_TEST_RXD  (GPIO_NUM_25)

#define BUF_SIZE (5120)


static const char *TAG = "uart_echo_example_main.c";

static void echo_task(void *arg)
{
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    uart_config_t uart_config_pc = {
        .baud_rate = 57600,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
   uart_config_t uart_config_dev = {
       .baud_rate = 9600,
       .data_bits = UART_DATA_8_BITS,
       .parity    = UART_PARITY_DISABLE,
       .stop_bits = UART_STOP_BITS_1,
       .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
   };
    //UART PC
    uart_param_config(UART_NUM_PC, &uart_config_pc);
    uart_set_pin(UART_NUM_PC, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
    uart_driver_install(UART_NUM_PC, BUF_SIZE * 2, 0, 0, NULL, 0);
    //UART DEV
    uart_param_config(UART_NUM_DEV, &uart_config_dev);
    uart_set_pin(UART_NUM_DEV, UART2_TEST_TXD, UART2_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
    uart_driver_install(UART_NUM_DEV, BUF_SIZE * 2, 0, 0, NULL, 0);

    // Configure a temporary buffer for the incoming data
    uint8_t *data_pc = (uint8_t *) malloc(BUF_SIZE);
    uint8_t *data_dev = (uint8_t *) malloc(BUF_SIZE);

    while (1)
    {
        // Read data from the UART DEV
        int len_dev = uart_read_bytes(UART_NUM_DEV, data_dev, BUF_SIZE, 1000 / portTICK_RATE_MS);
        // Read data from the UART PC, more time more data that acumulates before sending
        int len_pc = uart_read_bytes(UART_NUM_PC, data_pc, BUF_SIZE, 1000 / portTICK_RATE_MS);

        // Write data back to the UART
        if ( uart_write_bytes(UART_NUM_DEV, (const char *) data_pc, len_pc) >0)
        {
          uart_write_bytes(UART_NUM_DEV, "\r\n" , 2);
          uart_write_bytes(UART_NUM_PC, "\r\n" , 2);
          uart_write_bytes(UART_NUM_PC, "send to DEV", 10);
          uart_write_bytes(UART_NUM_PC, "\r\n" , 2);
          uart_flush(UART_NUM_PC);
        }

        // Write data back to the UART
        if ( uart_write_bytes(UART_NUM_PC, (const char *) data_dev, len_dev) >0)
        {
          uart_write_bytes(UART_NUM_PC, "from dev" , 8);
          uart_write_bytes(UART_NUM_PC, "\r\n" , 2);
          uart_flush(UART_NUM_DEV);
        }
    }
}

void app_main(void)
{
  /*static BaseType_t xTaskCreate(TaskFunction_t pvTaskCode, const char *const pcName, const uint32_t usStackDepth,
   *                            void *const pvParameters, UBaseType_t uxPriority, TaskHandle_t *const pvCreatedTask)
   */
    xTaskCreate(echo_task, "uart_echo_task", 5120, NULL, 10, NULL);
}
This is what i get on the ESP side:

Code: Select all

send to DEV		//here I send data that was recieved correctly on the other side
1236from dev 		//here I was supposed to recieve 123456
1236from dev 		//here I was supposed to recieve 123456
1236from dev 		//here I was supposed to recieve 123456
6541from dev 		//here I was supposed to recieve 654321
1233450from dev 	//here I was supposed to recieve 12345678901234567890
1from dev 		//here I was supposed to recieve 1
12from dev 		//here I was supposed to recieve 12
123from dev 		//here I was supposed to recieve 123
1234from dev 		//here I was supposed to recieve 1234
1235from dev 		//here I was supposed to recieve 12345
1111from dev 		//here I was supposed to recieve 11111
1110from dev 		//here I was supposed to recieve 11110
I suspect that there might be a problem with one of the following functions:
  • uart_write_bytes(UART_NUM_PC, (const char *) data_dev, len_dev)
  • int len_dev = uart_read_bytes(UART_NUM_DEV, data_dev, BUF_SIZE, 1000 / portTICK_RATE_MS);
but have no idea how to check them.

I want to know if anyone has an idea of what can be happening, Thanks.

Palonso
Posts: 95
Joined: Tue Sep 24, 2019 8:43 pm

Re: UART 1 and 2 communication

Postby Palonso » Tue Oct 08, 2019 3:46 pm

UPDATE: Suddenly started working, i don't know if i changed something but i'm leaving my code just in case someone notice something (or maybe you want to use it)

Code: Select all

/* UART Echo Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"

/**
 * This is an example which echos any data it receives on UART1 back to the sender,
 * with hardware flow control turned off. It does not use UART driver event queue.
 *
 * - Port: UART1
 * - Receive (Rx) buffer: on
 * - Transmit (Tx) buffer: off
 * - Flow control: off
 * - Event queue: off
 * - Pin assignment: see defines below
 */

//Definiciones UART a PC
#define UART_NUM_PC UART_NUM_1
#define ECHO_TEST_TXD  (GPIO_NUM_1)
#define ECHO_TEST_RXD  (GPIO_NUM_3)
#define ECHO_TEST_RTS  (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS  (UART_PIN_NO_CHANGE)

//Definiciones UART a Device
#define UART_NUM_DEV UART_NUM_2
#define UART2_TEST_TXD  (GPIO_NUM_26)
#define UART2_TEST_RXD  (GPIO_NUM_25)

#define BUF_SIZE (1024)


static const char *TAG = "uart_echo_example_main.c";

static void echo_task(void *arg)
{
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    uart_config_t uart_config_pc = {
        .baud_rate = 57600,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
   uart_config_t uart_config_dev = {
       .baud_rate = 115200,
       .data_bits = UART_DATA_8_BITS,
       .parity    = UART_PARITY_DISABLE,
       .stop_bits = UART_STOP_BITS_1,
       .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
   };
    //UART PC
    uart_param_config(UART_NUM_PC, &uart_config_pc);
    uart_set_pin(UART_NUM_PC, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
    uart_driver_install(UART_NUM_PC, BUF_SIZE * 2, 0, 0, NULL, 0);
    //UART DEV
    uart_param_config(UART_NUM_DEV, &uart_config_dev);
    uart_set_pin(UART_NUM_DEV, UART2_TEST_TXD, UART2_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
    uart_driver_install(UART_NUM_DEV, BUF_SIZE * 2, 0, 0, NULL, 0);

    // Configure a temporary buffer for the incoming data
    uint8_t *data_pc = (uint8_t *) malloc(BUF_SIZE);
    uint8_t *data_dev = (uint8_t *) malloc(BUF_SIZE);

    while (1)
    {
        // Read data from the UART DEV
        int len_dev = uart_read_bytes(UART_NUM_DEV, data_dev, BUF_SIZE, 20 / portTICK_RATE_MS);
        // Read data from the UART PC, more time more data that acumulates before sending
        int len_pc = uart_read_bytes(UART_NUM_PC, data_pc, BUF_SIZE, 20 / portTICK_RATE_MS);

        // Write data back to the UART
        if ( uart_write_bytes(UART_NUM_DEV, (const char *) data_pc, len_pc) >0)
        {
          uart_write_bytes(UART_NUM_DEV, "\r\n" , 2);
          uart_write_bytes(UART_NUM_PC, "\r\n" , 2);
          //uart_write_bytes(UART_NUM_PC, "send to DEV", 11);
          //uart_write_bytes(UART_NUM_PC, "\r\n" , 2);
          uart_flush(UART_NUM_PC);
        }

        // Write data back to the UART
        if ( uart_write_bytes(UART_NUM_PC, (const char *) data_dev, len_dev) >0)
        {
          //uart_write_bytes(UART_NUM_PC, " from dev" , 9);
          uart_write_bytes(UART_NUM_PC, "\r\n" , 2);
          uart_flush(UART_NUM_DEV);
        }
    }
}

void app_main(void)
{
  /*static BaseType_t xTaskCreate(TaskFunction_t pvTaskCode, const char *const pcName, const uint32_t usStackDepth,
   *                            void *const pvParameters, UBaseType_t uxPriority, TaskHandle_t *const pvCreatedTask)
   */
    xTaskCreate(echo_task, "uart_echo_task", 5120, NULL, 10, NULL);
}

amalhameed007
Posts: 1
Joined: Thu Oct 17, 2019 12:45 am

Re: UART 1 and 2 communication

Postby amalhameed007 » Thu Oct 17, 2019 12:50 am

For using UART0 we have to mention , UART_NUM_0 right ?and GPIO 1 , GPIO3

but here , you used GPIO1 , GPIO3 and mention UART_NUM_1 !!

did you get the proper output ? I am trying to establish UART between STM32 & ESP32-WROOM-32D .......

which IDE u r using ? for developing ESP32

Palonso
Posts: 95
Joined: Tue Sep 24, 2019 8:43 pm

Re: UART 1 and 2 communication

Postby Palonso » Wed Oct 23, 2019 3:56 pm

amalhameed007 wrote:which IDE u r using ? for developing ESP32
I'm using espressif IDE: ESP-IDF v4.1-dev-371-gebd2b3f3e-dirty
amalhameed007 wrote: For using UART0 we have to mention , UART_NUM_0 right ?and GPIO 1 , GPIO3

but here , you used GPIO1 , GPIO3 and mention UART_NUM_1 !!

did you get the proper output ? I am trying to establish UART between STM32 & ESP32-WROOM-32D .......
Being honest, you are right. GPIO 1 and 3 are used for UART0 but the Hardware Serial pins can be re-mapped to (i guess almost) any pin available on the board, so basically what i did was "changing the name" of the port.

On the other hand, when i named it UART0 the ESP32 started working weird, a thing that does not happen when i use UART1 and UART2.

Sorry for the short explanation, but thats sort of how far i understood about it.

Who is online

Users browsing this forum: No registered users and 97 guests