为了方便以后快速查阅代码,提高开发效率,在这里特将LTDC驱动的初始化代码贴上来。本代码是基于ST官方STM32F429 Discovery Demo板的,学习LTDC驱动时参考了Demo板的官方例程,但是总觉得官方例程写得有点繁琐,不简洁明了,于是在此基础上重新整理代码得到如下初始化模板。
说明:本测试例程只使用了Layer1,显示缓冲区为FrameBuffer,颜色格式为RGB565,初始时缓冲区为0,所以显示Layer1时是全黑色,如果不使能Layer1只显示background,则屏幕显示为红色。 - void LCD_Init(void)
- {
- LTDC_InitTypeDef LTDC_InitStruct;
-
- /* Configure the LCD Control pins ------------------------------------------*/
- LCD_CtrlLinesConfig();
-
- /* Configure the LCD_SPI interface -----------------------------------------*/
- LCD_SPI_Config();
-
- /* Power on the LCD --------------------------------------------------------*/
- LCD_PowerOn();
-
- /* Enable the LTDC Clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_LTDC, ENABLE);
-
- /* Configure the LCD Control pins */
- LCD_GPIO_Config();
-
- /* Enable Pixel Clock ------------------------------------------------------*/
-
- /* Configure PLLSAI prescalers for LCD */
- /* PLLSAI_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */
- /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAI_N = 192 Mhz */
- /* PLLLCDCLK = PLLSAI_VCO Output/PLLSAI_R = 192/3 = 64 Mhz */
- /* LTDC clock frequency = PLLLCDCLK / RCC_PLLSAIDivR = 64/8 = 8 Mhz */
- RCC_PLLSAIConfig(192, 7, 3);
- RCC_LTDCCLKDivConfig(RCC_PLLSAIDivR_Div8);
-
- /* Enable PLLSAI Clock */
- RCC_PLLSAICmd(ENABLE);
- /* Wait for PLLSAI activation */
- while(RCC_GetFlagStatus(RCC_FLAG_PLLSAIRDY) == RESET)
- {
- }
-
- /* LTDC Initialization -----------------------------------------------------*/
-
- /* Initialize the horizontal synchronization polarity as active low*/
- LTDC_InitStruct.LTDC_HSPolarity = LTDC_HSPolarity_AL;
- /* Initialize the vertical synchronization polarity as active low */
- LTDC_InitStruct.LTDC_VSPolarity = LTDC_VSPolarity_AL;
- /* Initialize the data enable polarity as active low */
- LTDC_InitStruct.LTDC_DEPolarity = LTDC_DEPolarity_AL;
- /* Initialize the pixel clock polarity as input pixel clock */
- LTDC_InitStruct.LTDC_PCPolarity = LTDC_PCPolarity_IPC;
-
- /* Timing configuration */
- /* Configure horizontal synchronization width */
- //LTDC_InitStruct.LTDC_HorizontalSync = 9;
- LTDC_InitStruct.LTDC_HorizontalSync = 9;
- /* Configure vertical synchronization height */
- LTDC_InitStruct.LTDC_VerticalSync = 1;
- /* Configure accumulated horizontal back porch */
- LTDC_InitStruct.LTDC_AccumulatedHBP = 29;
- /* Configure accumulated vertical back porch */
- LTDC_InitStruct.LTDC_AccumulatedVBP = 3;
- /* Configure accumulated active width */
- LTDC_InitStruct.LTDC_AccumulatedActiveW = 269;
- /* Configure accumulated active height */
- LTDC_InitStruct.LTDC_AccumulatedActiveH = 323;
- /* Configure total width */
- LTDC_InitStruct.LTDC_TotalWidth = 279;
- /* Configure total height */
- LTDC_InitStruct.LTDC_TotalHeigh = 327;
-
- /* Configure R,G,B component values for LCD background color */
- LTDC_InitStruct.LTDC_BackgroundRedValue = 0xff;
- LTDC_InitStruct.LTDC_BackgroundGreenValue = 0;
- LTDC_InitStruct.LTDC_BackgroundBlueValue = 0;
-
- LTDC_Init(<DC_InitStruct);
- }
LCD串行控制信号线配置: - void LCD_CtrlLinesConfig(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable GPIOs clock*/
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE);
- /* Configure NCS in Output Push-Pull mode */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- /* Configure WRX in Output Push-Pull mode */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* Set chip select pin high */
- GPIO_SetBits(GPIOC, GPIO_Pin_2);
- }
[code]void LCD_SPI_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable LCD_SPI_SCK_GPIO_CLK, LCD_SPI_MISO_GPIO_CLK and LCD_SPI_MOSI_GPIO_CLK clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
/* Enable LCD_SPI and SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI5, ENABLE);
/* Configure LCD_SPI SCK pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* Configure LCD_SPI MISO pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* Configure LCD_SPI MOSI pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* Connect SPI SCK */
GPIO_PinAFConfig(GPIOF, GPIO_PinSource7, GPIO_AF_SPI5);
/* Connect SPI MISO */
GPIO_PinAFConfig(GPIOF, GPIO_PinSource8, GPIO_AF_SPI5);
/* Connect SPI MOSI */
GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_SPI5);
SPI_I2S_DeInit(SPI5);
/* SPI configuration -------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
/* SPI baudrate is set to 5.6 MHz (PCLK2/SPI_BaudRatePrescaler = 90/16 = 5.625 MHz)
|