How to add i2c sensor driver correctly on nrf connect SDK?

Hi,

I'm trying to move project from nrf52 SDK to new NCS.

I'm using custom board with nrf52832. There're some problems when working on i2c communication with LSM6DSL.

The project I'm testing is copied from "ncs\v2.5.1\zephyr\samples\sensor\lsm6dsl", so the main code and prj.conf are all same as usual.

When I build and run the code to my board, just showed "sensor: device not ready."

Here are my other files currently, please help me to fix with it

for .dtsi file:

/*
 * Copyright (c) 2022 Nordic Semiconductor
 * SPDX-License-Identifier: Apache-2.0
 */

 &pinctrl {
	uart0_default: uart0_default {
		group1 {
			psels = <NRF_PSEL(UART_TX, 0, 6)>,
				<NRF_PSEL(UART_RX, 0, 8)>,
				<NRF_PSEL(UART_RTS, 0, 9)>,
				<NRF_PSEL(UART_CTS, 0, 7)>;
		};
	};

	uart0_sleep: uart0_sleep {
		group1 {
			psels = <NRF_PSEL(UART_TX, 0, 6)>,
				<NRF_PSEL(UART_RX, 0, 8)>,
				<NRF_PSEL(UART_RTS, 0, 9)>,
				<NRF_PSEL(UART_CTS, 0, 7)>;
			low-power-enable;
		};
	};

	i2c0_default: i2c0_default {
		group1 {
			psels = <NRF_PSEL(TWIM_SDA, 0, 11)>,
				<NRF_PSEL(TWIM_SCL, 0, 12)>;
			// bias-pull-up;
		};
	};

	i2c0_sleep: i2c0_sleep {
		group1 {
			psels = <NRF_PSEL(TWIM_SDA, 0, 11)>,
				<NRF_PSEL(TWIM_SCL, 0, 12)>;
			low-power-enable;
		};
	};

    pwm0_default: pwm0_default {
		group1 {
			psels = <NRF_PSEL(PWM_OUT0, 0, 3)>,
                <NRF_PSEL(PWM_OUT1, 0, 4)>,
                <NRF_PSEL(PWM_OUT2, 0, 5)>;
			nordic,invert;
		};
	};

	pwm0_sleep: pwm0_sleep {
		group1 {
			psels = <NRF_PSEL(PWM_OUT0, 0, 3)>,
                <NRF_PSEL(PWM_OUT1, 0, 4)>,
                <NRF_PSEL(PWM_OUT2, 0, 5)>;
			low-power-enable;
		};
	};

};

for dts file:

// Copyright (c) 2024 Nordic Semiconductor ASA
// SPDX-License-Identifier: Apache-2.0

/dts-v1/;
#include <nordic/nrf52832_qfaa.dtsi>
#include "custom_nrf52832-pinctrl.dtsi"

/ {
	model = "custom_nrf52832";
	compatible = "private,custom-nrf52832";

	chosen {
		zephyr,sram = &sram0;
		zephyr,flash = &flash0;
		zephyr,code-partition = &slot0_partition;
		zephyr,console = &uart0;
		zephyr,shell-uart = &uart0;
		zephyr,uart-mcumgr = &uart0;
	};

	leds {
		compatible = "gpio-leds";
		ledR: led_0 {
			gpios = <&gpio0 3 (GPIO_ACTIVE_LOW)>;
			label = "Red led R";
		};
		ledG: led_1 {
			gpios = <&gpio0 4 (GPIO_ACTIVE_LOW)>;
			label = "Green led G";
		};
		ledB: led_2 {
			gpios = <&gpio0 5 (GPIO_ACTIVE_LOW)>;
			label = "Blue led B";
		};
	};

	buttons {
		compatible = "gpio-keys";
		buttonC: button_0 {
			gpios = <&gpio0 27 0>;
			label = "Push button switch 0";
		};
	};

	pwmleds {
		compatible = "pwm-leds";
		pwm_ledR: pwm_led_0 {
			pwms = <&pwm0 0 PWM_MSEC(1) PWM_POLARITY_NORMAL>;
			label = "pwm led R";
		};
		pwm_ledG: pwm_led_1 {
			pwms = <&pwm0 1 PWM_MSEC(1) PWM_POLARITY_NORMAL>;
			label = "pwm led G";
		};
		pwm_ledB: pwm_led_2 {
			pwms = <&pwm0 2 PWM_MSEC(1) PWM_POLARITY_NORMAL>;
			label = "pwm led B";
		};
	};

	/* These aliases are provided for compatibility with samples */
	aliases {
		led0 = &ledR;
		led1 = &ledG;
		led2 = &ledB;
		pwm-led0 = &pwm_ledR;
		pwm-led1 = &pwm_ledG;
		pwm-led2 = &pwm_ledB;
		sw0 = &buttonC;
		bootloader-led0 = &ledR;
		mcuboot-button0 = &buttonC;
		mcuboot-led0 = &ledR;
		watchdog0 = &wdt0;
	};
};

&flash0 {
	partitions {
		compatible = "fixed-partitions";
		#address-cells = <1>;
		#size-cells = <1>;

		boot_partition: partition@0 {
			label = "mcuboot";
			reg = <0x0 0xc000>;
		};
		slot0_partition: partition@c000 {
			label = "image-0";
			reg = <0xc000 0x32000>;
		};
		slot1_partition: partition@3e000 {
			label = "image-1";
			reg = <0x3e000 0x32000>;
		};
		scratch_partition: partition@70000 {
			label = "image-scratch";
			reg = <0x70000 0xa000>;
		};
		storage_partition: partition@7a000 {
			label = "storage";
			reg = <0x7a000 0x6000>;
		};
	};
};


