Description
Hello!
This is my first experience with LVGL, so I don’t quite understand how it works yet, forgive me in advance for my stupid mistakes.
I am using a stm32h743vit6 microcontroller on a devebox board and a st7789 display with 3-pin spi without touchscreen.
I use half-duplex master mode spi with DMA, I wrote the display driver functions myself in main.c, I tested them separately, and they worked as they should.
The program compiles, but when I run the program, the display only initializes.
What MCU/Processor/Board and compiler are you using?
smt32h743vit6 on board DevEBox/ STM32CubeIDE
What LVGL version are you using?
master(latest)
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lvgl.h"
#include <stdio.h>
#define TFT_CS_PIN GPIO_PIN_4 // Пример: Пин CS
#define TFT_DC_PIN GPIO_PIN_5 // Пример: Пин DC
#define TFT_RST_PIN GPIO_PIN_6 // Пример: Пин RST
#define ST7789_HEIGHT 240
#define ST7789_WIDTH 320
#define BUFFER_SIZE ST7789_HEIGHT*ST7789_WIDTH*2
#define HW_BUFFER_SIZE 320*80
#define BYTES_PER_PIXEL (LV_COLOR_FORMAT_GET_SIZE(LV_COLOR_FORMAT_RGB565)) /* will be 2 for RGB565 */
static uint8_t buf_1[HW_BUFFER_SIZE*BYTES_PER_PIXEL];
static uint8_t buf_2[HW_BUFFER_SIZE*BYTES_PER_PIXEL];
lv_display_t * disp;
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
DMA_HandleTypeDef hdma_spi1_tx;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MPU_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_SPI1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void ST7789_IntToByte(uint16_t data1, uint8_t data2[]){
*data2 = (uint8_t)(data1>>8);
*(data2+1) = (uint8_t)(data1&0xFF);
}
void SPI_SendCommand(uint8_t cmd) {
HAL_GPIO_WritePin(GPIOB, TFT_CS_PIN, GPIO_PIN_RESET); // CS LOW
HAL_GPIO_WritePin(GPIOB, TFT_DC_PIN, GPIO_PIN_RESET); // Установите DC в 0 для команды
HAL_SPI_Transmit(&hspi1, &cmd, 1, 100); // Отправьте команду
HAL_GPIO_WritePin(GPIOB, TFT_CS_PIN, GPIO_PIN_SET); // CS HIGH
}
void SPI_SendData(uint8_t *data, uint32_t size, _Bool isDMA) {
HAL_GPIO_WritePin(GPIOB, TFT_CS_PIN, GPIO_PIN_RESET); // CS LOW
HAL_GPIO_WritePin(GPIOB, TFT_DC_PIN, GPIO_PIN_SET); // Установите DC в 1 для данных
if (!isDMA) HAL_SPI_Transmit(&hspi1, data, size, 500); // Отправьте данные
else {
HAL_SPI_Transmit_DMA(&hspi1, data, size);
HAL_GPIO_WritePin(GPIOB, TFT_CS_PIN, GPIO_PIN_SET); // CS HIGH
}
}
void ST7789_Init() {
HAL_GPIO_WritePin(GPIOB, TFT_RST_PIN, GPIO_PIN_RESET); // Сброс дисплея
HAL_Delay(200);
HAL_GPIO_WritePin(GPIOB, TFT_RST_PIN, GPIO_PIN_SET);
HAL_Delay(200);
SPI_SendCommand(0x01); // SWRESET
HAL_Delay(200);
SPI_SendCommand(0x11); // SLPOUT
HAL_Delay(500);
SPI_SendCommand(0x3A); // COLMOD
uint8_t color_mode = 0x05; // 16 битный цвет (RGB 565)
SPI_SendData(&color_mode, 1, 0);
SPI_SendCommand(0x36); // MADCTL
uint8_t madctl = 0x14; // Настройка направления (обычно 0x00)
SPI_SendData(&madctl, 1, 0);
SPI_SendCommand(0x21);
SPI_SendCommand(0x13);
SPI_SendCommand(0x29); // DISPON
}
void ST7789_FillScreen(uint16_t color) {
uint8_t data[HW_BUFFER_SIZE * 2]; // Массив для заполнения экрана
for (uint32_t i = 0; i < HW_BUFFER_SIZE; i++) {
ST7789_IntToByte(color, &data[2 * i]);
}
SPI_SendCommand(0x2A); // CASET
uint8_t caset[] = {0x00, 0x00, 0x00, 0xEF}; // Установите область по X
SPI_SendData(caset, sizeof(caset), 0);
SPI_SendCommand(0x2B); // RASET
uint8_t raset[] = {0x00, 0x00, 0x01, 0x3F}; // Установите область по Y
SPI_SendData(raset, sizeof(raset), 0);
SPI_SendCommand(0x2C); // RAMWR
for (int i = 0; i < BUFFER_SIZE / (HW_BUFFER_SIZE * 2); i++) {
SPI_SendData(data, sizeof(data), 1); // Отправка данных на дисплей
}
}
void ST7789_MakePicture(uint8_t data[], uint32_t sizeofdata, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
uint8_t caset[4];
uint8_t raset[4];
ST7789_IntToByte(x1, caset);
ST7789_IntToByte(y1, raset);
ST7789_IntToByte(x2, caset+2);
ST7789_IntToByte(y2, raset+2);
SPI_SendCommand(0x2A); // CASET
SPI_SendData(caset, sizeof(caset), 0);
SPI_SendCommand(0x2B); // RASET
SPI_SendData(raset, sizeof(raset), 0);
SPI_SendCommand(0x2C); // RAMWR
for (uint32_t i = 0; i < sizeofdata; i += (240 * 80 * 2)) {
uint16_t chunk_size = (sizeofdata - i > (240 * 80 * 2)) ? (240 * 80 * 2) : (sizeofdata - i);
SPI_SendData(data + i, chunk_size, 1);
}
}
void my_flush_cb(lv_display_t * disp, const lv_area_t * area, lv_color_t * color_p)
{
uint16_t height = area->y2 - area->y1 + 1;
uint16_t width = area->x2 - area->x1 + 1;
uint8_t buff[height * width * 2];
//Write colour to each pixel
for (int i = 0; i < width * height; i++) {
uint16_t color_full = (color_p->red << 11) | (color_p->green << 5) | (color_p->blue);
ST7789_IntToByte(color_full,&buff[i * 2]);
color_p++;
}
ST7789_MakePicture(buff, (uint32_t)(height * width * 2), area->x1, area->y1, area->x2, area->y2);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_SPI1_Init();
/* USER CODE BEGIN 2 */
//Initialise LVGL UI library
lv_init();
ST7789_Init();
disp = lv_display_create(ST7789_WIDTH, ST7789_HEIGHT); /* Basic initialization with horizontal and vertical resolution in pixels */
lv_display_set_flush_cb(disp, my_flush_cb); /* Set a flush callback to draw to the display */
lv_display_set_buffers(disp, buf_1, buf_2, sizeof(buf_1), LV_DISPLAY_RENDER_MODE_PARTIAL); /* Set an initialized buffer */
/* Change Active Screen's background color */
lv_obj_set_style_bg_color(lv_screen_active(), lv_color_hex(0x003a57), LV_PART_MAIN);
lv_obj_set_style_text_color(lv_screen_active(), lv_color_hex(0xffffff), LV_PART_MAIN);
/* Create a spinner */
lv_obj_t * spinner = lv_spinner_create(lv_screen_active());
lv_obj_set_size(spinner, 64, 64);
lv_obj_align(spinner, LV_ALIGN_BOTTOM_MID, 0, 0);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
lv_timer_handler();
HAL_Delay(10);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Supply configuration update enable
*/
HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 12;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
RCC_OscInitStruct.PLL.PLLFRACN = 4096;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief SPI1 Initialization Function
* @param None
* @retval None
*/
static void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_1LINE;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 0x0;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
/**
* Enable DMA controller clock
*/
static void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Stream0_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6, GPIO_PIN_RESET);
/*Configure GPIO pins : PB4 PB5 PB6 */
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{
if (hspi->Instance == SPI1)
{
//queue = 0;
lv_disp_flush_ready(disp);
}
}
/* USER CODE END 4 */
/* MPU Configuration */
void MPU_Config(void)
{
MPU_Region_InitTypeDef MPU_InitStruct = {0};
/* Disables the MPU */
HAL_MPU_Disable();
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.BaseAddress = 0x0;
MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
MPU_InitStruct.SubRegionDisable = 0x87;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/* Enables the MPU */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */