SPI HAL implemented and more (#207)
- Cleanup unused code and move ISR/IRQ checks to `Kernel.h` - Improve clang-format - Fix for LVGL lock transfer: ensure lock isn't activate when changing the lock - Implement SPI HAL - Remove `initHardware` HAL configuration entry - Fix `I2cScanner`: don't scan when port isn't started
This commit is contained in:
committed by
GitHub
parent
88b3bfbe3e
commit
c1f55429b6
@@ -3,6 +3,7 @@
|
||||
#include "./Power.h"
|
||||
#include "./SdCard.h"
|
||||
#include "./i2c/I2c.h"
|
||||
#include "Tactility/hal/spi/Spi.h"
|
||||
|
||||
namespace tt::hal {
|
||||
|
||||
@@ -23,41 +24,26 @@ struct Configuration {
|
||||
*/
|
||||
const InitBoot _Nullable initBoot = 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)
|
||||
*/
|
||||
/** Create and initialize all LVGL devices. (e.g. display, touch, keyboard) */
|
||||
const InitLvgl _Nullable initLvgl = nullptr;
|
||||
|
||||
/**
|
||||
* Display HAL functionality.
|
||||
*/
|
||||
/** Display HAL functionality. */
|
||||
const CreateDisplay _Nullable createDisplay = nullptr;
|
||||
|
||||
/**
|
||||
* Display HAL functionality.
|
||||
*/
|
||||
/** Display HAL functionality. */
|
||||
const CreateKeyboard _Nullable createKeyboard = nullptr;
|
||||
|
||||
/**
|
||||
* An optional SD card interface.
|
||||
*/
|
||||
/** An optional SD card interface. */
|
||||
const std::shared_ptr<SdCard> _Nullable sdcard = nullptr;
|
||||
|
||||
/**
|
||||
* An optional power interface for battery or other power delivery.
|
||||
*/
|
||||
/** An optional power interface for battery or other power delivery. */
|
||||
const CreatePower _Nullable power = nullptr;
|
||||
|
||||
/**
|
||||
* A list of i2c interfaces
|
||||
*/
|
||||
/** A list of I2C interfaces */
|
||||
const std::vector<i2c::Configuration> i2c = {};
|
||||
|
||||
/** A list of SPI interfaces */
|
||||
const std::vector<spi::Configuration> spi = {};
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -38,6 +38,7 @@ enum class Status {
|
||||
|
||||
bool init(const std::vector<i2c::Configuration>& configurations);
|
||||
|
||||
bool configure(i2c_port_t port, const i2c_config_t& configuration);
|
||||
bool start(i2c_port_t port);
|
||||
bool stop(i2c_port_t port);
|
||||
bool isStarted(i2c_port_t port);
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "SpiCompat.h"
|
||||
|
||||
#include <Tactility/Lockable.h>
|
||||
#include <Tactility/RtosCompat.h>
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace tt::hal::spi {
|
||||
|
||||
enum class InitMode {
|
||||
ByTactility, // Tactility will initialize it in the correct bootup phase
|
||||
ByExternal, // The device is already initialized and Tactility should assume it works
|
||||
Disabled // Not initialized by default
|
||||
};
|
||||
|
||||
struct Configuration {
|
||||
spi_host_device_t device;
|
||||
spi_common_dma_t dma;
|
||||
spi_bus_config_t config;
|
||||
/** 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;
|
||||
/** Optional custom lock */
|
||||
std::shared_ptr<Lockable> _Nullable lock;
|
||||
};
|
||||
|
||||
enum class Status {
|
||||
Started,
|
||||
Stopped,
|
||||
Unknown
|
||||
};
|
||||
|
||||
bool init(const std::vector<spi::Configuration>& configurations);
|
||||
|
||||
bool start(spi_host_device_t device);
|
||||
bool stop(spi_host_device_t device);
|
||||
bool isStarted(spi_host_device_t device);
|
||||
|
||||
bool lock(spi_host_device_t device, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
bool unlock(spi_host_device_t device);
|
||||
|
||||
} // namespace tt::hal::spi
|
||||
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <driver/spi_common.h>
|
||||
#else
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
enum spi_host_device_t {
|
||||
SPI1_HOST = 0,
|
||||
SPI2_HOST = 1,
|
||||
SPI3_HOST = 2,
|
||||
SPI_HOST_MAX,
|
||||
};
|
||||
|
||||
enum spi_common_dma_t {
|
||||
SPI_DMA_DISABLED = 0, ///< No DMA
|
||||
SPI_DMA_CH1 = 1, ///< DMA, select DMA Channel 1
|
||||
SPI_DMA_CH2 = 2, ///< DMA, select DMA Channel 2
|
||||
SPI_DMA_CH_AUTO = 3, ///< DMA, channel selected by driver
|
||||
};
|
||||
|
||||
enum esp_intr_cpu_affinity_t {
|
||||
ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
ESP_INTR_CPU_AFFINITY_0,
|
||||
ESP_INTR_CPU_AFFINITY_1,
|
||||
};
|
||||
|
||||
struct spi_bus_config_t {
|
||||
union {
|
||||
int mosi_io_num;
|
||||
int data0_io_num;
|
||||
};
|
||||
union {
|
||||
int miso_io_num;
|
||||
int data1_io_num;
|
||||
};
|
||||
int sclk_io_num;
|
||||
union {
|
||||
int quadwp_io_num;
|
||||
int data2_io_num;
|
||||
};
|
||||
union {
|
||||
int quadhd_io_num;
|
||||
int data3_io_num;
|
||||
};
|
||||
int data4_io_num;
|
||||
int data5_io_num;
|
||||
int data6_io_num;
|
||||
int data7_io_num;
|
||||
bool data_io_default_level;
|
||||
int max_transfer_sz;
|
||||
uint32_t flags;
|
||||
esp_intr_cpu_affinity_t isr_cpu_id;
|
||||
int intr_flags;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -9,6 +9,8 @@ enum class SystemEvent {
|
||||
BootInitHalEnd,
|
||||
BootInitI2cBegin,
|
||||
BootInitI2cEnd,
|
||||
BootInitSpiBegin,
|
||||
BootInitSpiEnd,
|
||||
BootInitLvglBegin,
|
||||
BootInitLvglEnd,
|
||||
BootSplash,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "Tactility/hal/Device.h"
|
||||
#include "Tactility/hal/Hal_i.h"
|
||||
#include "Tactility/hal/i2c/I2c.h"
|
||||
#include "Tactility/hal/spi/Spi.h"
|
||||
|
||||
#include <Tactility/kernel/SystemEvents.h>
|
||||
|
||||
@@ -13,12 +14,12 @@ void init(const Configuration& configuration) {
|
||||
|
||||
kernel::systemEventPublish(kernel::SystemEvent::BootInitI2cBegin);
|
||||
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");
|
||||
}
|
||||
kernel::systemEventPublish(kernel::SystemEvent::BootInitI2cEnd);
|
||||
|
||||
kernel::systemEventPublish(kernel::SystemEvent::BootInitSpiBegin);
|
||||
tt_check(spi::init(configuration.spi), "SPI init failed");
|
||||
kernel::systemEventPublish(kernel::SystemEvent::BootInitSpiEnd);
|
||||
|
||||
if (configuration.initBoot != nullptr) {
|
||||
TT_LOG_I(TAG, "Init power");
|
||||
tt_check(configuration.initBoot(), "Init power failed");
|
||||
|
||||
@@ -7,22 +7,21 @@
|
||||
|
||||
#include <esp_check.h>
|
||||
|
||||
#define TAG "i2c"
|
||||
|
||||
namespace tt::hal::i2c {
|
||||
|
||||
static const uint8_t ACK_CHECK_EN = 1;
|
||||
|
||||
typedef struct Data {
|
||||
struct Data {
|
||||
Mutex mutex;
|
||||
bool isConfigured = false;
|
||||
bool isStarted = false;
|
||||
Configuration configuration;
|
||||
} Data;
|
||||
};
|
||||
|
||||
static const uint8_t ACK_CHECK_EN = 1;
|
||||
static Data dataArray[I2C_NUM_MAX];
|
||||
|
||||
#define TAG "i2c"
|
||||
|
||||
const char* initModeToString(InitMode mode) {
|
||||
static const char* initModeToString(InitMode mode) {
|
||||
switch (mode) {
|
||||
using enum InitMode;
|
||||
case ByTactility:
|
||||
@@ -35,7 +34,7 @@ const char* initModeToString(InitMode mode) {
|
||||
tt_crash("not implemented");
|
||||
}
|
||||
|
||||
void printInfo(const Data& data) {
|
||||
static 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);
|
||||
@@ -76,13 +75,13 @@ 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 ESP_ERR_INVALID_STATE;
|
||||
return false;
|
||||
} else if (!data.configuration.hasMutableConfiguration) {
|
||||
TT_LOG_E(TAG, "(%d) Mutation not allowed by original configuration", port);
|
||||
return ESP_ERR_NOT_ALLOWED;
|
||||
return false;
|
||||
} else {
|
||||
data.configuration.config = configuration;
|
||||
return ESP_OK;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ static bool configureLocked(i2c_port_t port, const i2c_config_t& configuration)
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t configure(i2c_port_t port, const i2c_config_t& configuration) {
|
||||
bool configure(i2c_port_t port, const i2c_config_t& configuration) {
|
||||
lock(port);
|
||||
bool result = configureLocked(port, configuration);
|
||||
unlock(port);
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
#include "Tactility/hal/spi/Spi.h"
|
||||
|
||||
#include <Tactility/Mutex.h>
|
||||
|
||||
#define TAG "spi"
|
||||
|
||||
namespace tt::hal::spi {
|
||||
|
||||
struct Data {
|
||||
std::shared_ptr<Lockable> lock;
|
||||
bool isConfigured = false;
|
||||
bool isStarted = false;
|
||||
Configuration configuration;
|
||||
};
|
||||
|
||||
static Data dataArray[SPI_HOST_MAX];
|
||||
|
||||
static const char* initModeToString(InitMode mode) {
|
||||
switch (mode) {
|
||||
using enum InitMode;
|
||||
case ByTactility:
|
||||
return TT_STRINGIFY(InitMode::ByTactility);
|
||||
case ByExternal:
|
||||
return TT_STRINGIFY(InitMode::ByExternal);
|
||||
case Disabled:
|
||||
return TT_STRINGIFY(InitMode::Disabled);
|
||||
}
|
||||
tt_crash("not implemented");
|
||||
}
|
||||
|
||||
static void printInfo(const Data& data) {
|
||||
TT_LOG_V(TAG, "SPI info for device %d", data.configuration.device);
|
||||
TT_LOG_V(TAG, " isStarted: %d", data.isStarted);
|
||||
TT_LOG_V(TAG, " isConfigured: %d", data.isConfigured);
|
||||
TT_LOG_V(TAG, " initMode: %s", initModeToString(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, " MISO pin: %d", data.configuration.config.miso_io_num);
|
||||
TT_LOG_V(TAG, " MOSI pin: %d", data.configuration.config.mosi_io_num);
|
||||
TT_LOG_V(TAG, " SCLK pin: %d", data.configuration.config.sclk_io_num);
|
||||
}
|
||||
|
||||
bool init(const std::vector<spi::Configuration>& configurations) {
|
||||
TT_LOG_I(TAG, "Init");
|
||||
for (const auto& configuration: configurations) {
|
||||
Data& data = dataArray[configuration.device];
|
||||
data.configuration = configuration;
|
||||
data.isConfigured = true;
|
||||
if (configuration.lock != nullptr) {
|
||||
data.lock = configuration.lock;
|
||||
} else {
|
||||
data.lock = std::make_shared<Mutex>();
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& config: configurations) {
|
||||
printInfo(dataArray[config.device]);
|
||||
if (config.initMode == InitMode::ByTactility) {
|
||||
if (!start(config.device)) {
|
||||
return false;
|
||||
}
|
||||
} else if (config.initMode == InitMode::ByExternal) {
|
||||
dataArray[config.device].isStarted = true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool configureLocked(spi_host_device_t device, const spi_bus_config_t& configuration) {
|
||||
Data& data = dataArray[device];
|
||||
if (data.isStarted) {
|
||||
TT_LOG_E(TAG, "(%d) Cannot reconfigure while interface is started", device);
|
||||
return false;
|
||||
} else if (!data.configuration.hasMutableConfiguration) {
|
||||
TT_LOG_E(TAG, "(%d) Mutation not allowed by original configuration", device);
|
||||
return false;
|
||||
} else {
|
||||
data.configuration.config = configuration;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool configure(spi_host_device_t device, const spi_bus_config_t& configuration) {
|
||||
if (lock(device)) {
|
||||
bool result = configureLocked(device, configuration);
|
||||
unlock(device);
|
||||
return result;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", device);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool startLocked(spi_host_device_t device) {
|
||||
Data& data = dataArray[device];
|
||||
printInfo(data);
|
||||
|
||||
if (data.isStarted) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Already started", device);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!data.isConfigured) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Not configured", device);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
Configuration& config = data.configuration;
|
||||
auto result = spi_bus_initialize(device, &data.configuration.config, data.configuration.dma);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Failed to initialize: %s", device, esp_err_to_name(result));
|
||||
return false;
|
||||
} else {
|
||||
data.isStarted = true;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
data.isStarted = true;
|
||||
|
||||
#endif
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Started", device);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool start(spi_host_device_t device) {
|
||||
if (lock(device)) {
|
||||
bool result = startLocked(device);
|
||||
unlock(device);
|
||||
return result;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", device);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool stopLocked(spi_host_device_t device) {
|
||||
Data& data = dataArray[device];
|
||||
Configuration& config = data.configuration;
|
||||
|
||||
if (!config.canReinit) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Not allowed to re-init", device);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!data.isStarted) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Not started", device);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
auto result = spi_bus_free(device);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Failed to free device: %s", device, esp_err_to_name(result));
|
||||
return false;
|
||||
} else {
|
||||
data.isStarted = false;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
data.isStarted = false;
|
||||
|
||||
#endif
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Stopped", device);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool stop(spi_host_device_t device) {
|
||||
if (lock(device)) {
|
||||
bool result = stopLocked(device);
|
||||
unlock(device);
|
||||
return result;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", device);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool isStarted(spi_host_device_t device) {
|
||||
if (lock(device, 50 / portTICK_PERIOD_MS)) {
|
||||
bool started = dataArray[device].isStarted;
|
||||
unlock(device);
|
||||
return started;
|
||||
} else {
|
||||
// If we can't get a lock, we assume the device is busy and thus has started
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool lock(spi_host_device_t device, TickType_t timeout) {
|
||||
return dataArray[device].lock->lock(timeout);
|
||||
}
|
||||
|
||||
bool unlock(spi_host_device_t device) {
|
||||
return dataArray[device].lock->unlock();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,6 +30,10 @@ static const char* getEventName(SystemEvent event) {
|
||||
return TT_STRINGIFY(BootInitI2cBegin);
|
||||
case BootInitI2cEnd:
|
||||
return TT_STRINGIFY(BootInitI2cEnd);
|
||||
case BootInitSpiBegin:
|
||||
return TT_STRINGIFY(BootInitSpiBegin);
|
||||
case BootInitSpiEnd:
|
||||
return TT_STRINGIFY(BootInitSpiEnd);
|
||||
case BootInitLvglBegin:
|
||||
return TT_STRINGIFY(BootInitLvglBegin);
|
||||
case BootInitLvglEnd:
|
||||
|
||||
Reference in New Issue
Block a user