I2C Implementation (#84)

This commit is contained in:
Ken Van Hoeylandt
2024-11-24 18:04:57 +01:00
committed by GitHub
parent 881c8517bf
commit d8731eaa17
51 changed files with 936 additions and 433 deletions
@@ -4,7 +4,7 @@
#define TAG "cores3_touch"
static void touch_read_callback(TT_UNUSED lv_indev_t* indev, lv_indev_data_t* data) {
static void read_callback(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) {
@@ -24,6 +24,6 @@ _Nullable lv_indev_t* m5stack_lvgl_touch() {
}
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(indev, touch_read_callback);
lv_indev_set_read_cb(indev, read_callback);
return indev;
}
@@ -3,7 +3,8 @@
#include "Hal/Power.h"
#include "Hal/Sdcard.h"
extern bool m5stack_lvgl_init();
extern bool m5stack_bootstrap();
extern bool m5stack_lvgl_init();
extern const tt::hal::Power m5stack_power;
extern const tt::hal::sdcard::SdCard m5stack_sdcard;
+38
View File
@@ -0,0 +1,38 @@
#include "Hal/Power.h"
#include "M5Unified.hpp"
/**
* 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 charging_enabled = true;
static bool is_charging() {
return M5.Power.isCharging() == m5::Power_Class::is_charging;
}
static bool is_charging_enabled() {
return charging_enabled;
}
static void set_charging_enabled(bool enabled) {
charging_enabled = enabled; // Local shadow copy because M5 API doesn't provide a function for it
M5.Power.setBatteryCharge(enabled);
}
static uint8_t get_charge_level() {
uint16_t scaled = (uint16_t)M5.Power.getBatteryLevel() * 255 / 100;
return (uint8_t)scaled;
}
static int32_t get_current() {
return M5.Power.getBatteryCurrent();
}
extern const tt::hal::Power m5stack_power = {
.is_charging = &is_charging,
.is_charging_enabled = &is_charging_enabled,
.set_charging_enabled = &set_charging_enabled,
.get_charge_level = &get_charge_level,
.get_current = &get_current
};
+138
View File
@@ -0,0 +1,138 @@
#include "Check.h"
#include "Log.h"
#include "Hal/Sdcard.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#define TAG "m5stack_sdcard"
#define SDCARD_SPI_HOST SPI2_HOST
#define SDCARD_PIN_CS GPIO_NUM_4
#define SDCARD_SPI_FREQUENCY 800000U
#define SDCARD_FORMAT_ON_MOUNT_FAILED false
#define SDCARD_MAX_OPEN_FILES 4
#define SDCARD_ALLOC_UNIT_SIZE (16 * 1024)
#define SDCARD_STATUS_CHECK_ENABLED false
typedef struct {
const char* mount_point;
sdmmc_card_t* card;
} MountData;
/**
* Before we can initialize the sdcard's SPI communications, we have to set all
* other SPI pins on the board high.
* See https://github.com/espressif/esp-idf/issues/1597
* See https://github.com/Xinyuan-LilyGO/T-Deck/blob/master/examples/UnitTest/UnitTest.ino
* @return success result
*/
static bool sdcard_init() {
TT_LOG_D(TAG, "init");
gpio_config_t config = {
.pin_bit_mask = BIT64(GPIO_NUM_4) | BIT64(GPIO_NUM_5),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
if (gpio_config(&config) != ESP_OK) {
TT_LOG_E(TAG, "GPIO init failed");
return false;
}
if (gpio_set_level(GPIO_NUM_4, 1) != ESP_OK) {
TT_LOG_E(TAG, "Failed to set board CS pin high");
return false;
}
if (gpio_set_level(GPIO_NUM_5, 1) != ESP_OK) {
TT_LOG_E(TAG, "Failed to set board CS pin high");
return false;
}
return true;
}
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 = SDCARD_FORMAT_ON_MOUNT_FAILED,
.max_files = SDCARD_MAX_OPEN_FILES,
.allocation_unit_size = SDCARD_ALLOC_UNIT_SIZE,
.disk_status_check_enable = SDCARD_STATUS_CHECK_ENABLED,
.use_one_fat = false
};
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 = SDCARD_PIN_CS;
slot_config.host_id = SDCARD_SPI_HOST;
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.max_freq_khz = 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 nullptr;
}
auto* data = static_cast<MountData*>(malloc(sizeof(MountData)));
*data = (MountData) {
.mount_point = mount_point,
.card = card,
};
return data;
}
static void* sdcard_init_and_mount(const char* mount_point) {
if (!sdcard_init()) {
TT_LOG_E(TAG, "Failed to set SPI CS pins high. This is a pre-requisite for mounting.");
return NULL;
}
auto* data = static_cast<MountData*>(sdcard_mount(mount_point));
if (data == nullptr) {
TT_LOG_E(TAG, "Mount failed for %s", mount_point);
return nullptr;
}
sdmmc_card_print_info(stdout, data->card);
return data;
}
static void sdcard_unmount(void* context) {
auto* data = static_cast<MountData*>(context);
TT_LOG_I(TAG, "Unmounting %s", data->mount_point);
tt_assert(data != nullptr);
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) {
auto* data = static_cast<MountData*>(context);
return (data != nullptr) && (sdmmc_get_status(data->card) == ESP_OK);
}
extern const tt::hal::sdcard::SdCard m5stack_sdcard = {
.mount = &sdcard_init_and_mount,
.unmount = &sdcard_unmount,
.is_mounted = &sdcard_is_mounted,
.mount_behaviour = tt::hal::sdcard::MountBehaviourAnytime
};
@@ -1,38 +0,0 @@
#include "Hal/Power.h"
#include "M5Unified.hpp"
/**
* 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();
}
extern const tt::hal::Power m5stack_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
};