STM32H747 DMA2D 图像混合硬件加速原理与工程实现DMA2D Alpha Blending on the STM32H747: Hardware Acceleration Principles and Implementation


AI free token/AI免费积分

STM32H747 DMA2D 图像混合硬件加速原理与工程实现

本文来源:http://www.hqwc.cn/news/1570012.html
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

做图像显示和视觉应用时,“图像混合”这件事往往不像它表面上那么轻松。一个最典型的场景是:UI 上要做一个 50% 半透明的弹窗,底层是实时视频,上层要叠加图标和文字。如果直接在单片机上遍历每个像素,用 C 语言做 alpha 插值计算,一次混合一帧 800x480 的 RGB565 图像,少说要几百万次乘加运算,CPU 占用会高到让人怀疑人生。而 STM32H747 这类高性能 MCU 真正不同于普通单片机的地方,不只是主频高、内核多,更在于它内部有一个容易被忽略、却在 GUI 和视觉应用里扮演关键角色的硬件外设——DMA2D。

这篇文章就来拆解 DMA2D 的图形加速原理,重点讲清楚它在 STM32H747 上如何实现图像混合,以及在实际视觉应用开发中怎么配置、怎么验证、怎么避坑。通过这篇文章,你会得到一个可以直接照做的 DMA2D 混合加速方案,也能理解为什么说它解决的不只是“速度”问题,而是 CPU 的周期性负载问题。

假设你正在做一台基于 STM32H747 的视觉识别终端。屏幕显示实时视频画面,同时要在画面顶部叠加一个半透明的状态栏,在画面中央显示一个识别框。如果只做静态图片拷贝,用 memcpy 就能解决,但一旦涉及半透明效果、逐像素 alpha 混合,事情就变了。

没有 DMA2D 的时候,大多数开发者的做法是这样的:读取背景图层某个像素的颜色值,读取前景图层对应像素的颜色值,然后按照 alpha 权重做插值计算,再把结果写回显存。以 ARGB8888 格式为例,每个像素需要做 4 个通道的乘加运算。如果一帧是 800x480,每通道按公式 (fg * alpha + bg * (1 - alpha)) 计算,CPU 需要处理的运算量大约是:

每个像素至少 4 字节 × 每个字节一次乘加,约 150 万次以上运算

再加上循环、偏移计算、读写显存的时间,一帧下来即使主频 480MHz 的 Cortex-M7,也会消耗数毫秒甚至更长时间

这还是在没有做颜色格式转换的情况下。如果前景是 ARGB8888,背景是 RGB565,目标又是 RGB888,软件还需要在每一层做格式转换和像素对齐。代码复杂度会进一步上升。

如果只是开机时混一次静态图像,慢一点可以接受。问题在于视觉应用里混合是持续的:视频帧不断更新,UI 弹窗要跟着动画渐变,识别结果要叠加到视频流上。这意味着 CPU 每隔几十毫秒就要被“图像混合”任务打断一次,导致主循环无法稳定地处理摄像头采集、算法识别、通信协议等任务。

对于带双核的 STM32H747 来说,虽然可以把部分任务卸载给 Cortex-M4 核心,但如果混合操作始终占用 CPU 周期,就会挤压核心算法的可用算力。与其如此,不如把像素级运算交给专门的硬件。

这里需要强调一个判断:DMA2D 的本质不是“代替你做 UI”,而是把高频次、周期性的像素搬运与计算从 CPU 执行流中剥离出来。CPU 只需要配置好源地址、目标地址、像素格式和混合模式,然后发起一次传输,之后就可以去处理其他任务。这才是“加速”二字的真正含义。

2. STM32H747 双核架构与 DMA2D 基础概念

STM32H747 是 ST 推出的高性能双核 MCU,内部集成一个 Cortex-M7 内核和一个 Cortex-M4 内核。M7 内核主频高,适合跑算法和主业务逻辑;M4 内核可以作为协处理核心。两个核心通过共享内存和硬件信号量进行通信。

芯片内部集成了非常丰富的外设,除了常规的串口、SPI、I2C 之外,还有几个对图形和视觉应用特别关键的外设:DMA2D 图形加速器、LTDC LCD 控制器、DCMI 数字摄像头接口,以及大容量 SRAM。这些外设组合在一起,使得 STM32H747 非常适合做图形交互界面和轻量级视觉应用。

DMA2D 是 STM32 系列中面向 2D 图形加速的 DMA 控制器,ST 官方把它称为 Chrom-ART Accelerator。如果普通 DMA 解决的是“数据从一个地址搬到另一个地址”,那么 DMA2D 解决的是“数据在搬运过程中还能实现格式转换、颜色填充、Alpha 混合等像素级操作”。

DMA2D 并不是只能做图像混合。它支持四种工作模式:

在官方参考手册中,图像混合对应的是带 PFC 的存储器到存储器模式。它的关键能力在于,DMA2D 会同时读取前景层(Foreground,简称 FG)和背景层(Background,简称 BG)的像素数据,逐像素完成混合计算,再把结果写入目标地址。

