Skip to content

Commit

Permalink
Merge branch 'refactor/i2s_refactor_for_etm' into 'master'
Browse files Browse the repository at this point in the history
refactor(i2s): refactor to use i2s etm controlling

Closes IDF-10508

See merge request espressif/esp-idf!32315
  • Loading branch information
L-KAYA committed Aug 7, 2024
2 parents a547ebb + 1c52462 commit a500f3d
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 38 deletions.
16 changes: 12 additions & 4 deletions components/esp_driver_i2s/i2s_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ static void i2s_tx_channel_start(i2s_chan_handle_t handle)
i2s_hal_tx_enable_dma(&(handle->controller->hal));
i2s_hal_tx_start_link(&(handle->controller->hal), (uint32_t) handle->dma.desc[0]);
#endif
i2s_hal_tx_start(&(handle->controller->hal));
if (!handle->is_etm_start) {
i2s_hal_tx_start(&(handle->controller->hal));
}
}

static void i2s_rx_channel_start(i2s_chan_handle_t handle)
Expand All @@ -117,12 +119,16 @@ static void i2s_rx_channel_start(i2s_chan_handle_t handle)
i2s_hal_rx_enable_dma(&(handle->controller->hal));
i2s_hal_rx_start_link(&(handle->controller->hal), (uint32_t) handle->dma.desc[0]);
#endif
i2s_hal_rx_start(&(handle->controller->hal));
if (!handle->is_etm_start) {
i2s_hal_rx_start(&(handle->controller->hal));
}
}

