i2s_tdm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_tdm.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_tdm";
  23. // Same with standard mode except total slot number
  24. static esp_err_t i2s_tdm_calculate_clock(i2s_chan_handle_t handle, const i2s_tdm_clk_config_t *clk_cfg, i2s_hal_clock_info_t *clk_info)
  25. {
  26. uint32_t rate = clk_cfg->sample_rate_hz;
  27. i2s_tdm_slot_config_t *slot_cfg = &((i2s_tdm_config_t *)(handle->mode_info))->slot_cfg;
  28. uint32_t slot_bits = (slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO) ||
  29. ((int)slot_cfg->slot_bit_width < (int)slot_cfg->data_bit_width) ?
  30. slot_cfg->data_bit_width : slot_cfg->slot_bit_width;
  31. /* Calculate multiple
  32. * Fmclk = bck_div*fbck = fsclk/(mclk_div+b/a) */
  33. if (handle->role == I2S_ROLE_MASTER) {
  34. clk_info->bclk = rate * handle->total_slot * slot_bits;
  35. clk_info->mclk = rate * clk_cfg->mclk_multiple;
  36. clk_info->bclk_div = clk_info->mclk / clk_info->bclk;
  37. /* While RECEIVING multiple slots, the data will go wrong if the bclk_div is equal or smaller than 2 */
  38. if (clk_info->bclk_div <= 2) {
  39. clk_info->bclk_div = 3;
  40. clk_info->mclk = clk_info->bclk * clk_info->bclk_div;
  41. ESP_LOGW(TAG, "the current mclk multiple is too small, adjust the mclk multiple to %"PRIu32, clk_info->mclk / rate);
  42. }
  43. } else {
  44. if (clk_cfg->bclk_div < 8) {
  45. ESP_LOGW(TAG, "the current bclk division is too small, adjust the bclk division to 8");
  46. clk_info->bclk_div = 8;
  47. } else {
  48. clk_info->bclk_div = clk_cfg->bclk_div;
  49. }
  50. clk_info->bclk = rate * handle->total_slot * slot_bits;
  51. clk_info->mclk = clk_info->bclk * clk_info->bclk_div;
  52. }
  53. clk_info->sclk = clk_cfg->clk_src == I2S_CLK_SRC_EXTERNAL ?
  54. clk_cfg->ext_clk_freq_hz : i2s_get_source_clk_freq(clk_cfg->clk_src, clk_info->mclk);
  55. clk_info->mclk_div = clk_info->sclk / clk_info->mclk;
  56. /* Check if the configuration is correct */
  57. ESP_RETURN_ON_FALSE(clk_info->mclk_div, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large for the current clock source");
  58. return ESP_OK;
  59. }
  60. static esp_err_t i2s_tdm_set_clock(i2s_chan_handle_t handle, const i2s_tdm_clk_config_t *clk_cfg)
  61. {
  62. esp_err_t ret = ESP_OK;
  63. i2s_tdm_config_t *tdm_cfg = (i2s_tdm_config_t *)(handle->mode_info);
  64. i2s_hal_clock_info_t clk_info;
  65. /* Calculate clock parameters */
  66. ESP_RETURN_ON_ERROR(i2s_tdm_calculate_clock(handle, clk_cfg, &clk_info), TAG, "clock calculate failed");
  67. ESP_LOGD(TAG, "Clock division info: [sclk] %"PRIu32" Hz [mdiv] %d [mclk] %"PRIu32" Hz [bdiv] %d [bclk] %"PRIu32" Hz",
  68. clk_info.sclk, clk_info.mclk_div, clk_info.mclk, clk_info.bclk_div, clk_info.bclk);
  69. portENTER_CRITICAL(&g_i2s.spinlock);
  70. /* Set clock configurations in HAL*/
  71. I2S_CLOCK_SRC_ATOMIC() {
  72. if (handle->dir == I2S_DIR_TX) {
  73. i2s_hal_set_tx_clock(&handle->controller->hal, &clk_info, clk_cfg->clk_src);
  74. } else {
  75. i2s_hal_set_rx_clock(&handle->controller->hal, &clk_info, clk_cfg->clk_src);
  76. }
  77. }
  78. portEXIT_CRITICAL(&g_i2s.spinlock);
  79. /* Update the mode info: clock configuration */
  80. memcpy(&(tdm_cfg->clk_cfg), clk_cfg, sizeof(i2s_tdm_clk_config_t));
  81. return ret;
  82. }
  83. static esp_err_t i2s_tdm_set_slot(i2s_chan_handle_t handle, const i2s_tdm_slot_config_t *slot_cfg)
  84. {
  85. ESP_RETURN_ON_FALSE(slot_cfg->slot_mask, ESP_ERR_INVALID_ARG, TAG, "At least one channel should be enabled");
  86. /* Update the total slot num and active slot num */
  87. handle->active_slot = slot_cfg->slot_mode == I2S_SLOT_MODE_MONO ? 1 : __builtin_popcount(slot_cfg->slot_mask);
  88. uint32_t max_slot_num = 32 - __builtin_clz(slot_cfg->slot_mask);
  89. handle->total_slot = slot_cfg->total_slot < max_slot_num ? max_slot_num : slot_cfg->total_slot;
  90. handle->total_slot = handle->total_slot < 2 ? 2 : handle->total_slot; // At least two slots in a frame
  91. uint32_t buf_size = i2s_get_buf_size(handle, slot_cfg->data_bit_width, handle->dma.frame_num);
  92. /* The DMA buffer need to re-allocate if the buffer size changed */
  93. if (handle->dma.buf_size != buf_size) {
  94. handle->dma.buf_size = buf_size;
  95. ESP_RETURN_ON_ERROR(i2s_free_dma_desc(handle), TAG, "failed to free the old dma descriptor");
  96. ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, handle->dma.desc_num, buf_size),
  97. TAG, "allocate memory for dma descriptor failed");
  98. }
  99. bool is_slave = handle->role == I2S_ROLE_SLAVE;
  100. /* Share bck and ws signal in full-duplex mode */
  101. if (handle->controller->full_duplex) {
  102. i2s_ll_share_bck_ws(handle->controller->hal.dev, true);
  103. /* Since bck and ws are shared, only tx or rx can be master
  104. Force to set rx as slave to avoid conflict of clock signal */
  105. if (handle->dir == I2S_DIR_RX) {
  106. is_slave = true;
  107. }
  108. } else {
  109. i2s_ll_share_bck_ws(handle->controller->hal.dev, false);
  110. }
  111. portENTER_CRITICAL(&g_i2s.spinlock);
  112. /* Configure the hardware to apply TDM format */
  113. if (handle->dir == I2S_DIR_TX) {
  114. i2s_hal_tdm_set_tx_slot(&(handle->controller->hal), is_slave, (i2s_hal_slot_config_t *)slot_cfg);
  115. } else {
  116. i2s_hal_tdm_set_rx_slot(&(handle->controller->hal), is_slave, (i2s_hal_slot_config_t *)slot_cfg);
  117. }
  118. portEXIT_CRITICAL(&g_i2s.spinlock);
  119. /* Update the mode info: slot configuration */
  120. i2s_tdm_config_t *tdm_cfg = (i2s_tdm_config_t *)(handle->mode_info);
  121. memcpy(&(tdm_cfg->slot_cfg), slot_cfg, sizeof(i2s_tdm_slot_config_t));
  122. return ESP_OK;
  123. }
  124. static esp_err_t i2s_tdm_set_gpio(i2s_chan_handle_t handle, const i2s_tdm_gpio_config_t *gpio_cfg)
  125. {
  126. int id = handle->controller->id;
  127. /* Check validity of selected pins */
  128. ESP_RETURN_ON_FALSE((gpio_cfg->bclk == -1 || GPIO_IS_VALID_GPIO(gpio_cfg->bclk)),
  129. ESP_ERR_INVALID_ARG, TAG, "bclk invalid");
  130. ESP_RETURN_ON_FALSE((gpio_cfg->ws == -1 || GPIO_IS_VALID_GPIO(gpio_cfg->ws)),
  131. ESP_ERR_INVALID_ARG, TAG, "ws invalid");
  132. i2s_tdm_config_t *tdm_cfg = (i2s_tdm_config_t *)(handle->mode_info);
  133. /* Loopback if dout = din */
  134. if (gpio_cfg->dout != -1 &&
  135. gpio_cfg->dout == gpio_cfg->din) {
  136. i2s_gpio_loopback_set(gpio_cfg->dout, i2s_periph_signal[id].data_out_sig, i2s_periph_signal[id].data_in_sig);
  137. } else if (handle->dir == I2S_DIR_TX) {
  138. /* Set data output GPIO */
  139. i2s_gpio_check_and_set(gpio_cfg->dout, i2s_periph_signal[id].data_out_sig, false, false);
  140. } else {
  141. /* Set data input GPIO */
  142. i2s_gpio_check_and_set(gpio_cfg->din, i2s_periph_signal[id].data_in_sig, true, false);
  143. }
  144. /* Set mclk pin */
  145. ESP_RETURN_ON_ERROR(i2s_check_set_mclk(id, gpio_cfg->mclk, tdm_cfg->clk_cfg.clk_src, gpio_cfg->invert_flags.mclk_inv), TAG, "mclk config failed");
  146. if (handle->role == I2S_ROLE_SLAVE) {
  147. /* For "tx + slave" mode, select TX signal index for ws and bck */
  148. if (handle->dir == I2S_DIR_TX && !handle->controller->full_duplex) {
  149. #if SOC_I2S_HW_VERSION_2
  150. i2s_ll_mclk_bind_to_tx_clk(handle->controller->hal.dev);
  151. #endif
  152. i2s_gpio_check_and_set(gpio_cfg->ws, i2s_periph_signal[id].s_tx_ws_sig, true, gpio_cfg->invert_flags.ws_inv);
  153. i2s_gpio_check_and_set(gpio_cfg->bclk, i2s_periph_signal[id].s_tx_bck_sig, true, gpio_cfg->invert_flags.bclk_inv);
  154. /* For "tx + rx + slave" or "rx + slave" mode, select RX signal index for ws and bck */
  155. } else {
  156. i2s_gpio_check_and_set(gpio_cfg->ws, i2s_periph_signal[id].s_rx_ws_sig, true, gpio_cfg->invert_flags.ws_inv);
  157. i2s_gpio_check_and_set(gpio_cfg->bclk, i2s_periph_signal[id].s_rx_bck_sig, true, gpio_cfg->invert_flags.bclk_inv);
  158. }
  159. } else {
  160. /* For "rx + master" mode, select RX signal index for ws and bck */
  161. if (handle->dir == I2S_DIR_RX && !handle->controller->full_duplex) {
  162. #if SOC_I2S_HW_VERSION_2
  163. i2s_ll_mclk_bind_to_rx_clk(handle->controller->hal.dev);
  164. #endif
  165. i2s_gpio_check_and_set(gpio_cfg->ws, i2s_periph_signal[id].m_rx_ws_sig, false, gpio_cfg->invert_flags.ws_inv);
  166. i2s_gpio_check_and_set(gpio_cfg->bclk, i2s_periph_signal[id].m_rx_bck_sig, false, gpio_cfg->invert_flags.bclk_inv);
  167. /* For "tx + rx + master" or "tx + master" mode, select TX signal index for ws and bck */
  168. } else {
  169. i2s_gpio_check_and_set(gpio_cfg->ws, i2s_periph_signal[id].m_tx_ws_sig, false, gpio_cfg->invert_flags.ws_inv);
  170. i2s_gpio_check_and_set(gpio_cfg->bclk, i2s_periph_signal[id].m_tx_bck_sig, false, gpio_cfg->invert_flags.bclk_inv);
  171. }
  172. }
  173. /* Update the mode info: gpio configuration */
  174. memcpy(&(tdm_cfg->gpio_cfg), gpio_cfg, sizeof(i2s_tdm_gpio_config_t));
  175. return ESP_OK;
  176. }
  177. esp_err_t i2s_channel_init_tdm_mode(i2s_chan_handle_t handle, const i2s_tdm_config_t *tdm_cfg)
  178. {
  179. #if CONFIG_I2S_ENABLE_DEBUG_LOG
  180. esp_log_level_set(TAG, ESP_LOG_DEBUG);
  181. #endif
  182. I2S_NULL_POINTER_CHECK(TAG, handle);
  183. esp_err_t ret = ESP_OK;
  184. xSemaphoreTake(handle->mutex, portMAX_DELAY);
  185. ESP_GOTO_ON_FALSE(handle->state == I2S_CHAN_STATE_REGISTER, ESP_ERR_INVALID_STATE, err, TAG, "the channel has initialized already");
  186. handle->mode = I2S_COMM_MODE_TDM;
  187. /* Allocate memory for storing the configurations of TDM mode */
  188. if (handle->mode_info) {
  189. free(handle->mode_info);
  190. }
  191. handle->mode_info = calloc(1, sizeof(i2s_tdm_config_t));
  192. ESP_GOTO_ON_FALSE(handle->mode_info, ESP_ERR_NO_MEM, err, TAG, "no memory for storing the configurations");
  193. /* i2s_set_tdm_slot should be called before i2s_set_tdm_clock while initializing, because clock is relay on the slot */
  194. ESP_GOTO_ON_ERROR(i2s_tdm_set_slot(handle, &tdm_cfg->slot_cfg), err, TAG, "initialize channel failed while setting slot");
  195. #if SOC_I2S_SUPPORTS_APLL
  196. /* Enable APLL and acquire its lock when the clock source is APLL */
  197. if (tdm_cfg->clk_cfg.clk_src == I2S_CLK_SRC_APLL) {
  198. periph_rtc_apll_acquire();
  199. handle->apll_en = true;
  200. }
  201. #endif
  202. ESP_GOTO_ON_ERROR(i2s_tdm_set_clock(handle, &tdm_cfg->clk_cfg), err, TAG, "initialize channel failed while setting clock");
  203. /* i2s_tdm_set_gpio should be called after i2s_tdm_set_clock as mclk relies on the clock source */
  204. ESP_GOTO_ON_ERROR(i2s_tdm_set_gpio(handle, &tdm_cfg->gpio_cfg), err, TAG, "initialize channel failed while setting gpio pins");
  205. ESP_GOTO_ON_ERROR(i2s_init_dma_intr(handle, I2S_INTR_ALLOC_FLAGS), err, TAG, "initialize dma interrupt failed");
  206. #if SOC_I2S_HW_VERSION_2
  207. /* Enable clock to start outputting mclk signal. Some codecs will reset once mclk stop */
  208. if (handle->dir == I2S_DIR_TX) {
  209. i2s_ll_tx_enable_tdm(handle->controller->hal.dev);
  210. } else {
  211. i2s_ll_rx_enable_tdm(handle->controller->hal.dev);
  212. }
  213. #endif
  214. #ifdef CONFIG_PM_ENABLE
  215. esp_pm_lock_type_t pm_type = ESP_PM_APB_FREQ_MAX;
  216. #if SOC_I2S_SUPPORTS_APLL
  217. if (tdm_cfg->clk_cfg.clk_src == I2S_CLK_SRC_APLL) {
  218. pm_type = ESP_PM_NO_LIGHT_SLEEP;
  219. }
  220. #endif // SOC_I2S_SUPPORTS_APLL
  221. ESP_RETURN_ON_ERROR(esp_pm_lock_create(pm_type, 0, "i2s_driver", &handle->pm_lock), TAG, "I2S pm lock create failed");
  222. #endif
  223. /* Initialization finished, mark state as ready */
  224. handle->state = I2S_CHAN_STATE_READY;
  225. xSemaphoreGive(handle->mutex);
  226. ESP_LOGD(TAG, "The %s channel on I2S%d has been initialized to TDM mode successfully",
  227. handle->dir == I2S_DIR_TX ? "tx" : "rx", handle->controller->id);
  228. return ret;
  229. err:
  230. xSemaphoreGive(handle->mutex);
  231. return ret;
  232. }
  233. esp_err_t i2s_channel_reconfig_tdm_clock(i2s_chan_handle_t handle, const i2s_tdm_clk_config_t *clk_cfg)
  234. {
  235. I2S_NULL_POINTER_CHECK(TAG, handle);
  236. I2S_NULL_POINTER_CHECK(TAG, clk_cfg);
  237. esp_err_t ret = ESP_OK;
  238. xSemaphoreTake(handle->mutex, portMAX_DELAY);
  239. ESP_GOTO_ON_FALSE(handle->mode == I2S_COMM_MODE_TDM, ESP_ERR_INVALID_ARG, err, TAG, "this handle is not working in standard mode");
  240. 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");
  241. i2s_tdm_config_t *tdm_cfg = (i2s_tdm_config_t *)handle->mode_info;
  242. ESP_GOTO_ON_FALSE(tdm_cfg, ESP_ERR_INVALID_STATE, err, TAG, "initialization not complete");
  243. #if SOC_I2S_SUPPORTS_APLL
  244. /* Enable APLL and acquire its lock when the clock source is changed to APLL */
  245. if (clk_cfg->clk_src == I2S_CLK_SRC_APLL && tdm_cfg->clk_cfg.clk_src != I2S_CLK_SRC_APLL) {
  246. periph_rtc_apll_acquire();
  247. handle->apll_en = true;
  248. }
  249. /* Disable APLL and release its lock when clock source is changed to 160M_PLL */
  250. if (clk_cfg->clk_src != I2S_CLK_SRC_APLL && tdm_cfg->clk_cfg.clk_src == I2S_CLK_SRC_APLL) {
  251. periph_rtc_apll_release();
  252. handle->apll_en = false;
  253. }
  254. #endif
  255. ESP_GOTO_ON_ERROR(i2s_tdm_set_clock(handle, clk_cfg), err, TAG, "update clock failed");
  256. #ifdef CONFIG_PM_ENABLE
  257. // Create/Re-create power management lock
  258. if (tdm_cfg->clk_cfg.clk_src != clk_cfg->clk_src) {
  259. ESP_GOTO_ON_ERROR(esp_pm_lock_delete(handle->pm_lock), err, TAG, "I2S delete old pm lock failed");
  260. esp_pm_lock_type_t pm_type = ESP_PM_APB_FREQ_MAX;
  261. #if SOC_I2S_SUPPORTS_APLL
  262. if (clk_cfg->clk_src == I2S_CLK_SRC_APLL) {
  263. pm_type = ESP_PM_NO_LIGHT_SLEEP;
  264. }
  265. #endif // SOC_I2S_SUPPORTS_APLL
  266. ESP_GOTO_ON_ERROR(esp_pm_lock_create(pm_type, 0, "i2s_driver", &handle->pm_lock), err, TAG, "I2S pm lock create failed");
  267. }
  268. #endif //CONFIG_PM_ENABLE
  269. xSemaphoreGive(handle->mutex);
  270. return ESP_OK;
  271. err:
  272. xSemaphoreGive(handle->mutex);
  273. return ret;
  274. }
  275. esp_err_t i2s_channel_reconfig_tdm_slot(i2s_chan_handle_t handle, const i2s_tdm_slot_config_t *slot_cfg)
  276. {
  277. I2S_NULL_POINTER_CHECK(TAG, handle);
  278. I2S_NULL_POINTER_CHECK(TAG, slot_cfg);
  279. esp_err_t ret = ESP_OK;
  280. xSemaphoreTake(handle->mutex, portMAX_DELAY);
  281. ESP_GOTO_ON_FALSE(handle->mode == I2S_COMM_MODE_TDM, ESP_ERR_INVALID_ARG, err, TAG, "this handle is not working in standard mode");
  282. 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");
  283. i2s_tdm_config_t *tdm_cfg = (i2s_tdm_config_t *)handle->mode_info;
  284. ESP_GOTO_ON_FALSE(tdm_cfg, ESP_ERR_INVALID_STATE, err, TAG, "initialization not complete");
  285. ESP_GOTO_ON_ERROR(i2s_tdm_set_slot(handle, slot_cfg), err, TAG, "set i2s standard slot failed");
  286. /* If the slot bit width changed, then need to update the clock */
  287. uint32_t slot_bits = slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO ? slot_cfg->data_bit_width : slot_cfg->slot_bit_width;
  288. if (tdm_cfg->slot_cfg.slot_bit_width == slot_bits) {
  289. ESP_GOTO_ON_ERROR(i2s_tdm_set_clock(handle, &tdm_cfg->clk_cfg), err, TAG, "update clock failed");
  290. }
  291. /* Reset queue */
  292. xQueueReset(handle->msg_queue);
  293. xSemaphoreGive(handle->mutex);
  294. return ESP_OK;
  295. err:
  296. xSemaphoreGive(handle->mutex);
  297. return ret;
  298. }
  299. esp_err_t i2s_channel_reconfig_tdm_gpio(i2s_chan_handle_t handle, const i2s_tdm_gpio_config_t *gpio_cfg)
  300. {
  301. I2S_NULL_POINTER_CHECK(TAG, handle);
  302. I2S_NULL_POINTER_CHECK(TAG, gpio_cfg);
  303. esp_err_t ret = ESP_OK;
  304. xSemaphoreTake(handle->mutex, portMAX_DELAY);
  305. ESP_GOTO_ON_FALSE(handle->mode == I2S_COMM_MODE_TDM, ESP_ERR_INVALID_ARG, err, TAG, "This handle is not working in standard mode");
  306. 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");
  307. ESP_GOTO_ON_ERROR(i2s_tdm_set_gpio(handle, gpio_cfg), err, TAG, "set i2s standard slot failed");
  308. xSemaphoreGive(handle->mutex);
  309. return ESP_OK;
  310. err:
  311. xSemaphoreGive(handle->mutex);
  312. return ret;
  313. }