How to use canbus on Zero MCP2515

I am new to radxa zero I want to read and write data on can bus using Candump on lunix how to install and configure it for hardware I am using (https://www.waveshare.com/wiki/RS485_CAN_HAT) connected
how and which SPI should be on for this particular hat

It looks like the SPI_B pins on the R0 header are in the same positions as the SPI0 pins on the RPi0 header. The UART pins look like they line up too. But SPI_A is not pin compatible with SPI1 on the RPi0. If that hat only connects to power, ground, UART, and SPI0, then in theory it should work with the SPI_B overlay.

1 Like

Sorry for late response
Yes i found out the same I have design base pcb around MCP2515 and send for fabrication I’ll update soon if it works

Any progress here? I’ve got an MCP2515 board I designed and built. I’ve enabled the SPI in the overlays, but I don’t know how to tell Ubuntu that the SPI device is an MCP2515.

I am on the same situation i understand that to tell kernel that we have connected mcp2515 we have to make device tree file
@RadxaYuntian has share reference to me you should use this as a base for spi device: https://github.com/radxa/overlays/blob/main/arch/arm64/boot/dts/amlogic/overlays/meson-g12a-spi-b-enc28j60.dts

and reference this for how to configure mcp2515: https://github.com/krzk/tizen-tv-rpi-linux/blob/master/arch/arm/boot/dts/overlays/mcp2515-can0-overlay.dts

  1. ignore broadcom stuff in the 2nd link since zero uses different overlay

I have edited file and it look like this but I don’t know if it work I still have confusion

/dts-v1/;
/plugin/;

/ {
compatible = “radxa,zero”, “amlogic,g12a”;

fragment@0 
{
target-path = "/";

__overlay__ 
        {
	can_mcp2515_osc: can_mcp2515_oscs 
                {
		compatible = "fixed-clock";
		clock-frequency = <12000000>;
		#clock-cells = <0>;
	};
};

};

fragment@1 
{
target = <&gpio>;
__overlay__ 
        {
	
                mcp2515_int_pin: mcp2515_int_pin@0 
                {
                        amlogic,pins = <&gpio 48>;	/* or is it <&gpio 85> ? */
                        amlogic,function = "irq"; /* in - whether "out" (1) or "in" (0) */
                        // amlogic,function = <0>; /* in - whether "out" (1) or "in" (0) */
                        amlogic,pull = <0>;
                };
        };
};

fragment@3
{
target = <&spicc1>;
overlay
{
pinctrl-0 = <&spicc1_pins &spicc1_ss0_pins>;
pinctrl-names = “default”;
#address-cells = <1>;
#size-cells = <0>;
status = “okay”;
mcp2515: spidev@0
{
compatible = “microchip,mcp2515”;
reg = <0>;

           interrupt-parent = <&gpio_intc>;
           interrupts = <60 2>;
           
                            
            spi-max-frequency = <12000000>;
            clocks = <&can_mcp2515_osc>;
            pinctrl-0 = <&mcp2515_int_pin>;
    };
};

};

This is not right. I’ll post one tomorrow for you to try out.

Ok thanks

I have MCP2515 connected to spiB port of zero
connections are follows
MCP2515 <==> Zero Header Pins
MISO -21
MOSI -19
SS - 24
INT - 18
SCLK - 23

Please check if the following overlay works for you. I’m not too sure about how to define external clock on Zero, but the SPI part should be solid:

/dts-v1/;
/plugin/;

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/gpio/meson-g12a-gpio.h>
#include <dt-bindings/interrupt-controller/irq.h>

/ {
	metadata {
		title = "Enable MCP2515 on SPI_B";
		compatible = "radxa,zero";
		category = "misc";
		description = "Provide support for Microchip MCP2515 SPI CAN controller.\nUses Pin 18 (GPIOX_8) for INT.";
	};

	fragment@0 {
		target = <&spicc1>;
		__overlay__ {
			pinctrl-names = "default";
			pinctrl-0 = <&spicc1_pins>;
			#address-cells = <1>;
			#size-cells = <0>;
			cs-gpios = <&gpio GPIOH_6 GPIO_ACTIVE_LOW>;
			status = "okay";

			can0: mcp2515@0 {
				compatible = "microchip,mcp2515";
				reg = <0>;
				spi-max-frequency = <10000000>;

				pinctrl-names = "default";
				pinctrl-0 = <&mcp2515_int_pins>;

				interrupt-parent = <&gpio_intc>;
				interrupts = <85 IRQ_TYPE_EDGE_FALLING>;

				clocks = <&can0_osc>;
			};
		};
	};

	fragment@1 {
		target = <&periphs_pinctrl>;
		__overlay__ {
			mcp2515_int_pins: mcp2515-int-pins@0 {
				mux {
					groups = "GPIOX_8";
					function = "gpio_periphs";
					bias-pull-up;
					drive-strength-microamp = <4000>;
				};
    		};
		};
	};

	fragment@2 {
		target-path = "/";
		__overlay__ {
			can0_osc: can0_osc {
                compatible = "fixed-clock";
                #clock-cells = <0>;
                clock-frequency  = <12000000>;
            };
		};
	};
};
1 Like

Thanks
I’ll update you

rock@radxa-zero:~/test_io$ sudo dtc -@ -I dts -O dtb -o spi-mcp2515.dtbo mcp2515.dts
Error: mcp2515.dts:4.1-9 syntax error
FATAL ERROR: Unable to parse input tree

You need to first pass it thorough C preprocessor to handle the include files. You can reference: https://stackoverflow.com/questions/50658326/device-tree-compiler-not-recognizes-c-syntax-for-include-files

Here is the sample script shared by another user. You can remove unused -I from cpp command:

#!/bin/sh

src_dir=dts-rockchip
dts_file=rk3566-radxa-cm3-io

dts_file_ext=${src_dir}/${dts_file}.dts
echo dts_file_ext: ${dts_file_ext}

KERNEL_MODULE_DIR=/lib/modules/$(uname -r)
echo kernel_dir: ${KERNEL_MODULE_DIR}

KERNEL_INC=${KERNEL_MODULE_DIR}/build/include
echo kernel_inc: ${KERNEL_INC}

cpp -nostdinc -D LINUX_VERSION -I ${KERNEL_INC} -I platform/t210/common/kernel-dts -I soc/t210/kernel-dts -I soc/tegra/kernel-include -undef -x assembler-with-cpp  ${dts_file_ext} ${dts_file}.cpp.dts

# -@:Enable generation of symbols
# -H: epapr  - "phandle" properties only
dtc -@ -Hepapr -I dts -O dtb -p 0x1000 ${dts_file}.cpp.dts -o ${dts_file}.dtb
1 Like