This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 25443a37ee1f614b59145f8a9f99ec1e875fa909
Author: wangjianyu3 <wangjian...@xiaomi.com>
AuthorDate: Sat Apr 5 22:22:47 2025 +0800

    boards/szpi-esp32s3: Set default backlight for LCD
    
    Set default brightness of LCD backlight to about 10%.
    
    Signed-off-by: wangjianyu3 <wangjian...@xiaomi.com>
---
 .../esp32s3/lckfb-szpi-esp32s3/src/esp32s3-szpi.h  |  3 ++
 .../lckfb-szpi-esp32s3/src/esp32s3_board_lcd.c     | 56 +++++++++++++++++++---
 .../lckfb-szpi-esp32s3/src/esp32s3_board_spi.c     | 11 +++--
 3 files changed, 59 insertions(+), 11 deletions(-)

diff --git a/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3-szpi.h 
b/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3-szpi.h
index bf888c54ea..cb0724249a 100644
--- a/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3-szpi.h
+++ b/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3-szpi.h
@@ -39,6 +39,9 @@
 #define GPIO_LCD_DC         (39)
 #define GPIO_LCD_RST        (-1)
 #define SZPI_LCD_CS_PATH    "/dev/gpio0"
+#define SZPI_LCD_PWM_PATH   "/dev/pwm0"
+#define SZPI_LCD_PWM_FREQ   (100)
+#define SZPI_LCD_PWM_DUTY   (0xe666)       /* 0x1 ~ 0xffff */
 
 #define FT5X06_I2C_ADDRESS  (0x38)
 #define FT5X06_FREQUENCY    (400000)
diff --git a/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3_board_lcd.c 
b/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3_board_lcd.c
index b7d36e69a0..e7cca55a8f 100644
--- a/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3_board_lcd.c
+++ b/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3_board_lcd.c
@@ -31,6 +31,7 @@
 #include <fcntl.h>
 #include <debug.h>
 #include <errno.h>
+#include <sys/ioctl.h>
 
 #include <nuttx/arch.h>
 #include <nuttx/board.h>
@@ -39,6 +40,7 @@
 #include <nuttx/lcd/lcd.h>
 #include <nuttx/lcd/st7789.h>
 #include <nuttx/fs/fs.h>
+#include <nuttx/timers/pwm.h>
 
 #include <arch/board/board.h>
 
@@ -58,6 +60,7 @@
 
 static struct spi_dev_s *g_spidev;
 static struct lcd_dev_s *g_lcd;
+static struct file g_pwm_file;
 
 /****************************************************************************
  * Public Functions
@@ -78,27 +81,56 @@ static struct lcd_dev_s *g_lcd;
 
 int board_lcd_initialize(void)
 {
+  struct pwm_info_s pwm;
+  struct file f;
   ssize_t n;
-  int fd;
+  int ret;
 
   /* Pull down C/S */
 
-  fd = nx_open(SZPI_LCD_CS_PATH, O_RDWR);
-  if (fd < 0)
+  ret = file_open(&f, SZPI_LCD_CS_PATH, O_RDWR);
+  if (ret < 0)
     {
       spierr("open C/S pin failed\n");
       return -ENODEV;
     }
 
-  n = nx_write(fd, "1", 1);
-  nx_close(fd);
+  n = file_write(&f, "1", 1);
+  file_close(&f);
   if (n != 1)
     {
       spierr("write C/S pin failed\n");
       return -EIO;
     }
 
-  /* Turn on LCD backlight */
+  /* Turn on LCD backlight (10% brightness) */
+
+  pwm.frequency = SZPI_LCD_PWM_FREQ;
+  pwm.duty = SZPI_LCD_PWM_DUTY;
+
+  ret = file_open(&g_pwm_file, SZPI_LCD_PWM_PATH, O_RDONLY);
+  if (ret < 0)
+    {
+      pwmerr("Open PWM failed\n");
+      return -ENODEV;
+    }
+
+  ret = file_ioctl(&g_pwm_file, PWMIOC_SETCHARACTERISTICS,
+                   (unsigned long)((uintptr_t)&pwm));
+  if (ret < 0)
+    {
+      pwmerr("Set PWM failed\n");
+      file_close(&g_pwm_file);
+      return -ENOTTY;
+    }
+
+  ret = file_ioctl(&g_pwm_file, PWMIOC_START, 0);
+  if (ret < 0)
+    {
+      pwmerr("Start PWM failed\n");
+      file_close(&g_pwm_file);
+      return -ENOTTY;
+    }
 
   g_spidev = esp32s3_spibus_initialize(ESP32S3_SPI2);
   if (!g_spidev)
@@ -161,7 +193,19 @@ struct lcd_dev_s *board_lcd_getdev(int devno)
 
 void board_lcd_uninitialize(void)
 {
+  int ret;
+
   /* Turn the display off */
 
   g_lcd->setpower(g_lcd, 0);
+
+  /* Close backlight PWM */
+
+  ret = file_ioctl(&g_pwm_file, PWMIOC_STOP, 0);
+  if (ret < 0)
+    {
+      pwmerr("Stop PWM failed\n");
+    }
+
+  file_close(&g_pwm_file);
 }
diff --git a/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3_board_spi.c 
b/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3_board_spi.c
index 271874ab15..3ea1c837e6 100644
--- a/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3_board_spi.c
+++ b/boards/xtensa/esp32s3/lckfb-szpi-esp32s3/src/esp32s3_board_spi.c
@@ -90,23 +90,24 @@ int esp32s3_spi2_cmddata(struct spi_dev_s *dev, uint32_t 
devid, bool cmd)
 #if defined(CONFIG_ESP32S3_SPI2) && defined(CONFIG_ESP32S3_SPI_UDCS)
 void esp32s3_spi2_select(struct spi_dev_s *dev, uint32_t devid, bool select)
 {
+  struct file f;
   ssize_t n;
-  int fd;
+  int ret;
 
-  fd = nx_open(SZPI_LCD_CS_PATH, O_RDWR);
-  if (fd < 0)
+  ret = file_open(&f, SZPI_LCD_CS_PATH, O_RDWR);
+  if (ret < 0)
     {
       spierr("open C/S pin failed\n");
       return;
     }
 
-  n = nx_write(fd, select ? "0" : "1", 1);
+  n = file_write(&f, select ? "0" : "1", 1);
   if (n != 1)
     {
       spierr("write C/S pin failed\n");
     }
 
-  nx_close(fd);
+  file_close(&f);
 }
 #endif
 

Reply via email to