Страницы

Страницы

суббота, 11 февраля 2017 г.

LCD 800x480 init







#include "stm32f4xx.h"

static uint32_t frameBuf0 = (uint32_t)(0xd0000000);

#define SDRAM_MODEREG_BURST_LENGTH_1             ((uint16_t)0x0000)
#define SDRAM_MODEREG_BURST_LENGTH_2             ((uint16_t)0x0001)
#define SDRAM_MODEREG_BURST_LENGTH_4             ((uint16_t)0x0002)
#define SDRAM_MODEREG_BURST_LENGTH_8             ((uint16_t)0x0004)
#define SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL      ((uint16_t)0x0000)
#define SDRAM_MODEREG_BURST_TYPE_INTERLEAVED     ((uint16_t)0x0008)
#define SDRAM_MODEREG_CAS_LATENCY_2              ((uint16_t)0x0020)
#define SDRAM_MODEREG_CAS_LATENCY_3              ((uint16_t)0x0030)
#define SDRAM_MODEREG_OPERATING_MODE_STANDARD    ((uint16_t)0x0000)
#define SDRAM_MODEREG_WRITEBURST_MODE_PROGRAMMED ((uint16_t)0x0000)
#define SDRAM_MODEREG_WRITEBURST_MODE_SINGLE     ((uint16_t)0x0200)

static void __Delay(__IO uint32_t nCount)
{
    __IO uint32_t index = 0;
    for(index = (100000 * nCount); index != 0; index--)
    {
    }
}

static void LCD_AF_GPIOConfig(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;

    /* Enable GPIOI, GPIOJ, GPIOG, GPIOF, GPIOH AHB Clocks */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | \
                           RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | \
                           RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOG, ENABLE);

    /* GPIOs Configuration */
    /*
    +------------------------+-----------------------+----------------------------+
    +                       LCD pins assignment                                   +
    +------------------------+-----------------------+----------------------------+
    |  LCD_TFT R2 <-> PC.12  |  LCD_TFT G2 <-> PA.06 |  LCD_TFT B2 <-> PD.06      |
    |  LCD_TFT R3 <-> PB.00  |  LCD_TFT G3 <-> PG.10 |  LCD_TFT B3 <-> PG.11      |
    |  LCD_TFT R4 <-> PA.11  |  LCD_TFT G4 <-> PB.10 |  LCD_TFT B4 <-> PG.12      |
    |  LCD_TFT R5 <-> PA.12  |  LCD_TFT G5 <-> PB.11 |  LCD_TFT B5 <-> PA.03      |
    |  LCD_TFT R6 <-> PB.01  |  LCD_TFT G6 <-> PC.07 |  LCD_TFT B6 <-> PB.08      |
    |  LCD_TFT R7 <-> PG.06  |  LCD_TFT G7 <-> PD.03 |  LCD_TFT B7 <-> PB.09      |
    -------------------------------------------------------------------------------
    |  LCD_TFT HSYNC <-> PC.06  | LCDTFT VSYNC <->  PA.04 |
    |  LCD_TFT CLK   <-> PG.07  | LCD_TFT DE   <->  PF.10 |
    -----------------------------------------------------

    */

    /* GPIOA configuration */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_LTDC);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_6 | \
                               GPIO_Pin_11 | GPIO_Pin_12;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* GPIOB configuration */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource0,  0x09);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, 0x09);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_LTDC);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | \
                               GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;

    GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* GPIOC configuration */
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_LTDC);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_10;

    GPIO_Init(GPIOC, &GPIO_InitStruct);

    /* GPIOD configuration */
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_LTDC);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_6;

    GPIO_Init(GPIOD, &GPIO_InitStruct);

    /* GPIOF configuration */
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource10, GPIO_AF_LTDC);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;

    GPIO_Init(GPIOF, &GPIO_InitStruct);

    /* GPIOG configuration */
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource6, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource7, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource10, 0x09);
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource11, GPIO_AF_LTDC);
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource12, 0x09);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_10 | \
                               GPIO_Pin_11 | GPIO_Pin_12;

    GPIO_Init(GPIOG, &GPIO_InitStruct);
}

