I2C Implementation (#84)
This commit is contained in:
committed by
GitHub
parent
881c8517bf
commit
d8731eaa17
@@ -1,36 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include "TactilityCore.h"
|
||||
#include "Sdcard.h"
|
||||
#include "Power.h"
|
||||
#include "Sdcard.h"
|
||||
#include "I2c/I2c.h"
|
||||
#include "TactilityCore.h"
|
||||
|
||||
namespace tt::hal {
|
||||
|
||||
typedef bool (*Bootstrap)();
|
||||
typedef bool (*InitGraphics)();
|
||||
typedef bool (*InitPower)();
|
||||
typedef bool (*InitHardware)();
|
||||
typedef bool (*InitLvgl)();
|
||||
|
||||
typedef void (*SetBacklightDuty)(uint8_t);
|
||||
typedef struct {
|
||||
/** Set backlight duty */
|
||||
SetBacklightDuty set_backlight_duty;
|
||||
_Nullable SetBacklightDuty setBacklightDuty;
|
||||
} Display;
|
||||
|
||||
typedef struct {
|
||||
/**
|
||||
* Optional bootstrapping method (e.g. to turn peripherals on)
|
||||
* This is called after Tactility core init and before any other inits in the HardwareConfig.
|
||||
* */
|
||||
const Bootstrap _Nullable bootstrap;
|
||||
|
||||
/**
|
||||
* Initializes LVGL with all relevant hardware.
|
||||
* This includes the display and optional pointer devices (such as touch) or a keyboard.
|
||||
* Called before I2C/SPI/etc is initialized.
|
||||
* Used for powering on the peripherals manually.
|
||||
*/
|
||||
const InitGraphics init_graphics;
|
||||
const InitPower _Nullable initPower = nullptr;
|
||||
|
||||
/**
|
||||
* An interface for display features such as setting the backlight.
|
||||
* This does nothing when a display isn't present.
|
||||
* Called after I2C/SPI/etc is initialized.
|
||||
* This can be used to communicate with built-in peripherals such as an I2C keyboard.
|
||||
*/
|
||||
const InitHardware _Nullable initHardware = nullptr;
|
||||
|
||||
/**
|
||||
* Create and initialize all LVGL devices. (e.g. display, touch, keyboard)
|
||||
*/
|
||||
const InitLvgl initLvgl;
|
||||
|
||||
/**
|
||||
* Display HAL functionality.
|
||||
*/
|
||||
const Display display;
|
||||
|
||||
@@ -43,6 +49,11 @@ typedef struct {
|
||||
* An optional power interface for battery or other power delivery.
|
||||
*/
|
||||
const Power* _Nullable power;
|
||||
|
||||
/**
|
||||
* A list of i2c devices (can be empty, but preferably accurately represents the device capabilities)
|
||||
*/
|
||||
const std::vector<i2c::Configuration> i2c;
|
||||
} Configuration;
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -1,22 +1,31 @@
|
||||
#include "Hal/Hal_i.h"
|
||||
#include "Hal/I2c/I2c.h"
|
||||
|
||||
#define TAG "hardware"
|
||||
|
||||
namespace tt::hal {
|
||||
|
||||
void init(const Configuration* configuration) {
|
||||
if (configuration->bootstrap != nullptr) {
|
||||
TT_LOG_I(TAG, "Bootstrapping");
|
||||
tt_check(configuration->bootstrap(), "bootstrap failed");
|
||||
void init(const Configuration& configuration) {
|
||||
if (configuration.initPower != nullptr) {
|
||||
TT_LOG_I(TAG, "Init power");
|
||||
tt_check(configuration.initPower(), "Init power failed");
|
||||
}
|
||||
|
||||
if (configuration->sdcard != nullptr) {
|
||||
tt_check(i2c::init(configuration.i2c), "I2C init failed");
|
||||
if (configuration.initHardware != nullptr) {
|
||||
TT_LOG_I(TAG, "Init hardware");
|
||||
tt_check(configuration.initHardware(), "Hardware init failed");
|
||||
}
|
||||
|
||||
if (configuration.sdcard != nullptr) {
|
||||
TT_LOG_I(TAG, "Mounting sdcard");
|
||||
sdcard::mount(configuration->sdcard);
|
||||
if (!sdcard::mount(configuration.sdcard)) {
|
||||
TT_LOG_W(TAG, "SD card mount failed (init can continue)");
|
||||
}
|
||||
}
|
||||
|
||||
tt_check(configuration->init_graphics, "Graphics init not set");
|
||||
tt_check(configuration->init_graphics(), "Graphics init failed");
|
||||
tt_check(configuration.initLvgl, "Graphics init not set");
|
||||
tt_check(configuration.initLvgl(), "Graphics init failed");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#include "I2c.h"
|
||||
#include "Log.h"
|
||||
#include "Mutex.h"
|
||||
|
||||
#include <esp_check.h>
|
||||
|
||||
namespace tt::hal::i2c {
|
||||
|
||||
typedef struct Data {
|
||||
Mutex mutex;
|
||||
bool isConfigured = false;
|
||||
bool isStarted = false;
|
||||
Configuration configuration;
|
||||
} Data;
|
||||
|
||||
static Data dataArray[I2C_NUM_MAX];
|
||||
|
||||
#define TAG "i2c"
|
||||
|
||||
void printInfo(const Data& data) {
|
||||
TT_LOG_V(TAG, "I2C info for port %d", data.configuration.port);
|
||||
TT_LOG_V(TAG, " isStarted: %d", data.isStarted);
|
||||
TT_LOG_V(TAG, " isConfigured: %d", data.isConfigured);
|
||||
TT_LOG_V(TAG, " initMode: %d", data.configuration.initMode);
|
||||
TT_LOG_V(TAG, " canReinit: %d", data.configuration.canReinit);
|
||||
TT_LOG_V(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration);
|
||||
TT_LOG_V(TAG, " SDA pin: %d", data.configuration.config.sda_io_num);
|
||||
TT_LOG_V(TAG, " SCL pin: %d", data.configuration.config.scl_io_num);
|
||||
}
|
||||
|
||||
bool init(const std::vector<i2c::Configuration>& configurations) {
|
||||
TT_LOG_I(TAG, "Init");
|
||||
for (const auto& configuration: configurations) {
|
||||
if (configuration.config.mode != I2C_MODE_MASTER) {
|
||||
TT_LOG_E(TAG, "Currently only master mode is supported");
|
||||
return false;
|
||||
}
|
||||
Data& data = dataArray[configuration.port];
|
||||
data.configuration = configuration;
|
||||
data.isConfigured = true;
|
||||
}
|
||||
|
||||
for (const auto& config: configurations) {
|
||||
printInfo(dataArray[config.port]);
|
||||
if (config.initMode == InitByTactility) {
|
||||
if (!start(config.port)) {
|
||||
return false;
|
||||
}
|
||||
} else if (config.initMode == InitByExternal) {
|
||||
dataArray[config.port].isStarted = true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool configure_locked(i2c_port_t port, const i2c_config_t& configuration) {
|
||||
Data& data = dataArray[port];
|
||||
if (data.isStarted) {
|
||||
TT_LOG_E(TAG, "(%d) Cannot reconfigure while interface is started", port);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
} else if (!data.configuration.hasMutableConfiguration) {
|
||||
TT_LOG_E(TAG, "(%d) Mutation not allowed by original configuration", port);
|
||||
return ESP_ERR_NOT_ALLOWED;
|
||||
} else {
|
||||
data.configuration.config = configuration;
|
||||
return ESP_OK;
|
||||
}
|
||||
}
|
||||
|
||||
bool configure(i2c_port_t port, const i2c_config_t& configuration) {
|
||||
lock(port);
|
||||
bool result = configure_locked(port, configuration);
|
||||
unlock(port);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool startLocked(i2c_port_t port) {
|
||||
Data& data = dataArray[port];
|
||||
printInfo(data);
|
||||
Configuration& config = data.configuration;
|
||||
|
||||
if (data.isStarted) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Already started", port);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!data.isConfigured) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Not configured", port);
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_err_t result = i2c_param_config(port, &config.config);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Failed to configure: %s", port, esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
result = i2c_driver_install(port, config.config.mode, 0, 0, 0);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Failed to install driver: %s", port, esp_err_to_name(result));
|
||||
return false;
|
||||
} else {
|
||||
data.isStarted = true;
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Started", port);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool start(i2c_port_t port) {
|
||||
lock(port);
|
||||
bool result = startLocked(port);
|
||||
unlock(port);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool stopLocked(i2c_port_t port) {
|
||||
Data& data = dataArray[port];
|
||||
Configuration& config = data.configuration;
|
||||
|
||||
if (!config.canReinit) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Not allowed to re-init", port);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!data.isStarted) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Not started", port);
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_err_t result = i2c_driver_delete(port);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Failed to delete driver: %s", port, esp_err_to_name(result));
|
||||
return false;
|
||||
} else {
|
||||
data.isStarted = false;
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Stopped", port);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool stop(i2c_port_t port) {
|
||||
lock(port);
|
||||
bool result = stopLocked(port);
|
||||
unlock(port);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool isStarted(i2c_port_t port) {
|
||||
lock(port);
|
||||
bool started = dataArray[port].isStarted;
|
||||
unlock(port);
|
||||
return started;
|
||||
}
|
||||
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize) {
|
||||
lock(port);
|
||||
esp_err_t result = i2c_master_read_from_device(port, address, data, dataSize, dataArray[port].configuration.timeout);
|
||||
unlock(port);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool masterWrite(i2c_port_t port, uint16_t address, const uint8_t* data, uint16_t dataSize) {
|
||||
lock(port);
|
||||
esp_err_t result = i2c_master_write_to_device(port, address, data, dataSize, dataArray[port].configuration.timeout);
|
||||
unlock(port);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize) {
|
||||
lock(port);
|
||||
esp_err_t result = i2c_master_write_read_device(port, address, writeData, writeDataSize, readData, readDataSize, dataArray[port].configuration.timeout);
|
||||
unlock(port);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
TtStatus lock(i2c_port_t port, uint32_t timeout) {
|
||||
return dataArray[port].mutex.acquire(timeout);
|
||||
}
|
||||
|
||||
TtStatus unlock(i2c_port_t port) {
|
||||
return dataArray[port].mutex.release();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "I2cCompat.h"
|
||||
#include <CoreTypes.h>
|
||||
#include <vector>
|
||||
|
||||
namespace tt::hal::i2c {
|
||||
|
||||
typedef enum {
|
||||
InitByTactility, // Tactility will initialize it in the correct bootup phase
|
||||
InitByExternal, // The device is already initialized and Tactility should assume it works
|
||||
InitDisabled // Not initialized by default
|
||||
} InitMode;
|
||||
|
||||
typedef struct {
|
||||
/** The port to operate on */
|
||||
i2c_port_t port;
|
||||
/** Whether this bus should be initialized when device starts up */
|
||||
InitMode initMode;
|
||||
/** Whether this bus can stopped and re-started. */
|
||||
bool canReinit;
|
||||
/** Whether configuration can be changed. */
|
||||
bool hasMutableConfiguration;
|
||||
/** Read/write timeout (not related to mutex locking mechanism) */
|
||||
TickType_t timeout;
|
||||
/** Configuration that must be valid when initAtBoot is set to true. */
|
||||
i2c_config_t config;
|
||||
} Configuration;
|
||||
|
||||
bool init(const std::vector<i2c::Configuration>& configurations);
|
||||
|
||||
bool start(i2c_port_t port);
|
||||
bool stop(i2c_port_t port);
|
||||
bool isStarted(i2c_port_t port);
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize);
|
||||
bool masterWrite(i2c_port_t port, uint16_t address, const uint8_t* data, uint16_t dataSize);
|
||||
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize);
|
||||
TtStatus lock(i2c_port_t port, uint32_t timeout = UINT_MAX);
|
||||
TtStatus unlock(i2c_port_t port);
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef ESP_TARGET
|
||||
|
||||
#include <driver/i2c.h>
|
||||
|
||||
#else
|
||||
|
||||
#include <cstdint>
|
||||
#include "portmacro.h"
|
||||
|
||||
typedef int esp_err_t;
|
||||
|
||||
typedef enum {
|
||||
I2C_NUM_0 = 0,
|
||||
I2C_NUM_1,
|
||||
LP_I2C_NUM_0,
|
||||
I2C_NUM_MAX,
|
||||
} i2c_port_t;
|
||||
|
||||
typedef enum{
|
||||
I2C_MODE_MASTER,
|
||||
I2C_MODE_MAX,
|
||||
} i2c_mode_t;
|
||||
|
||||
typedef struct {
|
||||
i2c_mode_t mode;
|
||||
int sda_io_num;
|
||||
int scl_io_num;
|
||||
bool sda_pullup_en;
|
||||
bool scl_pullup_en;
|
||||
|
||||
union {
|
||||
struct {
|
||||
uint32_t clk_speed;
|
||||
} master;
|
||||
};
|
||||
uint32_t clk_flags;
|
||||
} i2c_config_t;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,102 @@
|
||||
#ifndef ESP_TARGET
|
||||
|
||||
/**
|
||||
* This code is based on i2c_manager from https://github.com/ropg/i2c_manager/blob/master/i2c_manager/i2c_manager.c (original has MIT license)
|
||||
*/
|
||||
#include "I2c.h"
|
||||
#include "Log.h"
|
||||
#include "Mutex.h"
|
||||
|
||||
namespace tt::hal::i2c {
|
||||
|
||||
typedef struct Data {
|
||||
Mutex mutex;
|
||||
bool isConfigured = false;
|
||||
bool isStarted = false;
|
||||
Configuration configuration;
|
||||
} Data;
|
||||
|
||||
static Data dataArray[I2C_NUM_MAX];
|
||||
|
||||
#define TAG "i2c"
|
||||
|
||||
bool init(const std::vector<i2c::Configuration>& configurations) {
|
||||
TT_LOG_I(TAG, "Init");
|
||||
for (const auto& configuration: configurations) {
|
||||
Data& data = dataArray[configuration.port];
|
||||
data.configuration = configuration;
|
||||
data.isConfigured = true;
|
||||
}
|
||||
|
||||
for (const auto& config: configurations) {
|
||||
if (config.initMode == InitByTactility && !start(config.port)) {
|
||||
return false;
|
||||
} else if (config.initMode == InitByExternal) {
|
||||
dataArray[config.port].isStarted = true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool configure_locked(i2c_port_t port, const i2c_config_t& configuration) {
|
||||
Data& data = dataArray[port];
|
||||
if (data.isStarted) {
|
||||
TT_LOG_E(TAG, "(%d) Cannot reconfigure while interface is started", port);
|
||||
return false;
|
||||
} else if (!data.configuration.hasMutableConfiguration) {
|
||||
TT_LOG_E(TAG, "(%d) Mutation not allowed by original configuration", port);
|
||||
return false;
|
||||
} else {
|
||||
data.configuration.config = configuration;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t configure(i2c_port_t port, const i2c_config_t& configuration) {
|
||||
lock(port);
|
||||
bool result = configure_locked(port, configuration);
|
||||
unlock(port);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool start(i2c_port_t port) {
|
||||
lock(port);
|
||||
dataArray[port].isStarted = true;
|
||||
unlock(port);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool stop(i2c_port_t port) {
|
||||
lock(port);
|
||||
dataArray[port].isStarted = false;
|
||||
unlock(port);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isStarted(i2c_port_t port) {
|
||||
lock(port);
|
||||
bool started = dataArray[port].isStarted;
|
||||
unlock(port);
|
||||
return started;
|
||||
}
|
||||
|
||||
bool read(i2c_port_t port, uint16_t address, uint32_t reg, uint8_t* buffer, uint16_t size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool write(i2c_port_t port, uint16_t address, uint32_t reg, const uint8_t* buffer, uint16_t size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TtStatus lock(i2c_port_t port, uint32_t timeout) {
|
||||
return dataArray[port].mutex.acquire(timeout);
|
||||
}
|
||||
|
||||
TtStatus unlock(i2c_port_t port) {
|
||||
return dataArray[port].mutex.release();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
@@ -76,7 +76,7 @@ static int32_t sdcard_task(void* context) {
|
||||
}
|
||||
|
||||
static void on_start(Service& service) {
|
||||
if (get_hardware_config()->sdcard != nullptr) {
|
||||
if (get_hardware_config().sdcard != nullptr) {
|
||||
ServiceData* data = service_data_alloc();
|
||||
service.setData(data);
|
||||
data->thread->start();
|
||||
|
||||
@@ -30,7 +30,7 @@ typedef struct {
|
||||
} Wifi;
|
||||
|
||||
|
||||
static Wifi* wifi = NULL;
|
||||
static Wifi* wifi = nullptr;
|
||||
|
||||
// Forward declarations
|
||||
static void wifi_lock(Wifi* wifi);
|
||||
|
||||
@@ -31,17 +31,18 @@ static void register_and_start_system_services() {
|
||||
}
|
||||
}
|
||||
|
||||
void headless_init(const hal::Configuration* config) {
|
||||
void init(const hal::Configuration& config) {
|
||||
#ifdef ESP_PLATFORM
|
||||
esp_init();
|
||||
#endif
|
||||
hardwareConfig = config;
|
||||
hardwareConfig = &config;
|
||||
hal::init(config);
|
||||
register_and_start_system_services();
|
||||
}
|
||||
|
||||
const hal::Configuration* get_hardware_config() {
|
||||
return hardwareConfig;
|
||||
const hal::Configuration& get_hardware_config() {
|
||||
tt_assert(hardwareConfig != nullptr);
|
||||
return *hardwareConfig;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
namespace tt {
|
||||
|
||||
void headless_init(const hal::Configuration* config);
|
||||
void init(const hal::Configuration& config);
|
||||
|
||||
const hal::Configuration* get_hardware_config();
|
||||
const hal::Configuration& get_hardware_config();
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user