Hi,
Sorry to be posting a lot about my LIS2DH12 on custom hardware lately. I do believe my different inquiries are to be treated as separate.
My (modified) hardware now has these connections:
Sensor
Sensor | Connection |
SCL | P0.05 |
SDA | P0.02 |
CS | VDD (I2C mode) |
SDO | VDD (addr LSB=1) |
INT1 | P0.06 |
INT2 | P0.07 |
I'm trying the LIS2DH sample, only modified it to give terminal output on RTT for debugging. The output I'm seeing is:
SEGGER J-Link V7.96l - Real time terminal output
SEGGER J-Link (unknown) V1.0, SN=682410030
Process: JLink.exe
Device LIS2DH is not ready
[00:00:00.947,387] <err> lis2dh: Failed to read chip id.
*** Booting nRF Connect SDK 3758bcbfa5cd ***
Here is my code:
main.c
/* * Copyright (c) 2019 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #include <stdio.h> #include <zephyr/kernel.h> #include <zephyr/device.h> #include <zephyr/drivers/sensor.h> #include <inttypes.h> #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(generallogs,LOG_LEVEL_DBG); static void fetch_and_display(const struct device *sensor) { static unsigned int count; struct sensor_value accel[3]; struct sensor_value temperature; const char *overrun = ""; int rc = sensor_sample_fetch(sensor); ++count; if (rc == -EBADMSG) { /* Sample overrun. Ignore in polled mode. */ if (IS_ENABLED(CONFIG_LIS2DH_TRIGGER)) { overrun = "[OVERRUN] "; } rc = 0; } if (rc == 0) { rc = sensor_channel_get(sensor, SENSOR_CHAN_ACCEL_XYZ, accel); } if (rc < 0) { printf("ERROR: Update failed: %d\n", rc); } else { printf("#%u @ %u ms: %sx %f , y %f , z %f", count, k_uptime_get_32(), overrun, sensor_value_to_double(&accel[0]), sensor_value_to_double(&accel[1]), sensor_value_to_double(&accel[2])); } if (IS_ENABLED(CONFIG_LIS2DH_MEASURE_TEMPERATURE)) { if (rc == 0) { rc = sensor_channel_get(sensor, SENSOR_CHAN_DIE_TEMP, &temperature); if (rc < 0) { printf("\nERROR: Unable to read temperature:%d\n", rc); } else { printf(", t %f\n", sensor_value_to_double(&temperature)); } } } else { printf("\n"); } } #ifdef CONFIG_LIS2DH_TRIGGER static void trigger_handler(const struct device *dev, const struct sensor_trigger *trig) { fetch_and_display(dev); } #endif int main(void) { const struct device *const sensor = DEVICE_DT_GET_ANY(st_lis2dh); if (sensor == NULL) { printf("No device found\n"); return 0; } if (!device_is_ready(sensor)) { printf("Device %s is not ready\n", sensor->name); return 0; } #if CONFIG_LIS2DH_TRIGGER { struct sensor_trigger trig; int rc; trig.type = SENSOR_TRIG_DATA_READY; trig.chan = SENSOR_CHAN_ACCEL_XYZ; if (IS_ENABLED(CONFIG_LIS2DH_ODR_RUNTIME)) { struct sensor_value odr = { .val1 = 1, }; rc = sensor_attr_set(sensor, trig.chan, SENSOR_ATTR_SAMPLING_FREQUENCY, &odr); if (rc != 0) { printf("Failed to set odr: %d\n", rc); return 0; } printf("Sampling at %u Hz\n", odr.val1); } rc = sensor_trigger_set(sensor, &trig, trigger_handler); if (rc != 0) { printf("Failed to set trigger: %d\n", rc); return 0; } printf("Waiting for triggers\n"); while (true) { k_sleep(K_MSEC(2000)); } } #else /* CONFIG_LIS2DH_TRIGGER */ printf("Polling at 0.5 Hz\n"); while (true) { fetch_and_display(sensor); k_sleep(K_MSEC(2000)); } #endif /* CONFIG_LIS2DH_TRIGGER */ }
prj.conf
CONFIG_STDOUT_CONSOLE=y CONFIG_I2C=y CONFIG_SENSOR=y CONFIG_CBPRINTF_FP_SUPPORT=y CONFIG_LOG=y CONFIG_LOG_PRINTK=y CONFIG_USE_SEGGER_RTT=y CONFIG_RTT_CONSOLE=y
.dts
// Copyright (c) 2024 Nordic Semiconductor ASA // SPDX-License-Identifier: Apache-2.0 /dts-v1/; #include <nordic/nrf52810_qfaa.dtsi> #include "myboard-pinctrl.dtsi" / { model = "my board"; compatible = "manuf,myboard"; chosen { zephyr,sram = &sram0; zephyr,flash = &flash0; zephyr,code-partition = &slot0_partition; }; buttons { compatible = "gpio-keys"; sw0: button_0 { label = "button_0"; gpios = <&gpio0 31 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; }; }; leds { compatible = "gpio-leds"; led_r: led_r { label = "Red LED"; gpios = <&gpio0 29 (GPIO_ACTIVE_LOW | (1 << 8))>; }; led_g: led_g { label = "Green LED"; gpios = <&gpio0 30 (GPIO_ACTIVE_LOW | (1 << 8))>; }; led_b: led_b { label = "Blue LED"; gpios = <&gpio0 28 (GPIO_ACTIVE_LOW | (1 << 8))>; }; }; }; &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 0xa000>; }; slot1_partition: partition@16000 { label = "image-1"; reg = <0x16000 0xa000>; }; scratch_partition: partition@20000 { label = "image-scratch"; reg = <0x20000 0xa000>; }; storage_partition: partition@2a000 { label = "storage"; reg = <0x2a000 0x6000>; }; }; }; &gpio0 { status = "okay"; }; &gpiote { status = "okay"; }; &i2c0 { status = "okay"; pinctrl-0 = <&i2c0_default>; pinctrl-1 = <&i2c0_sleep>; pinctrl-names = "default", "sleep"; lis2dh@19 { compatible = "st,lis2dh"; reg = <0x19>; label = "LIS2DH"; irq-gpios = <&gpio0 6 GPIO_ACTIVE_LOW>, <&gpio0 7 GPIO_ACTIVE_LOW>; }; }; /*&spi0 { cs-gpios = <&gpio0 4 GPIO_ACTIVE_LOW>; status = "okay"; max-frequency = <DT_FREQ_M(8)>; pinctrl-0 = <&spi0_default>; pinctrl-names = "default"; lis2dh_spi@0 { compatible = "st,lis2dh"; reg = <0>; supply-gpios = <&gpio0 8 0>; spi-max-frequency = <DT_FREQ_M(8)>; irq-gpios = <&gpio0 6 GPIO_ACTIVE_LOW>, <&gpio0 7 GPIO_ACTIVE_LOW>; }; };*/ &adc { status = "okay"; }; &pinctrl { i2c0_default { group1 { psels = <NRF_PSEL(TWIM_SDA, 0, 2)>, <NRF_PSEL(TWIM_SCL, 0, 5)>; }; }; i2c0_sleep { group1 { psels = <NRF_PSEL(TWIM_SDA, 0, 2)>, <NRF_PSEL(TWIM_SCL, 0, 5)>; }; }; };
pinctrl.dtsi
&pinctrl { i2c0_default: i2c0_default { group1 { psels = <NRF_PSEL(TWIM_SDA, 0, 2)>, <NRF_PSEL(TWIM_SCL, 0, 5)>; }; }; i2c0_sleep: i2c0_sleep { group1 { psels = <NRF_PSEL(TWIM_SDA, 0, 2)>, <NRF_PSEL(TWIM_SCL, 0, 5)>; low-power-enable; }; }; pwm0_default: pwm0_default { group1 { psels = <NRF_PSEL(PWM_OUT0, 0, 29)>; nordic,invert; }; }; pwm0_sleep: pwm0_sleep { group1 { psels = <NRF_PSEL(PWM_OUT0, 0, 29)>; low-power-enable; }; }; };