void SDRAM_InitSequence(void)
{
    FMC_SDRAMCommandTypeDef FMC_SDRAMCommandStructure;
    uint32_t tmpr = 0;

    /* Step 3 --------------------------------------------------------------------*/
    /* Configure a clock configuration enable command */
    FMC_SDRAMCommandStructure.FMC_CommandMode = FMC_Command_Mode_CLK_Enabled;
    FMC_SDRAMCommandStructure.FMC_CommandTarget = FMC_Command_Target_bank2;
    FMC_SDRAMCommandStructure.FMC_AutoRefreshNumber = 1;
    FMC_SDRAMCommandStructure.FMC_ModeRegisterDefinition = 0;
    /* Wait until the SDRAM controller is ready */
    while(FMC_GetFlagStatus(FMC_Bank2_SDRAM, FMC_FLAG_Busy) != RESET)
    {
    }
    /* Send the command */
    FMC_SDRAMCmdConfig(&FMC_SDRAMCommandStructure);

    /* Step 4 --------------------------------------------------------------------*/
    /* Insert 100 ms delay */
    __Delay(10);

    /* Step 5 --------------------------------------------------------------------*/
    /* Configure a PALL (precharge all) command */
    FMC_SDRAMCommandStructure.FMC_CommandMode = FMC_Command_Mode_PALL;
    FMC_SDRAMCommandStructure.FMC_CommandTarget = FMC_Command_Target_bank2;
    FMC_SDRAMCommandStructure.FMC_AutoRefreshNumber = 1;
    FMC_SDRAMCommandStructure.FMC_ModeRegisterDefinition = 0;
    /* Wait until the SDRAM controller is ready */
    while(FMC_GetFlagStatus(FMC_Bank2_SDRAM, FMC_FLAG_Busy) != RESET)
    {
    }
    /* Send the command */
    FMC_SDRAMCmdConfig(&FMC_SDRAMCommandStructure);

    /* Step 6 --------------------------------------------------------------------*/
    /* Configure a Auto-Refresh command */
    FMC_SDRAMCommandStructure.FMC_CommandMode = FMC_Command_Mode_AutoRefresh;
    FMC_SDRAMCommandStructure.FMC_CommandTarget = FMC_Command_Target_bank2;
    FMC_SDRAMCommandStructure.FMC_AutoRefreshNumber = 4;
    FMC_SDRAMCommandStructure.FMC_ModeRegisterDefinition = 0;
    /* Wait until the SDRAM controller is ready */
    while(FMC_GetFlagStatus(FMC_Bank2_SDRAM, FMC_FLAG_Busy) != RESET)
    {
    }
    /* Send the  first command */
    FMC_SDRAMCmdConfig(&FMC_SDRAMCommandStructure);

    /* Wait until the SDRAM controller is ready */
    while(FMC_GetFlagStatus(FMC_Bank2_SDRAM, FMC_FLAG_Busy) != RESET)
    {
    }
    /* Send the second command */
    FMC_SDRAMCmdConfig(&FMC_SDRAMCommandStructure);

    /* Step 7 --------------------------------------------------------------------*/
    /* Program the external memory mode register */
    tmpr = (uint32_t)SDRAM_MODEREG_BURST_LENGTH_2          |
           SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL   |
           SDRAM_MODEREG_CAS_LATENCY_3           |
           SDRAM_MODEREG_OPERATING_MODE_STANDARD |
           SDRAM_MODEREG_WRITEBURST_MODE_SINGLE;

    /* Configure a load Mode register command*/
    FMC_SDRAMCommandStructure.FMC_CommandMode = FMC_Command_Mode_LoadMode;
    FMC_SDRAMCommandStructure.FMC_CommandTarget = FMC_Command_Target_bank2;
    FMC_SDRAMCommandStructure.FMC_AutoRefreshNumber = 1;
    FMC_SDRAMCommandStructure.FMC_ModeRegisterDefinition = tmpr;
    /* Wait until the SDRAM controller is ready */
    while(FMC_GetFlagStatus(FMC_Bank2_SDRAM, FMC_FLAG_Busy) != RESET)
    {
    }
    /* Send the command */
    FMC_SDRAMCmdConfig(&FMC_SDRAMCommandStructure);

    /* Step 8 --------------------------------------------------------------------*/

    /* Set the refresh rate counter */
    /* (15.62 us x Freq) - 20 */
    /* Set the device refresh counter */
    FMC_SetRefreshCount(1386);
    /* Wait until the SDRAM controller is ready */
    while(FMC_GetFlagStatus(FMC_Bank2_SDRAM, FMC_FLAG_Busy) != RESET)
    {
    }
}

