forked from lvgl/lvgl_esp32_drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lvgl_helpers.c
240 lines (189 loc) · 6.22 KB
/
lvgl_helpers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* @file lvgl_helpers.c
*
*/
/*********************
* INCLUDES
*********************/
#include "sdkconfig.h"
#include "lvgl_helpers.h"
#include "esp_log.h"
#include "lvgl_tft/disp_spi.h"
#include "lvgl_touch/tp_spi.h"
#include "lvgl_spi_conf.h"
#include "lvgl_i2c_conf.h"
#include "driver/i2c.h"
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
#include "src/lv_core/lv_refr.h"
#else
#include "lvgl/src/lv_core/lv_refr.h"
#endif
/*********************
* DEFINES
*********************/
#define TAG "lvgl_helpers"
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/* Interface and driver initialization */
void lvgl_driver_init(void)
{
ESP_LOGI(TAG, "Display hor size: %d, ver size: %d", LV_HOR_RES_MAX, LV_VER_RES_MAX);
ESP_LOGI(TAG, "Display buffer size: %d", DISP_BUF_SIZE);
#if defined (CONFIG_LV_TFT_DISPLAY_CONTROLLER_FT81X)
ESP_LOGI(TAG, "Initializing SPI master for FT81X");
lvgl_spi_driver_init(TFT_SPI_HOST,
DISP_SPI_MISO, DISP_SPI_MOSI, DISP_SPI_CLK,
SPI_BUS_MAX_TRANSFER_SZ, 1,
DISP_SPI_IO2, DISP_SPI_IO3);
disp_spi_add_device(TFT_SPI_HOST);
disp_driver_init();
#if defined (CONFIG_LV_TOUCH_CONTROLLER_FT81X)
touch_driver_init();
#endif
return;
#endif
#if defined (SHARED_SPI_BUS)
ESP_LOGI(TAG, "Initializing shared SPI master");
lvgl_spi_driver_init(TFT_SPI_HOST,
TP_SPI_MISO, DISP_SPI_MOSI, DISP_SPI_CLK,
SPI_BUS_MAX_TRANSFER_SZ, 1,
-1, -1);
disp_spi_add_device(TFT_SPI_HOST);
tp_spi_add_device(TOUCH_SPI_HOST);
disp_driver_init();
touch_driver_init();
return;
#endif
#if defined (SHARED_I2C_BUS)
ESP_LOGI(TAG, "Initializing shared I2C master");
lvgl_i2c_driver_init(DISP_I2C_PORT,
DISP_I2C_SDA, DISP_I2C_SCL,
DISP_I2C_SPEED_HZ);
disp_driver_init();
touch_driver_init();
return;
#endif
/* Display controller initialization */
#if defined CONFIG_LV_TFT_DISPLAY_PROTOCOL_SPI
ESP_LOGI(TAG, "Initializing SPI master for display");
lvgl_spi_driver_init(TFT_SPI_HOST,
DISP_SPI_MISO, DISP_SPI_MOSI, DISP_SPI_CLK,
SPI_BUS_MAX_TRANSFER_SZ, 1,
DISP_SPI_IO2, DISP_SPI_IO3);
disp_spi_add_device(TFT_SPI_HOST);
disp_driver_init();
#elif defined (CONFIG_LV_TFT_DISPLAY_PROTOCOL_I2C)
ESP_LOGI(TAG, "Initializing I2C master for display");
/* Init the i2c master on the display driver code */
lvgl_i2c_driver_init(DISP_I2C_PORT,
DISP_I2C_SDA, DISP_I2C_SCL,
DISP_I2C_SPEED_HZ);
disp_driver_init();
#else
#error "No protocol defined for display controller"
#endif
/* Touch controller initialization */
#if CONFIG_LV_TOUCH_CONTROLLER != TOUCH_CONTROLLER_NONE
#if defined (CONFIG_LV_TOUCH_DRIVER_PROTOCOL_SPI)
ESP_LOGI(TAG, "Initializing SPI master for touch");
lvgl_spi_driver_init(TOUCH_SPI_HOST,
TP_SPI_MISO, TP_SPI_MOSI, TP_SPI_CLK,
0 /* Defaults to 4094 */, 2,
-1, -1);
tp_spi_add_device(TOUCH_SPI_HOST);
touch_driver_init();
#elif defined (CONFIG_LV_TOUCH_DRIVER_PROTOCOL_I2C)
ESP_LOGI(TAG, "Initializing I2C master for touch");
lvgl_i2c_driver_init(TOUCH_I2C_PORT,
TOUCH_I2C_SDA, TOUCH_I2C_SCL,
TOUCH_I2C_SPEED_HZ);
touch_driver_init();
#elif defined (CONFIG_LV_TOUCH_DRIVER_ADC)
touch_driver_init();
#elif defined (CONFIG_LV_TOUCH_DRIVER_DISPLAY)
touch_driver_init();
#else
#error "No protocol defined for touch controller"
#endif
#else
#endif
}
/* Config the i2c master
*
* This should init the i2c master to be used on display and touch controllers.
* So we should be able to know if the display and touch controllers shares the
* same i2c master.
*/
bool lvgl_i2c_driver_init(int port, int sda_pin, int scl_pin, int speed_hz)
{
esp_err_t err;
ESP_LOGI(TAG, "Initializing I2C master port %d...", port);
ESP_LOGI(TAG, "SDA pin: %d, SCL pin: %d, Speed: %d (Hz)",
sda_pin, scl_pin, speed_hz);
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = sda_pin,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = scl_pin,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = speed_hz,
};
ESP_LOGI(TAG, "Setting I2C master configuration...");
err = i2c_param_config(port, &conf);
assert(ESP_OK == err);
ESP_LOGI(TAG, "Installing I2C master driver...");
err = i2c_driver_install(port,
I2C_MODE_MASTER,
0, 0 /*I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE */,
0 /* intr_alloc_flags */);
assert(ESP_OK == err);
return ESP_OK != err;
}
/* Initialize spi bus master */
bool lvgl_spi_driver_init(int host,
int miso_pin, int mosi_pin, int sclk_pin,
int max_transfer_sz,
int dma_channel,
int quadwp_pin, int quadhd_pin)
{
#if defined (CONFIG_IDF_TARGET_ESP32)
assert((SPI_HOST <= host) && (VSPI_HOST >= host));
const char *spi_names[] = {
"SPI_HOST", "HSPI_HOST", "VSPI_HOST"
};
#elif defined (CONFIG_IDF_TARGET_ESP32S2)
assert((SPI_HOST <= host) && (HSPI_HOST >= host));
const char *spi_names[] = {
"SPI_HOST", "", ""
};
#endif
ESP_LOGI(TAG, "Configuring SPI host %s (%d)", spi_names[host], host);
ESP_LOGI(TAG, "MISO pin: %d, MOSI pin: %d, SCLK pin: %d, IO2/WP pin: %d, IO3/HD pin: %d",
miso_pin, mosi_pin, sclk_pin, quadwp_pin, quadhd_pin);
ESP_LOGI(TAG, "Max transfer size: %d (bytes)", max_transfer_sz);
spi_bus_config_t buscfg = {
.miso_io_num = miso_pin,
.mosi_io_num = mosi_pin,
.sclk_io_num = sclk_pin,
.quadwp_io_num = quadwp_pin,
.quadhd_io_num = quadhd_pin,
.max_transfer_sz = max_transfer_sz
};
ESP_LOGI(TAG, "Initializing SPI bus...");
esp_err_t ret = spi_bus_initialize(host, &buscfg, dma_channel);
assert(ret == ESP_OK);
return ESP_OK != ret;
}