are there any ready examples of uart command line? (send one command from uart-usb converter and esp32 sends responce with some data).
I have wrote some code, based on this example:
https://github.com/espressif/esp-idf/t ... art_events
But its hanging and need 25-35 ms to proceed command.
Is there any ways to achieve ~1ms response?
I don't see where problem is.
Here is my code.
app-main:
Code: Select all
void app_main()
{
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
esp_bt_controller_init(&bt_cfg);
esp_bt_controller_enable(ESP_BT_MODE_BTDM);
command_uart_init(); // UART add
gattc_client_test();
data_buf_init();
do{
if(xQueueReceive(command_uart_queue, (void * )&event, (portTickType)portMAX_DELAY))
{
ESP_LOGI(TAG, "uart event type: %d\n", event.type);
if(event.type == (UART_FIFO_OVF || UART_BUFFER_FULL))
{
uart_flush(COMMAND_UART);
}
uart_command_handler();
}
uart_data_handler();
} while(1);
}
Code: Select all
static QueueHandle_t command_uart_queue;
void command_uart_init(void)
{
const int uart_num = COMMAND_UART;
uart_config_t uart_config = {
.baud_rate = 9600,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 122,
};
uart_param_config(uart_num, &uart_config);
uart_set_pin(uart_num, CMD_PIN_TX, CMD_PIN_RX, CMD_PIN_RTS, CMD_PIN_CTS);
uart_driver_install(uart_num, 500, 500, 1, &command_uart_queue, 0);
uart_write_bytes(COMMAND_UART,"UART_OK\r", (sizeof("UART_OK\r")-1));
}
Code: Select all
char uart_command_handler(void)
{
uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
char uart_data_buff[BUF_SIZE];
int len = uart_read_bytes(COMMAND_UART, data, BUF_SIZE, 5 / portTICK_RATE_MS);
if (len == 0)
{
free(data);
data = NULL;
return 0;
}
if(len > 0)
{
do{
memcpy(uart_data_buff, (char*)data, len);
if ( strstr(uart_data_buff, DEVICE_PING_REQ) !=0 ) // device is pinging
{
ping_cnt++;
uart_write_bytes(COMMAND_UART,SENSOR_PING_ANS, strlen(SENSOR_PING_ANS)); // pong answer
free(data);
data = NULL;
return 1;
}
if ( strstr(uart_data_buff, DEVICE_DATA_REQ) !=0 ) // device request data
{
data_cnt++;
uart_write_bytes(COMMAND_UART,DATA_FOR_DEVICE, strlen(DATA_FOR_DEVICE)); // send data to device
free(data);
data = NULL;
return 1;
}
free(data);
data = NULL;
return 0;
} while(1);
}
free(data);
data = NULL;
return 0;
}