Learn / Screen and touch · guide 9 of 10

Get started with the display

The 4.3" screen is lit and shows your own picture: coloured rectangles and a line drawn from your code into a frame buffer, with one rectangle filled by the DMA2D instead of the CPU.

Advanced≈ 90 minBefore this:Blink an LED, Where your code and data live

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.

You will learn
  • 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)
You need
  • 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.

PinBallBoard netUsed here asAFOpen
PK0J14LCD_BLLCD_BL: backlight enable (high = on, as in ST’s BSP); the same pin can also be PWM from TIM8_CH3 for dimming
PD7C11LCD_DISPLCD_DISP: display enable (high = on). A plain output, not an LTDC signal
PI14H3LCD_CLKLCD_CLK: the pixel clock the LTDC sends to the panelAF14 LCD_CLK
PI12H1LCD_HSYNCLCD_HSYNC: horizontal sync pulseAF14 LCD_HSYNC
PI9E2LCD_VSYNCLCD_VSYNC: vertical sync pulseAF14 LCD_VSYNC
PK7D7LCD_DELCD_DE: data enable (high while a visible pixel is sent)AF14 LCD_DE
1

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:

  1. Backlight and display enable. Two output pins. The screen lights up.
  2. Pins, pixel clock and timings. Tell the LTDC how the panel wants its signals.
  3. 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.
  4. Draw pixels. Writing a 16-bit value into the array changes a pixel.
  5. DMA2D fill. Let a graphics DMA fill a rectangle while the CPU does something else.
  6. 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.

2

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 */
}

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.

3

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 */
}

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.

Read this
UM24887.4 LCD RGB connectorp.19Table 10: which MCU pin carries each LCD signalMB1381Sheet 12: LCD4.3 · LCD_CLKsheet 12The RGB data lines and sync signals from the MCU to the panelRM04338.5.8 Kernel clock selectionp.353Figure 54: the LTDC kernel clock is pll3_r_ckRM043332.3 LTDC functional descriptionp.1201Section 32.3.3 explains the LTDC clocks and resetRM043332.4 LTDC programmable parametersp.1204How sync, back porch, active width and total width relate (Figure 246)RM043332.6 LTDC programming procedurep.1211The official order of steps: clock, timings, polarity, layers, reload, enableRM0433RCC_PLLCKSELR: RCC PLL clock source selection registerp.397DIVM3: input divider of PLL3RM0433RCC_PLLCFGR: RCC PLL configuration registerp.399PLL3RGE, PLL3VCOSEL and the output enable DIVR3ENRM0433RCC_PLL3DIVR: RCC PLL3 divider configuration registerp.408DIVN3 and DIVR3 (stored minus one)RM0433RCC_APB3ENR: RCC APB3 clock registerp.460LTDCEN: clock for the LTDC registersRM0433GPIOx_AFRL: GPIO alternate function low registerp.544Alternate function number for pins 0 to 7RM0433GPIOx_AFRH: GPIO alternate function high registerp.545Alternate function number for pins 8 to 15 (AF14 = LTDC)RM0433GPIOx_OSPEEDR: GPIO port output speed registerp.541Output speed of the LTDC pinsRM0433LTDC_SSCR: LTDC synchronization size configuration registerp.1212HSW and VSH: sync size minus oneRM0433LTDC_BPCR: LTDC back porch configuration registerp.1212AHBP and AVBP: accumulated back porchRM0433LTDC_AWCR: LTDC active width configuration registerp.1213AAW and AAH: accumulated active width and heightRM0433LTDC_TWCR: LTDC total width configuration registerp.1214TOTALW and TOTALH: accumulated totalRM0433LTDC_GCR: LTDC global control registerp.1214Polarity bits and LTDCENRM0433LTDC_BCCR: LTDC background color configuration registerp.1216Background colour shown when no layer covers the pixel
4

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_LxWHPCR horizontally, LTDC_LxWVPCR vertically). The HAL does this addition for you; the register code does it with two lines.
  • What a pixel looks like. LTDC_LxPFCR = 010 means pixel format RGB565: 5 bits red, 6 bits green, 5 bits blue in 16 bits. (000 would be ARGB8888, 32 bits.)
  • Where the pixels are. LTDC_LxCFBAR holds the start address of the frame buffer.
  • How wide a row is. LTDC_LxCFBLR has 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. LEN in LTDC_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 */
}

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.

5

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 */
}

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.

6

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. */
}

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.

7

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_LxCFBAR at 0xD0000000. 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_LxCFBAR and reloading with VBR (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.c in 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 final LTDCEN. 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.

UM2488UM2488 STM32H745I/H750B-DK Discovery kit user manualp.15p.19
MB1381MB1381 Discovery board schematic (H750XB variant)sheet 12sheet 12sheet 12sheet 12
RM0433RM0433 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
DS12556STM32H750xB datasheetp.38
PM0253PM0253 Cortex-M7 programming manualp.240

Search next

Each of these opens the finder with pins, registers, schematic hits and document pages for the term.

Where to go next