forked from espressif/esp-bsp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
esp32_s3_eye.c
423 lines (372 loc) · 13.9 KB
/
esp32_s3_eye.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include "bsp/esp32_s3_eye.h"
#include "esp_vfs_fat.h"
#include "esp_timer.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/spi_master.h"
#include "driver/ledc.h"
#include "driver/i2c.h"
static const char *TAG = "S3-EYE";
/* Assert on error, if selected in menuconfig. Otherwise return error code. */
//TODO: Move this code into common header
#if CONFIG_BSP_ERROR_CHECK
#define BSP_ERROR_CHECK_RETURN_ERR(x) ESP_ERROR_CHECK(x)
#define BSP_ERROR_CHECK_RETURN_NULL(x) ESP_ERROR_CHECK(x)
#define BSP_NULL_CHECK(x, ret) assert(x)
#else
#define BSP_ERROR_CHECK_RETURN_ERR(x) do { \
esp_err_t err_rc_ = (x); \
if (unlikely(err_rc_ != ESP_OK)) { \
return err_rc_; \
} \
} while(0)
#define BSP_ERROR_CHECK_RETURN_NULL(x) do { \
if (unlikely((x) != ESP_OK)) { \
return NULL; \
} \
} while(0)
#define BSP_NULL_CHECK(x, ret) do { \
if ((x) == NULL) { \
return ret; \
} \
} while(0)
#endif
static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
static lv_disp_drv_t disp_drv; // contains callback functions
static SemaphoreHandle_t lvgl_mux; // LVGL mutex
sdmmc_card_t *bsp_sdcard = NULL; // Global uSD card handler
const button_config_t bsp_button_config[BSP_BUTTON_NUM] = {
{
.type = BUTTON_TYPE_ADC,
.adc_button_config.adc_channel = ADC1_CHANNEL_0, // ADC1 channel 0 is GPIO1
.adc_button_config.button_index = BSP_BUTTON_MENU,
.adc_button_config.min = 2310, // middle is 2410mV
.adc_button_config.max = 2510
},
{
.type = BUTTON_TYPE_ADC,
.adc_button_config.adc_channel = ADC1_CHANNEL_0, // ADC1 channel 0 is GPIO1
.adc_button_config.button_index = BSP_BUTTON_PLAY,
.adc_button_config.min = 1880, // middle is 1980mV
.adc_button_config.max = 2080
},
{
.type = BUTTON_TYPE_ADC,
.adc_button_config.adc_channel = ADC1_CHANNEL_0, // ADC1 channel 0 is GPIO1
.adc_button_config.button_index = BSP_BUTTON_DOWN,
.adc_button_config.min = 720, // middle is 820mV
.adc_button_config.max = 920
},
{
.type = BUTTON_TYPE_ADC,
.adc_button_config.adc_channel = ADC1_CHANNEL_0, // ADC1 channel 0 is GPIO1
.adc_button_config.button_index = BSP_BUTTON_UP,
.adc_button_config.min = 280, // middle is 380mV
.adc_button_config.max = 480
}
};
esp_err_t bsp_i2c_init(void)
{
const i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = BSP_I2C_SDA,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = BSP_I2C_SCL,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = CONFIG_BSP_I2C_CLK_SPEED_HZ
};
BSP_ERROR_CHECK_RETURN_ERR(i2c_param_config(BSP_I2C_NUM, &i2c_conf));
BSP_ERROR_CHECK_RETURN_ERR(i2c_driver_install(BSP_I2C_NUM, i2c_conf.mode, 0, 0, 0));
return ESP_OK;
}
esp_err_t bsp_i2c_deinit(void)
{
BSP_ERROR_CHECK_RETURN_ERR(i2c_driver_delete(BSP_I2C_NUM));
return ESP_OK;
}
esp_err_t bsp_sdcard_mount(void)
{
const esp_vfs_fat_sdmmc_mount_config_t mount_config = {
#ifdef CONFIG_BSP_SD_FORMAT_ON_MOUNT_FAIL
.format_if_mount_failed = true,
#else
.format_if_mount_failed = false,
#endif
.max_files = 5,
.allocation_unit_size = 16 * 1024
};
const sdmmc_host_t host = SDMMC_HOST_DEFAULT();
const sdmmc_slot_config_t slot_config = {
.clk = BSP_SD_CLK,
.cmd = BSP_SD_CMD,
.d0 = BSP_SD_D0,
.d1 = GPIO_NUM_NC,
.d2 = GPIO_NUM_NC,
.d3 = GPIO_NUM_NC,
.d4 = GPIO_NUM_NC,
.d5 = GPIO_NUM_NC,
.d6 = GPIO_NUM_NC,
.d7 = GPIO_NUM_NC,
.cd = SDMMC_SLOT_NO_CD,
.wp = SDMMC_SLOT_NO_WP,
.width = 1,
.flags = 0,
};
return esp_vfs_fat_sdmmc_mount(BSP_MOUNT_POINT, &host, &slot_config, &mount_config, &bsp_sdcard);
}
esp_err_t bsp_sdcard_unmount(void)
{
return esp_vfs_fat_sdcard_unmount(BSP_MOUNT_POINT, bsp_sdcard);
}
static bool lvgl_port_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
{
lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
lv_disp_flush_ready(disp_driver);
return false;
}
static void lvgl_port_flush_callback(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
{
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
const int offsetx1 = area->x1;
const int offsetx2 = area->x2;
const int offsety1 = area->y1;
const int offsety2 = area->y2;
// copy a buffer's content to a specific area of the display
esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
}
static void lvgl_port_update_callback(lv_disp_drv_t *drv)
{
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
switch (drv->rotated) {
case LV_DISP_ROT_NONE:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, false);
esp_lcd_panel_mirror(panel_handle, false, false);
esp_lcd_panel_set_gap(panel_handle, 0, 0);
break;
case LV_DISP_ROT_90:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_mirror(panel_handle, false, true);
esp_lcd_panel_set_gap(panel_handle, 80, 0);
break;
case LV_DISP_ROT_180:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, false);
esp_lcd_panel_mirror(panel_handle, true, true);
esp_lcd_panel_set_gap(panel_handle, 0, 80);
break;
case LV_DISP_ROT_270:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_mirror(panel_handle, true, false);
esp_lcd_panel_set_gap(panel_handle, 0, 0);
break;
}
}
#define LCD_CMD_BITS (8)
#define LCD_PARAM_BITS (8)
#define LCD_LEDC_CH (CONFIG_BSP_DISPLAY_BRIGHTNESS_LEDC_CH)
#define LVGL_TICK_PERIOD_MS (CONFIG_BSP_DISPLAY_LVGL_TICK)
#define LVGL_BUFF_SIZE_PIX (BSP_LCD_H_RES * BSP_LCD_V_RES)
static esp_err_t bsp_display_brightness_init(void)
{
// Setup LEDC peripheral for PWM backlight control
const ledc_channel_config_t LCD_backlight_channel = {
.gpio_num = BSP_LCD_BACKLIGHT,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LCD_LEDC_CH,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = 1,
.duty = 0,
.hpoint = 0,
.flags.output_invert = true
};
const ledc_timer_config_t LCD_backlight_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = 1,
.freq_hz = 5000,
.clk_cfg = LEDC_AUTO_CLK
};
BSP_ERROR_CHECK_RETURN_ERR(ledc_timer_config(&LCD_backlight_timer));
BSP_ERROR_CHECK_RETURN_ERR(ledc_channel_config(&LCD_backlight_channel));
return ESP_OK;
}
esp_err_t bsp_display_brightness_set(int brightness_percent)
{
if (brightness_percent > 100) {
brightness_percent = 100;
} else if (brightness_percent < 0) {
brightness_percent = 0;
}
ESP_LOGI(TAG, "Setting LCD backlight: %d%%", brightness_percent);
// LEDC resolution set to 10bits, thus: 100% = 1023
uint32_t duty_cycle = (1023 * brightness_percent) / 100;
BSP_ERROR_CHECK_RETURN_ERR(ledc_set_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH, duty_cycle));
BSP_ERROR_CHECK_RETURN_ERR(ledc_update_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH));
return ESP_OK;
}
esp_err_t bsp_display_backlight_off(void)
{
return bsp_display_brightness_set(0);
}
esp_err_t bsp_display_backlight_on(void)
{
return bsp_display_brightness_set(100);
}
static lv_disp_t *lvgl_port_display_init(void)
{
ESP_LOGD(TAG, "Initialize SPI bus");
const spi_bus_config_t buscfg = {
.sclk_io_num = BSP_LCD_SPI_CLK,
.mosi_io_num = BSP_LCD_SPI_MOSI,
.miso_io_num = GPIO_NUM_NC,
.quadwp_io_num = GPIO_NUM_NC,
.quadhd_io_num = GPIO_NUM_NC,
.max_transfer_sz = LVGL_BUFF_SIZE_PIX * sizeof(lv_color_t),
};
BSP_ERROR_CHECK_RETURN_NULL(spi_bus_initialize(BSP_LCD_SPI_NUM, &buscfg, SPI_DMA_CH_AUTO));
ESP_LOGD(TAG, "Install panel IO");
esp_lcd_panel_io_handle_t io_handle = NULL;
const esp_lcd_panel_io_spi_config_t io_config = {
.dc_gpio_num = BSP_LCD_DC,
.cs_gpio_num = BSP_LCD_SPI_CS,
.pclk_hz = BSP_LCD_PIXEL_CLOCK_HZ,
.lcd_cmd_bits = LCD_CMD_BITS,
.lcd_param_bits = LCD_PARAM_BITS,
.spi_mode = 2,
.trans_queue_depth = 10,
.on_color_trans_done = lvgl_port_flush_ready,
.user_ctx = &disp_drv,
};
// Attach the LCD to the SPI bus
BSP_ERROR_CHECK_RETURN_NULL(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)BSP_LCD_SPI_NUM, &io_config, &io_handle));
ESP_LOGD(TAG, "Install LCD driver");
esp_lcd_panel_handle_t panel_handle = NULL;
const esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = BSP_LCD_RST,
.color_space = ESP_LCD_COLOR_SPACE_RGB,
.bits_per_pixel = 16,
};
BSP_ERROR_CHECK_RETURN_NULL(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
esp_lcd_panel_reset(panel_handle);
esp_lcd_panel_init(panel_handle);
esp_lcd_panel_invert_color(panel_handle, true);
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
esp_lcd_panel_disp_off(panel_handle, false);
#else
esp_lcd_panel_disp_on_off(panel_handle, true);
#endif
// alloc draw buffers used by LVGL
// it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
// but we allocate one full frame buffer for best performance
lv_color_t *buf1 = heap_caps_malloc(LVGL_BUFF_SIZE_PIX * sizeof(lv_color_t), MALLOC_CAP_DMA);
BSP_NULL_CHECK(buf1, NULL);
lv_disp_draw_buf_init(&disp_buf, buf1, NULL, LVGL_BUFF_SIZE_PIX);
ESP_LOGI(TAG, "Registering display driver to LVGL");
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = BSP_LCD_H_RES;
disp_drv.ver_res = BSP_LCD_V_RES;
disp_drv.flush_cb = lvgl_port_flush_callback;
disp_drv.drv_update_cb = lvgl_port_update_callback;
disp_drv.draw_buf = &disp_buf;
disp_drv.user_data = panel_handle;
return lv_disp_drv_register(&disp_drv);
}
static void lvgl_port_tick_increment(void *arg)
{
/* Tell LVGL how many milliseconds has elapsed */
lv_tick_inc(LVGL_TICK_PERIOD_MS);
}
static esp_err_t lvgl_port_tick_init(void)
{
// Tick interface for LVGL (using esp_timer to generate periodic event)
const esp_timer_create_args_t lvgl_tick_timer_args = {
.callback = &lvgl_port_tick_increment,
.name = "LVGL tick"
};
esp_timer_handle_t lvgl_tick_timer = NULL;
BSP_ERROR_CHECK_RETURN_ERR(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
return esp_timer_start_periodic(lvgl_tick_timer, LVGL_TICK_PERIOD_MS * 1000);
}
static void lvgl_port_task(void *arg)
{
ESP_LOGI(TAG, "Starting LVGL task");
while (1) {
bsp_display_lock(0);
lv_timer_handler();
bsp_display_unlock();
vTaskDelay(pdMS_TO_TICKS(10));
}
}
lv_disp_t *bsp_display_start(void)
{
lv_init();
BSP_ERROR_CHECK_RETURN_NULL(bsp_display_brightness_init());
lv_disp_t *disp = lvgl_port_display_init();
BSP_ERROR_CHECK_RETURN_NULL(lvgl_port_tick_init());
lvgl_mux = xSemaphoreCreateMutex();
BSP_NULL_CHECK(lvgl_mux, NULL);
xTaskCreatePinnedToCore(lvgl_port_task, "LVGL task", 4096, NULL, CONFIG_BSP_DISPLAY_LVGL_TASK_PRIORITY, NULL, 1);
return disp;
}
bool bsp_display_lock(uint32_t timeout_ms)
{
assert(lvgl_mux && "bsp_display_start must be called first");
const TickType_t timeout_ticks = (timeout_ms == 0) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
return xSemaphoreTake(lvgl_mux, timeout_ticks) == pdTRUE;
}
void bsp_display_unlock(void)
{
assert(lvgl_mux && "bsp_display_start must be called first");
xSemaphoreGive(lvgl_mux);
}
void bsp_display_rotate(lv_disp_t *disp, lv_disp_rot_t rotation)
{
lv_disp_set_rotation(disp, rotation);
}
esp_err_t bsp_leds_init(void)
{
const gpio_config_t led_io_config = {
.pin_bit_mask = BIT64(BSP_LED_GREEN),
.mode = GPIO_MODE_OUTPUT_OD, // GPIO3 is connected directly to the LED (on board revision 2.1), so we use Open-drain here
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
BSP_ERROR_CHECK_RETURN_ERR(gpio_config(&led_io_config));
return ESP_OK;
}
esp_err_t bsp_led_set(const bsp_led_t led_io, const bool on)
{
BSP_ERROR_CHECK_RETURN_ERR(gpio_set_level((gpio_num_t) led_io, (uint32_t) on));
return ESP_OK;
}
esp_err_t bsp_audio_init(const i2s_std_config_t *i2s_config, i2s_chan_handle_t *tx_channel, i2s_chan_handle_t *rx_channel)
{
/* Setup I2S peripheral */
const i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
BSP_ERROR_CHECK_RETURN_ERR(i2s_new_channel(&chan_cfg, NULL, rx_channel));
if (tx_channel) {
*tx_channel = NULL; // TX is not used in ESP32-S3-EYE board
}
/* Setup I2S channel */
const i2s_std_config_t std_cfg_default = BSP_I2S_SIMPLEX_MONO_CFG(); // Default config
const i2s_std_config_t *p_i2s_cfg = i2s_config ? i2s_config : &std_cfg_default;
if (rx_channel != NULL) {
BSP_ERROR_CHECK_RETURN_ERR(i2s_channel_init_std_mode(*rx_channel, p_i2s_cfg));
BSP_ERROR_CHECK_RETURN_ERR(i2s_channel_enable(*rx_channel));
}
return ESP_OK;
}