This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 6a2eef788d4bb55a20ee632f1fffcbfbe5f68d22
Author: Eren Terzioglu <[email protected]>
AuthorDate: Fri Jun 19 17:21:18 2026 +0200

    arch/risc-v/espressif: Add TWAI2 support for esp32p4
    
    Enable TWAI2 support for esp32p4
    
    Signed-off-by: Eren Terzioglu <[email protected]>
---
 arch/risc-v/src/common/espressif/Kconfig    | 50 ++++++++++++++++++++++++++++
 arch/risc-v/src/common/espressif/esp_twai.c | 51 +++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+)

diff --git a/arch/risc-v/src/common/espressif/Kconfig 
b/arch/risc-v/src/common/espressif/Kconfig
index d983b2264e4..e4df0a5c2eb 100644
--- a/arch/risc-v/src/common/espressif/Kconfig
+++ b/arch/risc-v/src/common/espressif/Kconfig
@@ -1645,6 +1645,14 @@ config ESPRESSIF_TWAI1
        select ARCH_HAVE_CAN_ERRORS
        select CAN
 
+config ESPRESSIF_TWAI2
+       bool "TWAI2 (CAN)"
+       default n
+       depends on ARCH_CHIP_ESP32P4
+       select ESPRESSIF_TWAI
+       select ARCH_HAVE_CAN_ERRORS
+       select CAN
+
 config ESPRESSIF_USBSERIAL
        bool "USB-Serial-JTAG Driver"
        default n
@@ -3438,6 +3446,48 @@ config ESPRESSIF_TWAI1_SAM
 
 endif # ESPRESSIF_TWAI1
 
+if ESPRESSIF_TWAI2
+
+config ESPRESSIF_TWAI2_TXPIN
+       int "TWAI2 TX Pin"
+       default 6
+
+config ESPRESSIF_TWAI2_RXPIN
+       int "TWAI2 RX Pin"
+       default 7
+
+choice ESPRESSIF_TWAI2_TIMING
+       prompt "TWAI2 Timing config"
+       default TWAI2_TIMING_100KBITS
+       ---help---
+               These options control timing of TWAI2.
+
+config TWAI2_TIMING_100KBITS
+       bool "100 KBits"
+
+config TWAI2_TIMING_125KBITS
+       bool "125 KBits"
+
+config TWAI2_TIMING_250KBITS
+       bool "250 KBits"
+
+config TWAI2_TIMING_500KBITS
+       bool "500 KBits"
+
+config TWAI2_TIMING_800KBITS
+       bool "800 KBits"
+
+endchoice # ESPRESSIF_TWAI2_TIMING
+
+config ESPRESSIF_TWAI2_SAM
+       bool "TWAI2 sampling"
+       default n
+       ---help---
+               The bus is sampled 3 times (recommended for low to medium speed 
buses
+               to spikes on the bus-line).
+
+endif # ESPRESSIF_TWAI2
+
 config ESPRESSIF_TWAI_TEST_MODE
        bool "TWAI character driver loopback test mode (for testing only)"
        default n
diff --git a/arch/risc-v/src/common/espressif/esp_twai.c 
b/arch/risc-v/src/common/espressif/esp_twai.c
index 77920410d3e..166c753d3ad 100644
--- a/arch/risc-v/src/common/espressif/esp_twai.c
+++ b/arch/risc-v/src/common/espressif/esp_twai.c
@@ -99,6 +99,20 @@
 #  endif
 #endif
 
+#ifdef CONFIG_ESPRESSIF_TWAI2
+#  ifdef CONFIG_TWAI2_TIMING_100KBITS
+#    define TWAI2_TIMING_CONFIG TWAI_TIMING_CONFIG_100KBITS()
+#  elif CONFIG_TWAI2_TIMING_125KBITS
+#    define TWAI2_TIMING_CONFIG TWAI_TIMING_CONFIG_125KBITS()
+#  elif CONFIG_TWAI2_TIMING_250KBITS
+#    define TWAI2_TIMING_CONFIG TWAI_TIMING_CONFIG_250KBITS()
+#  elif CONFIG_TWAI2_TIMING_500KBITS
+#    define TWAI2_TIMING_CONFIG TWAI_TIMING_CONFIG_500KBITS()
+#  else
+#    define TWAI2_TIMING_CONFIG TWAI_TIMING_CONFIG_800KBITS()
+#  endif
+#endif
+
 #if defined(CONFIG_ARCH_CHIP_ESP32C3)
 #  define INT_ENA_REG(hw)       hw->interrupt_enable_reg.val
 #elif defined(CONFIG_ARCH_CHIP_ESP32P4)
@@ -226,6 +240,24 @@ static struct can_dev_s g_twai1dev =
 };
 #endif /* CONFIG_ESPRESSIF_TWAI1 */
 
+#ifdef CONFIG_ESPRESSIF_TWAI2
+static struct esp_twai_dev_s g_twai2priv =
+{
+  .port             = 2,
+  .cpuint           = -ENOMEM,
+  .t_config         = TWAI2_TIMING_CONFIG,
+#ifdef CONFIG_PM
+  .pm_lock          = NULL,
+#endif
+};
+
+static struct can_dev_s g_twai2dev =
+{
+  .cd_ops           = &g_twaiops,
+  .cd_priv          = &g_twai2priv,
+};
+#endif /* CONFIG_ESPRESSIF_TWAI2 */
+
 static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
 
 /****************************************************************************
@@ -865,6 +897,25 @@ struct can_dev_s *esp_twaiinitialize(int port)
   else
 #endif
 
+#ifdef CONFIG_ESPRESSIF_TWAI2
+  if (port == 2)
+    {
+      int tx_sig = twai_periph_signals[2].tx_sig;
+      int rx_sig = twai_periph_signals[2].rx_sig;
+
+      /* Configure CAN GPIO pins */
+
+      esp_gpio_matrix_out(CONFIG_ESPRESSIF_TWAI2_TXPIN, tx_sig, 0, 0);
+      esp_configgpio(CONFIG_ESPRESSIF_TWAI2_TXPIN, TX_PIN_ATTR);
+
+      esp_gpio_matrix_in(CONFIG_ESPRESSIF_TWAI2_RXPIN, rx_sig, 0);
+      esp_configgpio(CONFIG_ESPRESSIF_TWAI2_RXPIN, RX_PIN_ATTR);
+
+      dev = &g_twai2dev;
+    }
+  else
+#endif
+
     {
       canerr("ERROR: Unsupported port: %d\n", port);
       leave_critical_section(flags);

Reply via email to