i2s_hal.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // The HAL layer for I2S (common part)
  15. #include "soc/soc.h"
  16. #include "hal/i2s_hal.h"
  17. #define I2S_TX_PDM_FP_DEF 960 // Set to the recommended value(960) in TRM
  18. #define I2S_RX_PDM_DSR_DEF 0
  19. void i2s_hal_set_tx_mode(i2s_hal_context_t *hal, i2s_channel_t ch, i2s_bits_per_sample_t bits)
  20. {
  21. if (bits <= I2S_BITS_PER_SAMPLE_16BIT) {
  22. i2s_ll_set_tx_fifo_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
  23. } else {
  24. i2s_ll_set_tx_fifo_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 2 : 3);
  25. }
  26. i2s_ll_set_tx_chan_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
  27. #if SOC_I2S_SUPPORTS_DMA_EQUAL
  28. i2s_ll_set_tx_dma_equal(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
  29. #endif
  30. }
  31. void i2s_hal_set_rx_mode(i2s_hal_context_t *hal, i2s_channel_t ch, i2s_bits_per_sample_t bits)
  32. {
  33. if (bits <= I2S_BITS_PER_SAMPLE_16BIT) {
  34. i2s_ll_set_rx_fifo_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
  35. } else {
  36. i2s_ll_set_rx_fifo_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 2 : 3);
  37. }
  38. i2s_ll_set_rx_chan_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
  39. #if SOC_I2S_SUPPORTS_DMA_EQUAL
  40. i2s_ll_set_rx_dma_equal(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
  41. #endif
  42. }
  43. void i2s_hal_set_in_link(i2s_hal_context_t *hal, uint32_t bytes_num, uint32_t addr)
  44. {
  45. i2s_ll_set_in_link_addr(hal->dev, addr);
  46. i2s_ll_set_rx_eof_num(hal->dev, bytes_num);
  47. }
  48. #if SOC_I2S_SUPPORTS_PDM
  49. void i2s_hal_tx_pdm_cfg(i2s_hal_context_t *hal, uint32_t fp, uint32_t fs)
  50. {
  51. i2s_ll_tx_pdm_cfg(hal->dev, fp, fs);
  52. }
  53. void i2s_hal_get_tx_pdm(i2s_hal_context_t *hal, uint32_t *fp, uint32_t *fs)
  54. {
  55. i2s_ll_get_tx_pdm(hal->dev, fp, fs);
  56. }
  57. void i2s_hal_rx_pdm_cfg(i2s_hal_context_t *hal, uint32_t dsr)
  58. {
  59. i2s_ll_rx_pdm_cfg(hal->dev, dsr);
  60. }
  61. void i2s_hal_get_rx_pdm(i2s_hal_context_t *hal, uint32_t *dsr)
  62. {
  63. i2s_ll_get_rx_pdm(hal->dev, dsr);
  64. }
  65. #endif
  66. void i2s_hal_set_clk_div(i2s_hal_context_t *hal, int div_num, int div_a, int div_b, int tx_bck_div, int rx_bck_div)
  67. {
  68. i2s_ll_set_clkm_div_num(hal->dev, div_num);
  69. i2s_ll_set_clkm_div_a(hal->dev, div_a);
  70. i2s_ll_set_clkm_div_b(hal->dev, div_b);
  71. i2s_ll_set_tx_bck_div_num(hal->dev, tx_bck_div);
  72. i2s_ll_set_rx_bck_div_num(hal->dev, rx_bck_div);
  73. }
  74. void i2s_hal_set_tx_bits_mod(i2s_hal_context_t *hal, i2s_bits_per_sample_t bits)
  75. {
  76. i2s_ll_set_tx_bits_mod(hal->dev, bits);
  77. }
  78. void i2s_hal_set_rx_bits_mod(i2s_hal_context_t *hal, i2s_bits_per_sample_t bits)
  79. {
  80. i2s_ll_set_rx_bits_mod(hal->dev, bits);
  81. }
  82. void i2s_hal_reset(i2s_hal_context_t *hal)
  83. {
  84. // Reset I2S TX/RX module first, and then, reset DMA and FIFO.
  85. i2s_ll_reset_tx(hal->dev);
  86. i2s_ll_reset_rx(hal->dev);
  87. i2s_ll_reset_dma_in(hal->dev);
  88. i2s_ll_reset_dma_out(hal->dev);
  89. i2s_ll_reset_rx_fifo(hal->dev);
  90. i2s_ll_reset_tx_fifo(hal->dev);
  91. }
  92. void i2s_hal_start_tx(i2s_hal_context_t *hal)
  93. {
  94. i2s_ll_start_out_link(hal->dev);
  95. i2s_ll_start_tx(hal->dev);
  96. }
  97. void i2s_hal_start_rx(i2s_hal_context_t *hal)
  98. {
  99. i2s_ll_start_in_link(hal->dev);
  100. i2s_ll_start_rx(hal->dev);
  101. }
  102. void i2s_hal_stop_tx(i2s_hal_context_t *hal)
  103. {
  104. i2s_ll_stop_out_link(hal->dev);
  105. i2s_ll_stop_tx(hal->dev);
  106. }
  107. void i2s_hal_stop_rx(i2s_hal_context_t *hal)
  108. {
  109. i2s_ll_stop_in_link(hal->dev);
  110. i2s_ll_stop_rx(hal->dev);
  111. }
  112. void i2s_hal_format_config(i2s_hal_context_t *hal, const i2s_config_t *i2s_config)
  113. {
  114. switch (i2s_config->communication_format) {
  115. case I2S_COMM_FORMAT_STAND_MSB:
  116. if (i2s_config->mode & I2S_MODE_TX) {
  117. i2s_ll_set_tx_format_msb_align(hal->dev);
  118. }
  119. if (i2s_config->mode & I2S_MODE_RX) {
  120. i2s_ll_set_rx_format_msb_align(hal->dev);
  121. }
  122. break;
  123. case I2S_COMM_FORMAT_STAND_PCM_SHORT:
  124. if (i2s_config->mode & I2S_MODE_TX) {
  125. i2s_ll_set_tx_pcm_long(hal->dev);
  126. }
  127. if (i2s_config->mode & I2S_MODE_RX) {
  128. i2s_ll_set_rx_pcm_long(hal->dev);
  129. }
  130. break;
  131. case I2S_COMM_FORMAT_STAND_PCM_LONG:
  132. if (i2s_config->mode & I2S_MODE_TX) {
  133. i2s_ll_set_tx_pcm_short(hal->dev);
  134. }
  135. if (i2s_config->mode & I2S_MODE_RX) {
  136. i2s_ll_set_rx_pcm_short(hal->dev);
  137. }
  138. break;
  139. default: //I2S_COMM_FORMAT_STAND_I2S
  140. if (i2s_config->mode & I2S_MODE_TX) {
  141. i2s_ll_set_tx_format_philip(hal->dev);
  142. }
  143. if (i2s_config->mode & I2S_MODE_RX) {
  144. i2s_ll_set_rx_format_philip(hal->dev);
  145. }
  146. break;
  147. }
  148. }
  149. void i2s_hal_config_param(i2s_hal_context_t *hal, const i2s_config_t *i2s_config)
  150. {
  151. //reset i2s
  152. i2s_ll_reset_tx(hal->dev);
  153. i2s_ll_reset_rx(hal->dev);
  154. //reset dma
  155. i2s_ll_reset_dma_in(hal->dev);
  156. i2s_ll_reset_dma_out(hal->dev);
  157. i2s_ll_enable_dma(hal->dev);
  158. i2s_ll_set_lcd_en(hal->dev, 0);
  159. i2s_ll_set_camera_en(hal->dev, 0);
  160. i2s_ll_set_dscr_en(hal->dev, 0);
  161. i2s_ll_set_tx_chan_mod(hal->dev, i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? i2s_config->channel_format : (i2s_config->channel_format >> 1)); // 0-two channel;1-right;2-left;3-righ;4-left
  162. i2s_ll_set_tx_fifo_mod(hal->dev, i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 0 : 1); // 0-right&left channel;1-one channel
  163. i2s_ll_set_tx_mono(hal->dev, 0);
  164. i2s_ll_set_rx_chan_mod(hal->dev, i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? i2s_config->channel_format : (i2s_config->channel_format >> 1)); // 0-two channel;1-right;2-left;3-righ;4-left
  165. i2s_ll_set_rx_fifo_mod(hal->dev, i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 0 : 1); // 0-right&left channel;1-one channel
  166. i2s_ll_set_rx_mono(hal->dev, 0);
  167. i2s_ll_set_dscr_en(hal->dev, 1); //connect dma to fifo
  168. i2s_ll_stop_tx(hal->dev);
  169. i2s_ll_stop_rx(hal->dev);
  170. if (i2s_config->mode & I2S_MODE_TX) {
  171. i2s_ll_set_tx_msb_right(hal->dev, 0);
  172. i2s_ll_set_tx_right_first(hal->dev, 0);
  173. i2s_ll_set_tx_slave_mod(hal->dev, 0); // Master
  174. i2s_ll_set_tx_fifo_mod_force_en(hal->dev, 1);
  175. if (i2s_config->mode & I2S_MODE_SLAVE) {
  176. i2s_ll_set_tx_slave_mod(hal->dev, 1); //TX Slave
  177. }
  178. }
  179. if (i2s_config->mode & I2S_MODE_RX) {
  180. i2s_ll_set_rx_msb_right(hal->dev, 0);
  181. i2s_ll_set_rx_right_first(hal->dev, 0);
  182. i2s_ll_set_rx_slave_mod(hal->dev, 0); // Master
  183. i2s_ll_set_rx_fifo_mod_force_en(hal->dev, 1);
  184. if (i2s_config->mode & I2S_MODE_SLAVE) {
  185. i2s_ll_set_rx_slave_mod(hal->dev, 1); //RX Slave
  186. }
  187. }
  188. #if SOC_I2S_SUPPORTS_PDM
  189. if (!(i2s_config->mode & I2S_MODE_PDM)) {
  190. i2s_ll_set_rx_pdm_en(hal->dev, 0);
  191. i2s_ll_set_tx_pdm_en(hal->dev, 0);
  192. } else {
  193. if (i2s_config->mode & I2S_MODE_TX) {
  194. i2s_ll_tx_pdm_cfg(hal->dev, I2S_TX_PDM_FP_DEF, i2s_config->sample_rate/100);
  195. }
  196. if(i2s_config->mode & I2S_MODE_RX) {
  197. i2s_ll_rx_pdm_cfg(hal->dev, I2S_RX_PDM_DSR_DEF);
  198. }
  199. // PDM mode have nothing to do with communication format configuration.
  200. return;
  201. }
  202. #endif
  203. #if SOC_I2S_SUPPORTS_ADC_DAC
  204. if (i2s_config->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
  205. if (i2s_config->mode & I2S_MODE_DAC_BUILT_IN) {
  206. i2s_ll_build_in_dac_ena(hal->dev);
  207. }
  208. if (i2s_config->mode & I2S_MODE_ADC_BUILT_IN) {
  209. i2s_ll_build_in_adc_ena(hal->dev);
  210. i2s_ll_set_rx_chan_mod(hal->dev, 1);
  211. i2s_ll_set_rx_fifo_mod(hal->dev, 1);
  212. i2s_ll_set_rx_mono(hal->dev, 0);
  213. }
  214. // Buildin ADC and DAC have nothing to do with communication format configuration.
  215. return;
  216. }
  217. #endif
  218. i2s_hal_format_config(hal, i2s_config);
  219. }
  220. void i2s_hal_enable_master_mode(i2s_hal_context_t *hal)
  221. {
  222. i2s_ll_set_tx_slave_mod(hal->dev, 0); //MASTER Slave
  223. i2s_ll_set_rx_slave_mod(hal->dev, 1); //RX Slave
  224. }
  225. void i2s_hal_enable_slave_mode(i2s_hal_context_t *hal)
  226. {
  227. i2s_ll_set_tx_slave_mod(hal->dev, 1); //TX Slave
  228. i2s_ll_set_rx_slave_mod(hal->dev, 1); //RX Slave
  229. }
  230. void i2s_hal_init(i2s_hal_context_t *hal, int i2s_num)
  231. {
  232. //Get hardware instance.
  233. hal->dev = I2S_LL_GET_HW(i2s_num);
  234. }