&gpio0 {
	status = "okay";
};

&gpiote {
	status = "okay";
};

&uart0 {
	status = "okay";
	current-speed = <115200>;
	pinctrl-0 = <&uart0_default>;
	pinctrl-1 = <&uart0_sleep>;
	pinctrl-names = "default", "sleep";
};

&i2c0 {
	status = "okay";
	pinctrl-0 = <&i2c0_default>;
	pinctrl-1 = <&i2c0_sleep>;
	pinctrl-names = "default", "sleep";
	lsm6dsl@6a {
		compatible = "st,lsm6dsl";
		reg = <0x6a>;
		label = "GSENSOR";
	};
};

&pwm0 {
	status = "okay";
	pinctrl-0 = <&pwm0_default>;
	pinctrl-1 = <&pwm0_sleep>;
	pinctrl-names = "default", "sleep";
};

for board_defconfig:

# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

CONFIG_SOC_SERIES_NRF52X=y
CONFIG_SOC_NRF52832_QFAA=y
CONFIG_BOARD_CUSTOM_NRF52832=y

# Enable MPU
CONFIG_ARM_MPU=y

# Enable hardware stack protection
CONFIG_HW_STACK_PROTECTION=y

# Enable RTT
CONFIG_USE_SEGGER_RTT=y

# enable GPIO
CONFIG_GPIO=y

# enable uart driver
CONFIG_SERIAL=y

# enable console
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=n
CONFIG_RTT_CONSOLE=y

CONFIG_PINCTRL=y

Parents
  • I do not think we have any sample to show LSM6DSL working with I2C on the nRF Connect SDK. Also I do not see any inclusion of this sensor interface in the overlay or the dts file, something like below snippet

    st_lsm6dsl: &i2c3 {
    
    compatible = "nordic,nrf-twis";
    status = "okay";
    	pinctrl-0 = <&i2c1_default_alt_alt>;
    	pinctrl-1 = <&i2c1_sleep_alt_alt>;
    	pinctrl-names = "default", "sleep";
    	
    	....
    	...
    	};

Reply
  • I do not think we have any sample to show LSM6DSL working with I2C on the nRF Connect SDK. Also I do not see any inclusion of this sensor interface in the overlay or the dts file, something like below snippet

    st_lsm6dsl: &i2c3 {
    
    compatible = "nordic,nrf-twis";
    status = "okay";
    	pinctrl-0 = <&i2c1_default_alt_alt>;
    	pinctrl-1 = <&i2c1_sleep_alt_alt>;
    	pinctrl-names = "default", "sleep";
    	
    	....
    	...
    	};

Children
  • Hi Susheel,

    Thanks for rthe reply.

    I do not think we have any sample to show LSM6DSL working with I2C on the nRF Connect SDK.

    Sorry, that might be a misunderstood. The sample LSM6DSL must from Zephyr, but compatible with some nordic boards.

    Also I do not see any inclusion of this sensor interface in the overlay or the dts file, something like below snippet
    for dts file:

    Actually, I thought I had done that in my .dts file form line 123 to line 133. Do I just miss something like node name st_lsm6dsl in front of &i2c0 { or compatible = "nordic,nrf-twis"; ?

    st_lsm6dsl: &i2c0 {
    	compatible = "nordic,nrf-twim";
    	status = "okay";
    	clock-frequency = <I2C_BITRATE_STANDARD>;
    	pinctrl-0 = <&i2c0_default>;
    	pinctrl-1 = <&i2c0_sleep>;
    	pinctrl-names = "default", "sleep";
    	label = "I2C_0";
    	lsm6dsl: lsm6dsl@6a {
    		compatible = "st,lsm6dsl";
    		reg = <0x6a>;
    		label = "GSENSOR";
    	};
    };

    For further question, if I do this in .dts file, the sensor can't even be initialize in main code with 

    const struct device *const lsm6dsl_dev = DEVICE_DT_GET_ONE(st_lsm6dsl);
     
    can you see any problem from the code?
  • I can't this here so I have to rely on the error number / error messages you get at this line. I do not see anything obviously wrong in your dts file but it looks like the device did not get initialized or the macro being not used correctly. What error number and log did you get ?

  • Hello,

    In my main.c, I can see there is no definition found with st_lsm6dsl.

    Is that correct with the devicetree I had created?

    If I flash the original code from example, nothing goes error but only show "device not ready" 

    Does that means is failed in DEVICE_DT_GET_ONE?

  • Update: I think I had connect to sensor lsm6dsl after adding

    #define MY_I2C DT_NODELABEL(i2c0) and replace st_lsm6dsl to MY_I2C, DEVICE_DT_GET_ONE to DEVICE_DT_GET , but I can't correctly set the sensor, could you help with that?

  • Good to know that you are able to initializing the sensor now and be to get the device node pointer in the applicaiton. 

    ekidsalan123 said:
    but I can't correctly set the sensor, could you help with that?

    You need to be a bit more descriptive than can't correct set the sensor. 

    -Which API are you using to set the sensor?

    - What error are you getting?

    - Have you seen any activity on the I2C pins? Did you use any logic analyzer or oscillscope to see any activity on the I2C pins?

Related