2024-04-22 17:07:56 +08:00
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "M5Unified.hpp"
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
#include "uiflow_utility.h"
|
2024-11-13 18:22:04 +08:00
|
|
|
// #include <driver/periph_ctrl.h>
|
2024-10-24 12:00:01 +08:00
|
|
|
#include "esp_log.h"
|
|
|
|
|
|
2025-10-31 11:20:24 +08:00
|
|
|
#define MICROPY_HW_ESP_NEW_I2C_DRIVER 1
|
|
|
|
|
|
|
|
|
|
#if MICROPY_HW_ESP_NEW_I2C_DRIVER
|
|
|
|
|
#include "driver/i2c_master.h"
|
|
|
|
|
#else
|
|
|
|
|
#include "driver/i2c.h"
|
|
|
|
|
#include "hal/i2c_ll.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-11-05 12:19:46 +08:00
|
|
|
static void in_i2c_init(void)
|
2024-10-24 12:00:01 +08:00
|
|
|
{
|
|
|
|
|
gpio_num_t in_scl = (gpio_num_t)M5.getPin(m5::pin_name_t::in_i2c_scl);
|
|
|
|
|
gpio_num_t in_sda = (gpio_num_t)M5.getPin(m5::pin_name_t::in_i2c_sda);
|
|
|
|
|
gpio_num_t ex_scl = (gpio_num_t)M5.getPin(m5::pin_name_t::ex_i2c_scl);
|
|
|
|
|
gpio_num_t ex_sda = (gpio_num_t)M5.getPin(m5::pin_name_t::ex_i2c_sda);
|
|
|
|
|
i2c_port_t ex_port = I2C_NUM_0;
|
2025-06-12 16:46:09 +08:00
|
|
|
#if SOC_I2C_NUM == 1 || defined(CONFIG_IDF_TARGET_ESP32C6)
|
2024-10-24 12:00:01 +08:00
|
|
|
i2c_port_t in_port = I2C_NUM_0;
|
|
|
|
|
#else
|
|
|
|
|
i2c_port_t in_port = I2C_NUM_1;
|
|
|
|
|
if (in_scl == ex_scl && in_sda == ex_sda) {
|
|
|
|
|
in_port = ex_port;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-03-17 16:13:05 +08:00
|
|
|
if (in_scl != GPIO_NUM_NC || in_sda != GPIO_NUM_NC) {
|
2024-11-05 12:19:46 +08:00
|
|
|
ESP_LOGI("BOARD", "Internal I2C(%d) init", in_port);
|
2025-10-31 11:20:24 +08:00
|
|
|
#if MICROPY_HW_ESP_NEW_I2C_DRIVER
|
|
|
|
|
i2c_master_bus_handle_t bus_handle;
|
2025-11-14 12:00:32 +08:00
|
|
|
if (i2c_master_get_bus_handle(in_port, &bus_handle) != ESP_OK) {
|
2025-10-31 11:20:24 +08:00
|
|
|
i2c_master_bus_config_t i2c_bus_config;
|
|
|
|
|
memset(&i2c_bus_config, 0, sizeof(i2c_bus_config));
|
|
|
|
|
i2c_bus_config.clk_source = I2C_CLK_SRC_DEFAULT;
|
|
|
|
|
i2c_bus_config.i2c_port = in_port;
|
|
|
|
|
i2c_bus_config.scl_io_num = in_scl;
|
|
|
|
|
i2c_bus_config.sda_io_num = in_sda;
|
|
|
|
|
i2c_bus_config.glitch_ignore_cnt = 7;
|
2025-12-30 18:17:02 +08:00
|
|
|
i2c_bus_config.flags.enable_internal_pullup = true;
|
2025-10-31 11:20:24 +08:00
|
|
|
i2c_new_master_bus(&i2c_bus_config, &bus_handle);
|
|
|
|
|
}
|
|
|
|
|
#else
|
2024-10-24 12:00:01 +08:00
|
|
|
i2c_config_t conf;
|
|
|
|
|
memset(&conf, 0, sizeof(i2c_config_t));
|
|
|
|
|
conf.mode = I2C_MODE_MASTER;
|
|
|
|
|
conf.sda_io_num = in_sda;
|
|
|
|
|
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
|
|
|
|
|
conf.scl_io_num = in_scl;
|
|
|
|
|
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
|
|
|
|
|
conf.master.clk_speed = 100000;
|
|
|
|
|
// .clk_flags = 0, /*!< Optional, you can use I2C_SCLK_SRC_FLAG_* flags to choose i2c source clock here. */
|
|
|
|
|
i2c_param_config(in_port, &conf);
|
|
|
|
|
i2c_driver_install(in_port, I2C_MODE_MASTER, 0, 0, 0);
|
2025-10-31 11:20:24 +08:00
|
|
|
#endif
|
2024-10-24 12:00:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-22 17:07:56 +08:00
|
|
|
|
|
|
|
|
void board_init()
|
|
|
|
|
{
|
|
|
|
|
auto cfg = M5.config();
|
|
|
|
|
cfg.output_power = false;
|
2026-05-07 16:47:07 +08:00
|
|
|
cfg.clear_display = false;
|
2024-04-22 17:07:56 +08:00
|
|
|
M5.begin(cfg);
|
2024-11-05 12:21:21 +08:00
|
|
|
M5.In_I2C.release();
|
2025-05-20 15:43:05 +08:00
|
|
|
in_i2c_init();
|
2024-04-22 17:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void power_init()
|
|
|
|
|
{
|
2024-11-13 18:22:04 +08:00
|
|
|
m5::board_t board_id = M5.getBoard();
|
|
|
|
|
if (
|
|
|
|
|
! (board_id == m5::board_t::board_M5StackCore2
|
|
|
|
|
|| board_id == m5::board_t::board_M5StackCoreS3
|
|
|
|
|
|| board_id == m5::board_t::board_M5StackCoreS3SE
|
2024-12-20 11:09:36 +08:00
|
|
|
|| board_id == m5::board_t::board_M5Paper
|
|
|
|
|
|| board_id == m5::board_t::board_M5Station
|
|
|
|
|
|| board_id == m5::board_t::board_M5StickC
|
|
|
|
|
|| board_id == m5::board_t::board_M5StickCPlus
|
2024-11-13 18:22:04 +08:00
|
|
|
|| board_id == m5::board_t::board_M5Tough)
|
|
|
|
|
) {
|
|
|
|
|
ESP_LOGW("BOARD", "Power init skipped");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-22 17:07:56 +08:00
|
|
|
char power_mode[32] = {0};
|
|
|
|
|
size_t len;
|
|
|
|
|
bool usb_output = false;
|
|
|
|
|
bool bus_output = false;
|
2024-08-01 14:26:21 +08:00
|
|
|
|
|
|
|
|
bool ret = nvs_read_str_helper((char *)"uiflow", (char *)"power_mode", power_mode, &len);
|
|
|
|
|
if (ret == false) {
|
|
|
|
|
// 当 nvs 没有 "power_mode" 时,设置默认值
|
|
|
|
|
usb_output = false;
|
|
|
|
|
bus_output = true;
|
|
|
|
|
goto set_power;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 17:07:56 +08:00
|
|
|
if (strncmp(power_mode, "usb_in_bus_in", strlen("usb_in_bus_in")) == 0) {
|
|
|
|
|
usb_output = false;
|
|
|
|
|
bus_output = false;
|
|
|
|
|
} else if (strncmp(power_mode, "usb_in_bus_out", strlen("usb_in_bus_out")) == 0) {
|
|
|
|
|
usb_output = false;
|
|
|
|
|
bus_output = true;
|
|
|
|
|
} else if (strncmp(power_mode, "usb_out_bus_in", strlen("usb_out_bus_in")) == 0) {
|
|
|
|
|
usb_output = true;
|
|
|
|
|
bus_output = false;
|
|
|
|
|
} else if (strncmp(power_mode, "usb_out_bus_out", strlen("usb_out_bus_out")) == 0) {
|
|
|
|
|
usb_output = true;
|
|
|
|
|
bus_output = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 14:26:21 +08:00
|
|
|
set_power:
|
2024-04-22 17:07:56 +08:00
|
|
|
M5.Power.setUsbOutput(usb_output);
|
|
|
|
|
M5.Power.setExtOutput(bus_output);
|
|
|
|
|
}
|
2024-08-22 11:20:30 +02:00
|
|
|
}
|