void ltdc_init()
{
// enable ltdc clock
__HAL_RCC_LTDC_CLK_ENABLE();
// __HAL_RCC_DSI_CLK_ENABLE();
__HAL_RCC_WWDG1_CLK_ENABLE();
const int h_sync = 4;
const int h_back_porch = 43;
const int h_active = 320;
const int h_front_porch = 8;
const int v_sync = 4;
const int v_back_porch = 12;
const int v_active = 240;
const int v_front_porch = 2;
// configure the panel timings and signal polarity
LTDC->GCR &= ~LTDC_GCR_PCPOL; // synch signal polarity setting
// hsync and vsync
LTDC->SSCR = ((h_sync - 1) << 16) | (v_sync - 1);
// accumulated horizonal and vertical back porch
LTDC->BPCR = ((h_sync + h_back_porch - 1) << 16) | (v_sync + v_back_porch - 1);
// accumulated active width and height
LTDC->AWCR = ((h_sync + h_back_porch + h_active - 1) << 16) | (v_sync + v_back_porch + v_active - 1);
// accumulated total width and height
LTDC->TWCR = ((h_sync + h_back_porch + h_active + h_front_porch - 1) << 16) | (v_sync + v_back_porch + v_active + v_front_porch - 1);
// enable ltdc transfer and fifo underrun error interrupts
// LTDC->IER = LTDC_IT_TE | LTDC_IT_FU;
// configure ltdc layer
LTDC_Layer1->WHPCR &= ~(LTDC_LxWHPCR_WHSTPOS | LTDC_LxWHPCR_WHSPPOS);
LTDC_Layer1->WHPCR = ((0 + ((LTDC->BPCR & LTDC_BPCR_AHBP) >> 16U) + 1U) | ((320 + ((LTDC->BPCR & LTDC_BPCR_AHBP) >> 16U)) << 16U));
LTDC_Layer1->WVPCR &= ~(LTDC_LxWVPCR_WVSTPOS | LTDC_LxWVPCR_WVSPPOS);
LTDC_Layer1->WVPCR = ((0 + (LTDC->BPCR & LTDC_BPCR_AVBP) + 1U) | ((240 + (LTDC->BPCR & LTDC_BPCR_AVBP)) << 16U));
LTDC_Layer1->PFCR = LTDC_PIXEL_FORMAT_RGB565;
LTDC_Layer1->DCCR = 0x000000FF; // layer default color (back, 100% alpha)
LTDC_Layer1->CFBAR = (uint32_t)&__ltdc_start; // frame buffer start address
LTDC_Layer1->CFBLR = ((320 * 2) << LTDC_LxCFBLR_CFBP_Pos) | (((320 * 2) + 7) << LTDC_LxCFBLR_CFBLL_Pos); // frame buffer line length and pitch
LTDC_Layer1->CFBLNR = 240; // line count
LTDC_Layer1->CACR = 255; // alpha
LTDC_Layer1->CR |= LTDC_LxCR_LEN; // enable layer
// reload shadow registers
LTDC->SRCR = 0;//LTDC_SRCR_IMR;
// enable LTDC
LTDC->GCR |= LTDC_GCR_LTDCEN;
LTDC->BCCR = 0x000000FF;