void SDRAM_GPIOConfig(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    /* Enable GPIOs clock */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD |
                           RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOG, ENABLE);

    /*-- GPIOs Configuration -----------------------------------------------------*/
    /*
     +-------------------+--------------------+--------------------+--------------------+
     +                       SDRAM pins assignment                                      +
     +-------------------+--------------------+--------------------+--------------------+
     | PD0  <-> FMC_D2   | PE0  <-> FMC_NBL0  | PF0  <-> FMC_A0    | PG0  <-> FMC_A10   |
     | PD1  <-> FMC_D3   | PE1  <-> FMC_NBL1  | PF1  <-> FMC_A1    | PG1  <-> FMC_A11   |
     | PD8  <-> FMC_D13  | PE7  <-> FMC_D4    | PF2  <-> FMC_A2    | PG8  <-> FMC_SDCLK |
     | PD9  <-> FMC_D14  | PE8  <-> FMC_D5    | PF3  <-> FMC_A3    | PG15 <-> FMC_NCAS  |
     | PD10 <-> FMC_D15  | PE9  <-> FMC_D6    | PF4  <-> FMC_A4    |--------------------+
     | PD14 <-> FMC_D0   | PE10 <-> FMC_D7    | PF5  <-> FMC_A5    |
     | PD15 <-> FMC_D1   | PE11 <-> FMC_D8    | PF11 <-> FMC_NRAS  |
     +-------------------| PE12 <-> FMC_D9    | PF12 <-> FMC_A6    |
                         | PE13 <-> FMC_D10   | PF13 <-> FMC_A7    |
                         | PE14 <-> FMC_D11   | PF14 <-> FMC_A8    |
                         | PE15 <-> FMC_D12   | PF15 <-> FMC_A9    |
     +-------------------+--------------------+--------------------+
     | PB5 <-> FMC_SDCKE1|
     | PB6 <-> FMC_SDNE1 |
     | PC0 <-> FMC_SDNWE |
     +-------------------+

    */

    /* Common GPIO configuration */
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

    /* GPIOB configuration */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource5 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6 , GPIO_AF_FMC);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5  | GPIO_Pin_6;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /* GPIOC configuration */
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource0 , GPIO_AF_FMC);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

    GPIO_Init(GPIOC, &GPIO_InitStructure);

    /* GPIOD configuration */
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FMC);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1  | GPIO_Pin_8 |
                                  GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 |
                                  GPIO_Pin_15;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    /* GPIOE configuration */
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource0 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource1 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource7 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource8 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource9 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource10 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource11 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource12 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource13 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource14 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource15 , GPIO_AF_FMC);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0  | GPIO_Pin_1  | 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;

    GPIO_Init(GPIOE, &GPIO_InitStructure);

    /* GPIOF configuration */
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource0 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource1 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource2 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource3 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource4 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource5 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource11 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource12 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource13 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource14 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource15 , GPIO_AF_FMC);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0  | GPIO_Pin_1 | GPIO_Pin_2 |
                                  GPIO_Pin_3  | GPIO_Pin_4 | GPIO_Pin_5 |
                                  GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 |
                                  GPIO_Pin_14 | GPIO_Pin_15;

    GPIO_Init(GPIOF, &GPIO_InitStructure);

    /* GPIOG configuration */
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource0 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource1 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource4 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource5 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource8 , GPIO_AF_FMC);
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource15 , GPIO_AF_FMC);


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 |
                                  GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_15;

    GPIO_Init(GPIOG, &GPIO_InitStructure);
}

