前言
在项目中,我们不使用STM32CubeMX生成的DMA2D HAL库函数,而是采用安富莱实现的5个关于DMA2D寄存器操作函数,更多关于DMA2D的知识可参考《安富莱STM32-V6开发板_用户手册》
一、DMA2D配置
1、DMA2D配置比较简单,使能后直接生成工程即可 2、不使用DMA2D的HAL库函数,按照安富莱的方法,采用寄存器的方法效率更高。 代码如下:
#include "main.h"
void _DMA2D_Fill(void * pDst, uint32_t OffLine, uint32_t xSize, uint32_t ySize, uint32_t ColorIndex, uint32_t PixelFormat)
{
DMA2D->OMAR = (uint32_t)pDst;
DMA2D->OOR = OffLine;
DMA2D->NLR = (uint32_t)(xSize << 16) | (uint16_t)ySize;
DMA2D->CR = 0x00030000UL | (1 << 9);
DMA2D->OCOLR = ColorIndex;
DMA2D->OPFCCR = PixelFormat;
DMA2D->CR |= DMA2D_CR_START;
while (DMA2D->CR & DMA2D_CR_START) {}
}
void _DMA2D_Copy(void * pSrc, uint32_t OffLineSrc, void * pDst, uint32_t OffLineDst, uint32_t xSize, uint32_t ySize, uint32_t PixelFormat)
{
DMA2D->CR = 0x00000000UL | (1 << 9);
DMA2D->FGMAR = (uint32_t)pSrc;
DMA2D->OMAR = (uint32_t)pDst;
DMA2D->FGOR = OffLineSrc;
DMA2D->OOR = OffLineDst;
DMA2D->FGPFCCR = LTDC_PIXEL_FORMAT_RGB565;
DMA2D->OPFCCR = LTDC_PIXEL_FORMAT_RGB565;
DMA2D->NLR = (uint32_t)(xSize << 16) | (uint16_t)ySize;
DMA2D->CR |= DMA2D_CR_START;
while (DMA2D->CR & DMA2D_CR_START) {}
}
void _DMA2D_MixColorsBulk(uint32_t * pColorFG, uint32_t OffLineSrcFG, uint32_t * pColorDst, uint32_t OffLineDst, uint32_t xSize, uint32_t ySize, uint8_t Intens)
{
DMA2D->CR = 0x00020000UL | (1 << 9);
DMA2D->FGMAR = (uint32_t)pColorFG;
DMA2D->BGMAR = (uint32_t)pColorDst;
DMA2D->OMAR = (uint32_t)pColorDst;
DMA2D->FGOR = OffLineSrcFG;
DMA2D->BGOR = OffLineDst;
DMA2D->OOR = OffLineDst;
DMA2D->FGPFCCR = LTDC_PIXEL_FORMAT_RGB565
| (1UL << 16)
| ((uint32_t)Intens << 24);
DMA2D->BGPFCCR = LTDC_PIXEL_FORMAT_RGB565;
DMA2D->OPFCCR = LTDC_PIXEL_FORMAT_RGB565;
DMA2D->NLR = (uint32_t)(xSize << 16) | (uint16_t)ySize;
DMA2D->CR |= DMA2D_CR_START;
while (DMA2D->CR & DMA2D_CR_START) {}
}
void _DMA2D_AlphaBlendingBulk(uint32_t * pColorFG, uint32_t OffLineSrcFG, uint32_t * pColorBG, uint32_t OffLineSrcBG, uint32_t * pColorDst, uint32_t OffLineDst, uint32_t xSize, uint32_t ySize)
{
DMA2D->CR = 0x00020000UL | (1 << 9);
DMA2D->FGMAR = (uint32_t)pColorFG;
DMA2D->BGMAR = (uint32_t)pColorBG;
DMA2D->OMAR = (uint32_t)pColorDst;
DMA2D->FGOR = OffLineSrcFG;
DMA2D->BGOR = OffLineSrcBG;
DMA2D->OOR = OffLineDst;
DMA2D->FGPFCCR = LTDC_PIXEL_FORMAT_ARGB8888;
DMA2D->BGPFCCR = LTDC_PIXEL_FORMAT_ARGB8888;
DMA2D->OPFCCR = LTDC_PIXEL_FORMAT_RGB565;
DMA2D->NLR = (uint32_t)(xSize << 16) | (uint16_t)ySize;
DMA2D->CR |= DMA2D_CR_START;
while (DMA2D->CR & DMA2D_CR_START) {}
}
void _DMA2D_DrawAlphaBitmap(void * pDst, uint32_t OffLineSrc, void * pSrc, uint32_t OffLineDst, uint32_t xSize, uint32_t ySize, uint32_t PixelFormat)
{
DMA2D->CR = 0x00020000UL | (1 << 9);
DMA2D->FGMAR = (uint32_t)pSrc;
DMA2D->BGMAR = (uint32_t)pDst;
DMA2D->OMAR = (uint32_t)pDst;
DMA2D->FGOR = OffLineSrc;
DMA2D->BGOR = OffLineDst;
DMA2D->OOR = OffLineDst;
DMA2D->FGPFCCR = LTDC_PIXEL_FORMAT_ARGB8888;
DMA2D->BGPFCCR = PixelFormat;
DMA2D->OPFCCR = PixelFormat;
DMA2D->NLR = (uint32_t)(xSize << 16) | (uint16_t)ySize;
DMA2D->CR |= DMA2D_CR_START;
while (DMA2D->CR & DMA2D_CR_START) {}
}
二、测试DMA2D
DMA2D使用比较简单,使能DMA2D时钟后,直接调用DMA2D API即可。 代码如下(示例):
__HAL_RCC_DMA2D_CLK_ENABLE();
_DMA2D_Fill((void *)(LAYER1ADDR+30*480*4+30*4), 480-50, 50, 50, 0xff000000, LTDC_PIXEL_FORMAT_ARGB8888);
|