BB CANOpenBB with Waveshare

The BeagleBone boards are equipped with CAN interfaces.

It is easy to configure the communication based on CANOpen using Codesys.

For the example I will use the BBB Industrial and BB AI-64.

The BBB is preconfigured with two CAN networks, so only can0 and can1 autostart shall be configured.

The BB AI-64 requires more as the CAN networks are not configured as default.

BB CANOpen
BBB and BB AI-64 connected with CAN interface.

BBB Industrial CAN Configuration

Add following commands to the /etc/network/interfaces file:

auto can0
iface can0 inet manual
    pre-up /sbin/ip link set can0 type can bitrate 50000 triple-sampling on restart-ms 100
    up /sbin/ifconfig can0 up
    down /sbin/ifconfig can0 down

auto can1
iface can1 inet manual
    pre-up /sbin/ip link set can1 type can bitrate 50000 triple-sampling on restart-ms 100
    up /sbin/ifconfig can1 up
    down /sbin/ifconfig can1 down

BBAI64 CAN Configuration

BB AI-64 doesn’t have the CAN interface configured as default, so first we need setup DTS overlay for CAN.

Create two files:

BONE-CAN0.dts

/dts-v1/;
/plugin/;

/*
 * Helper to show loaded overlays under: /proc/device-tree/chosen/overlays/
 */
&{/chosen} {
	overlays {
		BONE-CAN0.kernel = __TIMESTAMP__;
	};
};

/* Change pinmux*/

&main_mcan0 {
	pinctrl-names = "default";
	pinctrl-0 = <
		&P9_19_can_pin /* mcan0_rxd */
		&P9_20_can_pin /* mcan0_txd */
	>;
	symlink = "bone/can/1";
	status = "okay";
};

and BONE-CAN4.dts

/dts-v1/;
/plugin/;

/*
 * Helper to show loaded overlays under: /proc/device-tree/chosen/overlays/
 */
&{/chosen} {
    overlays {
		BONE-CAN4.kernel = __TIMESTAMP__;
    };
};

/* Change pinmux*/

&main_mcan4 {
    pinctrl-names = "default";
    pinctrl-0 = <
		&P9_24_can_pin /* mcan4_rxd */
		&P9_26_can_pin /* mcan4_txd */
    >;
    symlink = "bone/can/4";
    status = "okay";
};

Copy the files to /opt/source/dtb-5.10-ti-arm64/src/arm64/overlays (the dir dtb-5.10-ti-arm64 may be different for different distributions).

Add the BONE-CAN0.dtbo and BONE-CAN4.dtbo to the /opt/source/dtb-5.10-ti-arm64/src/arm64/overlays/Makefile

Makefile

In the terminal go to the /opt/source/dtb-5.10-ti-arm64 make and install CAN overlays.

debian@BeagleBone:~$ cd /opt/source/dtb-5.10-ti-arm64
debian@BeagleBone:/opt/source/dtb-5.10-ti-arm64$ make
debian@BeagleBone:/opt/source/dtb-5.10-ti-arm64$ sudo make install

Enable the CAN overlays adding to the /boot/firmware/extlinux/extlinux.conf

    fdtoverlays /overlays/BONE-CAN0.dtbo /overlays/BONE-CAN4.dtbo

Add following commands to the /etc/network/interfaces file:

auto can0
iface can0 inet manual
	pre-up /sbin/ip link set can0 type can bitrate 500000
	up /sbin/ifconfig can0 up
	down /sbin/ifconfig can0 down

auto can1
iface can1 inet manual
	pre-up /sbin/ip link set can1 type can bitrate 500000
	up /sbin/ifconfig can1 up
	down /sbin/ifconfig can1 down

Restart BBB and BB AI-64, run the command ip a from terminal.

On both modules you should see information about CAN interfaces.

can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UP group default qlen 10
    link/can
can1: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UP group default qlen 10
    link/can

The connection diagram

So we have configured CAN interfaces. Now we shall connect the BBB and BB AI-64 with CAN transceivers. I’m using Waveshare CAN SN65HVD230 UART 3.3V TRANSCEIVERS.

WS CAN Transceiver

Connection Diagram
Connection diagram

Now we can check if the CAN connection works correctly. On BeagleBone Black we are using can0 interface. On BeagleBone Ai-64 can1 interface. So we try send CAN frame from BB AI-64 to BBB.

In terminal on BBB type candump can0. In terminal on BB AI-64 type cansend can1 123#DEADBEEF.

We have CAN interfaces working on both BeagleBone boards.

Codesys Configuration

Now we can try to configure the BeagleBone with Codesys.

The BeagleBone Black is configured as CANOpen Master, BeagleBone Ai-64 as Slave.

At the beginning we use PDOs to exchange data between CANOpen Master and Slave.

PDOs Configuration

The PDOs Master Configuration:

The PDOs Slave Configuration:

For the Master and Slave we will implement simple applications.

Slave App

Now we can check how it works…

SDO Read

The PDOs exchange data all the time. To read data from Slave we can use SDOs read frunction.

On the Slave we map Codesys variable to CANOpen Object.

On Master we write simple program to read object 16#300B Index 16#04.

If you want to read more than 4 bytes of data (for example to read string) you can use CIA405.SDO_READ_DATA instead of CIA405.SDO_READ4.

Now we can check how it works.

SDO Write

To write data to the Slave we can use SDO write function.

On Master we write simple program to read object 16#300B Index 16#04.

If you want to write more than 4 bytes of data (for example for string) you can use CIA405.SDO_WRITE_DATA instead of CIA405.SDO_WRITE4.

Now we can check how it works.

By admin