C++ refactoring (#91)

This commit is contained in:
Ken Van Hoeylandt
2024-11-26 17:51:05 +01:00
committed by GitHub
parent d06137ba76
commit ca5d4b8226
159 changed files with 904 additions and 1086 deletions
@@ -0,0 +1,58 @@
#pragma once
#include "Power.h"
#include "hal/sdcard/Sdcard.h"
#include "hal/i2c/I2c.h"
namespace tt::hal {
typedef bool (*InitPower)();
typedef bool (*InitHardware)();
typedef bool (*InitLvgl)();
typedef void (*SetBacklightDuty)(uint8_t);
typedef struct {
/** Set backlight duty */
_Nullable SetBacklightDuty setBacklightDuty;
} Display;
typedef struct {
/**
* Called before I2C/SPI/etc is initialized.
* Used for powering on the peripherals manually.
*/
const InitPower _Nullable initPower = nullptr;
/**
* 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 _Nullable initLvgl;
/**
* Display HAL functionality.
*/
const Display display;
/**
* An optional SD card interface.
*/
const sdcard::SdCard* _Nullable sdcard;
/**
* 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
+33
View File
@@ -0,0 +1,33 @@
#include "hal/Hal_i.h"
#include "hal/i2c/I2c.h"
#define TAG "hal"
namespace tt::hal {
void init(const Configuration& configuration) {
if (configuration.initPower != nullptr) {
TT_LOG_I(TAG, "Init power");
tt_check(configuration.initPower(), "Init power failed");
}
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");
if (!sdcard::mount(configuration.sdcard)) {
TT_LOG_W(TAG, "SD card mount failed (init can continue)");
}
}
if (configuration.initLvgl != nullptr) {
TT_LOG_I(TAG, "Init LVGL");
tt_check(configuration.initLvgl(), "LVGL init failed");
}
}
} // namespace
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include <cstdint>
namespace tt::hal {
typedef bool (*PowerIsCharging)();
typedef bool (*PowerIsChargingEnabled)();
typedef void (*PowerSetChargingEnabled)(bool enabled);
typedef uint8_t (*PowerGetBatteryCharge)(); // Power value [0, 255] which maps to 0-100% charge
typedef int32_t (*PowerGetCurrent)(); // Consumption or charge current in mAh
typedef struct {
PowerIsCharging isCharging;
PowerIsChargingEnabled isChargingEnabled;
PowerSetChargingEnabled setChargingEnabled;
PowerGetBatteryCharge getChargeLevel;
PowerGetCurrent getCurrent;
} Power;
} // namespace tt
+191
View File
@@ -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
+44
View File
@@ -0,0 +1,44 @@
#pragma once
#include "I2cCompat.h"
#include "CoreTypes.h"
#include <climits>
#include <string>
#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 {
std::string name;
/** 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) */
unsigned long 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,39 @@
#pragma once
#ifdef ESP_TARGET
#include <hal/i2c_types.h>
#include <driver/i2c.h>
#else
#include <cstdint>
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 configureLocked(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 = configureLocked(port, configuration);
unlock(port);
return result;
}
bool start(i2c_port_t port) {
lock(port);
dataArray[port].isStarted = true;
unlock(port);
return true;
}
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
@@ -0,0 +1,85 @@
#include "Sdcard.h"
#include "Mutex.h"
#include "TactilityCore.h"
namespace tt::hal::sdcard {
#define TAG "sdcard"
static Mutex mutex(MutexTypeRecursive);
typedef struct {
const SdCard* sdcard;
void* context;
} MountData;
static MountData data = {
.sdcard = nullptr,
.context = nullptr
};
static bool lock(uint32_t timeout_ticks) {
return mutex.acquire(timeout_ticks) == TtStatusOk;
}
static void unlock() {
mutex.release();
}
bool mount(const SdCard* sdcard) {
TT_LOG_I(TAG, "Mounting");
if (data.sdcard != nullptr) {
TT_LOG_E(TAG, "Failed to mount: already mounted");
return false;
}
if (lock(100)) {
void* context = sdcard->mount(TT_SDCARD_MOUNT_POINT);
data = (MountData) {
.sdcard = sdcard,
.context = context
};
unlock();
return (data.context != nullptr);
} else {
TT_LOG_E(TAG, "Failed to lock");
return false;
}
}
State getState() {
if (data.context == nullptr) {
return StateUnmounted;
} else if (data.sdcard->is_mounted(data.context)) {
return StateMounted;
} else {
return StateError;
}
}
bool unmount(uint32_t timeout_ticks) {
TT_LOG_I(TAG, "Unmounting");
bool result = false;
if (lock(timeout_ticks)) {
if (data.sdcard != nullptr) {
data.sdcard->unmount(data.context);
data = (MountData) {
.sdcard = nullptr,
.context = nullptr
};
result = true;
} else {
TT_LOG_E(TAG, "Can't unmount: nothing mounted");
}
unlock();
} else {
TT_LOG_E(TAG, "Failed to lock in %lu ticks", timeout_ticks);
}
return result;
}
} // namespace
@@ -0,0 +1,35 @@
#pragma once
#include "TactilityCore.h"
namespace tt::hal::sdcard {
#define TT_SDCARD_MOUNT_POINT "/sdcard"
typedef void* (*Mount)(const char* mount_path);
typedef void (*Unmount)(void* context);
typedef bool (*IsMounted)(void* context);
typedef enum {
StateMounted,
StateUnmounted,
StateError,
} State;
typedef enum {
MountBehaviourAtBoot, /** Only mount at boot */
MountBehaviourAnytime /** Mount/dismount any time */
} MountBehaviour;
typedef struct {
Mount mount;
Unmount unmount;
IsMounted is_mounted;
MountBehaviour mount_behaviour;
} SdCard;
bool mount(const SdCard* sdcard);
State getState();
bool unmount(uint32_t timeout_ticks);
} // namespace