TimJTi commented on code in PR #8789:
URL: https://github.com/apache/nuttx/pull/8789#discussion_r1145012589


##########
drivers/sensors/apds9922.c:
##########
@@ -0,0 +1,2619 @@
+/****************************************************************************
+ * drivers/sensors/apds9922.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 APDS9922 Proximity and Ambient Light Sensor     */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <poll.h>
+#include <debug.h>
+#include <stdlib.h>
+
+#include <nuttx/compiler.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/wqueue.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/signal.h>
+
+#include <nuttx/sensors/apds9922.h>
+#include <sys/ioctl.h>
+
+#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_APDS9922)
+
+/****************************************************************************
+ * Pre-process Definitions
+ ****************************************************************************/
+
+#ifndef CONFIG_APDS9922_I2C_FREQUENCY
+#  define CONFIG_APDS9922_I2C_FREQUENCY 400000
+#endif
+
+#ifndef CONFIG_APDS9922_ALS_NPOLLWAITERS
+#  define CONFIG_APDS9922_ALS_NPOLLWAITERS 2
+#endif
+
+#ifndef CONFIG_APDS9922_PS_NPOLLWAITERS
+#  define CONFIG_APDS9922_PS_NPOLLWAITERS 2
+#endif
+
+/* Helper macros */
+
+#ifdef CONFIG_ENDIAN_BIG
+#  define APDS9922_PACK_TO_UINT32(a) \
+   (((a)[0] >> 24) | ((a)[1] >> 16) | ((a)[2] >> 8) | ((a)[3]))
+#  define APDS9922_PACK_TO_UINT16(a) \
+   (((a)[0] >> 0) | ((a)[1]))
+#  define APDS9922_UNPACK_FROM_UINT32(w, b) \
+  do \
+    { \
+      (b)[0] = ((w) >> 24) & 0xff; \
+      (b)[1] = ((w) >> 16) & 0xff; \
+      (b)[2] = ((w) >> 8) & 0xff; \
+      (b)[3] = (w) & 0xff; \
+    } \
+  while (0)
+#  define APDS9922_UNPACK_FROM_UINT16(w, b) \
+  do \
+    { \
+      (b)[0] = ((w) >> 8) & 0xff; \
+      (b)[1] = (w) & 0xff; \
+    } \
+  while (0)
+#else
+#  define APDS9922_PACK_TO_UINT32(a) \
+   (((a)[3] << 24) | ((a)[2] << 16) | ((a)[1] << 8) | ((a)[0]))
+#  define APDS9922_PACK_TO_UINT16(a) \
+   (((a)[1] << 8) | ((a)[0]))
+#  define APDS9922_UNPACK_FROM_UINT32(w, b) \
+  do \
+    { \
+      (b)[3] = ((w) >> 24) & 0xff; \
+      (b)[2] = ((w) >> 16) & 0xff; \
+      (b)[1] = ((w) >> 8) & 0xff; \
+      (b)[0] = (w) & 0xff; \
+    } \
+  while (0)
+#  define APDS9922_UNPACK_FROM_UINT16(w, b) \
+  do \
+    { \
+      (b)[1] = ((w) >> 8) & 0xff; \
+      (b)[0] = (w) & 0xff; \
+    } \
+  while (0)
+#endif
+
+/* Register mappings */
+
+#define APDS9922_MAIN_CTRL      (0x00) /* SW reset, ALS Enable, PS enable  */
+#define APDS9922_PS_LED         (0x01) /* PS LED setup                     */
+#define APDS9922_PS_PULSES      (0x02) /* PS pulses setup                  */
+#define APDS9922_PS_MEAS_RATE   (0x03) /* PS Measurement rate              */
+#define APDS9922_ALS_MEAS_RATE  (0x04) /* ALS Measurement rate             */
+#define APDS9922_ALS_GAIN       (0x05) /* ALS gain                         */
+#define APDS9922_ID             (0x06) /* Part and Revision ID             */
+#define APDS9922_MAIN_STATUS    (0x07) /* Status register                  */
+#define APDS9922_PS_DATA0       (0x08) /* LSB of measured PS data          */
+#define APDS9922_ALS_DATA0      (0x0d) /* LSB of measured ALS data         */
+#define APDS9922_INT_CFG        (0x19) /* Interrupt configuration          */
+#define APDS9922_INT_PERSIST    (0x1a) /* Interrupt persistance            */
+#define APDS9922_PS_THRESHU     (0x1b) /* PS threshold, upper limit        */
+#define APDS9922_PS_THRESHL     (0x1d) /* PS threshold, lower limit        */
+#define APDS9922_CANCEL_LVLL    (0x1f) /* Intelligent Cancellation level   */
+#define APDS9922_CANCEL_LVLU    (0x20) /* Intelligent Cancellation level   */
+#define APDS9922_ALS_THRESHU    (0x21) /* ALS threshold, upper limit       */
+#define APDS9922_ALS_THRESHL    (0x24) /* ALS threshold, lower limit       */
+#define APDS9922_ALS_THRESH_VAR (0x27) /* ALS threshold variation          */
+
+/* APDS9922_MAIN_CTRL Register 0x01 */
+
+#define PS_ACTIVE_SHIFT         (0)
+#define PS_ACTIVE               (1 << PS_ACTIVE_SHIFT)
+#define ALS_ACTIVE_SHIFT        (1)
+#define ALS_ACTIVE              (1 << ALS_ACTIVE_SHIFT)
+#define SW_RESET_SHIFT          (4)
+#define APDS9922_SW_RESET       (1 << SW_RESET_SHIFT)
+
+/* APDS922_PS_LED register 0x02 */
+
+#define PS_LED_FREQ_SHIFT       (4)
+#define PS_LED_FREQ_MASK        (7 << PS_LED_FREQ_SHIFT)
+#define PS_SET_LED_FREQ(f)      ((f) << PS_LED_FREQ_SHIFT)
+#define PS_LED_PEAKING_ON       (1 << 3)
+#define PS_LED_CURRENT_SHIFT    (0)
+#define PS_LED_CURRENT_MASK     (7 << PS_LED_CURRENT_SHIFT)
+#define PS_SET_LED_CURRENT(i)   ((i) << PS_LED_CURRENT_SHIFT)
+
+/* APDS922_PS_PULSES register  0x03 */
+
+#define PS_LED_PULSES_MASK      (0x0fff)
+#define PS_SET_LED_PULSES(p)    ((p) & PS_LED_PULSES_MASK)
+
+/* APDS922_PS_MEAS_RATE 0x03 */
+
+#define PS_RESOLUTION_SHIFT     (3)
+#define PS_RESOLUTION_MASK      (3 << PS_RESOLUTION_SHIFT)
+#define PS_SET_RESOLUTION(r)    (((r) << PS_RESOLUTION_SHIFT) | 0x20)
+#define PS_MEASURERATE_SHIFT    (0)
+#define PS_MEASURERATE_MASK     (7 << PS_MEASURERATE_SHIFT)
+#define PS_SET_MEASURERATE(r)   ((r) << PS_MEASURERATE_SHIFT)
+
+/* APDS922_ALS_MEAS_RATE 0x04 */
+
+#define ALS_RESOLUTION_SHIFT    (4)
+#define ALS_RESOLUTION_MASK     (7 << ALS_RESOLUTION_SHIFT)
+#define ALS_SET_RESOLUTION(r)   ((r) << ALS_RESOLUTION_SHIFT)
+#define ALS_MEASURERATE_SHIFT   (0)
+#define ALS_MEASURERATE_MASK    (7 << ALS_MEASURERATE_SHIFT)
+#define ALS_SET_MEASURERATE(r)  ((r) << ALS_MEASURERATE_SHIFT)
+
+/* APDS922_ALS_GAIN 0x05 */
+
+#define ALS_GAIN_SHIFT          (0)
+#define ALS_GAIN_MASK           (7 << ALS_GAIN_SHIFT)
+#define ALS_SET_GAIN(g)         ((g) << ALS_GAIN_SHIFT)
+
+/* APDS_ALS_MAIN_STATUS 0x07 */
+
+#define ALS_INT_STATUS          (16)
+#define ALS_NEW_DATA            (8)
+#define PS_LOGIC_STATUS         (4)
+#define PS_INT_STATUS           (2)
+#define PS_NEW_DATA             (1)
+
+/* APDS9922_PS_DATA0 0x08 */
+
+#define PS_DATA_OVERFLOW_SHIFT  (3)
+#define PS_DATA_OVERFLOW        (1 << PS_DATA_OVERFLOW_SHIFT)
+
+/* APDS9922_INT_CFG 0x19 */
+
+#define PS_INT_EN_SHIFT         (0)
+#define PS_INT_EN               (1 << PS_INT_EN_SHIFT)
+#define PS_INT_MASK             (1 << PS_INT_EN_SHIFT)
+#define PS_LOGIC_MODE_SHIFT     (1)
+#define PS_LOGIC_MODE_NORMAL    (0 << PS_LOGIC_MODE_SHIFT)
+#define PS_LOGIC_MODE_LOGIC     (1 << PS_LOGIC_MODE_SHIFT)
+
+#define ALS_INT_EN_SHIFT        (2)
+#define ALS_INT_EN              (1 << ALS_INT_EN_SHIFT)
+#define ALS_INT_MASK            (5 << ALS_INT_EN_SHIFT)
+#define ALS_INT_VAR_SHIFT       (3)
+#define ALS_INT_VAR_MODE        (1 << ALS_INT_VAR_SHIFT)
+#define ALS_INT_THRESH_MODE     (0 << ALS_INT_VAR_SHIFT)
+#define ALS_INT_SRC_SHIFT       (4)
+#define ALS_INT_SRC_MASK        (3 << ALS_INT_SRC_SHIFT)
+#define ALS_INT_SET_SRC(s)      ((s) << ALS_INT_SRC_SHIFT)
+
+/* APDS922_INT_PERSIST 0x1a */
+
+#define ALS_PERSISTANCE_SHIFT   (4)
+#define ALS_PERSISTANCE_MASK    (15 << ALS_PERSISTANCE_SHIFT)
+#define ALS_SET_PERSISTANCE(p)  ((p) << ALS_PERSISTANCE_SHIFT)
+#define ALS_PERSISTANCE_MAX     (255)
+#define PS_PERSISTANCE_SHIFT    (0)
+#define PS_PERSISTANCE_MASK     (15 << PS_PERSISTANCE_SHIFT)
+#define PS_SET_PERSISTANCE(p)   ((p) << PS_PERSISTANCE_SHIFT)
+#define PS_PERSISTANCE_MAX      (255)
+
+/* APDS922_ALS_THRESH_VAR 0x27  */
+
+#define ALS_THRESH_VAR_SHIFT    (0)
+#define ALS_THRESH_VAR_MASK     (7 << ALS_THRESH_VAR_SHIFT)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+static const uint32_t apds9922_ps_res_table[] =

Review Comment:
   On reflection, your first suggestion makes sense. Implementing it now :)



-- 
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