Skip to content

Commit a87ed22

Browse files
SPVinayakhenrikbrixandersen
authored andcommitted
drivers: sensor: adxl367: fix for temperature readout
Fix to ensure temperature is sampled at the same timestamp as x,y,z axis data. Signed-off-by: Vinayak Sunilkumar Priyadarsini <[email protected]>
1 parent 43962de commit a87ed22

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

drivers/sensor/adi/adxl367/adxl367.c

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -743,29 +743,38 @@ int adxl367_get_accel_data(const struct device *dev,
743743
}
744744

745745
/**
746-
* @brief Reads the raw temperature of the device. If ADXL367_TEMP_EN is not
747-
* set, use adxl367_temp_read_en() first to enable temperature reading.
746+
* @brief Reads the raw temperature of the device.
748747
*
749-
* @param dev - The device structure.
750-
* @param raw_temp - Raw value of temperature.
748+
* If ADXL367_TEMP_EN is not set, use adxl367_temp_read_en() first to enable temperature reading.
749+
* Optionally checks the data ready status before reading temperature.
750+
*
751+
* @param dev - The device structure.
752+
* @param raw_temp - Raw value of temperature.
753+
* @param check_data_rdy - If true, waits for data ready status before reading temperature.
754+
* If false, reads temperature directly. Note: The DATA_READY bit is
755+
* cleared when data is read; set this flag to false if temperature from
756+
* the same sample frame is needed after performing an axis(x,y,z) read.
751757
*
752758
* @return 0 in case of success, negative error code otherwise.
753759
*/
754-
int adxl367_get_temp_data(const struct device *dev, int16_t *raw_temp)
760+
int adxl367_get_temp_data(const struct device *dev, int16_t *raw_temp, bool check_data_rdy)
755761
{
756762
int ret;
757763
uint8_t temp[2] = { 0 };
758-
uint8_t reg_data, nready = 1U;
759764
struct adxl367_data *data = dev->data;
760765

761-
while (nready != 0) {
762-
ret = data->hw_tf->read_reg(dev, ADXL367_STATUS, &reg_data);
763-
if (ret != 0) {
764-
return ret;
765-
}
766+
if (check_data_rdy) {
767+
uint8_t reg_data, nready = 1U;
766768

767-
if ((reg_data & ADXL367_STATUS_DATA_RDY) != 0) {
768-
nready = 0U;
769+
while (nready != 0) {
770+
ret = data->hw_tf->read_reg(dev, ADXL367_STATUS, &reg_data);
771+
if (ret != 0) {
772+
return ret;
773+
}
774+
775+
if ((reg_data & ADXL367_STATUS_DATA_RDY) != 0) {
776+
nready = 0U;
777+
}
769778
}
770779
}
771780

@@ -879,8 +888,14 @@ static int adxl367_sample_fetch(const struct device *dev,
879888
if (ret != 0) {
880889
return ret;
881890
}
891+
const struct adxl367_dev_config *cfg = dev->config;
892+
bool check_temp_data_ready = false;
882893

883-
return adxl367_get_temp_data(dev, &data->temp_val);
894+
if (cfg->temp_en) {
895+
ret = adxl367_get_temp_data(dev, &data->temp_val, check_temp_data_ready);
896+
}
897+
898+
return ret;
884899
}
885900
#ifdef CONFIG_SENSOR_ASYNC_API
886901
void adxl367_accel_convert(struct sensor_value *val, int16_t value,

drivers/sensor/adi/adxl367/adxl367.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ void adxl367_submit(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe);
442442
int adxl367_get_decoder(const struct device *dev, const struct sensor_decoder_api **decoder);
443443
int adxl367_get_accel_data(const struct device *dev,
444444
struct adxl367_xyz_accel_data *accel_data);
445-
int adxl367_get_temp_data(const struct device *dev, int16_t *raw_temp);
445+
int adxl367_get_temp_data(const struct device *dev, int16_t *raw_temp, bool check_data_rdy);
446446
void adxl367_accel_convert(struct sensor_value *val, int16_t value,
447447
enum adxl367_range range);
448448
void adxl367_temp_convert(struct sensor_value *val, int16_t value);

drivers/sensor/adi/adxl367/adxl367_rtio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ static void adxl367_submit_fetch(struct rtio_iodev_sqe *iodev_sqe)
4444
}
4545

4646
enc_data->xyz.range = data->range;
47+
bool check_temp_data_ready = false;
4748

48-
rc = adxl367_get_temp_data(dev, &enc_data->raw_temp);
49+
rc = adxl367_get_temp_data(dev, &enc_data->raw_temp, check_temp_data_ready);
4950
if (rc != 0) {
5051
LOG_ERR("Failed to fetch temp samples");
5152
rtio_iodev_sqe_err(iodev_sqe, rc);

0 commit comments

Comments
 (0)