void SDRAM_Init(void)
{
    FMC_SDRAMInitTypeDef  FMC_SDRAMInitStructure;
    FMC_SDRAMTimingInitTypeDef  FMC_SDRAMTimingInitStructure;

    /* GPIO configuration for FMC SDRAM bank */
    SDRAM_GPIOConfig();

    /* Enable FMC clock */
    RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FMC, ENABLE);

    /* FMC Configuration ---------------------------------------------------------*/
    /* FMC SDRAM Bank configuration */
    /* Timing configuration for 90 Mhz of SD clock frequency (180Mhz/2) */
    /* TMRD: 2 Clock cycles */
    FMC_SDRAMTimingInitStructure.FMC_LoadToActiveDelay    = 2;
    /* TXSR: min=70ns (7x11.11ns) */
    FMC_SDRAMTimingInitStructure.FMC_ExitSelfRefreshDelay = 7;
    /* TRAS: min=42ns (4x11.11ns) max=120k (ns) */
    FMC_SDRAMTimingInitStructure.FMC_SelfRefreshTime      = 4;
    /* TRC:  min=70 (7x11.11ns) */
    FMC_SDRAMTimingInitStructure.FMC_RowCycleDelay        = 7;
    /* TWR:  min=1+ 7ns (1+1x11.11ns) */
    FMC_SDRAMTimingInitStructure.FMC_WriteRecoveryTime    = 2;
    /* TRP:  20ns => 2x11.11ns */
    FMC_SDRAMTimingInitStructure.FMC_RPDelay              = 2;
    /* TRCD: 20ns => 2x11.11ns */
    FMC_SDRAMTimingInitStructure.FMC_RCDDelay             = 2;

    /* FMC SDRAM control configuration */
    FMC_SDRAMInitStructure.FMC_Bank = FMC_Bank2_SDRAM;
    /* Row addressing: [7:0] */
    FMC_SDRAMInitStructure.FMC_ColumnBitsNumber = FMC_ColumnBits_Number_8b;
    /* Column addressing: [11:0] */
    FMC_SDRAMInitStructure.FMC_RowBitsNumber = FMC_RowBits_Number_12b;
    FMC_SDRAMInitStructure.FMC_SDMemoryDataWidth = FMC_SDMemory_Width_16b;
    FMC_SDRAMInitStructure.FMC_InternalBankNumber = FMC_InternalBank_Number_4;
    FMC_SDRAMInitStructure.FMC_CASLatency = FMC_CAS_Latency_3;
    FMC_SDRAMInitStructure.FMC_WriteProtection = FMC_Write_Protection_Disable;
    FMC_SDRAMInitStructure.FMC_SDClockPeriod = FMC_SDClock_Period_2;
    FMC_SDRAMInitStructure.FMC_ReadBurst = FMC_Read_Burst_Disable;
    FMC_SDRAMInitStructure.FMC_ReadPipeDelay = FMC_ReadPipe_Delay_1;
    FMC_SDRAMInitStructure.FMC_SDRAMTimingStruct = &FMC_SDRAMTimingInitStructure;

    /* FMC SDRAM bank initialization */
    FMC_SDRAMInit(&FMC_SDRAMInitStructure);

    /* FMC SDRAM device initialization sequence */
    SDRAM_InitSequence();

}

