ESP32串口在非流控模式和硬件流控模式之间切换问题

1375917982
Posts: 2
Joined: Tue Nov 08, 2022 8:33 am

ESP32串口在非流控模式和硬件流控模式之间切换问题

Postby 1375917982 » Tue Nov 08, 2022 8:53 am

同一个串口在非流控模式和硬件流控模式之间切换,系统起来首次切换到硬件流控模式后esp32与模组串口通讯都正常;如果重新初始化串口,再设置成流控模式,esp32与模组串口通讯大概率会不正常,期间系统没有异常打印。

如果一直使用非流控模式,中途不管调用多少次uart_init(),esp32与模组串口通讯都是正常的。

调用uart_init()后 进入非流控模式,
调用uart_hw_flow_init()后 进入硬件流控模式

esp32串口非流控模式和硬件流控模式反复切换问题 有什么办法解决吗?

串口在非流控模式和硬件流控模式之间切换,是因为通过AT指令操控模组时,模组初始状态是非流控模式,初始化完成后设置模组为流控模式(使用流控模式是为了大数据量传输可靠),esp32串口也要相应切换到硬件流控模式。

Code: Select all

#define MODULE_TXD1_PIN             (GPIO_NUM_32)
#define MODULE_RXD1_PIN             (GPIO_NUM_35)//only use input

//UART1
#define RX1_BUF_SIZE         (512) //26//
#define TX1_BUF_SIZE         (512) //33//
#define TXD1_PIN             MODULE_TXD1_PIN //(GPIO_NUM_25)
#define RXD1_PIN             MODULE_RXD1_PIN //(GPIO_NUM_32) //(GPIO_NUM_5) //(GPIO_NUM_35)

#define RXD1_CTS             (GPIO_NUM_36)//only use input
#define TXD1_RTS             (GPIO_NUM_33)

Code: Select all

void uart_init(void)
{
    xSemaphoreTake(g_at_uart_lock, portMAX_DELAY);

    if(uart_is_driver_installed(AT_UART))
    {
        printf("%d %s()...uart_is_driver_installed \r\n", __LINE__, __FUNCTION__);
        ESP_ERROR_CHECK(uart_driver_delete(AT_UART));
    }

    int uart_num = AT_UART;

    uart_config_t uart_config = {
        .baud_rate = 115200,//921600,//115200,
        .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
    };

    ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
    ESP_ERROR_CHECK(uart_set_pin(uart_num, TXD1_PIN, RXD1_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
    ESP_ERROR_CHECK(uart_driver_install(uart_num, RX1_BUF_SIZE * 2, TX1_BUF_SIZE * 2, 0, NULL, ESP_INTR_FLAG_IRAM));

    xSemaphoreGive(g_at_uart_lock);
}

Code: Select all

void uart_hw_flow_init(void)
{
    xSemaphoreTake(g_at_uart_lock, portMAX_DELAY);

    if(uart_is_driver_installed(AT_UART))
    {
       printf("%d %s()...uart_is_driver_installed \r\n", __LINE__, __FUNCTION__);
        ESP_ERROR_CHECK(uart_driver_delete(AT_UART));
    }

#define  ENABLE_HW_FLOW  1//0

    int uart_num = AT_UART;
    uart_config_t uart_config = {
        .baud_rate = 115200,//921600,//115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        #if ENABLE_HW_FLOW
        .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS
        #else
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        #endif
        .rx_flow_ctrl_thresh = 122
    };

    ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
    #if ENABLE_HW_FLOW
    ESP_ERROR_CHECK(uart_set_pin(uart_num, TXD1_PIN, RXD1_PIN, TXD1_RTS, RXD1_CTS));
    #else
    ESP_ERROR_CHECK(uart_set_pin(uart_num, TXD1_PIN, RXD1_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
    #endif
    ESP_ERROR_CHECK(uart_driver_install(uart_num, RX1_BUF_SIZE * 2, TX1_BUF_SIZE * 2, 0, NULL, ESP_INTR_FLAG_IRAM));
   
#undef ENABLE_HW_FLOW

    xSemaphoreGive(g_at_uart_lock);
}

Who is online

Users browsing this forum: No registered users and 190 guests