mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-06-19 04:15:06 +00:00
Merge branch 'main' into grove-driver
This commit is contained in:
commit
e131b5343d
@ -38,3 +38,5 @@ CONFIG_WL_SECTOR_SIZE_512=y
|
||||
CONFIG_WL_SECTOR_SIZE=512
|
||||
CONFIG_WL_SECTOR_MODE_SAFE=y
|
||||
CONFIG_WL_SECTOR_MODE=1
|
||||
# Allow new i2c_master API (used by Tab5Keyboard for LP_I2C_NUM_0) to coexist with legacy i2c driver
|
||||
CONFIG_I2C_SKIP_LEGACY_CONFLICT_CHECK=y
|
||||
|
||||
@ -14,11 +14,12 @@ using namespace tt::hal;
|
||||
static constexpr auto* TAG = "Tab5";
|
||||
|
||||
static DeviceVector createDevices() {
|
||||
::Device* i2c2 = device_find_by_name("i2c2");
|
||||
return {
|
||||
createPower(),
|
||||
createDisplay(),
|
||||
createSdCard(),
|
||||
std::make_shared<Tab5Keyboard>()
|
||||
std::make_shared<Tab5Keyboard>(i2c2)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -13,7 +13,9 @@
|
||||
|
||||
static const auto LOGGER = tt::Logger("Tab5Display");
|
||||
|
||||
constexpr auto LCD_PIN_RESET = GPIO_NUM_0; // Match P4 EV board reset line
|
||||
// LCD reset is wired to the PI4IOE5V6408 IO expander (io_expander0, bit 4), pulsed in
|
||||
// Configuration.cpp's initExpander0() before display creation - not a direct SoC GPIO.
|
||||
constexpr auto LCD_PIN_RESET = GPIO_NUM_NC;
|
||||
constexpr auto LCD_PIN_BACKLIGHT = GPIO_NUM_22;
|
||||
|
||||
static std::shared_ptr<tt::hal::touch::TouchDevice> createGt911Touch() {
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
#include "Tab5Keyboard.h"
|
||||
#include <Tactility/app/App.h>
|
||||
#include <tactility/drivers/i2c_controller.h>
|
||||
#include <tactility/log.h>
|
||||
#include <esp_timer.h>
|
||||
#include <lvgl.h>
|
||||
|
||||
@ -135,19 +137,14 @@ static uint32_t tab5TranslateKey(uint8_t keycode, uint8_t modifier, bool ctrl) {
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// I2C helpers - direct i2c_master API (LP_I2C_NUM_0, GPIO 0/1)
|
||||
// I2C helpers - use Tactility I2C controller API
|
||||
// ---------------------------------------------------------------------------
|
||||
bool Tab5Keyboard::readReg(uint8_t reg, uint8_t& value) const {
|
||||
if (!i2cDev) return false;
|
||||
const esp_err_t err = i2c_master_transmit_receive(i2cDev, ®, 1, &value, 1, pdMS_TO_TICKS(50));
|
||||
return err == ESP_OK;
|
||||
bool Tab5Keyboard::readReg(uint8_t reg, uint8_t& value) {
|
||||
return i2c_controller_read_register(i2cController, I2C_ADDRESS, reg, &value, 1, pdMS_TO_TICKS(50)) == ERROR_NONE;
|
||||
}
|
||||
|
||||
bool Tab5Keyboard::writeReg(uint8_t reg, uint8_t value) const {
|
||||
if (!i2cDev) return false;
|
||||
const uint8_t buf[2] = { reg, value };
|
||||
const esp_err_t err = i2c_master_transmit(i2cDev, buf, 2, pdMS_TO_TICKS(50));
|
||||
return err == ESP_OK;
|
||||
bool Tab5Keyboard::writeReg(uint8_t reg, uint8_t value) {
|
||||
return i2c_controller_write_register(i2cController, I2C_ADDRESS, reg, &value, 1, pdMS_TO_TICKS(50)) == ERROR_NONE;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@ -161,11 +158,7 @@ void Tab5Keyboard::updateLeds() {
|
||||
0x00, 0x00, aaSticky ? uint8_t(0xA0) : uint8_t(0x00), // LED1: red if Aa latched
|
||||
};
|
||||
// Write 7-byte block starting at REG_RGB_BASE
|
||||
const uint8_t reg = REG_RGB_BASE;
|
||||
uint8_t tx[8];
|
||||
tx[0] = reg;
|
||||
for (int i = 0; i < 7; i++) tx[i + 1] = buf[i];
|
||||
i2c_master_transmit(i2cDev, tx, 8, pdMS_TO_TICKS(50));
|
||||
i2c_controller_write_register(i2cController, I2C_ADDRESS, REG_RGB_BASE, buf, 7, pdMS_TO_TICKS(50));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@ -371,41 +364,10 @@ bool Tab5Keyboard::startLvgl(lv_display_t* display) {
|
||||
LOG_E("Tab5Keyboard", "Input queue allocation failed — cannot start");
|
||||
return false;
|
||||
}
|
||||
// Create LP I2C master bus (LP_I2C_NUM_0, GPIO 0/1) via new i2c_master API
|
||||
i2c_master_bus_config_t bus_cfg = {
|
||||
.i2c_port = LP_I2C_NUM_0,
|
||||
.sda_io_num = GPIO_NUM_0,
|
||||
.scl_io_num = GPIO_NUM_1,
|
||||
.clk_source = static_cast<i2c_clock_source_t>(LP_I2C_SCLK_DEFAULT),
|
||||
.glitch_ignore_cnt = 7,
|
||||
.intr_priority = 0,
|
||||
.trans_queue_depth = 0,
|
||||
.flags = { .enable_internal_pullup = true },
|
||||
};
|
||||
if (i2c_new_master_bus(&bus_cfg, &i2cBus) != ESP_OK) {
|
||||
LOG_E("Tab5Keyboard", "Failed to create LP I2C master bus");
|
||||
return false;
|
||||
}
|
||||
|
||||
i2c_device_config_t dev_cfg = {
|
||||
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||
.device_address = I2C_ADDRESS,
|
||||
.scl_speed_hz = 100000,
|
||||
};
|
||||
if (i2c_master_bus_add_device(i2cBus, &dev_cfg, &i2cDev) != ESP_OK) {
|
||||
LOG_E("Tab5Keyboard", "Failed to add keyboard device to LP I2C bus");
|
||||
i2c_del_master_bus(i2cBus);
|
||||
i2cBus = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set Normal mode explicitly — device may power up in a different mode
|
||||
if (!writeReg(REG_KEYBOARD_MODE, 0x00)) {
|
||||
LOG_E("Tab5Keyboard", "Failed to set keyboard mode");
|
||||
i2c_master_bus_rm_device(i2cDev);
|
||||
i2c_del_master_bus(i2cBus);
|
||||
i2cDev = nullptr;
|
||||
i2cBus = nullptr;
|
||||
return false;
|
||||
}
|
||||
writeReg(REG_EVENT_NUM, 0x00); // flush event queue
|
||||
@ -426,10 +388,6 @@ bool Tab5Keyboard::startLvgl(lv_display_t* display) {
|
||||
// Enable Normal-mode interrupt (bit 0)
|
||||
if (!writeReg(REG_INT_CFG, 0x01)) {
|
||||
LOG_E("Tab5Keyboard", "Failed to configure interrupt register");
|
||||
i2c_master_bus_rm_device(i2cDev);
|
||||
i2c_del_master_bus(i2cBus);
|
||||
i2cDev = nullptr;
|
||||
i2cBus = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -470,38 +428,9 @@ bool Tab5Keyboard::stopLvgl() {
|
||||
lv_indev_delete(kbHandle);
|
||||
kbHandle = nullptr;
|
||||
|
||||
if (i2cDev) {
|
||||
i2c_master_bus_rm_device(i2cDev);
|
||||
i2cDev = nullptr;
|
||||
}
|
||||
if (i2cBus) {
|
||||
i2c_del_master_bus(i2cBus);
|
||||
i2cBus = nullptr;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Tab5Keyboard::isAttached() const {
|
||||
// If already started, just probe via the open bus handle
|
||||
if (i2cBus) {
|
||||
return i2c_master_probe(i2cBus, I2C_ADDRESS, pdMS_TO_TICKS(100)) == ESP_OK;
|
||||
}
|
||||
// Otherwise open a temporary bus to probe (LP I2C is not accessible via legacy API)
|
||||
i2c_master_bus_config_t bus_cfg = {
|
||||
.i2c_port = LP_I2C_NUM_0,
|
||||
.sda_io_num = GPIO_NUM_0,
|
||||
.scl_io_num = GPIO_NUM_1,
|
||||
.clk_source = static_cast<i2c_clock_source_t>(LP_I2C_SCLK_DEFAULT),
|
||||
.glitch_ignore_cnt = 7,
|
||||
.intr_priority = 0,
|
||||
.trans_queue_depth = 0,
|
||||
.flags = { .enable_internal_pullup = true },
|
||||
};
|
||||
i2c_master_bus_handle_t probe_bus = nullptr;
|
||||
if (i2c_new_master_bus(&bus_cfg, &probe_bus) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
const esp_err_t ret = i2c_master_probe(probe_bus, I2C_ADDRESS, pdMS_TO_TICKS(100));
|
||||
i2c_del_master_bus(probe_bus);
|
||||
return ret == ESP_OK;
|
||||
return i2c_controller_has_device_at_address(i2cController, I2C_ADDRESS, pdMS_TO_TICKS(100)) == ERROR_NONE;
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include <Tactility/hal/keyboard/KeyboardDevice.h>
|
||||
#include <Tactility/Timer.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <tactility/device.h>
|
||||
#include <driver/gpio.h>
|
||||
#include <freertos/queue.h>
|
||||
|
||||
@ -13,8 +13,7 @@ class Tab5Keyboard final : public tt::hal::keyboard::KeyboardDevice {
|
||||
static constexpr uint32_t REPEAT_INITIAL_MS = 400;
|
||||
static constexpr uint32_t REPEAT_RATE_MS = 80;
|
||||
|
||||
i2c_master_bus_handle_t i2cBus = nullptr;
|
||||
i2c_master_dev_handle_t i2cDev = nullptr;
|
||||
::Device* i2cController = nullptr;
|
||||
|
||||
lv_indev_t* kbHandle = nullptr;
|
||||
QueueHandle_t queue = nullptr;
|
||||
@ -37,8 +36,8 @@ class Tab5Keyboard final : public tt::hal::keyboard::KeyboardDevice {
|
||||
uint32_t repeatStartMs = 0;
|
||||
uint32_t repeatLastMs = 0;
|
||||
|
||||
bool readReg(uint8_t reg, uint8_t& value) const;
|
||||
bool writeReg(uint8_t reg, uint8_t value) const;
|
||||
bool readReg(uint8_t reg, uint8_t& value);
|
||||
bool writeReg(uint8_t reg, uint8_t value);
|
||||
void updateLeds();
|
||||
|
||||
bool configureIrqPin();
|
||||
@ -50,7 +49,7 @@ class Tab5Keyboard final : public tt::hal::keyboard::KeyboardDevice {
|
||||
static void readCallback(lv_indev_t* indev, lv_indev_data_t* data);
|
||||
|
||||
public:
|
||||
Tab5Keyboard() {
|
||||
explicit Tab5Keyboard(::Device* i2cController) : i2cController(i2cController) {
|
||||
queue = xQueueCreate(20, sizeof(uint32_t));
|
||||
// queue == nullptr on OOM; startLvgl() checks and refuses to start
|
||||
}
|
||||
|
||||
@ -49,5 +49,3 @@ CONFIG_CACHE_L2_CACHE_256KB=y
|
||||
CONFIG_LVGL_PORT_ENABLE_PPA=y
|
||||
CONFIG_LV_DRAW_BUF_ALIGN=64
|
||||
CONFIG_LV_DEF_REFR_PERIOD=15
|
||||
# Allow new i2c_master API (used by Tab5Keyboard for LP_I2C_NUM_0) to coexist with legacy i2c driver
|
||||
CONFIG_I2C_SKIP_LEGACY_CONFLICT_CHECK=y
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include <tactility/bindings/esp32_ble.h>
|
||||
#include <tactility/bindings/esp32_gpio.h>
|
||||
#include <tactility/bindings/esp32_i2c.h>
|
||||
#include <tactility/bindings/esp32_i2c_master.h>
|
||||
#include <tactility/bindings/esp32_i2s.h>
|
||||
#include <tactility/bindings/esp32_spi.h>
|
||||
#include <tactility/bindings/esp32_uart.h>
|
||||
@ -68,6 +69,15 @@
|
||||
pin-scl = <&gpio0 54 GPIO_FLAG_NONE>;
|
||||
};
|
||||
|
||||
i2c_keyboard: i2c2 {
|
||||
compatible = "espressif,esp32-i2c-master";
|
||||
port = <LP_I2C_NUM_0>;
|
||||
clock-frequency = <100000>;
|
||||
clock-source = <LP_I2C_SCLK_DEFAULT>;
|
||||
pin-sda = <&gpio0 0 GPIO_FLAG_PULL_UP>;
|
||||
pin-scl = <&gpio0 1 GPIO_FLAG_PULL_UP>;
|
||||
};
|
||||
|
||||
sdcard_spi: spi0 {
|
||||
compatible = "espressif,esp32-spi";
|
||||
host = <SPI2_HOST>;
|
||||
|
||||
@ -6,7 +6,7 @@ idf_component_register(
|
||||
SRCS ${SOURCES}
|
||||
INCLUDE_DIRS "include/"
|
||||
PRIV_INCLUDE_DIRS "private/"
|
||||
REQUIRES TactilityKernel driver vfs fatfs
|
||||
REQUIRES TactilityKernel driver esp_driver_i2c vfs fatfs
|
||||
)
|
||||
|
||||
idf_component_optional_requires(PRIVATE bt usb espressif__usb_host_hid espressif__usb_host_msc)
|
||||
|
||||
@ -0,0 +1,29 @@
|
||||
description: ESP32 I2C Controller
|
||||
|
||||
include: ["i2c-controller.yaml"]
|
||||
|
||||
compatible: "espressif,esp32-i2c-master"
|
||||
|
||||
properties:
|
||||
port:
|
||||
type: int
|
||||
required: true
|
||||
description: |
|
||||
The port number, defined by i2c_port_t.
|
||||
Depending on the hardware, these values are available: I2C_NUM_0, I2C_NUM_1, LP_I2C_NUM_0
|
||||
clock-frequency:
|
||||
type: int
|
||||
required: true
|
||||
description: Initial clock frequency in Hz
|
||||
clock-source:
|
||||
type: int
|
||||
default: 0
|
||||
description: |
|
||||
Clock source for the I2C peripheral.
|
||||
If not specified, a default clock source will be used.
|
||||
pin-sda:
|
||||
type: phandle-array
|
||||
required: true
|
||||
pin-scl:
|
||||
type: phandle-array
|
||||
required: true
|
||||
@ -10,7 +10,7 @@ properties:
|
||||
required: true
|
||||
description: |
|
||||
The port number, defined by i2c_port_t.
|
||||
Depending on the hardware, these values are available: I2C_NUM_0, I2C_NUM_1, LP_I2C_NUM_0
|
||||
Depending on the hardware, these values are available: I2C_NUM_0, I2C_NUM_1
|
||||
clock-frequency:
|
||||
type: int
|
||||
required: true
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/bindings/bindings.h>
|
||||
#include <tactility/drivers/esp32_i2c_master.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
DEFINE_DEVICETREE(esp32_i2c_master, struct Esp32I2cMasterConfig)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -0,0 +1,21 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/drivers/gpio.h>
|
||||
#include <driver/i2c_types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct Esp32I2cMasterConfig {
|
||||
i2c_port_num_t port;
|
||||
uint32_t clockFrequency;
|
||||
int32_t clkSource;
|
||||
struct GpioPinSpec pinSda;
|
||||
struct GpioPinSpec pinScl;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -175,16 +175,12 @@ static error_t start(Device* device) {
|
||||
check(gpio_descriptor_get_native_pin_number(sda_descriptor, &sda_pin) == ERROR_NONE);
|
||||
check(gpio_descriptor_get_native_pin_number(scl_descriptor, &scl_pin) == ERROR_NONE);
|
||||
|
||||
gpio_flags_t sda_flags, scl_flags;
|
||||
check(gpio_descriptor_get_flags(sda_descriptor, &sda_flags) == ERROR_NONE);
|
||||
check(gpio_descriptor_get_flags(scl_descriptor, &scl_flags) == ERROR_NONE);
|
||||
|
||||
i2c_config_t esp_config = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = sda_pin,
|
||||
.scl_io_num = scl_pin,
|
||||
.sda_pullup_en = (sda_flags & GPIO_FLAG_PULL_UP) != 0,
|
||||
.scl_pullup_en = (scl_flags & GPIO_FLAG_PULL_UP) != 0,
|
||||
.sda_pullup_en = (sda_spec.flags & GPIO_FLAG_PULL_UP) != 0,
|
||||
.scl_pullup_en = (scl_spec.flags & GPIO_FLAG_PULL_UP) != 0,
|
||||
.master {
|
||||
.clk_speed = dts_config->clockFrequency
|
||||
},
|
||||
|
||||
311
Platforms/platform-esp32/source/drivers/esp32_i2c_master.cpp
Normal file
311
Platforms/platform-esp32/source/drivers/esp32_i2c_master.cpp
Normal file
@ -0,0 +1,311 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <driver/i2c_master.h>
|
||||
|
||||
#include <new>
|
||||
|
||||
#include <tactility/error_esp32.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/gpio_controller.h>
|
||||
#include <tactility/drivers/i2c_controller.h>
|
||||
#include <tactility/drivers/esp32_i2c_master.h>
|
||||
#include <tactility/log.h>
|
||||
#include <tactility/time.h>
|
||||
|
||||
#define TAG "esp32_i2c_master"
|
||||
|
||||
struct Esp32I2cMasterInternal {
|
||||
Mutex mutex {};
|
||||
GpioDescriptor* sda_descriptor = nullptr;
|
||||
GpioDescriptor* scl_descriptor = nullptr;
|
||||
i2c_master_bus_handle_t bus_handle = nullptr;
|
||||
i2c_master_dev_handle_t dev_handle = nullptr;
|
||||
int current_address = -1;
|
||||
|
||||
Esp32I2cMasterInternal(GpioDescriptor* sda_descriptor, GpioDescriptor* scl_descriptor) :
|
||||
sda_descriptor(sda_descriptor),
|
||||
scl_descriptor(scl_descriptor)
|
||||
{
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
~Esp32I2cMasterInternal() {
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
};
|
||||
|
||||
#define GET_CONFIG(device) ((Esp32I2cMasterConfig*)device->config)
|
||||
#define GET_DATA(device) ((Esp32I2cMasterInternal*)device_get_driver_data(device))
|
||||
|
||||
#define lock(data) mutex_lock(&data->mutex);
|
||||
#define unlock(data) mutex_unlock(&data->mutex);
|
||||
|
||||
// Switches the device's target address only when it differs from the currently configured one.
|
||||
static esp_err_t ensure_address(Esp32I2cMasterInternal* driver_data, uint8_t address, int timeout_ms) {
|
||||
if (driver_data->current_address == address) {
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_err_t esp_error = i2c_master_device_change_address(driver_data->dev_handle, address, timeout_ms);
|
||||
if (esp_error == ESP_OK) {
|
||||
driver_data->current_address = address;
|
||||
}
|
||||
return esp_error;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
static int ticks_to_ms(TickType_t ticks) {
|
||||
if (ticks == portMAX_DELAY) return -1;
|
||||
return (int)pdTICKS_TO_MS(ticks);
|
||||
}
|
||||
|
||||
static error_t read(Device* device, uint8_t address, uint8_t* data, size_t data_size, TickType_t timeout) {
|
||||
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
|
||||
if (data_size == 0) return ERROR_INVALID_ARGUMENT;
|
||||
auto* driver_data = GET_DATA(device);
|
||||
int timeout_ms = ticks_to_ms(timeout);
|
||||
|
||||
lock(driver_data);
|
||||
esp_err_t esp_error = ensure_address(driver_data, address, timeout_ms);
|
||||
if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "change_address(0x%02X) failed: %s", address, esp_err_to_name(esp_error));
|
||||
} else {
|
||||
esp_error = i2c_master_receive(driver_data->dev_handle, data, data_size, timeout_ms);
|
||||
if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "receive(0x%02X) failed: %s", address, esp_err_to_name(esp_error));
|
||||
}
|
||||
}
|
||||
unlock(driver_data);
|
||||
return esp_err_to_error(esp_error);
|
||||
}
|
||||
|
||||
static error_t write(Device* device, uint8_t address, const uint8_t* data, uint16_t data_size, TickType_t timeout) {
|
||||
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
|
||||
if (data_size == 0) return ERROR_INVALID_ARGUMENT;
|
||||
auto* driver_data = GET_DATA(device);
|
||||
int timeout_ms = ticks_to_ms(timeout);
|
||||
|
||||
lock(driver_data);
|
||||
esp_err_t esp_error = ensure_address(driver_data, address, timeout_ms);
|
||||
if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "change_address(0x%02X) failed: %s", address, esp_err_to_name(esp_error));
|
||||
} else {
|
||||
esp_error = i2c_master_transmit(driver_data->dev_handle, data, data_size, timeout_ms);
|
||||
if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "transmit(0x%02X) failed: %s", address, esp_err_to_name(esp_error));
|
||||
}
|
||||
}
|
||||
unlock(driver_data);
|
||||
return esp_err_to_error(esp_error);
|
||||
}
|
||||
|
||||
static error_t write_read(Device* device, uint8_t address, const uint8_t* write_data, size_t write_data_size, uint8_t* read_data, size_t read_data_size, TickType_t timeout) {
|
||||
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
|
||||
if (write_data_size == 0 || read_data_size == 0) return ERROR_INVALID_ARGUMENT;
|
||||
auto* driver_data = GET_DATA(device);
|
||||
int timeout_ms = ticks_to_ms(timeout);
|
||||
|
||||
lock(driver_data);
|
||||
esp_err_t esp_error = ensure_address(driver_data, address, timeout_ms);
|
||||
if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "change_address(0x%02X) failed: %s", address, esp_err_to_name(esp_error));
|
||||
} else {
|
||||
esp_error = i2c_master_transmit_receive(driver_data->dev_handle, write_data, write_data_size, read_data, read_data_size, timeout_ms);
|
||||
if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "transmit_receive(0x%02X) failed: %s", address, esp_err_to_name(esp_error));
|
||||
}
|
||||
}
|
||||
unlock(driver_data);
|
||||
return esp_err_to_error(esp_error);
|
||||
}
|
||||
|
||||
static error_t read_register(Device* device, uint8_t address, uint8_t reg, uint8_t* data, size_t data_size, TickType_t timeout) {
|
||||
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
|
||||
if (data_size == 0) return ERROR_INVALID_ARGUMENT;
|
||||
auto* driver_data = GET_DATA(device);
|
||||
int timeout_ms = ticks_to_ms(timeout);
|
||||
|
||||
lock(driver_data);
|
||||
esp_err_t esp_error = ensure_address(driver_data, address, timeout_ms);
|
||||
if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "change_address(0x%02X) failed: %s", address, esp_err_to_name(esp_error));
|
||||
} else {
|
||||
esp_error = i2c_master_transmit_receive(driver_data->dev_handle, ®, 1, data, data_size, timeout_ms);
|
||||
if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "read_register(0x%02X, reg=0x%02X) failed: %s", address, reg, esp_err_to_name(esp_error));
|
||||
}
|
||||
}
|
||||
unlock(driver_data);
|
||||
return esp_err_to_error(esp_error);
|
||||
}
|
||||
|
||||
static error_t write_register(Device* device, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t data_size, TickType_t timeout) {
|
||||
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
|
||||
if (data_size == 0) return ERROR_INVALID_ARGUMENT;
|
||||
auto* driver_data = GET_DATA(device);
|
||||
int timeout_ms = ticks_to_ms(timeout);
|
||||
|
||||
lock(driver_data);
|
||||
esp_err_t esp_error = ensure_address(driver_data, address, timeout_ms);
|
||||
if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "change_address(0x%02X) failed: %s", address, esp_err_to_name(esp_error));
|
||||
} else {
|
||||
i2c_master_transmit_multi_buffer_info_t buffers[2] = {
|
||||
{.write_buffer = ®, .buffer_size = 1},
|
||||
{.write_buffer = data, .buffer_size = data_size}
|
||||
};
|
||||
esp_error = i2c_master_multi_buffer_transmit(driver_data->dev_handle, buffers, 2, timeout_ms);
|
||||
if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "write_register(0x%02X, reg=0x%02X) failed: %s", address, reg, esp_err_to_name(esp_error));
|
||||
}
|
||||
}
|
||||
unlock(driver_data);
|
||||
return esp_err_to_error(esp_error);
|
||||
}
|
||||
|
||||
static error_t probe(Device* device, uint8_t address, TickType_t timeout) {
|
||||
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
|
||||
auto* driver_data = GET_DATA(device);
|
||||
int timeout_ms = ticks_to_ms(timeout);
|
||||
|
||||
lock(driver_data);
|
||||
esp_err_t esp_error = i2c_master_probe(driver_data->bus_handle, address, timeout_ms);
|
||||
if (esp_error == ESP_ERR_NOT_FOUND) {
|
||||
// Expected outcome when no device acks - e.g. hot-plug attach polling
|
||||
LOG_D(TAG, "probe(0x%02X): not found", address);
|
||||
} else if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "probe(0x%02X) failed: %s", address, esp_err_to_name(esp_error));
|
||||
}
|
||||
unlock(driver_data);
|
||||
return esp_err_to_error(esp_error);
|
||||
}
|
||||
|
||||
static error_t start(Device* device) {
|
||||
ESP_LOGI(TAG, "start %s", device->name);
|
||||
auto dts_config = GET_CONFIG(device);
|
||||
|
||||
auto& sda_spec = dts_config->pinSda;
|
||||
auto& scl_spec = dts_config->pinScl;
|
||||
auto* sda_descriptor = gpio_descriptor_acquire(sda_spec.gpio_controller, sda_spec.pin, GPIO_OWNER_GPIO);
|
||||
if (!sda_descriptor) {
|
||||
LOG_E(TAG, "Failed to acquire pin %u", sda_spec.pin);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto* scl_descriptor = gpio_descriptor_acquire(scl_spec.gpio_controller, scl_spec.pin, GPIO_OWNER_GPIO);
|
||||
if (!scl_descriptor) {
|
||||
LOG_E(TAG, "Failed to acquire pin %u", scl_spec.pin);
|
||||
gpio_descriptor_release(sda_descriptor);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
gpio_num_t sda_pin, scl_pin;
|
||||
check(gpio_descriptor_get_native_pin_number(sda_descriptor, &sda_pin) == ERROR_NONE);
|
||||
check(gpio_descriptor_get_native_pin_number(scl_descriptor, &scl_pin) == ERROR_NONE);
|
||||
|
||||
i2c_master_bus_config_t bus_config = {
|
||||
.i2c_port = dts_config->port,
|
||||
.sda_io_num = sda_pin,
|
||||
.scl_io_num = scl_pin,
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.intr_priority = 0,
|
||||
.trans_queue_depth = 0,
|
||||
.flags = {
|
||||
.enable_internal_pullup = ((sda_spec.flags & GPIO_FLAG_PULL_UP) != 0) || ((scl_spec.flags & GPIO_FLAG_PULL_UP) != 0),
|
||||
.allow_pd = 0,
|
||||
}
|
||||
};
|
||||
|
||||
#if SOC_LP_I2C_SUPPORTED
|
||||
if (dts_config->port == LP_I2C_NUM_0) {
|
||||
bus_config.lp_source_clk = (dts_config->clkSource == 0) ? LP_I2C_SCLK_DEFAULT : static_cast<lp_i2c_clock_source_t>(dts_config->clkSource);
|
||||
} else {
|
||||
bus_config.clk_source = (dts_config->clkSource == 0) ? I2C_CLK_SRC_DEFAULT : static_cast<i2c_clock_source_t>(dts_config->clkSource);
|
||||
}
|
||||
#else
|
||||
bus_config.clk_source = (dts_config->clkSource == 0) ? I2C_CLK_SRC_DEFAULT : static_cast<i2c_clock_source_t>(dts_config->clkSource);
|
||||
#endif
|
||||
|
||||
i2c_master_bus_handle_t bus_handle;
|
||||
esp_err_t error = i2c_new_master_bus(&bus_config, &bus_handle);
|
||||
if (error != ESP_OK) {
|
||||
LOG_E(TAG, "Failed to create I2C bus at port %d: %s", (int)dts_config->port, esp_err_to_name(error));
|
||||
gpio_descriptor_release(sda_descriptor);
|
||||
gpio_descriptor_release(scl_descriptor);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
i2c_device_config_t dev_config = {
|
||||
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||
.device_address = 0, // Will be changed at runtime
|
||||
.scl_speed_hz = dts_config->clockFrequency,
|
||||
.scl_wait_us = 0,
|
||||
.flags = {
|
||||
.disable_ack_check = 0,
|
||||
}
|
||||
};
|
||||
|
||||
i2c_master_dev_handle_t dev_handle;
|
||||
error = i2c_master_bus_add_device(bus_handle, &dev_config, &dev_handle);
|
||||
if (error != ESP_OK) {
|
||||
LOG_E(TAG, "Failed to add I2C device: %s", esp_err_to_name(error));
|
||||
i2c_del_master_bus(bus_handle);
|
||||
gpio_descriptor_release(sda_descriptor);
|
||||
gpio_descriptor_release(scl_descriptor);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto* data = new(std::nothrow) Esp32I2cMasterInternal(sda_descriptor, scl_descriptor);
|
||||
if (data == nullptr) {
|
||||
i2c_master_bus_rm_device(dev_handle);
|
||||
i2c_del_master_bus(bus_handle);
|
||||
gpio_descriptor_release(sda_descriptor);
|
||||
gpio_descriptor_release(scl_descriptor);
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
data->bus_handle = bus_handle;
|
||||
data->dev_handle = dev_handle;
|
||||
|
||||
device_set_driver_data(device, data);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop(Device* device) {
|
||||
ESP_LOGI(TAG, "stop %s", device->name);
|
||||
auto* driver_data = GET_DATA(device);
|
||||
|
||||
i2c_master_bus_rm_device(driver_data->dev_handle);
|
||||
i2c_del_master_bus(driver_data->bus_handle);
|
||||
|
||||
gpio_descriptor_release(driver_data->sda_descriptor);
|
||||
gpio_descriptor_release(driver_data->scl_descriptor);
|
||||
|
||||
device_set_driver_data(device, nullptr);
|
||||
delete driver_data;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static constexpr I2cControllerApi ESP32_I2C_MASTER_API = {
|
||||
.read = read,
|
||||
.write = write,
|
||||
.write_read = write_read,
|
||||
.read_register = read_register,
|
||||
.write_register = write_register,
|
||||
.probe = probe
|
||||
};
|
||||
|
||||
extern Module platform_esp32_module;
|
||||
|
||||
Driver esp32_i2c_master_driver = {
|
||||
.name = "esp32_i2c_master",
|
||||
.compatible = (const char*[]) { "espressif,esp32-i2c-master", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = &ESP32_I2C_MASTER_API,
|
||||
.device_type = &I2C_CONTROLLER_TYPE,
|
||||
.owner = &platform_esp32_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
} // extern "C"
|
||||
@ -13,6 +13,7 @@ extern "C" {
|
||||
|
||||
extern Driver esp32_gpio_driver;
|
||||
extern Driver esp32_i2c_driver;
|
||||
extern Driver esp32_i2c_master_driver;
|
||||
extern Driver esp32_i2s_driver;
|
||||
#if SOC_SDMMC_HOST_SUPPORTED
|
||||
extern Driver esp32_sdmmc_driver;
|
||||
@ -38,6 +39,7 @@ static error_t start() {
|
||||
* there is no guarantee that the previously constructed drivers can be destroyed */
|
||||
check(driver_construct_add(&esp32_gpio_driver) == ERROR_NONE);
|
||||
check(driver_construct_add(&esp32_i2c_driver) == ERROR_NONE);
|
||||
check(driver_construct_add(&esp32_i2c_master_driver) == ERROR_NONE);
|
||||
check(driver_construct_add(&esp32_i2s_driver) == ERROR_NONE);
|
||||
#if SOC_SDMMC_HOST_SUPPORTED
|
||||
check(driver_construct_add(&esp32_sdmmc_driver) == ERROR_NONE);
|
||||
@ -77,6 +79,7 @@ static error_t stop() {
|
||||
#endif
|
||||
check(driver_remove_destruct(&esp32_gpio_driver) == ERROR_NONE);
|
||||
check(driver_remove_destruct(&esp32_i2c_driver) == ERROR_NONE);
|
||||
check(driver_remove_destruct(&esp32_i2c_master_driver) == ERROR_NONE);
|
||||
check(driver_remove_destruct(&esp32_i2s_driver) == ERROR_NONE);
|
||||
#if SOC_SDMMC_HOST_SUPPORTED
|
||||
check(driver_remove_destruct(&esp32_sdmmc_driver) == ERROR_NONE);
|
||||
|
||||
@ -21,7 +21,7 @@ extern "C" {
|
||||
#define GPIO_FLAG_DIRECTION_INPUT (1 << 1)
|
||||
#define GPIO_FLAG_DIRECTION_OUTPUT (1 << 2)
|
||||
#define GPIO_FLAG_DIRECTION_INPUT_OUTPUT (GPIO_FLAG_DIRECTION_INPUT | GPIO_FLAG_DIRECTION_OUTPUT)
|
||||
#define GPIO_FLAG_PULL_UP (0 << 3)
|
||||
#define GPIO_FLAG_PULL_UP (1 << 3)
|
||||
#define GPIO_FLAG_PULL_DOWN (1 << 4)
|
||||
#define GPIO_FLAG_INTERRUPT_BITMASK (0b111 << 5) // 3 bits to hold the values [0, 5]
|
||||
#define GPIO_FLAG_INTERRUPT_FROM_OPTIONS(options) (gpio_int_type_t)((options & GPIO_FLAG_INTERRUPT_BITMASK) >> 5)
|
||||
|
||||
@ -82,6 +82,17 @@ struct I2cControllerApi {
|
||||
* @retval ERROR_TIMEOUT when the operation timed out
|
||||
*/
|
||||
error_t (*write_register)(struct Device* device, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout);
|
||||
|
||||
/**
|
||||
* @brief Checks if a device responds at the given address, without performing a data transfer.
|
||||
* Optional: set to NULL if the driver does not support a dedicated probe operation, in which case
|
||||
* @ref i2c_controller_has_device_at_address falls back to a generic write-based probe.
|
||||
* @param[in] device the I2C controller device
|
||||
* @param[in] address the 7-bit I2C address to probe
|
||||
* @param[in] timeout the maximum time to wait for the operation to complete
|
||||
* @retval ERROR_NONE when a device acknowledged the address
|
||||
*/
|
||||
error_t (*probe)(struct Device* device, uint8_t address, TickType_t timeout);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -73,8 +73,12 @@ error_t i2c_controller_write_register_array(Device* device, uint8_t address, con
|
||||
|
||||
error_t i2c_controller_has_device_at_address(Device* device, uint8_t address, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
auto* api = I2C_DRIVER_API(driver);
|
||||
if (api->probe != nullptr) {
|
||||
return api->probe(device, address, timeout);
|
||||
}
|
||||
uint8_t message[2] = { 0, 0 };
|
||||
return I2C_DRIVER_API(driver)->write(device, address, message, 2, timeout);
|
||||
return api->write(device, address, message, 2, timeout);
|
||||
}
|
||||
|
||||
error_t i2c_controller_register16le_get(Device* device, uint8_t address, uint8_t reg, uint16_t* value, TickType_t timeout) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user