fix(twai): twai example crash using usb-serial-jtag pins

Calling gpio_hal_iomux_func_sel() via gpio_config() may
disconnects/disables any influence from USJ when using USJ pins
This commit is contained in:
wanlei 2023-10-25 12:42:39 +08:00 committed by Wan Lei
parent d5a32fa5e8
commit efef840124

View File

@ -277,19 +277,30 @@ TWAI_ISR_ATTR static void twai_intr_handler_main(void *arg)
} }
/* -------------------------- Helper functions ----------------------------- */ /* -------------------------- Helper functions ----------------------------- */
static esp_err_t twai_configure_gpio(gpio_num_t tx, gpio_num_t rx, gpio_num_t clkout, gpio_num_t bus_status)
static void twai_configure_gpio(gpio_num_t tx, gpio_num_t rx, gpio_num_t clkout, gpio_num_t bus_status)
{ {
//Set TX pin esp_err_t ret = ESP_FAIL;
gpio_set_pull_mode(tx, GPIO_FLOATING); assert(tx >= 0 && rx >= 0);
esp_rom_gpio_connect_out_signal(tx, TWAI_TX_IDX, false, false); // if TX and RX set to the same GPIO, which means we want to create a loop-back in the GPIO matrix
esp_rom_gpio_pad_select_gpio(tx); bool io_loop_back = (tx == rx);
gpio_config_t gpio_conf = {
.intr_type = GPIO_INTR_DISABLE,
.pull_down_en = false,
.pull_up_en = false,
};
//Set RX pin //Set RX pin
gpio_set_pull_mode(rx, GPIO_FLOATING); gpio_conf.mode = GPIO_MODE_INPUT | (io_loop_back ? GPIO_MODE_OUTPUT : 0);
gpio_conf.pin_bit_mask = 1ULL << rx;
ret = gpio_config(&gpio_conf);
TWAI_CHECK(ESP_OK == ret, ret);
esp_rom_gpio_connect_in_signal(rx, TWAI_RX_IDX, false); esp_rom_gpio_connect_in_signal(rx, TWAI_RX_IDX, false);
esp_rom_gpio_pad_select_gpio(rx);
gpio_set_direction(rx, GPIO_MODE_INPUT); //Set TX pin
gpio_conf.mode = GPIO_MODE_OUTPUT | (io_loop_back ? GPIO_MODE_INPUT : 0);
gpio_conf.pin_bit_mask = 1ULL << tx;
ret = gpio_config(&gpio_conf);
TWAI_CHECK(ESP_OK == ret, ret);
esp_rom_gpio_connect_out_signal(tx, TWAI_TX_IDX, false, false);
//Configure output clock pin (Optional) //Configure output clock pin (Optional)
if (clkout >= 0 && clkout < GPIO_NUM_MAX) { if (clkout >= 0 && clkout < GPIO_NUM_MAX) {
@ -304,6 +315,7 @@ static void twai_configure_gpio(gpio_num_t tx, gpio_num_t rx, gpio_num_t clkout,
esp_rom_gpio_connect_out_signal(bus_status, TWAI_BUS_OFF_ON_IDX, false, false); esp_rom_gpio_connect_out_signal(bus_status, TWAI_BUS_OFF_ON_IDX, false, false);
esp_rom_gpio_pad_select_gpio(bus_status); esp_rom_gpio_pad_select_gpio(bus_status);
} }
return ESP_OK;
} }
static void twai_free_driver_obj(twai_obj_t *p_obj) static void twai_free_driver_obj(twai_obj_t *p_obj)
@ -448,6 +460,13 @@ esp_err_t twai_driver_install(const twai_general_config_t *g_config, const twai_
p_twai_obj_dummy->mode = g_config->mode; p_twai_obj_dummy->mode = g_config->mode;
p_twai_obj_dummy->alerts_enabled = g_config->alerts_enabled; p_twai_obj_dummy->alerts_enabled = g_config->alerts_enabled;
//Assign GPIO
ret = twai_configure_gpio(g_config->tx_io, g_config->rx_io, g_config->clkout_io, g_config->bus_off_io);
if (ESP_OK != ret) {
ESP_LOGE(TWAI_TAG, "init gpio for twai failed");
goto err;
}
TWAI_ENTER_CRITICAL(); TWAI_ENTER_CRITICAL();
//Assign the TWAI object //Assign the TWAI object
if (p_twai_obj == NULL) { if (p_twai_obj == NULL) {
@ -469,9 +488,6 @@ esp_err_t twai_driver_install(const twai_general_config_t *g_config, const twai_
twai_hal_configure(&twai_context, t_config, f_config, DRIVER_DEFAULT_INTERRUPTS, g_config->clkout_divider); twai_hal_configure(&twai_context, t_config, f_config, DRIVER_DEFAULT_INTERRUPTS, g_config->clkout_divider);
TWAI_EXIT_CRITICAL(); TWAI_EXIT_CRITICAL();
//Assign GPIO
twai_configure_gpio(g_config->tx_io, g_config->rx_io, g_config->clkout_io, g_config->bus_off_io);
#if CONFIG_PM_ENABLE #if CONFIG_PM_ENABLE
//Acquire PM lock //Acquire PM lock
if (p_twai_obj->pm_lock) { if (p_twai_obj->pm_lock) {