LCD driver and emwin example of RT-Thread study notes series (2)

Preface

With the rapid development of the Internet of Things, smart home, smart control and other industries, there is an increasing demand for small, beautiful, fast-responsive and low-cost, modular, and customized embedded systems, which reduce usage requirements and improve user experience , Human-computer interaction is a very good method, so this article records the record and process of debugging rtthread+lcd+emwin GUI

1. Resource introduction

The hardware is a self-drawn PCB board, the resources are as follows

  • MCU:STM32F407ZGT6
  • LCD: 3.2 inches 320*240 resolution, driver chip: ILI9341. Except for the RESET and backlight control pins, other pin connections are compatible with the STM32F407 Explorer development board of Punctual Atom
  • Use FSMC interface driver

The software uses rtthread studio 1.1.5 version

Second, the configuration process

1. Add emwin package

Insert picture description here
Insert picture description here

The software package will be available under the packges of the project after the enable example is saved. The software package has LCD and OLED driver demos. After looking at it, it is similar to the driver of Punctual Atom. It can be used directly. There is no demo folder inside, so select the software package and right-click to open the directory where the resources are located . There is a demo folder in the software package. I use the lcd driver, so I copy the two files drv_lcd.h and drv_lcd.h to the drive The drivers folder is as follows
Insert picture description here

2. Configure external pins

Configure the off-chip FSMC interface, my configuration is as follows

static void HAL_FSMC_MspInit(void){
    
    
  /* USER CODE BEGIN FSMC_MspInit 0 */

  /* USER CODE END FSMC_MspInit 0 */
  GPIO_InitTypeDef GPIO_InitStruct ={
    
    0};

  /* Peripheral clock enable */
  __HAL_RCC_FSMC_CLK_ENABLE();

  /*lcd IO config*/
    __HAL_RCC_GPIOD_CLK_ENABLE();
    __HAL_RCC_GPIOE_CLK_ENABLE();
    __HAL_RCC_GPIOF_CLK_ENABLE();
    __HAL_RCC_GPIOG_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

    //PD0,1,4,5,8,9,10,14,15
    GPIO_InitStruct.Pin=GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_8|\
                     GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_14|GPIO_PIN_15;
    GPIO_InitStruct.Mode=GPIO_MODE_AF_PP;      //ÍÆÍ츴ÓÃ
    GPIO_InitStruct.Pull=GPIO_PULLUP;          //ÉÏÀ­
    GPIO_InitStruct.Speed=GPIO_SPEED_HIGH;     //¸ßËÙ
    GPIO_InitStruct.Alternate=GPIO_AF12_FSMC;  //¸´ÓÃΪFSMC
    HAL_GPIO_Init(GPIOD,&GPIO_InitStruct);     //³õʼ»¯

    //PE7,8,9,10,11,12,13,14,15
    GPIO_InitStruct.Pin=GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|\
                     GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
    HAL_GPIO_Init(GPIOE,&GPIO_InitStruct);

    //PF12
    GPIO_InitStruct.Pin=GPIO_PIN_12;
    HAL_GPIO_Init(GPIOF,&GPIO_InitStruct);

    //PG12
    GPIO_InitStruct.Pin=GPIO_PIN_12;
    HAL_GPIO_Init(GPIOG,&GPIO_InitStruct);
  /* USER CODE END FSMC_MspInit 1 */
}

The following macro definitions are enabled in the stm32f4xx_hal_conf.h file

#define HAL_CRC_MODULE_ENABLED
#define HAL_SRAM_MODULE_ENABLED

Since the project created by RT-Thread studio does not have the source file of the LL library, the FSMC interface needs to use the two files stm32f4xx_ll_fsmc.c and stm32f4xx_ll_fmc.c, so you need to copy these two files from the library files in the project generated by STM32CUBE
Insert picture description here
Go to the project and open the drv_lcd.c file to modify the definition of LCD_BL . I directly drive it with other IO elsewhere, so the operation of this LCD_BL pin under this file is fully commented

The LCD_BL , LCD_RESET and power PWR configuration of the LCD screen interface are as follows
: the hardware uses MOS tubes to control the power of the LCD, the following is to turn on the power directly after powering on

#define PWR_PIN  GET_PIN(C, 2)
#define LCD_BL   GET_PIN(A, 6)
#define LCD_RESET   GET_PIN(A, 4)
static int open_lcd_power(void)
{
    
    
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOC_CLK_ENABLE();
    rt_pin_mode(PWR_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(LCD_BL, PIN_MODE_OUTPUT);
    rt_pin_mode(LCD_RESET, PIN_MODE_OUTPUT);
    rt_pin_write(PWR_PIN, PIN_HIGH);
    rt_pin_write(LCD_BL, PIN_HIGH);
    rt_pin_write(LCD_RESET, PIN_HIGH);
    return RT_EOK;
}
INIT_BOARD_EXPORT(open_lcd_power);

Note: At this time, direct compilation will report an error, because the author of the package is made and uploaded by keil, so the emwin library is keil's
solution: the official library is to find a GCC compiler library to use, here is simple and crude Modifying the keil library to the format of the GCC library can even be used, just use it first, and look for it later.
Insert picture description here
At this time, there are still errors in the compilation
Insert picture description here
. The undefined _sbrk function is as follows . This function needs to use libc, so it is in the package Just enable this libc
Insert picture description here
Insert picture description here

Save the compilation without errors (the warnings are all because of library incompatibility, you can ignore it for the time being, you can find a library compiled by gcc to replace it later)

The emwin GUI needs to use hardware CRC, so you need to open the CRC clock, open the GUI_Test.c file to add code, use the cube to generate the HAL_CRC_MspInit function according to a stricter point, or simply add the __HAL_RCC_CRC_CLK_ENABLE() function directly in front, since the test program If you use a function call, then follow the standard.
Tip: There is a pit here that CrcHandle.State is not HAL_CRC_STATE_RESET by default, which makes it impossible to turn on the clock, so it is mandatory to make it equal to the HAL_CRC_STATE_RESET state (still a bit rougher)

void HAL_CRC_MspInit(CRC_HandleTypeDef* hcrc)
{
    
    
  if(hcrc->Instance==CRC)
  {
    
    
    __HAL_RCC_CRC_CLK_ENABLE();
  }
}
static void test_thread(void *param)
{
    
    
    CRC_HandleTypeDef   CrcHandle;
    CrcHandle.Instance = CRC; 
    CrcHandle.State = HAL_CRC_STATE_RESET;
    HAL_CRC_Init(&CrcHandle);
    GUI_Init();
    GUIDEMO_Main();
}

Compile and download at this time will run normally
Insert picture description here


to sum up

  • Emwin must need to enable CRC. I directly use the function to drive in the CRC state, which caused the clock to not turn on for a long time.
  • The RESET pin of the LCD screen must be pulled high. When drawing the board, the pin that was not directly connected to the board was suspended and the screen could not work.
  • If there is no LCD pin configuration in the software package, you need to configure it according to your own interface
  • emwin library files and libc need to be opened

Guess you like

Origin blog.csdn.net/weixin_39137443/article/details/109962978