1212#include <zephyr/sys/byteorder.h>
1313#include <zephyr/logging/log.h>
1414
15+ #include <wsen_sensors_common.h>
1516#include "wsen_itds_2533020201601.h"
1617
1718LOG_MODULE_REGISTER (WSEN_ITDS_2533020201601 , CONFIG_SENSOR_LOG_LEVEL );
@@ -40,7 +41,11 @@ static const int itds_2533020201601_full_scale_list[] = {
4041 16 ,
4142};
4243
43- #define MAX_POLL_STEP_COUNT 10
44+ /* Map of dts binding power mode to enum power mode*/
45+ static const ITDS_powerMode_t power_mode_map [] = {
46+ [0 ] = ITDS_lowPower ,
47+ [1 ] = ITDS_normalMode ,
48+ };
4449
4550/* convert raw temperature to celsius */
4651static inline int16_t itds_2533020201601_raw_temp_to_celsius (int16_t raw_temp )
@@ -67,6 +72,8 @@ static int itds_2533020201601_sample_fetch(const struct device *dev, enum sensor
6772 return - ENOTSUP ;
6873 }
6974
75+ uint32_t step_sleep_duration = 0 ;
76+
7077 if (cfg -> op_mode == ITDS_singleConversion ) {
7178 if (ITDS_startSingleDataConversion (& data -> sensor_interface , ITDS_enable ) !=
7279 WE_SUCCESS ) {
@@ -75,6 +82,12 @@ static int itds_2533020201601_sample_fetch(const struct device *dev, enum sensor
7582 }
7683
7784 k_sleep (K_MSEC (5 ));
85+ } else {
86+ if (!wsen_sensor_step_sleep_duration_milli_from_odr_hz (
87+ & itds_2533020201601_odr_list [data -> sensor_odr ], & step_sleep_duration )) {
88+ LOG_ERR ("Accelerometer is disabled." );
89+ return - ENOTSUP ;
90+ }
7891 }
7992
8093 ITDS_state_t acceleration_data_ready , temp_data_ready ;
@@ -83,10 +96,6 @@ static int itds_2533020201601_sample_fetch(const struct device *dev, enum sensor
8396
8497 bool data_ready = false;
8598 int step_count = 0 ;
86- uint32_t step_sleep_duration =
87- ((uint32_t )1000000000 /
88- (uint32_t )sensor_value_to_milli (& itds_2533020201601_odr_list [data -> sensor_odr ]) /
89- MAX_POLL_STEP_COUNT );
9099
91100 while (1 ) {
92101
@@ -594,68 +603,80 @@ int itds_2533020201601_init(const struct device *dev)
594603 return 0 ;
595604}
596605
606+ /* clang-format off */
607+
597608#ifdef CONFIG_WSEN_ITDS_2533020201601_TRIGGER
598- #define ITDS_2533020201601_CFG_EVENTS_IRQ (inst ) \
599- .events_interrupt_gpio = GPIO_DT_SPEC_INST_GET(inst, events_interrupt_gpios),
600- #define ITDS_2533020201601_CFG_DRDY_IRQ (inst ) \
601- .drdy_interrupt_gpio = GPIO_DT_SPEC_INST_GET(inst, drdy_interrupt_gpios),
609+ #define ITDS_2533020201601_CFG_EVENTS_IRQ (inst ) \
610+ .events_interrupt_gpio = \
611+ GPIO_DT_SPEC_INST_GET(inst, events_interrupt_gpios),
612+
613+ #define ITDS_2533020201601_CFG_DRDY_IRQ (inst ) \
614+ .drdy_interrupt_gpio = \
615+ GPIO_DT_SPEC_INST_GET(inst, drdy_interrupt_gpios),
602616#else
603617#define ITDS_2533020201601_CFG_EVENTS_IRQ (inst )
604618#define ITDS_2533020201601_CFG_DRDY_IRQ (inst )
605619#endif /* CONFIG_WSEN_ITDS_2533020201601_TRIGGER */
606620
607621#ifdef CONFIG_WSEN_ITDS_2533020201601_TAP
608- #define ITDS_2533020201601_CONFIG_TAP (inst ) \
609- .tap_mode = DT_INST_PROP(inst, tap_mode), \
610- .tap_threshold = DT_INST_PROP(inst, tap_threshold), \
611- .tap_shock = DT_INST_PROP(inst, tap_shock), \
612- .tap_latency = DT_INST_PROP(inst, tap_latency), \
622+ #define ITDS_2533020201601_CONFIG_TAP (inst ) \
623+ .tap_mode = DT_INST_PROP(inst, tap_mode), \
624+ .tap_threshold = DT_INST_PROP(inst, tap_threshold), \
625+ .tap_shock = DT_INST_PROP(inst, tap_shock), \
626+ .tap_latency = DT_INST_PROP(inst, tap_latency), \
613627 .tap_quiet = DT_INST_PROP(inst, tap_quiet),
614628#else
615629#define ITDS_2533020201601_CONFIG_TAP (inst )
616630#endif /* CONFIG_WSEN_ITDS_2533020201601_TAP */
617631
618632#ifdef CONFIG_WSEN_ITDS_2533020201601_FREEFALL
619- #define ITDS_2533020201601_CONFIG_FREEFALL (inst ) \
620- .freefall_duration = DT_INST_PROP(inst, freefall_duration), \
621- .freefall_threshold = \
633+ #define ITDS_2533020201601_CONFIG_FREEFALL (inst ) \
634+ .freefall_duration = DT_INST_PROP(inst, freefall_duration), \
635+ .freefall_threshold = \
622636 (ITDS_FreeFallThreshold_t)DT_INST_ENUM_IDX(inst, freefall_threshold),
623637#else
624638#define ITDS_2533020201601_CONFIG_FREEFALL (inst )
625639#endif /* CONFIG_WSEN_ITDS_2533020201601_FREEFALL */
626640
627641#ifdef CONFIG_WSEN_ITDS_2533020201601_DELTA
628- #define ITDS_2533020201601_CONFIG_DELTA (inst ) \
629- .delta_threshold = DT_INST_PROP(inst, delta_threshold), \
630- .delta_duration = DT_INST_PROP(inst, delta_duration), \
631- .delta_offsets = DT_INST_PROP(inst, delta_offsets), \
642+ #define ITDS_2533020201601_CONFIG_DELTA (inst ) \
643+ .delta_threshold = DT_INST_PROP(inst, delta_threshold), \
644+ .delta_duration = DT_INST_PROP(inst, delta_duration), \
645+ .delta_offsets = DT_INST_PROP(inst, delta_offsets), \
632646 .delta_offset_weight = DT_INST_PROP(inst, delta_offset_weight),
633647#else
634648#define ITDS_2533020201601_CONFIG_DELTA (inst )
635649#endif /* CONFIG_WSEN_ITDS_2533020201601_DELTA */
636650
637- #define ITDS_2533020201601_CONFIG_LN (inst ) \
638- .low_noise = COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, low_noise), \
639- ((ITDS_state_t)ITDS_enable), ((ITDS_state_t)ITDS_disable)),
640-
641- #define ITDS_2533020201601_CONFIG_COMMON (inst ) \
642- .odr = (ITDS_outputDataRate_t)(DT_INST_ENUM_IDX(inst, odr) + 1), \
643- .op_mode = (ITDS_operatingMode_t)DT_INST_ENUM_IDX(inst, op_mode), \
644- .power_mode = (ITDS_powerMode_t)DT_INST_ENUM_IDX(inst, power_mode), \
645- .range = DT_INST_PROP(inst, range), \
646- ITDS_2533020201601_CONFIG_LN(inst) \
647- ITDS_2533020201601_CONFIG_TAP(inst) \
648- ITDS_2533020201601_CONFIG_FREEFALL(inst) \
649- ITDS_2533020201601_CONFIG_DELTA(inst) \
650- COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, events_interrupt_gpios), \
651- (ITDS_2533020201601_CFG_EVENTS_IRQ(inst)), ()) \
652- COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_interrupt_gpios), \
653- (ITDS_2533020201601_CFG_DRDY_IRQ(inst)), ())
654- /*
655- * Instantiation macros used when device is on SPI bus.
656- */
657-
658- #define ITDS_2533020201601_SPI_OPERATION \
651+ #define ITDS_2533020201601_CONFIG_LN (inst ) \
652+ .low_noise = COND_CODE_1( \
653+ DT_INST_NODE_HAS_PROP(inst, low_noise), \
654+ ((ITDS_state_t)ITDS_enable), \
655+ ((ITDS_state_t)ITDS_disable) \
656+ ),
657+
658+ #define ITDS_2533020201601_CONFIG_COMMON (inst ) \
659+ .odr = (ITDS_outputDataRate_t)(DT_INST_ENUM_IDX(inst, odr) + 1), \
660+ .op_mode = (ITDS_operatingMode_t)DT_INST_ENUM_IDX(inst, op_mode), \
661+ .power_mode = power_mode_map[DT_INST_ENUM_IDX(inst, power_mode)], \
662+ .range = DT_INST_PROP(inst, range), \
663+ ITDS_2533020201601_CONFIG_LN(inst) \
664+ ITDS_2533020201601_CONFIG_TAP(inst) \
665+ ITDS_2533020201601_CONFIG_FREEFALL(inst) \
666+ ITDS_2533020201601_CONFIG_DELTA(inst) \
667+ COND_CODE_1( \
668+ DT_INST_NODE_HAS_PROP(inst, events_interrupt_gpios), \
669+ (ITDS_2533020201601_CFG_EVENTS_IRQ(inst)), \
670+ () \
671+ ) \
672+ COND_CODE_1( \
673+ DT_INST_NODE_HAS_PROP(inst, drdy_interrupt_gpios), \
674+ (ITDS_2533020201601_CFG_DRDY_IRQ(inst)), \
675+ () \
676+ )
677+
678+ /* SPI configuration */
679+ #define ITDS_2533020201601_SPI_OPERATION \
659680 (SPI_WORD_SET(8) | SPI_OP_MODE_MASTER | SPI_MODE_CPOL | SPI_MODE_CPHA)
660681
661682#define ITDS_2533020201601_CONFIG_SPI (inst ) \
@@ -665,38 +686,55 @@ int itds_2533020201601_init(const struct device *dev)
665686 }, \
666687 ITDS_2533020201601_CONFIG_COMMON(inst)}
667688
668- /*
669- * Instantiation macros used when device is on I2C bus.
670- */
671-
672- #define ITDS_2533020201601_CONFIG_I2C (inst ) \
673- {.bus_cfg = \
674- { \
675- .i2c = I2C_DT_SPEC_INST_GET(inst), \
676- }, \
677- ITDS_2533020201601_CONFIG_COMMON(inst)}
678-
679- #define ITDS_2533020201601_CONFIG_WE_INTERFACE (inst ) \
680- {COND_CODE_1(DT_INST_ON_BUS(inst, i2c), \
681- (.sensor_interface = {.interfaceType = WE_i2c}), \
682- ()) COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
683- (.sensor_interface = {.interfaceType = WE_spi}), \
684- ()) }
685-
686- /*
687- * Main instantiation macro. Use of COND_CODE_1() selects the right
688- * bus-specific macro at preprocessor time.
689- */
690- #define ITDS_2533020201601_DEFINE (inst ) \
691- static struct itds_2533020201601_data itds_2533020201601_data_##inst = \
692- ITDS_2533020201601_CONFIG_WE_INTERFACE(inst); \
693- static const struct itds_2533020201601_config itds_2533020201601_config_##inst = \
694- COND_CODE_1(DT_INST_ON_BUS(inst, i2c), (ITDS_2533020201601_CONFIG_I2C(inst)), ()) \
695- COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
696- (ITDS_2533020201601_CONFIG_SPI(inst)), ()); \
697- SENSOR_DEVICE_DT_INST_DEFINE(inst, itds_2533020201601_init, NULL, \
698- &itds_2533020201601_data_##inst, \
699- &itds_2533020201601_config_##inst, POST_KERNEL, \
700- CONFIG_SENSOR_INIT_PRIORITY, &itds_2533020201601_driver_api);
689+ /* I2C configuration */
690+ #define ITDS_2533020201601_CONFIG_I2C (inst ) \
691+ { \
692+ .bus_cfg = { \
693+ .i2c = I2C_DT_SPEC_INST_GET(inst), \
694+ }, \
695+ ITDS_2533020201601_CONFIG_COMMON(inst) \
696+ }
697+
698+ #define ITDS_2533020201601_CONFIG_WE_INTERFACE (inst ) \
699+ { \
700+ COND_CODE_1( \
701+ DT_INST_ON_BUS(inst, i2c), \
702+ (.sensor_interface = {.interfaceType = WE_i2c}), \
703+ () \
704+ ) \
705+ COND_CODE_1( \
706+ DT_INST_ON_BUS(inst, spi), \
707+ (.sensor_interface = {.interfaceType = WE_spi}), \
708+ () \
709+ ) \
710+ }
711+
712+ /* Main instantiation macro */
713+ #define ITDS_2533020201601_DEFINE (inst ) \
714+ static struct itds_2533020201601_data itds_2533020201601_data_##inst = \
715+ ITDS_2533020201601_CONFIG_WE_INTERFACE(inst); \
716+ static const struct itds_2533020201601_config itds_2533020201601_config_##inst = \
717+ COND_CODE_1( \
718+ DT_INST_ON_BUS(inst, i2c), \
719+ (ITDS_2533020201601_CONFIG_I2C(inst)), \
720+ () \
721+ ) \
722+ COND_CODE_1( \
723+ DT_INST_ON_BUS(inst, spi), \
724+ (ITDS_2533020201601_CONFIG_SPI(inst)), \
725+ () \
726+ ); \
727+ SENSOR_DEVICE_DT_INST_DEFINE( \
728+ inst, \
729+ itds_2533020201601_init, \
730+ NULL, \
731+ &itds_2533020201601_data_##inst, \
732+ &itds_2533020201601_config_##inst, \
733+ POST_KERNEL, \
734+ CONFIG_SENSOR_INIT_PRIORITY, \
735+ &itds_2533020201601_driver_api \
736+ );
701737
702738DT_INST_FOREACH_STATUS_OKAY (ITDS_2533020201601_DEFINE )
739+
740+ /* clang-format on */
0 commit comments