Skip to content

Conversation

@martinjaeger
Copy link
Member

@martinjaeger martinjaeger commented Sep 4, 2022

Background Information

This is a new attempt to add a CAN driver for the native_posix board to interface with a Linux host system using the SocketCAN API. The driver itself is called native_linux, as it supports only Linux hosts and no other POSIX-based systems.

A similar driver was introduced in #37124. However, the CAN implementation in Zephyr changed significantly since that PR, so the driver was pretty much rewritten from scratch. Comments by @gmarull and @alexanderwachter in previous PR are addressed.

Details about the Linux SocketCAN implementation can be found in the Linux kernel CAN documentation and API header file.

Testing

In order to test the driver, a virtual CAN interface has to be set up on the Linux host:

sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
sudo ip link property add dev vcan0 altname zcan0

In addition to that, the following overlay (or update of native_posix.dts) is required:

/ {
	chosen {
		zephyr,canbus = &can0;
	};
};

&can0 {
	status = "okay";
};

Afterwards, the driver passes all CAN driver tests:

scripts/twister -p native_posix -T tests/drivers/can

The driver does currently not pass the ISO-TP tests in tests/subsys/canbus/isotp. The ISO-TP tests are known to be brittle (e.g. they also fail for STM32G4, see #47014) and there is most probably a timing issue in the ISO-TP implementation itself. Fixing that is not within the scope of this PR.

Copy link
Member

@henrikbrixandersen henrikbrixandersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my comment about reworking the binding to the host CAN interface name. You could submit the two first commits in separate PRs and get them approved quickly in order to cut down on the changes here.

@martinjaeger
Copy link
Member Author

The first commit will be removed once #49907 is merged.

Copy link
Member

@jukkar jukkar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Couple of years ago I had some plans to implement this but got side tracked and plans changed.
Anyway, there is a helper script at zephyrproject-rtos/net-tools#26 that I created at that point. It tries to setup canbus things in linux side, similar ways as the net-setup.sh script is doing in networking. Not sure if that script contains anything useful but feel free to re-use it if needed.

Copy link
Member

@henrikbrixandersen henrikbrixandersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. One minor change proposal as discussed on Discord:

Copy link
Member

@henrikbrixandersen henrikbrixandersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@martinjaeger martinjaeger added this to the v3.2.0 milestone Sep 7, 2022
Copy link
Contributor

@str4t0m str4t0m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this addition! I am not very familiar with the socket can api on Linux, but still have a few comments.

@martinjaeger
Copy link
Member Author

Thanks @str4t0m for your very thorough review. I think all your points are valid and I'll come up with a solution latest tomorrow.

@martinjaeger
Copy link
Member Author

The remarks by @str4t0m are now considered. For easier review you can take a look at the changes which were squashed into the commit in this PR here: eef547f

I have also reduced the sleep time in the RX thread from 10ms to 1ms so that we can receive frames with less latency.

Copy link
Contributor

@str4t0m str4t0m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two more comments.

This driver provides an interface to SocketCAN interfaces of the Linux
system running a Zephyr application with the native_posix board. These
interfaces may be virtual or actual CAN buses.

Signed-off-by: Martin Jäger <martin@libre.solar>
@carlescufi carlescufi merged commit 77e4c65 into zephyrproject-rtos:main Sep 9, 2022
@martinjaeger martinjaeger deleted the can-native-linux branch September 13, 2022 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants