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
  • 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

Reply
  • 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

Children
Related