Deal with UART in esp-idf

skxo66
Posts: 6
Joined: Wed Oct 18, 2017 6:26 am

Deal with UART in esp-idf

Postby skxo66 » Wed May 01, 2019 6:23 am

Hello,

I try to adapt arduino code in esp-idf. The code has to deal with an UART to fill a buffer in memory.
In arduino I have the following code to fill a buffer via UART and It works well.

Code: Select all

int readline(int readch, char *buffer, int len) {
    static int pos = 0;
    int rpos;

    if (readch > 0) {
        switch (readch) {
            //case '\r': // Ignore CR
            //    break;
            case '\n': // Return on new-line
                rpos = pos;
                pos = 0;  // Reset position index ready for next time
                return rpos;
            default:
                if (pos < len-1) {
                    buffer[pos++] = readch;
                    buffer[pos] = 0;
                }
        }
    }
    return 0;
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN,OUTPUT);
}
 
void loop() {
 char buf[80];
 if (readline(Serial.read(), buf, 80) > 0) {
        Serial.print("\nYou entered: >");
        Serial.print(buf);
        Serial.println("<");
}
}
AND, I try to adapt the same code using esp-idf with this code but I cannot see the characters I have entered. The system does not print on the output monitor :-(

Code: Select all

#include "driver/gpio.h"
#include "sdkconfig.h"
#include "driver/uart.h"
#define BUF_SIZE (1024)

void init_uart() {
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    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_NUM_0, &uart_config);
    //uart_set_pin(UART_NUM_0, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
    uart_set_pin(UART_NUM_0,UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE,UART_PIN_NO_CHANGE,UART_PIN_NO_CHANGE);
    uart_driver_install(UART_NUM_0, BUF_SIZE * 2, 0, 0, NULL, 0);
}

int readline(int readch, char *buffer, int len) {
    static int pos = 0;
    int rpos;

    if (readch > 0) {
        switch (readch) {
            //case '\r': // Ignore CR
            //    break;
            case '\n': // Return on new-line
                rpos = pos;
                pos = 0;  // Reset position index ready for next time
                return rpos;
            default:
                if (pos < len-1) {
                    buffer[pos++] = readch;
                    buffer[pos] = 0;
                }
        }
    }
    return 0;
}


void app_main()
{
    //init uart
    init_uart();

    uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
    while(1) {
        //Read data from UART
        int len = uart_read_bytes(UART_NUM_0, data, BUF_SIZE, 20 / portTICK_RATE_MS);
        //Write data back to UART
        //uart_write_bytes(UART_NUM_0, (const char*) data, len);
        if (readline((const char*) data, buf, 80) > 0) {
        
        printf("\nYou entered: >");
        printf(buf);
        printf("<"); 
}
}
}
Could you help me to deal with UART using esp-idf please? I use the functions presented into the example, but I cannot reuse my arduino code to fill a buffer in memory. What am I doing wrong here? thanks in advance.
Regards
sk
Last edited by skxo66 on Tue Jun 04, 2019 10:27 am, edited 1 time in total.

ESP_houwenxiang
Posts: 118
Joined: Tue Jun 26, 2018 3:09 am

Re: Deal with UART in esp-idf

Postby ESP_houwenxiang » Thu May 16, 2019 5:35 am

Code: Select all

void app_main()
{
    //init uart
    init_uart();

    uint8_t data = 0;
    while(1) {
        //Read data from UART
        int len = uart_read_bytes(UART_NUM_0, &data, 1, 20 / portTICK_RATE_MS);
        //Write data back to UART
        //uart_write_bytes(UART_NUM_0, (const char*) data, len);
        if (len > 0 && readline(data, buf, 80) > 0) {
        
        printf("\nYou entered: >");
        printf(buf);
        printf("<");
}
}
}
wookooho

Who is online

Users browsing this forum: Bing [Bot], cdollar, MicroController and 194 guests