i2s_std.c 16 KB

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