Hi All,
Trying to use UART2 on ESP32 as an additional serial port - no flow control. The example code in esp-idf: uart_events work fine for UART2. However, on using fgetc and other file operations on /dev/uart/2 the results are not consistent. Are there any additional settings or config to enable file operations on UART2?
Thanks
USB Serial on UART2
Re: USB Serial on UART2
standard input on UARTs currently behaves as O_NONBLOCK; i.e. fgetc will return immediately, even if there is no data in UART FIFO. Does that explain the behaviour you are seeing? If not, could you describe "inconsistent" in more detail?
UART 1 is not working ( i am using ESP IDF)
#define ECHO_TEST_TXD (4)
#define ECHO_TEST_RXD (5)
#define ECHO_TEST_RTS (18)
#define ECHO_TEST_CTS (19)
#define BUF_SIZE (1024)
//an example of echo test with hardware flow control on UART1
static void echo_task()
{
const int uart_num = UART_NUM_1;
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_CTS_RTS,
.rx_flow_ctrl_thresh = 122,
};
//Configure UART1 parameters
uart_param_config(uart_num, &uart_config);
//Set UART1 pins(TX: IO4, RX: I05, RTS: IO18, CTS: IO19)
uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
//Install UART driver (we don't need an event queue here)
//In this example we don't even use a buffer for sending data.
uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
while(1) {
//Read data from UART
int len = uart_read_bytes(uart_num, data, BUF_SIZE, 20 / portTICK_RATE_MS);
//Write data back to UART
int u=0xfa;
//uart_write_bytes(uart_num, (const char*) data, len);
uart_write_bytes(uart_num,u,1);
}
}
void app_main()
{
//A uart read/write example without event queue;
echo_task();
xTaskCreate(echo_task, "uart_echo_task", 1024, NULL, 10, NULL);
}
#define ECHO_TEST_RXD (5)
#define ECHO_TEST_RTS (18)
#define ECHO_TEST_CTS (19)
#define BUF_SIZE (1024)
//an example of echo test with hardware flow control on UART1
static void echo_task()
{
const int uart_num = UART_NUM_1;
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_CTS_RTS,
.rx_flow_ctrl_thresh = 122,
};
//Configure UART1 parameters
uart_param_config(uart_num, &uart_config);
//Set UART1 pins(TX: IO4, RX: I05, RTS: IO18, CTS: IO19)
uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
//Install UART driver (we don't need an event queue here)
//In this example we don't even use a buffer for sending data.
uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
while(1) {
//Read data from UART
int len = uart_read_bytes(uart_num, data, BUF_SIZE, 20 / portTICK_RATE_MS);
//Write data back to UART
int u=0xfa;
//uart_write_bytes(uart_num, (const char*) data, len);
uart_write_bytes(uart_num,u,1);
}
}
void app_main()
{
//A uart read/write example without event queue;
echo_task();
xTaskCreate(echo_task, "uart_echo_task", 1024, NULL, 10, NULL);
}
Re: USB Serial on UART2
Hi,
Yes, the code which considers the non-blocking behaviour. By inconsistent, I mean that fgetc (stdin) works fine, whereas a fgetc from a file opened on UART device '/dev/uart/0 or '/dev/uart/2' does not give the same result. Some characters are dropped or ignored. Seems like I'm missing some config options for UART. UART init sequence is as follows:
uart_config_t uartConfig =
{
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
};
uart_param_config (uartNum, &uartConfig);
//Set UART2 pins(TX: IO17, RX: I016, RTS: Not Used, CTS: noy Used)
if (uartNum == UART_NUM_2)
{
tx_pin = GPIO_NUM_17;
rx_pin = GPIO_NUM_16;
}
else // default to UART 0 pins(TX: IO3, RX: I01)
{
tx_pin = GPIO_NUM_1;
rx_pin = GPIO_NUM_3;
}
uart_set_pin (uartNum, tx_pin, rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
//Install UART driver (we don't need an event queue here)
if (uart_driver_install (uartNum, 1024 * 2, 1024 * 2, 0, NULL, 0) != ESP_OK)
{
ESP_LOGE (__FILE__, "Error installing UART driver %d", __LINE__);
return ESP_FAIL;
}
Yes, the code which considers the non-blocking behaviour. By inconsistent, I mean that fgetc (stdin) works fine, whereas a fgetc from a file opened on UART device '/dev/uart/0 or '/dev/uart/2' does not give the same result. Some characters are dropped or ignored. Seems like I'm missing some config options for UART. UART init sequence is as follows:
uart_config_t uartConfig =
{
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
};
uart_param_config (uartNum, &uartConfig);
//Set UART2 pins(TX: IO17, RX: I016, RTS: Not Used, CTS: noy Used)
if (uartNum == UART_NUM_2)
{
tx_pin = GPIO_NUM_17;
rx_pin = GPIO_NUM_16;
}
else // default to UART 0 pins(TX: IO3, RX: I01)
{
tx_pin = GPIO_NUM_1;
rx_pin = GPIO_NUM_3;
}
uart_set_pin (uartNum, tx_pin, rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
//Install UART driver (we don't need an event queue here)
if (uart_driver_install (uartNum, 1024 * 2, 1024 * 2, 0, NULL, 0) != ESP_OK)
{
ESP_LOGE (__FILE__, "Error installing UART driver %d", __LINE__);
return ESP_FAIL;
}
Re: USB Serial on UART2
Currently the UART driver can not be used together with console IO on the same UART. Both will try to read from UART FIFO, and only one (either console input or UART driver) will get any given character.
Re: USB Serial on UART2
OK. I'm using UART0 as console and UART2 as additional serial. Serial out(fputs, frpintf) on UART2 files work well but input (using fgetc) from UART2 device file drops characters at times. Any pointers to what I might be missing?
Re: USB Serial on UART2
Sorry if didn't make my previous message clear... Currently you can not use UART driver ('uart_driver_install') and standard IO, specifically input (fscanf, scanf) on the same UART peripheral. UART driver will try to read characters from UART peripheral. Standard IO library will also try to read characters from the same FIFO. Only one of these two will get each character. So your calls to fscanf/scanf will not produce desired result if there's also a UART driver attached to the same UART peripheral.
Who is online
Users browsing this forum: No registered users and 102 guests