nRF9160: MCP2515 SPI bus not ready

I am trying to use the MCP2515 SPI2CAN adapter with the nRF9160 DK. 

I started by trying to get the Zephyr socket_can sample to work. My efforts are based on this thread.

The problem is, that I am always getting an "SPI bus not ready" error from the can_mcp2515 driver. 

I successfully tested the SPI with a simple loopback example, so the overlay should be correct.

Here is my device tree overlay:

&spi3 {
    status = "okay";
    cs-gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
	sck-pin = <10>;
	mosi-pin = <11>;
	miso-pin = <12>;	
  
    can1: mcp2515@0 {
      compatible = "microchip,mcp2515";
		spi-max-frequency = <1000000>;
		int-gpios = <&gpio0 31 GPIO_ACTIVE_LOW>;
		status = "okay";
		label = "CAN_1";
		reg = <0x0>;
		osc-freq = <10000000>;
		bus-speed = <1000000>;
		sjw = <1>;
		prop-seg = <2>;
		phase-seg1 = <7>;
		phase-seg2 = <6>;
		#address-cells = <1>;
		#size-cells = <0>;
    };
  };

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

prj.conf:

CONFIG_CAN=y
CONFIG_SPI=y
CONFIG_SPI_NRFX=y
CONFIG_CAN_MAX_FILTER=5
CONFIG_CAN_MCP2515=y
CONFIG_SPI_LOG_LEVEL_DBG=y

CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_CAN=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y

CONFIG_NET_IPV6=n
CONFIG_NET_IPV4=n
CONFIG_NET_MGMT=n
CONFIG_NET_TCP=n
CONFIG_NET_UDP=n

CONFIG_NET_DEFAULT_IF_CANBUS_RAW=y

CONFIG_NET_PKT_RX_COUNT=30
CONFIG_NET_PKT_TX_COUNT=30
CONFIG_NET_BUF_RX_COUNT=30
CONFIG_NET_BUF_TX_COUNT=30

CONFIG_TEST_RANDOM_GENERATOR=n

CONFIG_NET_SHELL=y
CONFIG_LOG=y
CONFIG_NET_LOG=y
CONFIG_LOG_MODE_IMMEDIATE=y

CONFIG_NET_SOCKETS_LOG_LEVEL_DBG=y
CONFIG_CAN_LOG_LEVEL_DBG=y
#CONFIG_NET_CONTEXT_LOG_LEVEL_DBG=y
#CONFIG_NET_CORE_LOG_LEVEL_DBG=y
#CONFIG_NET_CONN_LOG_LEVEL_DBG=y

# Use smaller buffers for net_buf as CANBUS packets are quite small.
CONFIG_NET_BUF_DATA_SIZE=64

Output:

Parents
  • Hello Yavor, 

    which NCS version are you currently operating with? I’m asking because the Socket CAN sample with your overlay file won't even build for me in NCS v1.9.1. 

    In file included from /home/user/ncs/zephyr/include/toolchain/gcc.h:72,
                     from /home/user/ncs/zephyr/include/toolchain.h:50,
                     from /home/user/ncs/zephyr/include/kernel_includes.h:19,
                     from /home/user/ncs/zephyr/include/kernel.h:17,
                     from /home/user/ncs/zephyr/drivers/can/can_mcp2515.c:9:
    /home/user/ncs/zephyr/drivers/can/can_mcp2515.c: In function 'socket_can_init':
    /home/user/ncs/zephyr/include/device.h:96:39: error: '__device_dts_ord_DT_N_INST_1_microchip_mcp2515_ORD' undeclared (first use in this function)
       96 | #define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
          |                                       ^~~~~~~~~
    /home/user/ncs/zephyr/include/toolchain/common.h:132:26: note: in definition of macro '_DO_CONCAT'
      132 | #define _DO_CONCAT(x, y) x ## y
          |                          ^
    /home/user/ncs/zephyr/include/device.h:96:31: note: in expansion of macro '_CONCAT'
       96 | #define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
          |                               ^~~~~~~
    /home/user/ncs/zephyr/include/device.h:291:37: note: in expansion of macro 'DEVICE_NAME_GET'
      291 | #define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_NAME(node_id))
          |                                     ^~~~~~~~~~~~~~~
    /home/user/ncs/zephyr/include/device.h:311:34: note: in expansion of macro 'DEVICE_DT_NAME_GET'
      311 | #define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
          |                                  ^~~~~~~~~~~~~~~~~~
    /home/user/ncs/zephyr/include/device.h:323:34: note: in expansion of macro 'DEVICE_DT_GET'
      323 | #define DEVICE_DT_INST_GET(inst) DEVICE_DT_GET(DT_DRV_INST(inst))
          |                                  ^~~~~~~~~~~~~
    /home/user/ncs/zephyr/drivers/can/can_mcp2515.c:945:33: note: in expansion of macro 'DEVICE_DT_INST_GET'
      945 |  const struct device *can_dev = DEVICE_DT_INST_GET(1);
          |                                 ^~~~~~~~~~~~~~~~~~
    /home/user/ncs/zephyr/include/device.h:96:39: note: each undeclared identifier is reported only once for each function it appears in
       96 | #define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
          |                                       ^~~~~~~~~
    /home/user/ncs/zephyr/include/toolchain/common.h:132:26: note: in definition of macro '_DO_CONCAT'
      132 | #define _DO_CONCAT(x, y) x ## y
          |                          ^
    /home/user/ncs/zephyr/include/device.h:96:31: note: in expansion of macro '_CONCAT'
       96 | #define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
          |                               ^~~~~~~
    /home/user/ncs/zephyr/include/device.h:291:37: note: in expansion of macro 'DEVICE_NAME_GET'
      291 | #define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_NAME(node_id))
          |                                     ^~~~~~~~~~~~~~~
    /home/user/ncs/zephyr/include/device.h:311:34: note: in expansion of macro 'DEVICE_DT_NAME_GET'
      311 | #define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
          |                                  ^~~~~~~~~~~~~~~~~~
    /home/user/ncs/zephyr/include/device.h:323:34: note: in expansion of macro 'DEVICE_DT_GET'
      323 | #define DEVICE_DT_INST_GET(inst) DEVICE_DT_GET(DT_DRV_INST(inst))
          |                                  ^~~~~~~~~~~~~
    /home/user/ncs/zephyr/drivers/can/can_mcp2515.c:945:33: note: in expansion of macro 'DEVICE_DT_INST_GET'
      945 |  const struct device *can_dev = DEVICE_DT_INST_GET(1);
          |                                 ^~~~~~~~~~~~~~~~~~
    ninja: build stopped: subcommand failed.
    FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/user/work/VSCode/can_app/build
    The terminal process terminated with exit code: 

    I’m not sure why the compiler throws this error, because the overlay file you are using appears to be correct to me. 

    Regards, 

    Markus 

