M5stack CoreS3 support (#71)

This commit is contained in:
Ken Van Hoeylandt
2024-11-07 23:17:26 +01:00
committed by GitHub
parent 68aa34ad14
commit 67b9fc710a
30 changed files with 355 additions and 334 deletions
-105
View File
@@ -1,105 +0,0 @@
#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 = ESP_INTR_CPU_AFFINITY_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
-98
View File
@@ -1,98 +0,0 @@
#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,
.data_endian = LCD_RGB_DATA_ENDIAN_BIG,
.bits_per_pixel = CORE2_LCD_BITS_PER_PIXEL,
.flags = {
.reset_active_high = false
},
.vendor_config = nullptr
};
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
@@ -1,42 +0,0 @@
#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;
}
+6 -8
View File
@@ -1,16 +1,14 @@
#include "m5stack_core2.h"
#include "bootstrap.h"
#include "lvgl_i.h"
#include "m5stack_shared.h"
extern const SdCard core2_sdcard;
extern Power core2_power; // Making it const fails the build
extern const SdCard m5stack_core2_sdcard;
const HardwareConfig m5stack_core2 = {
.bootstrap = &core2_bootstrap,
.bootstrap = &m5stack_bootstrap,
.display = {
.set_backlight_duty = NULL
},
.init_graphics = &core2_lvgl_init,
.sdcard = &core2_sdcard,
.power = &core2_power
.init_graphics = &m5stack_lvgl_init,
.sdcard = &m5stack_core2_sdcard,
.power = &m5stack_power
};
@@ -6,7 +6,7 @@
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#define TAG "core2_sdcard"
#define TAG "m5stack_core2_sdcard"
typedef struct {
const char* mount_point;
@@ -71,7 +71,7 @@ static bool sdcard_is_mounted(void* context) {
return (data != NULL) && (sdmmc_get_status(data->card) == ESP_OK);
}
const SdCard core2_sdcard = {
const SdCard m5stack_core2_sdcard = {
.mount = &sdcard_mount,
.unmount = &sdcard_unmount,
.is_mounted = &sdcard_is_mounted,
-46
View File
@@ -1,46 +0,0 @@
#include "power.h"
#include "M5Unified.hpp"
#ifdef __cplusplus
extern "C" {
#endif
/**
* M5.Power by default doesn't have a check to see if charging is enabled.
* However, it's always enabled by default after boot, so we cover that here:
*/
static bool is_charging_enabled = true;
static bool power_is_charging() {
return M5.Power.isCharging() == m5::Power_Class::is_charging;
}
static bool power_is_charging_enabled() {
return is_charging_enabled;
}
static void power_set_charging_enabled(bool enabled) {
is_charging_enabled = enabled; // Local shadow copy because M5 API doesn't provide a function for it
M5.Power.setBatteryCharge(enabled);
}
static uint8_t power_get_charge_level() {
uint16_t scaled = (uint16_t)M5.Power.getBatteryLevel() * 255 / 100;
return (uint8_t)scaled;
}
static int32_t power_get_current() {
return M5.Power.getBatteryCurrent();
}
Power core2_power = {
.is_charging = &power_is_charging,
.is_charging_enabled = &power_is_charging_enabled,
.set_charging_enabled = &power_set_charging_enabled,
.get_charge_level = &power_get_charge_level,
.get_current = &power_get_current
};
#ifdef __cplusplus
}
#endif
-38
View File
@@ -1,38 +0,0 @@
#include "tactility_core.h"
#include "lvgl.h"
#include "M5Unified.hpp"
#define TAG "core2_touch"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Touch seems to be offset by a certain amount.
* The docs don't mention it, so this is the estimated value.
*/
#define TOUCH_Y_OFFSET 16
static void read_touch(TT_UNUSED lv_indev_t* indev, lv_indev_data_t* data) {
lgfx::touch_point_t point; // Making it static makes it unreliable
bool touched = M5.Lcd.getTouch(&point) > 0;
if (touched) {
data->point.x = point.x;
data->point.y = point.y - TOUCH_Y_OFFSET;
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