[STM32H7] STM32H745I-DISCO试用】移植LVGL

[复制链接]
915|2
 楼主| tlled 发表于 2025-3-9 09:02 | 显示全部楼层 |阅读模式



一、下载LVGL源码


下载地址:https://github.com/lvgl/lvgl/tree/release/v8.3
001.png

二、添加源码

2.1、复制LVGL源文件到工程文件
002.png


2.2、添加文件到工程
003.png

2.3、添加BSP文件
测试使用到BSP中的驱动程序
004.png


2.4、修改文件


2.4.1、lv_port_disp_templ.c
使用UTIL_LCD_SetPixel(x, y, color_p->full);函数
  1. /**
  2. * [url=home.php?mod=space&uid=288409]@file[/url] lv_port_disp_templ.c
  3. *
  4. */

  5. /*Copy this file as "lv_port_disp.c" and set this value to "1" to enable content*/
  6. #if 1

  7. /*********************
  8. *      INCLUDES
  9. *********************/
  10. #include "lv_port_disp_template.h"
  11. #include <stdbool.h>
  12. #include "main.h"

  13. /*********************
  14. *      DEFINES
  15. *********************/

  16. #define MY_DISP_HOR_RES    480
  17. #define MY_DISP_VER_RES    272
  18. /**********************
  19. *      TYPEDEFS
  20. **********************/

  21. /**********************
  22. *  STATIC PROTOTYPES
  23. **********************/
  24. static void disp_init(void);

  25. static void disp_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p);
  26. //static void gpu_fill(lv_disp_drv_t * disp_drv, lv_color_t * dest_buf, lv_coord_t dest_width,
  27. //        const lv_area_t * fill_area, lv_color_t color);

  28. /**********************
  29. *  STATIC VARIABLES
  30. **********************/

  31. /**********************
  32. *      MACROS
  33. **********************/

  34. /**********************
  35. *   GLOBAL FUNCTIONS
  36. **********************/

  37. void lv_port_disp_init(void)
  38. {
  39.     /*-------------------------
  40.      * Initialize your display
  41.      * -----------------------*/
  42.     disp_init();

  43.     /*-----------------------------
  44.      * Create a buffer for drawing
  45.      *----------------------------*/

  46.     /**
  47.      * LVGL requires a buffer where it internally draws the widgets.
  48.      * Later this buffer will passed to your display driver's `flush_cb` to copy its content to your display.
  49.      * The buffer has to be greater than 1 display row
  50.      *
  51.      * There are 3 buffering configurations:
  52.      * 1. Create ONE buffer:
  53.      *      LVGL will draw the display's content here and writes it to your display
  54.      *
  55.      * 2. Create TWO buffer:
  56.      *      LVGL will draw the display's content to a buffer and writes it your display.
  57.      *      You should use DMA to write the buffer's content to the display.
  58.      *      It will enable LVGL to draw the next part of the screen to the other buffer while
  59.      *      the data is being sent form the first buffer. It makes rendering and flushing parallel.
  60.      *
  61.      * 3. Double buffering
  62.      *      Set 2 screens sized buffers and set disp_drv.full_refresh = 1.
  63.      *      This way LVGL will always provide the whole rendered screen in `flush_cb`
  64.      *      and you only need to change the frame buffer's address.
  65.      */

  66.     /* Example for 1) */
  67.     static lv_disp_draw_buf_t draw_buf_dsc_1;
  68.     static lv_color_t buf_1[MY_DISP_HOR_RES * 10];                          /*A buffer for 10 rows*/
  69.     lv_disp_draw_buf_init(&draw_buf_dsc_1, buf_1, NULL, MY_DISP_HOR_RES * 10);   /*Initialize the display buffer*/

  70. //    /* Example for 2) */
  71. //    static lv_disp_draw_buf_t draw_buf_dsc_2;
  72. //    static lv_color_t buf_2_1[MY_DISP_HOR_RES * 10];                        /*A buffer for 10 rows*/
  73. //    static lv_color_t buf_2_2[MY_DISP_HOR_RES * 10];                        /*An other buffer for 10 rows*/
  74. //    lv_disp_draw_buf_init(&draw_buf_dsc_2, buf_2_1, buf_2_2, MY_DISP_HOR_RES * 10);   /*Initialize the display buffer*/

  75. //    /* Example for 3) also set disp_drv.full_refresh = 1 below*/
  76. //    static lv_disp_draw_buf_t draw_buf_dsc_3;
  77. //    static lv_color_t buf_3_1[MY_DISP_HOR_RES * MY_DISP_VER_RES];            /*A screen sized buffer*/
  78. //    static lv_color_t buf_3_2[MY_DISP_HOR_RES * MY_DISP_VER_RES];            /*Another screen sized buffer*/
  79. //    lv_disp_draw_buf_init(&draw_buf_dsc_3, buf_3_1, buf_3_2,
  80. //                          MY_DISP_VER_RES * LV_VER_RES_MAX);   /*Initialize the display buffer*/

  81.     /*-----------------------------------
  82.      * Register the display in LVGL
  83.      *----------------------------------*/

  84.     static lv_disp_drv_t disp_drv;                         /*Descriptor of a display driver*/
  85.     lv_disp_drv_init(&disp_drv);                    /*Basic initialization*/

  86.     /*Set up the functions to access to your display*/

  87.     /*Set the resolution of the display*/
  88.     disp_drv.hor_res = MY_DISP_HOR_RES;
  89.     disp_drv.ver_res = MY_DISP_VER_RES;

  90.     /*Used to copy the buffer's content to the display*/
  91.     disp_drv.flush_cb = disp_flush;

  92.     /*Set a display buffer*/
  93.     disp_drv.draw_buf = &draw_buf_dsc_1;

  94.     /*Required for Example 3)*/
  95.     //disp_drv.full_refresh = 1;

  96.     /* Fill a memory array with a color if you have GPU.
  97.      * Note that, in lv_conf.h you can enable GPUs that has built-in support in LVGL.
  98.      * But if you have a different GPU you can use with this callback.*/
  99.     //disp_drv.gpu_fill_cb = gpu_fill;

  100.     /*Finally register the driver*/
  101.     lv_disp_drv_register(&disp_drv);
  102. }

  103. /**********************
  104. *   STATIC FUNCTIONS
  105. **********************/

  106. /*Initialize your display and the required peripherals.*/
  107. static void disp_init(void)
  108. {

  109. }

  110. volatile bool disp_flush_enabled = true;

  111. /* Enable updating the screen (the flushing process) when disp_flush() is called by LVGL
  112. */
  113. void disp_enable_update(void)
  114. {
  115.     disp_flush_enabled = true;
  116. }

  117. /* Disable updating the screen (the flushing process) when disp_flush() is called by LVGL
  118. */
  119. void disp_disable_update(void)
  120. {
  121.     disp_flush_enabled = false;
  122. }

  123. /*Flush the content of the internal buffer the specific area on the display
  124. *You can use DMA or any hardware acceleration to do this operation in the background but
  125. *'lv_disp_flush_ready()' has to be called when finished.*/
  126. static void disp_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
  127. {
  128.     if(disp_flush_enabled) {
  129.         /*The most simple case (but also the slowest) to put all pixels to the screen one-by-one*/


  130.         int32_t x;
  131.         int32_t y;
  132.         for(y = area->y1; y <= area->y2; y++) {
  133.             for(x = area->x1; x <= area->x2; x++) {
  134.                 /*Put a pixel to the display. For example:*/
  135.                 /*put_px(x, y, *color_p)*/
  136.                                                                 UTIL_LCD_SetPixel(x, y, color_p->full);
  137.                 color_p++;
  138.             }
  139.         }
  140.     }
  141.        

  142.     /*IMPORTANT!!!
  143.      *Inform the graphics library that you are ready with the flushing*/
  144.     lv_disp_flush_ready(disp_drv);
  145. }


