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

  • Additionally, CMakeLists.txt in both lis2dh and adxl372 don't use the zephyr_include_directories command, which makes it harder to include them any other way. I've tried adding zephyr_include_directories(.) but I still get the same error for lis2dh. 

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

Related