robertalexa2000 commented on code in PR #7466:
URL: https://github.com/apache/incubator-nuttx/pull/7466#discussion_r1008914550


##########
drivers/sensors/ltr308.c:
##########
@@ -0,0 +1,1000 @@
+/****************************************************************************
+ * drivers/sensors/ltr308.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.
+ *
+ ****************************************************************************/
+
+/* Character driver for the LTR-308ALS-01 Lite-On ambient light sensor */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/sensors/ltr308.h>
+
+#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_LTR308)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define LTR308_ADDR           0x53
+#define LTR308_FREQ           CONFIG_LTR308_I2C_FREQUENCY
+#define DEVID                 0xB1
+
+#define LTR308_CTRL           0x00
+#define LTR308_MEAS_RATE      0x04
+#define LTR308_ALS_GAIN       0x05
+#define LTR308_PART_ID        0x06
+#define LTR308_STATUS         0x07
+#define LTR308_DATA_0         0x0D
+#define LTR308_DATA_1         0x0E
+#define LTR308_DATA_2         0x0F
+#define LTR308_INTERRUPT      0x19
+#define LTR308_INTR_PERS      0x1A
+#define LTR308_THRES_UP_0     0x21
+#define LTR308_THRES_UP_1     0x22
+#define LTR308_THRES_UP_2     0x23
+#define LTR308_THRES_LOW_0    0x24
+#define LTR308_THRES_LOW_1    0x25
+#define LTR308_THRES_LOW_2    0x26
+
+#define BYTE_TO_BITS          8
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+struct ltr308_dev_s
+{
+  FAR struct i2c_master_s *i2c;  /* I2C interface */
+  uint8_t addr;                  /* I2C address */
+  int freq;                      /* I2C bus frequency */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  bool unlinked;                 /* True, driver has been unlinked */
+  int16_t crefs;                 /* Number of open references */
+#endif
+  sem_t devsem;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int ltr308_open(FAR struct file *filep);
+static int ltr308_close(FAR struct file *filep);
+#endif
+static ssize_t ltr308_read(FAR struct file *filep, FAR char *buffer,
+                           size_t len);
+static ssize_t ltr308_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int ltr308_unlink(FAR struct inode *inode);
+#endif
+
+/****************************************************************************
+ * Private Data
+****************************************************************************/
+
+static const struct file_operations g_ltr308fops =
+{
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  .open = ltr308_open,
+  .close = ltr308_close,
+#endif
+  .read = ltr308_read,
+  .ioctl = ltr308_ioctl,
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  .unlink = ltr308_unlink,
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ltr308_set_reg8
+ *
+ * Description:
+ *   Write to an 8-bit LTR308 register
+ *
+ ****************************************************************************/
+
+static int ltr308_set_reg8(FAR struct ltr308_dev_s *priv, uint8_t regaddr,
+                           uint8_t regval)
+{
+  struct i2c_msg_s msg;
+  uint8_t txbuffer[2];
+  int ret;
+
+  txbuffer[0]   = regaddr;
+  txbuffer[1]   = regval;
+
+  msg.frequency = priv->freq;
+  msg.addr      = priv->addr;
+  msg.flags     = 0;
+  msg.buffer    = txbuffer;
+  msg.length    = 2;
+
+  ret = I2C_TRANSFER(priv->i2c, &msg, 1);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed\n");
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ltr308_get_reg8
+ *
+ * Description:
+ *   Read from an 8-bit LTR308 register
+ *
+ ****************************************************************************/
+
+static int ltr308_get_reg8(FAR struct ltr308_dev_s *priv, uint8_t regaddr,
+                           FAR uint8_t *regval)
+{
+  struct i2c_msg_s msg[2];
+  int ret;
+
+  msg[0].frequency = priv->freq;
+  msg[0].addr      = priv->addr;
+  msg[0].flags     = 0;
+  msg[0].buffer    = &regaddr;
+  msg[0].length    = 1;
+
+  msg[1].frequency = priv->freq;
+  msg[1].addr      = priv->addr;
+  msg[1].flags     = I2C_M_READ;
+  msg[1].buffer    = regval;
+  msg[1].length    = 1;
+
+  ret = I2C_TRANSFER(priv->i2c, msg, 2);
+  if (ret < 0)
+    {
+      snerr("I2C_TRANSFER failed: %d\n", ret);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ltr308_set_reg24
+ *
+ * Description:
+ *   Write to 3 8-bit LTR308 registers
+ *
+ ****************************************************************************/
+
+static int ltr308_set_reg24(FAR struct ltr308_dev_s *priv, uint8_t regaddr,
+                            uint32_t val)
+{
+  int i, ret;
+
+  for (i = 0; i < 3; i++, regaddr++)
+    {
+      ret = ltr308_set_reg8(priv, regaddr, val);
+      if (ret < 0)
+        {
+          return ret;
+        }
+
+      val >>= BYTE_TO_BITS;
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ltr308_get_reg24
+ *
+ * Description:
+ *   Read from 3 8-bit LTR308 registers
+ *
+ ****************************************************************************/
+
+static int ltr308_get_reg24(FAR struct ltr308_dev_s *priv, uint8_t regaddr,
+                            FAR uint32_t *val)
+{
+  int i, ret;
+
+  *val = 0;
+  for (i = 0; i < 3; i++, regaddr++)
+    {
+      ret = ltr308_get_reg8(priv, regaddr, ((uint8_t *)val) + i);
+      if (ret < 0)
+        {
+          return ret;
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ltr308_set_ctrl
+ *
+ * Description:
+ *   Power up, shut down or reset LTR308
+ *
+ ****************************************************************************/
+
+static inline int ltr308_set_ctrl(FAR struct ltr308_dev_s *priv,

Review Comment:
   If your concern is that the compiler might ignore the inline qualifier, we 
could replace it with the always_inline attribute. Would that be a good 
solution? Replacing it with a macro looks rather ugly to me..



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

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

Reply via email to