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:

  • 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 

  • Hello Markus,

    I am using NCS v1.9.1. The build errors have been resolved in this thread.

    It is suggested to edit zephyr/drivers/can/can_mcp2515.c, line 970, and replace the 1 with a 0.

  • yavordobrev said:

    I am using NCS v1.9.1. The build errors have been resolved in this thread.

    It is suggested to edit zephyr/drivers/can/can_mcp2515.c, line 970, and replace the 1 with a 0.

    Ok, thanks Yavor! That being said, I should have read through the whole thread first ;-)

    The sample is failing at this point. When I debug into the function, the line below gives me the reason why it is failing. 

    I’m not sure why 'initialised' would be false here, as the devicetree should be configured correctly. But I do not have a MCP2515 available which I can connect to my DK, so possibly this might be required.  

    Otherwise, just for fault tracing, if you toggle the following lines: 

    // if (!spi_is_ready(&dev_cfg->bus)) { 
    // LOG_ERR("SPI bus %s not ready", dev_cfg->bus.bus->name); 
    // return -ENODEV; 
    // } 

    Does that make communication work, or do you end up in another fault?

    Regards, 

    Markus

  • I just tried what you proposed and the board just hangs in the spi_nrfx_transceive function. The logic analyzer shows there is also no activity on the pins. 

    Could it be that the mcp2515 driver is trying to use spi before it is actually initialized? 

  • yavordobrev said:
    Could it be that the mcp2515 driver is trying to use spi before it is actually initialized? 

    Yes, that appears to be the case. If I run the sample as a secure target (nrf9160dk_nrf9160), the output looks a little bit different. 

    *** Booting Zephyr OS build v2.7.99-ncs1-1  *** 
    
    [00:00:02.288,177] <dbg> net_sock_can.can_register_filters: (main): Registering 1 filters 
    [00:00:02.298,980] <dbg> net_sock_can.can_register_receiver: (main): Max 1 receivers 
    [00:00:02.309,448] <dbg> net_socket_can_sample.setup_socket: Started socket CAN TX thread 
    [00:00:02.320,220] <inf> net_socket_can_sample: 1st RX fd 0 
    [00:00:02.328,430] <dbg> net_socket_can_sample.rx: [0] Waiting CAN data... 
    [00:00:03.309,509] <dbg> net_socket_can_sample.tx: Sending CAN data... 
    [00:00:06.420,379] <dbg> mcp2515_can.socket_can_send: Cannot send socket CAN msg (-11) 

    Can you test this as well and confirm that it works? 

    In that case: I assume this is a timing issue with the Secure Partition Manager.

    Regards,

    Markus

Related