acassis commented on code in PR #12192:
URL: https://github.com/apache/nuttx/pull/12192#discussion_r1573375841
##########
drivers/sensors/Kconfig:
##########
@@ -899,6 +899,39 @@ config SENSORS_ADXL372
---help---
Enable driver support for the Analog Devices ADXL372 Sensor.
+if SENSORS_ADXL372
+
+config SENSORS_ADXL372_UORB
+ bool "ADXL372 UORB interfvace"
Review Comment:
```suggestion
bool "ADXL372 UORB interface"
##########
drivers/sensors/adxl372_uorb.c:
##########
@@ -0,0 +1,609 @@
+/****************************************************************************
+ * drivers/sensors/adxl372_uorb.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 <debug.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <sys/param.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/signal.h>
+#include <nuttx/sensors/adxl372.h>
+
+#include <nuttx/sensors/sensor.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONSTANTS_ONE_G 9.8f
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct adxl372_sensor_s
+{
+ struct sensor_lowerhalf_s lower;
+ FAR struct spi_dev_s *spi;
+ float scale;
+ int devno;
+#ifdef CONFIG_SENSORS_ADXL372_POLL
+ bool enabled;
+ unsigned long interval;
+ sem_t run;
+#endif
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t adxl372_getreg8(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr);
+static void adxl372_putreg8(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr, uint8_t regval);
+static void adxl372_getregs(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr, FAR uint8_t *regval, int len);
+static int16_t adxl372_data(FAR uint8_t *data);
+static int adxl372_checkid(FAR struct adxl372_sensor_s *priv);
+static void adxl372_start(FAR struct adxl372_sensor_s *priv);
+static void adxl372_stop(FAR struct adxl372_sensor_s *priv);
+
+/* Sensor ops functions */
+
+static int adxl372_activate(FAR struct sensor_lowerhalf_s *lower,
+ FAR struct file *filep,
+ bool enable);
+static int adxl372_set_interval(FAR struct sensor_lowerhalf_s *lower,
+ FAR struct file *filep,
+ FAR unsigned long *period_us);
+#ifndef CONFIG_SENSORS_ADXL372_POLL
+static int adxl372_fetch(FAR struct sensor_lowerhalf_s *lower,
+ FAR struct file *filep,
+ FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_adxl372_accel_ops =
+{
+ NULL, /* open */
+ NULL, /* close */
+ adxl372_activate,
+ adxl372_set_interval,
+ NULL, /* batch */
+#ifdef CONFIG_SENSORS_ADXL372_POLL
+ NULL, /* fetch */
+#else
+ adxl372_fetch,
+#endif
+ NULL, /* selftest */
+ NULL, /* set_calibvalue */
+ NULL, /* calibrate */
+ NULL /* control */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: adxl372_getreg8
+ *
+ * Description:
+ * Read from an 8-bit ADXL372 register
+ *
+ ****************************************************************************/
+
+static uint8_t adxl372_getreg8(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr)
+{
+ uint8_t regval = 0;
+
+ SPI_LOCK(priv->spi, true);
+
+ SPI_SETFREQUENCY(priv->spi, ADXL372_SPI_FREQUENCY);
+ SPI_SETMODE(priv->spi, ADXL372_SPI_MODE);
+
+ /* Select the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), true);
+
+ /* Send register to read and get the next byte */
+
+ SPI_SEND(priv->spi, (regaddr << 1) | ADXL372_READ);
+ SPI_RECVBLOCK(priv->spi, ®val, 1);
+
+ /* Deselect the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), false);
+
+ /* Unlock bus */
+
+ SPI_LOCK(priv->spi, false);
+
+ return regval;
+}
+
+/****************************************************************************
+ * Name: adxl372_putreg8
+ *
+ * Description:
+ * Write a value to an 8-bit ADXL372 register
+ *
+ ****************************************************************************/
+
+static void adxl372_putreg8(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr, uint8_t regval)
+{
+ SPI_LOCK(priv->spi, true);
+
+ SPI_SETFREQUENCY(priv->spi, ADXL372_SPI_FREQUENCY);
+ SPI_SETMODE(priv->spi, ADXL372_SPI_MODE);
+
+ /* Select the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), true);
+
+ /* Send register address and set the value */
+
+ SPI_SEND(priv->spi, (regaddr << 1) | ADXL372_WRITE);
+ SPI_SEND(priv->spi, regval);
+
+ /* Deselect the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), false);
+
+ /* Unlock bus */
+
+ SPI_LOCK(priv->spi, false);
+}
+
+/****************************************************************************
+ * Name: adxl372_getregs
+ *
+ * Description:
+ * Read bytes from specified regaddr
+ *
+ ****************************************************************************/
+
+static void adxl372_getregs(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr, FAR uint8_t *regval, int len)
+{
+ /* If SPI bus is shared then lock and configure it */
+
+ SPI_LOCK(priv->spi, true);
+
+ SPI_SETFREQUENCY(priv->spi, ADXL372_SPI_FREQUENCY);
+ SPI_SETMODE(priv->spi, ADXL372_SPI_MODE);
+
+ /* Select the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), true);
+
+ /* Send register to read and get the next 2 bytes */
+
+ SPI_SEND(priv->spi, (regaddr << 1) | ADXL372_READ);
+ SPI_RECVBLOCK(priv->spi, regval, len);
+
+ /* Deselect the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), false);
+
+ /* Unlock bus */
+
+ SPI_LOCK(priv->spi, false);
+}
+
+/****************************************************************************
+ * Name: adxl372_data
+ ****************************************************************************/
+
+static int16_t adxl372_data(FAR uint8_t *data)
+{
+ return ((int16_t)(((data[0] << 8) | (data[1] & 0xf0)))) >> 4;
+}
+
+/****************************************************************************
+ * Name: adxl372_checkid
+ *
+ * Description:
+ * Read and verify the ADXL372 chip ID
+ *
+ ****************************************************************************/
+
+static int adxl372_checkid(FAR struct adxl372_sensor_s *priv)
+{
+ uint8_t id = 0;
+
+ id = adxl372_getreg8(priv, ADXL372_DEVID_AD);
+ if (id != ADXL372_DEVID_AD_VALUE)
+ {
+ snerr("Wrong AD! %02x\n", id);
+ return -ENODEV;
+ }
+
+ id = adxl372_getreg8(priv, ADXL372_DEVID_MST);
+ if (id != ADXL372_DEVID_MST_VALUE)
+ {
+ snerr("Wrong MST! %02x\n", id);
+ return -ENODEV;
+ }
+
+ id = adxl372_getreg8(priv, ADXL372_PARTID);
+ if (id != ADXL372_PARTID_VALUE)
+ {
+ snerr("Wrong PARTID! %02x\n", id);
+ return -ENODEV;
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: adxl372_start
+ ****************************************************************************/
+
+static void adxl372_start(FAR struct adxl372_sensor_s *priv)
+{
+ adxl372_putreg8(priv, ADXL372_POWER_CTL,
+ ADXL372_POWER_HPF_DISABLE |
+ ADXL372_POWER_MODE_MEASURE);
+}
+
+/****************************************************************************
+ * Name: adxl372_stop
+ ****************************************************************************/
+
+static void adxl372_stop(FAR struct adxl372_sensor_s *priv)
+{
+ adxl372_putreg8(priv, ADXL372_POWER_CTL, 0);
+}
+
+/****************************************************************************
+ * Name: adxl372_reset
+ ****************************************************************************/
+
+static void adxl372_reset(FAR struct adxl372_sensor_s *priv)
+{
+ int wdcnt = 10;
+
+ /* Set stanby mode */
+
+ adxl372_putreg8(priv, ADXL372_POWER_CTL, 0);
+
+ /* Wait for boot to finish (15 ms error timeout) */
+
+ up_mdelay(5);
+ while (wdcnt > 0 && (0 != adxl372_getreg8(priv, ADXL372_RESET)))
+ {
+ up_mdelay(1);
+ }
+
+ /* Reset ADXL372 Accelerometer. Write only. Begin a boot. */
+
+ adxl372_putreg8(priv, ADXL372_RESET_VALUE, ADXL372_RESET);
+
+ /* Wait for boot to finish (15 ms error timeout) */
+
+ up_mdelay(5);
+ wdcnt = 10;
+ while (wdcnt > 0 && (0 != adxl372_getreg8(priv, ADXL372_RESET)))
+ {
+ up_mdelay(1);
Review Comment:
Ditto
##########
drivers/sensors/adxl372_uorb.c:
##########
@@ -0,0 +1,609 @@
+/****************************************************************************
+ * drivers/sensors/adxl372_uorb.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 <debug.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <sys/param.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/kthread.h>
+#include <nuttx/signal.h>
+#include <nuttx/sensors/adxl372.h>
+
+#include <nuttx/sensors/sensor.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONSTANTS_ONE_G 9.8f
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct adxl372_sensor_s
+{
+ struct sensor_lowerhalf_s lower;
+ FAR struct spi_dev_s *spi;
+ float scale;
+ int devno;
+#ifdef CONFIG_SENSORS_ADXL372_POLL
+ bool enabled;
+ unsigned long interval;
+ sem_t run;
+#endif
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static uint8_t adxl372_getreg8(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr);
+static void adxl372_putreg8(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr, uint8_t regval);
+static void adxl372_getregs(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr, FAR uint8_t *regval, int len);
+static int16_t adxl372_data(FAR uint8_t *data);
+static int adxl372_checkid(FAR struct adxl372_sensor_s *priv);
+static void adxl372_start(FAR struct adxl372_sensor_s *priv);
+static void adxl372_stop(FAR struct adxl372_sensor_s *priv);
+
+/* Sensor ops functions */
+
+static int adxl372_activate(FAR struct sensor_lowerhalf_s *lower,
+ FAR struct file *filep,
+ bool enable);
+static int adxl372_set_interval(FAR struct sensor_lowerhalf_s *lower,
+ FAR struct file *filep,
+ FAR unsigned long *period_us);
+#ifndef CONFIG_SENSORS_ADXL372_POLL
+static int adxl372_fetch(FAR struct sensor_lowerhalf_s *lower,
+ FAR struct file *filep,
+ FAR char *buffer, size_t buflen);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct sensor_ops_s g_adxl372_accel_ops =
+{
+ NULL, /* open */
+ NULL, /* close */
+ adxl372_activate,
+ adxl372_set_interval,
+ NULL, /* batch */
+#ifdef CONFIG_SENSORS_ADXL372_POLL
+ NULL, /* fetch */
+#else
+ adxl372_fetch,
+#endif
+ NULL, /* selftest */
+ NULL, /* set_calibvalue */
+ NULL, /* calibrate */
+ NULL /* control */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: adxl372_getreg8
+ *
+ * Description:
+ * Read from an 8-bit ADXL372 register
+ *
+ ****************************************************************************/
+
+static uint8_t adxl372_getreg8(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr)
+{
+ uint8_t regval = 0;
+
+ SPI_LOCK(priv->spi, true);
+
+ SPI_SETFREQUENCY(priv->spi, ADXL372_SPI_FREQUENCY);
+ SPI_SETMODE(priv->spi, ADXL372_SPI_MODE);
+
+ /* Select the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), true);
+
+ /* Send register to read and get the next byte */
+
+ SPI_SEND(priv->spi, (regaddr << 1) | ADXL372_READ);
+ SPI_RECVBLOCK(priv->spi, ®val, 1);
+
+ /* Deselect the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), false);
+
+ /* Unlock bus */
+
+ SPI_LOCK(priv->spi, false);
+
+ return regval;
+}
+
+/****************************************************************************
+ * Name: adxl372_putreg8
+ *
+ * Description:
+ * Write a value to an 8-bit ADXL372 register
+ *
+ ****************************************************************************/
+
+static void adxl372_putreg8(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr, uint8_t regval)
+{
+ SPI_LOCK(priv->spi, true);
+
+ SPI_SETFREQUENCY(priv->spi, ADXL372_SPI_FREQUENCY);
+ SPI_SETMODE(priv->spi, ADXL372_SPI_MODE);
+
+ /* Select the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), true);
+
+ /* Send register address and set the value */
+
+ SPI_SEND(priv->spi, (regaddr << 1) | ADXL372_WRITE);
+ SPI_SEND(priv->spi, regval);
+
+ /* Deselect the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), false);
+
+ /* Unlock bus */
+
+ SPI_LOCK(priv->spi, false);
+}
+
+/****************************************************************************
+ * Name: adxl372_getregs
+ *
+ * Description:
+ * Read bytes from specified regaddr
+ *
+ ****************************************************************************/
+
+static void adxl372_getregs(FAR struct adxl372_sensor_s *priv,
+ uint8_t regaddr, FAR uint8_t *regval, int len)
+{
+ /* If SPI bus is shared then lock and configure it */
+
+ SPI_LOCK(priv->spi, true);
+
+ SPI_SETFREQUENCY(priv->spi, ADXL372_SPI_FREQUENCY);
+ SPI_SETMODE(priv->spi, ADXL372_SPI_MODE);
+
+ /* Select the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), true);
+
+ /* Send register to read and get the next 2 bytes */
+
+ SPI_SEND(priv->spi, (regaddr << 1) | ADXL372_READ);
+ SPI_RECVBLOCK(priv->spi, regval, len);
+
+ /* Deselect the ADXL372 */
+
+ SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(priv->devno), false);
+
+ /* Unlock bus */
+
+ SPI_LOCK(priv->spi, false);
+}
+
+/****************************************************************************
+ * Name: adxl372_data
+ ****************************************************************************/
+
+static int16_t adxl372_data(FAR uint8_t *data)
+{
+ return ((int16_t)(((data[0] << 8) | (data[1] & 0xf0)))) >> 4;
+}
+
+/****************************************************************************
+ * Name: adxl372_checkid
+ *
+ * Description:
+ * Read and verify the ADXL372 chip ID
+ *
+ ****************************************************************************/
+
+static int adxl372_checkid(FAR struct adxl372_sensor_s *priv)
+{
+ uint8_t id = 0;
+
+ id = adxl372_getreg8(priv, ADXL372_DEVID_AD);
+ if (id != ADXL372_DEVID_AD_VALUE)
+ {
+ snerr("Wrong AD! %02x\n", id);
+ return -ENODEV;
+ }
+
+ id = adxl372_getreg8(priv, ADXL372_DEVID_MST);
+ if (id != ADXL372_DEVID_MST_VALUE)
+ {
+ snerr("Wrong MST! %02x\n", id);
+ return -ENODEV;
+ }
+
+ id = adxl372_getreg8(priv, ADXL372_PARTID);
+ if (id != ADXL372_PARTID_VALUE)
+ {
+ snerr("Wrong PARTID! %02x\n", id);
+ return -ENODEV;
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: adxl372_start
+ ****************************************************************************/
+
+static void adxl372_start(FAR struct adxl372_sensor_s *priv)
+{
+ adxl372_putreg8(priv, ADXL372_POWER_CTL,
+ ADXL372_POWER_HPF_DISABLE |
+ ADXL372_POWER_MODE_MEASURE);
+}
+
+/****************************************************************************
+ * Name: adxl372_stop
+ ****************************************************************************/
+
+static void adxl372_stop(FAR struct adxl372_sensor_s *priv)
+{
+ adxl372_putreg8(priv, ADXL372_POWER_CTL, 0);
+}
+
+/****************************************************************************
+ * Name: adxl372_reset
+ ****************************************************************************/
+
+static void adxl372_reset(FAR struct adxl372_sensor_s *priv)
+{
+ int wdcnt = 10;
+
+ /* Set stanby mode */
+
+ adxl372_putreg8(priv, ADXL372_POWER_CTL, 0);
+
+ /* Wait for boot to finish (15 ms error timeout) */
+
+ up_mdelay(5);
+ while (wdcnt > 0 && (0 != adxl372_getreg8(priv, ADXL372_RESET)))
+ {
+ up_mdelay(1);
Review Comment:
You are not decreasing the protection counter `wdcnt` to avoid stuck here
forever
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]