static void LCD_Config(void)
{
    LTDC_InitTypeDef               LTDC_InitStruct;
    LTDC_Layer_InitTypeDef         LTDC_Layer_InitStruct;
    GPIO_InitTypeDef               GPIO_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

    /* Configure NCS in Output Push-Pull mode */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    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(GPIOD, &GPIO_InitStructure);

    /* Enable the LTDC Clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_LTDC, ENABLE);

    /* Enable the DMA2D Clock */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2D, ENABLE);

    /* Configure the LCD Control pins */
    LCD_AF_GPIOConfig();

    /* Configure the FMC Parallel interface : SDRAM is used as Frame Buffer for
       LCD */
    SDRAM_Init();

    /* 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(96, 7, 2);
    RCC_LTDCCLKDivConfig(RCC_PLLSAIDivR_Div2);

    /* 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 = 1;
    /* Configure vertical synchronization height */
    LTDC_InitStruct.LTDC_VerticalSync = 1;
    /* Configure accumulated horizontal back porch */
    LTDC_InitStruct.LTDC_AccumulatedHBP = 40;
    /* Configure accumulated vertical back porch */
    LTDC_InitStruct.LTDC_AccumulatedVBP = 23;
    /* Configure accumulated active width */
    LTDC_InitStruct.LTDC_AccumulatedActiveW = 840;
    /* Configure accumulated active height */
    LTDC_InitStruct.LTDC_AccumulatedActiveH = 503;
    /* Configure total width */
    LTDC_InitStruct.LTDC_TotalWidth = 880;
    /* Configure total height */
    LTDC_InitStruct.LTDC_TotalHeigh = 516;

    LTDC_Init(&LTDC_InitStruct);

    /* Configure R,G,B component values for LCD background color */
    LTDC_InitStruct.LTDC_BackgroundRedValue = 0;
    LTDC_InitStruct.LTDC_BackgroundGreenValue = 0;
    LTDC_InitStruct.LTDC_BackgroundBlueValue = 0;

    LTDC_Init(&LTDC_InitStruct);

    /* LTDC initialization end -------------------------------------------------*/


    /* Layer1 Configuration ----------------------------------------------------*/

    /* Windowing configuration */
    /* In this case all the active display area is used to display a picture then:
    Horizontal start = horizontal synchronization + Horizontal back porch = 30
    Horizontal stop = Horizontal start + window width -1 = 30 + 240 -1
    Vertical start   = vertical synchronization + vertical back porch     = 4
    Vertical stop   = Vertical start + window height -1  = 4 + 160 -1      */
    LTDC_Layer_InitStruct.LTDC_HorizontalStart = 41;
    LTDC_Layer_InitStruct.LTDC_HorizontalStop = (800 + 40);
    LTDC_Layer_InitStruct.LTDC_VerticalStart = 24;
    LTDC_Layer_InitStruct.LTDC_VerticalStop = 480 + 23;

    /* Pixel Format configuration*/
    LTDC_Layer_InitStruct.LTDC_PixelFormat = LTDC_Pixelformat_RGB565;

    /* Alpha constant (255 totally opaque) */
    LTDC_Layer_InitStruct.LTDC_ConstantAlpha = 255;

    /* Configure blending factors */
    LTDC_Layer_InitStruct.LTDC_BlendingFactor_1 = LTDC_BlendingFactor1_PAxCA;
    LTDC_Layer_InitStruct.LTDC_BlendingFactor_2 = LTDC_BlendingFactor2_PAxCA;

    /* Default Color configuration (configure A,R,G,B component values) */
    LTDC_Layer_InitStruct.LTDC_DefaultColorBlue = 0;
    LTDC_Layer_InitStruct.LTDC_DefaultColorGreen = 0;
    LTDC_Layer_InitStruct.LTDC_DefaultColorRed = 0;
    LTDC_Layer_InitStruct.LTDC_DefaultColorAlpha = 0;

    /* Input Address configuration */
    LTDC_Layer_InitStruct.LTDC_CFBStartAdress = frameBuf0;

    /* the length of one line of pixels in bytes + 3 then :
    Line Lenth = Active high width x number of bytes per pixel + 3
    Active high width         = 240
    number of bytes per pixel = 2    (pixel_format : RGB565)
    */
    LTDC_Layer_InitStruct.LTDC_CFBLineLength = ((800 * 2) + 3);

    /*  the pitch is the increment from the start of one line of pixels to the
    start of the next line in bytes, then :
    Pitch = Active high width x number of bytes per pixel
    */
    LTDC_Layer_InitStruct.LTDC_CFBPitch = (800 * 2);

    /* configure the number of lines */
    LTDC_Layer_InitStruct.LTDC_CFBLineNumber = 480;

    LTDC_LayerInit(LTDC_Layer1, &LTDC_Layer_InitStruct);

    /* Layer1 Configuration end ------------------------------------------------*/

    LTDC_DitherCmd(ENABLE);
}

void cLCD_Clear(uint16_t Color)
{
    uint32_t index = 0;
    for (index = 0x00; index < 0x400000; index++)
    {
        *(volatile uint16_t *)(0xd0000000 + (2 * index)) = 0x0000;
    }
    for (index = 0x00; index < 0x400000; index++)
    {
        *(volatile uint16_t *)(0xd0000000 + (2 * index)) = 0x03f0;
    }

    for (index = 0x00; index < 480 * 800; index++)
    {
        *(volatile uint16_t *)(0xd0000000 + (2 * index)) = (uint16_t)Color;
    }
}


