ESP32S3+HX8347+3线SPI运行LVGL例程

2023-12-30 20:56:16

一、clone lv_port_esp32到本地

git clone https://github.com/lvgl/lv_port_esp32.git

?二、增加hx8347.c、hx8347.h

????????components\lvgl_esp32_drivers\lvgl_tft下新增2个文件:hx8347.c、hx8347.h。因为lv_port_esp32中没有hx8347的驱动,需要自己写。这两个hx8347.c、hx8347.h修改HX8357的配置文件改的,有很多冗余信息,不影响使用。

hx8347.h

/**
 * @file HX8347.h
 *
 * Roughly based on the Adafruit_HX8347_Library
 *
 * This library should work with:
 * Adafruit 3.5" TFT 320x480 + Touchscreen Breakout
 *    http://www.adafruit.com/products/2050
 *
 * Adafruit TFT FeatherWing - 3.5" 480x320 Touchscreen for Feathers
 *    https://www.adafruit.com/product/3651
 *
 */

#ifndef HX8347_H
#define HX8347_H

#ifdef __cplusplus
extern "C" {
#endif

/*********************
 *      INCLUDES
 *********************/
#include <stdbool.h>
#include <stdint.h>

#ifdef LV_LVGL_H_INCLUDE_SIMPLE
#include "lvgl.h"
#else
#include "lvgl/lvgl.h"
#endif
#include "../lvgl_helpers.h"

 /*********************
 *      DEFINES
 *********************/
#define HX8347_DC   CONFIG_LV_DISP_PIN_DC
#define HX8347_RST  CONFIG_LV_DISP_PIN_RST
#define HX8347_BCKL CONFIG_LV_DISP_PIN_BCKL
#define HX8347_CS   6//CONFIG_LV_DISP_SPI_CS

#define HX8347_ENABLE_BACKLIGHT_CONTROL CONFIG_LV_ENABLE_BACKLIGHT_CONTROL
#define HX8347_INVERT_COLORS            CONFIG_LV_INVERT_COLORS

#if CONFIG_LV_BACKLIGHT_ACTIVE_LVL
  #define HX8347_BCKL_ACTIVE_LVL 1
#else
  #define HX8347_BCKL_ACTIVE_LVL 0
#endif


/*******************
 * HX8347B/D REGS
*********************/
#define HX8347D                    0xD  ///< Our internal const for D type
#define HX8347B                    0xB  ///< Our internal const for B type

#define HX8347_TFTWIDTH            320  ///< 320 pixels wide
#define HX8347_TFTHEIGHT           240  ///< 480 pixels tall

#define HX8347_NOP                0x00  ///< No op
#define HX8347_SWRESET            0x01  ///< software reset
#define HX8347_RDDID              0x04  ///< Read ID
#define HX8347_RDDST              0x09  ///< (unknown)

#define HX8347_RDPOWMODE          0x0A  ///< Read power mode Read power mode
#define HX8347_RDMADCTL           0x0B  ///< Read MADCTL
#define HX8347_RDCOLMOD           0x0C  ///< Column entry mode
#define HX8347_RDDIM              0x0D  ///< Read display image mode
#define HX8347_RDDSDR             0x0F  ///< Read dosplay signal mode

#define HX8347_SLPIN              0x10  ///< Enter sleep mode
#define HX8347_SLPOUT             0x11  ///< Exit sleep mode
#define HX8347B_PTLON             0x12  ///< Partial mode on
#define HX8347B_NORON             0x13  ///< Normal mode

#define HX8347_INVOFF             0x20  ///< Turn off invert
#define HX8347_INVON              0x21  ///< Turn on invert
#define HX8347_DISPOFF            0x28  ///< Display on
#define HX8347_DISPON             0x29  ///< Display off

#define HX8347_CASET              0x2A  ///< Column addr set
#define HX8347_PASET              0x2B  ///< Page addr set
#define HX8347_RAMWR              0x2C  ///< Write VRAM
#define HX8347_RAMRD              0x2E  ///< Read VRAm

#define HX8347B_PTLAR             0x30  ///< (unknown)
#define HX8347_TEON               0x35  ///< Tear enable on
#define HX8347_TEARLINE           0x44  ///< (unknown)
#define HX8347_MADCTL             0x16  ///< Memory access control
#define HX8347_COLMOD             0x3A  ///< Color mode

#define HX8347_SETOSC             0xB0  ///< Set oscillator
#define HX8347_SETPWR1            0xB1  ///< Set power control
#define HX8347B_SETDISPLAY        0xB2  ///< Set display mode
#define HX8347_SETRGB             0xB3  ///< Set RGB interface
#define HX8347D_SETCOM            0xB6  ///< Set VCOM voltage

#define HX8347B_SETDISPMODE       0xB4  ///< Set display mode
#define HX8347D_SETCYC            0xB4  ///< Set display cycle reg
#define HX8347B_SETOTP            0xB7  ///< Set OTP memory
#define HX8347D_SETC              0xB9  ///< Enable extension command

#define HX8347B_SET_PANEL_DRIVING 0xC0  ///< Set panel drive mode
#define HX8347D_SETSTBA           0xC0  ///< Set source option
#define HX8347B_SETDGC            0xC1  ///< Set DGC settings
#define HX8347B_SETID             0xC3  ///< Set ID
#define HX8347B_SETDDB            0xC4  ///< Set DDB
#define HX8347B_SETDISPLAYFRAME   0xC5  ///< Set display frame
#define HX8347B_GAMMASET          0xC8  ///< Set Gamma correction
#define HX8347B_SETCABC           0xC9  ///< Set CABC
#define HX8347_SETPANEL           0xCC  ///< Set Panel

#define HX8347B_SETPOWER          0xD0  ///< Set power control
#define HX8347B_SETVCOM           0xD1  ///< Set VCOM
#define HX8347B_SETPWRNORMAL      0xD2  ///< Set power normal

#define HX8347B_RDID1             0xDA  ///< Read ID #1
#define HX8347B_RDID2             0xDB  ///< Read ID #2
#define HX8347B_RDID3             0xDC  ///< Read ID #3
#define HX8347B_RDID4             0xDD  ///< Read ID #4

#define HX8347D_SETGAMMA          0xE0  ///< Set Gamma

#define HX8347B_SETGAMMA          0xC8 ///< Set Gamma
#define HX8347B_SETPANELRELATED   0xE9 ///< Set panel related

 /**********************
 *      TYPEDEFS
 **********************/

/**********************
 * GLOBAL PROTOTYPES
 **********************/

void hx8347_init(void);
void hx8347_flush(lv_disp_drv_t * drv, const lv_area_t * area, lv_color_t * color_map);
void hx8347_enable_backlight(bool backlight);
void hx8347_set_rotation(uint8_t r);

/**********************
 *      MACROS
 **********************/

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /*HX8347_H*/

hx8347.c

/**
 * @file HX8347.c
 *
 * Roughly based on the Adafruit_HX8347_Library
 *
 * This library should work with:
 * Adafruit 3.5" TFT 320x480 + Touchscreen Breakout
 *    http://www.adafruit.com/products/2050
 *
 * Adafruit TFT FeatherWing - 3.5" 480x320 Touchscreen for Feathers
 *    https://www.adafruit.com/product/3651
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "hx8347.h"
#include "disp_spi.h"
#include "driver/gpio.h"
#include "rom/gpio.h"
#include <esp_log.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

/*********************
 *      DEFINES
 *********************/
#define TAG        "HX8347"

#define MADCTL_MY  0x80 ///< Bottom to top
#define MADCTL_MX  0x40 ///< Right to left
#define MADCTL_MV  0x20 ///< Reverse Mode
#define MADCTL_ML  0x10 ///< LCD refresh Bottom to top
#define MADCTL_RGB 0x00 ///< Red-Green-Blue pixel order
#define MADCTL_BGR 0x08 ///< Blue-Green-Red pixel order
//#define MADCTL_MH  0x04 ///< LCD refresh right to left

/**********************
 *      TYPEDEFS
 **********************/

/*The LCD needs a bunch of command/argument values to be initialized. They are stored in this struct. */
typedef struct {
    uint8_t cmd;
    uint8_t data[16];
    uint8_t databytes; //No of data in data; bit 7 = delay after set; 0xFF = end of cmds.
} lcd_init_cmd_t;

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void hx8347_send_cmd(uint8_t cmd);
static void hx8347_send_data(void * data, uint16_t length);
static void hx8347_send_color(void * data, uint16_t length);
static void hx8347_send_16bit(uint8_t d1,uint8_t d2);

/**********************
 *  INITIALIZATION ARRAYS
 **********************/
// Taken from the Adafruit driver
static const uint8_t
  initb[] = {

  //Driving ability Setting
        0xEA,1,0x00, //PTBA[15:8]
        0xEB,1,0x20, //PTBA[7:0]
        0xEC,1,0x0C, //STBA[15:8]
        0xED,1,0xC4, //STBA[7:0]
        0xE8,1,0x40, //OPON[7:0]
        0xE9,1,0x38, //OPON1[7:0]
        0xF1,1,0x01, //OTPS1B
        0xF2,1,0x10, //GEN
        0x27,1,0xA3,

        //Gamma 2.2 Setting
        0x40,1,0x01, //
        0x41,1,0x00, //
        0x42,1,0x00, //
        0x43,1,0x10, //
        0x44,1,0x0E, //
        0x45,1,0x24, //
        0x46,1,0x04, //
        0x47,1,0x50, //
        0x48,1,0x02, //
        0x49,1,0x13, //
        0x4A,1,0x19, //
        0x4B,1,0x19, //
        0x4C,1,0x16, //
        0x50,1,0x1B, //
        0x51,1,0x31, //
        0x52,1,0x2F, //
        0x53,1,0x3F, //
        0x54,1,0x3F, //
        0x55,1,0x3E, //
        0x56,1,0x2F, //
        0x57,1,0x7B, //
        0x58,1,0x09, //
        0x59,1,0x06, //
        0x5A,1,0x06, //
        0x5B,1,0x0C, //
        0x5C,1,0x1D, //
        0x5D,1,0xCC, //

              // Window setting
      //#if (DISP_ORIENTATION == 0)
      //    0x04,1,0x00,
      //    0x05,1,0xEF,
      //    0x08,1,0x01,
      //    0x09,1,0x3F,
      //#else
      //    0x04,1,0x01,
      //    0x05,1,0x3F,
      //    0x08,1,0x00,
      //    0x09,1,0xEF,
      //#endif

        //Power Voltage Setting
        0x1B,1,0x1B, //VRH=4.65V
        0x1A,1,0x01, //BT (VGH~15V,VGL~-10V,DDVDH~5V)
        0x24,1,0x15, //VMH(VCOM High voltage ~3.2V)
        0x25,1,0x50, //VML(VCOM Low voltage -1.2V)

        0x23,1,0x88, //for Flicker adjust //can reload from OTP
        //Power on Setting

        0x18,1,0x36, // I/P_RADJ,N/P_RADJ, Normal mode 60Hz
        0x19,1,0x01, //OSC_EN='1', start Osc
        0x01,1,0x00, //DP_STB='0', out deep sleep
        0x1F,1,0x88,// GAS=1, VOMG=00, PON=0, DK=1, XDK=0, DVDH_TRI=0, STB=0
        0xFF, 0x80 + 50/5,
        0x1F,1,0x80,// GAS=1, VOMG=00, PON=0, DK=0, XDK=0, DVDH_TRI=0, STB=0
        0xFF, 0x80 + 50/5,
        0x1F,1,0x90,// GAS=1, VOMG=00, PON=1, DK=0, XDK=0, DVDH_TRI=0, STB=0
        0xFF, 0x80 + 50/5,
        0x1F,1,0xD0,// GAS=1, VOMG=10, PON=1, DK=0, XDK=0, DDVDH_TRI=0, STB=0
        0xFF, 0x80 + 50/5,
        //262k/65k color selection
        0x17,1,0x05, //default 0x06 262k color // 0x05 65k color
        //SET PANEL
        0x36,1,0x00, //SS_P, GS_P,REV_P,BGR_P
        //Display ON Setting
        0x28,1,0x38, //GON=1, DTE=1, D=1000
        0xFF, 0x80 + 50/5,
        0x28,1,0x3C, //GON=1, DTE=1, D=1100

        0x16,1,0x38, // MY=0, MX=1, MV=1, BGR=1
       // 0x16,1,0x80, // MY=1, MX=0, MV=0,竖屏时去掉注释
        //Set GRAM Area
        0x02,1,0x00,
        0x03,1,0x00, //Column Start
        0x04,1,0x00,
        0x05,1,0xEF, //Column End
        0x06,1,0x00,
        0x07,1,0x00, //Row Start
        0x08,1,0x01,
        0x09,1,0x3F, //Row End

        // 0x0a,1,0x00,
        // 0x0b,1,0xef,
        // 0x0c,1,0x00,
        // 0x0d,1,0x00,





	//  //************* Start Initial Sequence **********//
  // 	 0x2e,1,0x79,
	//  0xee,1,0x0c,
	//  //Driving ability Setting
	//  0xea,1,0x00,
	//  0xeb,1,0x20,
	//  0xec,1,0x08,
	//  0xed,1,0xc4,
	//  0xe8,1,0x40,
	//  0xe9,1,0x38,
	//  0xf1,1,0x01,
	//  0xf2,1,0x10,
	//  0x27,1,0xa3,
	//  //Gamma 2.2 Setting
	//  0x40,1,0x01,
	//  0x41,1,0x07,
	//  0x42,1,0x07,
	//  0x43,1,0x13,
	//  0x44,1,0x11,
	//  0x45,1,0x24,
	//  0x46,1,0x10,
	//  0x47,1,0x57,
	//  0x48,1,0x09,
	//  0x49,1,0x14,
	//  0x4a,1,0x19,
	//  0x4b,1,0x19,
	//  0x4c,1,0x16,
	//  0x50,1,0x1b,
	//  0x51,1,0x2e,
	//  0x52,1,0x2c,

	//  0x53,1,0x38,
	//  0x54,1,0x38,
	//  0x55,1,0x3e,
	//  0x56,1,0x2a,
	//  0x57,1,0x6f,
	//  0x58,1,0x09,
	//  0x59,1,0x06,
	//  0x5a,1,0x06,
	//  0x5b,1,0x0b,
	//  0x5c,1,0x16,
	//  0x5d,1,0xcc,

	//  //Power Voltage Setting
	//  0x1b,1,0x1b,
	//  0x1a,1,0x01,
	//  0x24,1,0x29,
	//  0x25,1,0x57,

	//  //****VCOM offset**///
	//  0x23,1,0x8a,

	//  //Power on Setting
	//  0x18,1,0x36,
	//  0x19,1,0x01,
	//  0x01,1,0x00,
	//  0x1f,1,0x88,
	//  0xFF, 0x80 + 50/5,
	//  0x1f,1,0x80,
	//  0xFF, 0x80 + 50/5,
	//  0x1f,1,0x90,
	//   0xFF, 0x80 + 50/5,
	//  0x1f,1,0xd0,
	//   0xFF, 0x80 + 50/5,

	//  //262k/65k color selection
	//  0x17,1,0x05,

	//  //SET PANEL
	//  0x36,1,0x00,

	//  //Display ON Setting
	//  0x28,1,0x38,
	//  0xFF, 0x80 + 50/5,
	//  0x28,1,0x3c,



  //    //设置2.8寸的GAMA参数
  //   0x46,1,0x94,
  //   0x47,1,0x41,
  //   0x48,1,0x00,
  //   0x49,1,0x33,
  //   0x4a,1,0x23,
  //   0x4b,1,0x45,
  //   0x4c,1,0x44,
  //   0x4d,1,0x77,
  //   0x4e,1,0x12,
  //   0x4f,1,0xcc,
  //   0x50,1,0x46,
  //   0x51,1,0x82,

  //   //320*240 peixl setting
  //   0x02,1,0x00,
  //   0x03,1,0x00,
  //   0x04,1,0x00,
  //   0x05,1,0xef,
  //   0x06,1,0x00,
  //   0x07,1,0x00,
  //   0x08,1,0x01,
  //   0x09,1,0x3f,
  //   //Display setting
  //   0x01,1,0x06,
  //   0x16,1,0xc8,
  //   0x23,1,0x95,
  //   0x24,1,0x95,
  //   0x25,1,0xFF,
  //   0x27,1,0x02,
  //   0x28,1,0x02,
  //   0x29,1,0x02,
  //   0x2a,1,0x02,
  //   0x2C,1,0x02,
  //   0x2d,1,0x02,
  //   0x3a,1,0x01,
  //   0x3b,1,0x01,
  //   0x3c,1,0xf0,
  //   0x3d,1,0x00,
  //   0xFF, 0x80 + 50/5,
  //   0x35,1,0x38,
  //   0x36,1,0x78,
  //   0x3E,1,0x3,
  //   0x40,1,0x0F,
  //   0x41,1,0xF0,
  //   //power supply setting
  //   0x19,1,0x49,
  //   0x93,1,0x0F,
  //   0xFF, 0x80 + 50/5,
  //   0x20,1,0x40,
  //   0x1D,1,0x07,
  //   0x1E,1,0x00,
  //   0x1F,1,0x03,
  //   //VCOM SETTING FOR 2.4' PANNNEL'
  //   0x44,1,0x42,
  //   0x45,1,0x11,
  //   0xFF, 0x80 + 50/5,
  //   0x1C,1,0x04,
  //   0xFF, 0x80 + 50/5,
  //   0x43,1,0x80,
  //   0xFF, 0x80 + 50/5,
  //   0x1B,1,0x08,
  //  0xFF, 0x80 + 50/5,
  //   // Display ON Setting
  //   0x90,1,0x7F,
  //   0x26,1,0x04,
  //   0xFF, 0x80 + 50/5,
  //   0x26,1,0x24,
  //   0x26,1,0x2C,
  //   0xFF, 0x80 + 50/5,
  //   0x26,1,0x3C,
  //   // Fixed the read data issue
  //   0x57,1,0x02,
  //   0x56,1,0x02,
  //   0x57,1,0x00,

  // //Driving ability Setting
  // 0xEa,1,0x00,
  // 0xeb,1,0x20,
  // 0xec,1,0x0c,
  // 0xed,1,0xc4,
  // 0xe8,1,0x38,
  // 0xe9,1,0x10,
  // 0xf1,1,0x01,
  // 0xf2,1,0x10,

  // //Gamma 2.2 Setting
  // 0x40,1,0x01,
  // 0x41,1,0x00,
  // 0x42,1,0x00,
  // 0x43,1,0x10,
  // 0x44,1,0x0e,
  // 0x45,1,0x24,
  // 0x46,1,0x04,
  // 0x47,1,0x50,
  // 0x48,1,0x02,
  // 0x49,1,0x13,
  // 0x4a,1,0x19,
  // 0x4b,1,0x19,
  // 0x4c,1,0x16,

  // 0x50,1,0x1b,
  // 0x51,1,0x31,
  // 0x52,1,0x2f,
  // 0x53,1,0x3f,
  // 0x54,1,0x3f,
  // 0x55,1,0x3e,
  // 0x56,1,0x2f,
  // 0x57,1,0x7b,
  // 0x58,1,0x09,
  // 0x59,1,0x06,
  // 0x5a,1,0x06,
  // 0x5b,1,0x0c,
  // 0x5c,1,0x1d,
  // 0x5d,1,0xcc,

  // //Power Voltage Setting
  // 0x1b,1,0x1b,	//4.65v
  // 0x1a,1,0x01,
  // 0x24,1,0x2f,
  // 0x25,1,0x57,

  // //vcom offset
  // 0x23,1,0x88,

  // //Power on Setting
  // 0x19,1,0x01,
  // 0x01,1,0x00,
  // 0x1f,1,0x88,
  // 0xFF, 0x80 + 50/5,

  // 0x1f,1,0x80,
  // 0xFF, 0x80 + 50/5,

  // 0x1f,1,0x90,
  // 0xFF, 0x80 + 50/5,

  // 0x1f,1,0xd0,
  // 0xFF, 0x80 + 50/5,

  // 0x17,1,0x60,

  // 0x28,1,0x38,
  // 0xFF, 0x80 + 50/5,
  // 0x28,1,0x3f,


  // 0x02,1,0x00,
  // 0x03,1,0x00,
  // 0x04,1,0x00,
  // 0x05,1,0xef,

  // 0x06,1,0x00,
  // 0x07,1,0x00,
  // 0x08,1,0x01,
  // 0x09,1,0x3f,

  // 0x28,1,0x38,
  // 0xFF, 0x80 + 50/5,
  // 0x1f,1,0x89,
  // 0xFF, 0x80 + 50/5,
  // 0x28,1,0x04,
  // 0xFF, 0x80 + 50/5,
  // 0x19,1,0x00,
  // 0xFF, 0x80 + 50/5,

    0                            // END OF COMMAND LIST
  }, initd[] = {

    0x18,1,0x88,  //UADJ 75Hz
    0x19,1,0x01,  //OSC_EN='1', start Osc

    //Power Voltage Setting
    0x1B,1,0x1E, //VRH=4.60V
    0x1C,1,0x07, //AP Crosstalk    04
    0x1A,1,0x01, //BT (VGH~15V,1,VGL~-10V,1,DDVDH~5V)
    0x24,1,0x38, //VMH 27
    0x25,1,0x5F, //VML
    //VCOM offset
    0x23,1,0x8C, //for Flicker adjust
    0x1F,1,0x88,// GAS=1,1, VOMG=00,1, PON=0,1, DK=1,1, XDK=0,1, DVDH_TRI=0,1, STB=0
    0xFF, 0x80 + 50/5,
    0x1F,1,0x80,// GAS=1,1, VOMG=00,1, PON=0,1, DK=0,1, XDK=0,1, DVDH_TRI=0,1, STB=0
    0xFF, 0x80 + 50/5,
    0x1F,1,0x90,// GAS=1,1, VOMG=00,1, PON=1,1, DK=0,1, XDK=0,1, DVDH_TRI=0,1, STB=0
    0xFF, 0x80 + 50/5,
    0x1F,1,0xD0,// GAS=1,1, VOMG=10,1, PON=1,1, DK=0,1, XDK=0,1, DDVDH_TRI=0,1, STB=0
    0xFF, 0x80 + 50/5,
    //Display ON Setting
    0x28,1,0x38,   //GON=1,1, DTE=1,1, D=1000
    0xFF, 0x80 + 400/5,
    0x28,1,0x3C,   //GON=1,1, DTE=1,1, D=1100
    0x36,1,0x00,   //REV,1, BGR
    0x17,1,0x05,  //16BIT/PIXEL

    //Gamma 2.2 Setting
    0x40,1,0x00, //
    0x41,1,0x00, //
    0x42,1,0x00, //
    0x43,1,0x11, //
    0x44,1,0x0e, //
    0x45,1,0x23, //
    0x46,1,0x08, //
    0x47,1,0x53, //
    0x48,1,0x03, //
    0x49,1,0x11, //
    0x4A,1,0x18, //
    0x4B,1,0x1a, //
    0x4C,1,0x16, //
    0x50,1,0x1c, //
    0x51,1,0x31, //
    0x52,1,0x2e, //
    0x53,1,0x3f, //
    0x54,1,0x3f, //
    0x55,1,0x3f, //
    0x56,1,0x2c, //
    0x57,1,0x77, //
    0x58,1,0x09, //
    0x59,1,0x05, //
    0x5A,1,0x07, //
    0x5B,1,0x0e, //
    0x5C,1,0x1c, //
    0x5D,1,0x88, //
    0xFF, 0x80 + 100/5,
    0,                           // END OF COMMAND LIST
  };

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
static uint8_t displayType = HX8347B;   //HX8347B:RGB

void lcd_cs_port(uint8_t status)
{
   if (status) {
       gpio_set_level(HX8347_CS, 1);
   } else {
       gpio_set_level(HX8347_CS, 0);
   }
}

void hx8347_init(void)
{
	//Initialize non-SPI GPIOs

  //3线spi没有DC引脚
  //       gpio_pad_select_gpio(HX8347_DC);
	// gpio_set_direction(HX8347_DC, GPIO_MODE_OUTPUT);
  gpio_pad_select_gpio(HX8347_CS);
	gpio_set_direction(HX8347_CS, GPIO_MODE_OUTPUT);

  gpio_pad_select_gpio(HX8347_RST);
	gpio_set_direction(HX8347_RST, GPIO_MODE_OUTPUT);

#if HX8347_ENABLE_BACKLIGHT_CONTROL
        gpio_pad_select_gpio(HX8347_BCKL);
	gpio_set_direction(HX8347_BCKL, GPIO_MODE_OUTPUT);
#endif

	//Reset the display
	gpio_set_level(HX8347_RST, 0);
	vTaskDelay(10 / portTICK_PERIOD_MS);
	gpio_set_level(HX8347_RST, 1);
	vTaskDelay(120 / portTICK_PERIOD_MS);

	ESP_LOGI(TAG, "Initialization.");

	//Send all the commands
	const uint8_t *addr = (displayType == HX8347B) ? initb : initd;
	uint8_t        cmd, x, numArgs;
	while((cmd = *addr++) > 0) { // '0' command ends list
		x = *addr++;
		numArgs = x & 0x7F;
		if (cmd != 0xFF) { // '255' is ignored
			if (x & 0x80) {  // If high bit set, numArgs is a delay time
				hx8347_send_cmd(cmd);
			} else {
				hx8347_send_cmd(cmd);
				hx8347_send_data((void *) addr, numArgs);
				addr += numArgs;
			}
		}
		if (x & 0x80) {       // If high bit set...
			vTaskDelay(numArgs * 5 / portTICK_PERIOD_MS); // numArgs is actually a delay time (5ms units)
		}
	}

	//hx8347_set_rotation(2); //Portrait竖屏 选0或2;Landscape横屏 选1或3。

// #if HX8347_INVERT_COLORS
// 	hx8347_send_cmd(HX8347_INVON);
// #else
//     hx8347_send_cmd(HX8347_INVOFF);
// #endif

	//hx8347_enable_backlight(true);
}


void hx8347_flush(lv_disp_drv_t * drv, const lv_area_t * area, lv_color_t * color_map)
{
	uint32_t size = lv_area_get_width(area) * lv_area_get_height(area);

  uint8_t data[34]={0};
	/* Column addresses  */
	uint8_t xb[] = {
	    (uint8_t) (area->x1 >> 8) & 0xFF,
	    (uint8_t) (area->x1) & 0xFF,
	    (uint8_t) (area->x2 >> 8) & 0xFF,
	    (uint8_t) (area->x2) & 0xFF,
	};

	/* Page addresses  */
	uint8_t yb[] = {
	    (uint8_t) (area->y1 >> 8) & 0xFF,
	    (uint8_t) (area->y1) & 0xFF,
	    (uint8_t) (area->y2 >> 8) & 0xFF,
	    (uint8_t) (area->y2) & 0xFF,
	};

  //ESP_LOGI(TAG, "hx8347_flush X:%d,%d,%d,%d",xb[0],xb[1],xb[2],xb[3]);
  //ESP_LOGI(TAG, "hx8347_flush Y:%d,%d,%d,%d",yb[0],yb[1],yb[2],yb[3]);
  // /*Column addresses*/
  // hx8347_send_cmd(0x02);
  // hx8347_send_data(&xb[0], 1);
  // hx8347_send_cmd(0x03);
  // hx8347_send_data(&xb[1], 1);
  // hx8347_send_cmd(0x04);
  // hx8347_send_data(&xb[2], 1);
  // hx8347_send_cmd(0x05);
  // hx8347_send_data(&xb[3], 1);


	// /*Page addresses*/
  // hx8347_send_cmd(0x06);
  // hx8347_send_data(&yb[0], 1);
  // hx8347_send_cmd(0x07);
  // hx8347_send_data(&yb[1], 1);
  // hx8347_send_cmd(0x08);
  // hx8347_send_data(&yb[2], 1);
  // hx8347_send_cmd(0x09);
  // hx8347_send_data(&yb[3], 1);

	// /*Memory write*/
	// hx8347_send_cmd(0x22);

  data[0]=0x70; data[1]=0x02; data[2]=0x72; data[3]=xb[0];
  data[4]=0x70; data[5]=0x03; data[6]=0x72; data[7]=xb[1];
  data[8]=0x70; data[9]=0x04; data[10]=0x72;data[11]=xb[2];
  data[12]=0x70;data[13]=0x05;data[14]=0x72;data[15]=xb[3];

  data[16]=0x70; data[17]=0x06; data[18]=0x72; data[19]=yb[0];
  data[20]=0x70; data[21]=0x07; data[22]=0x72; data[23]=yb[1];
  data[24]=0x70; data[25]=0x08; data[26]=0x72; data[27]=yb[2];
  data[28]=0x70; data[29]=0x09; data[30]=0x72; data[31]=yb[3];

  data[32]=0x70; data[33]=0x22;


  for(uint8_t i=0;i<17;i++)
  {
    lcd_cs_port(0);
      //disp_spi_send_data(&data[2*i],2);
      hx8347_send_16bit(data[2*i],data[2*i+1]);
    lcd_cs_port(1);
  }




	hx8347_send_color(color_map, size * 2);
}

void hx8347_enable_backlight(bool backlight)
{
#if HX8347_ENABLE_BACKLIGHT_CONTROL
    ESP_LOGD(TAG, "%s backlight.\n", backlight ? "Enabling" : "Disabling");
    uint32_t tmp = 0;

#if (HX8347_BCKL_ACTIVE_LVL==1)
    tmp = backlight ? 1 : 0;
#else
    tmp = backlight ? 0 : 1;
#endif

    gpio_set_level(HX8347_BCKL, tmp);
#endif
}


void hx8347_set_rotation(uint8_t r)
{
	r = r & 3; // can't be higher than 3

	switch(r) {
		case 0:
			r = MADCTL_MX | MADCTL_MY | MADCTL_RGB;
			break;
		case 1:
			r = MADCTL_MV | MADCTL_MY | MADCTL_RGB;
      		break;
		case 2:
			r = MADCTL_RGB;
  			break;
		case 3:
			r = MADCTL_MX | MADCTL_MV | MADCTL_RGB;
		break;
	}

	hx8347_send_cmd(HX8347_MADCTL);
	hx8347_send_data(&r, 1);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

static void hx8347_send_16bit(uint8_t d1,uint8_t d2)
{
  uint8_t c[2];
  c[0]=d1;
  c[1]=d2;

  lcd_cs_port(0);
  disp_spi_send_data(&c, 2);      //发送2字节
  lcd_cs_port(1);
}


static void hx8347_send_cmd(uint8_t cmd)
{
  uint8_t c;
  c=0x70;
  hx8347_send_16bit(c,cmd);

  // lcd_cs_port(0);
  // disp_spi_send_data(&c, 1);      //Command mode
	// disp_spi_send_data(&cmd, 1);
  // lcd_cs_port(1);
}


static void hx8347_send_data(void * data, uint16_t length)
{

  uint8_t c;
  c=0x72;

  lcd_cs_port(0);
  disp_spi_send_data(&c, 1);      //Data mode
	disp_spi_send_data(data, length);
  lcd_cs_port(1);
}


static void hx8347_send_color(void * data, uint16_t length)
{
  uint8_t c;
  c=0x72;

  lcd_cs_port(0);
  disp_spi_send_data(&c, 1);      //Data mode
	disp_spi_send_colors(data, length);
  lcd_cs_port(1);
}

三、修改components\lvgl_esp32_drivers\lvgl_tft\Kconfig文件内容

? ? ? ? 在相关位置增加hx8347相关信息,位置可参考其他显示IC配置,方便idf.py menuconfig进行配置:

? ? config LV_TFT_DISPLAY_USER_CONTROLLER_HX8347

? ? ? ? bool "HX8347"

? ? ? ? ? ? select LV_TFT_DISPLAY_CONTROLLER_HX8347

? ? ? ? ? ? select LV_TFT_DISPLAY_PROTOCOL_SPI

? ? config LV_TFT_DISPLAY_CONTROLLER_HX8347

? ? ? ? bool

? ? ? ? help

? ? ? ? ? ? HX8347 display controller.

??

配好后,idf.py menuconfig 中就会有hx8347的选项:

?

四、其他的文件修改:

disp_spi.h

//增加了DISP_SPI_SEND_POLLING
static inline void disp_spi_send_colors(uint8_t *data, size_t length) {
    disp_spi_transaction(data, length,
        DISP_SPI_SEND_QUEUED | DISP_SPI_SIGNAL_FLUSH|DISP_SPI_SEND_POLLING,
        NULL, 0, 0);
}

disp_spi.c

void disp_spi_transaction(const uint8_t *data, size_t length,
    disp_spi_send_flag_t flags, uint8_t *out,
    uint64_t addr, uint8_t dummy_bits)
{
    .......


    if (length <= 4 && data != NULL) {
        t.base.flags = SPI_TRANS_USE_TXDATA;
        memcpy(t.base.tx_data, data, length);
    } else {
        t.base.flags =SPI_DEVICE_CLK_AS_CS|SPI_DEVICE_NO_DUMMY;//增加了这句
        t.base.tx_buffer = data;
    }
.......

disp_driver.h增加了:

#elif defined CONFIG_LV_TFT_DISPLAY_CONTROLLER_HX8347
#include "hx8347.h"

disp_dirver.c增加了:

#elif defined CONFIG_LV_TFT_DISPLAY_CONTROLLER_HX8347
    hx8347_init();



#elif defined CONFIG_LV_TFT_DISPLAY_CONTROLLER_HX8347
	hx8347_flush(drv, area, color_map);

应该还有其他的地方修改,总之是使lv_port_esp32支持HX8347。

五、最后

LVGL

六、总结

1、ESP32S3运行LVGL,需要LVGL+lv_port_esp32。

2、运行LVGL通常需要4线spi,很不巧,笔者手头的HX8347只支持3线SPI。所以什么都要自己写。3线SPI性能比4线差不少。CS还需要手动控制。

3、0x16寄存器要设置下面这样,否则左右可能颠倒

0x16,1,0x38, // MY=0, MX=1, MV=1, BGR=1

如果用竖屏,要加上:

?0x16,1,0x80, // MY=1, MX=0, MV=0,竖屏时去掉注释

4、idf.py menuconfig的一些设置:

flash size

?运行的例子:

LVGL configuration

LVGL TFT Display configration?

?

文章来源:https://blog.csdn.net/weixin_44067125/article/details/135308441
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。