Add kernel drivers for SPI and UART and make locking APIs more consistent (#489)
- Add kernel support for SPI driver - Add kernel support for UART driver - Implemented ESP32 UART kernel driver - Update existing UART-related code in Tactility to use new kernel driver - Remove UART from tt::hal::Configuration - Remove tt_hal_uart functionality but keep functions for now - Update devicetrees for UART changes - Kernel mutex and recursive mutex: improved locking API design - Other kernel improvements - Added device_exists_of_type() and device_find_by_name()
This commit is contained in:
committed by
GitHub
parent
7e24105d0c
commit
74127a5f6c
@@ -0,0 +1,27 @@
|
||||
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: int
|
||||
required: true
|
||||
description: TX pin
|
||||
pin-rx:
|
||||
type: int
|
||||
required: true
|
||||
description: RX pin
|
||||
pin-cts:
|
||||
type: int
|
||||
description: CTS pin
|
||||
pin-rts:
|
||||
type: int
|
||||
description: RTS pin
|
||||
@@ -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
|
||||
@@ -17,7 +17,6 @@ struct Esp32I2cConfig {
|
||||
bool pinSclPullUp;
|
||||
};
|
||||
|
||||
error_t esp32_i2c_get_port(struct Device* device, i2c_port_t* port);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <driver/spi_common.h>
|
||||
#include <tactility/drivers/spi_controller.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct Esp32SpiConfig {
|
||||
spi_host_device_t host;
|
||||
int pin_mosi;
|
||||
int pin_miso;
|
||||
int pin_sclk;
|
||||
int pin_wp;
|
||||
int pin_hd;
|
||||
int max_transfer_size;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <driver/uart.h>
|
||||
#include <tactility/drivers/uart_controller.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct Esp32UartConfig {
|
||||
uart_port_t port;
|
||||
int pinTx;
|
||||
int pinRx;
|
||||
int pinCts;
|
||||
int pinRts;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -12,20 +12,20 @@
|
||||
#define TAG "esp32_i2c"
|
||||
#define ACK_CHECK_EN 1
|
||||
|
||||
struct InternalData {
|
||||
struct Esp32SpiInternal {
|
||||
Mutex mutex { 0 };
|
||||
|
||||
InternalData() {
|
||||
Esp32SpiInternal() {
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
~InternalData() {
|
||||
~Esp32SpiInternal() {
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
};
|
||||
|
||||
#define GET_CONFIG(device) ((Esp32I2cConfig*)device->config)
|
||||
#define GET_DATA(device) ((InternalData*)device_get_driver_data(device))
|
||||
#define GET_DATA(device) ((Esp32SpiInternal*)device_get_driver_data(device))
|
||||
|
||||
#define lock(data) mutex_lock(&data->mutex);
|
||||
#define unlock(data) mutex_unlock(&data->mutex);
|
||||
@@ -144,11 +144,6 @@ on_error:
|
||||
return esp_err_to_error(error);
|
||||
}
|
||||
|
||||
error_t esp32_i2c_get_port(struct Device* device, i2c_port_t* port) {
|
||||
auto* config = GET_CONFIG(device);
|
||||
*port = config->port;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t start(Device* device) {
|
||||
ESP_LOGI(TAG, "start %s", device->name);
|
||||
@@ -177,14 +172,14 @@ static error_t start(Device* device) {
|
||||
LOG_E(TAG, "Failed to install driver at port %d: %s", static_cast<int>(dts_config->port), esp_err_to_name(error));
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
auto* data = new InternalData();
|
||||
auto* data = new Esp32SpiInternal();
|
||||
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<InternalData*>(device_get_driver_data(device));
|
||||
auto* driver_data = static_cast<Esp32SpiInternal*>(device_get_driver_data(device));
|
||||
|
||||
i2c_port_t port = GET_CONFIG(device)->port;
|
||||
esp_err_t result = i2c_driver_delete(port);
|
||||
|
||||
@@ -14,31 +14,31 @@
|
||||
|
||||
#define TAG "esp32_i2s"
|
||||
|
||||
struct InternalData {
|
||||
Mutex mutex { 0 };
|
||||
struct Esp32I2sInternal {
|
||||
Mutex mutex {};
|
||||
i2s_chan_handle_t tx_handle = nullptr;
|
||||
i2s_chan_handle_t rx_handle = nullptr;
|
||||
I2sConfig config {};
|
||||
bool config_set = false;
|
||||
|
||||
InternalData() {
|
||||
Esp32I2sInternal() {
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
~InternalData() {
|
||||
~Esp32I2sInternal() {
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
};
|
||||
|
||||
#define GET_CONFIG(device) ((Esp32I2sConfig*)device->config)
|
||||
#define GET_DATA(device) ((InternalData*)device_get_driver_data(device))
|
||||
#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(InternalData* driver_data) {
|
||||
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);
|
||||
@@ -188,7 +188,7 @@ static error_t get_config(Device* device, struct I2sConfig* config) {
|
||||
|
||||
static error_t start(Device* device) {
|
||||
ESP_LOGI(TAG, "start %s", device->name);
|
||||
auto* data = new(std::nothrow) InternalData();
|
||||
auto* data = new(std::nothrow) Esp32I2sInternal();
|
||||
if (!data) return ERROR_OUT_OF_MEMORY;
|
||||
|
||||
device_set_driver_data(device, data);
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/esp32_spi.h>
|
||||
#include <tactility/concurrent/mutex.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 {
|
||||
Mutex mutex;
|
||||
bool initialized = false;
|
||||
|
||||
Esp32SpiInternal() {
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
~Esp32SpiInternal() {
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
};
|
||||
|
||||
static error_t lock(Device* device) {
|
||||
auto* driver_data = GET_DATA(device);
|
||||
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 (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);
|
||||
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);
|
||||
|
||||
spi_bus_config_t buscfg = {
|
||||
.mosi_io_num = dts_config->pin_mosi,
|
||||
.miso_io_num = dts_config->pin_miso,
|
||||
.sclk_io_num = dts_config->pin_sclk,
|
||||
.data2_io_num = dts_config->pin_wp,
|
||||
.data3_io_num = dts_config->pin_hd,
|
||||
.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) {
|
||||
delete data;
|
||||
device_set_driver_data(device, nullptr);
|
||||
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);
|
||||
}
|
||||
|
||||
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,379 @@
|
||||
// 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 <new>
|
||||
|
||||
#define TAG "esp32_uart"
|
||||
|
||||
struct Esp32UartInternal {
|
||||
Mutex mutex {};
|
||||
UartConfig config {};
|
||||
bool config_set = false;
|
||||
bool is_open = false;
|
||||
|
||||
Esp32UartInternal() {
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
~Esp32UartInternal() {
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
};
|
||||
|
||||
#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->pinCts != UART_PIN_NO_CHANGE || dts_config->pinRts != UART_PIN_NO_CHANGE) {
|
||||
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;
|
||||
}
|
||||
|
||||
esp_error = uart_set_pin(dts_config->port, dts_config->pinTx, dts_config->pinRx, dts_config->pinCts, dts_config->pinRts);
|
||||
if (esp_error != ESP_OK) {
|
||||
LOG_E(TAG, "%s failed to set uart pins: %s", device->name, esp_err_to_name(esp_error));
|
||||
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->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"
|
||||
@@ -7,6 +7,8 @@ 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,
|
||||
@@ -14,6 +16,8 @@ static error_t start() {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -23,6 +27,8 @@ static error_t stop() {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user