Esoterical / voron_canbus

GNU General Public License v3.0
419 stars 59 forks source link

Suggested update to "Getting Started" section systemd-networkd (netplan) #83

Open clembo1 opened 4 days ago

clembo1 commented 4 days ago

The following link has some additional instructions on using Systemd/Network to configure CAN.
https://www.pragmaticlinux.com/2021/07/automatically-bring-up-a-socketcan-interface-on-boot/

Background:

  1. Just configuring 10-can.link and 25-can.network does not guarantee that the canbus will be started automatically. I tested and confirmed that nothing auto-started even post reboot. Additional steps are required; see URL above for details.
  2. On Armbian - Debian and Ubuntu distros, ifupdown and net-tools are not installed by default since Netplan is used by the distros. This results in missing folders & files for /etc/network/. While users should know linux, hunting down what packages are needed can be frustrating while to try following existing instructions for ifupdown section. Note: Installing the packages is easy but this option may or may not be available in the future (due to deprecation) based on what I read.
  3. You can use both NetworkManager (netplan) and systemd-networkd at the same time just as long as you don't try to control the same network device through both interfaces. Debian, Ubuntu and Fedora install systemd-networkd by default. The installer just leaves it inactive.

Instructions that worked for me section systemd-networkd (netplan) | copied in part from the url

  1. Pre-req's -- you have already got your mainboard flashed. CAN shows up in lsusb. In my case it is: Bus 002 Device 004: ID 1d50:606f OpenMoko, Inc. Geschwister Schneider CAN adapter

  2. Complete the 10-can.link and 25-can.network configurations in the original instructions. Note: I added an additional line on 25-can.network to included RestartSec=100ms. [Match] Name=can* [CAN] BitRate=1M RestartSec=100ms

  3. Load the SocketCAN kernel modules Before we can bring the can0 SocketCAN network interface in the UP state, we need to first load the SocketCAN related kernel modules. Open up the terminal again and run these commands:

    sudo modprobe can sudo modprobe can_raw To verify that the SocketCAN related kernel modules loaded properly, you can run lsmod | grep "can".

Should get something like this: voron@bigtreetech-cb1:~$ lsmod | grep "can" can_dev 36864 1 gs_usb can_raw 20480 0 can 20480 1 can_raw

  1. Configure and bring up the SocketCAN network interface

With the USB-to-CAN adapter plugged in and the SocketCAN kernel modules loaded, we continue with configuring the can0 SocketCAN network interface. note: you may or may not need the canbus cable plugged into the toolhead board. It was not necessary for me with the M8P v2.0 board. The command for this is:

`sudo ip link set can0 type can bitrate 1000000 restart-ms 100`

Note: the default with current instructions does not set the restart timeout, not sure if 100 milliseconds is needed or not This configures the can0 network interface for a CAN communication speed of 1,000,000 bits/second. Feel free to adjust this to match the CAN communication speed on your CAN bus. Furthermore, it configures can0 to automatically re-initialize the CAN controller in case of a CAN bus off event, after 100 milliseconds. Feel free to adjust this if needed, but 100 milliseconds works fine in most cases.

With the can0 SocketCAN network interface configured, we can continue with the last step, which is bringing it in the UP state:

sudo ip link set up can0

At this point you can run the command below to confirm the state ip -s -d link show can0 Result should be something like this (bold is mine):

3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1024 link/can promiscuity 0 allmulti 0 minmtu 0 maxmtu 0 can state ERROR-ACTIVE restart-ms 100 bitrate 1000000 sample-point 0.750 tq 62 prop-seg 5 phase-seg1 6 phase-seg2 4 sjw 2 brp 3 gs_usb: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..1024 brp_inc 1 clock 48000000 re-started bus-errors arbit-lost error-warn error-pass bus-off 0 0 0 0 0 0 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 parentbus usb parentdev 2-1.4:1.0 RX: bytes packets errors dropped missed mcast 0 0 0 0 0 0 TX: bytes packets errors dropped carrier collsns 0 0 0 0 0 0

Note: If you restart, the CAN will be in state "down."To solve this, we need to take an additional step to automatically put state UP on reboot

  1. Automatically bring up the SocketCAN network interface From the terminal run the following command to create this file and to edit it with the Nano terminal text editor sudo nano /etc/modules-load.d/can.conf

Next, enter the can and can_raw kernel modules each on their own line: can can_raw

exit and save changes

Before we move on with the configuration of our SocketCAN network interface, we need to first activate the systemd-networkd service. Run the following two commands from the terminal:

sudo systemctl start systemd-networkd
sudo systemctl enable systemd-networkd

This starts the systemd-networkd service and makes sure it also starts the next time your boot your Linux system.

To enable this configuration file for our can0 SocketCAN network interface, we just need to restart the systemd-networkd service:

sudo systemctl restart systemd-networkd

Run the command below to confirm the state "UP" ip -s -d link show can0 (optional, "sudo reboot" then run the above command to confirm state is "UP"

  1. That's it
Esoterical commented 1 day ago

Appreciate the info. This network management issue has been known for a bit, but I don't have any CB1/BTT Pi to test with so I wasn't confident in adding any info without personally testing. I've only really seen this issue crop up a few times, and unfortunately each time we try to go down to an actual fix the user ends up jsut installing the latest CB1 image from BTT which 'fixes' it.