forked from m5stack/M5Tab5-UserDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhal_esp32.cpp
More file actions
511 lines (441 loc) · 14.3 KB
/
Copy pathhal_esp32.cpp
File metadata and controls
511 lines (441 loc) · 14.3 KB
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
#include "hal/hal_esp32.h"
extern "C"
{
#include "utils/rx8130/rx8130.h"
}
#include <algorithm>
#include <bsp/m5stack_tab5.h>
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <initializer_list>
#include <mooncake_log.h>
#include <sys/time.h>
extern esp_lcd_touch_handle_t _lcd_touch_handle;
static const std::string _tag = "hal";
static void lvgl_read_cb(lv_indev_t* indev, lv_indev_data_t* data)
{
if (_lcd_touch_handle == NULL)
{
data->state = LV_INDEV_STATE_REL;
return;
}
uint16_t touch_x[1];
uint16_t touch_y[1];
uint16_t touch_strength[1];
uint8_t touch_cnt = 0;
esp_lcd_touch_read_data(_lcd_touch_handle);
bool touchpad_pressed = esp_lcd_touch_get_coordinates(
_lcd_touch_handle, touch_x, touch_y, touch_strength, &touch_cnt, 1);
// mclog::tagInfo(_tag, "touchpad pressed: {}", touchpad_pressed);
if (!touchpad_pressed)
{
data->state = LV_INDEV_STATE_REL;
return;
}
lv_display_t* disp = lv_indev_get_display(indev);
lv_display_rotation_t rotation = LV_DISPLAY_ROTATION_0;
lv_coord_t raw_w = BSP_LCD_H_RES;
lv_coord_t raw_h = BSP_LCD_V_RES;
static bool logged_right_edge_ok = false;
if (disp != nullptr)
{
rotation = lv_display_get_rotation(disp);
lv_coord_t phys_w = lv_display_get_physical_horizontal_resolution(disp);
lv_coord_t phys_h = lv_display_get_physical_vertical_resolution(disp);
if (phys_w > 0)
{
raw_w = phys_w;
}
if (phys_h > 0)
{
raw_h = phys_h;
}
}
lv_coord_t transformed_x = touch_x[0];
lv_coord_t transformed_y = touch_y[0];
switch (rotation)
{
case LV_DISPLAY_ROTATION_90:
transformed_x = touch_y[0];
transformed_y = raw_w - 1 - touch_x[0];
break;
case LV_DISPLAY_ROTATION_180:
transformed_x = raw_w - 1 - touch_x[0];
transformed_y = raw_h - 1 - touch_y[0];
break;
case LV_DISPLAY_ROTATION_270:
transformed_x = raw_h - 1 - touch_y[0];
transformed_y = touch_x[0];
break;
case LV_DISPLAY_ROTATION_0:
default:
break;
}
data->state = LV_INDEV_STATE_PR;
data->point.x = transformed_x;
data->point.y = transformed_y;
lv_coord_t right_edge_threshold = LV_MAX(raw_h - 5, 0);
if (!logged_right_edge_ok && rotation == LV_DISPLAY_ROTATION_90
&& transformed_x >= right_edge_threshold)
{
mclog::tagInfo(_tag,
"Touch rotation check: right edge press mapped to ({}, {})",
static_cast<int>(transformed_x),
static_cast<int>(transformed_y));
logged_right_edge_ok = true;
}
}
void HalEsp32::init()
{
mclog::tagInfo(_tag, "init");
mclog::tagInfo(_tag, "camera init");
bsp_cam_osc_init();
mclog::tagInfo(_tag, "i2c init");
bsp_i2c_init();
mclog::tagInfo(_tag, "io expander init");
i2c_master_bus_handle_t i2c_bus_handle = bsp_i2c_get_handle();
bsp_io_expander_pi4ioe_init(i2c_bus_handle);
// Bring the downstream 5 V rails up before probing peripherals that rely on
// them. The IO expanders default to the rails being disabled at power-on,
// so explicitly drive them high here to ensure the codec, RS485 transceiver
// and Wi-Fi co-processor are powered for the remainder of the HAL
// initialisation sequence.
setUsb5vEnable(true);
setExt5vEnable(true);
delay(10);
setChargeQcEnable(true);
delay(50);
setChargeEnable(true);
// setChargeEnable(false);
mclog::tagInfo(_tag, "i2c scan");
bsp_i2c_scan();
mclog::tagInfo(_tag, "codec init");
delay(200);
bsp_codec_init();
mclog::tagInfo(_tag, "imu init");
imu_init();
mclog::tagInfo(_tag, "ina226 init");
ina226.begin(i2c_bus_handle, 0x41);
ina226.configure(INA226_AVERAGES_16,
INA226_BUS_CONV_TIME_1100US,
INA226_SHUNT_CONV_TIME_1100US,
INA226_MODE_SHUNT_BUS_CONT);
ina226.calibrate(0.005, 8.192);
mclog::tagInfo(_tag, "bus voltage: {}", ina226.readBusVoltage());
mclog::tagInfo(_tag, "rx8130 init");
rx8130.begin(i2c_bus_handle, 0x32);
rx8130.initBat();
clearRtcIrq();
update_system_time();
mclog::tagInfo(_tag, "display init");
bsp_reset_tp();
bsp_display_cfg_t cfg = {.lvgl_port_cfg = ESP_LVGL_PORT_INIT_CONFIG(),
.buffer_size = BSP_LCD_H_RES * BSP_LCD_V_RES,
.double_buffer = true,
.flags = {
#if CONFIG_BSP_LCD_COLOR_FORMAT_RGB888
.buff_dma = false,
#else
.buff_dma = true,
#endif
.buff_spiram = true,
.sw_rotate = true,
}};
lvDisp = bsp_display_start_with_config(&cfg);
bool display_locked = false;
if (lvDisp == nullptr)
{
mclog::tagError(_tag, "Failed to start LVGL display");
}
else
{
display_locked = bsp_display_lock(0);
if (!display_locked)
{
mclog::tagError(_tag, "Failed to lock LVGL port during display init");
}
else
{
lv_display_set_rotation(lvDisp, LV_DISPLAY_ROTATION_90);
mclog::tagInfo(_tag, "Display rotation set to 90 degrees");
}
}
bsp_display_backlight_on();
lvTouchpad = bsp_display_get_input_dev();
if (display_locked)
{
if (lvTouchpad != nullptr)
{
lv_indev_set_display(lvTouchpad, lvDisp);
}
else
{
mclog::tagWarn(_tag, "LVGL touch input not provided by BSP; creating fallback driver");
lvTouchpad = lv_indev_create();
if (lvTouchpad == nullptr)
{
mclog::tagError(_tag, "Failed to allocate LVGL touch input device");
}
else
{
lv_indev_set_type(lvTouchpad, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(lvTouchpad, lvgl_read_cb);
lv_indev_set_display(lvTouchpad, lvDisp);
}
}
bsp_display_unlock();
display_locked = false;
}
else if (lvTouchpad == nullptr)
{
mclog::tagError(_tag, "Touch input unavailable and LVGL lock not taken");
}
mclog::tagInfo(_tag, "usb host init");
bsp_usb_host_start(BSP_USB_HOST_POWER_MODE_USB_DEV, true);
mclog::tagInfo(_tag, "hid init");
hid_init();
mclog::tagInfo(_tag, "rs485 init");
rs485_init();
mclog::tagInfo(_tag, "set gpio output capability");
set_gpio_output_capability();
}
void HalEsp32::set_gpio_output_capability()
{
auto set_drive_cap = [](gpio_drive_cap_t cap, std::initializer_list<gpio_num_t> gpios)
{
for (gpio_num_t gpio : gpios)
{
esp_err_t ret = gpio_set_drive_capability(gpio, cap);
if (ret == ESP_OK)
{
printf("GPIO %d drive capability set to %d\n", gpio, (int)cap);
}
else
{
printf("Failed to set GPIO %d drive capability: %s\n", gpio, esp_err_to_name(ret));
}
}
};
// EXT I2C lines are open-drain; keep the weakest drive strength to limit ringing.
set_drive_cap(GPIO_DRIVE_CAP_0,
{
GPIO_NUM_0,
GPIO_NUM_1,
});
// Maintain strong edges on the esp-hosted ESP32-C6 SPI link.
set_drive_cap(GPIO_DRIVE_CAP_3,
{
GPIO_NUM_8,
GPIO_NUM_9,
GPIO_NUM_10,
GPIO_NUM_11,
GPIO_NUM_12,
GPIO_NUM_13,
GPIO_NUM_15,
});
// Display RGB/8080 data lines toggle quickly; keep them at the maximum drive.
set_drive_cap(GPIO_DRIVE_CAP_3,
{
GPIO_NUM_22,
GPIO_NUM_23,
});
// I2S audio output requires high drive to avoid distortion on the codec bus.
set_drive_cap(GPIO_DRIVE_CAP_3,
{
GPIO_NUM_26,
GPIO_NUM_27,
GPIO_NUM_28,
GPIO_NUM_29,
GPIO_NUM_30,
});
// SYS I2C bus shares peripherals on the baseboard; keep the weaker drive.
set_drive_cap(GPIO_DRIVE_CAP_0,
{
GPIO_NUM_31,
GPIO_NUM_32,
});
// microSD runs SDMMC mode at high frequency; retain a strong push-pull drive.
set_drive_cap(GPIO_DRIVE_CAP_3,
{
GPIO_NUM_39,
GPIO_NUM_40,
GPIO_NUM_41,
GPIO_NUM_42,
GPIO_NUM_43,
GPIO_NUM_44,
});
}
/* -------------------------------------------------------------------------- */
/* System */
/* -------------------------------------------------------------------------- */
#include <driver/temperature_sensor.h>
static temperature_sensor_handle_t _temp_sensor = nullptr;
void HalEsp32::delay(uint32_t ms)
{
vTaskDelay(pdMS_TO_TICKS(ms));
}
uint32_t HalEsp32::millis()
{
return esp_timer_get_time() / 1000;
}
int HalEsp32::getCpuTemp()
{
if (_temp_sensor == nullptr)
{
temperature_sensor_config_t temp_sensor_config = {
.range_min = 20,
.range_max = 100,
};
temperature_sensor_install(&temp_sensor_config, &_temp_sensor);
temperature_sensor_enable(_temp_sensor);
}
float temp = 0;
temperature_sensor_get_celsius(_temp_sensor, &temp);
return temp;
}
/* -------------------------------------------------------------------------- */
/* Display */
/* -------------------------------------------------------------------------- */
void HalEsp32::setDisplayBrightness(uint8_t brightness)
{
_current_lcd_brightness = std::clamp((int)brightness, 0, 100);
mclog::tagInfo("hal", "set display brightness: {}%", _current_lcd_brightness);
bsp_display_brightness_set(_current_lcd_brightness);
}
uint8_t HalEsp32::getDisplayBrightness()
{
return _current_lcd_brightness;
}
void HalEsp32::lvglLock()
{
lvgl_port_lock(0);
}
void HalEsp32::lvglUnlock()
{
lvgl_port_unlock();
}
/* -------------------------------------------------------------------------- */
/* RTC */
/* -------------------------------------------------------------------------- */
void HalEsp32::clearRtcIrq()
{
mclog::tagInfo(_tag, "clear rtc irq");
rx8130.clearIrqFlags();
rx8130.disableIrq();
}
void HalEsp32::setRtcTime(tm time)
{
mclog::tagInfo(_tag,
"set rtc time to {}/{}/{} {:02d}:{:02d}:{:02d}",
time.tm_year + 1900,
time.tm_mon + 1,
time.tm_mday,
time.tm_hour,
time.tm_min,
time.tm_sec);
rx8130.setTime(&time);
delay(50);
update_system_time();
}
void HalEsp32::update_system_time()
{
mclog::tagInfo(_tag, "update system time");
struct tm time;
rx8130.getTime(&time);
mclog::tagInfo(_tag,
"sync to rtc time: {}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}",
time.tm_year + 1900,
time.tm_mon + 1,
time.tm_mday,
time.tm_hour,
time.tm_min,
time.tm_sec);
struct timeval now;
now.tv_sec = mktime(&time);
now.tv_usec = 0;
settimeofday(&now, NULL);
}
/* -------------------------------------------------------------------------- */
/* SD Card */
/* -------------------------------------------------------------------------- */
bool HalEsp32::isSdCardMounted()
{
return _sd_card_mounted;
}
std::vector<hal::HalBase::FileEntry_t> HalEsp32::scanSdCard(const std::string& dirPath)
{
(void)dirPath;
_sd_card_mounted = false;
mclog::tagWarn(_tag, "SD card access disabled; skipping scan request");
return {};
}
/* -------------------------------------------------------------------------- */
/* Interface */
/* -------------------------------------------------------------------------- */
bool HalEsp32::usbCDetect()
{
return bsp_usb_c_detect();
// return false;
}
bool HalEsp32::headPhoneDetect()
{
return bsp_headphone_detect();
}
std::vector<uint8_t> HalEsp32::i2cScan(bool isInternal)
{
i2c_master_bus_handle_t i2c_bus_handle;
std::vector<uint8_t> addrs;
if (isInternal)
{
i2c_bus_handle = bsp_i2c_get_handle();
}
else
{
i2c_bus_handle = bsp_ext_i2c_get_handle();
}
esp_err_t ret;
uint8_t address;
for (int i = 16; i < 128; i += 16)
{
for (int j = 0; j < 16; j++)
{
fflush(stdout);
address = i + j;
ret = i2c_master_probe(i2c_bus_handle, address, 50);
if (ret == ESP_OK)
{
addrs.push_back(address);
}
}
}
return addrs;
}
void HalEsp32::initPortAI2c()
{
mclog::tagInfo(_tag, "init port a i2c");
bsp_ext_i2c_init();
}
void HalEsp32::deinitPortAI2c()
{
mclog::tagInfo(_tag, "deinit port a i2c");
bsp_ext_i2c_deinit();
}
void HalEsp32::gpioInitOutput(uint8_t pin)
{
gpio_set_pull_mode((gpio_num_t)pin, GPIO_PULLUP_ONLY);
gpio_set_direction((gpio_num_t)pin, GPIO_MODE_OUTPUT);
}
void HalEsp32::gpioSetLevel(uint8_t pin, bool level)
{
gpio_set_level((gpio_num_t)pin, level);
}
void HalEsp32::gpioReset(uint8_t pin)
{
gpio_set_level((gpio_num_t)pin, false);
}