你用的是什么芯片和那颗LCD驱动IC,我写过一些代码供你参考,希望能对你有些帮助.
static void LCD_LL_Init(void)
{
DSI_PLLInitTypeDef dsiPllInit;
static RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
uint32_t LcdClock = 27429; /*!< LcdClk = 27429 kHz */
uint32_t Clockratio = 0;
uint32_t lcd_x_size = 0;
uint32_t lcd_y_size = 0;
uint32_t laneByteClk_kHz = 0;
uint32_t VSA;
uint32_t VBP;
uint32_t VFP;
uint32_t VACT;
uint32_t HSA;
uint32_t HBP;
uint32_t HFP;
uint32_t HACT;
/* Toggle Hardware Reset of the DSI LCD using
* its XRES signal (active low) */
LCD_LL_Reset();
/** @brief Enable the LTDC clock */
__HAL_RCC_LTDC_CLK_ENABLE();
/** @brief Toggle Sw reset of LTDC IP */
__HAL_RCC_LTDC_FORCE_RESET();
__HAL_RCC_LTDC_RELEASE_RESET();
/** @brief Enable the DMA2D clock */
__HAL_RCC_DMA2D_CLK_ENABLE();
/** @brief Toggle Sw reset of DMA2D IP */
__HAL_RCC_DMA2D_FORCE_RESET();
__HAL_RCC_DMA2D_RELEASE_RESET();
/** @brief Enable DSI Host and wrapper clocks */
__HAL_RCC_DSI_CLK_ENABLE();
/** @brief Soft Reset the DSI Host and wrapper */
__HAL_RCC_DSI_FORCE_RESET();
__HAL_RCC_DSI_RELEASE_RESET();
/** @brief NVIC configuration for LTDC interrupt that is now enabled */
HAL_NVIC_SetPriority(LTDC_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(LTDC_IRQn);
/** @brief NVIC configuration for DSI interrupt that is now enabled */
HAL_NVIC_SetPriority(DSI_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(DSI_IRQn);
/* Base address of DSI Host/Wrapper registers to be set before calling De-Init */
hdsi_eval.Instance = DSI;
HAL_DSI_DeInit(&(hdsi_eval));
dsiPllInit.PLLNDIV = 100;
dsiPllInit.PLLIDF = DSI_PLL_IN_DIV5;
dsiPllInit.PLLODF = DSI_PLL_OUT_DIV1;
laneByteClk_kHz = 62500; /* 500 MHz / 8 = 62.5 MHz = 62500 kHz */
/* Set number of Lanes */
hdsi_eval.Init.NumberOfLanes = DSI_TWO_DATA_LANES;
/* TXEscapeCkdiv = f(LaneByteClk)/15.62 = 4 */
hdsi_eval.Init.TXEscapeCkdiv = laneByteClk_kHz/15620;
HAL_DSI_Init(&(hdsi_eval), &(dsiPllInit));
Clockratio = laneByteClk_kHz/LcdClock;
/* Timing parameters for all Video modes
* Set Timing parameters of LTDC depending on its chosen orientation
*/
/* lcd_orientation == LCD_ORIENTATION_LANDSCAPE */
VSA = OTM8009A_800X480_VSYNC; /* 12 */
VBP = OTM8009A_800X480_VBP; /* 12 */
VFP = OTM8009A_800X480_VFP; /* 12 */
HSA = OTM8009A_800X480_HSYNC; /* 120 */
HBP = OTM8009A_800X480_HBP; /* 120 */
HFP = OTM8009A_800X480_HFP; /* 120 */
lcd_x_size = OTM8009A_800X480_WIDTH; /* 800 */
lcd_y_size = OTM8009A_800X480_HEIGHT; /* 480 */
HACT = lcd_x_size;
VACT = lcd_y_size;
hdsivideo_handle.VirtualChannelID = 0;
hdsivideo_handle.ColorCoding = LCD_DSI_PIXEL_DATA_FMT_RBG565;
hdsivideo_handle.VSPolarity = DSI_VSYNC_ACTIVE_HIGH;
hdsivideo_handle.HSPolarity = DSI_HSYNC_ACTIVE_HIGH;
hdsivideo_handle.DEPolarity = DSI_DATA_ENABLE_ACTIVE_HIGH;
hdsivideo_handle.Mode = DSI_VID_MODE_BURST; /* Mode Video burst ie : one LgP per line */
hdsivideo_handle.NullPacketSize = 0xFFF;
hdsivideo_handle.NumberOfChunks = 0;
hdsivideo_handle.PacketSize = HACT; /* Value depending on display orientation choice portrait/landscape */
hdsivideo_handle.HorizontalSyncActive = HSA*Clockratio;
hdsivideo_handle.HorizontalBackPorch = HBP*Clockratio;
hdsivideo_handle.HorizontalLine = (HACT + HSA + HBP + HFP)*Clockratio; /* Value depending on display orientation choice portrait/landscape */
hdsivideo_handle.VerticalSyncActive = VSA;
hdsivideo_handle.VerticalBackPorch = VBP;
hdsivideo_handle.VerticalFrontPorch = VFP;
hdsivideo_handle.VerticalActive = VACT; /* Value depending on display orientation choice portrait/landscape */
/* Enable or disable sending LP command while streaming is active in video mode */
hdsivideo_handle.LPCommandEnable = DSI_LP_COMMAND_ENABLE; /* Enable sending commands in mode LP (Low Power) */
/* Largest packet size possible to transmit in LP mode in VSA, VBP, VFP regions */
/* Only useful when sending LP packets is allowed while streaming is active in video mode */
hdsivideo_handle.LPLargestPacketSize = 64;
/* Largest packet size possible to transmit in LP mode in HFP region during VACT period */
/* Only useful when sending LP packets is allowed while streaming is active in video mode */
hdsivideo_handle.LPVACTLargestPacketSize = 64;
/* Specify for each region of the video frame, if the transmission of command in LP mode is allowed in this region */
/* while streaming is active in video mode */
hdsivideo_handle.LPHorizontalFrontPorchEnable = DSI_LP_HFP_ENABLE; /* Allow sending LP commands during HFP period */
hdsivideo_handle.LPHorizontalBackPorchEnable = DSI_LP_HBP_ENABLE; /* Allow sending LP commands during HBP period */
hdsivideo_handle.LPVerticalActiveEnable = DSI_LP_VACT_ENABLE; /* Allow sending LP commands during VACT period */
hdsivideo_handle.LPVerticalFrontPorchEnable = DSI_LP_VFP_ENABLE; /* Allow sending LP commands during VFP period */
hdsivideo_handle.LPVerticalBackPorchEnable = DSI_LP_VBP_ENABLE; /* Allow sending LP commands during VBP period */
hdsivideo_handle.LPVerticalSyncActiveEnable = DSI_LP_VSYNC_ENABLE; /* Allow sending LP commands during VSync = VSA period */
/* Configure DSI Video mode timings with settings set above */
HAL_DSI_ConfigVideoMode(&(hdsi_eval), &(hdsivideo_handle));
/* Enable the DSI host and wrapper : but LTDC is not started yet at this stage */
HAL_DSI_Start(&(hdsi_eval));
/* Timing Configuration */
hltdc_eval.Init.HorizontalSync = (HSA - 1);
hltdc_eval.Init.AccumulatedHBP = (HSA + HBP - 1);
hltdc_eval.Init.AccumulatedActiveW = (lcd_x_size + HSA + HBP - 1);
hltdc_eval.Init.TotalWidth = (lcd_x_size + HSA + HBP + HFP - 1);
/* Initialize the LCD pixel width and pixel height */
hltdc_eval.LayerCfg->ImageWidth = lcd_x_size;
hltdc_eval.LayerCfg->ImageHeight = lcd_y_size;
/* LCD clock configuration */
/* PLLSAI_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */
/* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN = 384 Mhz */
/* PLLLCDCLK = PLLSAI_VCO Output/PLLSAIR = 384 MHz / 7 = 54.857 MHz */
/* LTDC clock frequency = PLLLCDCLK / LTDC_PLLSAI_DIVR_2 = 54.857 MHz / 2 = 27.429 MHz */
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LTDC;
PeriphClkInitStruct.PLLSAI.PLLSAIN = 384;
PeriphClkInitStruct.PLLSAI.PLLSAIR = 7;
PeriphClkInitStruct.PLLSAIDivR = RCC_PLLSAIDIVR_2;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
/* Background value */
hltdc_eval.Init.Backcolor.Blue = 0;
hltdc_eval.Init.Backcolor.Green = 0;
hltdc_eval.Init.Backcolor.Red = 0;
hltdc_eval.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
hltdc_eval.Instance = LTDC;
/* Get LTDC Configuration from DSI Configuration */
HAL_LTDC_StructInitFromVideoConfig(&(hltdc_eval), &(hdsivideo_handle));
/* Initialize the LTDC */
HAL_LTDC_Init(&hltdc_eval);
OTM8009A_Init(hdsivideo_handle.ColorCoding, OTM8009A_ORIENTATION_LANDSCAPE );
HAL_LTDC_ProgramLineEvent(&hltdc_eval, 0);
}
void DSI_IO_WriteCmd(uint32_t NbrParams, uint8_t *pParams)
{
if(NbrParams <= 1)
{
HAL_DSI_ShortWrite(&hdsi_eval, 0, DSI_DCS_SHORT_PKT_WRITE_P1, pParams[0], pParams[1]);
}
else
{
HAL_DSI_LongWrite(&hdsi_eval, 0, DSI_DCS_LONG_PKT_WRITE, NbrParams, pParams[NbrParams], pParams);
}
} |