This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Including specific sensor header files

Hi everyone,

I have a board based on thingy91. I have lis2dh and adxl372 sensors on it. They are both connected via SPI, and I've added this to my overlay file:

&spi0 {
	compatible = "nordic,nrf-spim";
	status = "okay";
	sck-pin = <3>;
	mosi-pin = <4>;
	miso-pin = <5>;
	cs-gpios = <&gpio0 8 GPIO_ACTIVE_LOW>, <&gpio0 7 GPIO_ACTIVE_LOW>;

	lis2dh@0 {
		compatible = "st,lis2dh";
		label = "LIS2DH";
		spi-max-frequency = <8000000>;
		reg = <0>;
		irq-gpios = <&gpio0 9 0>;
	};

	adxl372@1 {
		compatible = "adi,adxl372";
		label = "ADXL372";
		spi-max-frequency = <8000000>;
		reg = <1>;
		int1-gpios = <&gpio0 6 0>;
	};
};

Also I've enabled the sensors in the prj.conf. I am able to initialize and get sensor readings, everything works fine there. The issue comes up when I try to include lis2dh.h and adxl372.h in my project. I do this because I want to have access to the SPI bus, and I want to be able to send custom values to the device registers(I wasn't able to find this as an option through the sensor api provided by Zephyr). I've added adxl372 and lis2dh to my list of include directories in top level CMakeLists.txt, and I am able now to include them in my source code. However when compiling, I get the following error:

error: 'const union lis2dh_bus_cfg' has no member named 'spi_cfg'
  122 |     const struct spi_config *spi_cfg = &cfg->bus_cfg.spi_cfg->spi_conf;
 

That's weird, because it is defined to use spi, and the devices do work. So I suspect that the macro DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) is not working well because these files are included in a weird way(directly from my top level cmakelists.txt, instead of the usual way). 

How can I include these files in my source code and use them properly?

Cheers,

Aleksa