2.4.2、lv_conf.h
LV_COLOR_DEPTH改为32位
  1. /*
  2. * Copy this file as `lv_conf.h`
  3. * 1. simply next to the `lvgl` folder
  4. * 2. or any other places and
  5. *    - define `LV_CONF_INCLUDE_SIMPLE`
  6. *    - add the path as include path
  7. */

  8. /* clang-format off */
  9. #if 1 /*Set it to "1" to enable content*/

  10. #ifndef LV_CONF_H
  11. #define LV_CONF_H

  12. #include <stdint.h>

  13. /*====================
  14.    COLOR SETTINGS
  15. *====================*/

  16. #define LV_HOR_RES_MAX  480
  17. #define LV_VER_RES_MAX  272

  18. /*Color depth: 1 (1 byte per pixel), 8 (RGB332), 16 (RGB565), 32 (ARGB8888)*/
  19. #define LV_COLOR_DEPTH 32

  20. /*Swap the 2 bytes of RGB565 color. Useful if the display has an 8-bit interface (e.g. SPI)*/
  21. #define LV_COLOR_16_SWAP 0

  22. /*Enable features to draw on transparent background.
  23. *It's required if opa, and transform_* style properties are used.
  24. *Can be also used if the UI is above another layer, e.g. an OSD menu or video player.*/
  25. #define LV_COLOR_SCREEN_TRANSP 0

  26. /* Adjust color mix functions rounding. GPUs might calculate color mix (blending) differently.
  27. * 0: round down, 64: round up from x.75, 128: round up from half, 192: round up from x.25, 254: round up */
  28. #define LV_COLOR_MIX_ROUND_OFS 0

  29. /*Images pixels with this color will not be drawn if they are chroma keyed)*/
  30. #define LV_COLOR_CHROMA_KEY lv_color_hex(0x00ff00)         /*pure green*/

2.4.3、main.c
  1. #include "main.h"
  2. static void MPU_Config(void);
  3. static void SystemClock_Config(void);
  4. static void CPU_CACHE_Enable(void);
  5. static void Error_Handler(void);

  6. int main(void)
  7. {
  8.         uint32_t i=0;
  9.   MPU_Config();
  10.   CPU_CACHE_Enable();
  11.   HAL_Init();
  12.   SystemClock_Config();

  13.         init_delay(400);
  14.         init_led();
  15.         init_uart3(115200);

  16.         BSP_SDRAM_Init(0);
  17.         BSP_LCD_Init(0, LCD_ORIENTATION_LANDSCAPE);
  18.         UTIL_LCD_SetFuncDriver(&LCD_Driver);
  19.         UTIL_LCD_Clear(UTIL_LCD_COLOR_RED);

  20.         lv_init();
  21.         lv_port_disp_init();
  22.         lv_demo_stress();
  23.   while (1)
  24.   {
  25.                 HAL_Delay(5);
  26.                 lv_task_handler();
  27.   }
  28. }

2.4.4、添加心跳函数
我这里在滴答定时器中增加,也可以创建其他定时器1MS中断增加。
  1. void SysTick_Handler(void)
  2. {
  3.         lv_tick_inc(1);
  4.   HAL_IncTick();
  5. }


三、程序运行
下载程序后,显示屏显示内容
lvgl.gif


yangjiaxu 发表于 2025-4-9 14:34 | 显示全部楼层
可以更丰富一些,感觉关于H7移植lvgl的教程不是太多,这种是很宝贵的教程
dongnanxibei 发表于 2025-4-10 10:39 | 显示全部楼层
还是LVGL好用啊, 界面美观。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

701

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部