mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-06-20 12:55:06 +00:00
- Add kernel support for SPI driver - Add kernel support for UART driver - Implemented ESP32 UART kernel driver - Update existing UART-related code in Tactility to use new kernel driver - Remove UART from tt::hal::Configuration - Remove tt_hal_uart functionality but keep functions for now - Update devicetrees for UART changes - Kernel mutex and recursive mutex: improved locking API design - Other kernel improvements - Added device_exists_of_type() and device_find_by_name()
112 lines
3.7 KiB
C++
112 lines
3.7 KiB
C++
#include <Tactility/hal/i2c/I2c.h>
|
|
|
|
#include <Tactility/Logger.h>
|
|
#include <Tactility/Mutex.h>
|
|
|
|
#include <tactility/check.h>
|
|
#include <tactility/drivers/i2c_controller.h>
|
|
#include <tactility/time.h>
|
|
|
|
#ifdef ESP_PLATFORM
|
|
#include <tactility/drivers/esp32_i2c.h>
|
|
#endif
|
|
|
|
namespace tt::hal::i2c {
|
|
|
|
class NoLock final : public tt::Lock {
|
|
bool lock(TickType_t timeout) const override { return true; }
|
|
void unlock() const override { /* NO-OP */ }
|
|
};
|
|
|
|
static NoLock NO_LOCK;
|
|
|
|
Device* findDevice(i2c_port_t port) {
|
|
#ifdef ESP_PLATFORM
|
|
struct Params {
|
|
i2c_port_t port;
|
|
Device* device;
|
|
};
|
|
|
|
Params params = {
|
|
.port = port,
|
|
.device = nullptr
|
|
};
|
|
|
|
device_for_each_of_type(&I2C_CONTROLLER_TYPE, ¶ms, [](auto* device, auto* context) {
|
|
auto* params_ptr = (Params*)context;
|
|
auto* driver = device_get_driver(device);
|
|
if (driver == nullptr) return true;
|
|
if (!driver_is_compatible(driver, "espressif,esp32-i2c")) return true;
|
|
auto* config = static_cast<const Esp32I2cConfig*>(device->config);
|
|
if (config->port != params_ptr->port) return true;
|
|
// Found it, stop iterating
|
|
params_ptr->device = device;
|
|
return false;
|
|
});
|
|
|
|
return params.device;
|
|
#else
|
|
return nullptr;
|
|
#endif
|
|
}
|
|
|
|
bool isStarted(i2c_port_t port) {
|
|
auto* device = findDevice(port);
|
|
if (device == nullptr) return false;
|
|
return device_is_ready(device);
|
|
}
|
|
|
|
const char* getName(i2c_port_t port) {
|
|
auto* device = findDevice(port);
|
|
if (device == nullptr) return nullptr;
|
|
return device->name;
|
|
}
|
|
|
|
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout) {
|
|
auto* device = findDevice(port);
|
|
if (device == nullptr) return false;
|
|
return i2c_controller_read(device, address, data, dataSize, timeout) == ERROR_NONE;
|
|
}
|
|
|
|
bool masterReadRegister(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout) {
|
|
auto* device = findDevice(port);
|
|
if (device == nullptr) return false;
|
|
return i2c_controller_read_register(device, address, reg, data, dataSize, timeout) == ERROR_NONE;
|
|
}
|
|
|
|
bool masterWrite(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
|
auto* device = findDevice(port);
|
|
if (device == nullptr) return false;
|
|
return i2c_controller_write(device, address, data, dataSize, timeout) == ERROR_NONE;
|
|
}
|
|
|
|
bool masterWriteRegister(i2c_port_t port, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
|
auto* device = findDevice(port);
|
|
if (device == nullptr) return false;
|
|
return i2c_controller_write_register(device, address, reg, data, dataSize, timeout) == ERROR_NONE;
|
|
}
|
|
|
|
bool masterWriteRegisterArray(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
|
auto* device = findDevice(port);
|
|
if (device == nullptr) return false;
|
|
return i2c_controller_write_register_array(device, address, data, dataSize, timeout) == ERROR_NONE;
|
|
}
|
|
|
|
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize, TickType_t timeout) {
|
|
auto* device = findDevice(port);
|
|
if (device == nullptr) return false;
|
|
return i2c_controller_write_read(device, address, writeData, writeDataSize, readData, readDataSize, timeout) == ERROR_NONE;
|
|
}
|
|
|
|
bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeout) {
|
|
auto* device = findDevice(port);
|
|
if (device == nullptr) return false;
|
|
return i2c_controller_has_device_at_address(device, address, timeout) == ERROR_NONE;
|
|
}
|
|
|
|
Lock& getLock(i2c_port_t port) {
|
|
return NO_LOCK;
|
|
}
|
|
|
|
} // namespace
|