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
-1
View File
@@ -1,6 +1,5 @@
idf_component_register(
SRC_DIRS "Source"
INCLUDE_DIRS "Source"
PRIV_INCLUDE_DIRS "Private"
REQUIRES Tactility M5stackShared vfs fatfs M5Unified
)
-13
View File
@@ -1,13 +0,0 @@
#pragma once
#include "driver/spi_common.h"
#include "driver/gpio.h"
// SD Card
#define CORES3_SDCARD_SPI_HOST SPI2_HOST
#define CORES3_SDCARD_PIN_CS GPIO_NUM_4
#define CORES3_SDCARD_SPI_FREQUENCY 800000U
#define CORES3_SDCARD_FORMAT_ON_MOUNT_FAILED false
#define CORES3_SDCARD_MAX_OPEN_FILES 4
#define CORES3_SDCARD_ALLOC_UNIT_SIZE (16 * 1024)
#define CORES3_SDCARD_STATUS_CHECK_ENABLED false
@@ -0,0 +1,51 @@
#include "M5stackCoreS3.h"
#include "M5stackShared.h"
extern const tt::hal::sdcard::SdCard m5stack_cores3_sdcard;
const tt::hal::Configuration m5stack_cores3 = {
.initPower = &m5stack_bootstrap,
.initLvgl = &m5stack_lvgl_init,
.sdcard = &m5stack_sdcard,
.power = &m5stack_power,
.i2c = {
// Internal
tt::hal::i2c::Configuration {
.port = I2C_NUM_0,
.initMode = tt::hal::i2c::InitByExternal,
.canReinit = false,
.hasMutableConfiguration = false,
.timeout = 1000,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_12,
.scl_io_num = GPIO_NUM_11,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 400000
},
.clk_flags = 0
}
},
// External (Grove)
tt::hal::i2c::Configuration {
.port = I2C_NUM_1,
.initMode = tt::hal::i2c::InitByExternal,
.canReinit = true,
.hasMutableConfiguration = true,
.timeout = 1000,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_2,
.scl_io_num = GPIO_NUM_1,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 400000
},
.clk_flags = 0
}
}
}
};
@@ -1,14 +0,0 @@
#include "m5stack_cores3.h"
#include "m5stack_shared.h"
extern const tt::hal::sdcard::SdCard m5stack_cores3_sdcard;
const tt::hal::Configuration m5stack_cores3 = {
.bootstrap = &m5stack_bootstrap,
.init_graphics = &m5stack_lvgl_init,
.display = {
.set_backlight_duty = nullptr
},
.sdcard = &m5stack_cores3_sdcard,
.power = &m5stack_power
};
@@ -1,79 +0,0 @@
#include "Hal/Sdcard.h"
#include "Check.h"
#include "Log.h"
#include "config.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#define TAG "m5stack_cores3_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 = CORES3_SDCARD_FORMAT_ON_MOUNT_FAILED,
.max_files = CORES3_SDCARD_MAX_OPEN_FILES,
.allocation_unit_size = CORES3_SDCARD_ALLOC_UNIT_SIZE,
.disk_status_check_enable = CORES3_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 = CORES3_SDCARD_PIN_CS;
slot_config.host_id = CORES3_SDCARD_SPI_HOST;
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.max_freq_khz = CORES3_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
};
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_cores3_sdcard = {
.mount = &sdcard_mount,
.unmount = &sdcard_unmount,
.is_mounted = &sdcard_is_mounted,
.mount_behaviour = tt::hal::sdcard::MountBehaviourAnytime
};