You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "radek-pesina (via GitHub)" <gi...@apache.org> on 2024/03/14 01:03:37 UTC

[PR] Rpesina/esp32c3 twai [nuttx]

radek-pesina opened a new pull request, #11913:
URL: https://github.com/apache/nuttx/pull/11913

   ## Summary
   Replace ESP32C3 TWAI driver with common SJA1000 CAN driver (in a separate pull request).  This pull request depends on the SJA1000 PR.
   
   ## Impact
   Commonises the ESP32C3 TWAI driver implementation with the platform-independent SJA1000 CAN driver.
   
   ## Testing
   Tested on ESP32C3 with Examples CAN app.  successfully sent/received messages.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rpesina/esp32c3 twai [nuttx]

Posted by "tmedicci (via GitHub)" <gi...@apache.org>.
tmedicci commented on PR #11913:
URL: https://github.com/apache/nuttx/pull/11913#issuecomment-2007203924

   Hi!
   
   This implementation has some problems regarding board-level vs arch-level. Just to summarize these problems, please note that `arch/risc-v/src/esp32c3-legacy/esp32c3_twai.c` was removed and most of its implementations (like interrupt handling) were moved to `boards/risc-v/esp32c3-legacy/common/src/esp32c3_board_twai.c`: this means that the TWAI peripheral, that is part of the ESP32-C3 arch, is now part of the ESP32-C3-based boards. This is not the correct approach.
   
   Even considering that this implementation is part of the SJA1000 generic driver, we should keep the TWAI driver (`arch/risc-v/src/esp32c3-legacy/esp32c3_twai.c`) in its location and move things like interrupt handling, general config and other configs to an init function (like `esp32c3_twaiinitialize`, for instance) from the board-level to arch driver.
   
   `esp32c3_board_twai.c` should contain only the initialization and bindings to SJA1000, like:
   ```
   twai = esp32c3_twaiinitialize(devno, tx_pin, rx_pin);
   
   dev = sja1000_instantiate(twai->priv);
   
   ret = can_register(devpath, dev);
   ```
   
   All the logic to support the interrupt handling, the settings for the base register and the functions that will be registered on SJA1000 are handled in the arch driver (arch/risc-v/src/esp32c3-legacy/esp32c3_twai.c), not in the board-level.
   
   That is, the lower-level is the TWAI driver that supports a generic interface (SJA1000) that, finally, is registered as a upper-half driver (can). Take, as an example, the audio interface in `nuttx/boards/xtensa/esp32/common/src/esp32_cs4344.c`:
   
   ```
     i2s = esp32_i2sbus_initialize(port);
   
     cs4344 = cs4344_initialize(i2s);
   
     pcm = pcm_decode_initialize(cs4344);
      
     ret = audio_register(devname, pcm);
   ```
   
   The I2S peripheral is used to drive the cs4344 (which is a "dumb" device that received I2S packets only). This CS4344 is registered in the PCM decode (which send PCM-encoded data through I2S peripheral, which is the lower-level) and, finally, the PCM device is registered as an audio interface. All these interfaces are drivers! Some of them are generic (pcm, cs4344) and other arch-specific (the I2S), but are drivers. Device-specific drivers are implemented in the arch folder.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rpesina/esp32c3 twai [nuttx]

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on code in PR #11913:
URL: https://github.com/apache/nuttx/pull/11913#discussion_r1526338712


