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

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

commit 7e99c9582bc2ea7269348f1fee1f49f6ad48d1dc
Author: p-szafonimateusz <p-szafonimate...@xiaomi.com>
AuthorDate: Thu Mar 13 12:57:34 2025 +0100

    boards/sim: add CAN support and configuration
    
    Add host CAN support for sim target.
    
    Signed-off-by: p-szafonimateusz <p-szafonimate...@xiaomi.com>
---
 .../platforms/sim/sim/boards/sim/index.rst         | 60 ++++++++++++++++++++++
 Documentation/platforms/sim/sim/index.rst          | 15 ++++++
 boards/sim/sim/sim/Kconfig                         | 20 ++++++++
 boards/sim/sim/sim/configs/can/defconfig           | 52 +++++++++++++++++++
 boards/sim/sim/sim/src/sim_bringup.c               | 16 ++++++
 5 files changed, 163 insertions(+)

diff --git a/Documentation/platforms/sim/sim/boards/sim/index.rst 
b/Documentation/platforms/sim/sim/boards/sim/index.rst
index efa8673ab4..676969c5a4 100644
--- a/Documentation/platforms/sim/sim/boards/sim/index.rst
+++ b/Documentation/platforms/sim/sim/boards/sim/index.rst
@@ -1886,6 +1886,66 @@ NOTES:
   you will find this annoying.  You can disable the password protection
   by de-selecting CONFIG_NSH_CONSOLE_LOGIN=y.
 
+can
+---
+
+This is a configuration with simulated CAN support. Both CAN character driver
+and SocketCAN are enabled and use the host ``vcan0`` interface.
+The ``vcan0`` host interface must be available when NuttX is started.
+
+For the CAN character device, there is ``examples/can`` application enabled in
+read-only mode.
+
+Additionally, SocketCAN ``candump`` and ``cansend`` utils are enabled.
+
+Below is an example of receiving CAN frames from host to NuttX.
+Requirement: ``cansequence`` tool from ``linux-can/can-utils``
+
+1. Create virtual CAN on host::
+
+     ip link add dev can0 type vcan
+     ifconfig can0 up
+
+2. Run NuttX::
+
+     ./nuttx
+
+3. Bring up can0 on NuttX::
+
+     nsh> ifup can0
+     ifup can0...OK
+
+4. read CAN messages from SocketCAN on NuttX::
+
+     nsh> candump can0
+
+5. send CAN messages from host to NuttX::
+
+     $ cansequence can0
+
+6. frames from host should be received on NuttX::
+
+     nsh> candump can0
+     can0  002   [1]  00
+     can0  002   [1]  01
+     can0  002   [1]  02
+     can0  002   [1]  03
+     can0  002   [1]  04
+     can0  002   [1]  05
+     can0  002   [1]  06
+     can0  002   [1]  07
+     can0  002   [1]  08
+     can0  002   [1]  09
+     can0  002   [1]  0A
+     can0  002   [1]  0B
+     can0  002   [1]  0C
+     can0  002   [1]  0D
+     can0  002   [1]  0E
+     can0  002   [1]  0F
+     can0  002   [1]  10
+     can0  002   [1]  11
+     can0  002   [1]  12
+
 README.txt
 ==========
 
diff --git a/Documentation/platforms/sim/sim/index.rst 
b/Documentation/platforms/sim/sim/index.rst
index 286243029b..38b0fb8fd7 100644
--- a/Documentation/platforms/sim/sim/index.rst
+++ b/Documentation/platforms/sim/sim/index.rst
@@ -103,6 +103,21 @@ You should see some squares in different colors displayed 
in remmina:
 
    remmina connected to sim's VNC Server
 
+Running Simulated CAN
+=====================
+
+The simulator supports CAN support via SocketCAN on the host.
+The CAN interface of the host must be properly configured::
+
+  ip link set can0 type can bitrate 1000000
+  ip link set can0 up
+
+Virtual CAN interface can be used as well::
+
+  ip link add dev can0 type vcan
+  ifconfig can0 up
+
+
 Supported Boards
 ================
 
diff --git a/boards/sim/sim/sim/Kconfig b/boards/sim/sim/sim/Kconfig
index d68906cbd3..00ff7908cc 100644
--- a/boards/sim/sim/sim/Kconfig
+++ b/boards/sim/sim/sim/Kconfig
@@ -63,3 +63,23 @@ config SIM_RNDIS_MACADDR
                with this selection.
 
 endif