int main(void)
{
    /* Configure LCD */
    LCD_Config();

    /* Enable Layer 1 */
    LTDC_LayerCmd(LTDC_Layer1, ENABLE);

    /* Reload LTDC configuration  */
    LTDC_ReloadConfig(LTDC_IMReload);

    /* Enable The LCD */
    LTDC_Cmd(ENABLE);

    cLCD_Clear(0x00f8);
    //LCD_Clear(0xf800);

    for(;;);
}

=======================================================================
=======================================================================
=======================================================================

#define  LCD_PIXEL_WIDTH    ((uint16_t)800)
#define  LCD_PIXEL_HEIGHT   ((uint16_t)480)

#define  HSW            ((uint16_t)48)     /* Horizontal synchronization */
#define  HBP            ((uint16_t)40)      /* Horizontal back porch      */
#define  HFP            ((uint16_t)40)      /* Horizontal front porch     */
#define  VSW            ((uint16_t)3)     /* Vertical synchronization   */
#define  VBP            ((uint16_t)29)      /* Vertical back porch        */
#define  VFP            ((uint16_t)13)      /* Vertical front porch       */

#define  HSTART         (HBP)
#define  HSTOP          (LCD_PIXEL_WIDTH+HSTART)
#define  VSTART         (VBP)
#define  VSTOP          (LCD_PIXEL_HEIGHT+VSTART)


#define LCD_FRAME_BUFFER       ((uint32_t)0xD0000000)  //LCD·SDRAM
#define BUFFER_OFFSET          ((uint32_t)0x60000)


void LCD_Init(void){
RCC_PLLSAIConfig(192, 7, 4);
  RCC_LTDCCLKDivConfig(RCC_PLLSAIDivR_Div2);//RCC_PLLSAIDivR_Div8
 
  /* Enable PLLSAI Clock */
  RCC_PLLSAICmd(ENABLE);
  /* Wait for PLLSAI activation */
  while(RCC_GetFlagStatus(RCC_FLAG_PLLSAIRDY) == RESET)
  {
  }
 
  /* Timing configuration */
  /* Configure horizontal synchronization width */  
  LTDC_InitStruct.LTDC_HorizontalSync = HSW-1;
  /* Configure vertical synchronization height */
  LTDC_InitStruct.LTDC_VerticalSync = VSW-1;
  /* Configure accumulated horizontal back porch */
  LTDC_InitStruct.LTDC_AccumulatedHBP = HBP-1;
  /* Configure accumulated vertical back porch */
  LTDC_InitStruct.LTDC_AccumulatedVBP = VBP-1;
  /* Configure accumulated active width */
  LTDC_InitStruct.LTDC_AccumulatedActiveW = LCD_PIXEL_WIDTH + HBP-1;
  /* Configure accumulated active height */
  LTDC_InitStruct.LTDC_AccumulatedActiveH = LCD_PIXEL_HEIGHT + VBP-1;
  /* Configure total width */
  LTDC_InitStruct.LTDC_TotalWidth = LCD_PIXEL_WIDTH + HBP + HFP-1;
  /* Configure total height */
  LTDC_InitStruct.LTDC_TotalHeigh = LCD_PIXEL_HEIGHT + VBP + VFP-1;
 
  LTDC_Init(&LTDC_InitStruct);


static void _LCD_InitController(int LayerIndex) 函数中:
  xSize = LCD_GetXSizeEx(LayerIndex);
  ySize = LCD_GetYSizeEx(LayerIndex);

  LTDC_Layer_InitStruct.LTDC_HorizontalStart = HSTART;
  LTDC_Layer_InitStruct.LTDC_HorizontalStop  = (xSize + HSTART-1);
  LTDC_Layer_InitStruct.LTDC_VerticalStart   = VSTART;
  LTDC_Layer_InitStruct.LTDC_VerticalStop    = (ySize + VSTART-1);
}


