TactilityKernel improvements and more (#459)

This commit is contained in:
Ken Van Hoeylandt
2026-01-25 23:54:33 +01:00
committed by GitHub
parent 96eccbdc8d
commit dfe2c865d1
36 changed files with 759 additions and 331 deletions
@@ -0,0 +1,120 @@
// SPDX-License-Identifier: Apache-2.0
#include <driver/gpio.h>
#include <Tactility/Driver.h>
#include <Tactility/drivers/Esp32Gpio.h>
#include <Tactility/ErrorEsp32.h>
#include <Tactility/Log.h>
#include <Tactility/drivers/Gpio.h>
#include <Tactility/drivers/GpioController.h>
#define TAG LOG_TAG(esp32_gpio)
#define GET_CONFIG(device) ((struct Esp32GpioConfig*)device->internal.driver_data)
extern "C" {
static error_t set_level(Device* device, gpio_pin_t pin, bool high) {
auto esp_error = gpio_set_level(static_cast<gpio_num_t>(pin), high);
return esp_err_to_error(esp_error);
}
static error_t get_level(Device* device, gpio_pin_t pin, bool* high) {
*high = gpio_get_level(static_cast<gpio_num_t>(pin)) != 0;
return ERROR_NONE;
}
static error_t set_options(Device* device, gpio_pin_t pin, gpio_flags_t options) {
const Esp32GpioConfig* config = GET_CONFIG(device);
if (pin >= config->gpioCount) {
return ERROR_INVALID_ARGUMENT;
}
gpio_mode_t mode;
if ((options & GPIO_DIRECTION_INPUT_OUTPUT) == GPIO_DIRECTION_INPUT_OUTPUT) {
mode = GPIO_MODE_INPUT_OUTPUT;
} else if (options & GPIO_DIRECTION_INPUT) {
mode = GPIO_MODE_INPUT;
} else if (options & GPIO_DIRECTION_OUTPUT) {
mode = GPIO_MODE_OUTPUT;
} else {
return ERROR_INVALID_ARGUMENT;
}
const gpio_config_t esp_config = {
.pin_bit_mask = 1ULL << pin,
.mode = mode,
.pull_up_en = (options & GPIO_PULL_UP) ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
.pull_down_en = (options & GPIO_PULL_DOWN) ? GPIO_PULLDOWN_ENABLE : GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTERRUPT_FROM_OPTIONS(options),
#if SOC_GPIO_SUPPORT_PIN_HYS_FILTER
.hys_ctrl_mode = GPIO_HYS_SOFT_DISABLE
#endif
};
auto esp_error = gpio_config(&esp_config);
return esp_err_to_error(esp_error);
}
static int get_options(Device* device, gpio_pin_t pin, gpio_flags_t* options) {
gpio_io_config_t esp_config;
if (gpio_get_io_config(static_cast<gpio_num_t>(pin), &esp_config) != ESP_OK) {
return ERROR_RESOURCE;
}
gpio_flags_t output = 0;
if (esp_config.pu) {
output |= GPIO_PULL_UP;
}
if (esp_config.pd) {
output |= GPIO_PULL_DOWN;
}
if (esp_config.ie) {
output |= GPIO_DIRECTION_INPUT;
}
if (esp_config.oe) {
output |= GPIO_DIRECTION_OUTPUT;
}
if (esp_config.oe_inv) {
output |= GPIO_ACTIVE_LOW;
}
*options = output;
return ERROR_NONE;
}
static error_t start(Device* device) {
ESP_LOGI(TAG, "start %s", device->name);
return ERROR_NONE;
}
static error_t stop(Device* device) {
ESP_LOGI(TAG, "stop %s", device->name);
return ERROR_NONE;
}
const static GpioControllerApi esp32_gpio_api = {
.set_level = set_level,
.get_level = get_level,
.set_options = set_options,
.get_options = get_options
};
Driver esp32_gpio_driver = {
.name = "esp32_gpio",
.compatible = (const char*[]) { "espressif,esp32-gpio", nullptr },
.startDevice = start,
.stopDevice = stop,
.api = (void*)&esp32_gpio_api,
.deviceType = nullptr,
.internal = { 0 }
};
} // extern "C"
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: Apache-2.0
#include <driver/i2c.h>
#include <Tactility/Driver.h>
#include <Tactility/drivers/Esp32I2c.h>
#include "Tactility/ErrorEsp32.h"
#include <Tactility/Log.h>
#include <Tactility/drivers/I2cController.h>
#define TAG LOG_TAG(esp32_i2c)
struct InternalData {
Mutex mutex { 0 };
InternalData() {
mutex_construct(&mutex);
}
~InternalData() {
mutex_destruct(&mutex);
}
};
#define GET_CONFIG(device) ((Esp32I2cConfig*)device->config)
#define GET_DATA(device) ((InternalData*)device->internal.driver_data)
#define lock(data) mutex_lock(&data->mutex);
#define unlock(data) mutex_unlock(&data->mutex);
extern "C" {
static int read(Device* device, uint8_t address, uint8_t* data, size_t data_size, TickType_t timeout) {
vPortAssertIfInISR();
auto* driver_data = GET_DATA(device);
lock(driver_data);
const esp_err_t esp_error = i2c_master_read_from_device(GET_CONFIG(device)->port, address, data, data_size, timeout);
unlock(driver_data);
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_error);
return esp_err_to_error(esp_error);
}
static int write(Device* device, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
vPortAssertIfInISR();
auto* driver_data = GET_DATA(device);
lock(driver_data);
const esp_err_t esp_error = i2c_master_write_to_device(GET_CONFIG(device)->port, address, data, dataSize, timeout);
unlock(driver_data);
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_error);
return esp_err_to_error(esp_error);
}
static int 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) {
vPortAssertIfInISR();
auto* driver_data = GET_DATA(device);
lock(driver_data);
const esp_err_t esp_error = i2c_master_write_read_device(GET_CONFIG(device)->port, address, write_data, write_data_size, read_data, read_data_size, timeout);
unlock(driver_data);
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_error);
return esp_err_to_error(esp_error);
}
static int start(Device* device) {
ESP_LOGI(TAG, "start %s", device->name);
auto* data = new InternalData();
device_set_driver_data(device, data);
return ERROR_NONE;
}
static int stop(Device* device) {
ESP_LOGI(TAG, "stop %s", device->name);
auto* driver_data = static_cast<InternalData*>(device_get_driver_data(device));
device_set_driver_data(device, nullptr);
delete driver_data;
return ERROR_NONE;
}
const static I2cControllerApi esp32_i2c_api = {
.read = read,
.write = write,
.write_read = write_read
};
Driver esp32_i2c_driver = {
.name = "esp32_i2c",
.compatible = (const char*[]) { "espressif,esp32-i2c", nullptr },
.startDevice = start,
.stopDevice = stop,
.api = (void*)&esp32_i2c_api,
.deviceType = &I2C_CONTROLLER_TYPE,
.internal = { 0 }
};
} // extern "C"
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: Apache-2.0
#include <Tactility/Driver.h>
extern "C" {
extern void register_platform_drivers() {
extern Driver esp32_gpio_driver;
driver_construct(&esp32_gpio_driver);
extern Driver esp32_i2c_driver;
driver_construct(&esp32_i2c_driver);
}
}