I2C Implementation (#84)
This commit is contained in:
committed by
GitHub
parent
881c8517bf
commit
d8731eaa17
@@ -1,14 +1,49 @@
|
||||
#include "M5stackCore2.h"
|
||||
#include "m5stack_shared.h"
|
||||
|
||||
extern const tt::hal::sdcard::SdCard m5stack_core2_sdcard;
|
||||
#include "M5stackShared.h"
|
||||
|
||||
extern const tt::hal::Configuration m5stack_core2 = {
|
||||
.bootstrap = &m5stack_bootstrap,
|
||||
.init_graphics = &m5stack_lvgl_init,
|
||||
.display = {
|
||||
.set_backlight_duty = nullptr
|
||||
},
|
||||
.sdcard = &m5stack_core2_sdcard,
|
||||
.power = &m5stack_power
|
||||
.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_21,
|
||||
.scl_io_num = GPIO_NUM_22,
|
||||
.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_32,
|
||||
.scl_io_num = GPIO_NUM_33,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master = {
|
||||
.clk_speed = 400000
|
||||
},
|
||||
.clk_flags = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
#include "Check.h"
|
||||
#include "Log.h"
|
||||
#include "Hal/Sdcard.h"
|
||||
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "sdmmc_cmd.h"
|
||||
|
||||
#define TAG "m5stack_core2_sdcard"
|
||||
|
||||
#define CORE2_SDCARD_SPI_HOST SPI2_HOST
|
||||
#define CORE2_SDCARD_PIN_CS GPIO_NUM_4
|
||||
#define CORE2_SDCARD_SPI_FREQUENCY 800000U
|
||||
#define CORE2_SDCARD_FORMAT_ON_MOUNT_FAILED false
|
||||
#define CORE2_SDCARD_MAX_OPEN_FILES 4
|
||||
#define CORE2_SDCARD_ALLOC_UNIT_SIZE (16 * 1024)
|
||||
#define CORE2_SDCARD_STATUS_CHECK_ENABLED false
|
||||
|
||||
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 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_core2_sdcard = {
|
||||
.mount = &sdcard_mount,
|
||||
.unmount = &sdcard_unmount,
|
||||
.is_mounted = &sdcard_is_mounted,
|
||||
.mount_behaviour = tt::hal::sdcard::MountBehaviourAnytime
|
||||
};
|
||||
Reference in New Issue
Block a user