problem using uart read byte on UART0

voomdisc2
Posts: 1
Joined: Mon Dec 11, 2017 11:35 am

problem using uart read byte on UART0

Postby voomdisc2 » Mon Dec 11, 2017 11:44 am

hello everyone
using esp32 on Adafruit HUZZAH32 board.
I want to implement UART0 readbyte using the esp-idf toolchain, and manipulating the example on
[uart rx tx async][https://github.com/espressif/esp-idf/tr ... _rxtxtasks],
to work with UART0 (the UART on the USB bridge).


I get the UART Tx messages, but could NOT detect incoming bytes on the esp32.

any idea what could be the problem?

I will appreciate your help :D


adding here my modified code:

Code: Select all

[/* UART asynchronous example, that uses separate RX and TX tasks

 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 "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "soc/uart_struct.h"
#include "string.h"

static const int RX_BUF_SIZE = 1024;

#define TXD_PIN (GPIO_NUM_4) // GPIO_NUM_4
#define RXD_PIN (GPIO_NUM_5) // GPIO_NUM_5
#define UART_CH (UART_NUM_0)
#define UART_TX_PERIOS_MS (5000)

void init() {
    const uart_config_t uart_config = {
        .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_param_config(UART_CH, &uart_config);
    uart_set_pin(UART_CH, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    // We won't use a buffer for sending data.
    uart_driver_install(UART_CH, RX_BUF_SIZE * 2, 0, 0, NULL, 0);

    ESP_LOGI("MY_DEBUG:", "Init Uart %d", UART_CH);
}

int sendData(const char* logName, const char* data)
{
    const int len = strlen(data);
    const int txBytes = uart_write_bytes(UART_CH, data, len);
    ESP_LOGI(logName, "Wrote voom %d bytes: %s", txBytes, data);
    return txBytes;
}

static void tx_task()
{
    static const char *TX_TASK_TAG = "TX_TASK";
    esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO);
    while (1) {
        sendData(TX_TASK_TAG, "Hello world");
        vTaskDelay(UART_TX_PERIOS_MS / portTICK_PERIOD_MS);
    }
}

static void rx_task()
{
    static const char *RX_TASK_TAG = "RX_TASK";
    esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
    uint8_t* data = (uint8_t*) malloc(RX_BUF_SIZE+1);
    ESP_LOGI("MY_DEBUG:", "reading Uart %d", UART_CH);
    while (1) {
        const int rxBytes = uart_read_bytes(UART_CH, data, RX_BUF_SIZE, 1000 / portTICK_RATE_MS);
        if (rxBytes > 0) {
            data[rxBytes] = 0;
            ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data);
            ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO);
        }
    }
    free(data);
}

void app_main()
{
    init();
    xTaskCreate(rx_task, "uart_rx_task", 1024*2, NULL, configMAX_PRIORITIES, NULL);
    xTaskCreate(tx_task, "uart_tx_task", 1024*2, NULL, configMAX_PRIORITIES-1, NULL);
}]

Who is online

Users browsing this forum: No registered users and 57 guests