Debian Codegraph @

lvgl/docs/src/integration/chip_vendors/stm32/lcd_stm32_guide.rst

raw
1 .. _lcd_stm32_guide:
2
3 =====================================
4 SPI Display Driver Creation for STM32
5 =====================================
6
7 Display Driver
8 --------------
9
10 Here is how you can drive a RGB565 240x320 SPI display using STM32 HAL SPI.
11 You can use direct or partial mode. Single-buffered direct is shown in this example.
12
13 In your initialization code, create the display and set the buffers and flush callback.
14
15 .. code-block:: c
16
17 static uint8_t fb[240 * 320 * 2];
18
19 lv_display_t * disp = lv_display_create(240, 320);
20 lv_display_set_buffers(disp, fb, NULL, sizeof(fb), LV_DISPLAY_RENDER_MODE_DIRECT);
21 lv_display_set_flush_cb(disp, flush_cb);
22 lv_display_set_color_format(disp, LV_COLOR_FORMAT_RGB565); /* not needed if LV_COLOR_DEPTH is 16 */
23
24 The generated STM32 HAL code should initialize the SPI peripheral for you based
25 on the configuration values in STM32CubeIDE or STM32CubeMX.
26
27 If there are any commands that should be sent to initialize the display, send them before
28 entering the timer loop.
29
30 Here is an example flush callback implementation.
31
32 .. code-block:: c
33
34 extern SPI_HandleTypeDef hspi1;
35
36 static void flush_cb(lv_display_t * disp, const lv_area_t * area, uint8_t * px_map)
37 {
38 if(lv_display_flush_is_last(disp)) {
39 /* Chip select (CS) pins are typically active-low. */
40 HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_RESET);
41 HAL_SPI_Transmit(&hspi1, px_map, lv_area_get_size(area) * 2, HAL_MAX_DELAY);
42 HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_SET);
43 }
44 lv_display_flush_ready(disp);
45 }
46
47 Performance can be improved by using DMA with double buffering.
48
49
50 Touch indev
51 -----------
52
53 In your initialization code, create the touch screen indev.
54
55 .. code-block:: c
56
57 lv_indev_t * indev = lv_indev_create();
58 lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
59 lv_indev_set_read_cb(indev, touch_read);
60
61 The generated STM32 HAL code should initialize the I2C peripheral for you based
62 on the configuration values in STM32CubeIDE or STM32CubeMX.
63
64 Here is an example implementation of a touch driver that uses i2c
65 communication and an interrupt pin to signal available data.
66
67 .. code-block:: c
68
69 extern I2C_HandleTypeDef hi2c2;
70
71 static volatile bool do_sample_touch = false;
72 static lv_indev_state_t last_state = LV_INDEV_STATE_RELEASED;
73
74 /* the STM32 HAL calls this function for you */
75 void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
76 {
77 if (GPIO_Pin == TP_IRQ_Pin)
78 {
79 do_sample_touch = true;
80 }
81 }
82
83 static void touch_read(lv_indev_t * indev, lv_indev_data_t * data)
84 {
85 NVIC_DisableIRQ(EXTI5_IRQn);
86 if (do_sample_touch)
87 {
88 uint8_t touches = 0;
89 uint8_t buf[6];
90 const uint16_t STATUS_REG = 0x814E;
91 const uint16_t TOUCH_POS_REG = 0x8150;
92 uint8_t ZERO = 0;
93
94 HAL_I2C_Mem_Read(&hi2c2, 0xBA, STATUS_REG, 2, buf, 1, HAL_MAX_DELAY);
95 touches = (0x0F & buf[0]);
96
97 HAL_I2C_Mem_Write(&hi2c2, 0xBA, STATUS_REG, 2, &ZERO, 1, HAL_MAX_DELAY);
98
99 do_sample_touch = false;
100
101 if (touches > 0)
102 {
103 last_state = LV_INDEV_STATE_PRESSED;
104
105 HAL_I2C_Mem_Read(&hi2c2, 0xBA, TOUCH_POS_REG, 2, buf, 4, HAL_MAX_DELAY);
106 data->point.x = buf[0] + (buf[1] << 8);
107 data->point.y = buf[2] + (buf[3] << 8);
108 }
109 else {
110 last_state = LV_INDEV_STATE_RELEASED;
111 }
112 }
113 NVIC_EnableIRQ(EXTI5_IRQn);
114
115 data->state = last_state;
116 }