Reply
  • Hello Yavor, 

    which NCS version are you currently operating with? I’m asking because the Socket CAN sample with your overlay file won't even build for me in NCS v1.9.1. 

    In file included from /home/user/ncs/zephyr/include/toolchain/gcc.h:72,
                     from /home/user/ncs/zephyr/include/toolchain.h:50,
                     from /home/user/ncs/zephyr/include/kernel_includes.h:19,
                     from /home/user/ncs/zephyr/include/kernel.h:17,
                     from /home/user/ncs/zephyr/drivers/can/can_mcp2515.c:9:
    /home/user/ncs/zephyr/drivers/can/can_mcp2515.c: In function 'socket_can_init':
    /home/user/ncs/zephyr/include/device.h:96:39: error: '__device_dts_ord_DT_N_INST_1_microchip_mcp2515_ORD' undeclared (first use in this function)
       96 | #define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
          |                                       ^~~~~~~~~
    /home/user/ncs/zephyr/include/toolchain/common.h:132:26: note: in definition of macro '_DO_CONCAT'
      132 | #define _DO_CONCAT(x, y) x ## y
          |                          ^
    /home/user/ncs/zephyr/include/device.h:96:31: note: in expansion of macro '_CONCAT'
       96 | #define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
          |                               ^~~~~~~
    /home/user/ncs/zephyr/include/device.h:291:37: note: in expansion of macro 'DEVICE_NAME_GET'
      291 | #define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_NAME(node_id))
          |                                     ^~~~~~~~~~~~~~~
    /home/user/ncs/zephyr/include/device.h:311:34: note: in expansion of macro 'DEVICE_DT_NAME_GET'
      311 | #define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
          |                                  ^~~~~~~~~~~~~~~~~~
    /home/user/ncs/zephyr/include/device.h:323:34: note: in expansion of macro 'DEVICE_DT_GET'
      323 | #define DEVICE_DT_INST_GET(inst) DEVICE_DT_GET(DT_DRV_INST(inst))
          |                                  ^~~~~~~~~~~~~
    /home/user/ncs/zephyr/drivers/can/can_mcp2515.c:945:33: note: in expansion of macro 'DEVICE_DT_INST_GET'
      945 |  const struct device *can_dev = DEVICE_DT_INST_GET(1);
          |                                 ^~~~~~~~~~~~~~~~~~
    /home/user/ncs/zephyr/include/device.h:96:39: note: each undeclared identifier is reported only once for each function it appears in
       96 | #define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
          |                                       ^~~~~~~~~
    /home/user/ncs/zephyr/include/toolchain/common.h:132:26: note: in definition of macro '_DO_CONCAT'
      132 | #define _DO_CONCAT(x, y) x ## y
          |                          ^
    /home/user/ncs/zephyr/include/device.h:96:31: note: in expansion of macro '_CONCAT'
       96 | #define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
          |                               ^~~~~~~
    /home/user/ncs/zephyr/include/device.h:291:37: note: in expansion of macro 'DEVICE_NAME_GET'
      291 | #define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_NAME(node_id))
          |                                     ^~~~~~~~~~~~~~~
    /home/user/ncs/zephyr/include/device.h:311:34: note: in expansion of macro 'DEVICE_DT_NAME_GET'
      311 | #define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
          |                                  ^~~~~~~~~~~~~~~~~~
    /home/user/ncs/zephyr/include/device.h:323:34: note: in expansion of macro 'DEVICE_DT_GET'
      323 | #define DEVICE_DT_INST_GET(inst) DEVICE_DT_GET(DT_DRV_INST(inst))
          |                                  ^~~~~~~~~~~~~
    /home/user/ncs/zephyr/drivers/can/can_mcp2515.c:945:33: note: in expansion of macro 'DEVICE_DT_INST_GET'
      945 |  const struct device *can_dev = DEVICE_DT_INST_GET(1);
          |                                 ^~~~~~~~~~~~~~~~~~
    ninja: build stopped: subcommand failed.
    FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/user/work/VSCode/can_app/build
    The terminal process terminated with exit code: 

    I’m not sure why the compiler throws this error, because the overlay file you are using appears to be correct to me. 

    Regards, 

    Markus 

Children
No Data
Related