void LCD_Init(void){
{
  LTDC_InitTypeDef LTDC_InitStruct;
  LTDC_Layer_InitTypeDef LTDC_Layer_InitStruct;
  LTDC_Layer_TypeDef LTDC_Layerx;
 

  / * НЛ ініціалізації порту * /
  LCD_GPIOInit ();

  LCD_DisplayOff ();
  / * Включити LCD годинник * /
  RCC_APB2PeriphClockCmd (RCC_APB2Periph_LTDC, то ENABLE);
  / * Включити DMA відсутня * /
  RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_DMA2D, то ENABLE);

  / * --- активний низький сигнал синхронізації по горизонталі * /
  LTDC_InitStruct.LTDC_HSPolarity = LTDC_HSPolarity_AL;    
  / * --- активний низький рівень сигналу вертикальної синхронізації сигналу * /
  LTDC_InitStruct.LTDC_VSPolarity = LTDC_VSPolarity_AL;    
  / * дані дозволяють активний низький сигнал --- * /
  LTDC_InitStruct.LTDC_DEPolarity = LTDC_DEPolarity_AL;    
  / * * конфігурація пікселя годинник --- /
  //LTDC_InitStruct.LTDC_PCPolarity = LTDC_DEPolarity_AL;
LTDC_InitStruct.LTDC_PCPolarity = LTDC_PCPolarity_IPC;

/ * встановити підсвічування ЖК - дисплея * /
  LTDC_InitStruct.LTDC_BackgroundRedValue = 0;          
  LTDC_InitStruct.LTDC_BackgroundGreenValue = 0;        
  LTDC_InitStruct.LTDC_BackgroundBlueValue = 0;  
  / *
   **************************** ****************************
   * PLLSAI_VCO PLLSAI_N ВШЕ * = /  LL_M. 8 * = 192 / 192MHz. 8 =
   * = PLLLCDCLK  LLSAI_VCO /  LLSAI_R = 192/3 = 64 Мгц
   * LTDC тактова частота =  LLLCDCLK / 64-RCC_PLLSAIDivR = / 2 = 32 Мгц
   ******** ************************************************** ******************
   * /
  RCC_PLLSAIConfig (192,. 7, 3);
  RCC_LTDCCLKDivConfig (RCC_PLLSAIDivR_Div4);
  / * включити PLLSAI годинник * /
  RCC_PLLSAICmd (Розблокування);
  / * очікування PLLSAI годинник * /
  цей час (RCC_GetFlagStatus (RCC_FLAG_PLLSAIRDY) == скидання) {}



  / * ширина * Налаштувати Горизонтальна синхронізація /    
  LTDC_InitStruct.LTDC_HorizontalSync = 39;
  / * Вертикальне Число висоти Налаштування синхронізації * /
  LTDC_InitStruct.LTDC_VerticalSync = 19;
  / * Налаштувати Накопичена Горизонтальні Назад * Веранда /
  LTDC_InitStruct.LTDC_AccumulatedHBP = 45;
  / * * Налаштувати Накопичена Вертикальна Кількість заднього псування /
  LTDC_InitStruct.LTDC_AccumulatedVBP = 22;
  / * ширина * Налаштувати Накопичена Active /
  LTDC_InitStruct.LTDC_AccumulatedActiveW = 845;
  / * * Налаштувати Накопичена Активна висота /
  LTDC_InitStruct.LTDC_AccumulatedActiveH = 502;
  / * * Налаштувати Загальна ширина /
  LTDC_InitStruct.LTDC_TotalWidth = тисячі п'ятьдесят-п'ять;
  / * * Налаштувати Загальна висота /
  LTDC_InitStruct.LTDC_TotalHeigh = 524;

  LTDC_Init (<DC_InitStruct);

  LTDC_ITConfig (LTDC_IER_LIE, то ENABLE);  
NVIC_SetPriority (LTDC_IRQn, 0);
  NVIC_EnableIRQ ( LTDC_IRQn);

   DMA2D_ITConfig (DMA2D_CR_TCIE, то ENABLE);
   NVIC_SetPriority (DMA2D_IRQn, 0);
   NVIC_EnableIRQ (DMA2D_IRQn);
   DMA2D-> IFCR = (U32) DMA2D_IFSR_CTCIF;
 
 
 

 LTDC_Layer_InitStruct.LTDC_HorizontalStart = 46; // 46
// LTDC_Layer_InitStruct.LTDC_HorizontalStart = 70;
 / / LTDC_Layer_InitStruct.LTDC_HorizontalStop = (480 + 43 - 1);
  LTDC_Layer_InitStruct.LTDC_HorizontalStop = (800 + 46 - 1);

  LTDC_Layer_InitStruct.LTDC_VarticalStart = 23; // 23
 // LTDC_Layer_InitStruct.LTDC_VerticalStop = (272 + 12 - 1);
  LTDC_Layer_InitStruct. = LTDC_VerticalStop (480 + 23 - 1);
  / *  ixel Формат конфігурації * /          
  LTDC_Layer_InitStruct.LTDC_PixelFormat = LTDC_Pixelformat_RGB565;
  / * Альфа Константа (255 Повністю непрозорий) * /
  LTDC_Layer_InitStruct.LTDC_ConstantAlpha = 255;
  / * за замовчуванням колір конфігурації (Налаштувати А, R & л , то G, B компонент значення) * /        
  LTDC_Layer_InitStruct.LTDC_DefaultColorBlue = 0;      
  LTDC_Layer_InitStruct.LTDC_DefaultColorGreen = 0;      
  LTDC_Layer_InitStruct.LTDC_DefaultColorRed = 0;        
  LTDC_Layer_InitStruct.LTDC_DefaultColorAlpha = 0;
  / * * Налаштувати Viewpoint купажу Фактори /      
  LTDC_Layer_InitStruct.LTDC_BlendingFactor_1 = LTDC_BlendingFactor1_CA;  
  LTDC_Layer_InitStruct.LTDC_BlendingFactor_2 = LTDC_BlendingFactor2_CA;
  / * при довжині одного рядка пікселів в байтах + 3 в той час :
     лінія Lenth = Активна висока ширина х кількість байт на піксель + 3
     Активна висока ширина = LCD_PIXEL_WIDTH
     кількість байтів на піксель = 2 (pixel_format: RGB565)
  * /
 LTDC_Layer_InitStruct.LTDC_CFBLineLength = // ((480 * 2) + 3);
 LTDC_Layer_InitStruct.LTDC_CFBLineLength = ((800 * 2) + 3);
  / * в непроглядній на інкремент з на початку одного рядка пікселів до в The
      Початок о даний рядок в байтах Далі, потім:
      ITCH висока ширина = Активний X Кількість байт на піксель    
  * /
 // LTDC_Layer_InitStruct.LTDC_CFBPitch = (480 * 2);
LTDC_Layer_InitStruct.LTDC_CFBPitch = (800 * 2);
  / * * Налаштувати на число рядків /
 // LTDC_Layer_InitStruct.LTDC_CFBLineNumber = 272;
LTDC_Layer_InitStruct.LTDC_CFBLineNumber = 480;
  / * * способу введення адресної конфігурації /  
  LTDC_Layer_InitStruct.LTDC_CFBStartAdress = LCD_FRAME_BUFFER;
 
  LTDC_LayerInit (LTDC_Layer1, <DC_Layer_InitStruct);

  / * * Налаштувати Layer2 /
// LTDC_Layer_InitStruct.LTDC_CFBStartAdress = LCD_FRAME_BUFFER + BUFFER_OFFSET;
 // LTDC_Layer_InitStruct.LTDC_BlendingFactor_1 = LTDC_BlendingFactor1_PAxCA;  
 // LTDC_Layer_InitStruct.LTDC_BlendingFactor_2 = LTDC_BlendingFactor2_PAxCA;
 // LTDC_LayerInit (LTDC_Layer2, <DC_Layer_InitStruct);

 // LTDC_ReloadConfig (LTDC_IMReload);
 
  / * * Включити передній план і фоновий режим Шари /
  LTDC_LayerCmd (LTDC_Layer1, то ENABLE) ;
 // LTDC_LayerCmd (LTDC_Layer2, то ENABLE);
  LTDC_ReloadConfig (LTDC_IMReload);

// LCD_DisplayOn ();
}

Комментариев нет:

Отправить комментарий