How to configure pins for CAN controller (mcp2515) in samples?

Hi, we are going to use the mcp2515 CAN controller in several of our projects, both on the nRF9160 and nRF5340.

As Zephyr supports the DFRobot CAN Shield 2.0, I suppose it should be possible to use the mcp2515 in out own hardware design.

There are two Zephyr sample for CAN (zephyr\samples\drivers\can\counter and zephyr\samples\drivers\can\babbling) supporting the DFRobot CAN Shield 2.0, but I could not find any information about CAN controller hardware configuration (I/O pins) in the samples. No overlay files there.

Then, I found the DFRobot CAN Shield 2.0 definition (zephyr\boards\shields\mcp2515), but I could not find any SPI configuration there either. I found a strange overlay file defining an arduino node:

&arduino_spi {
	status = "okay";
	cs-gpios = <&arduino_header 16 GPIO_ACTIVE_LOW>; /* D10 */

	mcp2515_dfrobot_can_bus_v2_0: can@0 {
		compatible = "microchip,mcp2515";
		spi-max-frequency = <1000000>;
		int-gpios = <&arduino_header 8 GPIO_ACTIVE_LOW>; /* D2 */
		status = "okay";
		reg = <0x0>;
		osc-freq = <16000000>;
		bus-speed = <125000>;
		sjw = <1>;
		sample-point = <875>;

		can-transceiver {
			max-bitrate = <1000000>;
		};
	};
};

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

We do not have Arduino headers on our custom board. How can we use the MCP2515 directly with a normal SPI interface?

And where can I find this kind of information? We will use some more other devices in the near future (like the nRF7002 and a RJ45 based ethernet controller), so I have to adjust pin configurations for them, too.

By the way, it would be very nice to have a Nordic Academy course about device trees. I still find working with them very difficult and time consuming. Also, there seems to be no clean hierarchy and type definitions, which makes understanding them even more difficult. Like in the example above with the Arduino definitions. It would be really bad if I need Arduino pins for the mcp2515 driver to work (what if we want to add a second mcp2515? What if the next driver is also linked to the same Arduino definitions?), instead of being linked to a simple SPI instance node (e.g. &spi2 or so).

Best regards,

Michael

  • Hi,

    The arduino_spi node is normally just a label for one of the other spi nodes in the chip, that is defined in the board devicetree:

    The pin definitions for spi nodes are set in the pinctrl devicetree files.

    You can create your own overlay file where you implement the MCP2515 using the spi node directly if you do not want to define a arduino_spi node in your board files, e.g., something like this:

    &spi3 {
    	status = "okay";
    	cs-gpios = <&arduino_header 16 GPIO_ACTIVE_LOW>; /* D10 */
    
    	mcp2515_dfrobot_can_bus_v2_0: can@0 {
    		compatible = "microchip,mcp2515";
    		spi-max-frequency = <1000000>;
    		int-gpios = <&arduino_header 8 GPIO_ACTIVE_LOW>; /* D2 */
    		status = "okay";
    		reg = <0x0>;
    		osc-freq = <16000000>;
    		bus-speed = <125000>;
    		sjw = <1>;
    		sample-point = <875>;
    
    		can-transceiver {
    			max-bitrate = <1000000>;
    		};
    	};
    };

    Best regards,
    Jørgen

Related