Skip to content

Commit 9c2a5ac

Browse files
committed
Merge branch 'fix/twai_io_init_glitch_v5.4' into 'release/v5.4'
fix(driver_twai): fixed bus-off when twai_init due to wrong gpio config (v5.4) See merge request espressif/esp-idf!34815
2 parents fb52ad1 + 928b7f6 commit 9c2a5ac

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

components/driver/twai/twai.c

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
#include "esp_heap_caps.h"
1919
#include "esp_clk_tree.h"
2020
#include "clk_ctrl_os.h"
21-
#include "driver/gpio.h"
2221
#include "esp_private/periph_ctrl.h"
2322
#include "esp_private/esp_clk.h"
23+
#include "esp_private/gpio.h"
2424
#include "driver/twai.h"
2525
#include "soc/soc_caps.h"
2626
#include "soc/soc.h"
27+
#include "soc/io_mux_reg.h"
2728
#include "soc/twai_periph.h"
28-
#include "soc/gpio_sig_map.h"
2929
#include "hal/twai_hal.h"
3030
#include "esp_rom_gpio.h"
3131
#if SOC_TWAI_SUPPORT_SLEEP_RETENTION
@@ -34,6 +34,7 @@
3434

3535
/* ---------------------------- Definitions --------------------------------- */
3636
//Internal Macros
37+
#define TWAI_TAG "TWAI"
3738
#define TWAI_CHECK(cond, ret_val) ({ \
3839
if (!(cond)) { \
3940
return (ret_val); \
@@ -46,7 +47,6 @@
4647
#ifdef CONFIG_TWAI_ISR_IN_IRAM
4748
#define TWAI_MALLOC_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
4849
#else
49-
#define TWAI_TAG "TWAI"
5050
#define TWAI_MALLOC_CAPS MALLOC_CAP_DEFAULT
5151
#endif //CONFIG_TWAI_ISR_IN_IRAM
5252

@@ -289,37 +289,26 @@ static void twai_configure_gpio(int controller_id, gpio_num_t tx, gpio_num_t rx,
289289
{
290290
// assert the GPIO number is not a negative number (shift operation on a negative number is undefined)
291291
assert(tx >= 0 && rx >= 0);
292-
// if TX and RX set to the same GPIO, which means we want to create a loop-back in the GPIO matrix
293-
bool io_loop_back = (tx == rx);
294-
gpio_config_t gpio_conf = {
295-
.intr_type = GPIO_INTR_DISABLE,
296-
.pull_down_en = false,
297-
.pull_up_en = false,
298-
};
292+
299293
//Set RX pin
300-
gpio_conf.mode = GPIO_MODE_INPUT | (io_loop_back ? GPIO_MODE_OUTPUT : 0);
301-
gpio_conf.pin_bit_mask = 1ULL << rx;
302-
gpio_config(&gpio_conf);
294+
gpio_func_sel(rx, PIN_FUNC_GPIO);
295+
gpio_input_enable(rx);
303296
esp_rom_gpio_connect_in_signal(rx, twai_controller_periph_signals.controllers[controller_id].rx_sig, false);
304297

305298
//Set TX pin
306-
gpio_conf.mode = GPIO_MODE_OUTPUT | (io_loop_back ? GPIO_MODE_INPUT : 0);
307-
gpio_conf.pin_bit_mask = 1ULL << tx;
308-
gpio_config(&gpio_conf);
299+
gpio_func_sel(tx, PIN_FUNC_GPIO);
309300
esp_rom_gpio_connect_out_signal(tx, twai_controller_periph_signals.controllers[controller_id].tx_sig, false, false);
310301

311302
//Configure output clock pin (Optional)
312303
if (clkout >= 0 && clkout < GPIO_NUM_MAX) {
313-
gpio_set_pull_mode(clkout, GPIO_FLOATING);
304+
gpio_func_sel(clkout, PIN_FUNC_GPIO);
314305
esp_rom_gpio_connect_out_signal(clkout, twai_controller_periph_signals.controllers[controller_id].clk_out_sig, false, false);
315-
esp_rom_gpio_pad_select_gpio(clkout);
316306
}
317307

318308
//Configure bus status pin (Optional)
319309
if (bus_status >= 0 && bus_status < GPIO_NUM_MAX) {
320-
gpio_set_pull_mode(bus_status, GPIO_FLOATING);
310+
gpio_func_sel(bus_status, PIN_FUNC_GPIO);
321311
esp_rom_gpio_connect_out_signal(bus_status, twai_controller_periph_signals.controllers[controller_id].bus_off_sig, false, false);
322-
esp_rom_gpio_pad_select_gpio(bus_status);
323312
}
324313
}
325314

0 commit comments

Comments
 (0)