i2s_tdm.c 16 KB

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