I have connected ESP32 to other controller(STM32)and I need to send/receive data by UART, something like command-answer (esp32 sends command and stm32 sends responce). Also there is TCP server on ESP32 side, but I thinks its not the issue here.
I'm sendting 10..50 bytes from esp32 and expecting to receive 150...170 bytes from stm32.
Problem:
I'm receiving only half of stm's packet, i.e:
instead
01 02 03 [...] 168 169 170
I have
77 78 79 [...] 168 169 170
I have devided rx and tx to different tasks. In RX task, I'm using uart events.
Part of code:
Code: Select all
#define BUF_SIZE (512)
static QueueHandle_t uart_queue;
void uart_init(void)
{
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(MY_UART, &uart_config);
uart_set_pin(MY_UART, USART_TXD, USART_RXD, USART_RTS, USART_CTS);
//uart_driver_install(RS485_UART, BUF_SIZE*2, BUF_SIZE, 0, NULL, 0);
uart_driver_install(MY_UART, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart_queue, 0);
}
// sending buffers to UART
void UART_send_data(DataBuffer_t buffToSend)
{
switch(buffToSend)
{
case E_DATA_1:
uart_write_bytes(RS485_UART, (char*)MyData.Buf_1, MyData.Buf_1_len);
break;
case E_DATA_2:
uart_write_bytes(RS485_UART, (char*)MyData.Buf_2, MyData.Buf_2_len);
break;
default:
break;
}
}
static void tx_task(void *pvParameters)
{
while(1)
{
//send data1 to UART
if(MyData.got_packet_1 == true)
{
UART_send_data(E_DATA_1);
MyData.got_packet_1 = false;
}
//send data2 to UART
if(MyData.got_packet_2 == true)
{
UART_send_data(E_DATA_2);
MyData.got_packet_2= false;
}
}
}
static void RS485_rx_task(void *pvParameters)
{
uart_event_t event;
int len = 0;
while (1)
{
if(xQueueReceive(uart_queue, (void * )&event, (portTickType)portMAX_DELAY))
{
ESP_LOGI(UART_TAG, "uart[%d] event:", MY_UART);
if(event.type == UART_DATA)
{
ESP_LOGI(UART_TAG, "(UART_DATA), event type: %d", event.type);
len = uart_read_bytes(MY_UART, MyData.UART_Buf, event.size, portMAX_DELAY);
}
else
{
ESP_LOGI(UART_TAG, "uart event type: %d", event.type);
}
}
if(len > 0)
{
MyData.UART_len = len;
MyData.UART_got_packet = true;
else if (MyData.TCP_conn == true) // send UART data to TCP
{
printf("FL_UART(len:%d):%s\n",MyData.UART_len, MyData.UART_Buf);
send(connect_socket, MyData.UART_Buf, MyData.UART_len, 0);
}
}
}
}
void app_main()
{
uart_init();
xTaskCreate(rx_task, "rx_task", 8*1024, NULL, 12, NULL);
xTaskCreate(tx_task, "tx_task", 8*1024, NULL, 5, NULL);
xTaskCreate(&tcp_server_task,"tcp_server_task", 4*1024, NULL, 5, NULL);
}
Please give me advice how to receive all data without loses.