PFC(Pixel Format Converter,像素格式转换器):负责把一种像素格式转换成另一种像素格式。例如把 ARGB8888 转成 RGB565,或者在转换过程中丢弃/补充 alpha 通道。

FG(Foreground):前景图层,通常是需要叠加在上层的图像,例如图标、弹窗、视频小窗口。

BG(Background):背景图层,通常是底层图像,例如主视频画面或桌面背景。

Alpha:透明度通道,取值范围 0 到 255(0x00 到 0xFF)。Alpha 为 0 表示完全透明,为 255 表示完全不透明。

ARGB8888:一种 32 位像素格式,按内存排列分别表示 Alpha、Red、Green、Blue 通道,每通道 8 位。

RGB565:一种 16 位像素格式,Red 占 5 位,Green 占 6 位,Blue 占 5 位,常用于 LCD 屏显存。

DMA2D 做混合时,混合计算发生在 PFC 之后。也就是说,FG 和 BG 的像素源数据会先被转换为统一的内部颜色格式,然后由混合器完成带 alpha 的计算,最后通过输出 PFC 把结果转换为目标格式,写入目标内存。这也是为什么 DMA2D 可以做到输入输出格式不一致。

很多初学者会把 DMA2D 和 LTDC 搞混。LTDC 是液晶屏控制器,它的任务是从显存中持续读取数据,并按照时序把像素数据发送给 RGB 接口的 LCD 面板。它解决的是“如何把图像显示到屏幕上”的问题。

DMA2D 解决的是“如何在显存中生成和处理图像数据”的问题。

一次典型的 GUI 绘制流程是:软件先在后台显存中用 DMA2D 完成图层混合和绘制,然后把混合后的显存地址交给 LTDC 去刷新显示。二者并不冲突,而是前后衔接的关系。在复杂的界面系统中,甚至会有多个显存区域,LTDC 从其中一个区域读数据,DMA2D 在另一个区域做绘制,二者并行工作,等你完成绘制后再切换显示地址。

3. DMA2D 图像混合的硬件处理流程

CPU 配置 FG 源地址、BG 源地址、目标地址

CPU 配置 FG/BG 像素格式、目标像素格式、图像尺寸

DMA2D 自动读取 FG/BG 像素并逐像素混合

从表面看,多了一次配置外设的步骤。但实际运行中,DMA2D 完成 384000 个像素的混合只消耗很少的 CPU 时间,因为配置寄存器的开销远小于遍历每个像素的开销。

在处理图像混合时,经常会遇到“把小尺寸图像混合到大尺寸图像指定位置”的需求。例如一个 200x100 的图标,要混合到 800x480 的背景坐标 (300, 200) 处。这时不能简单地设置源地址和目标地址就完事,因为目标图像每一行有 800 个像素,而需要写入的区域每行只有 200 个像素。

DMA2D 为此提供了行偏移(Offset)寄存器。FGOR 是前景图层行偏移,BGOR 是背景图层行偏移,OOR 是输出目标行偏移。行偏移的含义是:在 DMA2D 搬完图像中每一行有效像素之后,需要跳过多少“无效”像素,才能定位到下一行的起始位置。

目标行偏移 = 目标图像宽度 - 需要写入区域的宽度

例如 800 宽背景中写入 200 宽图标,OOR = 800 - 200 = 600

如果没有设置行偏移,DMA2D 会直接把下一行像素写到上一行末尾的紧挨地址处,导致图像整体错位,出现斜切效果。这个细节是新手最容易踩的坑之一。

DMA2D 混合器的核心计算公式如下:

输出颜色 = FGPFC 输出 × FG Alpha + BGPFC 输出 × (1 - FG Alpha)

其中 FG Alpha 既可以来自前景像素自带的 alpha 通道,也可以来自 FGPFCCR 寄存器中设置的固定 alpha 常量,还可以让二者相乘得到最终的 alpha 值。这种灵活性使得 DMA2D 既能处理带透明通道的 PNG 图片,也能对整个图层做统一透明度渐变。

具体来说,最终的有效 alpha 计算有两种模式:

DMA2D Alpha Blending on the STM32H747: Hardware Acceleration Principles and Implementation

本文来源:http://www.hqwc.cn/news/1570012.html
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

Blending images on a microcontroller is deceptively expensive. A typical case is a 50-percent translucent popup over a live video feed with icons and text composited on top: doing per-pixel alpha interpolation in C on an 800x480 RGB565 frame means several million multiply-accumulate operations, and CPU load quickly becomes unacceptable. What sets a high-end part like the STM32H747 apart is not just clock speed or a second core, but an often-overlooked peripheral that matters enormously for GUI and vision work: DMA2D.

This article breaks down how DMA2D works, how alpha blending is configured on the STM32H747, and how to validate and debug it in a real vision application. The payoff is not merely a faster blend — it is removing a periodic CPU load so the main loop can reliably service camera capture, inference, and communications.

