Implement UI scaling and more (#501)

**New Features**
 * Runtime font accessors and new symbol fonts for text, launcher, statusbar, and shared icons.
 * Added font height base setting to device.properties
 * Text fonts now have 3 sizes: small, default, large

**Improvements**
 * Renamed `UiScale` to `UiDensity`
 * Statusbar, toolbar and many UI components now compute heights and spacing from fonts/density.
 * SSD1306 initialization sequence refined for more stable startup.
 * Multiple image assets replaced by symbol-font rendering.
 * Many layout improvements related to density, font scaling and icon scaling
 * Updated folder name capitalization for newer style
This commit is contained in:
Ken Van Hoeylandt
2026-02-15 01:41:47 +01:00
committed by GitHub
parent 72c9b2b113
commit 9a11e6f47b
264 changed files with 5923 additions and 494 deletions
+10
View File
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.20)
file(GLOB_RECURSE SOURCES "source/*.c**")
idf_component_register(
SRCS ${SOURCES}
INCLUDE_DIRS "include/"
PRIV_INCLUDE_DIRS "private/"
REQUIRES TactilityKernel driver
)
@@ -0,0 +1,5 @@
description: ESP32 GPIO Controller
compatible: "espressif,esp32-gpio"
include: ["gpio-controller.yaml"]
@@ -0,0 +1,23 @@
description: ESP32 I2C Controller
include: ["i2c-controller.yaml"]
compatible: "espressif,esp32-i2c"
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
pin-sda:
type: phandle-array
required: true
pin-scl:
type: phandle-array
required: true
@@ -0,0 +1,33 @@
description: ESP32 I2S Controller
include: ["i2s-controller.yaml"]
compatible: "espressif,esp32-i2s"
properties:
port:
type: int
required: true
description: |
The port number, defined by i2s_port_t.
Depending on the hardware, these values are available: I2S_NUM_0, I2S_NUM_1
pin-bclk:
type: phandle-array
required: true
description: Bit clock pin
pin-ws:
type: phandle-array
required: true
description: Word (slot) select pin
pin-data-out:
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: Data output pin
pin-data-in:
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: Data input pin
pin-mclk:
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: Master clock pin
@@ -0,0 +1,38 @@
description: ESP32 SPI Controller
include: ["spi-controller.yaml"]
compatible: "espressif,esp32-spi"
properties:
host:
type: int
required: true
description: |
The SPI host (controller) to use.
Defined by spi_host_device_t (e.g. SPI2_HOST, SPI3_HOST).
pin-sclk:
type: phandle-array
default: GPIO_PIN_SPEC_NONE
pin-mosi:
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: MOSI (Data 0) pin
pin-miso:
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: MISO (Data 1) pin
pin-wp:
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: WP (Data 2) pin
pin-hd:
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: HD (Data 3) pin
max-transfer-size:
type: int
default: 0
description: |
Data transfer size limit in bytes.
0 means the platform decides the limit.
@@ -0,0 +1,29 @@
description: ESP32 UART Controller
include: ["uart-controller.yaml"]
compatible: "espressif,esp32-uart"
properties:
port:
type: int
required: true
description: |
The port number, defined by uart_port_t.
Depending on the hardware, these values are available: UART_NUM_0, UART_NUM_1, UART_NUM_2
pin-tx:
type: phandle-array
required: true
description: TX pin
pin-rx:
type: phandle-array
required: true
description: RX pin
pin-cts:
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: CTS pin
pin-rts:
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: RTS pin
+3
View File
@@ -0,0 +1,3 @@
dependencies:
- TactilityKernel
bindings: bindings
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <tactility/bindings/gpio.h>
#include <tactility/drivers/esp32_gpio.h>
#ifdef __cplusplus
extern "C" {
#endif
#define GPIO_ACTIVE_HIGH GPIO_FLAG_ACTIVE_HIGH
#define GPIO_ACTIVE_LOW GPIO_FLAG_ACTIVE_LOW
#define GPIO_DIRECTION_INPUT GPIO_FLAG_DIRECTION_INPUT
#define GPIO_DIRECTION_OUTPUT GPIO_FLAG_DIRECTION_OUTPUT
#define GPIO_DIRECTION_INPUT_OUTPUT GPIO_FLAG_DIRECTION_INPUT_OUTPUT
#define GPIO_PULL_UP GPIO_FLAG_PULL_UP
#define GPIO_PULL_DOWN GPIO_FLAG_PULL_DOWN
DEFINE_DEVICETREE(esp32_gpio, struct Esp32GpioConfig)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <tactility/drivers/esp32_i2c.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(esp32_i2c, struct Esp32I2cConfig)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <tactility/drivers/esp32_i2s.h>
#include <tactility/drivers/esp32_gpio.h>
#include <driver/i2s_common.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(esp32_i2s, struct Esp32I2sConfig)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <tactility/drivers/esp32_spi.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(esp32_spi, struct Esp32SpiConfig)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <tactility/drivers/esp32_uart.h>
#include <driver/uart.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(esp32_uart, struct Esp32UartConfig)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
struct Esp32GpioConfig {
uint8_t gpioCount;
};
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/drivers/gpio.h>
#include <hal/i2c_types.h>
#ifdef __cplusplus
extern "C" {
#endif
struct Esp32I2cConfig {
i2c_port_t port;
uint32_t clockFrequency;
struct GpioPinSpec pinSda;
struct GpioPinSpec pinScl;
};
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/drivers/i2s_controller.h>
#include <tactility/drivers/gpio.h>
#include <driver/i2s_common.h>
#ifdef __cplusplus
extern "C" {
#endif
struct Esp32I2sConfig {
i2s_port_t port;
struct GpioPinSpec pin_bclk;
struct GpioPinSpec pin_ws;
struct GpioPinSpec pin_data_out;
struct GpioPinSpec pin_data_in;
struct GpioPinSpec pin_mclk;
};
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <driver/spi_common.h>
#include <tactility/drivers/spi_controller.h>
#include <tactility/drivers/gpio.h>
#ifdef __cplusplus
extern "C" {
#endif
struct Esp32SpiConfig {
spi_host_device_t host;
/** Clock pin */
struct GpioPinSpec pin_sclk;
/** Data 0 pin */
struct GpioPinSpec pin_mosi;
/** Data 1 pin */
struct GpioPinSpec pin_miso;
/** Data 2 pin */
struct GpioPinSpec pin_wp;
/** Data 3 pin */
struct GpioPinSpec pin_hd;
/** Data transfer size limit in bytes. 0 means the platform decides the limit. */
int max_transfer_size;
};
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <driver/uart.h>
#include <tactility/drivers/uart_controller.h>
#include <tactility/drivers/gpio.h>
#ifdef __cplusplus
extern "C" {
#endif
struct Esp32UartConfig {
uart_port_t port;
struct GpioPinSpec pin_tx;
struct GpioPinSpec pin_rx;
struct GpioPinSpec pin_cts;
struct GpioPinSpec pin_rts;
};
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,8 @@
#pragma once
#include <esp_err.h>
#include <tactility/error.h>
/** Convert an esp_err_t to an error_t */
error_t esp_err_to_error(esp_err_t error);
@@ -0,0 +1,32 @@
#pragma once
#include <tactility/drivers/gpio_descriptor.h>
#include <driver/gpio.h>
/**
* Releases the given pin descriptor and sets the pointer value to NULL.
* If the descriptor pointer is null, do nothing.
*/
void release_pin(GpioDescriptor** gpio_descriptor);
/**
* Acquires the pin descriptor for the given pin spec.
* If the pin spec is invalid, the pointer is set to null.
* @return true if the pin was acquired successfully
*/
bool acquire_pin_or_set_null(const GpioPinSpec& pin_spec, GpioDescriptor** gpio_descriptor);
/**
* Safely acquire the native pin value.
* Set to GPIO_NUM_NC if the descriptor is null.
* @param[in] descriptor Pin descriptor to acquire
* @return Native pin number
*/
gpio_num_t get_native_pin(GpioDescriptor* descriptor);
/**
* Returns true if the given pin is inverted.
* @param[in] descriptor Pin descriptor to check, nullable
*/
bool is_pin_inverted(GpioDescriptor* descriptor);
@@ -0,0 +1,132 @@
// SPDX-License-Identifier: Apache-2.0
#include <driver/gpio.h>
#include <tactility/driver.h>
#include <tactility/drivers/esp32_gpio.h>
#include <tactility/error_esp32.h>
#include <tactility/log.h>
#include <tactility/drivers/gpio.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/gpio_descriptor.h>
#define TAG "esp32_gpio"
#define GET_CONFIG(device) ((struct Esp32GpioConfig*)device->config)
extern "C" {
static error_t set_level(GpioDescriptor* descriptor, bool high) {
auto esp_error = gpio_set_level(static_cast<gpio_num_t>(descriptor->pin), high);
return esp_err_to_error(esp_error);
}
static error_t get_level(GpioDescriptor* descriptor, bool* high) {
*high = gpio_get_level(static_cast<gpio_num_t>(descriptor->pin)) != 0;
return ERROR_NONE;
}
static error_t set_flags(GpioDescriptor* descriptor, gpio_flags_t flags) {
const Esp32GpioConfig* config = GET_CONFIG(descriptor->controller);
if (descriptor->pin >= config->gpioCount) {
return ERROR_INVALID_ARGUMENT;
}
gpio_mode_t mode;
if ((flags & GPIO_FLAG_DIRECTION_INPUT_OUTPUT) == GPIO_FLAG_DIRECTION_INPUT_OUTPUT) {
mode = GPIO_MODE_INPUT_OUTPUT;
} else if (flags & GPIO_FLAG_DIRECTION_INPUT) {
mode = GPIO_MODE_INPUT;
} else if (flags & GPIO_FLAG_DIRECTION_OUTPUT) {
mode = GPIO_MODE_OUTPUT;
} else {
return ERROR_INVALID_ARGUMENT;
}
const gpio_config_t esp_config = {
.pin_bit_mask = 1ULL << descriptor->pin,
.mode = mode,
.pull_up_en = (flags & GPIO_FLAG_PULL_UP) ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
.pull_down_en = (flags & GPIO_FLAG_PULL_DOWN) ? GPIO_PULLDOWN_ENABLE : GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_FLAG_INTERRUPT_FROM_OPTIONS(flags),
#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 error_t get_flags(GpioDescriptor* descriptor, gpio_flags_t* flags) {
gpio_io_config_t esp_config;
if (gpio_get_io_config(static_cast<gpio_num_t>(descriptor->pin), &esp_config) != ESP_OK) {
return ERROR_RESOURCE;
}
gpio_flags_t output = 0;
if (esp_config.pu) {
output |= GPIO_FLAG_PULL_UP;
}
if (esp_config.pd) {
output |= GPIO_FLAG_PULL_DOWN;
}
if (esp_config.ie) {
output |= GPIO_FLAG_DIRECTION_INPUT;
}
if (esp_config.oe) {
output |= GPIO_FLAG_DIRECTION_OUTPUT;
}
if (esp_config.oe_inv) {
output |= GPIO_FLAG_ACTIVE_LOW;
}
*flags = output;
return ERROR_NONE;
}
static error_t get_native_pin_number(GpioDescriptor* descriptor, void* pin_number) {
auto* esp_pin_number = reinterpret_cast<gpio_num_t*>(pin_number);
*esp_pin_number = static_cast<gpio_num_t>(descriptor->pin);
return ERROR_NONE;
}
static error_t start(Device* device) {
ESP_LOGI(TAG, "start %s", device->name);
auto pin_count = GET_CONFIG(device)->gpioCount;
return gpio_controller_init_descriptors(device, pin_count, nullptr);
}
static error_t stop(Device* device) {
ESP_LOGI(TAG, "stop %s", device->name);
return gpio_controller_deinit_descriptors(device);
}
const static GpioControllerApi esp32_gpio_api = {
.set_level = set_level,
.get_level = get_level,
.set_flags = set_flags,
.get_flags = get_flags,
.get_native_pin_number = get_native_pin_number
};
extern struct Module platform_module;
Driver esp32_gpio_driver = {
.name = "esp32_gpio",
.compatible = (const char*[]) { "espressif,esp32-gpio", nullptr },
.start_device = start,
.stop_device = stop,
.api = (void*)&esp32_gpio_api,
.device_type = &GPIO_CONTROLLER_TYPE,
.owner = &platform_module,
.internal = nullptr
};
} // extern "C"
@@ -0,0 +1,40 @@
#include <tactility/drivers/esp32_gpio_helpers.h>
#include <tactility/drivers/gpio_controller.h>
#define TAG "gpio"
void release_pin(GpioDescriptor** gpio_descriptor) {
if (*gpio_descriptor == nullptr) return;
check(gpio_descriptor_release(*gpio_descriptor) == ERROR_NONE);
*gpio_descriptor = nullptr;
}
bool acquire_pin_or_set_null(const GpioPinSpec& pin_spec, GpioDescriptor** gpio_descriptor) {
if (pin_spec.gpio_controller == nullptr) {
*gpio_descriptor = nullptr;
return true;
}
*gpio_descriptor = gpio_descriptor_acquire(pin_spec.gpio_controller, pin_spec.pin, GPIO_OWNER_GPIO);
if (*gpio_descriptor == nullptr) {
LOG_E(TAG, "Failed to acquire pin %u from %s", pin_spec.pin, pin_spec.gpio_controller->name);
}
return *gpio_descriptor != nullptr;
}
gpio_num_t get_native_pin(GpioDescriptor* descriptor) {
if (descriptor != nullptr) {
gpio_num_t pin;
check(gpio_descriptor_get_native_pin_number(descriptor, &pin) == ERROR_NONE);
return pin;
} else {
return GPIO_NUM_NC;
}
}
bool is_pin_inverted(GpioDescriptor* descriptor) {
if (!descriptor) return false;
gpio_flags_t flags;
check(gpio_descriptor_get_flags(descriptor, &flags) == ERROR_NONE);
return (flags & GPIO_FLAG_ACTIVE_LOW) != 0;
}
@@ -0,0 +1,261 @@
// SPDX-License-Identifier: Apache-2.0
#include <driver/i2c.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.h>
#include <tactility/log.h>
#include <tactility/time.h>
#define TAG "esp32_i2c"
#define ACK_CHECK_EN 1
struct Esp32I2cInternal {
Mutex mutex {};
GpioDescriptor* sda_descriptor = nullptr;
GpioDescriptor* scl_descriptor = nullptr;
Esp32I2cInternal(GpioDescriptor* sda_descriptor, GpioDescriptor* scl_descriptor) :
sda_descriptor(sda_descriptor),
scl_descriptor(scl_descriptor)
{
mutex_construct(&mutex);
}
~Esp32I2cInternal() {
mutex_destruct(&mutex);
}
};
#define GET_CONFIG(device) ((Esp32I2cConfig*)device->config)
#define GET_DATA(device) ((Esp32I2cInternal*)device_get_driver_data(device))
#define lock(data) mutex_lock(&data->mutex);
#define unlock(data) mutex_unlock(&data->mutex);
extern "C" {
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);
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);
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);
lock(driver_data);
const esp_err_t esp_error = i2c_master_write_to_device(GET_CONFIG(device)->port, address, data, data_size, timeout);
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);
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);
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) {
auto start_time = get_ticks();
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
if (data_size == 0) return ERROR_INVALID_ARGUMENT;
auto* driver_data = GET_DATA(device);
lock(driver_data);
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
esp_err_t error = ESP_OK;
if (cmd == nullptr) {
error = ESP_ERR_NO_MEM;
goto on_error;
}
// Set address pointer
error = i2c_master_start(cmd);
if (error != ESP_OK) goto on_error;
error = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
if (error != ESP_OK) goto on_error;
error = i2c_master_write(cmd, &reg, 1, ACK_CHECK_EN);
if (error != ESP_OK) goto on_error;
// Read length of response from current pointer
error = i2c_master_start(cmd);
if (error != ESP_OK) goto on_error;
error = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_READ, ACK_CHECK_EN);
if (error != ESP_OK) goto on_error;
if (data_size > 1) {
error = i2c_master_read(cmd, data, data_size - 1, I2C_MASTER_ACK);
if (error != ESP_OK) goto on_error;
}
error = i2c_master_read_byte(cmd, data + data_size - 1, I2C_MASTER_NACK);
if (error != ESP_OK) goto on_error;
error = i2c_master_stop(cmd);
if (error != ESP_OK) goto on_error;
error = i2c_master_cmd_begin(GET_CONFIG(device)->port, cmd, get_timeout_remaining_ticks(timeout, start_time));
if (error != ESP_OK) goto on_error;
i2c_cmd_link_delete(cmd);
unlock(driver_data);
return esp_err_to_error(error);
on_error:
i2c_cmd_link_delete(cmd);
unlock(driver_data);
return esp_err_to_error(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) {
auto start_time = get_ticks();
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
if (data_size == 0) return ERROR_INVALID_ARGUMENT;
auto* driver_data = GET_DATA(device);
lock(driver_data);
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
esp_err_t error = ESP_OK;
if (cmd == nullptr) {
error = ESP_ERR_NO_MEM;
goto on_error;
}
error = i2c_master_start(cmd);
if (error != ESP_OK) goto on_error;
error = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
if (error != ESP_OK) goto on_error;
error = i2c_master_write_byte(cmd, reg, ACK_CHECK_EN);
if (error != ESP_OK) goto on_error;
error = i2c_master_write(cmd, (uint8_t*) data, data_size, ACK_CHECK_EN);
if (error != ESP_OK) goto on_error;
error = i2c_master_stop(cmd);
if (error != ESP_OK) goto on_error;
error = i2c_master_cmd_begin(GET_CONFIG(device)->port, cmd, get_timeout_remaining_ticks(timeout, start_time));
if (error != ESP_OK) goto on_error;
i2c_cmd_link_delete(cmd);
unlock(driver_data);
return esp_err_to_error(error);
on_error:
i2c_cmd_link_delete(cmd);
unlock(driver_data);
return esp_err_to_error(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);
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,
.master {
.clk_speed = dts_config->clockFrequency
},
.clk_flags = 0
};
esp_err_t error = i2c_param_config(dts_config->port, &esp_config);
if (error != ESP_OK) {
LOG_E(TAG, "Failed to configure port %d: %s", static_cast<int>(dts_config->port), esp_err_to_name(error));
check(gpio_descriptor_release(sda_descriptor) == ERROR_NONE);
check(gpio_descriptor_release(scl_descriptor) == ERROR_NONE);
return ERROR_RESOURCE;
}
error = i2c_driver_install(dts_config->port, esp_config.mode, 0, 0, 0);
if (error != ESP_OK) {
LOG_E(TAG, "Failed to install driver at port %d: %s", static_cast<int>(dts_config->port), esp_err_to_name(error));
check(gpio_descriptor_release(sda_descriptor) == ERROR_NONE);
check(gpio_descriptor_release(scl_descriptor) == ERROR_NONE);
return ERROR_RESOURCE;
}
auto* data = new(std::nothrow) Esp32I2cInternal(sda_descriptor, scl_descriptor);
if (data == nullptr) {
check(gpio_descriptor_release(sda_descriptor) == ERROR_NONE);
check(gpio_descriptor_release(scl_descriptor) == ERROR_NONE);
return ERROR_OUT_OF_MEMORY;
}
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 = static_cast<Esp32I2cInternal*>(device_get_driver_data(device));
i2c_port_t port = GET_CONFIG(device)->port;
esp_err_t result = i2c_driver_delete(port);
if (result != ESP_OK) {
LOG_E(TAG, "Failed to delete driver at port %d: %s", static_cast<int>(port), esp_err_to_name(result));
return ERROR_RESOURCE;
}
check(gpio_descriptor_release(driver_data->sda_descriptor) == ERROR_NONE);
check(gpio_descriptor_release(driver_data->scl_descriptor) == ERROR_NONE);
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,
.read_register = read_register,
.write_register = write_register
};
extern struct Module platform_module;
Driver esp32_i2c_driver = {
.name = "esp32_i2c",
.compatible = (const char*[]) { "espressif,esp32-i2c", nullptr },
.start_device = start,
.stop_device = stop,
.api = (void*)&esp32_i2c_api,
.device_type = &I2C_CONTROLLER_TYPE,
.owner = &platform_module,
.internal = nullptr
};
} // extern "C"
@@ -0,0 +1,303 @@
// SPDX-License-Identifier: Apache-2.0
#include <driver/i2s_std.h>
#include <driver/i2s_common.h>
#include <tactility/driver.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/gpio_descriptor.h>
#include <tactility/drivers/i2s_controller.h>
#include <tactility/log.h>
#include <tactility/time.h>
#include <tactility/error_esp32.h>
#include <tactility/drivers/esp32_gpio_helpers.h>
#include <tactility/drivers/esp32_i2s.h>
#include <new>
#define TAG "esp32_i2s"
struct Esp32I2sInternal {
Mutex mutex {};
I2sConfig config {};
bool config_set = false;
GpioDescriptor* bclk_descriptor = nullptr;
GpioDescriptor* ws_descriptor = nullptr;
GpioDescriptor* data_out_descriptor = nullptr;
GpioDescriptor* data_in_descriptor = nullptr;
GpioDescriptor* mclk_descriptor = nullptr;
i2s_chan_handle_t tx_handle = nullptr;
i2s_chan_handle_t rx_handle = nullptr;
Esp32I2sInternal() {
mutex_construct(&mutex);
}
~Esp32I2sInternal() {
cleanup_pins();
mutex_destruct(&mutex);
}
void cleanup_pins() {
release_pin(&bclk_descriptor);
release_pin(&ws_descriptor);
release_pin(&data_out_descriptor);
release_pin(&data_in_descriptor);
release_pin(&mclk_descriptor);
}
bool init_pins(Esp32I2sConfig* dts_config) {
check (!ws_descriptor && !bclk_descriptor && !data_out_descriptor && !data_in_descriptor && !mclk_descriptor);
auto& ws_spec = dts_config->pin_ws;
auto& bclk_spec = dts_config->pin_bclk;
auto& data_in_spec = dts_config->pin_data_in;
auto& data_out_spec = dts_config->pin_data_out;
auto& mclk_spec = dts_config->pin_mclk;
bool success = acquire_pin_or_set_null(ws_spec, &ws_descriptor) &&
acquire_pin_or_set_null(bclk_spec, &bclk_descriptor) &&
acquire_pin_or_set_null(data_in_spec, &data_in_descriptor) &&
acquire_pin_or_set_null(data_out_spec, &data_out_descriptor) &&
acquire_pin_or_set_null(mclk_spec, &mclk_descriptor);
if (!success) {
cleanup_pins();
LOG_E(TAG, "Failed to acquire all pins");
return false;
}
return true;
}
};
#define GET_CONFIG(device) ((Esp32I2sConfig*)(device)->config)
#define GET_DATA(device) ((Esp32I2sInternal*)device_get_driver_data(device))
#define lock(data) mutex_lock(&data->mutex);
#define unlock(data) mutex_unlock(&data->mutex);
extern "C" {
static error_t cleanup_channel_handles(Esp32I2sInternal* driver_data) {
// TODO: error handling of i2ss functions
if (driver_data->tx_handle) {
i2s_channel_disable(driver_data->tx_handle);
i2s_del_channel(driver_data->tx_handle);
driver_data->tx_handle = nullptr;
}
if (driver_data->rx_handle) {
i2s_channel_disable(driver_data->rx_handle);
i2s_del_channel(driver_data->rx_handle);
driver_data->rx_handle = nullptr;
}
return ERROR_NONE;
}
static i2s_data_bit_width_t to_esp32_bits_per_sample(uint8_t bits) {
switch (bits) {
case 8: return I2S_DATA_BIT_WIDTH_8BIT;
case 16: return I2S_DATA_BIT_WIDTH_16BIT;
case 24: return I2S_DATA_BIT_WIDTH_24BIT;
case 32: return I2S_DATA_BIT_WIDTH_32BIT;
default: return I2S_DATA_BIT_WIDTH_16BIT;
}
}
static void get_esp32_std_config(Esp32I2sInternal* internal, const I2sConfig* config, i2s_std_config_t* std_cfg) {
std_cfg->clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(config->sample_rate);
std_cfg->slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(to_esp32_bits_per_sample(config->bits_per_sample), I2S_SLOT_MODE_STEREO);
if (config->communication_format & I2S_FORMAT_STAND_MSB) {
std_cfg->slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(to_esp32_bits_per_sample(config->bits_per_sample), I2S_SLOT_MODE_STEREO);
} else if (config->communication_format & (I2S_FORMAT_STAND_PCM_SHORT | I2S_FORMAT_STAND_PCM_LONG)) {
std_cfg->slot_cfg = I2S_STD_PCM_SLOT_DEFAULT_CONFIG(to_esp32_bits_per_sample(config->bits_per_sample), I2S_SLOT_MODE_STEREO);
}
if (config->channel_left != I2S_CHANNEL_NONE && config->channel_right == I2S_CHANNEL_NONE) {
std_cfg->slot_cfg.slot_mask = I2S_STD_SLOT_LEFT;
} else if (config->channel_left == I2S_CHANNEL_NONE && config->channel_right != I2S_CHANNEL_NONE) {
std_cfg->slot_cfg.slot_mask = I2S_STD_SLOT_RIGHT;
} else {
std_cfg->slot_cfg.slot_mask = I2S_STD_SLOT_BOTH;
}
gpio_num_t mclk_pin = get_native_pin(internal->mclk_descriptor);
gpio_num_t bclk_pin = get_native_pin(internal->bclk_descriptor);
gpio_num_t ws_pin = get_native_pin(internal->ws_descriptor);
gpio_num_t data_out_pin = get_native_pin(internal->data_out_descriptor);
gpio_num_t data_in_pin = get_native_pin(internal->data_in_descriptor);
LOG_I(TAG, "Configuring I2S pins: MCLK=%d, BCLK=%d, WS=%d, DATA_OUT=%d, DATA_IN=%d", mclk_pin, bclk_pin, ws_pin, data_out_pin, data_in_pin);
bool mclk_inverted = is_pin_inverted(internal->mclk_descriptor);
bool bclk_inverted = is_pin_inverted(internal->bclk_descriptor);
bool ws_inverted = is_pin_inverted(internal->ws_descriptor);
LOG_I(TAG, "Inverted pins: MCLK=%u, BCLK=%u, WS=%u", mclk_inverted, bclk_inverted, ws_inverted);
std_cfg->gpio_cfg = {
.mclk = mclk_pin,
.bclk = bclk_pin,
.ws = ws_pin,
.dout = data_out_pin,
.din = data_in_pin,
.invert_flags = {
.mclk_inv = mclk_inverted,
.bclk_inv = bclk_inverted,
.ws_inv = ws_inverted
}
};
}
static error_t read(Device* device, void* data, size_t data_size, size_t* bytes_read, TickType_t timeout) {
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
auto* driver_data = GET_DATA(device);
if (!driver_data->rx_handle) return ERROR_NOT_SUPPORTED;
lock(driver_data);
const esp_err_t esp_error = i2s_channel_read(driver_data->rx_handle, data, data_size, bytes_read, timeout);
unlock(driver_data);
return esp_err_to_error(esp_error);
}
static error_t write(Device* device, const void* data, size_t data_size, size_t* bytes_written, TickType_t timeout) {
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
auto* driver_data = GET_DATA(device);
if (!driver_data->tx_handle) return ERROR_NOT_SUPPORTED;
lock(driver_data);
const esp_err_t esp_error = i2s_channel_write(driver_data->tx_handle, data, data_size, bytes_written, timeout);
unlock(driver_data);
return esp_err_to_error(esp_error);
}
static error_t set_config(Device* device, const struct I2sConfig* config) {
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
if (
config->bits_per_sample != 8 &&
config->bits_per_sample != 16 &&
config->bits_per_sample != 24 &&
config->bits_per_sample != 32
) {
return ERROR_INVALID_ARGUMENT;
}
auto* internal = GET_DATA(device);
auto* dts_config = GET_CONFIG(device);
lock(internal);
cleanup_channel_handles(internal);
internal->config_set = false;
// Create new channel handles
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(dts_config->port, I2S_ROLE_MASTER);
esp_err_t esp_error = i2s_new_channel(&chan_cfg, &internal->tx_handle, &internal->rx_handle);
if (esp_error != ESP_OK) {
LOG_E(TAG, "Failed to create I2S channels: %s", esp_err_to_name(esp_error));
unlock(internal);
return ERROR_RESOURCE;
}
i2s_std_config_t std_cfg = {};
get_esp32_std_config(internal, config, &std_cfg);
if (internal->tx_handle) {
esp_error = i2s_channel_init_std_mode(internal->tx_handle, &std_cfg);
if (esp_error == ESP_OK) esp_error = i2s_channel_enable(internal->tx_handle);
}
if (esp_error == ESP_OK && internal->rx_handle) {
esp_error = i2s_channel_init_std_mode(internal->rx_handle, &std_cfg);
if (esp_error == ESP_OK) esp_error = i2s_channel_enable(internal->rx_handle);
}
if (esp_error != ESP_OK) {
LOG_E(TAG, "Failed to initialize/enable I2S channels: %s", esp_err_to_name(esp_error));
cleanup_channel_handles(internal);
unlock(internal);
return esp_err_to_error(esp_error);
}
// Update runtime config to reflect current state
memcpy(&internal->config, config, sizeof(I2sConfig));
internal->config_set = true;
unlock(internal);
return ERROR_NONE;
}
static error_t get_config(Device* device, struct I2sConfig* config) {
auto* driver_data = GET_DATA(device);
lock(driver_data);
if (!driver_data->config_set) {
unlock(driver_data);
return ERROR_RESOURCE;
}
memcpy(config, &driver_data->config, sizeof(I2sConfig));
unlock(driver_data);
return ERROR_NONE;
}
static error_t start(Device* device) {
ESP_LOGI(TAG, "start %s", device->name);
auto* dts_config = GET_CONFIG(device);
auto* data = new(std::nothrow) Esp32I2sInternal();
if (!data) return ERROR_OUT_OF_MEMORY;
if (!data->init_pins(dts_config)) {
LOG_E(TAG, "Failed to init one or more pins");
delete data;
return ERROR_RESOURCE;
}
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);
lock(driver_data);
cleanup_channel_handles(driver_data);
unlock(driver_data);
device_set_driver_data(device, nullptr);
delete driver_data;
return ERROR_NONE;
}
static error_t reset(Device* device) {
ESP_LOGI(TAG, "reset %s", device->name);
auto* driver_data = GET_DATA(device);
lock(driver_data);
cleanup_channel_handles(driver_data);
unlock(driver_data);
return ERROR_NONE;
}
const static I2sControllerApi esp32_i2s_api = {
.read = read,
.write = write,
.set_config = set_config,
.get_config = get_config,
.reset = reset
};
extern struct Module platform_module;
Driver esp32_i2s_driver = {
.name = "esp32_i2s",
.compatible = (const char*[]) { "espressif,esp32-i2s", nullptr },
.start_device = start,
.stop_device = stop,
.api = (void*)&esp32_i2s_api,
.device_type = &I2S_CONTROLLER_TYPE,
.owner = &platform_module,
.internal = nullptr
};
} // extern "C"
@@ -0,0 +1,158 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/device.h>
#include <tactility/drivers/esp32_spi.h>
#include <tactility/concurrent/recursive_mutex.h>
#include "tactility/drivers/gpio_descriptor.h"
#include <tactility/drivers/esp32_gpio_helpers.h>
#include <cstring>
#include <esp_log.h>
#include <new>
#include <soc/gpio_num.h>
#define TAG "esp32_spi"
#define GET_CONFIG(device) ((const struct Esp32SpiConfig*)device->config)
#define GET_DATA(device) ((struct Esp32SpiInternal*)device_get_driver_data(device))
extern "C" {
struct Esp32SpiInternal {
RecursiveMutex mutex = {};
bool initialized = false;
// Pin descriptors
GpioDescriptor* sclk_descriptor = nullptr;
GpioDescriptor* mosi_descriptor = nullptr;
GpioDescriptor* miso_descriptor = nullptr;
GpioDescriptor* wp_descriptor = nullptr;
GpioDescriptor* hd_descriptor = nullptr;
explicit Esp32SpiInternal() {
recursive_mutex_construct(&mutex);
}
~Esp32SpiInternal() {
cleanup_pins();
recursive_mutex_destruct(&mutex);
}
void cleanup_pins() {
release_pin(&sclk_descriptor);
release_pin(&mosi_descriptor);
release_pin(&miso_descriptor);
release_pin(&wp_descriptor);
release_pin(&hd_descriptor);
}
};
static error_t lock(Device* device) {
auto* driver_data = GET_DATA(device);
recursive_mutex_lock(&driver_data->mutex);
return ERROR_NONE;
}
static error_t try_lock(Device* device, TickType_t timeout) {
auto* driver_data = GET_DATA(device);
if (recursive_mutex_try_lock(&driver_data->mutex, timeout)) {
return ERROR_NONE;
}
return ERROR_TIMEOUT;
}
static error_t unlock(Device* device) {
auto* driver_data = GET_DATA(device);
recursive_mutex_unlock(&driver_data->mutex);
return ERROR_NONE;
}
static error_t start(Device* device) {
ESP_LOGI(TAG, "start %s", device->name);
auto* data = new (std::nothrow) Esp32SpiInternal();
if (!data) return ERROR_OUT_OF_MEMORY;
device_set_driver_data(device, data);
auto* dts_config = GET_CONFIG(device);
// Acquire pins from the specified GPIO pin specs. Optional pins are allowed.
bool pins_ok =
acquire_pin_or_set_null(dts_config->pin_sclk, &data->sclk_descriptor) &&
acquire_pin_or_set_null(dts_config->pin_mosi, &data->mosi_descriptor) &&
acquire_pin_or_set_null(dts_config->pin_miso, &data->miso_descriptor) &&
acquire_pin_or_set_null(dts_config->pin_wp, &data->wp_descriptor) &&
acquire_pin_or_set_null(dts_config->pin_hd, &data->hd_descriptor);
if (!pins_ok) {
ESP_LOGE(TAG, "Failed to acquire required SPI pins");
data->cleanup_pins();
device_set_driver_data(device, nullptr);
delete data;
return ERROR_RESOURCE;
}
spi_bus_config_t buscfg = {
.mosi_io_num = get_native_pin(data->mosi_descriptor),
.miso_io_num = get_native_pin(data->miso_descriptor),
.sclk_io_num = get_native_pin(data->sclk_descriptor),
.data2_io_num = get_native_pin(data->wp_descriptor),
.data3_io_num = get_native_pin(data->hd_descriptor),
.data4_io_num = GPIO_NUM_NC,
.data5_io_num = GPIO_NUM_NC,
.data6_io_num = GPIO_NUM_NC,
.data7_io_num = GPIO_NUM_NC,
.data_io_default_level = false,
.max_transfer_sz = dts_config->max_transfer_size,
.flags = 0,
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
.intr_flags = 0
};
esp_err_t ret = spi_bus_initialize(dts_config->host, &buscfg, SPI_DMA_CH_AUTO);
if (ret != ESP_OK) {
data->cleanup_pins();
device_set_driver_data(device, nullptr);
delete data;
ESP_LOGE(TAG, "Failed to initialize SPI bus: %s", esp_err_to_name(ret));
return ERROR_RESOURCE;
}
data->initialized = true;
return ERROR_NONE;
}
static error_t stop(Device* device) {
ESP_LOGI(TAG, "stop %s", device->name);
auto* driver_data = GET_DATA(device);
auto* dts_config = GET_CONFIG(device);
if (driver_data->initialized) {
spi_bus_free(dts_config->host);
}
driver_data->cleanup_pins();
device_set_driver_data(device, nullptr);
delete driver_data;
return ERROR_NONE;
}
const static struct SpiControllerApi esp32_spi_api = {
.lock = lock,
.try_lock = try_lock,
.unlock = unlock
};
extern struct Module platform_module;
Driver esp32_spi_driver = {
.name = "esp32_spi",
.compatible = (const char*[]) { "espressif,esp32-spi", nullptr },
.start_device = start,
.stop_device = stop,
.api = (void*)&esp32_spi_api,
.device_type = &SPI_CONTROLLER_TYPE,
.owner = &platform_module,
.internal = nullptr
};
} // extern "C"
@@ -0,0 +1,418 @@
// SPDX-License-Identifier: Apache-2.0
#include <driver/uart.h>
#include <tactility/concurrent/mutex.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/uart_controller.h>
#include <tactility/drivers/esp32_uart.h>
#include <tactility/error_esp32.h>
#include <tactility/log.h>
#include <tactility/time.h>
#include <tactility/drivers/gpio_descriptor.h>
#include <tactility/drivers/esp32_gpio_helpers.h>
#include <new>
#define TAG "esp32_uart"
struct Esp32UartInternal {
Mutex mutex {};
UartConfig config {};
bool config_set = false;
bool is_open = false;
// Pin descriptors
GpioDescriptor* tx_descriptor = nullptr;
GpioDescriptor* rx_descriptor = nullptr;
GpioDescriptor* cts_descriptor = nullptr;
GpioDescriptor* rts_descriptor = nullptr;
Esp32UartInternal() {
mutex_construct(&mutex);
}
~Esp32UartInternal() {
cleanup_pins();
mutex_destruct(&mutex);
}
void cleanup_pins() {
release_pin(&tx_descriptor);
release_pin(&rx_descriptor);
release_pin(&cts_descriptor);
release_pin(&rts_descriptor);
}
};
#define GET_CONFIG(device) ((Esp32UartConfig*)device->config)
#define GET_DATA(device) ((Esp32UartInternal*)device_get_driver_data(device))
#define lock(data) mutex_lock(&data->mutex)
#define unlock(data) mutex_unlock(&data->mutex)
extern "C" {
static uart_parity_t to_esp32_parity(enum UartParity parity) {
switch (parity) {
case UART_CONTROLLER_PARITY_DISABLE: return UART_PARITY_DISABLE;
case UART_CONTROLLER_PARITY_EVEN: return UART_PARITY_EVEN;
case UART_CONTROLLER_PARITY_ODD: return UART_PARITY_ODD;
default: return UART_PARITY_DISABLE;
}
}
static uart_word_length_t to_esp32_data_bits(enum UartDataBits bits) {
switch (bits) {
case UART_CONTROLLER_DATA_5_BITS: return UART_DATA_5_BITS;
case UART_CONTROLLER_DATA_6_BITS: return UART_DATA_6_BITS;
case UART_CONTROLLER_DATA_7_BITS: return UART_DATA_7_BITS;
case UART_CONTROLLER_DATA_8_BITS: return UART_DATA_8_BITS;
default: return UART_DATA_8_BITS;
}
}
static uart_stop_bits_t to_esp32_stop_bits(enum UartStopBits bits) {
switch (bits) {
case UART_CONTROLLER_STOP_BITS_1: return UART_STOP_BITS_1;
case UART_CONTROLLER_STOP_BITS_1_5: return UART_STOP_BITS_1_5;
case UART_CONTROLLER_STOP_BITS_2: return UART_STOP_BITS_2;
default: return UART_STOP_BITS_1;
}
}
static error_t read_byte(Device* device, uint8_t* out, TickType_t timeout) {
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
auto* driver_data = GET_DATA(device);
auto* dts_config = GET_CONFIG(device);
lock(driver_data);
if (!driver_data->is_open) {
unlock(driver_data);
return ERROR_INVALID_STATE;
}
int len = uart_read_bytes(dts_config->port, out, 1, timeout);
unlock(driver_data);
if (len < 0) return ERROR_RESOURCE;
if (len == 0) return ERROR_TIMEOUT;
return ERROR_NONE;
}
static error_t write_byte(Device* device, uint8_t out, TickType_t timeout) {
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
auto* driver_data = GET_DATA(device);
auto* dts_config = GET_CONFIG(device);
lock(driver_data);
if (!driver_data->is_open) {
unlock(driver_data);
return ERROR_INVALID_STATE;
}
int len = uart_write_bytes(dts_config->port, (const char*)&out, 1);
if (len < 0) {
unlock(driver_data);
return ERROR_RESOURCE;
}
// uart_write_bytes is non-blocking on the buffer but we might want to wait for it to be sent?
// The API signature has timeout, but ESP-IDF's uart_write_bytes doesn't use it for blocking.
// However, if we want to ensure it's SENT, we could use uart_wait_tx_done.
if (timeout > 0) {
esp_err_t err = uart_wait_tx_done(dts_config->port, timeout);
unlock(driver_data);
if (err == ESP_ERR_TIMEOUT) return ERROR_TIMEOUT;
if (err != ESP_OK) return ERROR_RESOURCE;
} else {
unlock(driver_data);
}
return ERROR_NONE;
}
static error_t write_bytes(Device* device, const uint8_t* buffer, size_t buffer_size, TickType_t timeout) {
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
auto* driver_data = GET_DATA(device);
auto* dts_config = GET_CONFIG(device);
lock(driver_data);
if (!driver_data->is_open) {
unlock(driver_data);
return ERROR_INVALID_STATE;
}
int len = uart_write_bytes(dts_config->port, (const char*)buffer, buffer_size);
if (len < 0) {
unlock(driver_data);
return ERROR_RESOURCE;
}
if (timeout > 0) {
esp_err_t err = uart_wait_tx_done(dts_config->port, timeout);
unlock(driver_data);
if (err == ESP_ERR_TIMEOUT) return ERROR_TIMEOUT;
if (err != ESP_OK) return ERROR_RESOURCE;
} else {
unlock(driver_data);
}
return ERROR_NONE;
}
static error_t read_bytes(Device* device, uint8_t* buffer, size_t buffer_size, TickType_t timeout) {
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
auto* driver_data = GET_DATA(device);
auto* dts_config = GET_CONFIG(device);
lock(driver_data);
if (!driver_data->is_open) {
unlock(driver_data);
return ERROR_INVALID_STATE;
}
int len = uart_read_bytes(dts_config->port, buffer, buffer_size, timeout);
unlock(driver_data);
if (len < 0) return ERROR_RESOURCE;
if (len < (int)buffer_size) return ERROR_TIMEOUT;
return ERROR_NONE;
}
static error_t get_available(Device* device, size_t* available_bytes) {
auto* driver_data = GET_DATA(device);
auto* dts_config = GET_CONFIG(device);
lock(driver_data);
if (!driver_data->is_open) {
unlock(driver_data);
return ERROR_INVALID_STATE;
}
esp_err_t err = uart_get_buffered_data_len(dts_config->port, available_bytes);
unlock(driver_data);
if (err != ESP_OK) return esp_err_to_error(err);
return ERROR_NONE;
}
static error_t set_config(Device* device, const struct UartConfig* config) {
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
auto* driver_data = GET_DATA(device);
lock(driver_data);
if (driver_data->is_open) {
unlock(driver_data);
return ERROR_INVALID_STATE;
}
memcpy(&driver_data->config, config, sizeof(UartConfig));
driver_data->config_set = true;
unlock(driver_data);
return ERROR_NONE;
}
static error_t get_config(Device* device, struct UartConfig* config) {
auto* driver_data = GET_DATA(device);
lock(driver_data);
if (!driver_data->config_set) {
unlock(driver_data);
return ERROR_RESOURCE;
}
memcpy(config, &driver_data->config, sizeof(UartConfig));
unlock(driver_data);
return ERROR_NONE;
}
static error_t open(Device* device) {
ESP_LOGI(TAG, "%s open", device->name);
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
auto* driver_data = GET_DATA(device);
auto* dts_config = GET_CONFIG(device);
lock(driver_data);
if (driver_data->is_open) {
unlock(driver_data);
LOG_W(TAG, "%s is already open", device->name);
return ERROR_INVALID_STATE;
}
if (!driver_data->config_set) {
unlock(driver_data);
LOG_E(TAG, "%s open failed: config not set", device->name);
return ERROR_INVALID_STATE;
}
esp_err_t esp_error = uart_driver_install(dts_config->port, 1024, 0, 0, NULL, 0);
if (esp_error != ESP_OK) {
LOG_E(TAG, "%s failed to install: %s", device->name, esp_err_to_name(esp_error));
unlock(driver_data);
return esp_err_to_error(esp_error);
}
uart_config_t uart_config = {
.baud_rate = (int)driver_data->config.baud_rate,
.data_bits = to_esp32_data_bits(driver_data->config.data_bits),
.parity = to_esp32_parity(driver_data->config.parity),
.stop_bits = to_esp32_stop_bits(driver_data->config.stop_bits),
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE, // Flow control is not yet exposed via UartConfig
.rx_flow_ctrl_thresh = 0,
.source_clk = UART_SCLK_DEFAULT,
.flags = {
.allow_pd = 0,
.backup_before_sleep = 0
}
};
if (dts_config->pin_cts.gpio_controller != nullptr || dts_config->pin_rts.gpio_controller != nullptr) {
LOG_W(TAG, "%s: CTS/RTS pins are defined but hardware flow control is disabled (not supported in UartConfig)", device->name);
}
esp_error = uart_param_config(dts_config->port, &uart_config);
if (esp_error != ESP_OK) {
LOG_E(TAG, "%s failed to configure: %s", device->name, esp_err_to_name(esp_error));
uart_driver_delete(dts_config->port);
unlock(driver_data);
return ERROR_RESOURCE;
}
// Acquire pins from the specified GPIO pin specs. Optional pins are allowed.
bool pins_ok =
acquire_pin_or_set_null(dts_config->pin_tx, &driver_data->tx_descriptor) &&
acquire_pin_or_set_null(dts_config->pin_rx, &driver_data->rx_descriptor) &&
acquire_pin_or_set_null(dts_config->pin_cts, &driver_data->cts_descriptor) &&
acquire_pin_or_set_null(dts_config->pin_rts, &driver_data->rts_descriptor);
if (!pins_ok) {
LOG_E(TAG, "%s failed to acquire UART pins", device->name);
driver_data->cleanup_pins();
uart_driver_delete(dts_config->port);
unlock(driver_data);
return ERROR_RESOURCE;
}
esp_error = uart_set_pin(dts_config->port,
get_native_pin(driver_data->tx_descriptor),
get_native_pin(driver_data->rx_descriptor),
get_native_pin(driver_data->rts_descriptor),
get_native_pin(driver_data->cts_descriptor)
);
if (esp_error != ESP_OK) {
LOG_E(TAG, "%s failed to set uart pins: %s", device->name, esp_err_to_name(esp_error));
driver_data->cleanup_pins();
uart_driver_delete(dts_config->port);
unlock(driver_data);
return ERROR_RESOURCE;
}
driver_data->is_open = true;
unlock(driver_data);
return ERROR_NONE;
}
static error_t close(Device* device) {
ESP_LOGI(TAG, "%s close", device->name);
if (xPortInIsrContext()) return ERROR_ISR_STATUS;
auto* driver_data = GET_DATA(device);
auto* dts_config = GET_CONFIG(device);
lock(driver_data);
if (!driver_data->is_open) {
unlock(driver_data);
LOG_W(TAG, "Already closed");
return ERROR_INVALID_STATE;
}
uart_driver_delete(dts_config->port);
driver_data->cleanup_pins();
driver_data->is_open = false;
unlock(driver_data);
return ERROR_NONE;
}
static bool is_open(Device* device) {
auto* driver_data = GET_DATA(device);
lock(driver_data);
bool status = driver_data->is_open;
unlock(driver_data);
return status;
}
static error_t flush_input(Device* device) {
auto* driver_data = GET_DATA(device);
auto* dts_config = GET_CONFIG(device);
lock(driver_data);
if (!driver_data->is_open) {
unlock(driver_data);
return ERROR_INVALID_STATE;
}
esp_err_t err = uart_flush_input(dts_config->port);
unlock(driver_data);
return esp_err_to_error(err);
}
static error_t start(Device* device) {
ESP_LOGI(TAG, "%s start", device->name);
auto* data = new(std::nothrow) Esp32UartInternal();
if (!data) return ERROR_OUT_OF_MEMORY;
device_set_driver_data(device, data);
return ERROR_NONE;
}
static error_t stop(Device* device) {
ESP_LOGI(TAG, "%s stop", device->name);
auto* driver_data = GET_DATA(device);
lock(driver_data);
if (driver_data->is_open) {
unlock(driver_data);
return ERROR_INVALID_STATE;
}
unlock(driver_data);
device_set_driver_data(device, nullptr);
delete driver_data;
return ERROR_NONE;
}
const static UartControllerApi esp32_uart_api = {
.read_byte = read_byte,
.write_byte = write_byte,
.write_bytes = write_bytes,
.read_bytes = read_bytes,
.get_available = get_available,
.set_config = set_config,
.get_config = get_config,
.open = open,
.close = close,
.is_open = is_open,
.flush_input = flush_input
};
extern struct Module platform_module;
Driver esp32_uart_driver = {
.name = "esp32_uart",
.compatible = (const char*[]) { "espressif,esp32-uart", nullptr },
.start_device = start,
.stop_device = stop,
.api = (void*)&esp32_uart_api,
.device_type = &UART_CONTROLLER_TYPE,
.owner = &platform_module,
.internal = nullptr
};
} // extern "C"
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/error_esp32.h>
error_t esp_err_to_error(esp_err_t error) {
switch (error) {
case ESP_OK:
return ERROR_NONE;
case ESP_ERR_INVALID_ARG:
return ERROR_INVALID_ARGUMENT;
case ESP_ERR_INVALID_STATE:
return ERROR_INVALID_STATE;
case ESP_ERR_TIMEOUT:
return ERROR_TIMEOUT;
case ESP_ERR_NO_MEM:
return ERROR_OUT_OF_MEMORY;
case ESP_ERR_NOT_SUPPORTED:
return ERROR_NOT_SUPPORTED;
default:
return ERROR_UNDEFINED;
}
}
@@ -0,0 +1,44 @@
#include <tactility/check.h>
#include <tactility/driver.h>
#include <tactility/module.h>
extern "C" {
extern Driver esp32_gpio_driver;
extern Driver esp32_i2c_driver;
extern Driver esp32_i2s_driver;
extern Driver esp32_spi_driver;
extern Driver esp32_uart_driver;
static error_t start() {
/* We crash when construct fails, because if a single driver fails to construct,
* 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_i2s_driver) == ERROR_NONE);
check(driver_construct_add(&esp32_spi_driver) == ERROR_NONE);
check(driver_construct_add(&esp32_uart_driver) == ERROR_NONE);
return ERROR_NONE;
}
static error_t stop() {
/* We crash when destruct fails, because if a single driver fails to destruct,
* there is no guarantee that the previously destroyed drivers can be recovered */
check(driver_remove_destruct(&esp32_gpio_driver) == ERROR_NONE);
check(driver_remove_destruct(&esp32_i2c_driver) == ERROR_NONE);
check(driver_remove_destruct(&esp32_i2s_driver) == ERROR_NONE);
check(driver_remove_destruct(&esp32_spi_driver) == ERROR_NONE);
check(driver_remove_destruct(&esp32_uart_driver) == ERROR_NONE);
return ERROR_NONE;
}
// The name must be exactly "platform_module"
struct Module platform_module = {
.name = "platform-esp32",
.start = start,
.stop = stop,
.symbols = nullptr,
.internal = nullptr
};
}