M5Stack Core2 support (#48)

This commit is contained in:
Ken Van Hoeylandt
2024-02-22 18:26:11 +01:00
committed by GitHub
parent 473fb673bd
commit d58f131033
23 changed files with 566 additions and 10 deletions
+105
View File
@@ -0,0 +1,105 @@
#include "bootstrap.h"
#include "M5Unified.hpp"
#include "config.h"
#include "log.h"
m5::IMU_Class& Imu = M5.Imu;
m5::Power_Class& Power = M5.Power;
m5::RTC8563_Class& Rtc = M5.Rtc;
m5::Touch_Class& Touch = M5.Touch;
m5::Speaker_Class& Speaker = M5.Speaker;
m5::Button_Class& BtnPWR = M5.BtnPWR;
#ifdef __cplusplus
extern "C" {
#endif
#define TAG "core2_bootstrap"
static bool init_i2c() {
const i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = CORE2_I2C_PIN_SDA,
.scl_io_num = CORE2_I2C_PIN_SCL,
.sda_pullup_en = GPIO_PULLUP_DISABLE,
.scl_pullup_en = GPIO_PULLUP_DISABLE,
.master = {
.clk_speed = 100000
},
.clk_flags = 0
};
if (i2c_param_config(CORE2_TOUCH_I2C_PORT, &i2c_conf) != ESP_OK) {
TT_LOG_E(TAG, "i2c config failed");
return false;
}
if (i2c_driver_install(CORE2_TOUCH_I2C_PORT, i2c_conf.mode, 0, 0, 0) != ESP_OK) {
TT_LOG_E(TAG, "i2c driver install failed");
return false;
}
return true;
}
static bool init_spi2() {
const spi_bus_config_t bus_config = {
.mosi_io_num = CORE2_SPI2_PIN_MOSI,
.miso_io_num = CORE2_SPI2_PIN_MISO,
.sclk_io_num = CORE2_SPI2_PIN_SCLK,
.data2_io_num = GPIO_NUM_NC,
.data3_io_num = GPIO_NUM_NC,
.data4_io_num = GPIO_NUM_NC,
.data5_io_num = GPIO_NUM_NC,
.data6_io_num = GPIO_NUM_NC,
.data7_io_num = GPIO_NUM_NC,
.max_transfer_sz = CORE2_SPI2_TRANSACTION_LIMIT,
.flags = 0,
.isr_cpu_id = INTR_CPU_ID_AUTO,
.intr_flags = 0
};
if (spi_bus_initialize(SPI2_HOST, &bus_config, SPI_DMA_CH_AUTO) != ESP_OK) {
TT_LOG_E(TAG, "SPI bus init failed");
return false;
}
return true;
}
static void log_power_status() {
TT_LOG_I(
TAG,
"Battery: level = %ld, voltage = %.1f, charging = %s",
M5.Power.getBatteryLevel(),
(float)M5.Power.getBatteryVoltage() / 1000.0f,
M5.Power.isCharging() ? "true" : "false"
);
}
bool core2_bootstrap() {
TT_LOG_I(TAG, "Initializing M5Unified");
M5.begin();
// For models with EPD : refresh control
M5.Display.setEpdMode(epd_mode_t::epd_fastest); // fastest but very-low quality.
log_power_status();
TT_LOG_I(TAG, "Initializing I2C");
if (!init_i2c()) {
return false;
}
TT_LOG_I(TAG, "Initializing SPI");
if (!init_spi2()) {
return false;
}
return true;
}
#ifdef __cplusplus
}
#endif
+93
View File
@@ -0,0 +1,93 @@
#include "config.h"
#include "tactility_core.h"
#include "esp_err.h"
#include "esp_lcd_ili9341.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lvgl_port.h"
#define TAG "core2"
#ifdef __cplusplus
extern "C" {
#endif
lv_disp_t* core2_display_init() {
TT_LOG_I(TAG, "Display init");
const esp_lcd_panel_io_spi_config_t panel_io_config = ILI9341_PANEL_IO_SPI_CONFIG(
CORE2_LCD_PIN_CS,
CORE2_LCD_PIN_DC,
nullptr,
nullptr
);
esp_lcd_panel_io_handle_t io_handle;
if (esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)CORE2_LCD_SPI_HOST, &panel_io_config, &io_handle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to create panel");
return nullptr;
}
const esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = GPIO_NUM_NC,
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
.bits_per_pixel = CORE2_LCD_BITS_PER_PIXEL,
};
esp_lcd_panel_handle_t panel_handle;
if (esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to create ili9341");
return nullptr;
}
if (esp_lcd_panel_reset(panel_handle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to reset panel");
return nullptr;
}
if (esp_lcd_panel_init(panel_handle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to init panel");
return nullptr;
}
if (esp_lcd_panel_invert_color(panel_handle, true) != ESP_OK) {
TT_LOG_E(TAG, "Failed to invert panel colours");
return nullptr;
}
if (esp_lcd_panel_disp_on_off(panel_handle, true) != ESP_OK) {
TT_LOG_E(TAG, "Failed to turn display on");
return nullptr;
}
const lvgl_port_display_cfg_t disp_cfg = {
.io_handle = io_handle,
.panel_handle = panel_handle,
.buffer_size = CORE2_LCD_DRAW_BUFFER_SIZE,
.double_buffer = false,
.trans_size = CORE2_SPI2_TRANSACTION_LIMIT,
.hres = CORE2_LCD_HORIZONTAL_RESOLUTION,
.vres = CORE2_LCD_VERTICAL_RESOLUTION,
.monochrome = false,
.rotation = {
.swap_xy = false,
.mirror_x = false,
.mirror_y = false,
},
.flags = {
.buff_dma = false,
.buff_spiram = true,
.sw_rotate = false,
.swap_bytes = true
}
};
lv_display_t* display = lvgl_port_add_disp(&disp_cfg);
return display;
}
#ifdef __cplusplus
}
#endif
+42
View File
@@ -0,0 +1,42 @@
#include "display_i.hpp"
#include "esp_lvgl_port.h"
#include "log.h"
#include "thread.h"
#include "touch_i.hpp"
#include "ui/lvgl_sync.h"
#define TAG "core2_lvgl"
bool core2_lvgl_init() {
static lv_display_t* display = NULL;
const lvgl_port_cfg_t lvgl_cfg = {
.task_priority = ThreadPriorityHigh,
.task_stack = 8096,
.task_affinity = -1, // core pinning
.task_max_sleep_ms = 500,
.timer_period_ms = 5
};
if (lvgl_port_init(&lvgl_cfg) != ESP_OK) {
TT_LOG_E(TAG, "lvgl port init failed");
return false;
}
// Add display
display = core2_display_init();
if (display == NULL) {
TT_LOG_E(TAG, "failed to add display");
return false;
}
// Add touch
if (!core2_touch_init()) {
return false;
}
// Set syncing functions
tt_lvgl_sync_set(&lvgl_port_lock, &lvgl_port_unlock);
return true;
}
@@ -0,0 +1,14 @@
#include "m5stack_core2.h"
#include "bootstrap.h"
#include "lvgl_i.h"
extern const SdCard core2_sdcard;
const HardwareConfig m5stack_core2 = {
.bootstrap = &core2_bootstrap,
.display = {
.set_backlight_duty = NULL
},
.init_lvgl = &core2_lvgl_init,
.sdcard = &core2_sdcard
};
+79
View File
@@ -0,0 +1,79 @@
#include "sdcard.h"
#include "check.h"
#include "log.h"
#include "config.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#define TAG "core2_sdcard"
typedef struct {
const char* mount_point;
sdmmc_card_t* card;
} MountData;
static void* sdcard_mount(const char* mount_point) {
TT_LOG_I(TAG, "Mounting %s", mount_point);
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = CORE2_SDCARD_FORMAT_ON_MOUNT_FAILED,
.max_files = CORE2_SDCARD_MAX_OPEN_FILES,
.allocation_unit_size = CORE2_SDCARD_ALLOC_UNIT_SIZE,
.disk_status_check_enable = CORE2_SDCARD_STATUS_CHECK_ENABLED
};
sdmmc_card_t* card;
// Init without card detect (CD) and write protect (WD)
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = CORE2_SDCARD_PIN_CS;
slot_config.host_id = CORE2_SDCARD_SPI_HOST;
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.max_freq_khz = CORE2_SDCARD_SPI_FREQUENCY;
esp_err_t ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
TT_LOG_E(TAG, "Mounting failed. Ensure the card is formatted with FAT.");
} else {
TT_LOG_E(TAG, "Mounting failed (%s)", esp_err_to_name(ret));
}
return NULL;
}
MountData* data = malloc(sizeof(MountData));
*data = (MountData) {
.card = card,
.mount_point = mount_point
};
sdmmc_card_print_info(stdout, data->card);
return data;
}
static void sdcard_unmount(void* context) {
MountData* data = (MountData*)context;
TT_LOG_I(TAG, "Unmounting %s", data->mount_point);
tt_assert(data != NULL);
if (esp_vfs_fat_sdcard_unmount(data->mount_point, data->card) != ESP_OK) {
TT_LOG_E(TAG, "Unmount failed for %s", data->mount_point);
}
free(data);
}
static bool sdcard_is_mounted(void* context) {
MountData* data = (MountData*)context;
return (data != NULL) && (sdmmc_get_status(data->card) == ESP_OK);
}
const SdCard core2_sdcard = {
.mount = &sdcard_mount,
.unmount = &sdcard_unmount,
.is_mounted = &sdcard_is_mounted,
.mount_behaviour = SdcardMountBehaviourAnytime
};
+32
View File
@@ -0,0 +1,32 @@
#include "tactility_core.h"
#include "lvgl.h"
#include "M5Unified.hpp"
#define TAG "core2_touch"
#ifdef __cplusplus
extern "C" {
#endif
static void read_touch(TT_UNUSED lv_indev_t* indev, lv_indev_data_t* data) {
static lgfx::touch_point_t point;
bool touched = M5.Lcd.getTouch(&point) > 0;
if (touched) {
data->point.x = point.x;
data->point.y = point.y;
data->state = LV_INDEV_STATE_PRESSED;
} else {
data->state = LV_INDEV_STATE_RELEASED;
}
}
bool core2_touch_init() {
lv_indev_t _Nullable* touch_indev = lv_indev_create();
lv_indev_set_type(touch_indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(touch_indev, read_touch);
return true;
}
#ifdef __cplusplus
}
#endif