can anybody share code of reading data from uart by using uart interrupt method

ningappa BS
Posts: 51
Joined: Sat Mar 17, 2018 4:49 am

can anybody share code of reading data from uart by using uart interrupt method

Postby ningappa BS » Mon May 14, 2018 1:28 pm

hi everyone,
i wanted to read the data from uart by using uart interrupt method but here am not getting either sample code or example code.Any help would be appreciated.


thanks :)

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: can anybody share code of reading data from uart by using uart interrupt method

Postby kolban » Mon May 14, 2018 2:16 pm

Can you describe what you've tried? If you have tried sample code, please provide links to that sample code. If something is not working, please describe the test/setup environment and any logs and/or description of the issue you are facing.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ningappa BS
Posts: 51
Joined: Sat Mar 17, 2018 4:49 am

Re: can anybody share code of reading data from uart by using uart interrupt method

Postby ningappa BS » Tue May 15, 2018 4:24 am

actually now am working on polling method of uart reading, this is a driver code but i wanted flow sequence or example code of uart interrupt method.

thanks

ningappa BS
Posts: 51
Joined: Sat Mar 17, 2018 4:49 am

Re: can anybody share code of reading data from uart by using uart interrupt method

Postby ningappa BS » Tue May 15, 2018 5:05 am

Dear sir,
Actually my requirement is to read uart whenever we send external data (for example my keyboard input ) and it should not read in remaining time.

ningappa BS
Posts: 51
Joined: Sat Mar 17, 2018 4:49 am

Re: can anybody share code of reading data from uart by using uart interrupt method

Postby ningappa BS » Tue May 15, 2018 6:35 am

i want to receive some data serially on my esp32 for that i want to enable uart receive interrupt.
Last edited by ningappa BS on Tue May 15, 2018 11:55 am, edited 2 times in total.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: can anybody share code of reading data from uart by using uart interrupt method

Postby ESP_Angus » Tue May 15, 2018 7:17 am

If you're happy to use the ESP-IDF uart driver, the IDF example code uses an ISR to receive data (uart_driver_install is the call which enables this, with the specified buffer size):
https://github.com/espressif/esp-idf/bl ... main.c#L46

If you want to code your ISR directly, this is something else.

ningappa BS
Posts: 51
Joined: Sat Mar 17, 2018 4:49 am

Re: can anybody share code of reading data from uart by using uart interrupt method

Postby ningappa BS » Tue May 15, 2018 12:47 pm

kolban wrote:Can you describe what you've tried? If you have tried sample code, please provide links to that sample code. If something is not working, please describe the test/setup environment and any logs and/or description of the issue you are facing.
here i do not want to read uart data continuously in a while loop instead it should read only when it get data. Like this, is there any method to read.

here am sharing my code



#define ECHO_TEST_TXD (GPIO_NUM_4)
#define ECHO_TEST_RXD (GPIO_NUM_5)
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)

#define BUF_SIZE (1024)

static void echo_task()
{
/* 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_1, &uart_config);
uart_set_pin(UART_NUM_1, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);

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

while (1) {
// Read data from the UART
int len = uart_read_bytes(UART_NUM_1, data, BUF_SIZE, 20 / portTICK_RATE_MS);
// Write data back to the UART
uart_write_bytes(UART_NUM_1, (const char *) data, len);
}
}

void app_main()
{
xTaskCreate(echo_task, "uart_echo_task", 1024, NULL, 10, NULL);
}

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: can anybody share code of reading data from uart by using uart interrupt method

Postby ESP_Angus » Wed May 16, 2018 2:40 am

ningappa BS wrote: here i do not want to read uart data continuously in a while loop instead it should read only when it get data. Like this, is there any method to read.
I think what you have written will do this, but the read from UART has a 20ms timeout so it's returning every 20ms regardless of whether there is data or not:

Code: Select all

int len = uart_read_bytes(UART_NUM_1, data, BUF_SIZE, 20 / portTICK_RATE_MS);
Change the 20 to a larger timeout (or replace the entire argument with portMAX_DELAY to block indefinitely) and the function will only return when there is data available on the UART.

ningappa BS
Posts: 51
Joined: Sat Mar 17, 2018 4:49 am

Re: can anybody share code of reading data from uart by using uart interrupt method

Postby ningappa BS » Wed May 16, 2018 4:33 am

i tried with larger timeout but it is reading every timeout regardless data available,and used portMAX_DELAY but here it is not returning from that function.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: can anybody share code of reading data from uart by using uart interrupt method

Postby WiFive » Wed May 16, 2018 4:40 am

If you do portmax_delay it should only return when there is data.

Who is online

Users browsing this forum: Bing [Bot] and 70 guests