You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/01/23 13:18:19 UTC

[GitHub] [incubator-nuttx] acassis opened a new pull request #5317: sensors: Add support to MS5611 barometer

acassis opened a new pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317


   ## Summary
   Add support to MS5611 barometer
   ## Impact
   User will be able to use MS5611 with NuttX
   ## Testing
   ESP32-DevkitC
   


-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790339884



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,703 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + (i)*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ERROR;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ERROR;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, data);
+      if (ret < 0)
+        {
+          snerr("ms5611_read16 failed\n");
+          return ret;
+        }
+
+      prom[i] = (uint16_t) data[0] << 8 | (uint16_t) data[1];
+    }
+
+  /* Get the 4-bit CRC from PROM */
+
+  crc = (uint8_t)(prom[7] & 0xf);
+
+  /* Verify if the calculated CRC is equal to PROM's CRC */
+
+  if (crc != msxxxx_crc4(prom, 7, 0xff))
+    {
+      snerr("ERROR: Calculated CRC different from PROM's CRC!\n");
+      return -ENODEV;
+    }
+
+  /* Fill read calibration coefficients */
+
+  priv->calib.c1 = prom[1];
+  priv->calib.c2 = prom[2];
+  priv->calib.c3 = prom[3];
+  priv->calib.c4 = prom[4];
+  priv->calib.c5 = prom[5];
+  priv->calib.c6 = prom[6];
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_temp
+ *
+ * Description:
+ *   calculate compensate temperature
+ *
+ * Input Parameters:
+ *   temp - uncompensate value of temperature.
+ *
+ * Returned Value:
+ *   calculate result of compensate temperature.
+ *
+ ****************************************************************************/
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int32_t dt;
+
+  /* dt = d1 - c5 * 256 */
+
+  dt = temp - ((int32_t) c->c5 << 8);
+
+  /* temp = 2000 + (dt * c6) / 8388608 */
+
+  temp = 2000 + (((int64_t) (dt * c->c6)) >> 23);
+
+  /* Save dt that will be used for pressure calibration */
+
+  *deltat = dt;
+
+  return temp;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_press
+ *
+ * Description:
+ *   calculate compensate pressure
+ *
+ * Input Parameters:
+ *   press - uncompensate value of pressure.
+ *
+ * Returned Value:
+ *   calculate result of compensate pressure.
+ *
+ ****************************************************************************/
+
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int64_t off;
+  int64_t sens;
+
+  off = ((int64_t) c->c2 * 65536) + ((int64_t) (c->c4 * dt) / 128);
+  sens = ((int64_t) c->c1 * 32768) + ((int64_t) (c->c3 * dt) / 256);
+  press = (((press * sens) / 2097152) - off) / 32768;
+
+  return press;
+}
+
+/****************************************************************************
+ * Name: ms5611_set_interval
+ ****************************************************************************/
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+
+  priv->interval = *period_us;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_activate
+ ****************************************************************************/
+
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable)
+{
+  bool start_thread = false;
+  struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)lower;
+
+  if (enable)
+    {
+      if (!priv->enabled)
+        {
+          start_thread = true;
+        }
+    }
+
+  priv->enabled = enable;
+
+  if (start_thread == true)
+    {
+      /* Wake up the thread */
+
+      nxsem_post(&priv->run);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_fetch
+ ****************************************************************************/
+
+/* N.B. When fetch is enabled the sensortest doesn't respect the
+ * interval (-i) parameter, so let keep it comment until further
+ * discussion about the "issue".
+ */
+
+#if 0
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+  struct sensor_event_baro baro_data;
+
+  if (buflen != sizeof(baro_data))
+    {
+      return -EINVAL;
+    }
+
+  baro_measure_read(priv, &baro_data);
+
+  memcpy(buffer, &baro_data, sizeof(baro_data));
+
+  return buflen;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_register
+ *
+ * Description:
+ *   Register the MS5611 character device
+ *
+ * Input Parameters:
+ *   i2c     - An instance of the I2C interface to use to communicate with
+ *             MS5611
+ *   devno   - Instance number for driver
+ *   addr    - The I2C address of the MS5611.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int ms5611_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr)
+{
+  FAR struct ms5611_dev_s *priv;
+  FAR char *argv[2];
+  char arg1[32];
+
+  int ret;
+
+  /* Initialize the MS5611 device structure */
+
+  priv = (FAR struct ms5611_dev_s *)kmm_zalloc(sizeof(struct ms5611_dev_s));
+  if (priv == NULL)
+    {
+      snerr("Failed to allocate instance\n");
+      return -ENOMEM;
+    }
+
+  priv->i2c      = i2c;
+  priv->addr     = addr;
+  priv->freq     = CONFIG_MS5611_I2C_FREQUENCY;
+  priv->interval = 1000000; /* Default interval 1s */
+
+  nxsem_init(&priv->run, 0, 0);
+  nxsem_init(&priv->exclsem, 0, 1);
+
+  priv->sensor_lower.ops = &g_sensor_ops;
+  priv->sensor_lower.type = SENSOR_TYPE_BAROMETER;
+
+  ret = ms5611_initialize(priv);
+  if (ret < 0)
+    {
+      snerr("Failed to initialize physical device ms5611:%d\n", ret);
+      kmm_free(priv);
+      return ret;
+    }
+
+  /* Register the character driver */
+
+  ret = sensor_register(&priv->sensor_lower, devno);
+  if (ret < 0)
+    {
+      snerr("Failed to register driver: %d\n", ret);
+      kmm_free(priv);
+    }
+
+  /* Create thread for polling sensor data */
+
+  snprintf(arg1, 16, "0x%" PRIxPTR, (uintptr_t)priv);
+  argv[0] = arg1;
+  argv[1] = NULL;
+  ret = kthread_create("ms5611_thread", SCHED_PRIORITY_DEFAULT,
+                       CONFIG_MS5611_THREAD_STACKSIZE,
+                       ms5611_thread, argv);
+  if (ret < 0)
+    {
+      snerr("Failed to create the notification kthread!\n");
+      kmm_free(priv);

Review comment:
       Do you think we need `sensor_unregister` in case if `kthread_create` fails?




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790317540



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);

Review comment:
       Done! Thnx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790330326



##########
File path: boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
##########
@@ -72,6 +72,10 @@
 #include "stm32_bmp180.h"
 #endif
 
+#ifdef CONFIG_SENSORS_MS5611
+#include "stm32_ms5611.h"

Review comment:
       Not need, please note that all other include doesn't have this space:
   ```
   #ifdef CONFIG_SENSORS_BMP180
   #include "stm32_bmp180.h"
   #endif
   
   #ifdef CONFIG_SENSORS_MS5611
   #include "stm32_ms5611.h"
   #endif
   
   #ifdef CONFIG_SENSORS_MAX6675
   #include "stm32_max6675.h"
   #endif
   
   #ifdef CONFIG_INPUT_NUNCHUCK
   #include "stm32_nunchuck.h"
   #endif
   
   #ifdef CONFIG_SENSORS_ZEROCROSS
   #include "stm32_zerocross.h"
   #endif
   ```




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790316652



##########
File path: boards/xtensa/esp32/common/src/esp32_ms5611.c
##########
@@ -0,0 +1,112 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_ms5611.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");

Review comment:
       Done! Thnx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790320782



##########
File path: include/nuttx/sensors/msxxxx_crc4.h
##########
@@ -0,0 +1,82 @@
+/****************************************************************************
+ * include/nuttx/sensors/msxxxx_crc4.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+#define __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms58xx_crc
+ *
+ * Description:
+ *   Calculate the CRC.
+ *
+ ****************************************************************************/
+
+static uint8_t msxxxx_crc4(FAR uint16_t *src,
+                           uint8_t crcndx,
+                           uint16_t crcmask)
+{
+  uint16_t cnt;
+  uint16_t n_rem;
+  uint16_t crc_read;
+  uint8_t n_bit;
+
+  n_rem = 0x00;
+  crc_read = src[crcndx];
+  src[crcndx] &= ~crcmask;
+
+  for (cnt = 0; cnt < 16; cnt++)
+    {
+      if (cnt % 2 == 1)
+        {
+          n_rem ^= (uint16_t)((src[cnt >> 1]) & 0x00ff);

Review comment:
       ```suggestion
             n_rem ^= (src[cnt >> 1]) & 0x00ff;
   ```
   since `src`is a `uint16_t *`

##########
File path: include/nuttx/sensors/msxxxx_crc4.h
##########
@@ -0,0 +1,82 @@
+/****************************************************************************
+ * include/nuttx/sensors/msxxxx_crc4.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+#define __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms58xx_crc
+ *
+ * Description:
+ *   Calculate the CRC.
+ *
+ ****************************************************************************/
+
+static uint8_t msxxxx_crc4(FAR uint16_t *src,
+                           uint8_t crcndx,
+                           uint16_t crcmask)
+{
+  uint16_t cnt;
+  uint16_t n_rem;
+  uint16_t crc_read;
+  uint8_t n_bit;
+
+  n_rem = 0x00;
+  crc_read = src[crcndx];
+  src[crcndx] &= ~crcmask;
+
+  for (cnt = 0; cnt < 16; cnt++)
+    {
+      if (cnt % 2 == 1)
+        {
+          n_rem ^= (uint16_t)((src[cnt >> 1]) & 0x00ff);
+        }
+      else
+        {
+          n_rem ^= (uint16_t)(src[cnt >> 1] >> 8);

Review comment:
       ```suggestion
             n_rem ^= src[cnt >> 1] >> 8;
   ```
   since `src` is a `uint16_t *`

##########
File path: include/nuttx/sensors/msxxxx_crc4.h
##########
@@ -0,0 +1,82 @@
+/****************************************************************************
+ * include/nuttx/sensors/msxxxx_crc4.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+#define __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms58xx_crc
+ *
+ * Description:
+ *   Calculate the CRC.
+ *
+ ****************************************************************************/
+
+static uint8_t msxxxx_crc4(FAR uint16_t *src,
+                           uint8_t crcndx,
+                           uint16_t crcmask)
+{
+  uint16_t cnt;
+  uint16_t n_rem;
+  uint16_t crc_read;
+  uint8_t n_bit;
+
+  n_rem = 0x00;
+  crc_read = src[crcndx];
+  src[crcndx] &= ~crcmask;
+
+  for (cnt = 0; cnt < 16; cnt++)
+    {
+      if (cnt % 2 == 1)
+        {
+          n_rem ^= (uint16_t)((src[cnt >> 1]) & 0x00ff);
+        }
+      else
+        {
+          n_rem ^= (uint16_t)(src[cnt >> 1] >> 8);
+        }
+
+      for (n_bit = 8; n_bit > 0; n_bit--)
+        {
+          if (n_rem & (0x8000))
+            {
+              n_rem = (n_rem << 1) ^ 0x3000;
+            }
+          else
+            {
+              n_rem = (n_rem << 1);
+            }
+        }
+    }
+
+  n_rem = (0x000f & (n_rem >> 12));
+  src[crcndx] = crc_read;
+  return (n_rem ^ 0x00);

Review comment:
       ```suggestion
     return n_rem ^ 0x00;
   ```

##########
File path: include/nuttx/sensors/msxxxx_crc4.h
##########
@@ -0,0 +1,82 @@
+/****************************************************************************
+ * include/nuttx/sensors/msxxxx_crc4.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+#define __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms58xx_crc
+ *
+ * Description:
+ *   Calculate the CRC.
+ *
+ ****************************************************************************/
+
+static uint8_t msxxxx_crc4(FAR uint16_t *src,
+                           uint8_t crcndx,
+                           uint16_t crcmask)
+{
+  uint16_t cnt;
+  uint16_t n_rem;
+  uint16_t crc_read;
+  uint8_t n_bit;
+
+  n_rem = 0x00;
+  crc_read = src[crcndx];
+  src[crcndx] &= ~crcmask;
+
+  for (cnt = 0; cnt < 16; cnt++)
+    {
+      if (cnt % 2 == 1)
+        {
+          n_rem ^= (uint16_t)((src[cnt >> 1]) & 0x00ff);
+        }
+      else
+        {
+          n_rem ^= (uint16_t)(src[cnt >> 1] >> 8);
+        }
+
+      for (n_bit = 8; n_bit > 0; n_bit--)
+        {
+          if (n_rem & (0x8000))
+            {
+              n_rem = (n_rem << 1) ^ 0x3000;
+            }
+          else
+            {
+              n_rem = (n_rem << 1);
+            }
+        }
+    }
+
+  n_rem = (0x000f & (n_rem >> 12));

Review comment:
       ```suggestion
     n_rem = 0x000f & (n_rem >> 12);
   ```




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790316741



##########
File path: boards/xtensa/esp32/common/src/esp32_ms5611.c
##########
@@ -0,0 +1,112 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_ms5611.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");
+
+  /* Initialize BMP180 */
+
+  i2c = esp32_i2cbus_initialize(busno);
+
+  if (i2c)

Review comment:
       Done! Thnx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790340357



##########
File path: boards/arm/stm32/common/include/stm32_ms5611.h
##########
@@ -0,0 +1,80 @@
+/****************************************************************************
+ * boards/arm/stm32/common/include/stm32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MS5611_H
+#define __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MS5611_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.

Review comment:
       ```suggestion
    *   Initialize and register the MS5611 Pressure Sensor driver.
   ```




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790317226



##########
File path: boards/arm/stm32/common/src/stm32_ms5611.c
##########
@@ -0,0 +1,110 @@
+/****************************************************************************
+ * boards/arm/stm32/common/src/stm32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "stm32_i2c.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");

Review comment:
       Done! Thnx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790315815



##########
File path: boards/arm/stm32/common/src/stm32_ms5611.c
##########
@@ -0,0 +1,110 @@
+/****************************************************************************
+ * boards/arm/stm32/common/src/stm32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "stm32_i2c.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/

Review comment:
       Done, thx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790315786



##########
File path: boards/arm/stm32/common/include/stm32_ms5611.h
##########
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * boards/arm/stm32/common/include/stm32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H

Review comment:
       Done, thx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790328364



##########
File path: include/nuttx/sensors/msxxxx_crc4.h
##########
@@ -0,0 +1,82 @@
+/****************************************************************************
+ * include/nuttx/sensors/msxxxx_crc4.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+#define __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms58xx_crc
+ *
+ * Description:
+ *   Calculate the CRC.
+ *
+ ****************************************************************************/
+
+static uint8_t msxxxx_crc4(FAR uint16_t *src,
+                           uint8_t crcndx,
+                           uint16_t crcmask)
+{
+  uint16_t cnt;
+  uint16_t n_rem;
+  uint16_t crc_read;
+  uint8_t n_bit;
+
+  n_rem = 0x00;
+  crc_read = src[crcndx];
+  src[crcndx] &= ~crcmask;
+
+  for (cnt = 0; cnt < 16; cnt++)
+    {
+      if (cnt % 2 == 1)
+        {
+          n_rem ^= (src[cnt >> 1]) & 0x00ff;

Review comment:
       ```suggestion
             n_rem ^= src[cnt >> 1] & 0x00ff;
   ```




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790328488



##########
File path: boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
##########
@@ -72,6 +72,10 @@
 #include "stm32_bmp180.h"
 #endif
 
+#ifdef CONFIG_SENSORS_MS5611
+#include "stm32_ms5611.h"

Review comment:
       ```suggestion
   #  include "stm32_ms5611.h"
   ```




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790322544



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,704 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, data);
+      if (ret < 0)
+        {
+          snerr("ms5611_read16 failed\n");
+          return ret;
+        }
+
+      prom[i] = (uint16_t) data[0] << 8 | (uint16_t) data[1];
+    }
+
+  /* Get the 4-bit CRC from PROM */
+
+  crc = (uint8_t)(prom[7] & 0xf);
+
+  /* Verify if the calculated CRC is equal to PROM's CRC */
+
+  if (crc != msxxxx_crc4(prom, 7, 0xff))
+    {
+      snerr("ERROR: Calculated CRC different from PROM's CRC!\n");
+      return -ENODEV;
+    }
+
+  /* Fill read calibration coefficients */
+
+  priv->calib.c1 = prom[1];
+  priv->calib.c2 = prom[2];
+  priv->calib.c3 = prom[3];
+  priv->calib.c4 = prom[4];
+  priv->calib.c5 = prom[5];
+  priv->calib.c6 = prom[6];
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_temp
+ *
+ * Description:
+ *   calculate compensate temperature
+ *
+ * Input Parameters:
+ *   temp - uncompensate value of temperature.
+ *
+ * Returned Value:
+ *   calculate result of compensate temperature.
+ *
+ ****************************************************************************/
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int32_t dt;
+
+  /* dt = d1 - c5 * 256 */
+
+  dt = temp - ((int32_t) c->c5 << 8);
+
+  /* temp = 2000 + (dt * c6) / 8388608 */
+
+  temp = 2000 + (((int64_t) (dt * c->c6)) >> 23);
+
+  /* Save dt that will be used for pressure calibration */
+
+  *deltat = dt;
+
+  return temp;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_press
+ *
+ * Description:
+ *   calculate compensate pressure
+ *
+ * Input Parameters:
+ *   press - uncompensate value of pressure.
+ *
+ * Returned Value:
+ *   calculate result of compensate pressure.
+ *
+ ****************************************************************************/
+
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int64_t off;
+  int64_t sens;
+
+  off = ((int64_t) c->c2 * 65536) + ((int64_t) (c->c4 * dt) / 128);
+  sens = ((int64_t) c->c1 * 32768) + ((int64_t) (c->c3 * dt) / 256);
+  press = (((press * sens) / 2097152) - off) / 32768;
+
+  return press;
+}
+
+/****************************************************************************
+ * Name: ms5611_set_interval
+ ****************************************************************************/
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+
+  priv->interval = *period_us;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_activate
+ ****************************************************************************/
+
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable)
+{
+  bool start_thread = false;
+  struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)lower;
+
+  if (enable)
+    {
+      if (!priv->enabled)
+        {
+          start_thread = true;
+        }
+    }
+
+  priv->enabled = enable;
+
+  if (start_thread == true)
+    {
+      /* Wake up the thread */
+
+      nxsem_post(&priv->run);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_fetch
+ ****************************************************************************/
+
+/* N.B. When fetch is enabled the sensortest doesn't respect the
+ * interval (-i) parameter, so let keep it comment until further
+ * discussion about the "issue".
+ */
+
+#if 0
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+  struct sensor_event_baro baro_data;
+
+  if (buflen != sizeof(baro_data))
+    {
+      return -EINVAL;
+    }
+
+  baro_measure_read(priv, &baro_data);
+
+  memcpy(buffer, &baro_data, sizeof(baro_data));
+
+  return buflen;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_register
+ *
+ * Description:
+ *   Register the MS5611 character device
+ *
+ * Input Parameters:
+ *   i2c     - An instance of the I2C interface to use to communicate with
+ *             MS5611
+ *   devno   - Instance number for driver
+ *   addr    - The I2C address of the MS5611.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int ms5611_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr)
+{
+  FAR struct ms5611_dev_s *priv;
+  FAR char *argv[2];
+  char arg1[32];
+
+  int ret;
+
+  /* Initialize the MS5611 device structure */
+
+  priv = (FAR struct ms5611_dev_s *)kmm_zalloc(sizeof(struct ms5611_dev_s));
+  if (priv == NULL)
+    {
+      snerr("Failed to allocate instance\n");
+      return -ENOMEM;
+    }
+
+  priv->i2c      = i2c;
+  priv->addr     = addr;
+  priv->freq     = CONFIG_MS5611_I2C_FREQUENCY;
+  priv->interval = 1000000; /* Default interval 1s */
+
+  nxsem_init(&priv->run, 0, 0);
+  nxsem_init(&priv->exclsem, 0, 1);
+  nxsem_set_protocol(&priv->run, SEM_PRIO_NONE);
+  nxsem_set_protocol(&priv->exclsem, SEM_PRIO_NONE);
+
+  priv->sensor_lower.ops = &g_sensor_ops;
+  priv->sensor_lower.type = SENSOR_TYPE_BAROMETER;
+
+  ret = ms5611_initialize(priv);
+  if (ret < 0)
+    {
+      snerr("Failed to initialize physical device ms5611:%d\n", ret);
+      kmm_free(priv);
+      return ret;
+    }
+
+  /* Register the character driver */
+
+  ret = sensor_register(&priv->sensor_lower, devno);
+  if (ret < 0)
+    {
+      snerr("Failed to register driver: %d\n", ret);
+      kmm_free(priv);
+    }
+
+  /* Create thread for polling sensor data */
+
+  snprintf(arg1, 16, "0x%" PRIxPTR, (uintptr_t)priv);
+  argv[0] = arg1;
+  argv[1] = NULL;
+  ret = kthread_create("ms5611_thread", SCHED_PRIORITY_DEFAULT,
+                       CONFIG_MS5611_THREAD_STACKSIZE,
+                       ms5611_thread, argv);
+  if (ret < 0)
+    {
+      snerr("Failed to create the notification kthread!\n");
+      return ret;

Review comment:
       Good catch! Thank you!




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790298071



##########
File path: boards/arm/stm32/common/src/stm32_ms5611.c
##########
@@ -0,0 +1,110 @@
+/****************************************************************************
+ * boards/arm/stm32/common/src/stm32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "stm32_i2c.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");
+
+  /* Initialize I2C */
+
+  i2c = stm32_i2cbus_initialize(busno);
+
+  if (i2c)
+    {
+      /* Then try to register the barometer sensor in I2C0 */
+
+      snprintf(devpath, 12, "/dev/press%d", devno);

Review comment:
       ```suggestion
         snprintf(devpath, sizeof(devpath), "/dev/press%d", devno);
   ```

##########
File path: boards/arm/stm32/common/src/stm32_ms5611.c
##########
@@ -0,0 +1,110 @@
+/****************************************************************************
+ * boards/arm/stm32/common/src/stm32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "stm32_i2c.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/

Review comment:
       can be removed

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, (int32_t *) &deltat);

Review comment:
       ```suggestion
     temp = ms5611_compensate_temp(priv, temp, &deltat);
   ```

##########
File path: boards/xtensa/esp32/common/include/esp32_ms5611.h
##########
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/include/esp32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/

Review comment:
       can be removed

##########
File path: include/nuttx/sensors/ms5611.h
##########
@@ -0,0 +1,101 @@
+/****************************************************************************
+ * include/nuttx/sensors/ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_SENSORS_MS5611

Review comment:
       ```suggestion
   #ifndef __INCLUDE_NUTTX_SENSORS_MS5611_H
   ```

##########
File path: boards/arm/stm32/common/include/stm32_ms5611.h
##########
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * boards/arm/stm32/common/include/stm32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H

Review comment:
       Please fix

##########
File path: boards/arm/stm32/common/include/stm32_ms5611.h
##########
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * boards/arm/stm32/common/include/stm32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/

Review comment:
       can be removed

##########
File path: boards/arm/stm32/common/src/stm32_ms5611.c
##########
@@ -0,0 +1,110 @@
+/****************************************************************************
+ * boards/arm/stm32/common/src/stm32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "stm32_i2c.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");
+
+  /* Initialize I2C */
+
+  i2c = stm32_i2cbus_initialize(busno);
+
+  if (i2c)

Review comment:
       ```suggestion
     if (i2c != NULL)
   ```

##########
File path: boards/xtensa/esp32/common/src/esp32_ms5611.c
##########
@@ -0,0 +1,112 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_ms5611.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");
+
+  /* Initialize BMP180 */
+
+  i2c = esp32_i2cbus_initialize(busno);
+
+  if (i2c)
+    {
+      /* Then try to register the barometer sensor in I2C0 */
+
+      snprintf(devpath, 12, "/dev/press%d", devno);

Review comment:
       ```suggestion
         snprintf(devpath, sizeof(devpath), "/dev/press%d", devno);
   ```

##########
File path: boards/xtensa/esp32/common/include/esp32_ms5611.h
##########
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/include/esp32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/

Review comment:
       can be removed

##########
File path: boards/xtensa/esp32/common/src/esp32_ms5611.c
##########
@@ -0,0 +1,112 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_ms5611.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");

Review comment:
       ```suggestion
     sninfo("Initializing MS5611!\n");
   ```

##########
File path: boards/xtensa/esp32/common/src/esp32_ms5611.c
##########
@@ -0,0 +1,112 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_ms5611.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");
+
+  /* Initialize BMP180 */
+
+  i2c = esp32_i2cbus_initialize(busno);
+
+  if (i2c)

Review comment:
       ```suggestion
     if (i2c != NULL)
   ```

##########
File path: boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c
##########
@@ -409,6 +413,18 @@ int esp32_bringup(void)
     }
 #endif
 
+#ifdef CONFIG_SENSORS_MS5611
+  /* Try to register MS5611 device in I2C0 as device 0: I2C addr 0x77 */
+
+  ret = board_ms5611_initialize(0, 0);
+
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "Failed to initialize BMP180 driver: %d\n", ret);

Review comment:
       ```suggestion
         syslog(LOG_ERR, "Failed to initialize MS5611 driver: %d\n", ret);
   ```

##########
File path: boards/xtensa/esp32/common/src/esp32_ms5611.c
##########
@@ -0,0 +1,112 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_ms5611.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/

Review comment:
       can be removed

##########
File path: boards/xtensa/esp32/common/src/esp32_ms5611.c
##########
@@ -0,0 +1,112 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_ms5611.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");
+
+  /* Initialize BMP180 */
+
+  i2c = esp32_i2cbus_initialize(busno);
+
+  if (i2c)
+    {
+      /* Then try to register the barometer sensor in I2C0 */
+
+      snprintf(devpath, 12, "/dev/press%d", devno);
+
+      ret = ms5611_register(i2c, devno, MS5611_ADDR0);
+      if (ret < 0)
+        {
+          snerr("ERROR: Error registering BMP180 in I2C%d\n", busno);

Review comment:
       ```suggestion
             snerr("ERROR: Error registering MS5611 in I2C%d\n", busno);
   ```

##########
File path: boards/arm/stm32/common/src/stm32_ms5611.c
##########
@@ -0,0 +1,110 @@
+/****************************************************************************
+ * boards/arm/stm32/common/src/stm32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "stm32_i2c.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");

Review comment:
       ```suggestion
     sninfo("Initializing MS5611!\n");
   ```

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))

Review comment:
       I do not see why `|| defined(CONFIG_SPI)` is needed

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);

Review comment:
       ```suggestion
     ret = ms5611_read24(priv, buffer);
   ```

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);

Review comment:
       ```suggestion
     ret = ms5611_read24(priv, buffer);
   ```

##########
File path: boards/xtensa/esp32/common/include/esp32_ms5611.h
##########
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/include/esp32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H

Review comment:
       Please fix define

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, (int32_t *) &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, (uint8_t *) &data);

Review comment:
       ```suggestion
         ret = ms5611_read16(priv, data);
   ```

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, (int32_t *) &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, (uint8_t *) &data);
+      if (ret < 0)
+        {
+          snerr("ms5611_read16 failed\n");
+          return ret;
+        }
+
+      prom[i] = (uint16_t) data[0] << 8 | (uint16_t) data[1];
+    }
+
+  /* Get the 4-bit CRC from PROM */
+
+  crc = (uint8_t)(prom[7] & 0xf);
+
+  /* Verify if the calculated CRC is equal to PROM's CRC */
+
+  if (crc != msxxxx_crc4(prom, 7, 0xff))
+    {
+      snerr("ERROR: Calculated CRC different from PROM's CRC!\n");
+      return -ENODEV;
+    }
+
+  /* Fill read calibration coefficients */
+
+  priv->calib.c1 = prom[1];
+  priv->calib.c2 = prom[2];
+  priv->calib.c3 = prom[3];
+  priv->calib.c4 = prom[4];
+  priv->calib.c5 = prom[5];
+  priv->calib.c6 = prom[6];
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_temp
+ *
+ * Description:
+ *   calculate compensate temperature
+ *
+ * Input Parameters:
+ *   temp - uncompensate value of temperature.
+ *
+ * Returned Value:
+ *   calculate result of compensate temperature.
+ *
+ ****************************************************************************/
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int32_t dt;
+
+  /* dt = d1 - c5 * 256 */
+
+  dt = temp - ((int32_t) c->c5 << 8);
+
+  /* temp = 2000 + (dt * c6) / 8388608 */
+
+  temp = 2000 + (((int64_t) (dt * c->c6)) >> 23);
+
+  /* Save dt that will be used for pressure calibration */
+
+  *deltat = dt;
+
+  return temp;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_press
+ *
+ * Description:
+ *   calculate compensate pressure
+ *
+ * Input Parameters:
+ *   press - uncompensate value of pressure.
+ *
+ * Returned Value:
+ *   calculate result of compensate pressure.
+ *
+ ****************************************************************************/
+
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int64_t off;
+  int64_t sens;
+
+  off = ((int64_t) c->c2 * 65536) + ((int64_t) (c->c4 * dt) / 128);
+  sens = ((int64_t) c->c1 * 32768) + ((int64_t) (c->c3 * dt) / 256);
+  press = (((press * sens) / 2097152) - off) / 32768;
+
+  return press;
+}
+
+/****************************************************************************
+ * Name: ms5611_set_interval
+ ****************************************************************************/
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+
+  priv->interval = *period_us;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_activate
+ ****************************************************************************/
+
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable)
+{
+  bool start_thread = false;
+  struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)lower;
+
+  if (enable)
+    {
+      if (!priv->enabled)
+        {
+          start_thread = true;
+        }
+    }
+
+  priv->enabled = enable;
+
+  if (start_thread == true)
+    {
+      /* Wake up the thread */
+
+      nxsem_post(&priv->run);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_fetch
+ ****************************************************************************/
+
+/* N.B. When fetch is enabled the sensortest doesn't respect the
+ * interval (-i) parameter, so let keep it comment until further
+ * discussion about the "issue".
+ */
+
+#if 0
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+  struct sensor_event_baro baro_data;
+
+  if (buflen != sizeof(baro_data))
+    {
+      return -EINVAL;
+    }
+
+  baro_measure_read(priv, &baro_data);
+
+  memcpy(buffer, &baro_data, sizeof(baro_data));
+
+  return buflen;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_register
+ *
+ * Description:
+ *   Register the MS5611 character device
+ *
+ * Input Parameters:
+ *   devno   - Instance number for driver
+ *   i2c     - An instance of the I2C interface to use to communicate with
+ *             MS5611
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int ms5611_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr)
+{
+  FAR struct ms5611_dev_s *priv;
+  FAR char *argv[2];
+  char arg1[32];
+
+  int ret;
+
+  /* Initialize the MS5611 device structure */
+
+  priv = (FAR struct ms5611_dev_s *)kmm_zalloc(sizeof(struct ms5611_dev_s));
+  if (!priv)

Review comment:
       ```suggestion
     if (priv == NULL)
   ```

##########
File path: include/nuttx/sensors/ms5611.h
##########
@@ -0,0 +1,101 @@
+/****************************************************************************
+ * include/nuttx/sensors/ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_SENSORS_MS5611
+#define __INCLUDE_NUTTX_SENSORS_MS5611
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/sensors/ioctl.h>
+
+#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_MS5611)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Configuration ************************************************************
+ * Prerequisites:
+ *
+ * CONFIG_I2C
+ *   Enables support for I2C drivers
+ * CONFIG_SENSORS_MS5611
+ *   Enables support for the MS5611 driver
+ */
+
+/* I2C Address **************************************************************/
+
+#define MS5611_ADDR0       0x77
+#define MS5611_ADDR1       0x76
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct ms5611_measure_s
+{
+  int32_t temperature;  /* in Degree   x100    */
+  int32_t pressure;     /* in mBar     x10     */
+};
+
+struct i2c_master_s;
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Name: ms5611_register
+ *
+ * Description:
+ *   Register the MS5611 character device as 'devpath'.
+ *
+ * Input Parameters:
+ *   devpath - The full path to the driver to register, e.g., "/dev/press0".
+ *   i2c     - An I2C driver instance.
+ *   addr    - The I2C address of the MS5611.
+ *   osr     - The oversampling ratio.
+ *   model   - The MS5611 model.

Review comment:
       Please correct description

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, (int32_t *) &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, (uint8_t *) &data);
+      if (ret < 0)
+        {
+          snerr("ms5611_read16 failed\n");
+          return ret;
+        }
+
+      prom[i] = (uint16_t) data[0] << 8 | (uint16_t) data[1];
+    }
+
+  /* Get the 4-bit CRC from PROM */
+
+  crc = (uint8_t)(prom[7] & 0xf);
+
+  /* Verify if the calculated CRC is equal to PROM's CRC */
+
+  if (crc != msxxxx_crc4(prom, 7, 0xff))
+    {
+      snerr("ERROR: Calculated CRC different from PROM's CRC!\n");
+      return -ENODEV;
+    }
+
+  /* Fill read calibration coefficients */
+
+  priv->calib.c1 = prom[1];
+  priv->calib.c2 = prom[2];
+  priv->calib.c3 = prom[3];
+  priv->calib.c4 = prom[4];
+  priv->calib.c5 = prom[5];
+  priv->calib.c6 = prom[6];
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_temp
+ *
+ * Description:
+ *   calculate compensate temperature
+ *
+ * Input Parameters:
+ *   temp - uncompensate value of temperature.
+ *
+ * Returned Value:
+ *   calculate result of compensate temperature.
+ *
+ ****************************************************************************/
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int32_t dt;
+
+  /* dt = d1 - c5 * 256 */
+
+  dt = temp - ((int32_t) c->c5 << 8);
+
+  /* temp = 2000 + (dt * c6) / 8388608 */
+
+  temp = 2000 + (((int64_t) (dt * c->c6)) >> 23);
+
+  /* Save dt that will be used for pressure calibration */
+
+  *deltat = dt;
+
+  return temp;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_press
+ *
+ * Description:
+ *   calculate compensate pressure
+ *
+ * Input Parameters:
+ *   press - uncompensate value of pressure.
+ *
+ * Returned Value:
+ *   calculate result of compensate pressure.
+ *
+ ****************************************************************************/
+
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int64_t off;
+  int64_t sens;
+
+  off = ((int64_t) c->c2 * 65536) + ((int64_t) (c->c4 * dt) / 128);
+  sens = ((int64_t) c->c1 * 32768) + ((int64_t) (c->c3 * dt) / 256);
+  press = (((press * sens) / 2097152) - off) / 32768;
+
+  return press;
+}
+
+/****************************************************************************
+ * Name: ms5611_set_interval
+ ****************************************************************************/
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+
+  priv->interval = *period_us;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_activate
+ ****************************************************************************/
+
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable)
+{
+  bool start_thread = false;
+  struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)lower;
+
+  if (enable)
+    {
+      if (!priv->enabled)
+        {
+          start_thread = true;
+        }
+    }
+
+  priv->enabled = enable;
+
+  if (start_thread == true)
+    {
+      /* Wake up the thread */
+
+      nxsem_post(&priv->run);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_fetch
+ ****************************************************************************/
+
+/* N.B. When fetch is enabled the sensortest doesn't respect the
+ * interval (-i) parameter, so let keep it comment until further
+ * discussion about the "issue".
+ */
+
+#if 0
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+  struct sensor_event_baro baro_data;
+
+  if (buflen != sizeof(baro_data))
+    {
+      return -EINVAL;
+    }
+
+  baro_measure_read(priv, &baro_data);
+
+  memcpy(buffer, &baro_data, sizeof(baro_data));
+
+  return buflen;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_register
+ *
+ * Description:
+ *   Register the MS5611 character device
+ *
+ * Input Parameters:
+ *   devno   - Instance number for driver
+ *   i2c     - An instance of the I2C interface to use to communicate with
+ *             MS5611
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int ms5611_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr)
+{
+  FAR struct ms5611_dev_s *priv;
+  FAR char *argv[2];
+  char arg1[32];
+
+  int ret;
+
+  /* Initialize the MS5611 device structure */
+
+  priv = (FAR struct ms5611_dev_s *)kmm_zalloc(sizeof(struct ms5611_dev_s));
+  if (!priv)
+    {
+      snerr("Failed to allocate instance\n");
+      return -ENOMEM;
+    }
+
+  priv->i2c      = i2c;
+  priv->addr     = addr;
+  priv->freq     = CONFIG_MS5611_I2C_FREQUENCY;
+  priv->interval = 1000000; /* Default interval 1s */
+
+  nxsem_init(&priv->run, 0, 0);
+  nxsem_init(&priv->exclsem, 0, 1);
+  nxsem_set_protocol(&priv->run, SEM_PRIO_NONE);
+  nxsem_set_protocol(&priv->exclsem, SEM_PRIO_NONE);
+
+  priv->sensor_lower.ops = &g_sensor_ops;
+  priv->sensor_lower.type = SENSOR_TYPE_BAROMETER;
+
+  ret = ms5611_initialize(priv);
+  if (ret < 0)
+    {
+      snerr("Failed to initialize physical device ms5611:%d\n", ret);
+      kmm_free(priv);
+      return ret;
+    }
+
+  /* Register the character driver */
+
+  ret = sensor_register(&priv->sensor_lower, devno);
+  if (ret < 0)
+    {
+      snerr("Failed to register driver: %d\n", ret);
+      kmm_free(priv);
+    }
+
+  /* Create thread for polling sensor data */
+
+  snprintf(arg1, 16, "0x%" PRIxPTR, (uintptr_t)priv);
+  argv[0] = arg1;
+  argv[1] = NULL;
+  ret = kthread_create("ms5611_thread", SCHED_PRIORITY_DEFAULT,
+                       CONFIG_MS5611_THREAD_STACKSIZE,
+                       ms5611_thread, argv);
+  if (ret > 0)
+    {
+      return OK;
+    }
+
+  sninfo("MS5611 driver loaded successfully!\n");

Review comment:
       I do not fully understand this. Should `ms5611_register` always return `OK`?




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790315223



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, (int32_t *) &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, (uint8_t *) &data);
+      if (ret < 0)
+        {
+          snerr("ms5611_read16 failed\n");
+          return ret;
+        }
+
+      prom[i] = (uint16_t) data[0] << 8 | (uint16_t) data[1];
+    }
+
+  /* Get the 4-bit CRC from PROM */
+
+  crc = (uint8_t)(prom[7] & 0xf);
+
+  /* Verify if the calculated CRC is equal to PROM's CRC */
+
+  if (crc != msxxxx_crc4(prom, 7, 0xff))
+    {
+      snerr("ERROR: Calculated CRC different from PROM's CRC!\n");
+      return -ENODEV;
+    }
+
+  /* Fill read calibration coefficients */
+
+  priv->calib.c1 = prom[1];
+  priv->calib.c2 = prom[2];
+  priv->calib.c3 = prom[3];
+  priv->calib.c4 = prom[4];
+  priv->calib.c5 = prom[5];
+  priv->calib.c6 = prom[6];
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_temp
+ *
+ * Description:
+ *   calculate compensate temperature
+ *
+ * Input Parameters:
+ *   temp - uncompensate value of temperature.
+ *
+ * Returned Value:
+ *   calculate result of compensate temperature.
+ *
+ ****************************************************************************/
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int32_t dt;
+
+  /* dt = d1 - c5 * 256 */
+
+  dt = temp - ((int32_t) c->c5 << 8);
+
+  /* temp = 2000 + (dt * c6) / 8388608 */
+
+  temp = 2000 + (((int64_t) (dt * c->c6)) >> 23);
+
+  /* Save dt that will be used for pressure calibration */
+
+  *deltat = dt;
+
+  return temp;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_press
+ *
+ * Description:
+ *   calculate compensate pressure
+ *
+ * Input Parameters:
+ *   press - uncompensate value of pressure.
+ *
+ * Returned Value:
+ *   calculate result of compensate pressure.
+ *
+ ****************************************************************************/
+
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int64_t off;
+  int64_t sens;
+
+  off = ((int64_t) c->c2 * 65536) + ((int64_t) (c->c4 * dt) / 128);
+  sens = ((int64_t) c->c1 * 32768) + ((int64_t) (c->c3 * dt) / 256);
+  press = (((press * sens) / 2097152) - off) / 32768;
+
+  return press;
+}
+
+/****************************************************************************
+ * Name: ms5611_set_interval
+ ****************************************************************************/
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+
+  priv->interval = *period_us;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_activate
+ ****************************************************************************/
+
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable)
+{
+  bool start_thread = false;
+  struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)lower;
+
+  if (enable)
+    {
+      if (!priv->enabled)
+        {
+          start_thread = true;
+        }
+    }
+
+  priv->enabled = enable;
+
+  if (start_thread == true)
+    {
+      /* Wake up the thread */
+
+      nxsem_post(&priv->run);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_fetch
+ ****************************************************************************/
+
+/* N.B. When fetch is enabled the sensortest doesn't respect the
+ * interval (-i) parameter, so let keep it comment until further
+ * discussion about the "issue".
+ */
+
+#if 0
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+  struct sensor_event_baro baro_data;
+
+  if (buflen != sizeof(baro_data))
+    {
+      return -EINVAL;
+    }
+
+  baro_measure_read(priv, &baro_data);
+
+  memcpy(buffer, &baro_data, sizeof(baro_data));
+
+  return buflen;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_register
+ *
+ * Description:
+ *   Register the MS5611 character device
+ *
+ * Input Parameters:
+ *   devno   - Instance number for driver
+ *   i2c     - An instance of the I2C interface to use to communicate with
+ *             MS5611
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int ms5611_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr)
+{
+  FAR struct ms5611_dev_s *priv;
+  FAR char *argv[2];
+  char arg1[32];
+
+  int ret;
+
+  /* Initialize the MS5611 device structure */
+
+  priv = (FAR struct ms5611_dev_s *)kmm_zalloc(sizeof(struct ms5611_dev_s));
+  if (!priv)
+    {
+      snerr("Failed to allocate instance\n");
+      return -ENOMEM;
+    }
+
+  priv->i2c      = i2c;
+  priv->addr     = addr;
+  priv->freq     = CONFIG_MS5611_I2C_FREQUENCY;
+  priv->interval = 1000000; /* Default interval 1s */
+
+  nxsem_init(&priv->run, 0, 0);
+  nxsem_init(&priv->exclsem, 0, 1);
+  nxsem_set_protocol(&priv->run, SEM_PRIO_NONE);
+  nxsem_set_protocol(&priv->exclsem, SEM_PRIO_NONE);
+
+  priv->sensor_lower.ops = &g_sensor_ops;
+  priv->sensor_lower.type = SENSOR_TYPE_BAROMETER;
+
+  ret = ms5611_initialize(priv);
+  if (ret < 0)
+    {
+      snerr("Failed to initialize physical device ms5611:%d\n", ret);
+      kmm_free(priv);
+      return ret;
+    }
+
+  /* Register the character driver */
+
+  ret = sensor_register(&priv->sensor_lower, devno);
+  if (ret < 0)
+    {
+      snerr("Failed to register driver: %d\n", ret);
+      kmm_free(priv);
+    }
+
+  /* Create thread for polling sensor data */
+
+  snprintf(arg1, 16, "0x%" PRIxPTR, (uintptr_t)priv);
+  argv[0] = arg1;
+  argv[1] = NULL;
+  ret = kthread_create("ms5611_thread", SCHED_PRIORITY_DEFAULT,
+                       CONFIG_MS5611_THREAD_STACKSIZE,
+                       ms5611_thread, argv);
+  if (ret > 0)
+    {
+      return OK;
+    }
+
+  sninfo("MS5611 driver loaded successfully!\n");

Review comment:
       Good catch, it is in fact wrong! 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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790315847



##########
File path: boards/arm/stm32/common/src/stm32_ms5611.c
##########
@@ -0,0 +1,110 @@
+/****************************************************************************
+ * boards/arm/stm32/common/src/stm32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "stm32_i2c.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");
+
+  /* Initialize I2C */
+
+  i2c = stm32_i2cbus_initialize(busno);
+
+  if (i2c)

Review comment:
       Done, thx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790314603



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))

Review comment:
       Currently the SPI is not implemented, but I already added option to support I2C or SPI




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790320705



##########
File path: include/nuttx/sensors/msxxxx_crc4.h
##########
@@ -0,0 +1,82 @@
+/****************************************************************************
+ * include/nuttx/sensors/msxxxx_crc4.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+#define __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms58xx_crc
+ *
+ * Description:
+ *   Calculate the CRC.
+ *
+ ****************************************************************************/
+
+static uint8_t msxxxx_crc4(FAR uint16_t *src,
+                           uint8_t crcndx,
+                           uint16_t crcmask)
+{
+  uint16_t cnt;
+  uint16_t n_rem;
+  uint16_t crc_read;
+  uint8_t n_bit;
+
+  n_rem = 0x00;
+  crc_read = src[crcndx];
+  src[crcndx] &= ~crcmask;
+
+  for (cnt = 0; cnt < 16; cnt++)
+    {
+      if (cnt % 2 == 1)
+        {
+          n_rem ^= (uint16_t)((src[cnt >> 1]) & 0x00ff);
+        }
+      else
+        {
+          n_rem ^= (uint16_t)(src[cnt >> 1] >> 8);
+        }
+
+      for (n_bit = 8; n_bit > 0; n_bit--)
+        {
+          if (n_rem & (0x8000))
+            {
+              n_rem = (n_rem << 1) ^ 0x3000;
+            }
+          else
+            {
+              n_rem = (n_rem << 1);

Review comment:
       ```suggestion
                 n_rem <<= 1;
   ```




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790320782



##########
File path: include/nuttx/sensors/msxxxx_crc4.h
##########
@@ -0,0 +1,82 @@
+/****************************************************************************
+ * include/nuttx/sensors/msxxxx_crc4.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+#define __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms58xx_crc
+ *
+ * Description:
+ *   Calculate the CRC.
+ *
+ ****************************************************************************/
+
+static uint8_t msxxxx_crc4(FAR uint16_t *src,
+                           uint8_t crcndx,
+                           uint16_t crcmask)
+{
+  uint16_t cnt;
+  uint16_t n_rem;
+  uint16_t crc_read;
+  uint8_t n_bit;
+
+  n_rem = 0x00;
+  crc_read = src[crcndx];
+  src[crcndx] &= ~crcmask;
+
+  for (cnt = 0; cnt < 16; cnt++)
+    {
+      if (cnt % 2 == 1)
+        {
+          n_rem ^= (uint16_t)((src[cnt >> 1]) & 0x00ff);

Review comment:
       ```suggestion
             n_rem ^= src[cnt >> 1] & 0x00ff;
   ```
   since `src`is a `uint16_t *`




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790327657



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,704 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);

Review comment:
       It is not an issue, because in this case the thread that is running will go on and the thread that was waiting for semaphore will just return because "ret" will be negative. BTW other drivers doesn't enforce this exclsem for measure_read(). I added it because I was thinking that fetch() and the thread worker should work at same time, but in fact this is the the right thing to do. I think in this case be interruptable by a signal will be a good side effect because you could use Ctrl+C to interrupt the sensor application easily




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790343193



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,705 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + (i)*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ERROR;

Review comment:
       Why `ret` is returned in `ms5611_sendcmd()`, but `ERROR` here? 

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,705 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + (i)*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)

Review comment:
       ```suggestion
   static int ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
   ```

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,705 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + (i)*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ERROR;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ERROR;

Review comment:
       Why `ret` is returned in `ms5611_sendcmd()`, but `ERROR` here? 




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790317643



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, (int32_t *) &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, (uint8_t *) &data);

Review comment:
       Done! Thnx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790317845



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, (int32_t *) &deltat);

Review comment:
       Done! Thnx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790340504



##########
File path: drivers/sensors/Kconfig
##########
@@ -561,6 +561,47 @@ config MCP9844_I2C_FREQUENCY
 	range 1 400000
 	depends on SENSORS_MCP9844
 
+config SENSORS_MS5611
+	bool "MS5611 Barometric Pressure Sensor support"
+	default n
+	select I2C

Review comment:
       ```suggestion
   	select I2C
   ```
   I think that I2C selection is done in `MS5611_I2C` case




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790320065



##########
File path: boards/xtensa/esp32/common/include/esp32_ms5611.h
##########
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/include/esp32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_MS5611_H

Review comment:
       ```suggestion
   #ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_MS5611_H
   ```

##########
File path: boards/arm/stm32/common/include/stm32_ms5611.h
##########
@@ -0,0 +1,80 @@
+/****************************************************************************
+ * boards/arm/stm32/common/include/stm32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_ARM_STM32_COMMON_INCLUDE_BOARD_MS5611_H

Review comment:
       ```suggestion
   #ifndef __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MS5611_H
   ```

##########
File path: include/nuttx/sensors/msxxxx_crc4.h
##########
@@ -0,0 +1,82 @@
+/****************************************************************************
+ * include/nuttx/sensors/msxxxx_crc4.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+#define __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms58xx_crc
+ *
+ * Description:
+ *   Calculate the CRC.
+ *
+ ****************************************************************************/
+
+static uint8_t msxxxx_crc4(FAR uint16_t *src,
+                           uint8_t crcndx,
+                           uint16_t crcmask)
+{
+  uint16_t cnt;
+  uint16_t n_rem;
+  uint16_t crc_read;
+  uint8_t n_bit;
+
+  n_rem = 0x00;
+  crc_read = src[crcndx];
+  src[crcndx] &= ~crcmask;
+
+  for (cnt = 0; cnt < 16; cnt++)
+    {
+      if (cnt % 2 == 1)
+        {
+          n_rem ^= (uint16_t)((src[cnt >> 1]) & 0x00ff);
+        }
+      else
+        {
+          n_rem ^= (uint16_t)(src[cnt >> 1] >> 8);
+        }
+
+      for (n_bit = 8; n_bit > 0; n_bit--)
+        {
+          if (n_rem & (0x8000))

Review comment:
       ```suggestion
             if ((n_rem & 0x8000) != 0)
   ```

##########
File path: boards/arm/stm32/common/src/stm32_ms5611.c
##########
@@ -0,0 +1,110 @@
+/****************************************************************************
+ * boards/arm/stm32/common/src/stm32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "stm32_i2c.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");

Review comment:
       Still here

##########
File path: boards/xtensa/esp32/common/include/esp32_ms5611.h
##########
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/include/esp32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/

Review comment:
       still here

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,704 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, data);
+      if (ret < 0)
+        {
+          snerr("ms5611_read16 failed\n");
+          return ret;
+        }
+
+      prom[i] = (uint16_t) data[0] << 8 | (uint16_t) data[1];
+    }
+
+  /* Get the 4-bit CRC from PROM */
+
+  crc = (uint8_t)(prom[7] & 0xf);
+
+  /* Verify if the calculated CRC is equal to PROM's CRC */
+
+  if (crc != msxxxx_crc4(prom, 7, 0xff))
+    {
+      snerr("ERROR: Calculated CRC different from PROM's CRC!\n");
+      return -ENODEV;
+    }
+
+  /* Fill read calibration coefficients */
+
+  priv->calib.c1 = prom[1];
+  priv->calib.c2 = prom[2];
+  priv->calib.c3 = prom[3];
+  priv->calib.c4 = prom[4];
+  priv->calib.c5 = prom[5];
+  priv->calib.c6 = prom[6];
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_temp
+ *
+ * Description:
+ *   calculate compensate temperature
+ *
+ * Input Parameters:
+ *   temp - uncompensate value of temperature.
+ *
+ * Returned Value:
+ *   calculate result of compensate temperature.
+ *
+ ****************************************************************************/
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int32_t dt;
+
+  /* dt = d1 - c5 * 256 */
+
+  dt = temp - ((int32_t) c->c5 << 8);
+
+  /* temp = 2000 + (dt * c6) / 8388608 */
+
+  temp = 2000 + (((int64_t) (dt * c->c6)) >> 23);
+
+  /* Save dt that will be used for pressure calibration */
+
+  *deltat = dt;
+
+  return temp;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_press
+ *
+ * Description:
+ *   calculate compensate pressure
+ *
+ * Input Parameters:
+ *   press - uncompensate value of pressure.
+ *
+ * Returned Value:
+ *   calculate result of compensate pressure.
+ *
+ ****************************************************************************/
+
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int64_t off;
+  int64_t sens;
+
+  off = ((int64_t) c->c2 * 65536) + ((int64_t) (c->c4 * dt) / 128);
+  sens = ((int64_t) c->c1 * 32768) + ((int64_t) (c->c3 * dt) / 256);
+  press = (((press * sens) / 2097152) - off) / 32768;
+
+  return press;
+}
+
+/****************************************************************************
+ * Name: ms5611_set_interval
+ ****************************************************************************/
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+
+  priv->interval = *period_us;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_activate
+ ****************************************************************************/
+
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable)
+{
+  bool start_thread = false;
+  struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)lower;
+
+  if (enable)
+    {
+      if (!priv->enabled)
+        {
+          start_thread = true;
+        }
+    }
+
+  priv->enabled = enable;
+
+  if (start_thread == true)
+    {
+      /* Wake up the thread */
+
+      nxsem_post(&priv->run);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_fetch
+ ****************************************************************************/
+
+/* N.B. When fetch is enabled the sensortest doesn't respect the
+ * interval (-i) parameter, so let keep it comment until further
+ * discussion about the "issue".
+ */
+
+#if 0
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+  struct sensor_event_baro baro_data;
+
+  if (buflen != sizeof(baro_data))
+    {
+      return -EINVAL;
+    }
+
+  baro_measure_read(priv, &baro_data);
+
+  memcpy(buffer, &baro_data, sizeof(baro_data));
+
+  return buflen;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_register
+ *
+ * Description:
+ *   Register the MS5611 character device
+ *
+ * Input Parameters:
+ *   i2c     - An instance of the I2C interface to use to communicate with
+ *             MS5611
+ *   devno   - Instance number for driver
+ *   addr    - The I2C address of the MS5611.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int ms5611_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr)
+{
+  FAR struct ms5611_dev_s *priv;
+  FAR char *argv[2];
+  char arg1[32];
+
+  int ret;
+
+  /* Initialize the MS5611 device structure */
+
+  priv = (FAR struct ms5611_dev_s *)kmm_zalloc(sizeof(struct ms5611_dev_s));
+  if (priv == NULL)
+    {
+      snerr("Failed to allocate instance\n");
+      return -ENOMEM;
+    }
+
+  priv->i2c      = i2c;
+  priv->addr     = addr;
+  priv->freq     = CONFIG_MS5611_I2C_FREQUENCY;
+  priv->interval = 1000000; /* Default interval 1s */
+
+  nxsem_init(&priv->run, 0, 0);
+  nxsem_init(&priv->exclsem, 0, 1);
+  nxsem_set_protocol(&priv->run, SEM_PRIO_NONE);
+  nxsem_set_protocol(&priv->exclsem, SEM_PRIO_NONE);
+
+  priv->sensor_lower.ops = &g_sensor_ops;
+  priv->sensor_lower.type = SENSOR_TYPE_BAROMETER;
+
+  ret = ms5611_initialize(priv);
+  if (ret < 0)
+    {
+      snerr("Failed to initialize physical device ms5611:%d\n", ret);
+      kmm_free(priv);
+      return ret;
+    }
+
+  /* Register the character driver */
+
+  ret = sensor_register(&priv->sensor_lower, devno);
+  if (ret < 0)
+    {
+      snerr("Failed to register driver: %d\n", ret);
+      kmm_free(priv);
+    }
+
+  /* Create thread for polling sensor data */
+
+  snprintf(arg1, 16, "0x%" PRIxPTR, (uintptr_t)priv);
+  argv[0] = arg1;
+  argv[1] = NULL;
+  ret = kthread_create("ms5611_thread", SCHED_PRIORITY_DEFAULT,
+                       CONFIG_MS5611_THREAD_STACKSIZE,
+                       ms5611_thread, argv);
+  if (ret < 0)
+    {
+      snerr("Failed to create the notification kthread!\n");
+      return ret;

Review comment:
       ```suggestion
         kmm_free(priv);
         return ret;
   ```

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,704 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, data);
+      if (ret < 0)
+        {
+          snerr("ms5611_read16 failed\n");
+          return ret;
+        }
+
+      prom[i] = (uint16_t) data[0] << 8 | (uint16_t) data[1];
+    }
+
+  /* Get the 4-bit CRC from PROM */
+
+  crc = (uint8_t)(prom[7] & 0xf);
+
+  /* Verify if the calculated CRC is equal to PROM's CRC */
+
+  if (crc != msxxxx_crc4(prom, 7, 0xff))
+    {
+      snerr("ERROR: Calculated CRC different from PROM's CRC!\n");
+      return -ENODEV;
+    }
+
+  /* Fill read calibration coefficients */
+
+  priv->calib.c1 = prom[1];
+  priv->calib.c2 = prom[2];
+  priv->calib.c3 = prom[3];
+  priv->calib.c4 = prom[4];
+  priv->calib.c5 = prom[5];
+  priv->calib.c6 = prom[6];
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_temp
+ *
+ * Description:
+ *   calculate compensate temperature
+ *
+ * Input Parameters:
+ *   temp - uncompensate value of temperature.
+ *
+ * Returned Value:
+ *   calculate result of compensate temperature.
+ *
+ ****************************************************************************/
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int32_t dt;
+
+  /* dt = d1 - c5 * 256 */
+
+  dt = temp - ((int32_t) c->c5 << 8);
+
+  /* temp = 2000 + (dt * c6) / 8388608 */
+
+  temp = 2000 + (((int64_t) (dt * c->c6)) >> 23);
+
+  /* Save dt that will be used for pressure calibration */
+
+  *deltat = dt;
+
+  return temp;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_press
+ *
+ * Description:
+ *   calculate compensate pressure
+ *
+ * Input Parameters:
+ *   press - uncompensate value of pressure.
+ *
+ * Returned Value:
+ *   calculate result of compensate pressure.
+ *
+ ****************************************************************************/
+
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int64_t off;
+  int64_t sens;
+
+  off = ((int64_t) c->c2 * 65536) + ((int64_t) (c->c4 * dt) / 128);
+  sens = ((int64_t) c->c1 * 32768) + ((int64_t) (c->c3 * dt) / 256);
+  press = (((press * sens) / 2097152) - off) / 32768;
+
+  return press;
+}
+
+/****************************************************************************
+ * Name: ms5611_set_interval
+ ****************************************************************************/
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+
+  priv->interval = *period_us;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_activate
+ ****************************************************************************/
+
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable)
+{
+  bool start_thread = false;
+  struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)lower;
+
+  if (enable)
+    {
+      if (!priv->enabled)
+        {
+          start_thread = true;
+        }
+    }
+
+  priv->enabled = enable;
+
+  if (start_thread == true)
+    {
+      /* Wake up the thread */
+
+      nxsem_post(&priv->run);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_fetch
+ ****************************************************************************/
+
+/* N.B. When fetch is enabled the sensortest doesn't respect the
+ * interval (-i) parameter, so let keep it comment until further
+ * discussion about the "issue".
+ */
+
+#if 0
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+  struct sensor_event_baro baro_data;
+
+  if (buflen != sizeof(baro_data))
+    {
+      return -EINVAL;
+    }
+
+  baro_measure_read(priv, &baro_data);
+
+  memcpy(buffer, &baro_data, sizeof(baro_data));
+
+  return buflen;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_register
+ *
+ * Description:
+ *   Register the MS5611 character device
+ *
+ * Input Parameters:
+ *   i2c     - An instance of the I2C interface to use to communicate with
+ *             MS5611
+ *   devno   - Instance number for driver
+ *   addr    - The I2C address of the MS5611.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int ms5611_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr)
+{
+  FAR struct ms5611_dev_s *priv;
+  FAR char *argv[2];
+  char arg1[32];
+
+  int ret;
+
+  /* Initialize the MS5611 device structure */
+
+  priv = (FAR struct ms5611_dev_s *)kmm_zalloc(sizeof(struct ms5611_dev_s));
+  if (priv == NULL)
+    {
+      snerr("Failed to allocate instance\n");
+      return -ENOMEM;
+    }
+
+  priv->i2c      = i2c;
+  priv->addr     = addr;
+  priv->freq     = CONFIG_MS5611_I2C_FREQUENCY;
+  priv->interval = 1000000; /* Default interval 1s */
+
+  nxsem_init(&priv->run, 0, 0);
+  nxsem_init(&priv->exclsem, 0, 1);
+  nxsem_set_protocol(&priv->run, SEM_PRIO_NONE);
+  nxsem_set_protocol(&priv->exclsem, SEM_PRIO_NONE);

Review comment:
       Do we really need to disable priority inheritance on `exclsem`?




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790321083



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,704 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */

Review comment:
       ```suggestion
   #define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + (i)*2) /* 0xA0 - 0xAE */
   ```

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,704 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;

Review comment:
       ```suggestion
         return ERROR;
   ```

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,704 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;

Review comment:
       ```suggestion
         return ERROR;
   ```

##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,704 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);

Review comment:
       can be interrupted by a signal. should we use non-interruptable version?




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] xiaoxiang781216 merged pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 merged pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317


   


-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790330707



##########
File path: boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
##########
@@ -72,6 +72,10 @@
 #include "stm32_bmp180.h"
 #endif
 
+#ifdef CONFIG_SENSORS_MS5611
+#include "stm32_ms5611.h"

Review comment:
       This is something that is not well-defined or enforced by the coding style. Case it became an enforcement it need to be detected by the nxstyle to avoid people forget to follow it.




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790316862



##########
File path: boards/xtensa/esp32/common/src/esp32_ms5611.c
##########
@@ -0,0 +1,112 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_ms5611.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  char devpath[12];
+  int ret;
+
+  sninfo("Initializing BMP180!\n");
+
+  /* Initialize BMP180 */
+
+  i2c = esp32_i2cbus_initialize(busno);
+
+  if (i2c)
+    {
+      /* Then try to register the barometer sensor in I2C0 */
+
+      snprintf(devpath, 12, "/dev/press%d", devno);

Review comment:
       Done! Thnx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790316505



##########
File path: boards/xtensa/esp32/common/include/esp32_ms5611.h
##########
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/include/esp32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/

Review comment:
       Done, thx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790322842



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,704 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, data);
+      if (ret < 0)
+        {
+          snerr("ms5611_read16 failed\n");
+          return ret;
+        }
+
+      prom[i] = (uint16_t) data[0] << 8 | (uint16_t) data[1];
+    }
+
+  /* Get the 4-bit CRC from PROM */
+
+  crc = (uint8_t)(prom[7] & 0xf);
+
+  /* Verify if the calculated CRC is equal to PROM's CRC */
+
+  if (crc != msxxxx_crc4(prom, 7, 0xff))
+    {
+      snerr("ERROR: Calculated CRC different from PROM's CRC!\n");
+      return -ENODEV;
+    }
+
+  /* Fill read calibration coefficients */
+
+  priv->calib.c1 = prom[1];
+  priv->calib.c2 = prom[2];
+  priv->calib.c3 = prom[3];
+  priv->calib.c4 = prom[4];
+  priv->calib.c5 = prom[5];
+  priv->calib.c6 = prom[6];
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_temp
+ *
+ * Description:
+ *   calculate compensate temperature
+ *
+ * Input Parameters:
+ *   temp - uncompensate value of temperature.
+ *
+ * Returned Value:
+ *   calculate result of compensate temperature.
+ *
+ ****************************************************************************/
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int32_t dt;
+
+  /* dt = d1 - c5 * 256 */
+
+  dt = temp - ((int32_t) c->c5 << 8);
+
+  /* temp = 2000 + (dt * c6) / 8388608 */
+
+  temp = 2000 + (((int64_t) (dt * c->c6)) >> 23);
+
+  /* Save dt that will be used for pressure calibration */
+
+  *deltat = dt;
+
+  return temp;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_press
+ *
+ * Description:
+ *   calculate compensate pressure
+ *
+ * Input Parameters:
+ *   press - uncompensate value of pressure.
+ *
+ * Returned Value:
+ *   calculate result of compensate pressure.
+ *
+ ****************************************************************************/
+
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int64_t off;
+  int64_t sens;
+
+  off = ((int64_t) c->c2 * 65536) + ((int64_t) (c->c4 * dt) / 128);
+  sens = ((int64_t) c->c1 * 32768) + ((int64_t) (c->c3 * dt) / 256);
+  press = (((press * sens) / 2097152) - off) / 32768;
+
+  return press;
+}
+
+/****************************************************************************
+ * Name: ms5611_set_interval
+ ****************************************************************************/
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+
+  priv->interval = *period_us;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_activate
+ ****************************************************************************/
+
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable)
+{
+  bool start_thread = false;
+  struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)lower;
+
+  if (enable)
+    {
+      if (!priv->enabled)
+        {
+          start_thread = true;
+        }
+    }
+
+  priv->enabled = enable;
+
+  if (start_thread == true)
+    {
+      /* Wake up the thread */
+
+      nxsem_post(&priv->run);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_fetch
+ ****************************************************************************/
+
+/* N.B. When fetch is enabled the sensortest doesn't respect the
+ * interval (-i) parameter, so let keep it comment until further
+ * discussion about the "issue".
+ */
+
+#if 0
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+  struct sensor_event_baro baro_data;
+
+  if (buflen != sizeof(baro_data))
+    {
+      return -EINVAL;
+    }
+
+  baro_measure_read(priv, &baro_data);
+
+  memcpy(buffer, &baro_data, sizeof(baro_data));
+
+  return buflen;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_register
+ *
+ * Description:
+ *   Register the MS5611 character device
+ *
+ * Input Parameters:
+ *   i2c     - An instance of the I2C interface to use to communicate with
+ *             MS5611
+ *   devno   - Instance number for driver
+ *   addr    - The I2C address of the MS5611.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int ms5611_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr)
+{
+  FAR struct ms5611_dev_s *priv;
+  FAR char *argv[2];
+  char arg1[32];
+
+  int ret;
+
+  /* Initialize the MS5611 device structure */
+
+  priv = (FAR struct ms5611_dev_s *)kmm_zalloc(sizeof(struct ms5611_dev_s));
+  if (priv == NULL)
+    {
+      snerr("Failed to allocate instance\n");
+      return -ENOMEM;
+    }
+
+  priv->i2c      = i2c;
+  priv->addr     = addr;
+  priv->freq     = CONFIG_MS5611_I2C_FREQUENCY;
+  priv->interval = 1000000; /* Default interval 1s */
+
+  nxsem_init(&priv->run, 0, 0);
+  nxsem_init(&priv->exclsem, 0, 1);
+  nxsem_set_protocol(&priv->run, SEM_PRIO_NONE);
+  nxsem_set_protocol(&priv->exclsem, SEM_PRIO_NONE);

Review comment:
       I don't know for sure, I used as base other sensor driver example. Let see if it works fine without using SEM_PRIO_NONE




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790339954



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,703 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + (i)*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ERROR;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ERROR;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, data);
+      if (ret < 0)
+        {
+          snerr("ms5611_read16 failed\n");
+          return ret;
+        }
+
+      prom[i] = (uint16_t) data[0] << 8 | (uint16_t) data[1];
+    }
+
+  /* Get the 4-bit CRC from PROM */
+
+  crc = (uint8_t)(prom[7] & 0xf);
+
+  /* Verify if the calculated CRC is equal to PROM's CRC */
+
+  if (crc != msxxxx_crc4(prom, 7, 0xff))
+    {
+      snerr("ERROR: Calculated CRC different from PROM's CRC!\n");
+      return -ENODEV;
+    }
+
+  /* Fill read calibration coefficients */
+
+  priv->calib.c1 = prom[1];
+  priv->calib.c2 = prom[2];
+  priv->calib.c3 = prom[3];
+  priv->calib.c4 = prom[4];
+  priv->calib.c5 = prom[5];
+  priv->calib.c6 = prom[6];
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_temp
+ *
+ * Description:
+ *   calculate compensate temperature
+ *
+ * Input Parameters:
+ *   temp - uncompensate value of temperature.
+ *
+ * Returned Value:
+ *   calculate result of compensate temperature.
+ *
+ ****************************************************************************/
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int32_t dt;
+
+  /* dt = d1 - c5 * 256 */
+
+  dt = temp - ((int32_t) c->c5 << 8);
+
+  /* temp = 2000 + (dt * c6) / 8388608 */
+
+  temp = 2000 + (((int64_t) (dt * c->c6)) >> 23);
+
+  /* Save dt that will be used for pressure calibration */
+
+  *deltat = dt;
+
+  return temp;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_press
+ *
+ * Description:
+ *   calculate compensate pressure
+ *
+ * Input Parameters:
+ *   press - uncompensate value of pressure.
+ *
+ * Returned Value:
+ *   calculate result of compensate pressure.
+ *
+ ****************************************************************************/
+
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int64_t off;
+  int64_t sens;
+
+  off = ((int64_t) c->c2 * 65536) + ((int64_t) (c->c4 * dt) / 128);
+  sens = ((int64_t) c->c1 * 32768) + ((int64_t) (c->c3 * dt) / 256);
+  press = (((press * sens) / 2097152) - off) / 32768;
+
+  return press;
+}
+
+/****************************************************************************
+ * Name: ms5611_set_interval
+ ****************************************************************************/
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+
+  priv->interval = *period_us;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_activate
+ ****************************************************************************/
+
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable)
+{
+  bool start_thread = false;
+  struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)lower;
+
+  if (enable)
+    {
+      if (!priv->enabled)
+        {
+          start_thread = true;
+        }
+    }
+
+  priv->enabled = enable;
+
+  if (start_thread == true)

