xiaoxiang781216 commented on code in PR #19674:
URL: https://github.com/apache/nuttx/pull/19674#discussion_r3712702064


##########
drivers/input/st7123.c:
##########
@@ -0,0 +1,988 @@
+/****************************************************************************
+ * drivers/input/st7123.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 <assert.h>
+#include <debug.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <nuttx/bits.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/input/touchscreen.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/sched.h>
+#include <nuttx/wqueue.h>
+
+#include <nuttx/input/st7123.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define ST7123_TOUCH_FW_VERSION         0x00
+#define ST7123_TOUCH_STATUS             0x01
+#define ST7123_TOUCH_DEV_CTRL           0x02
+
+/* XY Coordinate resolution, Maximum Number of Touches Register */
+
+#define ST7123_TOUCH_MAX_X_COORD_H      0x05
+#define ST7123_TOUCH_MAX_X_COORD_L      0x06
+#define ST7123_TOUCH_MAX_Y_COORD_H      0x07
+#define ST7123_TOUCH_MAX_Y_COORD_L      0x08
+#define ST7123_TOUCH_MAX_TOUCHES        0x09
+#define ST7123_TOUCH_SENSING_COUNTER_H  0x0a
+#define ST7123_TOUCH_SENSING_COUNTER_L  0x0b
+#define ST7123_TOUCH_FW_REV_3           0x0c
+#define ST7123_TOUCH_FW_REV_2           0x0d
+#define ST7123_TOUCH_FW_REV_1           0x0e
+#define ST7123_TOUCH_FW_REV_0           0x0f
+#define ST7123_TOUCH_ADV_TOUCH_INFO     0x10
+#define ST7123_TOUCH_GESTURE_INFO       0x12
+#define ST7123_TOUCH_KEYS               0x13
+#define ST7123_TOUCH_MISC_INFO          0xf0
+#define ST7123_TOUCH_MISC_CTRL          0xf1
+
+/* Touch area registers */
+
+#define ST7123_TOUCH_AREA_SIZE         7    /* There are 7 registers for each 
touch area */
+#define ST7123_TOUCH_DATA_START        0x14
+
+/* Maximum number of touch areas this driver is able to report.  The value
+ * read back from ST7123_TOUCH_MAX_TOUCHES is clamped to this limit so that a
+ * bogus register value can never overrun the frame or sample buffers.
+ */
+
+#define ST7123_MAX_TOUCH_AREAS         10
+
+/* Registers 0x10 through 0x13 precede the per-area data at 0x14 and are read
+ * as part of the same touch frame.
+ */
+
+#define ST7123_FRAME_HEADER_SIZE \
+  (ST7123_TOUCH_DATA_START - ST7123_TOUCH_ADV_TOUCH_INFO)
+
+/* Advanced Touch Info Masks */
+
+#define ST7123_ADV_TOUCH_WITH_PROX     BIT(2)
+#define ST7123_ADV_TOUCH_WITH_COORD    BIT(3)
+#define ST7123_ADV_TOUCH_RST_CHIP      BIT(7)
+#define ST7123_ADV_TOUCH_PROX_STATUS   0x70
+
+/* Devices status obtained from STATUS register bits [3:0]
+ * and error codes obtained from STATUS register bits [7:4].
+ */
+
+#define ST7123_STATUS_MASK 0x0f
+#define ST7123_ERROR_MASK 0xf0
+#define ST7123_ERROR_SHIFT 4
+
+/* Miscellaneous information register */
+
+#define ST7123_MISC_SUPPORT_COORD_CHKSUM     BIT(4)
+#define ST7123_MISC_SUPPORT_PROXIMITY        BIT(5)
+#define ST7123_MISC_SUPPORT_SMART_WAKEUP_EN  BIT(7)
+
+/* Miscellaneous control register */
+
+#define ST7123_MISC_CTRL_SMART_WAKEUP_EN_BIT BIT(7)
+
+/* Device control register valid bits. All other bits must be set to 0. */
+
+#define ST7123_DEVICE_CTRL_RESET_BIT         BIT(0)
+#define ST7123_DEVICE_CTRL_POWER_DOWN_BIT    BIT(1)
+#define ST7123_DEVICE_CTRL_PROXIMITY_EN_BIT  BIT(5)
+
+/* Gesture codes */
+
+#define ST7123_GESTURE_NONE             0x00
+#define ST7123_GESTURE_DET_FAILED       0xff
+#define ST7123_GESTURE_DOUBLE_TAP       0xb0
+#define ST7123_GESTURE_SINGLE_TAP       0xb1
+#define ST7123_GESTURE_LONG_PRESS       0xb2
+#define ST7123_GESTURE_SWIPE_RIGHT      0xc0
+#define ST7123_GESTURE_SWIPE_LEFT       0xc1
+#define ST7123_GESTURE_SWIPE_DOWN       0xc2
+#define ST7123_GESTURE_SWIPE_UP         0xc3
+#define ST7123_GESTURE_ARROW_TOP        0xc4
+#define ST7123_GESTURE_ARROW_RIGHT      0xc5
+#define ST7123_GESTURE_ARROW_BOTTOM     0xc6
+#define ST7123_GESTURE_ARROW_LEFT       0xc7
+#define ST7123_GESTURE_TWO_FINGER_DOWN  0xc8
+
+#define ST7123_I2C_ADDRLEN        7
+
+/* Driver registration */
+
+#define DEV_FORMAT     "/dev/input%d"
+#define DEV_NAMELEN    16
+
+/* Startup */
+
+#define WAIT_TIMEOUT_STEP_US 100
+#define WAIT_TIMEOUT_MAX_US (WAIT_TIMEOUT_STEP_US * 1000)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* Raw area data with proper bit field for software parsing */
+
+begin_packed_struct struct st7123_area_report_t
+{
+  uint8_t x_coord_h: 6;
+  uint8_t reserved_s: 1;
+  uint8_t valid: 1;
+  uint8_t x_coord_l;
+  uint8_t y_coord_h;
+  uint8_t y_coord_l;
+  uint8_t area_size;
+  uint8_t intensity;
+  uint8_t reserved_e;
+} end_packed_struct;
+
+/* Firmware revision from registers 0x0c through 0x0f */
+
+begin_packed_struct struct st7123_fw_revision_reg_t
+{
+  uint8_t fw_rev_3;
+  uint8_t fw_rev_2;
+  uint8_t fw_rev_1;
+  uint8_t fw_rev_0;
+} end_packed_struct;
+
+/* Advanced touch information from register 0x10 */
+
+begin_packed_struct struct st7123_adv_touch_info_reg_t
+{
+  uint8_t reserved: 2;
+  uint8_t with_prox: 1;
+  uint8_t with_coord: 1;
+  uint8_t prox_status: 3;
+  uint8_t rst_chip: 1;
+} end_packed_struct;
+
+/* Miscellaneous information from register 0xf0 */
+
+begin_packed_struct struct st7123_misc_info_reg_t
+{
+  uint8_t reserved_0: 3;
+  uint8_t support_coord_chksum: 1;
+  uint8_t support_proximity: 1;
+  uint8_t reserved_1: 1;
+  uint8_t support_smart_wakeup_en: 1;
+} end_packed_struct;
+
+/* Device capabilities from registers 0x05 through 0x09 */
+
+begin_packed_struct struct st7123_device_caps_reg_t
+{
+  uint8_t max_x_coord_h: 5;
+  uint8_t reserved_x: 2;
+  uint8_t max_x_coord_l;
+  uint8_t max_y_coord_h: 5;
+  uint8_t reserved_y: 2;
+  uint8_t max_y_coord_l;
+  uint8_t max_touches;
+} end_packed_struct;
+
+/* A complete touch report, registers 0x10 upwards. The controller keeps the
+ * interrupt line asserted until the whole frame has been consumed, so it
+ * must be fetched in a single transaction.
+ */
+
+begin_packed_struct struct st7123_frame_t
+{
+  struct st7123_adv_touch_info_reg_t adv_touch_info;
+  uint8_t reserved;
+  uint8_t gesture;
+  uint8_t keys;
+  struct st7123_area_report_t areas[ST7123_MAX_TOUCH_AREAS];
+} end_packed_struct;
+
+/* Status codes */
+
+typedef enum st7123_status_e
+{
+  ST7123_STATUS_NORMAL = 0,
+  ST7123_STATUS_INIT = 1,
+  ST7123_STATUS_ERROR = 2,
+  ST7123_STATUS_SMART_WAKEUP = 3,
+  ST7123_STATUS_IDLE = 4,
+  ST7123_STATUS_POWER_DOWN = 5,
+  ST7123_STATUS_PRE_SMART_WAKEUP = 6,
+  ST7123_STATUS_PROXIMITY = 7,
+  ST7123_STATUS_TRGT_PROXIMITY = 8,
+} st7123_status_t;
+
+/* Error codes */
+
+typedef enum st7123_error_e
+{
+  ST7123_ERROR_NO_ERROR = 0,
+  ST7123_ERROR_INVALID_ADDR = 1,
+  ST7123_ERROR_INVALID_VALUE = 2,
+  ST7123_ERROR_INVALID_PLATFORM = 3,
+  ST7123_ERROR_DEV_NOT_FOUND = 4,
+  ST7123_ERROR_DEV_STACK_OVERFLOW = 5,
+  ST7123_ERROR_DEV_INVALID_FW_TABLE = 6,
+} st7123_error_t;
+
+/* Main device structure */
+
+struct st7123_dev_t
+{
+  FAR struct touch_lowerhalf_s lower;
+  struct i2c_master_s *i2c;
+  struct i2c_config_s i2c_config;
+  struct work_s work;
+  mutex_t lock;
+
+  struct st7123_frame_t frame;
+  struct st7123_adv_touch_info_reg_t adv_touch_info;
+  struct st7123_device_caps_reg_t device_caps;
+  struct st7123_fw_revision_reg_t fw_revision;
+  struct st7123_misc_info_reg_t misc_info;
+  uint8_t fw_version;
+
+  /* Contacts reported as down by the previous frame, one bit per area.  Used
+   * to turn the per-frame valid bits into DOWN/MOVE/UP transitions.
+   */
+
+  uint16_t downmap;
+
+  /* Last known position of each contact, so that the release event can be
+   * reported at the position where the contact was lost.
+   */
+
+  int16_t lastx[ST7123_MAX_TOUCH_AREAS];
+  int16_t lasty[ST7123_MAX_TOUCH_AREAS];
+
+  /* Scratch buffer for touch_event() samples. */
+
+  uint8_t sample_buf[SIZEOF_TOUCH_SAMPLE_S(ST7123_MAX_TOUCH_AREAS)];
+};
+
+/****************************************************************************
+ * Static Function Prototypes
+ ****************************************************************************/
+
+static int st7123_open(struct touch_lowerhalf_s *lower);
+static int st7123_close(struct touch_lowerhalf_s *lower);
+static int st7123_control(struct touch_lowerhalf_s *lower, int cmd,
+                          unsigned long arg);
+static int st7123_write(struct touch_lowerhalf_s *lower,
+                        FAR const char *buffer, size_t buflen);
+static int st7123_read_reg(struct st7123_dev_t *dev, uint8_t reg,
+                           uint8_t *value);
+static int st7123_write_reg(struct st7123_dev_t *dev, uint8_t reg,
+                            uint8_t value);
+static int st7123_read_sequential(struct st7123_dev_t *dev, uint8_t reg,
+                                  uint8_t *value, size_t count);
+static int st7123_read_status(struct st7123_dev_t *dev,
+                              FAR st7123_status_t *status);
+static int st7123_read_error(struct st7123_dev_t *dev,
+                             FAR st7123_error_t *error);
+static int st7123_probe_device(struct st7123_dev_t *dev);
+static void st7123_data_worker(void *arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Registered device instance used by st7123_interrupt_callback(). */
+
+static FAR struct st7123_dev_t *g_st7123_dev;

Review Comment:
   how to support the multiple instances? let's remove this global variable



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

Reply via email to