Consider a visual recognition terminal built on the STM32H747: the screen shows live video, a semi-transparent status bar is overlaid at the top, and a detection box sits in the centre. A static memcpy is trivial, but translucency requires reading the background pixel and the foreground pixel, interpolating by an alpha weight, and writing the result back. With ARGB8888 each pixel needs four channel multiply-accumulates; at 800x480 that is more than 1.5 million operations per frame, plus loop overhead, offset calculation and framebuffer traffic. Even a 480 MHz Cortex-M7 spends several milliseconds per frame. If the foreground is ARGB8888, the background RGB565 and the target RGB888, format conversion and alignment on every layer add further complexity.

A one-off blend at boot would be tolerable, but in a vision application blending is continuous: video frames refresh, popups animate, and detection results are overlaid on the stream. The CPU is interrupted every few dozen milliseconds and cannot stably handle capture, inference and protocol work. Offloading part of the pipeline to the Cortex-M4 helps, but as long as blending consumes CPU cycles it competes with the core algorithm. The real point is that DMA2D is not a UI framework: it strips high-frequency, periodic pixel movement and arithmetic out of the CPU execution flow. Software only configures source addresses, destination address, pixel formats and blend mode, starts the transfer, and goes back to other work.

The STM32H747 integrates a Cortex-M7 for algorithms and main logic alongside a Cortex-M4 coprocessor, communicating through shared memory and hardware semaphores. Alongside the usual UART, SPI and I2C, it carries a set of peripherals that matter for graphics: the DMA2D Chrom-ART Accelerator, the LTDC LCD controller, a DCMI camera interface and generous SRAM. Where ordinary DMA moves data from one address to another, DMA2D can convert formats, fill colours and perform alpha blending while moving it.

DMA2D supports four operating modes; blending corresponds to memory-to-memory mode with PFC (Pixel Format Converter). It reads the foreground (FG) and background (BG) layers simultaneously, blends them pixel by pixel, and writes the result to the target address. PFC converts between formats such as ARGB8888 and RGB565, discarding or synthesising an alpha channel as needed. Alpha ranges from 0 (fully transparent) to 255 (fully opaque); ARGB8888 is a 32-bit format with 8 bits each for alpha, red, green and blue, while RGB565 is the 16-bit format commonly used for LCD framebuffers. Because blending happens after the input PFCs, both layers are normalised to a common internal colour format, blended, then converted by the output PFC to the destination format — which is why input and output formats may differ.

DMA2D and LTDC are frequently confused. LTDC is the display controller: it continuously reads framebuffer memory and clocks pixel data out to an RGB LCD panel, answering 'how do I get an image onto the screen'. DMA2D answers 'how do I generate and process image data in framebuffer memory'. A typical GUI frame uses DMA2D to composite layers in a back buffer, then hands the resulting address to LTDC for scanout. In more complex systems several framebuffer regions exist, with LTDC reading one while DMA2D draws into another, and the display address is switched once drawing completes.

The hardware flow is short: the CPU configures FG source address, BG source address and destination address; sets FG/BG pixel formats, output format and image dimensions; then DMA2D fetches and blends every pixel autonomously. Blending all 384000 pixels of an 800x480 frame costs very little CPU time because register setup is far cheaper than iterating over pixels.

A common requirement is blending a smaller image into a specified position of a larger one — for example a 200x100 icon at coordinate (300, 200) in an 800x480 background. Simply setting source and destination addresses is not enough: each destination row is 800 pixels wide while only 200 pixels should be written. DMA2D therefore provides line-offset registers: FGOR for the foreground, BGOR for the background and OOR for the output. The offset is the number of 'invalid' pixels to skip after each row of valid pixels before locating the start of the next row. Output offset equals destination width minus the width of the written region, so an 800-wide background receiving a 200-wide icon needs OOR = 600. Omitting the offset makes DMA2D write the next row immediately after the previous one, shearing the image diagonally — one of the most common beginner mistakes.

The blender's core formula is: output colour = FGPFC output x FG Alpha + BGPFC output x (1 - FG Alpha). FG Alpha may come from the foreground pixel's own alpha channel, from a constant configured in the FGPFCCR register, or from the product of both. That flexibility lets DMA2D handle PNG images with per-pixel transparency as well as apply a uniform fade to an entire layer.

The value of this peripheral is not reducing one blend from 5 ms to 0.5 ms, but eliminating a high-frequency periodic CPU load so the M7 core can stay on vision algorithms and business logic, while format conversion and colour filling come along for free as a hardware 2D pipeline. For anyone building an STM32H747 vision terminal, HMI screen or other display-equipped embedded product, the article recommends three next steps: measure DMA2D blend time at the target resolution in both internal SRAM and SDRAM to locate the bandwidth bottleneck; get DMA2D working with LTDC double buffering; and understand how the chosen UI stack — LVGL, ThreadX GUIX or an in-house renderer — calls DMA2D underneath. That is what makes it possible to tell quickly whether a rendering defect lives in UI logic, DMA2D configuration or the cache policy of the memory system. A follow-up piece is planned to chain DCMI camera capture, DMA2D overlay and LCD output into a complete embedded vision demo.

暂无评论,快来发表第一条评论吧!No comments yet. Be the first!

📮 需求咨询📮 Inquiry