Device not found with device_get_binding

Hi,

I'm trying to mount two spi flash device on spi0, but can't found the second one(device_get_binding() return NULL).

The two device has compatible ("jedec,spi-nor") mean they has spi-nor flash driver.

My main functions like this:

 

#define FLASH_DEVICE_0 "DEVICE_LABEL_0"
#define FLASH_DEVICE_1 "DEVICE_LABEL_1"

static void device_0(void) {
  const struct device *flash_dev = device_get_binding(FLASH_DEVICE_0);

  if (!flash_dev) {
    LOG_INF("SPI flash driver %s was not found!", __func__);
    return;
  }

  LOG_INF("Get %s.", __func__);
}

static void device_1(void) {
  const struct device *flash_dev = device_get_binding(FLASH_DEVICE_1);

  if (!flash_dev) {
    LOG_INF("SPI flash driver %s was not found!", __func__);
    return;
  }

  LOG_INF("Get %s.", __func__);
}

void main(void) {
  LOG_INF("Hello");
  device_0();
  device_1();
}

And i add app.overlay to my project. Because of no actual device was connected, i just make MISO to GND, so jedec-id is zero.

&spi0 {
    compatible = "nordic,nrf-spi";
    status = "okay";
    pinctrl-0 = <&spi_default>;
    pinctrl-1 = <&spi_sleep>;
    pinctrl-names = "default", "sleep";
    cs-gpios = <&gpio0 28 GPIO_ACTIVE_LOW>, <&gpio0 30 GPIO_ACTIVE_LOW>;

    DEVICE_0:DEVICE_0@0 {
        compatible = "jedec,spi-nor";
        reg = <0>;
        spi-max-frequency = <8000000>;
        label = "DEVICE_LABEL_0";
        jedec-id = [ 00 00 00 ];
        size = < 0x8000000 >;
    };
    DEVICE_1:DEVICE_1@1 {
        compatible = "jedec,spi-nor";
        reg = <1>;
        spi-max-frequency = <8000000>;
        label = "DEVICE_LABEL_1";
        jedec-id = [ 00 00 00 ];
        size = < 0x8000000 >;
    };
};

After download zephyr.hex to nRF52840, the RTT View shows as follow:

*** Booting Zephyr OS build v3.0.99-ncs1  ***

[00:00:00.331,207] <inf> multi_spi: Hello
[00:00:00.331,237] <inf> multi_spi: Get device_0.
[00:00:00.331,268] <inf> multi_spi: SPI flash driver device_1 was not found!

It is shows that "device 0" can found but "device 1" can NOT found. I debug it and see  z_impl_device_get_binding() has a device named "device 0", but no device named "device 1".

"device 1" can be only found when i was deleted "device 0" on the app.overlay file.

what does i missing?

  • Hello!

    Why exactly do you want to mount two devices on spi0?

    Do you have two devices you want to talk to over spi, or do you want two separate spi buses on spi0?

    In the first case you'll only need one spi device, and you'll choose what sensor to talk to with the chip select signal.

    In the latter case you'll probably want to look into the new Pin control API.

    Best regards,

    Einar

  • Hi,

    Why exactly do you want to mount two devices on spi0?

    Because i have two spi devices used the same pin to connect, such as "CLK", "MOSI", "MISO".

    That means i have two devices i want to talk to over spi.

    > In the first case you'll only need one spi device, and you'll choose what sensor to talk to with the chip select signal.

    Do you means i only add one device and two CS pins in my app.overlay file, and select the device by CS pin?

    Actually, i have one device is spi flash memory, and another device is EPD display, their communication pins are SPI.

    The spi flash has four pins such as "CS", "CLK", "MOSI", "MISO". And the EPD display has extra two pins.

    But i don't have enough PINs, so i have to reuse some pins which is spi flash and EPD display in this case.

    Back to the topic, i have to add two devices in my app.overlay file, and add all pins for each devices.

    Best regards,

    HuYang

  • >Do you means i only add one device and two CS pins in my app.overlay file, and select the device by CS pin?

    Yes

    >The spi flash has four pins such as "CS", "CLK", "MOSI", "MISO". And the EPD display has extra two pins.

    >But i don't have enough PINs, so i have to reuse some pins which is spi flash and EPD display in this case.

    This is exactly why you have the CS pin.

    You don't define your peripheral devices in the devicetree, you only define the SPI bus device and then use the SPI bus to talk to your peripheral devices.

Related