The screen is the big goal of this board, and it is less scary in stages. First light the backlight. Then give the LTDC its pixel clock and timings. Then point one layer at a frame buffer in RAM. Then write pixels into that buffer and watch them appear. Then let the DMA2D fill rectangles for you. Every stage works on its own, and every number points at a page.
- What the LTDC does: it scans a framebuffer in memory out to the panel by itself, with no CPU work
- How a panel is described to the LTDC: pixel clock, sync pulses and porches (display timing)
- How one layer, a pixel format and a pitch describe a frame buffer, and why the shadow registers need a reload
- Where to put the frame buffer (AXI SRAM first, SDRAM later) and why that choice matters
- How to draw a pixel, a rectangle and a DMA2D fill (DMA2D)
- STM32H750B-DK and a USB cable to CN14 (the ST-LINK connector)
- STM32CubeIDE with the STM32CubeH7 firmware package (for the HAL; ST’s BSP and examples are optional)
- The clocks guide finished: this guide assumes the 25 MHz HSE and PLL1 from there
On this board
Pulled from the datasheet tables and the board schematic. Click a pin for everything the finder knows about it.
| Pin | Ball | Board net | Used here as | AF | Open |
|---|---|---|---|---|---|
| PK0 | J14 | LCD_BL | LCD_BL: backlight enable (high = on, as in ST’s BSP); the same pin can also be PWM from TIM8_CH3 for dimming | schematicdatasheet | |
| PD7 | C11 | LCD_DISP | LCD_DISP: display enable (high = on). A plain output, not an LTDC signal | schematicdatasheet | |
| PI14 | H3 | LCD_CLK | LCD_CLK: the pixel clock the LTDC sends to the panel | AF14 LCD_CLK | schematicdatasheet |
| PI12 | H1 | LCD_HSYNC | LCD_HSYNC: horizontal sync pulse | AF14 LCD_HSYNC | schematicdatasheet |
| PI9 | E2 | LCD_VSYNC | LCD_VSYNC: vertical sync pulse | AF14 LCD_VSYNC | schematicdatasheet |
| PK7 | D7 | LCD_DE | LCD_DE: data enable (high while a visible pixel is sent) | AF14 LCD_DE | schematicdatasheet |
| PJ12 | D11 | LCD_B0 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ13 | E10 | LCD_B1 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ14 | D10 | LCD_B2 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ15 | B10 | LCD_B3 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PK3 | C8 | LCD_B4 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PK4 | B8 | LCD_B5 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PK5 | A8 | LCD_B6 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PK6 | C7 | LCD_B7 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ7 | N14 | LCD_G0 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ8 | N13 | LCD_G1 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ9 | M14 | LCD_G2 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ10 | L14 | LCD_G3 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ11 | K14 | LCD_G4 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PI0 | A16 | LCD_G5 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PI1 | A15 | LCD_G6 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PK2 | H17 | LCD_G7 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PG2 | H16 | LCD_INT | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PI15 | P5 | LCD_R0 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ0 | N6 | LCD_R1 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ1 | P6 | LCD_R2 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PH9 | R13 | LCD_R3 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ3 | U6 | LCD_R4 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ4 | U7 | LCD_R5 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ5 | R12 | LCD_R6 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PJ6 | N15 | LCD_R7 | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet | |
| PB12 | T14 | LCD_RST | LCD connector signal (24 RGB data lines, control, touch interrupt and reset) | schematicdatasheet |
The big picture and the plan
The board has a 4.3" colour screen with a resolution of 480 × 272 pixels. The schematic names the module RK043FN48H-CT672B. It is an RGB panel: it has 24 data wires (8 each for red, green and blue), a pixel clock, two sync signals and a data-enable signal, plus a backlight, a display-enable pin and a touch controller.
You do not wiggle those pins yourself. The LTDC peripheral reads a picture from memory, the framebuffer, over and over, about 60 times a second, and produces all the signals. The CPU is free. To change what is on the screen you change numbers in RAM.
So the job splits into stages, and you can stop after any of them:
- Backlight and display enable. Two output pins. The screen lights up.
- Pins, pixel clock and timings. Tell the LTDC how the panel wants its signals.
- One layer. Point the LTDC at a frame buffer in RAM: 480 × 272 pixels in RGB565 is 480 × 272 × 2 = 261,120 bytes, which fits in the 512 KB AXI SRAM at
0x24000000. - Draw pixels. Writing a 16-bit value into the array changes a pixel.
- DMA2D fill. Let a graphics DMA fill a rectangle while the CPU does something else.
- Next steps: frame buffer in SDRAM, double buffering, and graphics libraries.
If you would rather read working code first, ST's STM32CubeH7 package has two examples for exactly this board: Examples/LTDC/LTDC_Display_1Layer (a picture shown from flash through one layer) and Applications/Display/LTDC_Paint. This guide builds the same thing in small pieces so you know what each line does. They are ST's code and not part of the PDFs on this site.
Stage 1: light the backlight and enable the display
Two ordinary output pins first. They need no LTDC at all, so this is a safe way to test the board and your toolchain.
- **PK0,
LCD_BL.** It goes to the enable pin of a small LED driver chip (U13) that powers the backlight. High turns it on. - **PD7,
LCD_DISP.** It goes to the DISP pin of the panel, which enables the display. High turns it on.
The polarity (high = on) is the way ST's BSP drives them; the schematic shows the wiring, not the logic level. Do exactly what you did for the LED: clock the ports (RCC_AHB4ENR), set the mode (GPIOx_MODER), write GPIOx_BSRR.
When this runs, the backlight comes on and the glass glows. What the panel shows without any signal from the LTDC depends on the panel (often plain white), so do not expect a picture yet. A glowing screen means the wiring and the pin choice are right.
A warning about ST's own code: the BSP header names a macro LCD_DISP_EN_PIN and puts it on port K pin 7. The schematic, the user manual (Table 10) and the BSP's own LTDC initialisation all put LCD_DISP on PD7. PK7 is LCD_DE, the data-enable signal from the LTDC. Follow the schematic.
#include "stm32h7xx.h" /* with STM32H750xx defined by the project */
void lcd_power_pins_init(void)
{
RCC->AHB4ENR |= RCC_AHB4ENR_GPIODEN | RCC_AHB4ENR_GPIOKEN; /* ports D and K */
(void)RCC->AHB4ENR;
GPIOD->MODER = (GPIOD->MODER & ~GPIO_MODER_MODE7_Msk) | (1U << GPIO_MODER_MODE7_Pos); /* PD7 = LCD_DISP, output */
GPIOK->MODER = (GPIOK->MODER & ~GPIO_MODER_MODE0_Msk) | (1U << GPIO_MODER_MODE0_Pos); /* PK0 = LCD_BL, output */
GPIOD->BSRR = GPIO_BSRR_BS7; /* LCD_DISP high: display enabled (polarity as in ST's BSP) */
GPIOK->BSRR = GPIO_BSRR_BS0; /* LCD_BL high: backlight on */
}void lcd_power_pins_init(void)
{
GPIO_InitTypeDef io = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOK_CLK_ENABLE();
io.Mode = GPIO_MODE_OUTPUT_PP;
io.Pull = GPIO_NOPULL;
io.Speed = GPIO_SPEED_FREQ_LOW;
io.Pin = GPIO_PIN_7; /* PD7 = LCD_DISP */
HAL_GPIO_Init(GPIOD, &io);
io.Pin = GPIO_PIN_0; /* PK0 = LCD_BL */
HAL_GPIO_Init(GPIOK, &io);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); /* display enabled */
HAL_GPIO_WritePin(GPIOK, GPIO_PIN_0, GPIO_PIN_SET); /* backlight on */
}Tip. PK0 can also be driven by TIM8 channel 3 (alternate function 3) to dim the backlight with PWM. ST’s BSP does this. For now a steady high is enough.
Stage 2: pins, pixel clock and timings
Now the LTDC. Three things must be right before it can talk to the panel.
1. The pins. 28 pins carry LTDC signals: 24 colour lines, CLK, HSYNC, VSYNC and DE. They are on ports H, I, J and K, all with alternate function 14. The list is in the user manual (Table 10) and the finder shows it in the pin table of this page. Careful with port J: PJ2 is the LED from the first guide, not a display line, so do not switch all of port J.
2. The pixel clock. The LTDC clock comes from the R output of PLL3, and nowhere else. The panel wants about 9.6 MHz. With the 25 MHz HSE: 25 / 5 = 5 MHz, × 160 = 800 MHz at the VCO, / 83 = 9.64 MHz. These are the numbers ST's BSP uses. PLL3 is independent of PLL1, so the CPU clock is untouched.
3. The timings. The panel needs a rhythm of pulses and gaps. Each line is: a sync pulse, a back porch, 480 visible pixels, a front porch. Each frame is the same in lines. The panel documentation is not among the PDFs here. These values are **from ST's BSP driver (rk043fn48h.h), not from these documents**:
- Horizontal: sync 41, back porch 13, front porch 32 pixel clocks. A line is 41 + 13 + 480 + 32 = 566 clocks.
- Vertical: sync 10, back porch 2, front porch 2 lines. A frame is 10 + 2 + 272 + 2 = 286 lines.
- Frame rate: 9.64 MHz / (566 × 286) = about 59.5 frames per second.
The LTDC registers want these as accumulated values, each minus one: LTDC_SSCR holds the sync width, LTDC_BPCR the sync plus back porch, LTDC_AWCR adds the active area and LTDC_TWCR adds the front porch. The polarity of HSYNC, VSYNC, DE and the pixel clock is four bits in LTDC_GCR: ST uses active low for all, which is the reset value, so you write 0.
The HAL version below also enables the LTDC at the end (the register version waits until the layer exists). Either way you still see no picture yet: the LTDC has no layer, so it sends the background colour, black.
#include "stm32h7xx.h"
/* Panel timings: from ST's BSP driver (rk043fn48h.h), NOT from the documents on this site */
#define LCD_W 480U
#define LCD_H 272U
#define LCD_HSYNC 41U
#define LCD_HBP 13U
#define LCD_HFP 32U
#define LCD_VSYNC 10U
#define LCD_VBP 2U
#define LCD_VFP 2U
typedef struct { GPIO_TypeDef *port; uint8_t pin; } lcd_pin_t;
static const lcd_pin_t lcd_pins[] = {
{GPIOI,15}, {GPIOJ,0}, {GPIOJ,1}, {GPIOH,9}, {GPIOJ,3}, {GPIOJ,4}, {GPIOJ,5}, {GPIOJ,6}, /* R0..R7 */
{GPIOJ,7}, {GPIOJ,8}, {GPIOJ,9}, {GPIOJ,10}, {GPIOJ,11}, {GPIOI,0}, {GPIOI,1}, {GPIOK,2}, /* G0..G7 */
{GPIOJ,12}, {GPIOJ,13}, {GPIOJ,14}, {GPIOJ,15}, {GPIOK,3}, {GPIOK,4}, {GPIOK,5}, {GPIOK,6}, /* B0..B7 */
{GPIOI,14}, {GPIOI,12}, {GPIOI,9}, {GPIOK,7}, /* CLK, HSYNC, VSYNC, DE */
};
static void lcd_pins_init(void)
{
RCC->AHB4ENR |= RCC_AHB4ENR_GPIOHEN | RCC_AHB4ENR_GPIOIEN | RCC_AHB4ENR_GPIOJEN | RCC_AHB4ENR_GPIOKEN;
(void)RCC->AHB4ENR;
for (unsigned i = 0; i < sizeof lcd_pins / sizeof lcd_pins[0]; i++) {
GPIO_TypeDef *p = lcd_pins[i].port;
unsigned n = lcd_pins[i].pin;
volatile uint32_t *afr = &p->AFR[n >> 3]; /* AFRL for pins 0-7, AFRH for 8-15 */
p->MODER = (p->MODER & ~(3U << (2U * n))) | (2U << (2U * n)); /* alternate function */
p->OSPEEDR = (p->OSPEEDR & ~(3U << (2U * n))) | (2U << (2U * n)); /* high speed */
*afr = (*afr & ~(0xFU << (4U * (n & 7U)))) | (14U << (4U * (n & 7U))); /* AF14 = LTDC */
}
}
void lcd_clock_and_timing_init(void)
{
lcd_pins_init();
/* Pixel clock from PLL3: 25 MHz / 5 = 5 MHz, x 160 = 800 MHz, / 83 = 9.64 MHz on the R output.
(PLLSRC is shared by all PLLs and is already HSE from the clocks guide.) */
RCC->PLLCKSELR = (RCC->PLLCKSELR & ~RCC_PLLCKSELR_DIVM3) | (5U << RCC_PLLCKSELR_DIVM3_Pos);
RCC->PLLCFGR = (RCC->PLLCFGR & ~(RCC_PLLCFGR_PLL3RGE | RCC_PLLCFGR_PLL3VCOSEL | RCC_PLLCFGR_PLL3FRACEN |
RCC_PLLCFGR_DIVP3EN | RCC_PLLCFGR_DIVQ3EN))
| RCC_PLLCFGR_PLL3RGE_2 | RCC_PLLCFGR_DIVR3EN; /* 4 to 8 MHz input, wide VCO, only R enabled */
RCC->PLL3DIVR = ((160U - 1U) << RCC_PLL3DIVR_N3_Pos) | ((83U - 1U) << RCC_PLL3DIVR_R3_Pos);
RCC->CR |= RCC_CR_PLL3ON;
while (!(RCC->CR & RCC_CR_PLL3RDY)) { }
RCC->APB3ENR |= RCC_APB3ENR_LTDCEN; /* clock for the LTDC registers */
(void)RCC->APB3ENR;
/* All values are "minus one", and accumulated: each register adds the previous parts. */
LTDC->SSCR = ((LCD_HSYNC - 1U) << LTDC_SSCR_HSW_Pos) | ((LCD_VSYNC - 1U) << LTDC_SSCR_VSH_Pos);
LTDC->BPCR = ((LCD_HSYNC + LCD_HBP - 1U) << LTDC_BPCR_AHBP_Pos) | ((LCD_VSYNC + LCD_VBP - 1U) << LTDC_BPCR_AVBP_Pos);
LTDC->AWCR = ((LCD_HSYNC + LCD_HBP + LCD_W - 1U) << LTDC_AWCR_AAW_Pos) | ((LCD_VSYNC + LCD_VBP + LCD_H - 1U) << LTDC_AWCR_AAH_Pos);
LTDC->TWCR = ((LCD_HSYNC + LCD_HBP + LCD_W + LCD_HFP - 1U) << LTDC_TWCR_TOTALW_Pos)
| ((LCD_VSYNC + LCD_VBP + LCD_H + LCD_VFP - 1U) << LTDC_TWCR_TOTALH_Pos);
LTDC->GCR = 0; /* HSPOL, VSPOL, DEPOL, PCPOL all 0 = active low, as in ST's example */
LTDC->BCCR = 0; /* background colour: black */
}LTDC_HandleTypeDef hltdc;
void lcd_clock_and_timing_init(void)
{
RCC_PeriphCLKInitTypeDef pll3 = {0};
GPIO_InitTypeDef io = {0};
/* LTDC pins (AF14): R0..R7, G0..G7, B0..B7, CLK, HSYNC, VSYNC, DE */
__HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOI_CLK_ENABLE();
__HAL_RCC_GPIOJ_CLK_ENABLE(); __HAL_RCC_GPIOK_CLK_ENABLE();
io.Mode = GPIO_MODE_AF_PP; io.Pull = GPIO_NOPULL; io.Speed = GPIO_SPEED_FREQ_HIGH; io.Alternate = GPIO_AF14_LTDC;
io.Pin = GPIO_PIN_9; HAL_GPIO_Init(GPIOH, &io);
io.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_9 | GPIO_PIN_12 | GPIO_PIN_14 | GPIO_PIN_15; HAL_GPIO_Init(GPIOI, &io);
io.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | 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(GPIOJ, &io); /* all of port J except PJ2 (the LED) */
io.Pin = GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7; HAL_GPIO_Init(GPIOK, &io);
/* Pixel clock: PLL3 R output = 25 MHz / 5 * 160 / 83 = 9.64 MHz */
pll3.PeriphClockSelection = RCC_PERIPHCLK_LTDC;
pll3.PLL3.PLL3M = 5; pll3.PLL3.PLL3N = 160; pll3.PLL3.PLL3P = 2; pll3.PLL3.PLL3Q = 2; pll3.PLL3.PLL3R = 83;
pll3.PLL3.PLL3RGE = RCC_PLL3VCIRANGE_2; pll3.PLL3.PLL3VCOSEL = RCC_PLL3VCOWIDE; pll3.PLL3.PLL3FRACN = 0;
if (HAL_RCCEx_PeriphCLKConfig(&pll3) != HAL_OK) Error_Handler();
__HAL_RCC_LTDC_CLK_ENABLE();
hltdc.Instance = LTDC;
hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
hltdc.Init.HorizontalSync = 41 - 1; /* HSYNC - 1 */
hltdc.Init.VerticalSync = 10 - 1; /* VSYNC - 1 */
hltdc.Init.AccumulatedHBP = 41 + 13 - 1;
hltdc.Init.AccumulatedVBP = 10 + 2 - 1;
hltdc.Init.AccumulatedActiveW = 41 + 13 + 480 - 1;
hltdc.Init.AccumulatedActiveH = 10 + 2 + 272 - 1;
hltdc.Init.TotalWidth = 41 + 13 + 480 + 32 - 1;
hltdc.Init.TotalHeigh = 10 + 2 + 272 + 2 - 1; /* sic: the HAL field name has a typo */
hltdc.Init.Backcolor.Blue = 0; hltdc.Init.Backcolor.Green = 0; hltdc.Init.Backcolor.Red = 0;
if (HAL_LTDC_Init(&hltdc) != HAL_OK) Error_Handler(); /* also sets LTDCEN: the panel now gets a signal */
}Tip. If the picture later appears shifted by a few pixels or the edges are cut off, the timing numbers are the first suspect. If there is no picture at all or it flickers, check the polarity bits in LTDC_GCR and that PLL3 is running.
Stage 3: one layer pointing at a frame buffer
The LTDC has two layers; one is enough. A layer is described by six facts:
- Where on screen. The window. It must lie inside the active area, so its first pixel is the accumulated back porch plus 1 and its last pixel is the accumulated active width (
LTDC_LxWHPCRhorizontally,LTDC_LxWVPCRvertically). The HAL does this addition for you; the register code does it with two lines. - What a pixel looks like.
LTDC_LxPFCR=010means pixel format RGB565: 5 bits red, 6 bits green, 5 bits blue in 16 bits. (000would be ARGB8888, 32 bits.) - Where the pixels are.
LTDC_LxCFBARholds the start address of the frame buffer. - How wide a row is.
LTDC_LxCFBLRhas two fields: the pitch in bytes (480 × 2 = 960) and the line length in bytes plus 7 (967). Easy to get wrong. - How many rows.
LTDC_LxCFBLNR= 272. - Switch it on.
LENinLTDC_LxCR.
Then comes the step everybody forgets. The layer registers are shadowed: what you write sits in a holding area until you reload it by writing IMR (immediately) or VBR (at the next vertical blanking) in LTDC_SRCR. Without the reload the LTDC keeps using the old, empty configuration. After the reload you enable the whole controller with LTDCEN in LTDC_GCR. This is the order from the reference manual's programming procedure.
Where does the frame buffer live? Not in the DTCM: the LTDC cannot read it. A global array of 261,120 bytes in the default CubeIDE linker script ends up in .bss, which that script places in the AXI SRAM at 0x24000000. Open the .map file once and check the address of fb. It starts at 0 everywhere (.bss is cleared at start-up), which in RGB565 is black.
After this stage the screen shows a black picture from RAM. Still nothing to see, but now it is your memory being displayed.
#include "stm32h7xx.h"
#include <stdint.h>
#define LCD_W 480U
#define LCD_H 272U
#define LCD_HSYNC 41U
#define LCD_HBP 13U
#define LCD_VSYNC 10U
#define LCD_VBP 2U
/* RGB565 = 2 bytes per pixel: 480 * 272 * 2 = 261,120 bytes. With CubeIDE's default linker script a
global array lands in .bss, which is in AXI SRAM (0x24000000). 32-byte aligned for the cache. */
uint16_t fb[LCD_W * LCD_H] __attribute__((aligned(32)));
void lcd_layer1_init(void)
{
uint32_t ahbp = LCD_HSYNC + LCD_HBP - 1U; /* the value already written to LTDC_BPCR */
uint32_t avbp = LCD_VSYNC + LCD_VBP - 1U;
/* The layer window covers the whole active area: first pixel = AHBP + 1, last pixel = AAW */
LTDC_Layer1->WHPCR = ((ahbp + LCD_W) << LTDC_LxWHPCR_WHSPPOS_Pos) | ((ahbp + 1U) << LTDC_LxWHPCR_WHSTPOS_Pos);
LTDC_Layer1->WVPCR = ((avbp + LCD_H) << LTDC_LxWVPCR_WVSPPOS_Pos) | ((avbp + 1U) << LTDC_LxWVPCR_WVSTPOS_Pos);
LTDC_Layer1->PFCR = 2U; /* PF = 010: RGB565 */
LTDC_Layer1->CFBAR = (uint32_t)fb; /* where the pixels are */
LTDC_Layer1->CFBLR = ((LCD_W * 2U) << LTDC_LxCFBLR_CFBP_Pos) /* pitch: bytes from line to line */
| (LCD_W * 2U + 7U); /* line length in bytes, plus 7 */
LTDC_Layer1->CFBLNR = LCD_H; /* number of lines */
/* CACR (alpha 255) and BFCR (blend with constant alpha) already have usable reset values. */
LTDC_Layer1->CR |= LTDC_LxCR_LEN; /* layer on */
LTDC->SRCR = LTDC_SRCR_IMR; /* copy the shadow registers to the active ones now */
LTDC->GCR |= LTDC_GCR_LTDCEN; /* start scanning out the frame buffer */
}extern LTDC_HandleTypeDef hltdc; /* the handle from stage 2, already initialised */
uint16_t fb[480 * 272] __attribute__((aligned(32))); /* 261,120 bytes of RGB565 */
void lcd_layer1_init(void)
{
LTDC_LayerCfgTypeDef l = {0};
l.WindowX0 = 0; l.WindowX1 = 480; /* HAL adds the back porch for you */
l.WindowY0 = 0; l.WindowY1 = 272;
l.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
l.FBStartAdress = (uint32_t)fb; /* (sic) the HAL field name has a typo */
l.ImageWidth = 480;
l.ImageHeight = 272;
l.Alpha = 255; /* layer fully opaque */
l.Alpha0 = 0;
l.BlendingFactor1 = LTDC_BLENDING_FACTOR1_CA;
l.BlendingFactor2 = LTDC_BLENDING_FACTOR2_CA;
l.Backcolor.Blue = 0; l.Backcolor.Green = 0; l.Backcolor.Red = 0;
if (HAL_LTDC_ConfigLayer(&hltdc, &l, LTDC_LAYER_1) != HAL_OK) Error_Handler(); /* writes the layer registers and reloads */
}Tip. ST’s LTDC_Display_1Layer example passes the layer index 1 to HAL_LTDC_ConfigLayer, which is the second layer even though its comments say layer 1. Use LTDC_LAYER_1, which is 0.
Stage 4: draw a pixel, a rectangle and a line
Now the fun part. The screen is an array. Pixel (x, y) is element y * 480 + x, a 16-bit value: 5 bits red in the top, 6 green, 5 blue at the bottom. A small macro packs three 8-bit colours into that format by dropping the low bits.
put_pixel writes one element. fill_rect is two loops around it. A line is a loop that moves x and y together. Nothing else is needed: no "refresh" call, no flush. The LTDC is reading the array 60 times a second, so a change in RAM shows on the next frame.
The one trap is the cache. If you enabled the D-cache, the CPU's writes may sit in the cache while the LTDC reads the stale RAM behind it. After drawing, clean the cache range of the frame buffer (see the memory guide). A project that never turned the data cache on does not need it, but leaving the call in is harmless.
Expected result: a black screen with a red, a green and a blue rectangle in a row and a white diagonal line below them. If you see them, you have a working display driver. Everything else on this page, and everything TouchGFX and LVGL do, is a faster or fancier way to fill the same array.
#include "stm32h7xx.h"
#include <stdint.h>
#define LCD_W 480
#define LCD_H 272
extern uint16_t fb[LCD_W * LCD_H];
/* 8-bit r, g, b -> RGB565: keep the top 5, 6 and 5 bits */
#define RGB565(r, g, b) ((uint16_t)((((r) & 0xF8) << 8) | (((g) & 0xFC) << 3) | ((b) >> 3)))
static inline void put_pixel(int x, int y, uint16_t colour)
{
if ((unsigned)x < LCD_W && (unsigned)y < LCD_H) fb[y * LCD_W + x] = colour;
}
void fill_rect(int x0, int y0, int w, int h, uint16_t colour)
{
for (int y = y0; y < y0 + h; y++)
for (int x = x0; x < x0 + w; x++)
put_pixel(x, y, colour);
}
void draw_demo(void)
{
fill_rect(0, 0, LCD_W, LCD_H, RGB565(0, 0, 0)); /* black background */
fill_rect( 40, 40, 120, 80, RGB565(255, 0, 0)); /* red */
fill_rect(180, 40, 120, 80, RGB565(0, 255, 0)); /* green */
fill_rect(320, 40, 120, 80, RGB565(0, 0, 255)); /* blue */
for (int i = 0; i < 200; i++) put_pixel(40 + i, 160 + i / 2, RGB565(255, 255, 255)); /* a white line */
SCB_CleanDCache_by_Addr((uint32_t *)fb, sizeof fb); /* only needed when the D-cache is on */
}int main(void)
{
HAL_Init();
SystemClock_Config(); /* from the clocks guide: it also starts the HSE that PLL3 needs below */
lcd_power_pins_init(); /* stage 1 */
lcd_clock_and_timing_init(); /* stage 2 */
lcd_layer1_init(); /* stage 3 */
draw_demo(); /* stage 4 */
while (1) { }
}Tip. Try a gradient: fb[y * 480 + x] = RGB565(x / 2, y * 255 / 271, 0) for every pixel. A smooth sweep proves that all 16 bits and the 960-byte pitch are right.
Stage 5: let the DMA2D fill rectangles
The double loop in fill_rect keeps the CPU busy for every pixel. A full-screen fill is 130,560 pixels, which is a lot of wasted time. The DMA2D is a small DMA that understands rectangles: give it a start address, a width, a height and how many pixels to skip at the end of each row, and it fills the area by itself.
The simplest mode is register-to-memory: no source, just one colour written many times. That is MODE = 011 in DMA2D_CR. The registers to fill in:
DMA2D_OPFCCR.CM=010: the output format is RGB565, the same as the layer.DMA2D_OCOLR: the colour (an RGB565 value for this format).DMA2D_OMAR: the address of the rectangle's first pixel,&fb[y * 480 + x].DMA2D_OOR: the line offset, the number of pixels to skip at the end of each row to get to the start of the next:480 - width.DMA2D_NLR: width in the upper half (PL), height in the lower half (NL).
Then START in DMA2D_CR. When the transfer is done, TCIF in DMA2D_ISR is set and you clear it with CTCIF in DMA2D_IFCR. The DMA2D has its own clock bit in RCC_AHB3ENR.
Two things to know. The DMA2D reads and writes RAM directly, so if the D-cache is on, invalidate the range before the CPU reads those pixels, and the frame buffer must be somewhere it can reach (AXI SRAM or SDRAM, not the TCM). And the polling loop below waits, so it is no faster than the CPU unless you do something useful meanwhile; with the transfer-complete interrupt (TCIE) the CPU is really free.
Try it: replace the black fill in draw_demo with dma2d_fill(0, 0, 480, 272, RGB565(0, 0, 0)), or add a yellow rectangle of 100 × 60 at (350, 180).
#include "stm32h7xx.h"
#include <stdint.h>
#define LCD_W 480U
extern uint16_t fb[480 * 272];
void dma2d_fill(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t rgb565)
{
RCC->AHB3ENR |= RCC_AHB3ENR_DMA2DEN; /* clock for the DMA2D registers */
(void)RCC->AHB3ENR;
DMA2D->CR = DMA2D_CR_MODE_0 | DMA2D_CR_MODE_1; /* MODE = 011: register-to-memory (a plain fill) */
DMA2D->OPFCCR = 2U; /* CM = 010: output format RGB565 */
DMA2D->OCOLR = rgb565; /* the colour to write */
DMA2D->OMAR = (uint32_t)&fb[y * LCD_W + x]; /* address of the first pixel */
DMA2D->OOR = LCD_W - w; /* pixels to skip at the end of each line */
DMA2D->NLR = (w << DMA2D_NLR_PL_Pos) | h; /* pixels per line, number of lines */
DMA2D->CR |= DMA2D_CR_START;
while (!(DMA2D->ISR & DMA2D_ISR_TCIF)) { } /* wait for transfer complete */
DMA2D->IFCR = DMA2D_IFCR_CTCIF; /* clear the flag */
/* If the D-cache is on and the CPU will now read these pixels: SCB_InvalidateDCache_by_Addr() first. */
}extern uint16_t fb[480 * 272];
static DMA2D_HandleTypeDef hdma2d;
void dma2d_fill(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t rgb565)
{
__HAL_RCC_DMA2D_CLK_ENABLE();
hdma2d.Instance = DMA2D;
hdma2d.Init.Mode = DMA2D_R2M; /* CR.MODE = register-to-memory */
hdma2d.Init.ColorMode = DMA2D_OUTPUT_RGB565; /* OPFCCR.CM */
hdma2d.Init.OutputOffset = 480 - w; /* OOR */
if (HAL_DMA2D_Init(&hdma2d) != HAL_OK) Error_Handler();
if (HAL_DMA2D_Start(&hdma2d, rgb565, (uint32_t)&fb[y * 480 + x], w, h) != HAL_OK) Error_Handler();
if (HAL_DMA2D_PollForTransfer(&hdma2d, 100) != HAL_OK) Error_Handler();
}Tip. The DMA2D can do more than fills: copy an image into the frame buffer, convert pixel formats on the way, and blend two images. That is what graphics libraries use it for. Start with DMA2D_CR modes 000 to 010 in the reference manual.
Where to go from here
You now have the whole chain: pixels in RAM, scanned out by the LTDC, filled by the DMA2D. The natural next steps:
- Move the frame buffer to the SDRAM. Initialise the SDRAM (memory guide), then point
LTDC_LxCFBARat0xD0000000. You get room for many full-screen buffers. The price is slower CPU access, so draw into AXI SRAM and copy, or let the DMA2D do the heavy work. - Double buffering. Keep two buffers. Draw into the hidden one, then swap by writing the new address to
LTDC_LxCFBARand reloading withVBR(at the vertical blanking), so the swap happens between frames and you never see a half-drawn picture. - ARGB8888 and a second layer. 32 bits per pixel gives 8 bits of transparency, and the second layer blends on top of the first. A full-screen ARGB8888 buffer is 522,240 bytes, which leaves only 2 KB of the 512 KB AXI SRAM: another reason to use SDRAM.
- Touch. The panel also has a touch controller. That is the next guide.
- Use a graphics library. Writing buttons, fonts and animations from scratch is a lot of work. These are external projects, not part of these documents: LVGL (open source, runs on any frame buffer) and TouchGFX (ST's tool, integrated with CubeMX, has a designer with a simulator). Both need exactly what you built here: a frame buffer, a pitch and a flush.
- Read ST's BSP once.
stm32h750b_discovery_lcd.cin the STM32CubeH7 package is this same code with extras (orientation, text, brightness). You can now read it.
Common mistakes
- Forgetting the reload (
LTDC_SRCR) or the finalLTDCEN. The layer is configured but the LTDC never shows it: a black screen with the backlight on. - Writing the line length as 960 instead of 960 + 7 (
LTDC_LxCFBLR.CFBLL). The reference manual says "bytes per line plus 7". A wrong value gives a shifted or torn picture, or an underrun. - Putting the frame buffer in DTCM (
0x20000000) or ITCM. The LTDC and DMA2D cannot reach them. Use AXI SRAM or SDRAM. - Following ST’s BSP macro for the display-enable pin. It says PK7; the schematic and the user manual say PD7, and PK7 is the data-enable signal. Also note the BSP header names PA2 as a reset pin, while the schematic and the user manual’s I/O assignment table connect LCD_RST to PB12, which ST’s BSP source files never touch.
- Switching all of port J to alternate function 14. PJ2 is the green LED LD7.
- Forgetting cache maintenance with the D-cache on. The screen shows old pixels or half an update. Clean after the CPU draws, invalidate before the CPU reads what the DMA2D wrote.
- Using the panel timings as if they came from the documents. They are from ST’s BSP driver; the panel datasheet is not among the seven PDFs. If you use a different panel, replace them with its datasheet values.
- Forgetting that most LTDC and timing registers hold "minus one" and accumulated values. Writing the plain sum shifts the picture.
- Not enabling clocks: the LTDC (
RCC_APB3ENR), the DMA2D (RCC_AHB3ENR), the GPIO ports and PLL3. A peripheral with no clock reads as zero and ignores writes.
Check yourself
Try to answer before opening. If you get stuck, the pages linked above have the answer.
Why does a 480 × 272 RGB565 frame buffer take 261,120 bytes, and why does that fit the AXI SRAM?
480 × 272 = 130,560 pixels, 2 bytes each, so 261,120 bytes. The AXI SRAM is 512 KB (524,288 bytes), so it fits with room to spare. The DTCM (128 KB) would be too small, and the LTDC cannot reach it anyway.
What do you write to LTDC_LxCFBLR for a 480-pixel RGB565 line?
Pitch 960 in bits 28:16 and line length 960 + 7 = 967 in bits 12:0: (960 << 16) | 967 = 0x03C003C7. The reference manual’s own example for a 320-pixel RGB888 line (also 960 bytes) gives the same number.
The backlight is on but the screen stays white or black. Name four things to check, in order.
Did the PLL3 start (PLL3RDY) and is the LTDC clock enabled in RCC_APB3ENR? Are the 28 LTDC pins in alternate function 14? Is LTDCEN set in LTDC_GCR, and did you reload with LTDC_SRCR after the layer settings, with LEN set in LTDC_LxCR? Is the frame buffer address in a RAM the LTDC can reach, and does it contain what you expect?
Why do you need a cache clean after drawing, and when would you not?
The CPU writes into its data cache; the LTDC reads RAM directly. Without a clean the LTDC sees old data. You do not need it when the D-cache is off, or when the buffer is in a region the MPU marks non-cacheable.
Reading list
Every page this guide sent you to, by document. Reading them in order is a good revision.
| UM2488 | UM2488 STM32H745I/H750B-DK Discovery kit user manual | p.15p.19 |
| MB1381 | MB1381 Discovery board schematic (H750XB variant) | sheet 12sheet 12sheet 12sheet 12 |
| RM0433 | RM0433 reference manual (STM32H742/743/753/750) | p.136p.353p.397p.399p.408p.451p.457p.460p.540p.541p.543p.544p.545p.725p.731p.732p.733p.741p.742p.744p.744p.745p.859p.1201p.1204p.1211p.1212p.1212p.1213p.1214p.1214p.1216p.1216p.1220p.1221p.1222p.1223p.1226p.1226p.1227 |
| DS12556 | STM32H750xB datasheet | p.38 |
| PM0253 | PM0253 Cortex-M7 programming manual | p.240 |
Search next
Each of these opens the finder with pins, registers, schematic hits and document pages for the term.