Skip to content

Commit 0cb4bdc

Browse files
committed
feat(i2s): support sleep retention
1 parent b835986 commit 0cb4bdc

File tree

33 files changed

+437
-23
lines changed

33 files changed

+437
-23
lines changed

components/esp_driver_i2s/i2s_common.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
#include "esp_private/i2s_platform.h"
4343
#include "esp_private/esp_clk.h"
44+
#include "esp_private/sleep_retention.h"
4445

4546
#include "driver/gpio.h"
4647
#include "esp_private/gpio.h"
@@ -85,6 +86,34 @@ inline void *i2s_dma_calloc(i2s_chan_handle_t handle, size_t num, size_t size)
8586
Scope: This file only
8687
----------------------------------------------------------------------------*/
8788

89+
#if I2S_USE_RETENTION_LINK
90+
static esp_err_t s_i2s_create_sleep_retention_link_cb(void *arg)
91+
{
92+
i2s_controller_t *i2s_obj = (i2s_controller_t *)arg;
93+
ESP_RETURN_ON_ERROR(sleep_retention_entries_create(i2s_reg_retention_info[i2s_obj->id].entry_array,
94+
i2s_reg_retention_info[i2s_obj->id].array_size,
95+
REGDMA_LINK_PRI_I2S, i2s_obj->slp_retention_mod),
96+
TAG, "create retention link failed");
97+
return ESP_OK;
98+
}
99+
100+
static void s_i2s_create_retention_module(i2s_controller_t *i2s_obj)
101+
{
102+
sleep_retention_module_t module = i2s_obj->slp_retention_mod;
103+
104+
_lock_acquire(&i2s_obj->mutex);
105+
if (i2s_obj->retention_link_created == false) {
106+
if (sleep_retention_module_allocate(module) != ESP_OK) {
107+
// even though the sleep retention module create failed, I2S driver should still work, so just warning here
108+
ESP_LOGW(TAG, "create retention module failed, power domain can't turn off");
109+
} else {
110+
i2s_obj->retention_link_created = true;
111+
}
112+
}
113+
_lock_release(&i2s_obj->mutex);
114+
}
115+
#endif // I2S_USE_RETENTION_LINK
116+
88117
static void i2s_tx_channel_start(i2s_chan_handle_t handle)
89118
{
90119
i2s_hal_tx_reset(&(handle->controller->hal));
@@ -175,6 +204,14 @@ static esp_err_t i2s_destroy_controller_obj(i2s_controller_t **i2s_obj)
175204
#if SOC_I2S_HW_VERSION_1
176205
i2s_ll_enable_dma((*i2s_obj)->hal.dev, false);
177206
#endif
207+
#if I2S_USE_RETENTION_LINK
208+
if ((*i2s_obj)->slp_retention_mod) {
209+
if ((*i2s_obj)->retention_link_created) {
210+
sleep_retention_module_free((*i2s_obj)->slp_retention_mod);
211+
}
212+
sleep_retention_module_deinit((*i2s_obj)->slp_retention_mod);
213+
}
214+
#endif // I2S_USE_RETENTION_LINK
178215
free(*i2s_obj);
179216
*i2s_obj = NULL;
180217
return i2s_platform_release_occupation(I2S_CTLR_HP, id);
@@ -219,6 +256,25 @@ static i2s_controller_t *i2s_acquire_controller_obj(int id)
219256
adc_ll_digi_set_data_source(0);
220257
}
221258
#endif
259+
260+
#if I2S_USE_RETENTION_LINK
261+
sleep_retention_module_t module = i2s_periph_signal[id].retention_module;
262+
sleep_retention_module_init_param_t init_param = {
263+
.cbs = {
264+
.create = {
265+
.handle = s_i2s_create_sleep_retention_link_cb,
266+
.arg = i2s_obj,
267+
},
268+
},
269+
.depends = BIT(SLEEP_RETENTION_MODULE_CLOCK_SYSTEM)
270+
};
271+
if (sleep_retention_module_init(module, &init_param) == ESP_OK) {
272+
i2s_obj->slp_retention_mod = module;
273+
} else {
274+
// even the sleep retention module init failed, I2S driver should still work, so just warning here
275+
ESP_LOGW(TAG, "init sleep retention failed for I2S%d, power domain may be turned off during sleep", id);
276+
}
277+
#endif // I2S_USE_RETENTION_LINK
222278
} else {
223279
free(pre_alloc);
224280
portENTER_CRITICAL(&g_i2s.spinlock);
@@ -879,6 +935,9 @@ esp_err_t i2s_new_channel(const i2s_chan_config_t *chan_cfg, i2s_chan_handle_t *
879935
ESP_RETURN_ON_FALSE(chan_cfg->id < SOC_I2S_NUM || chan_cfg->id == I2S_NUM_AUTO, ESP_ERR_INVALID_ARG, TAG, "invalid I2S port id");
880936
ESP_RETURN_ON_FALSE(chan_cfg->dma_desc_num >= 2, ESP_ERR_INVALID_ARG, TAG, "there should be at least 2 DMA buffers");
881937
ESP_RETURN_ON_FALSE(chan_cfg->intr_priority >= 0 && chan_cfg->intr_priority <= 7, ESP_ERR_INVALID_ARG, TAG, "intr_priority should be within 0~7");
938+
#if !SOC_I2S_SUPPORT_SLEEP_RETENTION
939+
ESP_RETURN_ON_FALSE(!chan_cfg->backup_before_sleep, ESP_ERR_NOT_SUPPORTED, TAG, "register back up is not supported");
940+
#endif
882941

883942
esp_err_t ret = ESP_OK;
884943
i2s_controller_t *i2s_obj = NULL;
@@ -937,6 +996,11 @@ esp_err_t i2s_new_channel(const i2s_chan_config_t *chan_cfg, i2s_chan_handle_t *
937996
if ((tx_handle != NULL) && (rx_handle != NULL)) {
938997
i2s_obj->full_duplex = true;
939998
}
999+
#if I2S_USE_RETENTION_LINK
1000+
if (chan_cfg->backup_before_sleep) {
1001+
s_i2s_create_retention_module(i2s_obj);
1002+
}
1003+
#endif
9401004

9411005
return ESP_OK;
9421006
/* i2s_obj allocated but register channel failed */

components/esp_driver_i2s/i2s_private.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#pragma once
88

9+
#include <sys/lock.h>
910
#include "freertos/FreeRTOS.h"
1011
#include "freertos/semphr.h"
1112
#include "freertos/queue.h"
@@ -25,6 +26,7 @@
2526
#endif
2627
#include "esp_private/periph_ctrl.h"
2728
#include "esp_private/esp_gpio_reserve.h"
29+
#include "esp_private/sleep_retention.h"
2830
#include "esp_pm.h"
2931
#include "esp_err.h"
3032
#include "sdkconfig.h"
@@ -56,6 +58,8 @@ extern "C" {
5658
#define I2S_RCC_ATOMIC()
5759
#endif
5860

61+
#define I2S_USE_RETENTION_LINK (SOC_I2S_SUPPORT_SLEEP_RETENTION && CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP)
62+
5963
#define I2S_NULL_POINTER_CHECK(tag, p) ESP_RETURN_ON_FALSE((p), ESP_ERR_INVALID_ARG, tag, "input parameter '"#p"' is NULL")
6064

6165
/**
@@ -130,6 +134,9 @@ typedef struct {
130134
bool full_duplex; /*!< is full_duplex */
131135
i2s_chan_handle_t tx_chan; /*!< tx channel handler */
132136
i2s_chan_handle_t rx_chan; /*!< rx channel handler */
137+
_lock_t mutex; /*!< mutex for controller */
138+
sleep_retention_module_t slp_retention_mod; /*!< Sleep retention module */
139+
bool retention_link_created; /*!< Whether the retention link is created */
133140
int mclk; /*!< MCK out pin, shared by tx/rx*/
134141
#if CONFIG_IDF_TARGET_ESP32
135142
esp_clock_output_mapping_handle_t mclk_out_hdl; /*!< The handle of MCLK output signal */

components/esp_driver_i2s/include/driver/i2s_common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern "C" {
2626
.dma_frame_num = 240, \
2727
.auto_clear_after_cb = false, \
2828
.auto_clear_before_cb = false, \
29+
.backup_before_sleep = false, \
2930
.intr_priority = 0, \
3031
}
3132

@@ -73,6 +74,9 @@ typedef struct {
7374
bool auto_clear_before_cb; /*!< Set to auto clear DMA TX buffer before `on_sent` callback, I2S will always send zero automatically if no data to send
7475
* So that user can access data in the callback that just finished to send.
7576
*/
77+
bool backup_before_sleep; /*!< If set, the driver will backup/restore the I2S registers before/after entering/exist sleep mode.
78+
By this approach, the system can power off I2S's power domain.
79+
This can save power, but at the expense of more RAM being consumed */
7680
int intr_priority; /*!< I2S interrupt priority, range [0, 7], if set to 0, the driver will try to allocate an interrupt with a relative low priority (1,2,3) */
7781
} i2s_chan_config_t;
7882

components/esp_driver_i2s/test_apps/i2s/main/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ if(CONFIG_SOC_I2S_SUPPORTS_ETM AND CONFIG_SOC_GPIO_SUPPORT_ETM)
66
set(srcs ${srcs} "test_i2s_etm.c")
77
endif()
88

9+
if(CONFIG_SOC_I2S_SUPPORT_SLEEP_RETENTION)
10+
list(APPEND srcs "test_i2s_slp_retention.c")
11+
endif()
12+
913
idf_component_register(SRCS ${srcs}
10-
PRIV_REQUIRES unity esp_driver_pcnt spi_flash esp_driver_gpio esp_driver_i2s
14+
PRIV_REQUIRES unity esp_driver_pcnt spi_flash esp_driver_gpio esp_driver_i2s esp_driver_uart
1115
WHOLE_ARCHIVE)

components/esp_driver_i2s/test_apps/i2s/main/test_i2s.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static void i2s_test_io_config(int mode)
9494
}
9595
}
9696

97-
static void i2s_read_write_test(i2s_chan_handle_t tx_chan, i2s_chan_handle_t rx_chan)
97+
void i2s_read_write_test(i2s_chan_handle_t tx_chan, i2s_chan_handle_t rx_chan)
9898
{
9999
#define I2S_SEND_BUF_LEN 100
100100
#define I2S_RECV_BUF_LEN 10000
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include "sdkconfig.h"
9+
#include "freertos/FreeRTOS.h"
10+
#include "freertos/task.h"
11+
#include "unity.h"
12+
#include "unity_test_utils.h"
13+
#include "driver/i2s_std.h"
14+
#include "driver/uart.h"
15+
#include "soc/i2s_struct.h"
16+
#include "esp_sleep.h"
17+
#include "esp_private/sleep_cpu.h"
18+
#include "esp_private/esp_sleep_internal.h"
19+
#include "esp_private/esp_pmu.h"
20+
#include "../../test_inc/test_i2s.h"
21+
22+
#if SOC_I2S_SUPPORT_SLEEP_RETENTION && CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP
23+
24+
extern void i2s_read_write_test(i2s_chan_handle_t tx_chan, i2s_chan_handle_t rx_chan);
25+
26+
static void test_i2s_enter_light_sleep(int sec)
27+
{
28+
esp_sleep_context_t sleep_ctx;
29+
esp_sleep_set_sleep_context(&sleep_ctx);
30+
printf("Entering light sleep for %d seconds\n", sec);
31+
#if ESP_SLEEP_POWER_DOWN_CPU
32+
printf("Enable CPU power down\n");
33+
TEST_ESP_OK(sleep_cpu_configure(true));
34+
#endif
35+
uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
36+
TEST_ESP_OK(esp_sleep_enable_timer_wakeup(sec * 1000 * 1000));
37+
TEST_ESP_OK(esp_light_sleep_start());
38+
39+
#if ESP_SLEEP_POWER_DOWN_CPU
40+
TEST_ESP_OK(sleep_cpu_configure(false));
41+
#endif
42+
printf("Woke up from light sleep\n");
43+
44+
TEST_ASSERT_EQUAL(0, sleep_ctx.sleep_request_result);
45+
TEST_ASSERT_EQUAL(PMU_SLEEP_PD_TOP, sleep_ctx.sleep_flags & PMU_SLEEP_PD_TOP);
46+
esp_sleep_set_sleep_context(NULL);
47+
}
48+
49+
TEST_CASE("I2S_sleep_retention_test", "[i2s]")
50+
{
51+
i2s_chan_handle_t tx_handle;
52+
i2s_chan_handle_t rx_handle;
53+
54+
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_0, I2S_ROLE_MASTER);
55+
chan_cfg.backup_before_sleep = true;
56+
i2s_std_config_t std_cfg = {
57+
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(SAMPLE_RATE),
58+
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(SAMPLE_BITS, I2S_SLOT_MODE_STEREO),
59+
.gpio_cfg = I2S_TEST_MASTER_DEFAULT_PIN,
60+
};
61+
std_cfg.gpio_cfg.din = std_cfg.gpio_cfg.dout;
62+
TEST_ESP_OK(i2s_new_channel(&chan_cfg, &tx_handle, &rx_handle));
63+
TEST_ESP_OK(i2s_channel_init_std_mode(tx_handle, &std_cfg));
64+
TEST_ESP_OK(i2s_channel_init_std_mode(rx_handle, &std_cfg));
65+
66+
/* I2S retention is depended on GDMA retention.
67+
* Only take two registers as sample to check the I2S retention when GDMA retention has not been supported. */
68+
#if !SOC_GDMA_SUPPORT_SLEEP_RETENTION
69+
i2s_tx_conf_reg_t tx_reg_before_slp = I2S0.tx_conf;
70+
i2s_rx_conf_reg_t rx_reg_before_slp = I2S0.rx_conf;
71+
#endif
72+
73+
/* Enter light sleep and wake up after 1 second */
74+
test_i2s_enter_light_sleep(1);
75+
76+
#if SOC_GDMA_SUPPORT_SLEEP_RETENTION
77+
/* Check whether I2S can work correctly after light sleep */
78+
TEST_ESP_OK(i2s_channel_enable(tx_handle));
79+
TEST_ESP_OK(i2s_channel_enable(rx_handle));
80+
i2s_read_write_test(tx_handle, rx_handle);
81+
#else
82+
/* Only check whether the register values are restored if GDMA retention has not been supported */
83+
i2s_tx_conf_reg_t tx_reg_after_slp = I2S0.tx_conf;
84+
i2s_rx_conf_reg_t rx_reg_after_slp = I2S0.rx_conf;
85+
86+
TEST_ASSERT_EQUAL_UINT32(tx_reg_before_slp.val, tx_reg_after_slp.val);
87+
TEST_ASSERT_EQUAL_UINT32(rx_reg_before_slp.val, rx_reg_after_slp.val);
88+
89+
TEST_ESP_OK(i2s_channel_enable(tx_handle));
90+
TEST_ESP_OK(i2s_channel_enable(rx_handle));
91+
#endif
92+
93+
printf("I2S works as expected after light sleep\n");
94+
95+
TEST_ESP_OK(i2s_channel_disable(tx_handle));
96+
TEST_ESP_OK(i2s_channel_disable(rx_handle));
97+
TEST_ESP_OK(i2s_del_channel(tx_handle));
98+
TEST_ESP_OK(i2s_del_channel(rx_handle));
99+
}
100+
101+
#endif // SOC_I2S_SUPPORT_SLEEP_RETENTION && CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP

components/esp_driver_i2s/test_apps/i2s/pytest_i2s.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
1+
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
22
# SPDX-License-Identifier: Apache-2.0
33
import pytest
44
from pytest_embedded import Dut

components/esp_driver_i2s/test_apps/i2s/sdkconfig.ci.release

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ CONFIG_FREERTOS_USE_TICKLESS_IDLE=y
33
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
44
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
55
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
6+
CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP=y
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
CONFIG_I2S_ENABLE_DEBUG_LOG=y
22
CONFIG_ESP_TASK_WDT_EN=n
3+
# primitives for checking sleep internal state
4+
CONFIG_ESP_SLEEP_DEBUG=y

components/soc/esp32/i2s_periph.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const i2s_signal_conn_t i2s_periph_signal[SOC_I2S_NUM] = {
2929
.data_in_sig = I2S0I_DATA_IN15_IDX,
3030

3131
.irq = ETS_I2S0_INTR_SOURCE,
32-
.module = PERIPH_I2S0_MODULE,
3332
},
3433
{
3534
.mck_out_sig = -1, // Unavailable
@@ -49,6 +48,5 @@ const i2s_signal_conn_t i2s_periph_signal[SOC_I2S_NUM] = {
4948
.data_in_sig = I2S1I_DATA_IN15_IDX,
5049

5150
.irq = ETS_I2S1_INTR_SOURCE,
52-
.module = PERIPH_I2S1_MODULE,
5351
}
5452
};

0 commit comments

Comments
 (0)