RT-Thread版本:4.1.0
RT-Thread Studio版本:2.2.8
开发板:正点原子阿波罗F429IGT6
RT-Thread官方的LTDC屏幕刷新是封装了HAL库的LTDC刷新函数,效率比较低,所以使用DMA2D快速刷新的时候会出现撕裂和残影,效果非常不好。如果不解决这个问题,后续的LVGL什么的也就不用想了。这个问题困扰了我一周时间,后来在原子的官方例程找到了灵感:
先来对比一下rtt的刷新和原子的刷新
RT-Thread的LTDC刷新:
需要调用lcd_control里边的update指令,接着往下:
这一坨就是rtt官方的LTDC刷新函数,中间涉及到了函数调用以及数据搬运,非常耗时间。
原子的LTDC刷新:
这个就是最后的刷新函数,直接访问SDRAM内存,效率比较高。
这时候疑问来了,怎么才能把rtt的刷新修改成原子那样直接访问内存呢?
修改思路
先来看一下原子是如何完成对内存的直接访问
1.首先在SDRAM指定区域(SDRAM起始)创建了一个变量ltdc_lcd_framebuf
uint16_t ltdc_lcd_framebuf[1280][800] __attribute__((at(0XC0000000))); /* 定义最大屏分辨率时,LTDC所需的帧缓存数组大小 */
2.在LTDC初始化的时候,把ltdc_lcd_framebuf的指针变量赋值给了g_ltdc_framebuf,让g_ltdc_framebuf指向SDRAM的内存地址
3.设置层颜色帧缓存起始地址,将g_ltdc_framebuf数组首地址赋值给playercfg.FBStartAdress
做完了这些操作,我们就可以直接对SDRAM进行操作,现在就在RTT上试一下吧!
在RT-Thread上的实现
在指定内存区域创建变量
直接复制原子的代码到drv_lcd.c
报错,RAM直接爆表,说明创建失败
在查阅了很多资料之后发现要增加一个类似bss的section,然后在把变量创建在section上
首先打开linkscripts\STM32F429IG\link.lds,在MEMORY区域增加sdram
MEMORY
{
ROM (rx) : ORIGIN = 0x08000000, LENGTH = 1024k /* 1024K flash */
RAM (rw) : ORIGIN = 0x20000000, LENGTH = 192k /* 192K sram */
sdram (rw) : ORIGIN =0xC0000000,LENGTH =32768k
}
然后在SECTIONS里增加sdram,注意一定要在.bss之前添加!!
.sdram (NOLOAD) : ALIGN(4)
{
. = ALIGN(4);
*(.sdram)
*(.bss.sdram)
*(.sdram.*)
. = ALIGN(4);
__sdram_free__ = .;
} > sdram
然后再创建变量
uint16_t ltdc_lcd_framebuf[1280][800] __attribute__((section(".sdram")));
编译没问题,但是RAM爆表,不知道咋解决,但是不影响使用,如果有知道的大佬欢迎评论区留言告知
将把ltdc_lcd_framebuf的指针变量赋值给我们的LTDC全局buff,让全局buff指向SDRAM的内存地址
int drv_lcd_hw_init(void)
{
rt_err_t result = RT_EOK;
struct rt_device *device = &_lcd.parent;
/* memset _lcd to zero */
memset(&_lcd, 0x00, sizeof(_lcd));
/* init lcd_lock semaphore */
result = rt_sem_init(&_lcd.lcd_lock, "lcd_lock", 0, RT_IPC_FLAG_FIFO);
if (result != RT_EOK)
{
LOG_E("init semaphore failed!\n");
result = -RT_ENOMEM;
goto __exit;
}
/* config LCD dev info */
_lcd.lcd_info.height = LCD_HEIGHT;
_lcd.lcd_info.width = LCD_WIDTH;
_lcd.lcd_info.bits_per_pixel = LCD_BITS_PER_PIXEL;
_lcd.lcd_info.pixel_format = LCD_PIXEL_FORMAT;
/* malloc memory for Triple Buffering */
// g_ltdc_framebuf[0] = ;
_lcd.lcd_info.framebuffer = (uint8_t *)<dc_lcd_framebuf;
_lcd.back_buf = rt_malloc(LCD_BUF_SIZE);
_lcd.front_buf = rt_malloc(LCD_BUF_SIZE);
if (_lcd.lcd_info.framebuffer == RT_NULL || _lcd.back_buf == RT_NULL || _lcd.front_buf == RT_NULL)
{
LOG_E("init frame buffer failed!\n");
result = -RT_ENOMEM;
goto __exit;
}
/* memset buff to 0xFF */
memset(_lcd.lcd_info.framebuffer, 0xFF, LCD_BUF_SIZE);
memset(_lcd.back_buf, 0xFF, LCD_BUF_SIZE);
memset(_lcd.front_buf, 0xFF, LCD_BUF_SIZE);
device->type = RT_Device_Class_Graphic;
#ifdef RT_USING_DEVICE_OPS
device->ops = &lcd_ops;
#else
device->init = drv_lcd_init;
device->control = drv_lcd_control;
#endif
/* register lcd device */
rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);
/* init stm32 LTDC */
if (stm32_lcd_init(&_lcd) != RT_EOK)
{
result = -RT_ERROR;
goto __exit;
}
else
{
turn_on_lcd_backlight();
}
__exit:
if (result != RT_EOK)
{
rt_sem_detach(&_lcd.lcd_lock);
if (_lcd.lcd_info.framebuffer)
{
rt_free(_lcd.lcd_info.framebuffer);
}
if (_lcd.back_buf)
{
rt_free(_lcd.back_buf);
}
if (_lcd.front_buf)
{
rt_free(_lcd.front_buf);
}
}
return result;
}
设置层颜色帧缓存起始地址,将全局buff数组首地址赋值给playercfg.FBStartAdress
rt_err_t stm32_lcd_init(struct drv_lcd_device *lcd)
{
LTDC_LayerCfgTypeDef pLayerCfg = {0};
/* LTDC Initialization -------------------------------------------------------*/
MX_LTDC_MspInit();
/* Polarity configuration */
/* Initialize the horizontal synchronization polarity as active low */
LtdcHandle.Init.HSPolarity = LTDC_HSPOLARITY_AL;
/* Initialize the vertical synchronization polarity as active low */
LtdcHandle.Init.VSPolarity = LTDC_VSPOLARITY_AL;
/* Initialize the data enable polarity as active low */
LtdcHandle.Init.DEPolarity = LTDC_DEPOLARITY_AL;
/* Initialize the pixel clock polarity as input pixel clock */
LtdcHandle.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
/* Timing configuration */
/* Horizontal synchronization width = Hsync - 1 */
LtdcHandle.Init.HorizontalSync = LCD_HSYNC_WIDTH - 1;
/* Vertical synchronization height = Vsync - 1 */
LtdcHandle.Init.VerticalSync = LCD_VSYNC_HEIGHT - 1;
/* Accumulated horizontal back porch = Hsync + HBP - 1 */
LtdcHandle.Init.AccumulatedHBP = LCD_HSYNC_WIDTH + LCD_HBP - 1;
/* Accumulated vertical back porch = Vsync + VBP - 1 */
LtdcHandle.Init.AccumulatedVBP = LCD_VSYNC_HEIGHT + LCD_VBP - 1;
/* Accumulated active width = Hsync + HBP + Active Width - 1 */
LtdcHandle.Init.AccumulatedActiveW = LCD_HSYNC_WIDTH + LCD_HBP + lcd->lcd_info.width - 1;
/* Accumulated active height = Vsync + VBP + Active Heigh - 1 */
LtdcHandle.Init.AccumulatedActiveH = LCD_VSYNC_HEIGHT + LCD_VBP + lcd->lcd_info.height - 1;
/* Total height = Vsync + VBP + Active Heigh + VFP - 1 */
LtdcHandle.Init.TotalHeigh = LtdcHandle.Init.AccumulatedActiveH + LCD_VFP;
/* Total width = Hsync + HBP + Active Width + HFP - 1 */
LtdcHandle.Init.TotalWidth = LtdcHandle.Init.AccumulatedActiveW + LCD_HFP;
/* Configure R,G,B component values for LCD background color */
LtdcHandle.Init.Backcolor.Blue = 0;
LtdcHandle.Init.Backcolor.Green = 0;
LtdcHandle.Init.Backcolor.Red = 0;
LtdcHandle.Instance = LTDC;
/* Layer1 Configuration ------------------------------------------------------*/
/* Windowing configuration */
pLayerCfg.WindowX0 = 0;
pLayerCfg.WindowX1 = lcd->lcd_info.width;
pLayerCfg.WindowY0 = 0;
pLayerCfg.WindowY1 = lcd->lcd_info.height;
/* Pixel Format configuration*/
if (lcd->lcd_info.pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB565)
{
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
}
else if (lcd->lcd_info.pixel_format == RTGRAPHIC_PIXEL_FORMAT_ARGB888)
{
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_ARGB8888;
}
else if (lcd->lcd_info.pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB888)
{
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB888;
}
else if (lcd->lcd_info.pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB888)
{
pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB888;
}
else
{
LOG_E("unsupported pixel format");
return -RT_ERROR;
}
// g_ltdc_framebuf[0] = (uint32_t *)<dc_lcd_framebuf;
/* Start Address configuration : frame buffer is located at FLASH memory */
pLayerCfg.FBStartAdress = _lcd.lcd_info.framebuffer;
/* Alpha constant (255 totally opaque) */
pLayerCfg.Alpha = 255;
/* Default Color configuration (configure A,R,G,B component values) */
pLayerCfg.Alpha0 = 255;
pLayerCfg.Backcolor.Blue = 0;
pLayerCfg.Backcolor.Green = 0;
pLayerCfg.Backcolor.Red = 0;
/* Configure blending factors */
/* Constant Alpha value: pLayerCfg.Alpha / 255
C: Current Layer Color
Cs: Background color
BC = Constant Alpha x C + (1 - Constant Alpha ) x Cs */
/* BlendingFactor1: Pixel Alpha x Constant Alpha */
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_CA;
/* BlendingFactor2: 1 - (Pixel Alpha x Constant Alpha) */
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_CA;
/* Configure the number of lines and number of pixels per line */
pLayerCfg.ImageWidth = lcd->lcd_info.width;
pLayerCfg.ImageHeight = lcd->lcd_info.height;
/* Configure the LTDC */
if (HAL_LTDC_Init(&LtdcHandle) != HAL_OK)
{
LOG_E("LTDC init failed");
return -RT_ERROR;
}
/* Configure the Background Layer*/
if (HAL_LTDC_ConfigLayer(&LtdcHandle, &pLayerCfg, 0) != HAL_OK)
{
LOG_E("LTDC layer init failed");
return -RT_ERROR;
}
else
{
/* enable LTDC interrupt */
HAL_NVIC_SetPriority(LTDC_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(LTDC_IRQn);
LOG_D("LTDC init success");
return RT_EOK;
}
}
这时候就已经可以通过_lcd.lcd_info.framebuffer直接访问SDRAM了
如果有帮助请点赞收藏!
参考链接:
RT-Thread-GCC的链接脚本怎么增加一个类似bss的section呢RT-Thread问答社区 - RT-Thread