vrmay23 commented on code in PR #18301:
URL: https://github.com/apache/nuttx/pull/18301#discussion_r2755881953


##########
drivers/lcd/st7796.c:
##########
@@ -0,0 +1,936 @@
+/****************************************************************************
+ * drivers/lcd/st7796.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 <sys/types.h>
+#include <sys/ioctl.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/spi/spi.h>
+#include <nuttx/video/fb.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/clock.h>
+#include <nuttx/signal.h>
+#include <nuttx/lcd/st7796.h>
+
+#ifdef CONFIG_LCD_ST7796
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Force SPI MODE 0 (CPOL=0, CPHA=0) - Standard for ST7796 */
+
+#define ST7796_SPIMODE SPIDEV_MODE0
+
+/* Bytes per pixel based on BPP */
+
+#define ST7796_BYTESPP(bpp)     (((bpp) == 18) ? 3 : 2)
+
+/* Color format based on BPP */
+
+#define ST7796_COLORFMT(bpp)    (((bpp) == 18) ? FB_FMT_RGB24 : \
+                                 FB_FMT_RGB16_565)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct st7796_dev_s
+{
+  struct fb_vtable_s vtable;
+  FAR struct spi_dev_s *spi;
+  FAR uint8_t *fbmem;
+  FAR uint16_t *swap_buf;             /* Persistent buffer for byte-swapping */
+  struct st7796_config_s config;      /* Board-provided configuration */
+  uint16_t rotation;                  /* Current rotation in degrees */
+  bool power;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int st7796_getvideoinfo(FAR struct fb_vtable_s *vtable,
+                               FAR struct fb_videoinfo_s *vinfo);
+static int st7796_getplaneinfo(FAR struct fb_vtable_s *vtable, int planeno,
+                               FAR struct fb_planeinfo_s *pinfo);
+static int st7796_updatearea(FAR struct fb_vtable_s *vtable,
+                             FAR const struct fb_area_s *area);
+static int st7796_ioctl(FAR struct fb_vtable_s *vtable, int cmd,
+                        unsigned long arg);
+static void st7796_select(FAR struct st7796_dev_s *priv);
+static void st7796_deselect(FAR struct st7796_dev_s *priv);
+static void st7796_sendcmd(FAR struct st7796_dev_s *priv, uint8_t cmd);
+static void st7796_senddata(FAR struct st7796_dev_s *priv,
+                            FAR const uint8_t *data, size_t len);
+static void st7796_send_sequence(FAR struct st7796_dev_s *priv,
+                                 FAR const struct st7796_cmd_s *seq,
+                                 size_t count);
+static void st7796_setarea(FAR struct st7796_dev_s *priv,
+                           uint16_t x0, uint16_t y0,
+                           uint16_t x1, uint16_t y1);
+static void st7796_apply_rotation(FAR struct st7796_dev_s *priv);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct st7796_dev_s g_st7796dev;
+
+/* Initialization sequence data arrays
+ *
+ * ST7796S Register Configuration Reference:
+ *
+ * CSCON (Command Set Control):
+ *   0xc3, 0x96  - Enable extended command access (unlock)
+ *   0x3c, 0x69  - Disable extended command access (lock)
+ *
+ * PIXFMT (Pixel Format):
+ *   0x55 - RGB565 16bpp (0101 0101b: 16-bit RGB + 16-bit MCU)
+ *
+ * INVCTR (Display Inversion):
+ *   0x01 - Column inversion mode
+ *
+ * DFC (Display Function Control):
+ *   0x80 - Source output scan direction
+ *   0x02 - Gate output scan direction
+ *   0x3b - Number of lines (59 * 8 = 472 lines)
+ *
+ * CMD 0xE8 (Undocumented):
+ *   Internal timing adjustment, from manufacturer reference
+ *
+ * PWCTRL2/3 (Power Control):
+ *   0x06 - VGH/VGL voltage setting
+ *   0xa7 - VCOM voltage adjustment
+ *
+ * VCOM:
+ *   0x18 - VCOM voltage for contrast tuning
+ *
+ * GAMMAPOS/GAMMANEG:
+ *   14-byte gamma correction curves for color linearity
+ *
+ * MADCTL values provided by board configuration
+ */
+
+static const uint8_t g_cscon1_data[] =
+{
+  0xc3
+};
+
+static const uint8_t g_cscon2_data[] =
+{
+  0x96
+};
+
+static const uint8_t g_pixfmt_data[] =
+{
+  0x55
+};
+
+static const uint8_t g_invctr_data[] =
+{
+  0x01
+};
+
+static const uint8_t g_dfc_data[] =
+{
+  0x80, 0x02, 0x3b
+};
+
+static const uint8_t g_cmd_e8_data[] =
+{
+  0x40, 0x8a, 0x00, 0x00, 0x29, 0x19, 0xa5, 0x33
+};
+
+static const uint8_t g_pwctrl2_data[] =
+{
+  0x06
+};
+
+static const uint8_t g_pwctrl3_data[] =
+{
+  0xa7
+};
+
+static const uint8_t g_vcom_data[] =
+{
+  0x18
+};
+
+static const uint8_t g_gammapos_data[] =
+{
+  0xf0, 0x09, 0x0b, 0x06, 0x04, 0x15,
+  0x2f, 0x54, 0x42, 0x3c, 0x17, 0x14,
+  0x18, 0x1b
+};
+
+static const uint8_t g_gammaneg_data[] =
+{
+  0xf0, 0x09, 0x0b, 0x06, 0x04, 0x03,
+  0x2d, 0x43, 0x42, 0x3b, 0x16, 0x14,
+  0x17, 0x1b
+};
+
+static const uint8_t g_cscon3_data[] =
+{
+  0x3c
+};
+
+static const uint8_t g_cscon4_data[] =
+{
+  0x69
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: st7796_select
+ *
+ * Description:
+ *   Select the SPI device, locking the bus and configuring SPI parameters.
+ *   This function locks the SPI bus, sets the SPI mode, bit width, and
+ *   frequency, then asserts the chip select signal.
+ *
+ * Input Parameters:
+ *   priv - Reference to the ST7796 device structure
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void st7796_select(FAR struct st7796_dev_s *priv)
+{
+  SPI_LOCK(priv->spi, true);
+  SPI_SETMODE(priv->spi, ST7796_SPIMODE);
+  SPI_SETBITS(priv->spi, 8);
+  SPI_SETFREQUENCY(priv->spi, priv->config.frequency);
+  SPI_SELECT(priv->spi, SPIDEV_DISPLAY(0), true);
+}
+
+/****************************************************************************
+ * Name: st7796_deselect
+ *
+ * Description:
+ *   Deselect the SPI device, releasing the chip select and unlocking the
+ *   SPI bus for other devices.
+ *
+ * Input Parameters:
+ *   priv - Reference to the ST7796 device structure
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void st7796_deselect(FAR struct st7796_dev_s *priv)
+{
+  SPI_SELECT(priv->spi, SPIDEV_DISPLAY(0), false);
+  SPI_LOCK(priv->spi, false);
+}
+
+/****************************************************************************
+ * Name: st7796_sendcmd
+ *
+ * Description:
+ *   Send a command byte to the ST7796 display controller. The D/C (Data/
+ *   Command) line is set to command mode before transmission.
+ *
+ * Input Parameters:
+ *   priv - Reference to the ST7796 device structure
+ *   cmd  - Command byte to send
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void st7796_sendcmd(FAR struct st7796_dev_s *priv, uint8_t cmd)
+{
+#ifdef CONFIG_SPI_CMDDATA
+  SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY(0), true);
+  SPI_SEND(priv->spi, cmd);
+#else
+#  error "CONFIG_SPI_CMDDATA must be enabled for ST7796"

Review Comment:
   Hello. Yes, I totally agree on this one. So when I put this 'guards' at the 
beggining of the code, it will fail even before to process the code. I will 
change it! Thank you for your feedback.



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