You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2022/02/14 09:13:59 UTC

[GitHub] [mynewt-core] kasjer commented on a change in pull request #1801: Added support to Infineon DPS368/3xx barometric pressure sensor.

kasjer commented on a change in pull request #1801:
URL: https://github.com/apache/mynewt-core/pull/1801#discussion_r805636664



##########
File path: hw/drivers/sensors/dps368/src/dps368.c
##########
@@ -0,0 +1,1026 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdint.h>
+#include <syscfg/syscfg.h>
+#include <math.h>
+
+#include "sensor/sensor.h"
+#include "sensor/temperature.h"
+#include "sensor/pressure.h"
+
+
+#include "dps368/dps368.h"
+#include "dps368_priv.h"
+
+
+#if !MYNEWT_VAL(BUS_DRIVER_PRESENT)
+static struct hal_spi_settings spi_dps368_settings_s = {
+    .data_order = HAL_SPI_MSB_FIRST,
+    .data_mode  = HAL_SPI_MODE3,
+    .baudrate   = 4000,
+    .word_size  = HAL_SPI_WORD_SIZE_8BIT,
+};
+#endif
+
+/* Exports for the sensor API */
+static int dps368_sensor_read(struct sensor *, sensor_type_t,
+        sensor_data_func_t, void *, uint32_t);
+static int dps368_sensor_get_config(struct sensor *, sensor_type_t,
+        struct sensor_cfg *);
+static int dps368_sensor_set_config(struct sensor *, void *);
+
+static const struct sensor_driver dps368_sensor_driver_s = {
+    .sd_read = dps368_sensor_read,
+    .sd_get_config = dps368_sensor_get_config,
+    .sd_set_config = dps368_sensor_set_config,
+    .sd_reset = dps368_soft_reset,
+};
+
+
+
+/*Local helper functions implementations*/
+/**
+ * Verify sensor on bus
+ *
+ * @param The sensor interface
+ * @param ptr to indicate if sensor hw is ready
+ * @return 0 on success, non-zero on failure
+ */
+static int
+dps368_verify_sensor(struct sensor_itf *itf, uint8_t *sensor_exist)
+{
+    int rc;
+    uint8_t hwid;
+
+    rc = dps368_get_hwid(itf,&hwid);
+
+    if (rc) {
+        *sensor_exist = 0;
+        return rc;
+    }
+
+    if (hwid == IFX_DPS368_DEV_PROD_REVID) {
+        *sensor_exist = 1;
+    } else {
+        *sensor_exist = 0;
+    }
+
+    return 0;
+
+}
+
+/**
+ * Check status to see if the sensor is ready finishing self init after boot/soft-reset
+ * typical time is 40 ms after reboot/soft-reset
+ * access/use sensor after this
+ * @param The sensor interface
+ * @param ptr to indicate if sensor hw is ready after initialization
+ * @return 0 on success, non-zero on failure
+ */
+static int
+dps368_is_init_complete(struct sensor_itf *itf, uint8_t *ready)
+{
+    uint8_t status;
+    int rc;
+
+    rc = dps368_read_regs(itf, IFX_DPS368_MEAS_CFG_REG_ADDR, &status,
+            IFX_DPS368_MEAS_CFG_REG_LEN);
+
+    if (rc) {
+        return rc;
+    }
+
+    if (status & IFX_DPS368_MEAS_CFG_REG_SEN_RDY_VAL) {
+        *ready = 1;
+    } else {
+        *ready = 0;
+    }
+
+    return 0;
+
+}
+
+/**
+ * Check status to see if the sensor is ready trim
+ * typical time is 12ms after reboot/soft-reset
+ * read calib data after this
+ * @param The sensor interface
+ * @param ptr to indicate if sensor hw is ready after initialization
+ * @return 0 on success, non-zero on failure
+ */
+static int
+dps368_is_trim_complete(struct sensor_itf *itf, uint8_t *ready)
+{
+    uint8_t status;
+    int rc;
+
+    rc = dps368_read_regs(itf, IFX_DPS368_MEAS_CFG_REG_ADDR, &status,
+            IFX_DPS368_MEAS_CFG_REG_LEN);
+
+    if (rc) {
+        *ready = 0;
+        return rc;
+    }
+
+    if (status & IFX_DPS368_MEAS_CFG_REG_COEF_RDY_VAL) {
+        *ready = 1;
+    } else {
+        *ready = 0;
+    }
+
+    return 0;
+}
+
+/**
+ * Prepare calibration coefficients once sensor is ready
+ *
+ * @param The sensor interface
+ * @param ptr read calib reg data
+ * @return 0 on success, non-zero on failure
+ */
+static int
+dps368_prepare_calib_coeff(struct sensor_itf *itf,
+        dps3xx_cal_coeff_regs_s *coeffs, dps3xx_temperature_src_e *src_t)
+{
+     uint8_t read_buffer[IFX_DPS368_COEF_LEN] = {0};
+     int rc;
+
+     rc = dps368_read_regs(itf, IFX_DPS368_COEF_REG_ADDR, read_buffer,
+             IFX_DPS368_COEF_LEN);
+
+     if (rc) {
+         return rc;
+     }
+
+     coeffs->C0 = (read_buffer[0] << 4) + ((read_buffer[1] >>4) & 0x0F);
+     if(coeffs->C0 > POW_2_11_MINUS_1)
+         coeffs->C0 = coeffs->C0 - POW_2_12;
+
+     coeffs->C1 = (read_buffer[2] + ((read_buffer[1] & 0x0F)<<8));
+     if(coeffs->C1 > POW_2_11_MINUS_1)
+         coeffs->C1 = coeffs->C1 - POW_2_12;
+
+     coeffs->C00 = ((read_buffer[4]<<4) + (read_buffer[3]<<12)) + ((read_buffer[5]>>4) & 0x0F);
+     if(coeffs->C00 > POW_2_19_MINUS_1)
+         coeffs->C00 = coeffs->C00 -POW_2_20;
+
+     coeffs->C10 = ((read_buffer[5] & 0x0F)<<16) + read_buffer[7] + (read_buffer[6]<<8);
+     if(coeffs->C10 > POW_2_19_MINUS_1)
+         coeffs->C10 = coeffs->C10 - POW_2_20;
+
+     coeffs->C01 = (read_buffer[9] + (read_buffer[8]<<8));
+     if(coeffs->C01 > ((POW_2_15_MINUS_1)))
+         coeffs->C01 = coeffs->C01 - POW_2_16;
+
+     coeffs->C11 = (read_buffer[11] + (read_buffer[10]<<8));
+     if(coeffs->C11 > ((POW_2_15_MINUS_1)))
+         coeffs->C11 = coeffs->C11 - POW_2_16;
+
+     coeffs->C20 = (read_buffer[13] + (read_buffer[12]<<8));
+     if(coeffs->C20 > ((POW_2_15_MINUS_1)))
+         coeffs->C20 = coeffs->C20 - POW_2_16;
+
+     coeffs->C21 = (read_buffer[15] + (read_buffer[14]<<8));
+     if(coeffs->C21 > ((POW_2_15_MINUS_1)))
+         coeffs->C21 = coeffs->C21 - POW_2_16;
+
+     coeffs->C30 = (read_buffer[17] + (read_buffer[16]<<8));
+     if(coeffs->C30 > ((POW_2_15_MINUS_1)))
+         coeffs->C30 = coeffs->C30 - POW_2_16;
+
+     memset(read_buffer, 0, sizeof(IFX_DPS368_COEF_LEN));
+
+     rc = dps368_read_regs(itf, IFX_DPS368_TMP_COEF_SRCE_REG_ADDR, read_buffer,
+             IFX_DPS368_TMP_CFG_REG_LEN);
+
+     if (rc) {
+         return rc;
+     }
+
+    if ((read_buffer[0] >> IFX_DPS368_TMP_COEF_SRCE_REG_POS) & 1) {
+        *src_t = TMP_EXT_MEMS;
+    } else {
+        *src_t = TMP_EXT_ASIC;
+    }
+
+     return 0;
+}
+
+/**
+ * Choose appropriate scalling factor for given OSR
+ *
+ * @param selected osr
+ * @return chosen scaling coefficient for given osr
+ */
+static dps3xx_scaling_coeffs_e
+dps368_get_scaling_coef (dps3xx_osr_e osr)
+{
+    dps3xx_scaling_coeffs_e scaling_coeff;
+
+    switch (osr){
+
+    case OSR_1:
+        scaling_coeff = OSR_SF_1;
+        break;
+    case OSR_2:
+        scaling_coeff = OSR_SF_2;
+        break;
+    case OSR_4:
+        scaling_coeff = OSR_SF_4;
+        break;
+    case OSR_8:
+        scaling_coeff = OSR_SF_8;
+        break;
+    case OSR_16:
+        scaling_coeff = OSR_SF_16;
+        break;
+    case OSR_32:
+        scaling_coeff = OSR_SF_32;
+        break;
+    case OSR_64:
+        scaling_coeff = OSR_SF_64;
+        break;
+    case OSR_128:
+        scaling_coeff = OSR_SF_128;
+        break;
+    default:
+        scaling_coeff = OSR_SF_1;
+        break;
+    }
+
+    return scaling_coeff;
+}
+
+/**
+ * post init sequence
+ *
+ * @param ptr sensor interface
+ * @return 0 on success else non 0 val
+ */
+static int
+dps368_set_oem_parameters(struct sensor_itf *itf)
+{
+    int rc;
+
+    if ((rc = dps368_write_reg(itf, (uint8_t) 0x0E, (uint8_t) 0xA5))) {
+        return rc;
+    }
+
+    if ((rc = dps368_write_reg(itf, (uint8_t) 0x0F, (uint8_t) 0x96))) {
+        return rc;
+    }
+
+    if ((rc = dps368_write_reg(itf, (uint8_t) 0x62, (uint8_t) 0x02))) {
+        return rc;
+    }
+
+    if ((rc = dps368_write_reg(itf, (uint8_t) 0x0E, (uint8_t) 0x00))) {
+        return rc;
+    }
+
+    if ((rc = dps368_write_reg(itf, (uint8_t) 0x0F, (uint8_t) 0x00))) {
+        return rc;
+    }
+
+    DPS368_LOG(INFO,"DPS368:OEM Parameters are set\n");
+
+    return 0;
+
+}

Review comment:
       @saumitra-chafekar I was long time ago, but maybe you remember what this code does to sensor.
   Registers 0x0E, 0x0F, 0x62, are not documented in Infineon datasheet and this code is not present in Infineon driver for Arduino.
   
   Is this code really needed? Can it be safely removed?
   Thanks




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@mynewt.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org