diff --git a/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.cpp b/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.cpp index 7254acc6..a3ecec77 100644 --- a/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.cpp +++ b/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.cpp @@ -1,13 +1,16 @@ #include "TdeckKeyboard.h" + +#include "../../../../TactilityKernel/include/tactility/drivers/i2c_controller.h" + +#include +#include +#include #include +#include +#include #include #include -#include -#include -#include #include -#include -#include using tt::hal::findFirstDevice; @@ -91,5 +94,6 @@ bool TdeckKeyboard::stopLvgl() { } bool TdeckKeyboard::isAttached() const { - return tt::hal::i2c::masterHasDeviceAtAddress(TDECK_KEYBOARD_I2C_BUS_HANDLE, TDECK_KEYBOARD_SLAVE_ADDRESS, 100); + auto controller = device_find_by_name("i2c_internal"); + return i2c_controller_has_device_at_address(controller, TDECK_KEYBOARD_SLAVE_ADDRESS, 100) == ERROR_NONE; } diff --git a/Devices/m5stack-cores3/Source/devices/Display.cpp b/Devices/m5stack-cores3/Source/devices/Display.cpp index 0c03dea8..7ff3f56f 100644 --- a/Devices/m5stack-cores3/Source/devices/Display.cpp +++ b/Devices/m5stack-cores3/Source/devices/Display.cpp @@ -11,7 +11,8 @@ static const auto LOGGER = tt::Logger("CoreS3Display"); static void setBacklightDuty(uint8_t backlightDuty) { const uint8_t voltage = 20 + ((8 * backlightDuty) / 255); // [0b00000, 0b11100] - under 20 is too dark // TODO: Refactor to use Axp2102 driver subproject. Reference: https://github.com/m5stack/M5Unified/blob/b8cfec7fed046242da7f7b8024a4e92004a51ff7/src/utility/AXP2101_Class.cpp#L42 - if (!tt::hal::i2c::masterWriteRegister(I2C_NUM_0, AXP2101_ADDRESS, 0x99, &voltage, 1, 1000)) { // Sets DLD01 + auto controller = device_find_by_name("i2c_internal"); + if (i2c_controller_write_register(controller, AXP2101_ADDRESS, 0x99, &voltage, 1, 1000) != ERROR_NONE) { // Sets DLD01 LOGGER.error("Failed to set display backlight voltage"); } } diff --git a/Devices/m5stack-stackchan/Source/devices/Display.cpp b/Devices/m5stack-stackchan/Source/devices/Display.cpp index cca04077..e1da002f 100644 --- a/Devices/m5stack-stackchan/Source/devices/Display.cpp +++ b/Devices/m5stack-stackchan/Source/devices/Display.cpp @@ -10,7 +10,8 @@ static const auto LOGGER = tt::Logger("StackChanDisplay"); static void setBacklightDuty(uint8_t backlightDuty) { const uint8_t voltage = 20 + ((8 * backlightDuty) / 255); // [0b00000, 0b11100] - if (!tt::hal::i2c::masterWriteRegister(I2C_NUM_0, AXP2101_ADDRESS, 0x99, &voltage, 1, 1000)) { + auto controller = device_find_by_name("i2c_internal"); + if (i2c_controller_write_register(controller, AXP2101_ADDRESS, 0x99, &voltage, 1, 1000) != ERROR_NONE) { // Sets DLD01 LOGGER.error("Failed to set display backlight voltage"); } } diff --git a/Tactility/Include/Tactility/hal/i2c/I2c.h b/Tactility/Include/Tactility/hal/i2c/I2c.h index f4f9c34f..4b1c0394 100644 --- a/Tactility/Include/Tactility/hal/i2c/I2c.h +++ b/Tactility/Include/Tactility/hal/i2c/I2c.h @@ -5,60 +5,11 @@ #include -#include -#include - namespace tt::hal::i2c { constexpr TickType_t defaultTimeout = 10 / portTICK_PERIOD_MS; -enum class Status { - Started, - Stopped, - Unknown -}; - -/** Start the bus for the specified port. */ -bool start(i2c_port_t port); - -/** Stop the bus for the specified port. */ -bool stop(i2c_port_t port); - -/** @return true if the bus is started */ -bool isStarted(i2c_port_t port); - -/** @return name or nullptr */ -const char* getName(i2c_port_t port); - -/** Read bytes in master mode. */ -bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout = defaultTimeout); - -/** Read bytes from the specified register in master mode. */ -bool masterReadRegister(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout = defaultTimeout); - -/** Write bytes in master mode. */ -bool masterWrite(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout = defaultTimeout); - -/** Write bytes to a register in master mode */ -bool masterWriteRegister(i2c_port_t port, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout = defaultTimeout); - -/** - * Write multiple values to multiple registers in master mode. - * The input is as follows: { register1, value1, register2, value2, ... } - * @return false if any of the write operations failed - */ -bool masterWriteRegisterArray(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout = defaultTimeout); - -/** Write bytes and then read the response bytes in master mode*/ -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 = defaultTimeout); - /** @return true when a device is detected at the specified address */ bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeout = defaultTimeout); -/** - * The lock for the specified bus. - * This can be used when calling native I2C functionality outside of Tactility. - */ -Lock& getLock(i2c_port_t port); - } // namespace diff --git a/Tactility/Source/hal/i2c/I2c.cpp b/Tactility/Source/hal/i2c/I2c.cpp index ae8bb62e..fa17b2b7 100644 --- a/Tactility/Source/hal/i2c/I2c.cpp +++ b/Tactility/Source/hal/i2c/I2c.cpp @@ -1,11 +1,9 @@ #include -#include #include #include #include -#include #ifdef ESP_PLATFORM #include @@ -13,13 +11,6 @@ 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 { @@ -37,7 +28,7 @@ Device* findDevice(i2c_port_t port) { 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(device->config); + const auto* config = static_cast(device->config); if (config->port != params_ptr->port) return true; // Found it, stop iterating params_ptr->device = device; @@ -50,62 +41,10 @@ Device* findDevice(i2c_port_t port) { #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