Parents
  • Hi,

    Are you sure the overlay is being applied? Check if you have the lis2dh device in build_folder/zephyr/zephyr.dts

    And for the overlay, don't use spi0, use spi3 instead.

  • Hi Sigurd,

    I've changed to the spi3, it didn't help. By the way is the reason why you suggested this because the pins I use are pins of the spi3 or is it something else?

    Yes I do have lis2dh device in the zephyr.dts. Is this zephyr.dts a final output of the overlay and the default dts files?

    Cheers,

    Aleksa

  • Aleksa said:
    By the way is the reason why you suggested this because the pins I use are pins of the spi3 or is it something else?

     Yes, It's what the default thingy91 board is configured for. See this: https://github.com/nrfconnect/sdk-nrf/blob/master/boards/arm/thingy91_nrf9160/thingy91_nrf9160_common.dts#L128

    + also if you are using uart0, then you cannot use spi0 at the same time, since they share the same memory ID/address: https://infocenter.nordicsemi.com/topic/ps_nrf9160/memory.html#topic

    Aleksa said:
    Is this zephyr.dts a final output of the overlay and the default dts files?

     Yes, correct. Can you upload it ? and your CMakeLists.txt file ?

    Also, if there are other spi devices on spi3, you should use a unique device instance number for lis2dh, change

    lis2dh@0 {
    compatible = "st,lis2dh";
    label = "LIS2DH";
    spi-max-frequency = <8000000>;
    reg = <0>;
    irq-gpios = <&gpio0 9 0>;
    };

    to

    lis2dh@2 {
    compatible = "st,lis2dh";
    label = "LIS2DH";
    spi-max-frequency = <8000000>;
    reg = <0>;
    irq-gpios = <&gpio0 9 0>;
    };

  • #
    # Copyright (c) 2018 Nordic Semiconductor
    #
    # SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
    #
    cmake_minimum_required(VERSION 3.13.1)
    
    set(spm_CONF_FILE ${CMAKE_CURRENT_SOURCE_DIR}/spm.conf)
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    if(CONFIG_PROVISION_CERTIFICATES)
      message(WARNING "
          ------------------------------------------------------------
          --- WARNING: Provisioning certificates is ENABLED. Do    ---
          --- not use this binary in production or share it with   ---
          --- anyone. It has certificates stored in readable flash,---
          --- the binary, and the modem traces. Only use this      ---
          --- binary once to provision certificates for development---
          --- to reduce flash tear. After the certificates are     ---
          --- provisioned, disable this option and rebuild the     ---
          --- sample.                                              ---
          ------------------------------------------------------------")
    endif()
    project(mg105)
    
    target_sources(app PRIVATE
      mg105/app/src/main.c
      )
    
    target_include_directories(app PRIVATE 
      mg105/app/src
      mg105/bsp/src
      mg105/hal/src
      mg105/osal/src/zephyr
      lib/nordic/ncs/zephyr/drivers/sensor/adxl372
      lib/nordic/ncs/zephyr/drivers/sensor/lis2dh
      )
    
    zephyr_include_directories(
      mg105/app/src
      mg105/bsp/src
      mg105/hal/src
      mg105/osal/src/zephyr
      lib/nordic/ncs/zephyr/drivers/sensor/adxl372
      lib/nordic/ncs/zephyr/drivers/sensor/lis2dh
      )
    
    # Include application events and configuration headers
    zephyr_library_include_directories(
      mg105/app/src/ABLY
      mg105/app/src/APC
      mg105/app/src/AWS
      mg105/app/src/GPS
      mg105/app/src/Motion
      mg105/app/src/Watchdog
      mg105/app/src/nRF52840
      mg105/app/src/nRF52840/serial_dfu_host
      mg105/app/src/Modem_Controller
      mg105/app/src/Gateway_Data
      mg105/app/src/AGPS
      mg105/app/src/MQTT
      mg105/app/src/Power
      mg105/app/src/logger
      mg105/app/src/utils
      $ENV{BSP_SRC}/../inc
      mg105/hal/inc
      mg105/osal/inc
      mg105/osal/src/zephyr/sys
      )
    
    # Application sources
    add_subdirectory(mg105/app/src/ABLY)
    add_subdirectory(mg105/app/src/APC)
    add_subdirectory(mg105/app/src/AWS)
    add_subdirectory(mg105/app/src/GPS)
    add_subdirectory(mg105/app/src/Motion)
    add_subdirectory(mg105/app/src/Watchdog)
    add_subdirectory(mg105/app/src/nRF52840)
    add_subdirectory(mg105/app/src/nRF52840/serial_dfu_host)
    add_subdirectory(mg105/app/src/Modem_Controller)
    add_subdirectory(mg105/app/src/Gateway_Data)
    add_subdirectory(mg105/app/src/AGPS)
    add_subdirectory(mg105/app/src/MQTT)
    add_subdirectory(mg105/app/src/Power)
    add_subdirectory(mg105/app/src/logger)
    add_subdirectory(mg105/app/src/utils)
    
    add_subdirectory($ENV{BSP_SRC}/drivers)
    add_subdirectory($ENV{BSP_SRC}/flash)
    add_subdirectory($ENV{BSP_SRC}/gps)
    add_subdirectory($ENV{BSP_SRC}/logger)
    add_subdirectory($ENV{BSP_SRC}/modem)
    add_subdirectory($ENV{BSP_SRC}/power)
    add_subdirectory($ENV{BSP_SRC}/sensors)
    add_subdirectory($ENV{BSP_SRC}/serial)
    add_subdirectory($ENV{BSP_SRC}/time)
    add_subdirectory($ENV{BSP_SRC}/watchdog)
    
    add_subdirectory(mg105/hal/src/flash)
    add_subdirectory(mg105/hal/src/gps)
    add_subdirectory(mg105/hal/src/modem)
    add_subdirectory(mg105/hal/src/power)
    add_subdirectory(mg105/hal/src/sensors)
    add_subdirectory(mg105/hal/src/serial)
    add_subdirectory(mg105/hal/src/time)
    add_subdirectory(mg105/hal/src/logger)
    add_subdirectory(mg105/hal/src/watchdog)
    
    add_subdirectory(mg105/osal/src/zephyr)
    
    0815.zephyr.dts

    Here are the two files. Here is ther error message(we use some build script to set things up for cmake before running it).

    <MY_PATH>/bsp_sensor.c:122:53: error: 'const union lis2dh_bus_cfg' has no member named 'spi_cfg'
      122 |     const struct spi_config *spi_cfg = &cfg->bus_cfg.spi_cfg->spi_conf;
          |                                                     ^
    <MY_PATH>/bsp_sensor.c: In function 'bsp_sensor_lis3dh_spi_read':
    <MY_PATH>/bsp_sensor.c:159:53: error: 'const union lis2dh_bus_cfg' has no member named 'spi_cfg'
      159 |     const struct spi_config *spi_cfg = &cfg->bus_cfg.spi_cfg->spi_conf;
    

Reply
  • #
    # Copyright (c) 2018 Nordic Semiconductor
    #
    # SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
    #
    cmake_minimum_required(VERSION 3.13.1)
    
    set(spm_CONF_FILE ${CMAKE_CURRENT_SOURCE_DIR}/spm.conf)
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    if(CONFIG_PROVISION_CERTIFICATES)
      message(WARNING "
          ------------------------------------------------------------
          --- WARNING: Provisioning certificates is ENABLED. Do    ---
          --- not use this binary in production or share it with   ---
          --- anyone. It has certificates stored in readable flash,---
          --- the binary, and the modem traces. Only use this      ---
          --- binary once to provision certificates for development---
          --- to reduce flash tear. After the certificates are     ---
          --- provisioned, disable this option and rebuild the     ---
          --- sample.                                              ---
          ------------------------------------------------------------")
    endif()
    project(mg105)
    
    target_sources(app PRIVATE
      mg105/app/src/main.c
      )
    
    target_include_directories(app PRIVATE 
      mg105/app/src
      mg105/bsp/src
      mg105/hal/src
      mg105/osal/src/zephyr
      lib/nordic/ncs/zephyr/drivers/sensor/adxl372
      lib/nordic/ncs/zephyr/drivers/sensor/lis2dh
      )
    
    zephyr_include_directories(
      mg105/app/src
      mg105/bsp/src
      mg105/hal/src
      mg105/osal/src/zephyr
      lib/nordic/ncs/zephyr/drivers/sensor/adxl372
      lib/nordic/ncs/zephyr/drivers/sensor/lis2dh
      )
    
    # Include application events and configuration headers
    zephyr_library_include_directories(
      mg105/app/src/ABLY
      mg105/app/src/APC
      mg105/app/src/AWS
      mg105/app/src/GPS
      mg105/app/src/Motion
      mg105/app/src/Watchdog
      mg105/app/src/nRF52840
      mg105/app/src/nRF52840/serial_dfu_host
      mg105/app/src/Modem_Controller
      mg105/app/src/Gateway_Data
      mg105/app/src/AGPS
      mg105/app/src/MQTT
      mg105/app/src/Power
      mg105/app/src/logger
      mg105/app/src/utils
      $ENV{BSP_SRC}/../inc
      mg105/hal/inc
      mg105/osal/inc
      mg105/osal/src/zephyr/sys
      )
    
    # Application sources
    add_subdirectory(mg105/app/src/ABLY)
    add_subdirectory(mg105/app/src/APC)
    add_subdirectory(mg105/app/src/AWS)
    add_subdirectory(mg105/app/src/GPS)
    add_subdirectory(mg105/app/src/Motion)
    add_subdirectory(mg105/app/src/Watchdog)
    add_subdirectory(mg105/app/src/nRF52840)
    add_subdirectory(mg105/app/src/nRF52840/serial_dfu_host)
    add_subdirectory(mg105/app/src/Modem_Controller)
    add_subdirectory(mg105/app/src/Gateway_Data)
    add_subdirectory(mg105/app/src/AGPS)
    add_subdirectory(mg105/app/src/MQTT)
    add_subdirectory(mg105/app/src/Power)
    add_subdirectory(mg105/app/src/logger)
    add_subdirectory(mg105/app/src/utils)
    
    add_subdirectory($ENV{BSP_SRC}/drivers)
    add_subdirectory($ENV{BSP_SRC}/flash)
    add_subdirectory($ENV{BSP_SRC}/gps)
    add_subdirectory($ENV{BSP_SRC}/logger)
    add_subdirectory($ENV{BSP_SRC}/modem)
    add_subdirectory($ENV{BSP_SRC}/power)
    add_subdirectory($ENV{BSP_SRC}/sensors)
    add_subdirectory($ENV{BSP_SRC}/serial)
    add_subdirectory($ENV{BSP_SRC}/time)
    add_subdirectory($ENV{BSP_SRC}/watchdog)
    
    add_subdirectory(mg105/hal/src/flash)
    add_subdirectory(mg105/hal/src/gps)
    add_subdirectory(mg105/hal/src/modem)
    add_subdirectory(mg105/hal/src/power)
    add_subdirectory(mg105/hal/src/sensors)
    add_subdirectory(mg105/hal/src/serial)
    add_subdirectory(mg105/hal/src/time)
    add_subdirectory(mg105/hal/src/logger)
    add_subdirectory(mg105/hal/src/watchdog)
    
    add_subdirectory(mg105/osal/src/zephyr)
    
    0815.zephyr.dts

    Here are the two files. Here is ther error message(we use some build script to set things up for cmake before running it).

    <MY_PATH>/bsp_sensor.c:122:53: error: 'const union lis2dh_bus_cfg' has no member named 'spi_cfg'
      122 |     const struct spi_config *spi_cfg = &cfg->bus_cfg.spi_cfg->spi_conf;
          |                                                     ^
    <MY_PATH>/bsp_sensor.c: In function 'bsp_sensor_lis3dh_spi_read':
    <MY_PATH>/bsp_sensor.c:159:53: error: 'const union lis2dh_bus_cfg' has no member named 'spi_cfg'
      159 |     const struct spi_config *spi_cfg = &cfg->bus_cfg.spi_cfg->spi_conf;
    

Children
  • Aleksa said:
    I've changed to the spi3,

     Could you use and upload that file instead? The one you are using now is still using spi0

  • Actually I've posted the wrong output, but I use spi3 now and the issue is still the same.

  • So far I'm not able to reproduce this.

    Testing with asset_tracker_v2. I modified thingy91_nrf9160ns.overlay so it looks like this:

    /*
     * Copyright (c) 2021 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    / {
            aliases {
                    temp-sensor = &bme680;
                    humidity-sensor = &bme680;
                    accelerometer = &adxl362;
            };
    };
    
    &i2c2 {
            bme680: bme680@76 {};
    };
    
    &spi3 {
            adxl362: adxl362@0 {};
            
            lis2dh@2 {
                    compatible = "st,lis2dh";
                    label = "LIS2DH";
                    spi-max-frequency = <8000000>;
                    reg = <0>;
                    irq-gpios = <&gpio0 9 0>;
            };
    };
    
    
    

    I added this to CMakeLists.txt :
    zephyr_include_directories(${ZEPHYR_BASE}/drivers/sensor/lis2dh)

    and added

    #include <lis2dh.h>
    to main.c, it compiled without any warnings or errors.
  • I have tried to make my own driver(because I feel that the sensor API is way too limited for these two sensors). I don't want to edit the zephyr unless I really have to(makes it harder to update ncs versions if I just edit it whenever I don't like how it works). For example in the adxl372.c there is a adxl372_bus_access() function, that extracts some configuration and in the end calls:

    return spi_transceive(adxl372_data->bus, &adxl372_data->spi_cfg, &tx, &rx);
    

    When I try to make the same function in my own code, it says spi_cfg is not a part of the struct adxl372_data. But it definitely has to be a part of it, since I've set CONFIG_ADXL372_SPI=y.  Same happens with the lis2dh, where it doesn't allow access to the spi part of the configuration.

    PS: Before I've tried accessing these fields that depend on whether or not the sensor is used via spi, my code did build too. Could you please try accessing them too?

  • I'm not able to reproduce this.

    Could you upload a small sample that reproduces this? (If you have done changes to Zephyr, add those files as well.)

Related