Review comment:
       ```suggestion
     if (start_thread)
   ```




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790315700



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read temperature!\n");
+      return;
+    }
+
+  temp = (uint32_t) buffer[0] << 16 |
+         (uint32_t) buffer[1] << 8 |
+         (uint32_t) buffer[2];
+
+  /* Release the semaphore */
+
+  nxsem_post(&priv->exclsem);
+
+  /* Compensate the temp/press with calibration data */
+
+  temp = ms5611_compensate_temp(priv, temp, (int32_t *) &deltat);
+  press = ms5611_compensate_press(priv, press, deltat);
+
+  baro->timestamp = ms5611_curtime();
+  baro->pressure = press / 100.0f;
+  baro->temperature = temp / 100.0f;
+}
+
+/****************************************************************************
+ * Name: ms5611_thread
+ *
+ * Description: Thread for performing interval measurement cycle and data
+ *              read.
+ *
+ * Parameter:
+ *   argc - Number opf arguments
+ *   argv - Pointer to argument list
+ ****************************************************************************/
+
+static int ms5611_thread(int argc, char **argv)
+{
+  FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
+        ((uintptr_t)strtoul(argv[1], NULL, 0));
+
+  struct sensor_event_baro baro_data;
+
+  while (true)
+    {
+      int ret;
+
+      if (!priv->enabled)
+        {
+          /* Waiting to be woken up */
+
+          ret = nxsem_wait(&priv->run);
+          if (ret < 0)
+            {
+              continue;
+            }
+        }
+
+       baro_measure_read(priv, &baro_data);
+
+       priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
+                                     sizeof(struct sensor_event_baro));
+
+      /* Sleeping thread before fetching the next sensor data */
+
+      nxsig_usleep(priv->interval);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_initialize
+ *
+ * Description:
+ *   Initialize MS5611 device
+ *
+ ****************************************************************************/
+
+static int ms5611_initialize(FAR struct ms5611_dev_s *priv)
+{
+  uint16_t prom[8];
+  uint8_t data[2];
+  uint8_t crc;
+  int i;
+  int ret;
+
+  /* Get calibration data. */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_RESET);
+  if (ret < 0)
+    {
+      snerr("ms5611 reset failed\n");
+      return ret;
+    }
+
+  /* We have to wait before the prom is ready is be read */
+
+  up_udelay(10000);
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = ms5611_sendcmd(priv, MS5611_CMD_ADC_PROM_READ(i));
+      if (ret < 0)
+        {
+          snerr("ms5611_sendcmd failed\n");
+          return ret;
+        }
+
+      ret = ms5611_read16(priv, (uint8_t *) &data);
+      if (ret < 0)
+        {
+          snerr("ms5611_read16 failed\n");
+          return ret;
+        }
+
+      prom[i] = (uint16_t) data[0] << 8 | (uint16_t) data[1];
+    }
+
+  /* Get the 4-bit CRC from PROM */
+
+  crc = (uint8_t)(prom[7] & 0xf);
+
+  /* Verify if the calculated CRC is equal to PROM's CRC */
+
+  if (crc != msxxxx_crc4(prom, 7, 0xff))
+    {
+      snerr("ERROR: Calculated CRC different from PROM's CRC!\n");
+      return -ENODEV;
+    }
+
+  /* Fill read calibration coefficients */
+
+  priv->calib.c1 = prom[1];
+  priv->calib.c2 = prom[2];
+  priv->calib.c3 = prom[3];
+  priv->calib.c4 = prom[4];
+  priv->calib.c5 = prom[5];
+  priv->calib.c6 = prom[6];
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_temp
+ *
+ * Description:
+ *   calculate compensate temperature
+ *
+ * Input Parameters:
+ *   temp - uncompensate value of temperature.
+ *
+ * Returned Value:
+ *   calculate result of compensate temperature.
+ *
+ ****************************************************************************/
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int32_t dt;
+
+  /* dt = d1 - c5 * 256 */
+
+  dt = temp - ((int32_t) c->c5 << 8);
+
+  /* temp = 2000 + (dt * c6) / 8388608 */
+
+  temp = 2000 + (((int64_t) (dt * c->c6)) >> 23);
+
+  /* Save dt that will be used for pressure calibration */
+
+  *deltat = dt;
+
+  return temp;
+}
+
+/****************************************************************************
+ * Name: ms5611_compensate_press
+ *
+ * Description:
+ *   calculate compensate pressure
+ *
+ * Input Parameters:
+ *   press - uncompensate value of pressure.
+ *
+ * Returned Value:
+ *   calculate result of compensate pressure.
+ *
+ ****************************************************************************/
+
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt)
+{
+  struct ms5611_calib_s *c = &priv->calib;
+  int64_t off;
+  int64_t sens;
+
+  off = ((int64_t) c->c2 * 65536) + ((int64_t) (c->c4 * dt) / 128);
+  sens = ((int64_t) c->c1 * 32768) + ((int64_t) (c->c3 * dt) / 256);
+  press = (((press * sens) / 2097152) - off) / 32768;
+
+  return press;
+}
+
+/****************************************************************************
+ * Name: ms5611_set_interval
+ ****************************************************************************/
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+
+  priv->interval = *period_us;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_activate
+ ****************************************************************************/
+
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable)
+{
+  bool start_thread = false;
+  struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)lower;
+
+  if (enable)
+    {
+      if (!priv->enabled)
+        {
+          start_thread = true;
+        }
+    }
+
+  priv->enabled = enable;
+
+  if (start_thread == true)
+    {
+      /* Wake up the thread */
+
+      nxsem_post(&priv->run);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_fetch
+ ****************************************************************************/
+
+/* N.B. When fetch is enabled the sensortest doesn't respect the
+ * interval (-i) parameter, so let keep it comment until further
+ * discussion about the "issue".
+ */
+
+#if 0
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen)
+{
+  FAR struct ms5611_dev_s *priv = container_of(lower,
+                                               FAR struct ms5611_dev_s,
+                                               sensor_lower);
+  struct sensor_event_baro baro_data;
+
+  if (buflen != sizeof(baro_data))
+    {
+      return -EINVAL;
+    }
+
+  baro_measure_read(priv, &baro_data);
+
+  memcpy(buffer, &baro_data, sizeof(baro_data));
+
+  return buflen;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_register
+ *
+ * Description:
+ *   Register the MS5611 character device
+ *
+ * Input Parameters:
+ *   devno   - Instance number for driver
+ *   i2c     - An instance of the I2C interface to use to communicate with
+ *             MS5611
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int ms5611_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr)
+{
+  FAR struct ms5611_dev_s *priv;
+  FAR char *argv[2];
+  char arg1[32];
+
+  int ret;
+
+  /* Initialize the MS5611 device structure */
+
+  priv = (FAR struct ms5611_dev_s *)kmm_zalloc(sizeof(struct ms5611_dev_s));
+  if (!priv)

Review comment:
       Done, thx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790317521



##########
File path: drivers/sensors/ms5611.c
##########
@@ -0,0 +1,702 @@
+/****************************************************************************
+ * drivers/sensors/ms5611.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/nuttx.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/signal.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/sensor.h>
+#include <nuttx/sensors/ms5611.h>
+#include <nuttx/sensors/msxxxx_crc4.h>
+
+#if defined(CONFIG_SENSORS_MS5611) && \
+    (defined(CONFIG_I2C) || defined(CONFIG_SPI))
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MS5611_CMD_RESET              0x1e
+#define MS5611_CMD_START_ADC_READ     0x00
+#define MS5611_CMD_CONV_D1_OSR_256    0x40 /* D1 = uncompensated pressure */
+#define MS5611_CMD_CONV_D1_OSR_512    0x42
+#define MS5611_CMD_CONV_D1_OSR_1024   0x44
+#define MS5611_CMD_CONV_D1_OSR_2048   0x46
+#define MS5611_CMD_CONV_D1_OSR_4096   0x48
+#define MS5611_CMD_CONV_D2_OSR_256    0x50 /* D2 = uncompensated pressure */
+#define MS5611_CMD_CONV_D2_OSR_512    0x52
+#define MS5611_CMD_CONV_D2_OSR_1024   0x54
+#define MS5611_CMD_CONV_D2_OSR_2048   0x56
+#define MS5611_CMD_CONV_D2_OSR_4096   0x58
+#define MS5611_CMD_ADC_READ           0x00
+#define MS5611_CMD_ADC_PROM_READ(i)   (0xa0 + i*2) /* 0xA0 - 0xAE */
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ms5611_calib_s
+{
+  uint16_t reversed;
+  uint16_t c1;
+  uint16_t c2;
+  uint16_t c3;
+  uint16_t c4;
+  uint16_t c5;
+  uint16_t c6;
+  uint16_t crc;
+};
+
+struct ms5611_dev_s
+{
+  FAR struct sensor_lowerhalf_s sensor_lower;
+
+#ifdef CONFIG_MS5611_I2C
+  FAR struct i2c_master_s  *i2c;       /* I2C interface */
+  uint8_t                   addr;      /* I2C address */
+#endif
+
+#ifdef CONFIG_MS5611_SPI
+  FAR struct spi_dev_s     *spi;       /* SPI interface */
+#endif
+
+  uint32_t                  freq;      /* Bus Frequency I2C/SPI */
+  struct ms5611_calib_s     calib;     /* Calib. params from ROM */
+  unsigned int              interval;  /* Polling interval */
+  bool                      enabled;   /* Enable/Disable MS5611 */
+  sem_t                     run;       /* Locks measure cycle */
+  sem_t                     exclsem;   /* Manages exclusive to device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv,
+                              uint8_t cmd);
+static int ms5611_read16(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+static int ms5611_read24(FAR struct ms5611_dev_s *priv,
+                         FAR uint8_t *regval);
+
+static int32_t ms5611_compensate_temp(FAR struct ms5611_dev_s *priv,
+                                      uint32_t temp, int32_t *deltat);
+static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
+                                        uint32_t press, uint32_t dt);
+
+static unsigned long ms5611_curtime(void);
+
+/* Sensor methods */
+
+static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
+                               FAR unsigned int *period_us);
+static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
+                           bool enable);
+
+#if 0 /* Please read below */
+static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
+                        FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_sensor_ops =
+{
+  .activate      = ms5611_activate,
+  .fetch         = NULL, /* ms5611_fetch */
+  .set_interval  = ms5611_set_interval,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ms5611_curtime
+ *
+ * Description: Helper to get current timestamp.
+ *
+ * Return:
+ *   Timestamp in microseconds
+ ****************************************************************************/
+
+static unsigned long ms5611_curtime(void)
+{
+  struct timespec ts;
+#ifdef CONFIG_CLOCK_MONOTONIC
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+  clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+
+  return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
+}
+
+/****************************************************************************
+ * Name: ms5611_sendcmd
+ *
+ * Description:
+ *   Send a command (8-bit) to MS5611
+ *
+ ****************************************************************************/
+
+static uint8_t ms5611_sendcmd(FAR struct ms5611_dev_s *priv, uint8_t cmd)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = &cmd;
+  msg.length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read16
+ *
+ * Description:
+ *   Read 16-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read16(FAR struct ms5611_dev_s *priv, FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: ms5611_read24
+ *
+ * Description:
+ *   Read 24-bit from a MS5611 register
+ *
+ ****************************************************************************/
+
+static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
+{
+  struct i2c_msg_s msg;
+  int ret;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = I2C_M_READ;
+  msg.buffer    = regval;
+  msg.length    = 3;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+      return -1;
+    }
+
+  return OK;
+}
+
+static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
+                                     FAR struct sensor_event_baro *baro)
+{
+  uint32_t press;
+  uint32_t temp;
+  int32_t deltat;
+  int ret;
+  uint8_t buffer[3];
+
+  /* Enforce exclusive access */
+
+  ret = nxsem_wait(&priv->exclsem);
+  if (ret < 0)
+    {
+      return;
+    }
+
+  /* Send command to start a D1 (pressure) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D1_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D1_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);
+  if (ret < 0)
+    {
+      snerr("Fail to read pressure!\n");
+      return;
+    }
+
+  press = (uint32_t) buffer[0] << 16 |
+          (uint32_t) buffer[1] << 8 |
+          (uint32_t) buffer[2];
+
+  /* Send command to start a D2 (temperature) conversion */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_CONV_D2_OSR_4096);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_CONV_D2_OSR_4096!\n");
+      return;
+    }
+
+  /* Wait data acquisition */
+
+  up_udelay(10000);
+
+  /* Send command to start a read sequence */
+
+  ret = ms5611_sendcmd(priv, MS5611_CMD_START_ADC_READ);
+  if (ret < 0)
+    {
+      snerr("Fail to send cmd MS5611_CMD_START_ADC_READ!\n");
+      return;
+    }
+
+  /* Wait data get ready */
+
+  up_udelay(4000);
+
+  ret = ms5611_read24(priv, (uint8_t *) buffer);

Review comment:
       Done! Thnx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790316194



##########
File path: boards/arm/stm32/common/include/stm32_ms5611.h
##########
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * boards/arm/stm32/common/include/stm32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/

Review comment:
       Done thx!




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790316393



##########
File path: boards/xtensa/esp32/common/include/esp32_ms5611.h
##########
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/include/esp32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H

Review comment:
       Done, thx




-- 
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@nuttx.apache.org

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



[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5317: sensors: Add support to MS5611 barometer

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on a change in pull request #5317:
URL: https://github.com/apache/incubator-nuttx/pull/5317#discussion_r790340322



##########
File path: boards/arm/stm32/common/include/stm32_ms5611.h
##########
@@ -0,0 +1,80 @@
+/****************************************************************************
+ * boards/arm/stm32/common/include/stm32_ms5611.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MS5611_H
+#define __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MS5611_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_ms5611_initialize
+ *
+ * Description:
+ *   Initialize and register the BMP180 Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_ms5611_initialize(int devno, int busno);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BOARDS_ARN_STM32_COMMON_INCLUDE_BOARD_MS5611_H */

Review comment:
       ```suggestion
   #endif /* __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MS5611_H */
   ```




-- 
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@nuttx.apache.org

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