##########
include/nuttx/can/sja1000.h:
##########
@@ -0,0 +1,236 @@
+/****************************************************************************
+ * include/nuttx/can/sja1000.h
+ *
+ * SJA1000 CAN driver based on esp32c3_twai.h
+ *
+ * License header retained from original source.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_CAN_SJA1000_H
+#define __INCLUDE_NUTTX_CAN_SJA1000_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/* CAN hardware-dependent bit-timing constant
+ *
+ * Used for calculating and checking bit-timing parameters
+ */
+
+struct can_bittiming_const_s
+{
+  char name[16];      /* Name of the CAN controller hardware */

Review Comment:
   Why to include the CAN controller name? Do you want to print it to the user?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rpesina/esp32c3 twai [nuttx]

Posted by "radek-pesina (via GitHub)" <gi...@apache.org>.
radek-pesina commented on PR #11913:
URL: https://github.com/apache/nuttx/pull/11913#issuecomment-2008656104

   Hi All,
   
   I've provided this as an example driver usage of the commonised SJA1000.  Unfortunately our project schedule's tight so I won't get a chance to further this driver as it's not our target platform.  If someone finds use in this PR please feel free to take it over, else if not I am happy for it to be closed off.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rpesina/esp32c3 twai [nuttx]

Posted by "ppisa (via GitHub)" <gi...@apache.org>.
ppisa commented on PR #11913:
URL: https://github.com/apache/nuttx/pull/11913#issuecomment-2009080968

   I understand and I think that ESP32 would take some time to resolve legacy and others. But as I have suggested, I would be happy with some option to map it into PolarFire FPGA area with configurable base as an example for others how to integrate it. I would suggest to simplify the common interrupt service routine such way that it directly accepts struct can_dev_s *dev as a main argument and can be registered directly into nuttix as the hadler. Possible indirection by specifying handler in struct sja1000_config_s *config can have use for some special IRQ acknowledgement on some targets etc.
   
   As for ESP32, I have actually received two EP32C6 kits from Brno Espressif office, we want to use them with NuttX for education. I have found that most drivers have moved to  `arch/risc-v/src/common/espressif` but TWAI support is lost for non legacy version... So we can try to do something sane if the level of indirection through esp-hal-3rdparty allows it.
   
   We have another NuttX update for https://github.com/ICE-V-Wireless/ICE-V-Wireless which has stuck, as I know, on old IDF and is becoming rotten because community have problems to move forward. We have working solution based on prepared generic iCE40 NuttX FPGA SPI driver and then board mapping for legacy ESP32C3 NuttX support. I hope that @tmedicci  and other Espressif people would be supportive and help us to use it even with non legacy ESP32C2 version. I have significant fear, that it would get messy and would require to tinker with esp-hal-3rdparty and would tend to break etc... But we will see and you can demonstrate that solution is flexible and works for real, knowledgeable NuttX contributors well.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rpesina/esp32c3 twai [nuttx]

Posted by "ppisa (via GitHub)" <gi...@apache.org>.
ppisa commented on code in PR #11913:
URL: https://github.com/apache/nuttx/pull/11913#discussion_r1529924136


##########
boards/risc-v/esp32c3-legacy/common/src/esp32c3_board_twai.c:
##########
@@ -24,78 +27,615 @@
 
 #include <nuttx/config.h>
 
-#include <errno.h>
+#include <assert.h>
 #include <debug.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-#include <nuttx/can/can.h>
 #include <arch/board/board.h>
+#include <nuttx/can/can.h>
+#include <nuttx/irq.h>
 
 #include "chip.h"
-/* #include "arm_arch.h" */
 
-#include "esp32c3_twai.h"
+#include <nuttx/can/sja1000.h>
+
+#include <nuttx/signal.h>
+
+#include "riscv_internal.h"
+
+#include "esp32c3_clockconfig.h"
+#include "esp32c3_gpio.h"
+#include "esp32c3_irq.h"
+
+#include "hardware/esp32c3_gpio_sigmap.h"
+#include "hardware/esp32c3_system.h"
 
 #include "esp32c3_board_twai.h"
 
-#ifdef CONFIG_CAN
+#ifdef CONFIG_ESP32C3_TWAI
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Configuration ************************************************************/
+/* Sampling of BUS 3 times */
+
+#  ifndef CONFIG_CAN_SJA1000_SAM
+#    define CONFIG_CAN_SJA1000_SAM  false
+#  endif
+
+/* Debug ********************************************************************/
+
+/* Non-standard debug that may be enabled just for testing SJA1000 */
 
-#define TWAI_PORT0 0
+#ifndef CONFIG_DEBUG_CAN_INFO
+#  undef CONFIG_CANBUS_REGDEBUG
+#endif
+
+#if defined(CONFIG_CAN_SJA1000_DEBUG)
+#  define cantrace _info
+#else
+#  define cantrace _none
+#endif /* CONFIG_CAN_SJA1000_DEBUG */
 
 /****************************************************************************
- * Public Functions
+ * Private Types
  ****************************************************************************/
 
+struct esp32c3_twai_config_s
+{
+  /* Configuration structure as seen by the SJA1000 driver */
+
+  struct sja1000_config_s config;
+
+  /* Additional private definitions only known to this driver */
+
+  struct sja1000_dev_s *handle; /* The SJA1000 driver handle */
+  sja1000_handler_t handler;    /* The SJA1000 interrupt handler */
+  void *arg;                    /* Argument to pass interrupt handler */
+};
+
 /****************************************************************************
- * Name: board_twai_setup
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* IRQ access callbacks.  These operations all hidden behind callbacks
+ * to isolate the SJA1000 driver from differences in interrupt handling
+ * by varying boards and MCUs.
+ *
+ *   twai_attach  - Attach the TWAI interrupt handler to the interrupt
+ *   twai_detach  - Detach the TWAI interrupt handler from interrupt
+ */
+
+static int twai_attach(struct sja1000_config_s *config,
+                           sja1000_handler_t handler, void *arg);
+
+static int twai_detach(struct sja1000_config_s *config);
+
+static uint32_t twai_getreg(struct sja1000_dev_s *sja_priv,
+                               uint32_t reg);
+
+static void twai_putreg(struct sja1000_dev_s *sja_priv,
+                           uint32_t reg, uint32_t value);
+
+static void twai_modifyreg32(struct sja1000_dev_s *sja_priv,
+      uint32_t reg, uint32_t clearbits, uint32_t setbits);
+
+static int twai_init_gpio(FAR struct sja1000_config_s *config);
+
+static int board_twai_initialise(int devno);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* CAN hardware-dependent bit-timing constant
+ * Used for calculating and checking bit-timing parameters
+ */
+
+static const struct can_bittiming_const_s esp32c3_twai_bittiming_const =
+{
+  .tseg1_min = 1,
+  .tseg1_max = 16,
+  .tseg2_min = 1,
+  .tseg2_max = 8,
+  .sjw_max   = 3,
+  .brp_min   = 1,
+  .brp_max   = 64,
+  .brp_inc   = 1,
+};
+
+/* A reference to a structure of this type must be passed to the SJA1000
+ * driver.  This structure provides information about the configuration
+ * of the SJA1000 and provides some board-specific hooks.
+ *
+ * Memory for this structure is provided by the caller.  It is not copied
+ * by the driver and is presumed to persist while the driver is active. The
+ * memory must be writable because, under certain circumstances, the driver
+ * may modify frequency or X plate resistance values.
+ */
+
+static struct esp32c3_twai_config_s
+    g_esp32c3_twai_config[] =
+{
+#ifdef CONFIG_ESP32C3_TWAI0
+    {
+      .config =
+        {
+          .bittiming_const = &esp32c3_twai_bittiming_const,
+          .port     = 0,
+          .periph   = ESP32C3_PERIPH_TWAI,
+          .irq      = ESP32C3_IRQ_TWAI,
+          .cpuint   = -ENOMEM,
+          .bitrate  = CONFIG_CAN_SJA1000_BITRATE,
+          .samplep  = CONFIG_CAN_SJA1000_SAMPLEP,
+          .sjw      = CONFIG_CAN_SJA1000_SJW,
+#ifdef CONFIG_CAN_LOOPBACK
+          .loopback = true,
+#else
+          .loopback = false,
+#endif /* #ifdef CONFIG_CAN_LOOPBACK */
+          .triple_sample = CONFIG_CAN_SJA1000_SAM,
+          .attach        = twai_attach,
+          .detach        = twai_detach,
+        },
+    },
+#endif /* #ifdef CONFIG_ESP32C3_TWAI0 */
+#ifdef CONFIG_ESP32C3_TWAI1
+#  error "TODO - TWAI1 not yet defined"
+#endif /* #ifdef CONFIG_ESP32C3_TWAI1 */
+};
+
+static struct sja1000_dev_s g_twai_priv[] =
+{
+#ifdef CONFIG_ESP32C3_TWAI0
+    {
+      .getreg = twai_getreg,
+      .putreg = twai_putreg,
+      .modifyreg32 = twai_modifyreg32,
+      .config = &g_esp32c3_twai_config[0].config,
+      .base = DR_REG_TWAI_BASE,
+    },
+#endif /* #ifdef CONFIG_ESP32C3_TWAI0 */
+};
+

Review Comment:
   I am not sure about layering there. I would suggest to leave chop specific
   ```
         .putreg = twai_putreg,
         .modifyreg32 = twai_modifyreg32,
         .config = &g_esp32c3_twai_config[0].config,
         .base = DR_REG_TWAI_BASE,
   ```
   in the `struct sja1000_config_s` and copied these for speedup into private `struct sja1000_dev_s` during chip initialization. This way the BSP level does not need to know any details about actual driver private `struct sja1000_dev_s`. The arguments against this option would be if even priv structure can be kept const (in Flash), but I expect that it should be usable for the device state storage. And yes, there is some duplication and space waste due to copied base and pointers to reg functions in my prefered solution, but it provides speedup and HW access functions simpler.



##########
boards/risc-v/esp32c3-legacy/common/src/esp32c3_board_twai.c:
##########
@@ -24,78 +27,615 @@
 
 #include <nuttx/config.h>
 
-#include <errno.h>
+#include <assert.h>
 #include <debug.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-#include <nuttx/can/can.h>
 #include <arch/board/board.h>
+#include <nuttx/can/can.h>
+#include <nuttx/irq.h>
 
 #include "chip.h"
-/* #include "arm_arch.h" */
 
-#include "esp32c3_twai.h"
+#include <nuttx/can/sja1000.h>
+
+#include <nuttx/signal.h>
+
+#include "riscv_internal.h"
+
+#include "esp32c3_clockconfig.h"
+#include "esp32c3_gpio.h"
+#include "esp32c3_irq.h"
+
+#include "hardware/esp32c3_gpio_sigmap.h"
+#include "hardware/esp32c3_system.h"
 
 #include "esp32c3_board_twai.h"
 
-#ifdef CONFIG_CAN
+#ifdef CONFIG_ESP32C3_TWAI
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Configuration ************************************************************/
+/* Sampling of BUS 3 times */
+
+#  ifndef CONFIG_CAN_SJA1000_SAM
+#    define CONFIG_CAN_SJA1000_SAM  false
+#  endif
+
+/* Debug ********************************************************************/
+
+/* Non-standard debug that may be enabled just for testing SJA1000 */
 
-#define TWAI_PORT0 0
+#ifndef CONFIG_DEBUG_CAN_INFO
+#  undef CONFIG_CANBUS_REGDEBUG
+#endif
+
+#if defined(CONFIG_CAN_SJA1000_DEBUG)
+#  define cantrace _info
+#else
+#  define cantrace _none
+#endif /* CONFIG_CAN_SJA1000_DEBUG */
 
 /****************************************************************************
- * Public Functions
+ * Private Types
  ****************************************************************************/
 
+struct esp32c3_twai_config_s
+{
+  /* Configuration structure as seen by the SJA1000 driver */
+
+  struct sja1000_config_s config;
+
+  /* Additional private definitions only known to this driver */
+
+  struct sja1000_dev_s *handle; /* The SJA1000 driver handle */
+  sja1000_handler_t handler;    /* The SJA1000 interrupt handler */
+  void *arg;                    /* Argument to pass interrupt handler */
+};
+
 /****************************************************************************
- * Name: board_twai_setup
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* IRQ access callbacks.  These operations all hidden behind callbacks
+ * to isolate the SJA1000 driver from differences in interrupt handling
+ * by varying boards and MCUs.
+ *
+ *   twai_attach  - Attach the TWAI interrupt handler to the interrupt
+ *   twai_detach  - Detach the TWAI interrupt handler from interrupt
+ */
+
+static int twai_attach(struct sja1000_config_s *config,
+                           sja1000_handler_t handler, void *arg);
+
+static int twai_detach(struct sja1000_config_s *config);
+
+static uint32_t twai_getreg(struct sja1000_dev_s *sja_priv,
+                               uint32_t reg);
+
+static void twai_putreg(struct sja1000_dev_s *sja_priv,
+                           uint32_t reg, uint32_t value);
+
+static void twai_modifyreg32(struct sja1000_dev_s *sja_priv,
+      uint32_t reg, uint32_t clearbits, uint32_t setbits);
+
+static int twai_init_gpio(FAR struct sja1000_config_s *config);
+
+static int board_twai_initialise(int devno);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* CAN hardware-dependent bit-timing constant
+ * Used for calculating and checking bit-timing parameters
+ */
+
+static const struct can_bittiming_const_s esp32c3_twai_bittiming_const =
+{
+  .tseg1_min = 1,
+  .tseg1_max = 16,
+  .tseg2_min = 1,
+  .tseg2_max = 8,
+  .sjw_max   = 3,
+  .brp_min   = 1,
+  .brp_max   = 64,
+  .brp_inc   = 1,
+};
+
+/* A reference to a structure of this type must be passed to the SJA1000
+ * driver.  This structure provides information about the configuration
+ * of the SJA1000 and provides some board-specific hooks.
+ *
+ * Memory for this structure is provided by the caller.  It is not copied
+ * by the driver and is presumed to persist while the driver is active. The
+ * memory must be writable because, under certain circumstances, the driver
+ * may modify frequency or X plate resistance values.
+ */
+
+static struct esp32c3_twai_config_s
+    g_esp32c3_twai_config[] =
+{
+#ifdef CONFIG_ESP32C3_TWAI0
+    {
+      .config =
+        {
+          .bittiming_const = &esp32c3_twai_bittiming_const,
+          .port     = 0,
+          .periph   = ESP32C3_PERIPH_TWAI,
+          .irq      = ESP32C3_IRQ_TWAI,
+          .cpuint   = -ENOMEM,
+          .bitrate  = CONFIG_CAN_SJA1000_BITRATE,
+          .samplep  = CONFIG_CAN_SJA1000_SAMPLEP,
+          .sjw      = CONFIG_CAN_SJA1000_SJW,
+#ifdef CONFIG_CAN_LOOPBACK
+          .loopback = true,
+#else
+          .loopback = false,
+#endif /* #ifdef CONFIG_CAN_LOOPBACK */
+          .triple_sample = CONFIG_CAN_SJA1000_SAM,
+          .attach        = twai_attach,
+          .detach        = twai_detach,
+        },
+    },
+#endif /* #ifdef CONFIG_ESP32C3_TWAI0 */
+#ifdef CONFIG_ESP32C3_TWAI1
+#  error "TODO - TWAI1 not yet defined"
+#endif /* #ifdef CONFIG_ESP32C3_TWAI1 */
+};
+
+static struct sja1000_dev_s g_twai_priv[] =
+{
+#ifdef CONFIG_ESP32C3_TWAI0
+    {
+      .getreg = twai_getreg,
+      .putreg = twai_putreg,
+      .modifyreg32 = twai_modifyreg32,
+      .config = &g_esp32c3_twai_config[0].config,
+      .base = DR_REG_TWAI_BASE,
+    },
+#endif /* #ifdef CONFIG_ESP32C3_TWAI0 */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_twai_interrupt
  *
  * Description:
- *   Configure the TWAI driver.
+ *   This is the TWAI Interrupt handler.
  *
  * Input Parameters:
- *   None.
+ *   irq - Interrupt number
+ *   context - interrupt context
+ *   arg - pointer to sja1000_dev_s (lower CAN driver) context.
  *
  * Returned Value:
- *   Zero (OK) is returned on success; A negated errno value is returned
- *   to indicate the nature of any failure.
+ *   OK on success or negated error value.
  *
  ****************************************************************************/
 
-int board_twai_setup(void)
+int esp32c3_twai_interrupt(int irq, void *context, void *arg)
 {
-#ifdef CONFIG_ESP32C3_TWAI0
-  struct can_dev_s *twai;
+  struct sja1000_dev_s *priv = (struct sja1000_dev_s *)arg;
+  struct esp32c3_twai_config_s *esp32c3_twai_config
+      = &g_esp32c3_twai_config[priv->config->port];
+
+  cantrace("Entered for CAN port %" PRId8 ".\n", priv->config->port);
+  DEBUGASSERT(priv != NULL);
+  DEBUGASSERT(esp32c3_twai_config != NULL);
+  DEBUGASSERT(esp32c3_twai_config->handler != NULL);
+  DEBUGASSERT(priv->config != NULL);
+  DEBUGASSERT(esp32c3_twai_config->arg);
+
+  esp32c3_twai_config->handler(priv->config, esp32c3_twai_config->arg);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: twai_detach
+ *
+ * Description:
+ *   Attach SJA1000 instance to interrupt.
+ *
+ * Input Parameters:
+ *   config - Configuration instance of the SJA1000 driver to attach
+ *   handler - interrupt callback hander
+ *   arg - argument to register for passing to interrupt handler.
+ *
+ * Returned Value:
+ *   OK on success or negated error value.
+ *
+ ****************************************************************************/
+
+static int twai_attach(struct sja1000_config_s *config,
+                          sja1000_handler_t handler, void *arg)
+{
+  struct can_dev_s *dev = (struct can_dev_s *)arg;
+  struct sja1000_dev_s *priv = dev->cd_priv;
+  irqstate_t flags;
+  int devno = config->port;
+  DEBUGASSERT(devno < ESP32C3_TWAI_NUM_PORTS);
   int ret;
+  struct esp32c3_twai_config_s *esp32c3_twai_config =
+      &g_esp32c3_twai_config[devno];
+
+  DEBUGASSERT(esp32c3_twai_config);
+
+  flags = enter_critical_section();
+
+  cantrace("Saving handler %p, arg %p for port %" PRId8 ".\n",
+      handler, arg, devno);
 
-  /* Call esp32c3_twaiinitialize() to get an instance of the TWAI0
-   * interface
-   * */
+  esp32c3_twai_config->handler = handler;
+  esp32c3_twai_config->arg = arg;
+
+  if (config->cpuint >= 0)
+    {
+      /* Disable the provided CPU Interrupt to configure it. */
+
+      up_disable_irq(config->irq);
+    }
 
-  twai = esp32c3_twaiinitialize(TWAI_PORT0);
-  if (twai == NULL)
+  config->cpuint = esp32c3_setup_irq(
+      config->periph, ESP32C3_INT_PRIO_DEF, ESP32C3_INT_LEVEL);
+  if (config->cpuint < 0)
     {
-      canerr("ERROR:  Failed to get TWAI0 interface\n");
-      return -ENODEV;
+      /* Failed to allocate a CPU interrupt of this type. */
+
+      ret = config->cpuint;
+      leave_critical_section(flags);
+
+      return ret;
     }
 
-  /* Register the TWAI0 driver at "/dev/can0" */
+  ret = irq_attach(config->irq, esp32c3_twai_interrupt, priv);
 
-  ret = can_register("/dev/can0", twai);
-  if (ret < 0)
+  if (ret != OK)
     {
-      canerr("ERROR: TWAI0 register failed: %d\n", ret);
+      /* Failed to attach IRQ, so CPU interrupt must be freed. */
+
+      canerr("Failed to initialize device %d.\n", devno);
+      esp32c3_teardown_irq(config->periph, config->cpuint);
+      config->cpuint = -ENOMEM;
+      leave_critical_section(flags);
+
       return ret;
     }
 
+  /* Enable the CPU interrupt that is linked to the TWAI device. */
+
+  up_enable_irq(config->irq);
+
+  leave_critical_section(flags);
+
   return OK;
-#else
-  return -ENODEV;
-#endif
 }
 
-#endif /* CONFIG_CAN */
+/****************************************************************************
+ * Name: twai_detach
+ *
+ * Description:
+ *   Detach from interrupt.
+ *
+ * Input Parameters:
+ *   config - Configuration instance of the SJA1000 driver to detach
+ *
+ * Returned Value:
+ *   OK on success or negated error value.
+ *
+ ****************************************************************************/
+
+static int twai_detach(struct sja1000_config_s *config)
+{
+  irqstate_t flags;
+  int devno = config->port;
+  DEBUGASSERT(devno < ESP32C3_TWAI_NUM_PORTS);
+  struct esp32c3_twai_config_s *esp32c3_twai_config =
+      &g_esp32c3_twai_config[devno];
+
+  DEBUGASSERT(esp32c3_twai_config);
+
+  flags = enter_critical_section();
+
+  cantrace("Detaching interrupts for port %" PRId8 ".\n", devno);
+
+  esp32c3_twai_config->handler = NULL;
+  esp32c3_twai_config->arg     = NULL;
+
+  if (config->cpuint >= 0)
+    {
+      /* Disable cpu interrupt */
+
+      up_disable_irq(config->irq);
+
+      /* Dissociate the IRQ from the ISR */
+
+      irq_detach(config->irq);
+
+      config->cpuint = -ENOMEM;
+    }
+
+  leave_critical_section(flags);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: twai_getreg
+ *
+ * Description:
+ *   Read the value of an TWAI register.
+ *
+ * Input Parameters:
+ *   priv - sja1000 lower-half driver context
+ *   reg - The register address to read
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+static uint32_t twai_getreg(struct sja1000_dev_s *sja_priv,
+      uint32_t reg)
+{
+  uint32_t value;
+  uint32_t mem_addr = (sja_priv->base + (reg << 2));
+
+  /* Read the value from the register */
+
+  value = getreg32(mem_addr);
+#ifdef CONFIG_CANBUS_REGDEBUG
+  cantrace("%08" PRIx32 " -> %08" PRIx32 "\n", mem_addr, value);
+#endif /* CONFIG_CANBUS_REGDEBUG */
+  return value;
+}
+
+/****************************************************************************
+ * Name: twai_putreg
+ *
+ * Description:
+ *   Set the value of an TWAI register.
+ *
+ * Input Parameters:
+ *   priv - sja1000 lower-half driver context
+ *   reg - The register address to write
+ *   value - The value to write to the register
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void twai_putreg(
+    struct sja1000_dev_s *sja_priv, uint32_t reg, uint32_t value)
+{
+  uint32_t mem_addr = (sja_priv->base + (reg << 2));
+
+#ifdef CONFIG_CANBUS_REGDEBUG
+  /* Show the register value being written */
+
+  cantrace("%08" PRIx32 " <- %08" PRIx32 "\n", mem_addr, value);
+#endif /* CONFIG_CANBUS_REGDEBUG */
+
+  /* Write the value */
+
+  putreg32(value, mem_addr);
+}
+

Review Comment:
   Ack



##########
boards/risc-v/esp32c3-legacy/common/src/esp32c3_board_twai.c:
##########
@@ -24,78 +27,615 @@
 
 #include <nuttx/config.h>
 
-#include <errno.h>
+#include <assert.h>
 #include <debug.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-#include <nuttx/can/can.h>
 #include <arch/board/board.h>
+#include <nuttx/can/can.h>
+#include <nuttx/irq.h>
 
 #include "chip.h"
-/* #include "arm_arch.h" */
 
-#include "esp32c3_twai.h"
+#include <nuttx/can/sja1000.h>
+
+#include <nuttx/signal.h>
+
+#include "riscv_internal.h"
+
+#include "esp32c3_clockconfig.h"
+#include "esp32c3_gpio.h"
+#include "esp32c3_irq.h"
+
+#include "hardware/esp32c3_gpio_sigmap.h"
+#include "hardware/esp32c3_system.h"
 
 #include "esp32c3_board_twai.h"
 
-#ifdef CONFIG_CAN
+#ifdef CONFIG_ESP32C3_TWAI
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Configuration ************************************************************/
+/* Sampling of BUS 3 times */
+
+#  ifndef CONFIG_CAN_SJA1000_SAM
+#    define CONFIG_CAN_SJA1000_SAM  false
+#  endif
+
+/* Debug ********************************************************************/
+
+/* Non-standard debug that may be enabled just for testing SJA1000 */
 
-#define TWAI_PORT0 0
+#ifndef CONFIG_DEBUG_CAN_INFO
+#  undef CONFIG_CANBUS_REGDEBUG
+#endif
+
+#if defined(CONFIG_CAN_SJA1000_DEBUG)
+#  define cantrace _info
+#else
+#  define cantrace _none
+#endif /* CONFIG_CAN_SJA1000_DEBUG */
 
 /****************************************************************************
- * Public Functions
+ * Private Types
  ****************************************************************************/
 
+struct esp32c3_twai_config_s
+{
+  /* Configuration structure as seen by the SJA1000 driver */
+
+  struct sja1000_config_s config;
+
+  /* Additional private definitions only known to this driver */
+
+  struct sja1000_dev_s *handle; /* The SJA1000 driver handle */
+  sja1000_handler_t handler;    /* The SJA1000 interrupt handler */
+  void *arg;                    /* Argument to pass interrupt handler */
+};
+
 /****************************************************************************
- * Name: board_twai_setup
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* IRQ access callbacks.  These operations all hidden behind callbacks
+ * to isolate the SJA1000 driver from differences in interrupt handling
+ * by varying boards and MCUs.
+ *
+ *   twai_attach  - Attach the TWAI interrupt handler to the interrupt
+ *   twai_detach  - Detach the TWAI interrupt handler from interrupt
+ */
+
+static int twai_attach(struct sja1000_config_s *config,
+                           sja1000_handler_t handler, void *arg);
+
+static int twai_detach(struct sja1000_config_s *config);
+
+static uint32_t twai_getreg(struct sja1000_dev_s *sja_priv,
+                               uint32_t reg);
+
+static void twai_putreg(struct sja1000_dev_s *sja_priv,
+                           uint32_t reg, uint32_t value);
+
+static void twai_modifyreg32(struct sja1000_dev_s *sja_priv,
+      uint32_t reg, uint32_t clearbits, uint32_t setbits);
+
+static int twai_init_gpio(FAR struct sja1000_config_s *config);
+
+static int board_twai_initialise(int devno);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* CAN hardware-dependent bit-timing constant
+ * Used for calculating and checking bit-timing parameters
+ */
+
+static const struct can_bittiming_const_s esp32c3_twai_bittiming_const =
+{
+  .tseg1_min = 1,
+  .tseg1_max = 16,
+  .tseg2_min = 1,
+  .tseg2_max = 8,
+  .sjw_max   = 3,
+  .brp_min   = 1,
+  .brp_max   = 64,
+  .brp_inc   = 1,
+};
+
+/* A reference to a structure of this type must be passed to the SJA1000
+ * driver.  This structure provides information about the configuration
+ * of the SJA1000 and provides some board-specific hooks.
+ *
+ * Memory for this structure is provided by the caller.  It is not copied
+ * by the driver and is presumed to persist while the driver is active. The
+ * memory must be writable because, under certain circumstances, the driver
+ * may modify frequency or X plate resistance values.
+ */
+
+static struct esp32c3_twai_config_s
+    g_esp32c3_twai_config[] =
+{
+#ifdef CONFIG_ESP32C3_TWAI0
+    {
+      .config =
+        {
+          .bittiming_const = &esp32c3_twai_bittiming_const,
+          .port     = 0,
+          .periph   = ESP32C3_PERIPH_TWAI,
+          .irq      = ESP32C3_IRQ_TWAI,
+          .cpuint   = -ENOMEM,
+          .bitrate  = CONFIG_CAN_SJA1000_BITRATE,
+          .samplep  = CONFIG_CAN_SJA1000_SAMPLEP,
+          .sjw      = CONFIG_CAN_SJA1000_SJW,
+#ifdef CONFIG_CAN_LOOPBACK
+          .loopback = true,
+#else
+          .loopback = false,
+#endif /* #ifdef CONFIG_CAN_LOOPBACK */
+          .triple_sample = CONFIG_CAN_SJA1000_SAM,
+          .attach        = twai_attach,
+          .detach        = twai_detach,
+        },
+    },
+#endif /* #ifdef CONFIG_ESP32C3_TWAI0 */
+#ifdef CONFIG_ESP32C3_TWAI1
+#  error "TODO - TWAI1 not yet defined"
+#endif /* #ifdef CONFIG_ESP32C3_TWAI1 */
+};
+
+static struct sja1000_dev_s g_twai_priv[] =
+{
+#ifdef CONFIG_ESP32C3_TWAI0
+    {
+      .getreg = twai_getreg,
+      .putreg = twai_putreg,
+      .modifyreg32 = twai_modifyreg32,
+      .config = &g_esp32c3_twai_config[0].config,
+      .base = DR_REG_TWAI_BASE,
+    },
+#endif /* #ifdef CONFIG_ESP32C3_TWAI0 */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_twai_interrupt
  *
  * Description:
- *   Configure the TWAI driver.
+ *   This is the TWAI Interrupt handler.
  *
  * Input Parameters:
- *   None.
+ *   irq - Interrupt number
+ *   context - interrupt context
+ *   arg - pointer to sja1000_dev_s (lower CAN driver) context.
  *
  * Returned Value:
- *   Zero (OK) is returned on success; A negated errno value is returned
- *   to indicate the nature of any failure.
+ *   OK on success or negated error value.
  *
  ****************************************************************************/
 
-int board_twai_setup(void)
+int esp32c3_twai_interrupt(int irq, void *context, void *arg)
 {
-#ifdef CONFIG_ESP32C3_TWAI0
-  struct can_dev_s *twai;
+  struct sja1000_dev_s *priv = (struct sja1000_dev_s *)arg;
+  struct esp32c3_twai_config_s *esp32c3_twai_config
+      = &g_esp32c3_twai_config[priv->config->port];
+
+  cantrace("Entered for CAN port %" PRId8 ".\n", priv->config->port);
+  DEBUGASSERT(priv != NULL);
+  DEBUGASSERT(esp32c3_twai_config != NULL);
+  DEBUGASSERT(esp32c3_twai_config->handler != NULL);
+  DEBUGASSERT(priv->config != NULL);
+  DEBUGASSERT(esp32c3_twai_config->arg);
+
+  esp32c3_twai_config->handler(priv->config, esp32c3_twai_config->arg);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: twai_detach
+ *
+ * Description:
+ *   Attach SJA1000 instance to interrupt.
+ *
+ * Input Parameters:
+ *   config - Configuration instance of the SJA1000 driver to attach
+ *   handler - interrupt callback hander
+ *   arg - argument to register for passing to interrupt handler.
+ *
+ * Returned Value:
+ *   OK on success or negated error value.
+ *
+ ****************************************************************************/
+
+static int twai_attach(struct sja1000_config_s *config,
+                          sja1000_handler_t handler, void *arg)
+{
+  struct can_dev_s *dev = (struct can_dev_s *)arg;
+  struct sja1000_dev_s *priv = dev->cd_priv;
+  irqstate_t flags;
+  int devno = config->port;
+  DEBUGASSERT(devno < ESP32C3_TWAI_NUM_PORTS);
   int ret;
+  struct esp32c3_twai_config_s *esp32c3_twai_config =
+      &g_esp32c3_twai_config[devno];
+
+  DEBUGASSERT(esp32c3_twai_config);
+
+  flags = enter_critical_section();
+
+  cantrace("Saving handler %p, arg %p for port %" PRId8 ".\n",
+      handler, arg, devno);
 
-  /* Call esp32c3_twaiinitialize() to get an instance of the TWAI0
-   * interface
-   * */
+  esp32c3_twai_config->handler = handler;
+  esp32c3_twai_config->arg = arg;
+
+  if (config->cpuint >= 0)
+    {
+      /* Disable the provided CPU Interrupt to configure it. */
+
+      up_disable_irq(config->irq);
+    }
 
-  twai = esp32c3_twaiinitialize(TWAI_PORT0);
-  if (twai == NULL)
+  config->cpuint = esp32c3_setup_irq(
+      config->periph, ESP32C3_INT_PRIO_DEF, ESP32C3_INT_LEVEL);
+  if (config->cpuint < 0)
     {
-      canerr("ERROR:  Failed to get TWAI0 interface\n");
-      return -ENODEV;
+      /* Failed to allocate a CPU interrupt of this type. */
+
+      ret = config->cpuint;
+      leave_critical_section(flags);
+
+      return ret;
     }
 
-  /* Register the TWAI0 driver at "/dev/can0" */
+  ret = irq_attach(config->irq, esp32c3_twai_interrupt, priv);
 
-  ret = can_register("/dev/can0", twai);
-  if (ret < 0)
+  if (ret != OK)
     {
-      canerr("ERROR: TWAI0 register failed: %d\n", ret);
+      /* Failed to attach IRQ, so CPU interrupt must be freed. */
+
+      canerr("Failed to initialize device %d.\n", devno);
+      esp32c3_teardown_irq(config->periph, config->cpuint);
+      config->cpuint = -ENOMEM;
+      leave_critical_section(flags);
+
       return ret;
     }
 
+  /* Enable the CPU interrupt that is linked to the TWAI device. */
+
+  up_enable_irq(config->irq);
+
+  leave_critical_section(flags);
+
   return OK;
-#else
-  return -ENODEV;
-#endif
 }
 
-#endif /* CONFIG_CAN */
+/****************************************************************************
+ * Name: twai_detach
+ *
+ * Description:
+ *   Detach from interrupt.
+ *
+ * Input Parameters:
+ *   config - Configuration instance of the SJA1000 driver to detach
+ *
+ * Returned Value:
+ *   OK on success or negated error value.
+ *
+ ****************************************************************************/
+
+static int twai_detach(struct sja1000_config_s *config)
+{
+  irqstate_t flags;
+  int devno = config->port;
+  DEBUGASSERT(devno < ESP32C3_TWAI_NUM_PORTS);
+  struct esp32c3_twai_config_s *esp32c3_twai_config =
+      &g_esp32c3_twai_config[devno];
+
+  DEBUGASSERT(esp32c3_twai_config);
+
+  flags = enter_critical_section();
+
+  cantrace("Detaching interrupts for port %" PRId8 ".\n", devno);
+
+  esp32c3_twai_config->handler = NULL;
+  esp32c3_twai_config->arg     = NULL;
+
+  if (config->cpuint >= 0)
+    {
+      /* Disable cpu interrupt */
+
+      up_disable_irq(config->irq);
+
+      /* Dissociate the IRQ from the ISR */
+
+      irq_detach(config->irq);
+
+      config->cpuint = -ENOMEM;
+    }
+
+  leave_critical_section(flags);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: twai_getreg
+ *
+ * Description:
+ *   Read the value of an TWAI register.
+ *
+ * Input Parameters:
+ *   priv - sja1000 lower-half driver context
+ *   reg - The register address to read
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+static uint32_t twai_getreg(struct sja1000_dev_s *sja_priv,
+      uint32_t reg)
+{
+  uint32_t value;
+  uint32_t mem_addr = (sja_priv->base + (reg << 2));
+
+  /* Read the value from the register */
+
+  value = getreg32(mem_addr);
+#ifdef CONFIG_CANBUS_REGDEBUG
+  cantrace("%08" PRIx32 " -> %08" PRIx32 "\n", mem_addr, value);
+#endif /* CONFIG_CANBUS_REGDEBUG */
+  return value;
+}
+

Review Comment:
   Ack



##########
boards/risc-v/esp32c3-legacy/common/src/esp32c3_board_twai.c:
##########
@@ -24,78 +27,615 @@
 
 #include <nuttx/config.h>
 
-#include <errno.h>
+#include <assert.h>
 #include <debug.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-#include <nuttx/can/can.h>
 #include <arch/board/board.h>
+#include <nuttx/can/can.h>
+#include <nuttx/irq.h>
 
 #include "chip.h"
-/* #include "arm_arch.h" */
 
-#include "esp32c3_twai.h"
+#include <nuttx/can/sja1000.h>
+
+#include <nuttx/signal.h>
+
+#include "riscv_internal.h"
+
+#include "esp32c3_clockconfig.h"
+#include "esp32c3_gpio.h"
+#include "esp32c3_irq.h"
+
+#include "hardware/esp32c3_gpio_sigmap.h"
+#include "hardware/esp32c3_system.h"
 
 #include "esp32c3_board_twai.h"
 
-#ifdef CONFIG_CAN
+#ifdef CONFIG_ESP32C3_TWAI
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Configuration ************************************************************/
+/* Sampling of BUS 3 times */
+
+#  ifndef CONFIG_CAN_SJA1000_SAM
+#    define CONFIG_CAN_SJA1000_SAM  false
+#  endif
+
+/* Debug ********************************************************************/
+
+/* Non-standard debug that may be enabled just for testing SJA1000 */
 
-#define TWAI_PORT0 0
+#ifndef CONFIG_DEBUG_CAN_INFO
+#  undef CONFIG_CANBUS_REGDEBUG
+#endif
+
+#if defined(CONFIG_CAN_SJA1000_DEBUG)
+#  define cantrace _info
+#else
+#  define cantrace _none
+#endif /* CONFIG_CAN_SJA1000_DEBUG */
 
 /****************************************************************************
- * Public Functions
+ * Private Types
  ****************************************************************************/
 
+struct esp32c3_twai_config_s
+{
+  /* Configuration structure as seen by the SJA1000 driver */
+
+  struct sja1000_config_s config;
+
+  /* Additional private definitions only known to this driver */
+
+  struct sja1000_dev_s *handle; /* The SJA1000 driver handle */
+  sja1000_handler_t handler;    /* The SJA1000 interrupt handler */
+  void *arg;                    /* Argument to pass interrupt handler */
+};
+
 /****************************************************************************
- * Name: board_twai_setup
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* IRQ access callbacks.  These operations all hidden behind callbacks
+ * to isolate the SJA1000 driver from differences in interrupt handling
+ * by varying boards and MCUs.
+ *
+ *   twai_attach  - Attach the TWAI interrupt handler to the interrupt
+ *   twai_detach  - Detach the TWAI interrupt handler from interrupt
+ */
+
+static int twai_attach(struct sja1000_config_s *config,
+                           sja1000_handler_t handler, void *arg);
+
+static int twai_detach(struct sja1000_config_s *config);
+
+static uint32_t twai_getreg(struct sja1000_dev_s *sja_priv,
+                               uint32_t reg);
+
+static void twai_putreg(struct sja1000_dev_s *sja_priv,
+                           uint32_t reg, uint32_t value);
+
+static void twai_modifyreg32(struct sja1000_dev_s *sja_priv,
+      uint32_t reg, uint32_t clearbits, uint32_t setbits);
+
+static int twai_init_gpio(FAR struct sja1000_config_s *config);
+
+static int board_twai_initialise(int devno);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* CAN hardware-dependent bit-timing constant
+ * Used for calculating and checking bit-timing parameters
+ */
+
+static const struct can_bittiming_const_s esp32c3_twai_bittiming_const =
+{
+  .tseg1_min = 1,
+  .tseg1_max = 16,
+  .tseg2_min = 1,
+  .tseg2_max = 8,
+  .sjw_max   = 3,
+  .brp_min   = 1,
+  .brp_max   = 64,
+  .brp_inc   = 1,
+};
+
+/* A reference to a structure of this type must be passed to the SJA1000
+ * driver.  This structure provides information about the configuration
+ * of the SJA1000 and provides some board-specific hooks.
+ *
+ * Memory for this structure is provided by the caller.  It is not copied
+ * by the driver and is presumed to persist while the driver is active. The
+ * memory must be writable because, under certain circumstances, the driver
+ * may modify frequency or X plate resistance values.
+ */
+
+static struct esp32c3_twai_config_s
+    g_esp32c3_twai_config[] =
+{
+#ifdef CONFIG_ESP32C3_TWAI0
+    {
+      .config =
+        {
+          .bittiming_const = &esp32c3_twai_bittiming_const,
+          .port     = 0,
+          .periph   = ESP32C3_PERIPH_TWAI,
+          .irq      = ESP32C3_IRQ_TWAI,
+          .cpuint   = -ENOMEM,
+          .bitrate  = CONFIG_CAN_SJA1000_BITRATE,
+          .samplep  = CONFIG_CAN_SJA1000_SAMPLEP,
+          .sjw      = CONFIG_CAN_SJA1000_SJW,
+#ifdef CONFIG_CAN_LOOPBACK
+          .loopback = true,
+#else
+          .loopback = false,
+#endif /* #ifdef CONFIG_CAN_LOOPBACK */
+          .triple_sample = CONFIG_CAN_SJA1000_SAM,
+          .attach        = twai_attach,
+          .detach        = twai_detach,
+        },
+    },
+#endif /* #ifdef CONFIG_ESP32C3_TWAI0 */
+#ifdef CONFIG_ESP32C3_TWAI1
+#  error "TODO - TWAI1 not yet defined"
+#endif /* #ifdef CONFIG_ESP32C3_TWAI1 */
+};
+
+static struct sja1000_dev_s g_twai_priv[] =
+{
+#ifdef CONFIG_ESP32C3_TWAI0
+    {
+      .getreg = twai_getreg,
+      .putreg = twai_putreg,
+      .modifyreg32 = twai_modifyreg32,
+      .config = &g_esp32c3_twai_config[0].config,
+      .base = DR_REG_TWAI_BASE,
+    },
+#endif /* #ifdef CONFIG_ESP32C3_TWAI0 */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_twai_interrupt
  *
  * Description:
- *   Configure the TWAI driver.
+ *   This is the TWAI Interrupt handler.
  *
  * Input Parameters:
- *   None.
+ *   irq - Interrupt number
+ *   context - interrupt context
+ *   arg - pointer to sja1000_dev_s (lower CAN driver) context.
  *
  * Returned Value:
- *   Zero (OK) is returned on success; A negated errno value is returned
- *   to indicate the nature of any failure.
+ *   OK on success or negated error value.
  *
  ****************************************************************************/
 
-int board_twai_setup(void)
+int esp32c3_twai_interrupt(int irq, void *context, void *arg)
 {
-#ifdef CONFIG_ESP32C3_TWAI0
-  struct can_dev_s *twai;
+  struct sja1000_dev_s *priv = (struct sja1000_dev_s *)arg;
+  struct esp32c3_twai_config_s *esp32c3_twai_config
+      = &g_esp32c3_twai_config[priv->config->port];
+
+  cantrace("Entered for CAN port %" PRId8 ".\n", priv->config->port);
+  DEBUGASSERT(priv != NULL);
+  DEBUGASSERT(esp32c3_twai_config != NULL);
+  DEBUGASSERT(esp32c3_twai_config->handler != NULL);
+  DEBUGASSERT(priv->config != NULL);
+  DEBUGASSERT(esp32c3_twai_config->arg);
+
+  esp32c3_twai_config->handler(priv->config, esp32c3_twai_config->arg);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: twai_detach
+ *
+ * Description:
+ *   Attach SJA1000 instance to interrupt.
+ *
+ * Input Parameters:
+ *   config - Configuration instance of the SJA1000 driver to attach
+ *   handler - interrupt callback hander
+ *   arg - argument to register for passing to interrupt handler.
+ *
+ * Returned Value:
+ *   OK on success or negated error value.
+ *
+ ****************************************************************************/
+
+static int twai_attach(struct sja1000_config_s *config,
+                          sja1000_handler_t handler, void *arg)
+{
+  struct can_dev_s *dev = (struct can_dev_s *)arg;
+  struct sja1000_dev_s *priv = dev->cd_priv;
+  irqstate_t flags;
+  int devno = config->port;
+  DEBUGASSERT(devno < ESP32C3_TWAI_NUM_PORTS);
   int ret;
+  struct esp32c3_twai_config_s *esp32c3_twai_config =
+      &g_esp32c3_twai_config[devno];
+
+  DEBUGASSERT(esp32c3_twai_config);
+
+  flags = enter_critical_section();
+
+  cantrace("Saving handler %p, arg %p for port %" PRId8 ".\n",
+      handler, arg, devno);
 
-  /* Call esp32c3_twaiinitialize() to get an instance of the TWAI0
-   * interface
-   * */
+  esp32c3_twai_config->handler = handler;
+  esp32c3_twai_config->arg = arg;
+
+  if (config->cpuint >= 0)
+    {
+      /* Disable the provided CPU Interrupt to configure it. */
+
+      up_disable_irq(config->irq);
+    }
 
-  twai = esp32c3_twaiinitialize(TWAI_PORT0);
-  if (twai == NULL)
+  config->cpuint = esp32c3_setup_irq(
+      config->periph, ESP32C3_INT_PRIO_DEF, ESP32C3_INT_LEVEL);
+  if (config->cpuint < 0)
     {
-      canerr("ERROR:  Failed to get TWAI0 interface\n");
-      return -ENODEV;
+      /* Failed to allocate a CPU interrupt of this type. */
+
+      ret = config->cpuint;
+      leave_critical_section(flags);
+
+      return ret;
     }
 
-  /* Register the TWAI0 driver at "/dev/can0" */
+  ret = irq_attach(config->irq, esp32c3_twai_interrupt, priv);
 
-  ret = can_register("/dev/can0", twai);
-  if (ret < 0)
+  if (ret != OK)
     {
-      canerr("ERROR: TWAI0 register failed: %d\n", ret);
+      /* Failed to attach IRQ, so CPU interrupt must be freed. */
+
+      canerr("Failed to initialize device %d.\n", devno);
+      esp32c3_teardown_irq(config->periph, config->cpuint);
+      config->cpuint = -ENOMEM;
+      leave_critical_section(flags);
+
       return ret;
     }
 
+  /* Enable the CPU interrupt that is linked to the TWAI device. */
+
+  up_enable_irq(config->irq);
+
+  leave_critical_section(flags);
+
   return OK;
-#else
-  return -ENODEV;
-#endif
 }
 
-#endif /* CONFIG_CAN */
+/****************************************************************************
+ * Name: twai_detach
+ *
+ * Description:
+ *   Detach from interrupt.
+ *
+ * Input Parameters:
+ *   config - Configuration instance of the SJA1000 driver to detach
+ *
+ * Returned Value:
+ *   OK on success or negated error value.
+ *
+ ****************************************************************************/
+
+static int twai_detach(struct sja1000_config_s *config)
+{
+  irqstate_t flags;
+  int devno = config->port;
+  DEBUGASSERT(devno < ESP32C3_TWAI_NUM_PORTS);
+  struct esp32c3_twai_config_s *esp32c3_twai_config =
+      &g_esp32c3_twai_config[devno];
+
+  DEBUGASSERT(esp32c3_twai_config);
+
+  flags = enter_critical_section();
+
+  cantrace("Detaching interrupts for port %" PRId8 ".\n", devno);
+
+  esp32c3_twai_config->handler = NULL;
+  esp32c3_twai_config->arg     = NULL;
+
+  if (config->cpuint >= 0)
+    {
+      /* Disable cpu interrupt */
+
+      up_disable_irq(config->irq);
+
+      /* Dissociate the IRQ from the ISR */
+
+      irq_detach(config->irq);
+
+      config->cpuint = -ENOMEM;
+    }
+
+  leave_critical_section(flags);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: twai_getreg
+ *
+ * Description:
+ *   Read the value of an TWAI register.
+ *
+ * Input Parameters:
+ *   priv - sja1000 lower-half driver context
+ *   reg - The register address to read
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+static uint32_t twai_getreg(struct sja1000_dev_s *sja_priv,
+      uint32_t reg)
+{
+  uint32_t value;
+  uint32_t mem_addr = (sja_priv->base + (reg << 2));
+
+  /* Read the value from the register */
+
+  value = getreg32(mem_addr);
+#ifdef CONFIG_CANBUS_REGDEBUG
+  cantrace("%08" PRIx32 " -> %08" PRIx32 "\n", mem_addr, value);
+#endif /* CONFIG_CANBUS_REGDEBUG */
+  return value;
+}
+
+/****************************************************************************
+ * Name: twai_putreg
+ *
+ * Description:
+ *   Set the value of an TWAI register.
+ *
+ * Input Parameters:
+ *   priv - sja1000 lower-half driver context
+ *   reg - The register address to write
+ *   value - The value to write to the register
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void twai_putreg(
+    struct sja1000_dev_s *sja_priv, uint32_t reg, uint32_t value)
+{
+  uint32_t mem_addr = (sja_priv->base + (reg << 2));
+
+#ifdef CONFIG_CANBUS_REGDEBUG
+  /* Show the register value being written */
+
+  cantrace("%08" PRIx32 " <- %08" PRIx32 "\n", mem_addr, value);
+#endif /* CONFIG_CANBUS_REGDEBUG */
+
+  /* Write the value */
+
+  putreg32(value, mem_addr);
+}
+
+/****************************************************************************
+ * Name: twai_modifyreg32
+ *
+ * Description:
+ *   Modify the value of an TWAI register.
+ *
+ * Input Parameters:
+ *   priv - sja1000 lower-half driver context
+ *   reg - The address to the register to modify
+ *   clearbits - Bitmask of the bits to clear in a register
+ *   setbits - Bitmask of the bits to set in a register
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void twai_modifyreg32(
+      struct sja1000_dev_s *sja_priv, uint32_t reg, uint32_t clearbits,
+      uint32_t setbits)
+{
+  uint32_t mem_addr = (sja_priv->base + (reg << 2));
+
+#ifdef CONFIG_CANBUS_REGDEBUG
+  /* Show the register value being modified */
+
+  cantrace("%08" PRIx32" <- %08" PRIx32 ", %08" PRIx32 "\n",
+          mem_addr, clearbits, setbits);
+#endif /* CONFIG_CANBUS_REGDEBUG */
+
+  /* Write the value */
+
+  modifyreg32(mem_addr, clearbits, setbits);
+}
+

Review Comment:
   Ack



##########
boards/risc-v/esp32c3-legacy/common/src/esp32c3_board_twai.c:
##########
@@ -24,78 +27,615 @@
 
 #include <nuttx/config.h>
 
-#include <errno.h>
+#include <assert.h>
 #include <debug.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-#include <nuttx/can/can.h>
 #include <arch/board/board.h>
+#include <nuttx/can/can.h>
+#include <nuttx/irq.h>
 
 #include "chip.h"
-/* #include "arm_arch.h" */
 
-#include "esp32c3_twai.h"
+#include <nuttx/can/sja1000.h>
+
+#include <nuttx/signal.h>
+
+#include "riscv_internal.h"
+
+#include "esp32c3_clockconfig.h"
+#include "esp32c3_gpio.h"
+#include "esp32c3_irq.h"
+
+#include "hardware/esp32c3_gpio_sigmap.h"
+#include "hardware/esp32c3_system.h"
 
 #include "esp32c3_board_twai.h"
 
-#ifdef CONFIG_CAN
+#ifdef CONFIG_ESP32C3_TWAI
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Configuration ************************************************************/
+/* Sampling of BUS 3 times */
+
+#  ifndef CONFIG_CAN_SJA1000_SAM
+#    define CONFIG_CAN_SJA1000_SAM  false
+#  endif
+
+/* Debug ********************************************************************/
+
+/* Non-standard debug that may be enabled just for testing SJA1000 */
 
-#define TWAI_PORT0 0
+#ifndef CONFIG_DEBUG_CAN_INFO
+#  undef CONFIG_CANBUS_REGDEBUG
+#endif
+
+#if defined(CONFIG_CAN_SJA1000_DEBUG)
+#  define cantrace _info
+#else
+#  define cantrace _none
+#endif /* CONFIG_CAN_SJA1000_DEBUG */
 
 /****************************************************************************
- * Public Functions
+ * Private Types
  ****************************************************************************/
 
+struct esp32c3_twai_config_s
+{
+  /* Configuration structure as seen by the SJA1000 driver */
+
+  struct sja1000_config_s config;
+
+  /* Additional private definitions only known to this driver */
+
+  struct sja1000_dev_s *handle; /* The SJA1000 driver handle */
+  sja1000_handler_t handler;    /* The SJA1000 interrupt handler */
+  void *arg;                    /* Argument to pass interrupt handler */
+};
+
 /****************************************************************************
- * Name: board_twai_setup
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* IRQ access callbacks.  These operations all hidden behind callbacks
+ * to isolate the SJA1000 driver from differences in interrupt handling
+ * by varying boards and MCUs.
+ *
+ *   twai_attach  - Attach the TWAI interrupt handler to the interrupt
+ *   twai_detach  - Detach the TWAI interrupt handler from interrupt
+ */
+
+static int twai_attach(struct sja1000_config_s *config,
+                           sja1000_handler_t handler, void *arg);
+
+static int twai_detach(struct sja1000_config_s *config);
+
+static uint32_t twai_getreg(struct sja1000_dev_s *sja_priv,
+                               uint32_t reg);
+
+static void twai_putreg(struct sja1000_dev_s *sja_priv,
+                           uint32_t reg, uint32_t value);
+
+static void twai_modifyreg32(struct sja1000_dev_s *sja_priv,
+      uint32_t reg, uint32_t clearbits, uint32_t setbits);
+
+static int twai_init_gpio(FAR struct sja1000_config_s *config);
+
+static int board_twai_initialise(int devno);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* CAN hardware-dependent bit-timing constant
+ * Used for calculating and checking bit-timing parameters
+ */
+
+static const struct can_bittiming_const_s esp32c3_twai_bittiming_const =
+{
+  .tseg1_min = 1,
+  .tseg1_max = 16,
+  .tseg2_min = 1,
+  .tseg2_max = 8,
+  .sjw_max   = 3,
+  .brp_min   = 1,
+  .brp_max   = 64,
+  .brp_inc   = 1,
+};
+
+/* A reference to a structure of this type must be passed to the SJA1000
+ * driver.  This structure provides information about the configuration
+ * of the SJA1000 and provides some board-specific hooks.
+ *
+ * Memory for this structure is provided by the caller.  It is not copied
+ * by the driver and is presumed to persist while the driver is active. The
+ * memory must be writable because, under certain circumstances, the driver
+ * may modify frequency or X plate resistance values.
+ */
+
+static struct esp32c3_twai_config_s
+    g_esp32c3_twai_config[] =
+{
+#ifdef CONFIG_ESP32C3_TWAI0
+    {
+      .config =
+        {
+          .bittiming_const = &esp32c3_twai_bittiming_const,
+          .port     = 0,
+          .periph   = ESP32C3_PERIPH_TWAI,
+          .irq      = ESP32C3_IRQ_TWAI,
+          .cpuint   = -ENOMEM,
+          .bitrate  = CONFIG_CAN_SJA1000_BITRATE,
+          .samplep  = CONFIG_CAN_SJA1000_SAMPLEP,
+          .sjw      = CONFIG_CAN_SJA1000_SJW,
+#ifdef CONFIG_CAN_LOOPBACK
+          .loopback = true,
+#else
+          .loopback = false,
+#endif /* #ifdef CONFIG_CAN_LOOPBACK */
+          .triple_sample = CONFIG_CAN_SJA1000_SAM,
+          .attach        = twai_attach,
+          .detach        = twai_detach,
+        },
+    },
+#endif /* #ifdef CONFIG_ESP32C3_TWAI0 */
+#ifdef CONFIG_ESP32C3_TWAI1
+#  error "TODO - TWAI1 not yet defined"
+#endif /* #ifdef CONFIG_ESP32C3_TWAI1 */
+};
+
+static struct sja1000_dev_s g_twai_priv[] =
+{
+#ifdef CONFIG_ESP32C3_TWAI0
+    {
+      .getreg = twai_getreg,
+      .putreg = twai_putreg,
+      .modifyreg32 = twai_modifyreg32,
+      .config = &g_esp32c3_twai_config[0].config,
+      .base = DR_REG_TWAI_BASE,
+    },
+#endif /* #ifdef CONFIG_ESP32C3_TWAI0 */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_twai_interrupt
  *
  * Description:
- *   Configure the TWAI driver.
+ *   This is the TWAI Interrupt handler.
  *
  * Input Parameters:
- *   None.
+ *   irq - Interrupt number
+ *   context - interrupt context
+ *   arg - pointer to sja1000_dev_s (lower CAN driver) context.
  *
  * Returned Value:
- *   Zero (OK) is returned on success; A negated errno value is returned
- *   to indicate the nature of any failure.
+ *   OK on success or negated error value.
  *
  ****************************************************************************/
 
-int board_twai_setup(void)
+int esp32c3_twai_interrupt(int irq, void *context, void *arg)
 {
-#ifdef CONFIG_ESP32C3_TWAI0
-  struct can_dev_s *twai;
+  struct sja1000_dev_s *priv = (struct sja1000_dev_s *)arg;
+  struct esp32c3_twai_config_s *esp32c3_twai_config
+      = &g_esp32c3_twai_config[priv->config->port];
+
+  cantrace("Entered for CAN port %" PRId8 ".\n", priv->config->port);
+  DEBUGASSERT(priv != NULL);
+  DEBUGASSERT(esp32c3_twai_config != NULL);
+  DEBUGASSERT(esp32c3_twai_config->handler != NULL);
+  DEBUGASSERT(priv->config != NULL);
+  DEBUGASSERT(esp32c3_twai_config->arg);
+
+  esp32c3_twai_config->handler(priv->config, esp32c3_twai_config->arg);
+
+  return OK;
+}
+

Review Comment:
   I am not sure why there is required so much indirection. I would expect that we can directly register common `sja1000_interrupt` there. It should receive  `struct sja1000_dev_s` as argument and this function should be  registered directly as ISR If you need some specific/extended config for some board then it should be defined to contain common one and can be extended in board specific handler wrapper by `container_of`.
   
   On the other hand I agree with attach and detach and ability to setup there some integration specific `sja1000_interrupt` wrapper. But I would use same arguments for common SJA1000 interrupt ISR as for the wrappers.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rpesina/esp32c3 twai [nuttx]

Posted by "ppisa (via GitHub)" <gi...@apache.org>.
ppisa commented on code in PR #11913:
URL: https://github.com/apache/nuttx/pull/11913#discussion_r1529844017


##########
include/nuttx/can/sja1000.h:
##########
@@ -0,0 +1,236 @@
+/****************************************************************************
+ * include/nuttx/can/sja1000.h
+ *
+ * SJA1000 CAN driver based on esp32c3_twai.h
+ *
+ * License header retained from original source.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_CAN_SJA1000_H
+#define __INCLUDE_NUTTX_CAN_SJA1000_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/* CAN hardware-dependent bit-timing constant
+ *
+ * Used for calculating and checking bit-timing parameters
+ */
+
+struct can_bittiming_const_s
+{
+  char name[16];      /* Name of the CAN controller hardware */

Review Comment:
   It is equivalent to my original ScketCAN proposal. It has next reason, if in (Linux) kernel bitrate computation is not used, trusted and some external application takes information over IOCTL, does certified parameters computation and then sets raw bittiming parameters back to the chip, then some carmakers has specific certified combinations for each controller used in the car network. The field allows to check that actual enumerated interfaces matches one of these rules.
   
   If  can_bittiming_const_s instance for specific controller is defined as const, then even for NuttX RAM constrained targets it is not a problem. But I personally do not insist on this field for NuttX. On the other hand 1:1 match to Linux is good.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rpesina/esp32c3 twai [nuttx]

Posted by "xiaoxiang781216 (via GitHub) via commits" <co...@nuttx.apache.org>.
xiaoxiang781216 closed pull request #11913: Rpesina/esp32c3 twai
URL: https://github.com/apache/nuttx/pull/11913


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org