1.. _lcd_stm32_guide: 2 3========================================================================= 4Step-by-step Guide: How to use the LVGL v9 LCD drivers with STM32 devices 5========================================================================= 6 7Introduction 8------------ 9 10This guide is intended to be a step-by-step instruction of how to configure the STM32Cube HAL with the new TFT-LCD display drivers introduced in LVGL v9.0. The example code has been tested on the STM32F746-based Nucleo-F746ZG board with an ST7789-based LCD panel connected via SPI. The application itself and the hardware configuration code were generated with the STM32CubeIDE 1.14.0 tool. 11 12.. tip:: 13 ST Micro provide their own TFT-LCD drivers in their X-CUBE-DISPLAY Software Extension Package. While these drivers can be used with LVGL as well, the LVGL LCD drivers do not depend on this package. 14 15 The LVGL LCD drivers are meant as an alternative, simple to use API to implement LCD support for your LVGL-based project on any platform. Moreover, even in the initial release we support more LCD controllers than X-CUBE-DISPLAY currently provides, and we plan to add support for even more LCD controllers in the future. 16 17 Please note however, that – unlike X-CUBE-DISPLAY – the LVGL LCD drivers do not implement the communication part, whether SPI, parallel i8080 bus or other. It is the user's responsibility to implement – and optimize – these on their chosen platform. LVGL will only provide examples for the most popular platforms. 18 19By following the steps you will have a fully functional program, which can be used as the foundation of your own LVGL-based project. If you are in a hurry and not interested in the details, you can find the final project `here <https://github.com/lvgl/lv_port_lcd_stm32>`__. You will only need to configure LVGL to use the driver corresponding to your hardware (if it is other than the ST7789), and implement the function ``ui_init()`` to create your widgets. 20 21.. note:: 22 23 This example is not meant as the best possible implementation, or the recommended solution. It relies solely on the HAL drivers provided by ST Micro, which favor portability over performance. Despite of this the performance is very good, thanks to the efficient, DMA-based implementation of the drivers. 24 25.. note:: 26 27 Although the example uses FreeRTOS, this is not a strict requirement with the LVGL LCD display drivers. 28 29You can find the source code snippets of this guide in the `lv_port_lcd_stm32_template.c <https://github.com/lvgl/lvgl/examples/porting/lv_port_lcd_stm32_template.c>`__ example. 30 31Hardware configuration 32---------------------- 33 34In this example we'll use the SPI1 peripheral to connect the microcontroller to the LCD panel. Besides the hardware-controlled SPI pins SCK and MOSI we need some additional output pins for the chip select, command/data select, and LCD reset: 35 36==== ============= ======= ========== 37pin configuration LCD user label 38==== ============= ======= ========== 39PA4 GPIO_Output CS LCD_CS 40PA5 SPI1_SCK SCK -- 41PA7 SPI1_MOSI SDI -- 42PA15 GPIO_Output RESET LCD_RESET 43PB10 GPIO_Output DC LCD_DCX 44==== ============= ======= ========== 45 46Step-by-step instructions 47------------------------- 48 49#. Create new project in File/New/STM32 Project. 50#. Select target processor/board. 51#. Set project name and location. 52#. Set Targeted Project Type to STM32Cube and press Finish. 53#. Say "Yes" to Initialize peripherals with their default Mode? After the project is created, the configuration file (.ioc) is opened automatically. 54#. Switch to the Pinout & Configuration tab. 55#. In the System Core category switch to RCC. 56#. Set High Speed Clock to "BYPASS Clock Source", and Low Speed Clock to "Crystal/Ceramic Resonator". 57#. In the System Core category select SYS, and set Timebase Source to other than SysTick (in our example, TIM2). 58#. Switch to the Clock Configuration tab. 59#. Set the HCLK clock frequency to the maximum value (216 MHz for the STM32F746). 60#. Switch back to the Pinout & Configuration tab, and in the Middleware and Software Packs category select FREERTOS. 61#. Select Interface: CMSIS_V1. 62#. In the Advanced Settings tab enable USE_NEWLIB_REENTRANT. We are finished here. 63#. In the Pinout view configure PA5 as SPI1_SCK, PA7 as SPI1_MOSI (right click the pin and select the function). 64#. In the Pinout & Configuration/Connectivity category select SPI1. 65#. Set Mode to Transmit Only Master, and Hardware NSS Signal to Disable. 66#. In the Configuration subwindow switch to Parameter Settings. 67#. Set Frame Format to Motorola, Data Size to 8 Bits, First Bit to MSB First. 68#. Set the Prescaler to the maximum value according to the LCD controller’s datasheet (e.g., 15 MBits/s). Set CPOL/CPHA as required (leave as default). 69#. Set NSSP Mode to Disabled and NSS Signal Type to Software. 70#. In DMA Settings add a new Request for SPI1_TX (when using SPI1). 71#. Set Priority to Medium, Data Width to Half Word. 72#. In NVIC Settings enable SPI1 global interrupt. 73#. In GPIO Settings set SPI1_SCK to Pull-down and Very High output speed and set the User Label to ``LCD_SCK``. 74#. Set SPI1_MOSI to Pull-up and Very High, and name it ``LCD_SDI``. 75#. Select System Core/GPIO category. In the Pinout view configure additional pins for chip select, reset and command/data select. Name them ``LCD_CS``, ``LCD_RESET`` and ``LCD_DCX``, respectively. Configure them as GPIO Output. (In this example we will use PA4 for ``LCD_CS``, PA15 for ``LCD_RESET`` and PB10 for ``LCD_DCX``.) 76#. Set ``LCD_CS`` to No pull-up and no pull-down, Low level and Very High speed. 77#. Set ``LCD_RESET`` to Pull-up and High level. 78#. Set ``LCD_DCX`` to No pull-up and no pull-down, High level and Very High speed. 79#. Open the Project Manager tab, and select Advanced Settings. On the right hand side there is a Register Callback window. Select SPI and set it to ENABLE. 80#. We are ready with the hardware configuration. Save the configuration and let STM32Cube generate the source. 81#. In the project tree clone the LVGL repository into the Middlewares/Third_Party folder (this tutorial uses the release/v9.0 branch of LVGL): 82 83 .. code-block:: dosbatch 84 85 git clone https://github.com/lvgl/lvgl.git -b release/v9.0 86 87#. Cloning should create an 'lvgl' subfolder inside the 'Third_Party' folder. From the 'lvgl' folder copy 'lv_conf_template.h' into the 'Middlewares' folder, and rename it to 'lv_conf.h'. Refresh the project tree. 88#. Open 'lv_conf.h', and in line 15 change ``#if 0`` to ``#if 1``. 89#. Search for the string ``LV_USE_ST7735``, and enable the appropriate LCD driver by setting its value to 1. This example uses the ST7789 driver: 90 91 .. code-block:: c 92 93 #define LV_USE_ST7789 1 94 95#. Right click the folder 'Middlewares/Third_Party/lvgl/tests', select Resource Configurations/Exclude from Build..., check both Debug and Release, then press OK. 96#. Right click the project name and select "Properties". In the C/C++ Build/Settings panel select MCU GCC Compiler/Include paths. In the Configuration dropdown select [ All configurations ]. Add the following Include path: 97 98 .. code-block:: c 99 100 ../Middlewares/Third_Party/lvgl 101 102#. Open Core/Src/stm32xxx_it.c (the file name depends on the processor variation). Add 'lv_tick.h' to the Private includes section: 103 104 .. code-block:: c 105 106 /* Private includes ----------------------------------------------------------*/ 107 /* USER CODE BEGIN Includes */ 108 #include "./src/tick/lv_tick.h" 109 /* USER CODE END Includes */ 110 111#. Find the function ``TIM2_IRQHandler``. Add a call to ``lv_tick_inc()``: 112 113 .. code-block:: c 114 115 void TIM2_IRQHandler(void) 116 { 117 /* USER CODE BEGIN TIM2_IRQn 0 */ 118 119 /* USER CODE END TIM2_IRQn 0 */ 120 HAL_TIM_IRQHandler(&htim2); 121 /* USER CODE BEGIN TIM2_IRQn 1 */ 122 lv_tick_inc(1); 123 /* USER CODE END TIM2_IRQn 1 */ 124 } 125 126 127#. Save the file, then open Core/Src/main.c. Add the following lines to the Private includes (if your LCD uses other than the ST7789, replace the driver path and header with the appropriate one): 128 129 .. code-block:: c 130 131 /* Private includes ----------------------------------------------------------*/ 132 /* USER CODE BEGIN Includes */ 133 #include "lvgl.h" 134 #include "./src/drivers/display/st7789/lv_st7789.h" 135 /* USER CODE END Includes */ 136 137#. Add the following lines to Private defines (change them according to your LCD specs): 138 139 .. code-block:: c 140 141 #define LCD_H_RES 240 142 #define LCD_V_RES 320 143 #define BUS_SPI1_POLL_TIMEOUT 0x1000U 144 145 146#. Add the following lines to the Private variables: 147 148 .. code-block:: c 149 150 osThreadId LvglTaskHandle; 151 lv_display_t *lcd_disp; 152 volatile int lcd_bus_busy = 0; 153 154#. Add the following line to the Private function prototypes: 155 156 .. code-block:: c 157 158 void ui_init(lv_display_t *disp); 159 void LVGL_Task(void const *argument); 160 161#. Add the following lines after USER CODE BEGIN RTOS_THREADS: 162 163 .. code-block:: c 164 165 osThreadDef(LvglTask, LVGL_Task, osPriorityIdle, 0, 1024); 166 LvglTaskHandle = osThreadCreate(osThread(LvglTask), NULL); 167 168#. Copy and paste the hardware initialization and the transfer callback functions from the example code after USER CODE BEGIN 4: 169 170 .. code-block:: c 171 172 /* USER CODE BEGIN 4 */ 173 174 void lcd_color_transfer_ready_cb(SPI_HandleTypeDef *hspi) 175 { 176 /* CS high */ 177 HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_SET); 178 lcd_bus_busy = 0; 179 lv_display_flush_ready(lcd_disp); 180 } 181 182 /* Initialize LCD I/O bus, reset LCD */ 183 static int32_t lcd_io_init(void) 184 { 185 /* Register SPI Tx Complete Callback */ 186 HAL_SPI_RegisterCallback(&hspi1, HAL_SPI_TX_COMPLETE_CB_ID, lcd_color_transfer_ready_cb); 187 188 /* reset LCD */ 189 HAL_GPIO_WritePin(LCD_RESET_GPIO_Port, LCD_RESET_Pin, GPIO_PIN_RESET); 190 HAL_Delay(100); 191 HAL_GPIO_WritePin(LCD_RESET_GPIO_Port, LCD_RESET_Pin, GPIO_PIN_SET); 192 HAL_Delay(100); 193 194 HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_SET); 195 HAL_GPIO_WritePin(LCD_DCX_GPIO_Port, LCD_DCX_Pin, GPIO_PIN_SET); 196 197 return HAL_OK; 198 } 199 200 /* Platform-specific implementation of the LCD send command function. In general this should use polling transfer. */ 201 static void lcd_send_cmd(lv_display_t *disp, const uint8_t *cmd, size_t cmd_size, const uint8_t *param, size_t param_size) 202 { 203 LV_UNUSED(disp); 204 while (lcd_bus_busy); /* wait until previous transfer is finished */ 205 /* Set the SPI in 8-bit mode */ 206 hspi1.Init.DataSize = SPI_DATASIZE_8BIT; 207 HAL_SPI_Init(&hspi1); 208 /* DCX low (command) */ 209 HAL_GPIO_WritePin(LCD_DCX_GPIO_Port, LCD_DCX_Pin, GPIO_PIN_RESET); 210 /* CS low */ 211 HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_RESET); 212 /* send command */ 213 if (HAL_SPI_Transmit(&hspi1, cmd, cmd_size, BUS_SPI1_POLL_TIMEOUT) == HAL_OK) { 214 /* DCX high (data) */ 215 HAL_GPIO_WritePin(LCD_DCX_GPIO_Port, LCD_DCX_Pin, GPIO_PIN_SET); 216 /* for short data blocks we use polling transfer */ 217 HAL_SPI_Transmit(&hspi1, (uint8_t *)param, (uint16_t)param_size, BUS_SPI1_POLL_TIMEOUT); 218 /* CS high */ 219 HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_SET); 220 } 221 } 222 223 /* Platform-specific implementation of the LCD send color function. For better performance this should use DMA transfer. 224 * In case of a DMA transfer a callback must be installed to notify LVGL about the end of the transfer. 225 */ 226 static void lcd_send_color(lv_display_t *disp, const uint8_t *cmd, size_t cmd_size, uint8_t *param, size_t param_size) 227 { 228 LV_UNUSED(disp); 229 while (lcd_bus_busy); /* wait until previous transfer is finished */ 230 /* Set the SPI in 8-bit mode */ 231 hspi1.Init.DataSize = SPI_DATASIZE_8BIT; 232 HAL_SPI_Init(&hspi1); 233 /* DCX low (command) */ 234 HAL_GPIO_WritePin(LCD_DCX_GPIO_Port, LCD_DCX_Pin, GPIO_PIN_RESET); 235 /* CS low */ 236 HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_RESET); 237 /* send command */ 238 if (HAL_SPI_Transmit(&hspi1, cmd, cmd_size, BUS_SPI1_POLL_TIMEOUT) == HAL_OK) { 239 /* DCX high (data) */ 240 HAL_GPIO_WritePin(LCD_DCX_GPIO_Port, LCD_DCX_Pin, GPIO_PIN_SET); 241 /* for color data use DMA transfer */ 242 /* Set the SPI in 16-bit mode to match endianness */ 243 hspi1.Init.DataSize = SPI_DATASIZE_16BIT; 244 HAL_SPI_Init(&hspi1); 245 lcd_bus_busy = 1; 246 HAL_SPI_Transmit_DMA(&hspi1, param, (uint16_t)param_size / 2); 247 /* NOTE: CS will be reset in the transfer ready callback */ 248 } 249 } 250 251#. Add the LVGL_Task() function. Replace the ``lv_st7789_create()`` call with the appropriate driver. You can change the default orientation by adjusting the parameter of ``lv_display_set_rotation()``. You will also need to create the display buffers here. This example uses a double buffering scheme with 1/10th size partial buffers. In most cases this is a good compromise between the required memory size and performance, but you are free to experiment with other settings. 252 253 .. code-block:: c 254 255 void LVGL_Task(void const *argument) 256 { 257 /* Initialize LVGL */ 258 lv_init(); 259 260 /* Initialize LCD I/O */ 261 if (lcd_io_init() != 0) 262 return; 263 264 /* Create the LVGL display object and the LCD display driver */ 265 lcd_disp = lv_st7789_create(LCD_H_RES, LCD_V_RES, LV_LCD_FLAG_NONE, lcd_send_cmd, lcd_send_color); 266 lv_display_set_rotation(lcd_disp, LV_DISPLAY_ROTATION_270); 267 268 /* Allocate draw buffers on the heap. In this example we use two partial buffers of 1/10th size of the screen */ 269 lv_color_t * buf1 = NULL; 270 lv_color_t * buf2 = NULL; 271 272 uint32_t buf_size = LCD_H_RES * LCD_V_RES / 10 * lv_color_format_get_size(lv_display_get_color_format(lcd_disp)); 273 274 buf1 = lv_malloc(buf_size); 275 if(buf1 == NULL) { 276 LV_LOG_ERROR("display draw buffer malloc failed"); 277 return; 278 } 279 280 buf2 = lv_malloc(buf_size); 281 if(buf2 == NULL) { 282 LV_LOG_ERROR("display buffer malloc failed"); 283 lv_free(buf1); 284 return; 285 } 286 lv_display_set_buffers(lcd_disp, buf1, buf2, buf_size, LV_DISPLAY_RENDER_MODE_PARTIAL); 287 288 ui_init(lcd_disp); 289 290 for(;;) { 291 /* The task running lv_timer_handler should have lower priority than that running `lv_tick_inc` */ 292 lv_timer_handler(); 293 /* raise the task priority of LVGL and/or reduce the handler period can improve the performance */ 294 osDelay(10); 295 } 296 } 297 298#. All that's left is to implement ``ui_init()`` to create the screen. Here's a simple "Hello World" example: 299 300 .. code-block:: c 301 302 void ui_init(lv_display_t *disp) 303 { 304 lv_obj_t *obj; 305 306 /* set screen background to white */ 307 lv_obj_t *scr = lv_screen_active(); 308 lv_obj_set_style_bg_color(scr, lv_color_white(), 0); 309 lv_obj_set_style_bg_opa(scr, LV_OPA_100, 0); 310 311 /* create label */ 312 obj = lv_label_create(scr); 313 lv_obj_set_align(widget, LV_ALIGN_CENTER); 314 lv_obj_set_height(widget, LV_SIZE_CONTENT); 315 lv_obj_set_width(widget, LV_SIZE_CONTENT); 316 lv_obj_set_style_text_font(widget, &lv_font_montserrat_14, 0); 317 lv_obj_set_style_text_color(widget, lv_color_black(), 0); 318 lv_label_set_text(widget, "Hello World!"); 319 } 320 321