+
+if SIM_CANDEV_CHAR
+
+config SIM_CANDEV_CHAR_IDX
+       int "CAN chardev interface"
+       default 0
+       ---help---
+               Host SocketCAN network interface used by simulated CAN 
character driver
+
+endif
+
+if SIM_CANDEV_SOCK
+
+config SIM_CANDEV_SOCK_IDX
+       int "CAN SocketCAN interface"
+       default 0
+       ---help---
+               Host SocketCAN network interface used by simulated SocketCAN
+
+endif
diff --git a/boards/sim/sim/sim/configs/can/defconfig 
b/boards/sim/sim/sim/configs/can/defconfig
new file mode 100644
index 0000000000..670ffe5b95
--- /dev/null
+++ b/boards/sim/sim/sim/configs/can/defconfig
@@ -0,0 +1,52 @@
+#
+# This file is autogenerated: PLEASE DO NOT EDIT IT.
+#
+# You can use "make menuconfig" to make any modifications to the installed 
.config file.
+# You can then do "make savedefconfig" to generate a new defconfig file that 
includes your
+# modifications.
+#
+CONFIG_ALLOW_BSD_COMPONENTS=y
+CONFIG_ARCH="sim"
+CONFIG_ARCH_BOARD="sim"
+CONFIG_ARCH_BOARD_SIM=y
+CONFIG_ARCH_CHIP="sim"
+CONFIG_ARCH_SIM=y
+CONFIG_BOARDCTL_POWEROFF=y
+CONFIG_BOARD_LOOPSPERMSEC=0
+CONFIG_BOOT_RUNFROMEXTSRAM=y
+CONFIG_BUILTIN=y
+CONFIG_CAN=y
+CONFIG_CANUTILS_CANDUMP=y
+CONFIG_CANUTILS_CANSEND=y
+CONFIG_CANUTILS_LIBCANUTILS=y
+CONFIG_CAN_EXTID=y
+CONFIG_CAN_FD=y
+CONFIG_DEBUG_SYMBOLS=y
+CONFIG_EXAMPLES_CAN=y
+CONFIG_EXAMPLES_CAN_READ=y
+CONFIG_FS_PROCFS=y
+CONFIG_HAVE_CXXINITIALIZE=y
+CONFIG_IDLETHREAD_STACKSIZE=4096
+CONFIG_INIT_ENTRYPOINT="nsh_main"
+CONFIG_NET=y
+CONFIG_NETDEV_IFINDEX=y
+CONFIG_NET_CAN=y
+CONFIG_NET_CAN_ERRORS=y
+CONFIG_NET_CAN_EXTID=y
+CONFIG_NET_CAN_SOCK_OPTS=y
+CONFIG_NSH_ARCHINIT=y
+CONFIG_NSH_BUILTIN_APPS=y
+CONFIG_NSH_DISABLE_PS=y
+CONFIG_NSH_READLINE=y
+CONFIG_READLINE_TABCOMPLETION=y
+CONFIG_SCHED_BACKTRACE=y
+CONFIG_SCHED_EVENTS=y
+CONFIG_SCHED_HAVE_PARENT=y
+CONFIG_SCHED_WAITPID=y
+CONFIG_SIM_CANDEV=y
+CONFIG_SIM_CANDEV_CHAR=y
+CONFIG_SIM_CANDEV_SOCK=y
+CONFIG_START_MONTH=6
+CONFIG_START_YEAR=2008
+CONFIG_SYSTEM_DUMPSTACK=y
+CONFIG_SYSTEM_NSH=y
diff --git a/boards/sim/sim/sim/src/sim_bringup.c 
b/boards/sim/sim/sim/src/sim_bringup.c
index 7366d04424..070e287e50 100644
--- a/boards/sim/sim/sim/src/sim_bringup.c
+++ b/boards/sim/sim/sim/src/sim_bringup.c
@@ -553,5 +553,21 @@ int sim_bringup(void)
   usbdev_rndis_initialize(mac);
 #endif
 
+#ifdef CONFIG_SIM_CANDEV_CHAR
+  ret = sim_canchar_initialize(CONFIG_SIM_CANDEV_CHAR_IDX, 0);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: sim_canchar_initialize() failed: %d\n", ret);
+    }
+#endif
+
+#ifdef CONFIG_SIM_CANDEV_SOCK
+  ret = sim_cansock_initialize(CONFIG_SIM_CANDEV_SOCK_IDX);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: sim_cansock_initialize() failed: %d\n", ret);
+    }
+#endif
+
   return ret;
 }

Reply via email to