static void i2s_tx_channel_stop(i2s_chan_handle_t handle)
{
i2s_hal_tx_stop(&(handle->controller->hal));
if (!handle->is_etm_stop) {
i2s_hal_tx_stop(&(handle->controller->hal));
}
#if SOC_GDMA_SUPPORTED
gdma_stop(handle->dma.dma_chan);
#else
Expand All @@ -135,7 +141,9 @@ static void i2s_tx_channel_stop(i2s_chan_handle_t handle)

static void i2s_rx_channel_stop(i2s_chan_handle_t handle)
{
i2s_hal_rx_stop(&(handle->controller->hal));
if (!handle->is_etm_stop) {
i2s_hal_rx_stop(&(handle->controller->hal));
}
#if SOC_GDMA_SUPPORTED
gdma_stop(handle->dma.dma_chan);
#else
Expand Down
46 changes: 37 additions & 9 deletions components/esp_driver_i2s/i2s_etm.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,29 @@

static const char *TAG = "i2s-etm";

static esp_err_t i2s_del_etm_event(esp_etm_event_t *event)
typedef struct {
esp_etm_task_t base; /*!< Base ETM task object */
i2s_chan_handle_t handle; /*!< I2S channel handle of this etm task */
i2s_etm_task_type_t task_type; /*!< I2S ETM task type */
} i2s_etm_task_t;

static esp_err_t s_i2s_del_etm_event(esp_etm_event_t *event)
{
free(event);
return ESP_OK;
}

static esp_err_t i2s_del_etm_task(esp_etm_task_t *task)
static esp_err_t s_i2s_del_etm_task(esp_etm_task_t *task)
{
free(task);
i2s_etm_task_t *i2s_task = __containerof(task, i2s_etm_task_t, base);
if (i2s_task->task_type == I2S_ETM_TASK_START) {
// The i2s start no longer be controlled by etm
i2s_task->handle->is_etm_start = false;
} else {
// The i2s stop no longer be controlled by etm
i2s_task->handle->is_etm_stop = false;
}
free(i2s_task);
return ESP_OK;
}

Expand All @@ -41,8 +55,11 @@ esp_err_t i2s_new_etm_event(i2s_chan_handle_t handle, const i2s_etm_event_config
esp_etm_event_t *event = heap_caps_calloc(1, sizeof(esp_etm_event_t), ETM_MEM_ALLOC_CAPS);
ESP_RETURN_ON_FALSE(event, ESP_ERR_NO_MEM, TAG, "no memory for ETM event");

// Get the event id from the I2S ETM event table
uint32_t event_id = I2S_LL_ETM_EVENT_TABLE(handle->controller->id, handle->dir, config->event_type);
// If the event type is threshold, set the threshold to the hardware
if (config->event_type == I2S_ETM_EVENT_REACH_THRESH) {
// Check if the threshold within the supported range
ESP_GOTO_ON_FALSE(config->threshold <= I2S_LL_ETM_MAX_THRESH_NUM, ESP_ERR_INVALID_ARG, err, TAG,
"exceed the max threshold %"PRIu32, (uint32_t)I2S_LL_ETM_MAX_THRESH_NUM);
if (handle->dir == I2S_DIR_TX) {
Expand All @@ -55,7 +72,7 @@ esp_err_t i2s_new_etm_event(i2s_chan_handle_t handle, const i2s_etm_event_config
// fill the ETM event object
event->event_id = event_id;
event->trig_periph = ETM_TRIG_PERIPH_I2S;
event->del = i2s_del_etm_event;
event->del = s_i2s_del_etm_event;
*out_event = event;
return ret;
err:
Expand All @@ -67,15 +84,26 @@ esp_err_t i2s_new_etm_task(i2s_chan_handle_t handle, const i2s_etm_task_config_t
{
ESP_RETURN_ON_FALSE(handle && config && out_task, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
ESP_RETURN_ON_FALSE(config->task_type < I2S_ETM_TASK_MAX, ESP_ERR_INVALID_ARG, TAG, "invalid task type");
esp_etm_task_t *task = heap_caps_calloc(1, sizeof(esp_etm_task_t), ETM_MEM_ALLOC_CAPS);
i2s_etm_task_t *task = heap_caps_calloc(1, sizeof(i2s_etm_task_t), ETM_MEM_ALLOC_CAPS);
ESP_RETURN_ON_FALSE(task, ESP_ERR_NO_MEM, TAG, "no memory for ETM task");

// Get the task id from the I2S ETM task table
uint32_t task_id = I2S_LL_ETM_TASK_TABLE(handle->controller->id, handle->dir, config->task_type);

// fill the ETM task object
task->task_id = task_id;
task->trig_periph = ETM_TRIG_PERIPH_I2S;
task->del = i2s_del_etm_task;
*out_task = task;
task->base.task_id = task_id;
task->base.trig_periph = ETM_TRIG_PERIPH_I2S;
task->base.del = s_i2s_del_etm_task;
task->handle = handle;
task->task_type = config->task_type;
if (config->task_type == I2S_ETM_TASK_START) {
// The i2s start will be controlled by etm
handle->is_etm_start = true;
} else {
// The i2s stop will be controlled by etm
handle->is_etm_stop = true;
}
*out_task = &(task->base);

return ESP_OK;
}
2 changes: 2 additions & 0 deletions components/esp_driver_i2s/i2s_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ struct i2s_channel_obj_t {
/* Stored configurations */
int intr_prio_flags;/*!< i2s interrupt priority flags */
void *mode_info; /*!< Slot, clock and gpio information of each mode */
bool is_etm_start; /*!< Whether start by etm tasks */
bool is_etm_stop; /*!< Whether stop by etm tasks */
#if SOC_I2S_SUPPORTS_APLL
bool apll_en; /*!< Flag of whether APLL enabled */
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ if(CONFIG_SOC_I2S_SUPPORTS_ETM AND CONFIG_SOC_GPIO_SUPPORT_ETM)
endif()

idf_component_register(SRCS ${srcs}
PRIV_REQUIRES unity esp_driver_pcnt driver spi_flash esp_driver_gpio
PRIV_REQUIRES unity esp_driver_pcnt spi_flash esp_driver_gpio esp_driver_i2s
WHOLE_ARCHIVE)
72 changes: 48 additions & 24 deletions components/esp_driver_i2s/test_apps/i2s/main/test_i2s_etm.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static void s_i2s_etm_check_status(void)
}
#endif // ETM_LL_SUPPORT_STATUS

static void s_i2s_init(uint8_t *buf)
static void s_i2s_init(void *buf)
{
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
chan_cfg.dma_desc_num = TEST_DESC_NUM;
Expand Down Expand Up @@ -92,9 +92,11 @@ static void s_gpio_init(void)

TEST_CASE("i2s_etm_event_test", "[etm]")
{
uint8_t *buf = calloc(1, TEST_BUFF_SIZE);
uint32_t *buf = calloc(1, TEST_BUFF_SIZE);
assert(buf);
memset(buf, 0x3C, TEST_BUFF_SIZE);
for (int i = 0; i < TEST_BUFF_SIZE / 4; i++) {
buf[i] = i;
}

/* I2S init */
s_i2s_init(buf);
Expand Down Expand Up @@ -153,9 +155,11 @@ TEST_CASE("i2s_etm_event_test", "[etm]")

TEST_CASE("i2s_etm_task_test", "[etm]")
{
uint8_t *buf = calloc(1, TEST_BUFF_SIZE);
uint32_t *buf = calloc(1, TEST_BUFF_SIZE);
assert(buf);
memset(buf, 0x3C, TEST_BUFF_SIZE);
for (int i = 0; i < TEST_BUFF_SIZE / 4; i++) {
buf[i] = i;
}

/* I2S init */
s_i2s_init(buf);
Expand All @@ -165,39 +169,55 @@ TEST_CASE("i2s_etm_task_test", "[etm]")

/* GPIO ETM event */
gpio_etm_event_config_t gpio_event_cfg = {
.edge = GPIO_ETM_EVENT_EDGE_POS,
.edges = {GPIO_ETM_EVENT_EDGE_POS, GPIO_ETM_EVENT_EDGE_NEG},
};
esp_etm_event_handle_t gpio_event_handle;
TEST_ESP_OK(gpio_new_etm_event(&gpio_event_cfg, &gpio_event_handle));
TEST_ESP_OK(gpio_etm_event_bind_gpio(gpio_event_handle, TEST_GPIO_ETM_NUM));
esp_etm_event_handle_t gpio_pos_event_handle;
esp_etm_event_handle_t gpio_neg_event_handle;
TEST_ESP_OK(gpio_new_etm_event(&gpio_event_cfg, &gpio_pos_event_handle, &gpio_neg_event_handle));
TEST_ESP_OK(gpio_etm_event_bind_gpio(gpio_pos_event_handle, TEST_GPIO_ETM_NUM));
TEST_ESP_OK(gpio_etm_event_bind_gpio(gpio_neg_event_handle, TEST_GPIO_ETM_NUM));

/* I2S Task init */
i2s_etm_task_config_t i2s_task_cfg = {
i2s_etm_task_config_t i2s_start_task_cfg = {
.task_type = I2S_ETM_TASK_START,
};
esp_etm_task_handle_t i2s_start_task_handle;
TEST_ESP_OK(i2s_new_etm_task(s_tx_handle, &i2s_start_task_cfg, &i2s_start_task_handle));
i2s_etm_task_config_t i2s_stop_task_cfg = {
.task_type = I2S_ETM_TASK_STOP,
};
esp_etm_task_handle_t i2s_task_handle;
TEST_ESP_OK(i2s_new_etm_task(s_tx_handle, &i2s_task_cfg, &i2s_task_handle));
esp_etm_task_handle_t i2s_stop_task_handle;
TEST_ESP_OK(i2s_new_etm_task(s_tx_handle, &i2s_stop_task_cfg, &i2s_stop_task_handle));

/* ETM connect */
esp_etm_channel_config_t etm_config = {};
esp_etm_channel_handle_t etm_channel = NULL;
TEST_ESP_OK(esp_etm_new_channel(&etm_config, &etm_channel));
TEST_ESP_OK(esp_etm_channel_connect(etm_channel, gpio_event_handle, i2s_task_handle));
TEST_ESP_OK(esp_etm_channel_enable(etm_channel));
esp_etm_channel_handle_t i2s_etm_start_chan = NULL;
esp_etm_channel_handle_t i2s_etm_stop_chan = NULL;
TEST_ESP_OK(esp_etm_new_channel(&etm_config, &i2s_etm_start_chan));
TEST_ESP_OK(esp_etm_new_channel(&etm_config, &i2s_etm_stop_chan));
TEST_ESP_OK(esp_etm_channel_connect(i2s_etm_start_chan, gpio_pos_event_handle, i2s_start_task_handle));
TEST_ESP_OK(esp_etm_channel_connect(i2s_etm_stop_chan, gpio_neg_event_handle, i2s_stop_task_handle));
TEST_ESP_OK(esp_etm_channel_enable(i2s_etm_start_chan));
TEST_ESP_OK(esp_etm_channel_enable(i2s_etm_stop_chan));
esp_etm_dump(stdout);

TEST_ESP_OK(i2s_channel_enable(s_tx_handle));
TEST_ESP_OK(i2s_channel_enable(s_rx_handle));

/* Test */
// receive normally
i2s_channel_read(s_rx_handle, buf, TEST_BUFF_SIZE, NULL, portMAX_DELAY);
// Set the GPIO to stop the I2S TX via ETM
// TX not started, read timeout
TEST_ESP_ERR(ESP_ERR_TIMEOUT, i2s_channel_read(s_rx_handle, buf, TEST_BUFF_SIZE, NULL, 100));
// start TX via GPIO pos event
TEST_ESP_OK(gpio_set_level(TEST_GPIO_ETM_NUM, 1));
// RX can receive data normally
TEST_ESP_OK(i2s_channel_read(s_rx_handle, buf, TEST_BUFF_SIZE, NULL, 100));
// Stop TX via GPIO neg event
TEST_ESP_OK(gpio_set_level(TEST_GPIO_ETM_NUM, 0));
// TX stopped, read will timeout when no legacy data in the queue
esp_err_t ret = ESP_OK;
// Receive will timeout after TX stopped
for (int i = 0; i < 20 && ret == ESP_OK; i++) {
ret = i2s_channel_read(s_rx_handle, buf, TEST_BUFF_SIZE, NULL, 1000);
ret = i2s_channel_read(s_rx_handle, buf, TEST_BUFF_SIZE, NULL, 100);
}
TEST_ESP_ERR(ESP_ERR_TIMEOUT, ret);

Expand All @@ -206,10 +226,14 @@ TEST_CASE("i2s_etm_task_test", "[etm]")
TEST_ESP_OK(i2s_channel_disable(s_tx_handle));
free(buf);

TEST_ESP_OK(esp_etm_channel_disable(etm_channel));
TEST_ESP_OK(esp_etm_del_event(gpio_event_handle));
TEST_ESP_OK(esp_etm_del_task(i2s_task_handle));
TEST_ESP_OK(esp_etm_del_channel(etm_channel));
TEST_ESP_OK(esp_etm_channel_disable(i2s_etm_start_chan));
TEST_ESP_OK(esp_etm_channel_disable(i2s_etm_stop_chan));
TEST_ESP_OK(esp_etm_del_event(gpio_pos_event_handle));
TEST_ESP_OK(esp_etm_del_event(gpio_neg_event_handle));
TEST_ESP_OK(esp_etm_del_task(i2s_start_task_handle));
TEST_ESP_OK(esp_etm_del_task(i2s_stop_task_handle));
TEST_ESP_OK(esp_etm_del_channel(i2s_etm_start_chan));
TEST_ESP_OK(esp_etm_del_channel(i2s_etm_stop_chan));

s_i2s_deinit();
}
2 changes: 2 additions & 0 deletions docs/en/api-reference/peripherals/etm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Other Peripheral Events
:SOC_MCPWM_SUPPORT_ETM: - Refer to :doc:`/api-reference/peripherals/mcpwm` for how to get the ETM event handle from MCPWM.
:SOC_ANA_CMPR_SUPPORT_ETM: - Refer to :doc:`/api-reference/peripherals/ana_cmpr` for how to get the ETM event handle from analog comparator.
:SOC_TEMPERATURE_SENSOR_SUPPORT_ETM: - Refer to :doc:`/api-reference/peripherals/temp_sensor` for how to get the ETM event handle from temperature sensor.
:SOC_I2S_SUPPORTS_ETM: - Refer to :doc:`/api-reference/peripherals/i2s` for how to get the ETM event handle from I2S.

.. _etm-task:

Expand All @@ -98,6 +99,7 @@ Other Peripheral Tasks

:SOC_TIMER_SUPPORT_ETM: - Refer to :doc:`GPTimer </api-reference/peripherals/gptimer>` for how to get the ETM task handle from GPTimer.
:SOC_TEMPERATURE_SENSOR_SUPPORT_ETM: - Refer to :doc:`/api-reference/peripherals/temp_sensor` for how to get the ETM task handle from temperature sensor.
:SOC_I2S_SUPPORTS_ETM: - Refer to :doc:`/api-reference/peripherals/i2s` for how to get the ETM task handle from I2S.

.. _etm-channel-control:

Expand Down
73 changes: 73 additions & 0 deletions docs/en/api-reference/peripherals/i2s.rst
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,79 @@ Here is an example of how to allocate a pair of full-duplex channels:
i2s_channel_init_std_mode(rx_handle, &std_rx_cfg);
i2s_channel_enable(rx_handle);
.. only:: SOC_I2S_SUPPORTS_ETM

I2S ETM Usage
^^^^^^^^^^^^^

{IDF_TARGET_NAME} supports I2S ETM (Event Task Matrix), which allows to trigger other ETM tasks via I2S ETM events, or to control the start/stop by I2S ETM tasks.

The I2S ETM APIs can be found in ``driver/i2s_etm.h``, the following example shows how to use GPIO to start/stop I2S channel via ETM:

.. code-block:: c
#include "driver/i2s_etm.h"
// ...
i2s_chan_handle_t tx_handle;
// Initialize I2S channel
// ......
int ctrl_gpio = 4;
// Initialize GPIO
// ......
/* Register GPIO ETM events */
gpio_etm_event_config_t gpio_event_cfg = {
.edges = {GPIO_ETM_EVENT_EDGE_POS, GPIO_ETM_EVENT_EDGE_NEG},
};
esp_etm_event_handle_t gpio_pos_event_handle;
esp_etm_event_handle_t gpio_neg_event_handle;
gpio_new_etm_event(&gpio_event_cfg, &gpio_pos_event_handle, &gpio_neg_event_handle);
gpio_etm_event_bind_gpio(gpio_pos_event_handle, ctrl_gpio);
gpio_etm_event_bind_gpio(gpio_neg_event_handle, ctrl_gpio);
/* Register I2S ETM tasks */
i2s_etm_task_config_t i2s_start_task_cfg = {
.task_type = I2S_ETM_TASK_START,
};
esp_etm_task_handle_t i2s_start_task_handle;
i2s_new_etm_task(tx_handle, &i2s_start_task_cfg, &i2s_start_task_handle);
i2s_etm_task_config_t i2s_stop_task_cfg = {
.task_type = I2S_ETM_TASK_STOP,
};
esp_etm_task_handle_t i2s_stop_task_handle;
i2s_new_etm_task(tx_handle, &i2s_stop_task_cfg, &i2s_stop_task_handle);
/* Bind GPIO events to I2S ETM tasks */
esp_etm_channel_config_t etm_config = {};
esp_etm_channel_handle_t i2s_etm_start_chan = NULL;
esp_etm_channel_handle_t i2s_etm_stop_chan = NULL;
esp_etm_new_channel(&etm_config, &i2s_etm_start_chan);
esp_etm_new_channel(&etm_config, &i2s_etm_stop_chan);
esp_etm_channel_connect(i2s_etm_start_chan, gpio_pos_event_handle, i2s_start_task_handle);
esp_etm_channel_connect(i2s_etm_stop_chan, gpio_neg_event_handle, i2s_stop_task_handle);
esp_etm_channel_enable(i2s_etm_start_chan);
esp_etm_channel_enable(i2s_etm_stop_chan);
/* Enable I2S channel first before starting I2S channel */
i2s_channel_enable(tx_handle);
// (Optional) Able to load the data into the internal DMA buffer here,
// but tx_channel does not start yet, will timeout when the internal buffer is full
// i2s_channel_write(tx_handle, data, data_size, NULL, 0);
/* Start I2S channel by setting the GPIO to high */
gpio_set_level(ctrl_gpio, 1);
// Write data ......
// i2s_channel_write(tx_handle, data, data_size, NULL, 1000);
/* Stop I2S channel by setting the GPIO to low */
gpio_set_level(ctrl_gpio, 0);
/* Free resources */
i2s_channel_disable(tx_handle);
esp_etm_channel_disable(i2s_etm_start_chan);
esp_etm_channel_disable(i2s_etm_stop_chan);
esp_etm_del_event(gpio_pos_event_handle);
esp_etm_del_event(gpio_neg_event_handle);
esp_etm_del_task(i2s_start_task_handle);
esp_etm_del_task(i2s_stop_task_handle);
esp_etm_del_channel(i2s_etm_start_chan);
esp_etm_del_channel(i2s_etm_stop_chan);
// De-initialize I2S and GPIO
// ......
Application Notes
-----------------
Expand Down
2 changes: 2 additions & 0 deletions docs/zh_CN/api-reference/peripherals/etm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ GPIO **边沿** 事件是最常见的事件类型,任何 GPIO 管脚均可触
:SOC_MCPWM_SUPPORT_ETM: - 要了解如何从 MCPWM 中获取 ETM 事件句柄,请参阅 :doc:`/api-reference/peripherals/mcpwm`。
:SOC_ANA_CMPR_SUPPORT_ETM: - 要了解如何从模拟比较器获取 ETM 事件句柄,请参阅 :doc:`/api-reference/peripherals/ana_cmpr`。
:SOC_TEMPERATURE_SENSOR_SUPPORT_ETM: - 要了解如何从温度传感器获取 ETM 事件句柄,请参阅 :doc:`/api-reference/peripherals/temp_sensor`。
:SOC_I2S_SUPPORTS_ETM: - 要了解如何从 I2S 获取 ETM 事件句柄,请参阅 :doc:`/api-reference/peripherals/i2s`。

.. _etm-task:

Expand All @@ -98,6 +99,7 @@ GPIO 任务是最常见的任务类型。一个 GPIO 可以采取一个或多个

:SOC_TIMER_SUPPORT_ETM: - 要了解如何从 GPTimer 获取 ETM 任务句柄,请参阅 :doc:`/api-reference/peripherals/gptimer`。
:SOC_TEMPERATURE_SENSOR_SUPPORT_ETM: - 要了解如何从温度传感器获取 ETM 任务句柄,请参阅 :doc:`/api-reference/peripherals/temp_sensor`。
:SOC_I2S_SUPPORTS_ETM: - 要了解如何从 I2S 获取 ETM 任务句柄,请参阅 :doc:`/api-reference/peripherals/i2s`。

.. _etm-channel-control:

Expand Down
Loading

0 comments on commit a500f3d

Please sign in to comment.