i2s_std.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/semphr.h"
  9. #include "sdkconfig.h"
  10. #if CONFIG_I2S_ENABLE_DEBUG_LOG
  11. // The local log level must be defined before including esp_log.h
  12. // Set the maximum log level for this source file
  13. #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  14. #endif
  15. #include "hal/i2s_hal.h"
  16. #include "driver/gpio.h"
  17. #include "driver/i2s_std.h"
  18. #include "i2s_private.h"
  19. #include "clk_ctrl_os.h"
  20. #include "esp_intr_alloc.h"
  21. #include "esp_check.h"
  22. const static char *TAG = "i2s_std";
  23. static esp_err_t i2s_std_calculate_clock(i2s_chan_handle_t handle, const i2s_std_clk_config_t *clk_cfg, i2s_hal_clock_info_t *clk_info)
  24. {
  25. uint32_t rate = clk_cfg->sample_rate_hz;
  26. i2s_std_slot_config_t *slot_cfg = &((i2s_std_config_t *)(handle->mode_info))->slot_cfg;
  27. uint32_t slot_bits = (slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO) ||
  28. ((int)slot_cfg->slot_bit_width < (int)slot_cfg->data_bit_width) ?
  29. slot_cfg->data_bit_width : slot_cfg->slot_bit_width;
  30. /* Calculate multiple
  31. * Fmclk = bck_div*fbck = fsclk/(mclk_div+b/a) */
  32. if (handle->role == I2S_ROLE_MASTER) {
  33. clk_info->bclk = rate * handle->total_slot * slot_bits;
  34. clk_info->mclk = rate * clk_cfg->mclk_multiple;
  35. clk_info->bclk_div = clk_info->mclk / clk_info->bclk;
  36. } else {
  37. /* For slave mode, mclk >= bclk * 8, so fix bclk_div to 2 first */
  38. clk_info->bclk_div = 8;
  39. clk_info->bclk = rate * handle->total_slot * slot_bits;
  40. clk_info->mclk = clk_info->bclk * clk_info->bclk_div;
  41. }
  42. clk_info->sclk = i2s_get_source_clk_freq(clk_cfg->clk_src, clk_info->mclk);
  43. clk_info->mclk_div = clk_info->sclk / clk_info->mclk;
  44. /* Check if the configuration is correct */
  45. ESP_RETURN_ON_FALSE(clk_info->mclk_div, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large");
  46. return ESP_OK;
  47. }
  48. static esp_err_t i2s_std_set_clock(i2s_chan_handle_t handle, const i2s_std_clk_config_t *clk_cfg)
  49. {
  50. esp_err_t ret = ESP_OK;
  51. i2s_std_config_t *std_cfg = (i2s_std_config_t *)(handle->mode_info);
  52. ESP_RETURN_ON_FALSE(std_cfg->slot_cfg.data_bit_width != I2S_DATA_BIT_WIDTH_24BIT ||
  53. (clk_cfg->mclk_multiple % 3 == 0), ESP_ERR_INVALID_ARG, TAG,
  54. "The 'mclk_multiple' should be the multiple of 3 while using 24-bit data width");
  55. i2s_hal_clock_info_t clk_info;
  56. /* Calculate clock parameters */
  57. ESP_RETURN_ON_ERROR(i2s_std_calculate_clock(handle, clk_cfg, &clk_info), TAG, "clock calculate failed");
  58. ESP_LOGD(TAG, "Clock division info: [sclk] %"PRIu32" Hz [mdiv] %d [mclk] %"PRIu32" Hz [bdiv] %d [bclk] %"PRIu32" Hz",
  59. clk_info.sclk, clk_info.mclk_div, clk_info.mclk, clk_info.bclk_div, clk_info.bclk);
  60. portENTER_CRITICAL(&g_i2s.spinlock);
  61. /* Set clock configurations in HAL*/
  62. if (handle->dir == I2S_DIR_TX) {
  63. i2s_hal_set_tx_clock(&handle->controller->hal, &clk_info, clk_cfg->clk_src);
  64. } else {
  65. i2s_hal_set_rx_clock(&handle->controller->hal, &clk_info, clk_cfg->clk_src);
  66. }
  67. portEXIT_CRITICAL(&g_i2s.spinlock);
  68. /* Update the mode info: clock configuration */
  69. memcpy(&(std_cfg->clk_cfg), clk_cfg, sizeof(i2s_std_clk_config_t));
  70. return ret;
  71. }
  72. static esp_err_t i2s_std_set_slot(i2s_chan_handle_t handle, const i2s_std_slot_config_t *slot_cfg)
  73. {
  74. /* Update the total slot num and active slot num */
  75. handle->total_slot = 2;
  76. handle->active_slot = slot_cfg->slot_mode == I2S_SLOT_MODE_MONO ? 1 : 2;
  77. uint32_t buf_size = i2s_get_buf_size(handle, slot_cfg->data_bit_width, handle->dma.frame_num);
  78. /* The DMA buffer need to re-allocate if the buffer size changed */
  79. if (handle->dma.buf_size != buf_size) {
  80. handle->dma.buf_size = buf_size;
  81. ESP_RETURN_ON_ERROR(i2s_free_dma_desc(handle), TAG, "failed to free the old dma descriptor");
  82. ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, handle->dma.desc_num, buf_size),
  83. TAG, "allocate memory for dma descriptor failed");
  84. }
  85. bool is_slave = handle->role == I2S_ROLE_SLAVE;
  86. /* Share bck and ws signal in full-duplex mode */
  87. if (handle->controller->full_duplex) {
  88. i2s_ll_share_bck_ws(handle->controller->hal.dev, true);
  89. /* Since bck and ws are shared, only tx or rx can be master
  90. Force to set rx as slave to avoid conflict of clock signal */
  91. if (handle->dir == I2S_DIR_RX) {
  92. is_slave = true;
  93. }
  94. } else {
  95. i2s_ll_share_bck_ws(handle->controller->hal.dev, false);
  96. }
  97. portENTER_CRITICAL(&g_i2s.spinlock);
  98. /* Configure the hardware to apply STD format */
  99. if (handle->dir == I2S_DIR_TX) {
  100. i2s_hal_std_set_tx_slot(&(handle->controller->hal), is_slave, (i2s_hal_slot_config_t *)slot_cfg);
  101. } else {
  102. i2s_hal_std_set_rx_slot(&(handle->controller->hal), is_slave, (i2s_hal_slot_config_t *)slot_cfg);
  103. }
  104. portEXIT_CRITICAL(&g_i2s.spinlock);
  105. /* Update the mode info: slot configuration */
  106. i2s_std_config_t *std_cfg = (i2s_std_config_t *)(handle->mode_info);
  107. memcpy(&(std_cfg->slot_cfg), slot_cfg, sizeof(i2s_std_slot_config_t));
  108. return ESP_OK;
  109. }
  110. static esp_err_t i2s_std_set_gpio(i2s_chan_handle_t handle, const i2s_std_gpio_config_t *gpio_cfg)
  111. {
  112. int id = handle->controller->id;
  113. /* Check validity of selected pins */
  114. ESP_RETURN_ON_FALSE((gpio_cfg->bclk == -1 || GPIO_IS_VALID_GPIO(gpio_cfg->bclk)),
  115. ESP_ERR_INVALID_ARG, TAG, "bclk invalid");
  116. ESP_RETURN_ON_FALSE((gpio_cfg->ws == -1 || GPIO_IS_VALID_GPIO(gpio_cfg->ws)),
  117. ESP_ERR_INVALID_ARG, TAG, "ws invalid");
  118. i2s_std_config_t *std_cfg = (i2s_std_config_t *)(handle->mode_info);
  119. /* Loopback if dout = din */
  120. if (gpio_cfg->dout != -1 &&
  121. gpio_cfg->dout == gpio_cfg->din) {
  122. i2s_gpio_loopback_set(gpio_cfg->dout, i2s_periph_signal[id].data_out_sig, i2s_periph_signal[id].data_in_sig);
  123. } else if (handle->dir == I2S_DIR_TX) {
  124. /* Set data output GPIO */
  125. i2s_gpio_check_and_set(gpio_cfg->dout, i2s_periph_signal[id].data_out_sig, false, false);
  126. } else {
  127. /* Set data input GPIO */
  128. i2s_gpio_check_and_set(gpio_cfg->din, i2s_periph_signal[id].data_in_sig, true, false);
  129. }
  130. if (handle->role == I2S_ROLE_SLAVE) {
  131. /* For "tx + slave" mode, select TX signal index for ws and bck */
  132. if (handle->dir == I2S_DIR_TX && !handle->controller->full_duplex) {
  133. #if SOC_I2S_HW_VERSION_2
  134. i2s_ll_mclk_bind_to_tx_clk(handle->controller->hal.dev);
  135. #endif
  136. i2s_gpio_check_and_set(gpio_cfg->ws, i2s_periph_signal[id].s_tx_ws_sig, true, gpio_cfg->invert_flags.ws_inv);
  137. i2s_gpio_check_and_set(gpio_cfg->bclk, i2s_periph_signal[id].s_tx_bck_sig, true, gpio_cfg->invert_flags.bclk_inv);
  138. /* For "tx + rx + slave" or "rx + slave" mode, select RX signal index for ws and bck */
  139. } else {
  140. i2s_gpio_check_and_set(gpio_cfg->ws, i2s_periph_signal[id].s_rx_ws_sig, true, gpio_cfg->invert_flags.ws_inv);
  141. i2s_gpio_check_and_set(gpio_cfg->bclk, i2s_periph_signal[id].s_rx_bck_sig, true, gpio_cfg->invert_flags.bclk_inv);
  142. }
  143. } else {
  144. /* mclk only available in master mode */
  145. #if SOC_I2S_SUPPORTS_APLL
  146. bool is_apll = std_cfg->clk_cfg.clk_src == I2S_CLK_SRC_APLL;
  147. #else
  148. bool is_apll = false;
  149. #endif
  150. ESP_RETURN_ON_ERROR(i2s_check_set_mclk(id, gpio_cfg->mclk, is_apll, gpio_cfg->invert_flags.mclk_inv), TAG, "mclk config failed");
  151. /* For "rx + master" mode, select RX signal index for ws and bck */
  152. if (handle->dir == I2S_DIR_RX && !handle->controller->full_duplex) {
  153. #if SOC_I2S_HW_VERSION_2
  154. i2s_ll_mclk_bind_to_rx_clk(handle->controller->hal.dev);
  155. #endif
  156. i2s_gpio_check_and_set(gpio_cfg->ws, i2s_periph_signal[id].m_rx_ws_sig, false, gpio_cfg->invert_flags.ws_inv);
  157. i2s_gpio_check_and_set(gpio_cfg->bclk, i2s_periph_signal[id].m_rx_bck_sig, false, gpio_cfg->invert_flags.bclk_inv);
  158. /* For "tx + rx + master" or "tx + master" mode, select TX signal index for ws and bck */
  159. } else {
  160. i2s_gpio_check_and_set(gpio_cfg->ws, i2s_periph_signal[id].m_tx_ws_sig, false, gpio_cfg->invert_flags.ws_inv);
  161. i2s_gpio_check_and_set(gpio_cfg->bclk, i2s_periph_signal[id].m_tx_bck_sig, false, gpio_cfg->invert_flags.bclk_inv);
  162. }
  163. }
  164. /* Update the mode info: gpio configuration */
  165. memcpy(&(std_cfg->gpio_cfg), gpio_cfg, sizeof(i2s_std_gpio_config_t));
  166. return ESP_OK;
  167. }
  168. esp_err_t i2s_channel_init_std_mode(i2s_chan_handle_t handle, const i2s_std_config_t *std_cfg)
  169. {
  170. #if CONFIG_I2S_ENABLE_DEBUG_LOG
  171. esp_log_level_set(TAG, ESP_LOG_DEBUG);
  172. #endif
  173. I2S_NULL_POINTER_CHECK(TAG, handle);
  174. esp_err_t ret = ESP_OK;
  175. xSemaphoreTake(handle->mutex, portMAX_DELAY);
  176. handle->mode = I2S_COMM_MODE_STD;
  177. /* Allocate memory for storing the configurations of standard mode */
  178. if (handle->mode_info) {
  179. free(handle->mode_info);
  180. }
  181. handle->mode_info = calloc(1, sizeof(i2s_std_config_t));
  182. ESP_GOTO_ON_FALSE(handle->mode_info, ESP_ERR_NO_MEM, err, TAG, "no memory for storing the configurations");
  183. ESP_GOTO_ON_FALSE(handle->state == I2S_CHAN_STATE_REGISTER, ESP_ERR_INVALID_STATE, err, TAG, "the channel has initialized already");
  184. ESP_GOTO_ON_ERROR(i2s_std_set_gpio(handle, &std_cfg->gpio_cfg), err, TAG, "initialize channel failed while setting gpio pins");
  185. /* i2s_set_std_slot should be called before i2s_set_std_clock while initializing, because clock is relay on the slot */
  186. ESP_GOTO_ON_ERROR(i2s_std_set_slot(handle, &std_cfg->slot_cfg), err, TAG, "initialize channel failed while setting slot");
  187. #if SOC_I2S_SUPPORTS_APLL
  188. /* Enable APLL and acquire its lock when the clock source is APLL */
  189. if (std_cfg->clk_cfg.clk_src == I2S_CLK_SRC_APLL) {
  190. periph_rtc_apll_acquire();
  191. handle->apll_en = true;
  192. }
  193. #endif
  194. ESP_GOTO_ON_ERROR(i2s_std_set_clock(handle, &std_cfg->clk_cfg), err, TAG, "initialize channel failed while setting clock");
  195. ESP_GOTO_ON_ERROR(i2s_init_dma_intr(handle, I2S_INTR_ALLOC_FLAGS), err, TAG, "initialize dma interrupt failed");
  196. #if SOC_I2S_HW_VERSION_2
  197. /* Enable clock to start outputting mclk signal. Some codecs will reset once mclk stop */
  198. if (handle->dir == I2S_DIR_TX) {
  199. i2s_ll_tx_enable_std(handle->controller->hal.dev);
  200. i2s_ll_tx_enable_clock(handle->controller->hal.dev);
  201. } else {
  202. i2s_ll_rx_enable_std(handle->controller->hal.dev);
  203. i2s_ll_rx_enable_clock(handle->controller->hal.dev);
  204. }
  205. #endif
  206. #ifdef CONFIG_PM_ENABLE
  207. esp_pm_lock_type_t pm_type = ESP_PM_APB_FREQ_MAX;
  208. #if SOC_I2S_SUPPORTS_APLL
  209. if (std_cfg->clk_cfg.clk_src == I2S_CLK_SRC_APLL) {
  210. pm_type = ESP_PM_NO_LIGHT_SLEEP;
  211. }
  212. #endif // SOC_I2S_SUPPORTS_APLL
  213. ESP_RETURN_ON_ERROR(esp_pm_lock_create(pm_type, 0, "i2s_driver", &handle->pm_lock), TAG, "I2S pm lock create failed");
  214. #endif
  215. /* Initialization finished, mark state as ready */
  216. handle->state = I2S_CHAN_STATE_READY;
  217. xSemaphoreGive(handle->mutex);
  218. ESP_LOGD(TAG, "The %s channel on I2S%d has been initialized to STD mode successfully",
  219. handle->dir == I2S_DIR_TX ? "tx" : "rx", handle->controller->id);
  220. return ret;
  221. err:
  222. xSemaphoreGive(handle->mutex);
  223. return ret;
  224. }
  225. esp_err_t i2s_channel_reconfig_std_clock(i2s_chan_handle_t handle, const i2s_std_clk_config_t *clk_cfg)
  226. {
  227. I2S_NULL_POINTER_CHECK(TAG, handle);
  228. I2S_NULL_POINTER_CHECK(TAG, clk_cfg);
  229. esp_err_t ret = ESP_OK;
  230. xSemaphoreTake(handle->mutex, portMAX_DELAY);
  231. ESP_GOTO_ON_FALSE(handle->mode == I2S_COMM_MODE_STD, ESP_ERR_INVALID_ARG, err, TAG, "this handle is not working in standard mode");
  232. ESP_GOTO_ON_FALSE(handle->state == I2S_CHAN_STATE_READY, ESP_ERR_INVALID_STATE, err, TAG, "invalid state, I2S should be disabled before reconfiguring the clock");
  233. i2s_std_config_t *std_cfg = (i2s_std_config_t *)handle->mode_info;
  234. ESP_GOTO_ON_FALSE(std_cfg, ESP_ERR_INVALID_STATE, err, TAG, "initialization not complete");
  235. #if SOC_I2S_SUPPORTS_APLL
  236. /* Enable APLL and acquire its lock when the clock source is changed to APLL */
  237. if (clk_cfg->clk_src == I2S_CLK_SRC_APLL && std_cfg->clk_cfg.clk_src != I2S_CLK_SRC_APLL) {
  238. periph_rtc_apll_acquire();
  239. handle->apll_en = true;
  240. }
  241. /* Disable APLL and release its lock when clock source is changed to 160M_PLL */
  242. if (clk_cfg->clk_src != I2S_CLK_SRC_APLL && std_cfg->clk_cfg.clk_src == I2S_CLK_SRC_APLL) {
  243. periph_rtc_apll_release();
  244. handle->apll_en = false;
  245. }
  246. #endif
  247. ESP_GOTO_ON_ERROR(i2s_std_set_clock(handle, clk_cfg), err, TAG, "update clock failed");
  248. #ifdef CONFIG_PM_ENABLE
  249. // Create/Re-create power management lock
  250. if (std_cfg->clk_cfg.clk_src != clk_cfg->clk_src) {
  251. ESP_GOTO_ON_ERROR(esp_pm_lock_delete(handle->pm_lock), err, TAG, "I2S delete old pm lock failed");
  252. esp_pm_lock_type_t pm_type = ESP_PM_APB_FREQ_MAX;
  253. #if SOC_I2S_SUPPORTS_APLL
  254. if (clk_cfg->clk_src == I2S_CLK_SRC_APLL) {
  255. pm_type = ESP_PM_NO_LIGHT_SLEEP;
  256. }
  257. #endif // SOC_I2S_SUPPORTS_APLL
  258. ESP_GOTO_ON_ERROR(esp_pm_lock_create(pm_type, 0, "i2s_driver", &handle->pm_lock), err, TAG, "I2S pm lock create failed");
  259. }
  260. #endif //CONFIG_PM_ENABLE
  261. xSemaphoreGive(handle->mutex);
  262. return ESP_OK;
  263. err:
  264. xSemaphoreGive(handle->mutex);
  265. return ret;
  266. }
  267. esp_err_t i2s_channel_reconfig_std_slot(i2s_chan_handle_t handle, const i2s_std_slot_config_t *slot_cfg)
  268. {
  269. I2S_NULL_POINTER_CHECK(TAG, handle);
  270. I2S_NULL_POINTER_CHECK(TAG, slot_cfg);
  271. esp_err_t ret = ESP_OK;
  272. xSemaphoreTake(handle->mutex, portMAX_DELAY);
  273. ESP_GOTO_ON_FALSE(handle->mode == I2S_COMM_MODE_STD, ESP_ERR_INVALID_ARG, err, TAG, "this handle is not working in standard mode");
  274. ESP_GOTO_ON_FALSE(handle->state == I2S_CHAN_STATE_READY, ESP_ERR_INVALID_STATE, err, TAG, "invalid state, I2S should be disabled before reconfiguring the slot");
  275. i2s_std_config_t *std_cfg = (i2s_std_config_t *)handle->mode_info;
  276. ESP_GOTO_ON_FALSE(std_cfg, ESP_ERR_INVALID_STATE, err, TAG, "initialization not complete");
  277. ESP_GOTO_ON_ERROR(i2s_std_set_slot(handle, slot_cfg), err, TAG, "set i2s standard slot failed");
  278. /* If the slot bit width changed, then need to update the clock */
  279. uint32_t slot_bits = slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO ? slot_cfg->data_bit_width : slot_cfg->slot_bit_width;
  280. if (std_cfg->slot_cfg.slot_bit_width == slot_bits) {
  281. ESP_GOTO_ON_ERROR(i2s_std_set_clock(handle, &std_cfg->clk_cfg), err, TAG, "update clock failed");
  282. }
  283. xSemaphoreGive(handle->mutex);
  284. return ESP_OK;
  285. err:
  286. xSemaphoreGive(handle->mutex);
  287. return ret;
  288. }
  289. esp_err_t i2s_channel_reconfig_std_gpio(i2s_chan_handle_t handle, const i2s_std_gpio_config_t *gpio_cfg)
  290. {
  291. I2S_NULL_POINTER_CHECK(TAG, handle);
  292. I2S_NULL_POINTER_CHECK(TAG, gpio_cfg);
  293. esp_err_t ret = ESP_OK;
  294. xSemaphoreTake(handle->mutex, portMAX_DELAY);
  295. ESP_GOTO_ON_FALSE(handle->mode == I2S_COMM_MODE_STD, ESP_ERR_INVALID_ARG, err, TAG, "This handle is not working in standard mode");
  296. ESP_GOTO_ON_FALSE(handle->state == I2S_CHAN_STATE_READY, ESP_ERR_INVALID_STATE, err, TAG, "Invalid state, I2S should be disabled before reconfiguring the gpio");
  297. ESP_GOTO_ON_ERROR(i2s_std_set_gpio(handle, gpio_cfg), err, TAG, "set i2s standard slot failed");
  298. xSemaphoreGive(handle->mutex);
  299. return ESP_OK;
  300. err:
  301. xSemaphoreGive(handle->mutex);
  302. return ret;
  303. }