mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-18 10:53:17 +00:00
I2S driver implementation (#480)
* **New Features** * ESP32 I2S controller support: runtime-configurable digital audio I/O with read/write/set/get operations and multiple formats. * **Board Support** * LilyGO T-Deck device tree entry added to enable I2S peripheral pin configuration. * **Documentation** * New/updated bindings and descriptors for I2S, I2C, GPIO, and root nodes. * **Other** * Added GPIO "no pin" sentinel and exposed I2S controller API symbols.
This commit is contained in:
parent
9a672a30ff
commit
a1c835e073
@ -3,6 +3,7 @@
|
|||||||
#include <tactility/bindings/root.h>
|
#include <tactility/bindings/root.h>
|
||||||
#include <tactility/bindings/esp32_gpio.h>
|
#include <tactility/bindings/esp32_gpio.h>
|
||||||
#include <tactility/bindings/esp32_i2c.h>
|
#include <tactility/bindings/esp32_i2c.h>
|
||||||
|
#include <tactility/bindings/esp32_i2s.h>
|
||||||
|
|
||||||
/ {
|
/ {
|
||||||
compatible = "root";
|
compatible = "root";
|
||||||
@ -13,6 +14,16 @@
|
|||||||
gpio-count = <49>;
|
gpio-count = <49>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
i2s0 {
|
||||||
|
compatible = "espressif,esp32-i2s";
|
||||||
|
port = <I2S_NUM_0>;
|
||||||
|
pin-bclk = <7>;
|
||||||
|
pin-ws = <5>;
|
||||||
|
pin-data-out = <6>;
|
||||||
|
pin-data-in = <GPIO_PIN_NONE>;
|
||||||
|
pin-mclk = <GPIO_PIN_NONE>;
|
||||||
|
};
|
||||||
|
|
||||||
i2c_internal {
|
i2c_internal {
|
||||||
compatible = "espressif,esp32-i2c";
|
compatible = "espressif,esp32-i2c";
|
||||||
port = <I2C_NUM_0>;
|
port = <I2C_NUM_0>;
|
||||||
|
|||||||
@ -108,7 +108,7 @@ static error_t stop(Device* device) {
|
|||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
const struct DeviceType HAL_DEVICE_TYPE {0};
|
extern const struct DeviceType HAL_DEVICE_TYPE {0};
|
||||||
|
|
||||||
extern struct Module hal_device_module;
|
extern struct Module hal_device_module;
|
||||||
|
|
||||||
|
|||||||
33
Platforms/PlatformEsp32/Bindings/espressif,esp32-i2s.yaml
Normal file
33
Platforms/PlatformEsp32/Bindings/espressif,esp32-i2s.yaml
Normal file
@ -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: int
|
||||||
|
required: true
|
||||||
|
description: BCK pin
|
||||||
|
pin-ws:
|
||||||
|
type: int
|
||||||
|
required: true
|
||||||
|
description: WS pin
|
||||||
|
pin-data-out:
|
||||||
|
type: int
|
||||||
|
required: true
|
||||||
|
description: DATA OUT pin
|
||||||
|
pin-data-in:
|
||||||
|
type: int
|
||||||
|
required: true
|
||||||
|
description: DATA IN pin
|
||||||
|
pin-mclk:
|
||||||
|
type: int
|
||||||
|
required: true
|
||||||
|
description: MCLK pin
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <tactility/bindings/bindings.h>
|
||||||
|
#include <tactility/drivers/esp32_i2s.h>
|
||||||
|
#include <driver/i2s_common.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
DEFINE_DEVICETREE(esp32_i2s, struct Esp32I2sConfig)
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <tactility/drivers/i2s_controller.h>
|
||||||
|
#include <driver/i2s_common.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct Esp32I2sConfig {
|
||||||
|
i2s_port_t port;
|
||||||
|
int pin_bclk;
|
||||||
|
int pin_ws;
|
||||||
|
int pin_data_out;
|
||||||
|
int pin_data_in;
|
||||||
|
int pin_mclk;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
242
Platforms/PlatformEsp32/Source/drivers/esp32_i2s.cpp
Normal file
242
Platforms/PlatformEsp32/Source/drivers/esp32_i2s.cpp
Normal file
@ -0,0 +1,242 @@
|
|||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
#include <driver/i2s_std.h>
|
||||||
|
#include <driver/i2s_common.h>
|
||||||
|
|
||||||
|
#include <tactility/driver.h>
|
||||||
|
#include <tactility/drivers/i2s_controller.h>
|
||||||
|
#include <tactility/log.h>
|
||||||
|
|
||||||
|
#include <tactility/time.h>
|
||||||
|
#include <tactility/error_esp32.h>
|
||||||
|
#include <tactility/drivers/esp32_i2s.h>
|
||||||
|
|
||||||
|
#include <new>
|
||||||
|
|
||||||
|
#define TAG "esp32_i2s"
|
||||||
|
|
||||||
|
struct InternalData {
|
||||||
|
Mutex mutex { 0 };
|
||||||
|
i2s_chan_handle_t tx_handle = nullptr;
|
||||||
|
i2s_chan_handle_t rx_handle = nullptr;
|
||||||
|
I2sConfig config {};
|
||||||
|
bool config_set = false;
|
||||||
|
|
||||||
|
InternalData() {
|
||||||
|
mutex_construct(&mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
~InternalData() {
|
||||||
|
mutex_destruct(&mutex);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#define GET_CONFIG(device) ((Esp32I2sConfig*)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 error_t cleanup_channel_handles(InternalData* 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(const I2sConfig* config, const Esp32I2sConfig* dts_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
std_cfg->gpio_cfg = {
|
||||||
|
.mclk = (gpio_num_t)dts_config->pin_mclk,
|
||||||
|
.bclk = (gpio_num_t)dts_config->pin_bclk,
|
||||||
|
.ws = (gpio_num_t)dts_config->pin_ws,
|
||||||
|
.dout = (gpio_num_t)dts_config->pin_data_out,
|
||||||
|
.din = (gpio_num_t)dts_config->pin_data_in,
|
||||||
|
.invert_flags = {
|
||||||
|
.mclk_inv = false,
|
||||||
|
.bclk_inv = false,
|
||||||
|
.ws_inv = false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
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* driver_data = GET_DATA(device);
|
||||||
|
auto* dts_config = GET_CONFIG(device);
|
||||||
|
lock(driver_data);
|
||||||
|
|
||||||
|
cleanup_channel_handles(driver_data);
|
||||||
|
driver_data->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, &driver_data->tx_handle, &driver_data->rx_handle);
|
||||||
|
if (esp_error != ESP_OK) {
|
||||||
|
LOG_E(TAG, "Failed to create I2S channels: %s", esp_err_to_name(esp_error));
|
||||||
|
unlock(driver_data);
|
||||||
|
return ERROR_RESOURCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2s_std_config_t std_cfg;
|
||||||
|
get_esp32_std_config(config, dts_config, &std_cfg);
|
||||||
|
|
||||||
|
if (driver_data->tx_handle) {
|
||||||
|
esp_error = i2s_channel_init_std_mode(driver_data->tx_handle, &std_cfg);
|
||||||
|
if (esp_error == ESP_OK) esp_error = i2s_channel_enable(driver_data->tx_handle);
|
||||||
|
}
|
||||||
|
if (esp_error == ESP_OK && driver_data->rx_handle) {
|
||||||
|
esp_error = i2s_channel_init_std_mode(driver_data->rx_handle, &std_cfg);
|
||||||
|
if (esp_error == ESP_OK) esp_error = i2s_channel_enable(driver_data->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(driver_data);
|
||||||
|
unlock(driver_data);
|
||||||
|
return esp_err_to_error(esp_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update runtime config to reflect current state
|
||||||
|
memcpy(&driver_data->config, config, sizeof(I2sConfig));
|
||||||
|
driver_data->config_set = true;
|
||||||
|
|
||||||
|
unlock(driver_data);
|
||||||
|
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* data = new(std::nothrow) InternalData();
|
||||||
|
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, "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,
|
||||||
|
.driver_private = nullptr
|
||||||
|
};
|
||||||
|
|
||||||
|
} // extern "C"
|
||||||
@ -6,12 +6,14 @@ extern "C" {
|
|||||||
|
|
||||||
extern Driver esp32_gpio_driver;
|
extern Driver esp32_gpio_driver;
|
||||||
extern Driver esp32_i2c_driver;
|
extern Driver esp32_i2c_driver;
|
||||||
|
extern Driver esp32_i2s_driver;
|
||||||
|
|
||||||
static error_t start() {
|
static error_t start() {
|
||||||
/* We crash when construct fails, because if a single driver fails to construct,
|
/* 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 */
|
* 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_gpio_driver) == ERROR_NONE);
|
||||||
check(driver_construct_add(&esp32_i2c_driver) == ERROR_NONE);
|
check(driver_construct_add(&esp32_i2c_driver) == ERROR_NONE);
|
||||||
|
check(driver_construct_add(&esp32_i2s_driver) == ERROR_NONE);
|
||||||
return ERROR_NONE;
|
return ERROR_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,6 +22,7 @@ static error_t stop() {
|
|||||||
* there is no guarantee that the previously destroyed drivers can be recovered */
|
* 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_gpio_driver) == ERROR_NONE);
|
||||||
check(driver_remove_destruct(&esp32_i2c_driver) == ERROR_NONE);
|
check(driver_remove_destruct(&esp32_i2c_driver) == ERROR_NONE);
|
||||||
|
check(driver_remove_destruct(&esp32_i2s_driver) == ERROR_NONE);
|
||||||
return ERROR_NONE;
|
return ERROR_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
description: GPIO controller
|
||||||
properties:
|
properties:
|
||||||
gpio-count:
|
gpio-count:
|
||||||
type: int
|
type: int
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
bus: i2c
|
description: I2C controller
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
on-bus: i2c
|
description: I2C device
|
||||||
|
|
||||||
properties:
|
properties:
|
||||||
register:
|
register:
|
||||||
|
|||||||
1
TactilityKernel/Bindings/i2s-controller.yaml
Normal file
1
TactilityKernel/Bindings/i2s-controller.yaml
Normal file
@ -0,0 +1 @@
|
|||||||
|
description: I2S controller
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
description: Represents root node of a device tree. It holds all other nodes.
|
||||||
|
|
||||||
compatible: "root"
|
compatible: "root"
|
||||||
|
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
@ -26,6 +26,8 @@ extern "C" {
|
|||||||
#define GPIO_INTERRUPT_FROM_OPTIONS(options) (gpio_int_type_t)((options & GPIO_INTERRUPT_BITMASK) >> 5)
|
#define GPIO_INTERRUPT_FROM_OPTIONS(options) (gpio_int_type_t)((options & GPIO_INTERRUPT_BITMASK) >> 5)
|
||||||
#define GPIO_INTERRUPT_TO_OPTIONS(options, interrupt) (options | (interrupt << 5))
|
#define GPIO_INTERRUPT_TO_OPTIONS(options, interrupt) (options | (interrupt << 5))
|
||||||
|
|
||||||
|
#define GPIO_PIN_NONE -1
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
GPIO_INTERRUPT_DISABLE = 0,
|
GPIO_INTERRUPT_DISABLE = 0,
|
||||||
GPIO_INTERRUPT_POS_EDGE = 1,
|
GPIO_INTERRUPT_POS_EDGE = 1,
|
||||||
|
|||||||
141
TactilityKernel/Include/tactility/drivers/i2s_controller.h
Normal file
141
TactilityKernel/Include/tactility/drivers/i2s_controller.h
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "gpio.h"
|
||||||
|
|
||||||
|
#include <tactility/freertos/freertos.h>
|
||||||
|
#include <tactility/error.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief I2S communication format
|
||||||
|
*/
|
||||||
|
enum I2sCommunicationFormat {
|
||||||
|
I2S_FORMAT_STAND_I2S = 0x01,
|
||||||
|
I2S_FORMAT_STAND_MSB = 0x02,
|
||||||
|
I2S_FORMAT_STAND_PCM_SHORT = 0x04,
|
||||||
|
I2S_FORMAT_STAND_PCM_LONG = 0x08,
|
||||||
|
};
|
||||||
|
|
||||||
|
#define I2S_CHANNEL_NONE -1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief I2S Config
|
||||||
|
*/
|
||||||
|
struct I2sConfig {
|
||||||
|
enum I2sCommunicationFormat communication_format;
|
||||||
|
uint32_t sample_rate;
|
||||||
|
uint8_t bits_per_sample; // 16, 24, 32
|
||||||
|
int8_t channel_left; // I2S_CHANNEL_NONE to disable
|
||||||
|
int8_t channel_right; // I2S_CHANNEL_NONE to disable
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief API for I2S controller drivers.
|
||||||
|
*/
|
||||||
|
struct I2sControllerApi {
|
||||||
|
/**
|
||||||
|
* @brief Reads data from I2S.
|
||||||
|
* @param[in] device the I2S controller device
|
||||||
|
* @param[out] data the buffer to store the read data
|
||||||
|
* @param[in] data_size the number of bytes to read
|
||||||
|
* @param[out] bytes_read the number of bytes actually read
|
||||||
|
* @param[in] timeout the maximum time to wait for the operation to complete
|
||||||
|
* @retval ERROR_NONE when the read operation was successful
|
||||||
|
* @retval ERROR_TIMEOUT when the operation timed out
|
||||||
|
*/
|
||||||
|
error_t (*read)(struct Device* device, void* data, size_t data_size, size_t* bytes_read, TickType_t timeout);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Writes data to I2S.
|
||||||
|
* @param[in] device the I2S controller device
|
||||||
|
* @param[in] data the buffer containing the data to write
|
||||||
|
* @param[in] data_size the number of bytes to write
|
||||||
|
* @param[out] bytes_written the number of bytes actually written
|
||||||
|
* @param[in] timeout the maximum time to wait for the operation to complete
|
||||||
|
* @retval ERROR_NONE when the write operation was successful
|
||||||
|
* @retval ERROR_TIMEOUT when the operation timed out
|
||||||
|
*/
|
||||||
|
error_t (*write)(struct Device* device, const void* data, size_t data_size, size_t* bytes_written, TickType_t timeout);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets the I2S configuration.
|
||||||
|
* @param[in] device the I2S controller device
|
||||||
|
* @param[in] config the new configuration
|
||||||
|
* @retval ERROR_NONE when the operation was successful
|
||||||
|
*/
|
||||||
|
error_t (*set_config)(struct Device* device, const struct I2sConfig* config);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current I2S configuration.
|
||||||
|
* @param[in] device the I2S controller device
|
||||||
|
* @param[out] config the buffer to store the current configuration
|
||||||
|
* @retval ERROR_NONE when the operation was successful
|
||||||
|
*/
|
||||||
|
error_t (*get_config)(struct Device* device, struct I2sConfig* config);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Resets the I2S controller. Must configure it again before it can be used.
|
||||||
|
* @param[in] device the I2S controller device
|
||||||
|
* @retval ERROR_NONE when the operation was successful
|
||||||
|
*/
|
||||||
|
error_t (*reset)(struct Device* device);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reads data from I2S using the specified controller.
|
||||||
|
* @param[in] device the I2S controller device
|
||||||
|
* @param[out] data the buffer to store the read data
|
||||||
|
* @param[in] data_size the number of bytes to read
|
||||||
|
* @param[out] bytes_read the number of bytes actually read
|
||||||
|
* @param[in] timeout the maximum time to wait for the operation to complete
|
||||||
|
* @retval ERROR_NONE when the read operation was successful
|
||||||
|
*/
|
||||||
|
error_t i2s_controller_read(struct Device* device, void* data, size_t data_size, size_t* bytes_read, TickType_t timeout);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Writes data to I2S using the specified controller.
|
||||||
|
* @param[in] device the I2S controller device
|
||||||
|
* @param[in] data the buffer containing the data to write
|
||||||
|
* @param[in] data_size the number of bytes to write
|
||||||
|
* @param[out] bytes_written the number of bytes actually written
|
||||||
|
* @param[in] timeout the maximum time to wait for the operation to complete
|
||||||
|
* @retval ERROR_NONE when the write operation was successful
|
||||||
|
*/
|
||||||
|
error_t i2s_controller_write(struct Device* device, const void* data, size_t data_size, size_t* bytes_written, TickType_t timeout);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets the I2S configuration using the specified controller.
|
||||||
|
* @param[in] device the I2S controller device
|
||||||
|
* @param[in] config the new configuration
|
||||||
|
* @retval ERROR_NONE when the operation was successful
|
||||||
|
*/
|
||||||
|
error_t i2s_controller_set_config(struct Device* device, const struct I2sConfig* config);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current I2S configuration using the specified controller.
|
||||||
|
* @param[in] device the I2S controller device
|
||||||
|
* @param[out] config the buffer to store the current configuration
|
||||||
|
* @retval ERROR_NONE when the operation was successful
|
||||||
|
*/
|
||||||
|
error_t i2s_controller_get_config(struct Device* device, struct I2sConfig* config);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Resets the I2S controller. Must configure it again before it can be used.
|
||||||
|
* @param[in] device the I2S controller device
|
||||||
|
* @retval ERROR_NONE when the operation was successful
|
||||||
|
*/
|
||||||
|
error_t i2s_controller_reset(struct Device* device);
|
||||||
|
|
||||||
|
extern const struct DeviceType I2S_CONTROLLER_TYPE;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -32,6 +32,6 @@ error_t gpio_controller_get_pin_count(struct Device* device, uint32_t* count) {
|
|||||||
return GPIO_DRIVER_API(driver)->get_pin_count(device, count);
|
return GPIO_DRIVER_API(driver)->get_pin_count(device, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct DeviceType GPIO_CONTROLLER_TYPE { 0 };
|
extern const struct DeviceType GPIO_CONTROLLER_TYPE { 0 };
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -49,6 +49,6 @@ error_t i2c_controller_has_device_at_address(Device* device, uint8_t address, Ti
|
|||||||
return I2C_DRIVER_API(driver)->write(device, address, message, 2, timeout);
|
return I2C_DRIVER_API(driver)->write(device, address, message, 2, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct DeviceType I2C_CONTROLLER_TYPE { 0 };
|
extern const struct DeviceType I2C_CONTROLLER_TYPE { 0 };
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
37
TactilityKernel/Source/drivers/i2s_controller.cpp
Normal file
37
TactilityKernel/Source/drivers/i2s_controller.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
#include <tactility/drivers/i2s_controller.h>
|
||||||
|
#include <tactility/error.h>
|
||||||
|
#include <tactility/device.h>
|
||||||
|
|
||||||
|
#define I2S_DRIVER_API(driver) ((struct I2sControllerApi*)driver->api)
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
error_t i2s_controller_read(Device* device, void* data, size_t dataSize, size_t* bytesRead, TickType_t timeout) {
|
||||||
|
const auto* driver = device_get_driver(device);
|
||||||
|
return I2S_DRIVER_API(driver)->read(device, data, dataSize, bytesRead, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
error_t i2s_controller_write(Device* device, const void* data, size_t dataSize, size_t* bytesWritten, TickType_t timeout) {
|
||||||
|
const auto* driver = device_get_driver(device);
|
||||||
|
return I2S_DRIVER_API(driver)->write(device, data, dataSize, bytesWritten, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
error_t i2s_controller_set_config(Device* device, const struct I2sConfig* config) {
|
||||||
|
const auto* driver = device_get_driver(device);
|
||||||
|
return I2S_DRIVER_API(driver)->set_config(device, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
error_t i2s_controller_get_config(Device* device, struct I2sConfig* config) {
|
||||||
|
const auto* driver = device_get_driver(device);
|
||||||
|
return I2S_DRIVER_API(driver)->get_config(device, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
error_t i2s_controller_reset(struct Device* device) {
|
||||||
|
const auto* driver = device_get_driver(device);
|
||||||
|
return I2S_DRIVER_API(driver)->reset(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern const struct DeviceType I2S_CONTROLLER_TYPE { 0 };
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
#include <tactility/device.h>
|
#include <tactility/device.h>
|
||||||
#include <tactility/driver.h>
|
#include <tactility/driver.h>
|
||||||
#include <tactility/drivers/i2c_controller.h>
|
#include <tactility/drivers/i2c_controller.h>
|
||||||
|
#include <tactility/drivers/i2s_controller.h>
|
||||||
#include <tactility/drivers/gpio_controller.h>
|
#include <tactility/drivers/gpio_controller.h>
|
||||||
#include <tactility/concurrent/dispatcher.h>
|
#include <tactility/concurrent/dispatcher.h>
|
||||||
#include <tactility/concurrent/event_group.h>
|
#include <tactility/concurrent/event_group.h>
|
||||||
@ -58,6 +59,7 @@ const struct ModuleSymbol KERNEL_SYMBOLS[] = {
|
|||||||
DEFINE_MODULE_SYMBOL(gpio_controller_set_options),
|
DEFINE_MODULE_SYMBOL(gpio_controller_set_options),
|
||||||
DEFINE_MODULE_SYMBOL(gpio_controller_get_options),
|
DEFINE_MODULE_SYMBOL(gpio_controller_get_options),
|
||||||
DEFINE_MODULE_SYMBOL(gpio_controller_get_pin_count),
|
DEFINE_MODULE_SYMBOL(gpio_controller_get_pin_count),
|
||||||
|
DEFINE_MODULE_SYMBOL(GPIO_CONTROLLER_TYPE),
|
||||||
// drivers/i2c_controller
|
// drivers/i2c_controller
|
||||||
DEFINE_MODULE_SYMBOL(i2c_controller_read),
|
DEFINE_MODULE_SYMBOL(i2c_controller_read),
|
||||||
DEFINE_MODULE_SYMBOL(i2c_controller_write),
|
DEFINE_MODULE_SYMBOL(i2c_controller_write),
|
||||||
@ -66,6 +68,14 @@ const struct ModuleSymbol KERNEL_SYMBOLS[] = {
|
|||||||
DEFINE_MODULE_SYMBOL(i2c_controller_write_register),
|
DEFINE_MODULE_SYMBOL(i2c_controller_write_register),
|
||||||
DEFINE_MODULE_SYMBOL(i2c_controller_write_register_array),
|
DEFINE_MODULE_SYMBOL(i2c_controller_write_register_array),
|
||||||
DEFINE_MODULE_SYMBOL(i2c_controller_has_device_at_address),
|
DEFINE_MODULE_SYMBOL(i2c_controller_has_device_at_address),
|
||||||
|
DEFINE_MODULE_SYMBOL(I2C_CONTROLLER_TYPE),
|
||||||
|
// drivers/i2s_controller
|
||||||
|
DEFINE_MODULE_SYMBOL(i2s_controller_read),
|
||||||
|
DEFINE_MODULE_SYMBOL(i2s_controller_write),
|
||||||
|
DEFINE_MODULE_SYMBOL(i2s_controller_set_config),
|
||||||
|
DEFINE_MODULE_SYMBOL(i2s_controller_get_config),
|
||||||
|
DEFINE_MODULE_SYMBOL(i2s_controller_reset),
|
||||||
|
DEFINE_MODULE_SYMBOL(I2S_CONTROLLER_TYPE),
|
||||||
// concurrent/dispatcher
|
// concurrent/dispatcher
|
||||||
DEFINE_MODULE_SYMBOL(dispatcher_alloc),
|
DEFINE_MODULE_SYMBOL(dispatcher_alloc),
|
||||||
DEFINE_MODULE_SYMBOL(dispatcher_free),
|
DEFINE_MODULE_SYMBOL(dispatcher_free),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user