i2s_hal.c 8.8 KB

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