drv_dao.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c) 2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <rtthread.h>
  8. #include <rtdevice.h>
  9. #define DBG_TAG "dao"
  10. #define DBG_LVL DBG_INFO
  11. #include <rtdbg.h>
  12. #ifdef BSP_USING_DAO
  13. #include "drivers/dev_audio.h"
  14. #include "hpm_i2s_drv.h"
  15. #include "hpm_dao_drv.h"
  16. #include "board.h"
  17. #include "drv_dao.h"
  18. #ifdef HPMSOC_HAS_HPMSDK_DMAV2
  19. #include "hpm_dmav2_drv.h"
  20. #else
  21. #include "hpm_dma_drv.h"
  22. #endif
  23. #include "hpm_dmamux_drv.h"
  24. #include "hpm_l1c_drv.h"
  25. #include "hpm_clock_drv.h"
  26. #include "hpm_dma_mgr.h"
  27. /* DAO connect to I2S1 TX*/
  28. #define DAO_DMA_REQ HPM_DMA_SRC_I2S1_TX
  29. #define DAO_I2S_DATA_LINE 0
  30. struct hpm_dao
  31. {
  32. struct rt_audio_device audio;
  33. struct rt_audio_configure play_config;
  34. rt_uint8_t* tx_fifo;
  35. };
  36. struct hpm_dao hpm_dao_dev = { 0 };
  37. static dma_resource_t dma_resource = { 0 };
  38. void dao_dma_tc_callback(DMA_Type *ptr, uint32_t channel, void *user_data)
  39. {
  40. rt_audio_tx_complete(&hpm_dao_dev.audio);
  41. }
  42. static rt_err_t hpm_dao_getcaps(struct rt_audio_device* audio, struct rt_audio_caps* caps)
  43. {
  44. rt_err_t result = RT_EOK;
  45. RT_ASSERT(audio != RT_NULL);
  46. struct hpm_dao* hpm_audio = (struct hpm_dao*)audio->parent.user_data;
  47. switch(caps->main_type)
  48. {
  49. case AUDIO_TYPE_OUTPUT:
  50. {
  51. switch(caps->sub_type)
  52. {
  53. case AUDIO_DSP_PARAM:
  54. caps->udata.config.samplerate = hpm_audio->play_config.samplerate;
  55. caps->udata.config.channels = hpm_audio->play_config.channels;
  56. caps->udata.config.samplebits = hpm_audio->play_config.samplebits;
  57. break;
  58. case AUDIO_DSP_SAMPLERATE:
  59. caps->udata.config.samplerate = hpm_audio->play_config.samplerate;
  60. break;
  61. case AUDIO_DSP_CHANNELS:
  62. caps->udata.config.channels = hpm_audio->play_config.channels;
  63. break;
  64. case AUDIO_DSP_SAMPLEBITS:
  65. caps->udata.config.samplebits = hpm_audio->play_config.samplebits;
  66. break;
  67. default:
  68. result = -RT_ERROR;
  69. break;
  70. }
  71. break;
  72. }
  73. default:
  74. result = -RT_ERROR;
  75. break;
  76. }
  77. return result;
  78. }
  79. static bool i2s_is_enabled(I2S_Type *ptr)
  80. {
  81. return ((ptr->CTRL & I2S_CTRL_I2S_EN_MASK) != 0);
  82. }
  83. static rt_err_t hpm_dao_set_samplerate(uint32_t samplerate)
  84. {
  85. uint32_t mclk_hz;
  86. i2s_transfer_config_t transfer;
  87. mclk_hz = clock_get_frequency(clock_i2s1);
  88. i2s_get_default_transfer_config_for_dao(&transfer);
  89. transfer.sample_rate = samplerate;
  90. bool is_enabled = i2s_is_enabled(DAO_I2S);
  91. if (is_enabled) {
  92. dma_abort_channel(dma_resource.base, dma_resource.channel);
  93. }
  94. if (status_success != i2s_config_tx(DAO_I2S, mclk_hz, &transfer))
  95. {
  96. LOG_E("dao_i2s configure transfer failed\n");
  97. return -RT_ERROR;
  98. }
  99. if (is_enabled)
  100. {
  101. i2s_enable(DAO_I2S);
  102. }
  103. return RT_EOK;
  104. }
  105. static rt_err_t hpm_dao_configure(struct rt_audio_device* audio, struct rt_audio_caps* caps)
  106. {
  107. rt_err_t result = RT_EOK;
  108. RT_ASSERT(audio != RT_NULL);
  109. struct hpm_dao* hpm_audio = (struct hpm_dao*)audio->parent.user_data;
  110. switch(caps->main_type)
  111. {
  112. case AUDIO_TYPE_OUTPUT:
  113. {
  114. switch(caps->sub_type)
  115. {
  116. case AUDIO_DSP_SAMPLERATE:
  117. {
  118. hpm_audio->play_config.samplerate = caps->udata.config.samplerate;
  119. hpm_dao_set_samplerate(caps->udata.config.samplerate);
  120. break;
  121. }
  122. default:
  123. result = -RT_ERROR;
  124. break;
  125. }
  126. }
  127. default:
  128. result = -RT_ERROR;
  129. break;
  130. }
  131. return result;
  132. }
  133. static rt_err_t hpm_dao_init(struct rt_audio_device* audio)
  134. {
  135. RT_ASSERT(audio != RT_NULL);
  136. rt_uint32_t mclk_hz;
  137. i2s_config_t i2s_config;
  138. i2s_transfer_config_t transfer;
  139. dao_config_t dao_config;
  140. init_dao_pins();
  141. board_init_dao_clock();
  142. i2s_get_default_config(DAO_I2S, &i2s_config);
  143. i2s_init(DAO_I2S, &i2s_config);
  144. i2s_enable_tx_dma_request(DAO_I2S);
  145. mclk_hz = clock_get_frequency(clock_i2s1);
  146. i2s_get_default_transfer_config_for_dao(&transfer);
  147. if (status_success != i2s_config_tx(DAO_I2S, mclk_hz, &transfer))
  148. {
  149. LOG_E("dao_i2s configure transfer failed\n");
  150. return -RT_ERROR;
  151. }
  152. /* init audio configure */
  153. hpm_dao_dev.play_config.channels = 2U; /* fix to 2 channels*/
  154. hpm_dao_dev.play_config.samplebits = 32U; /* fix to 32 sample bits */
  155. hpm_dao_dev.play_config.samplerate = 48000U; /* default 48KHz sample rate */
  156. dao_get_default_config(HPM_DAO, &dao_config);
  157. dao_config.enable_mono_output = true;
  158. if (status_success != dao_init(HPM_DAO, &dao_config)) {
  159. LOG_E("dao init failed\n");
  160. return -RT_ERROR;
  161. }
  162. return RT_EOK;
  163. }
  164. static rt_err_t hpm_dao_start(struct rt_audio_device* audio, int stream)
  165. {
  166. RT_ASSERT(audio != RT_NULL);
  167. i2s_disable(DAO_I2S);
  168. i2s_disable_tx_dma_request(DAO_I2S);
  169. dao_stop(HPM_DAO);
  170. dao_software_reset(HPM_DAO);
  171. if (dma_mgr_request_resource(&dma_resource) == status_success) {
  172. uint8_t dmamux_ch;
  173. dma_mgr_install_chn_tc_callback(&dma_resource, dao_dma_tc_callback, NULL);
  174. dma_mgr_enable_dma_irq_with_priority(&dma_resource, 1);
  175. dmamux_ch = DMA_SOC_CHN_TO_DMAMUX_CHN(dma_resource.base, dma_resource.channel);
  176. dmamux_config(HPM_DMAMUX, dmamux_ch, DAO_DMA_REQ, true);
  177. } else {
  178. LOG_E("no dma resource available for DAO transfer.\n");
  179. return -RT_ERROR;
  180. }
  181. /* fill 2 dummy data, it is suitable for 1/2 channel of audio */
  182. i2s_reset_tx(DAO_I2S);
  183. if (i2s_fill_tx_dummy_data(DAO_I2S, DAO_I2S_DATA_LINE, 2) != status_success) {
  184. return -RT_ERROR;
  185. }
  186. rt_audio_tx_complete(audio);
  187. i2s_enable(DAO_I2S);
  188. i2s_enable_tx_dma_request(DAO_I2S);
  189. dao_start(HPM_DAO);
  190. return RT_EOK;
  191. }
  192. static rt_err_t hpm_dao_stop(struct rt_audio_device* audio, int stream)
  193. {
  194. RT_ASSERT(audio != RT_NULL);
  195. dao_stop(HPM_DAO);
  196. i2s_stop(DAO_I2S);
  197. dma_abort_channel(dma_resource.base, dma_resource.channel);
  198. dma_mgr_release_resource(&dma_resource);
  199. return RT_EOK;
  200. }
  201. static rt_ssize_t hpm_dao_transmit(struct rt_audio_device* audio, const void* writeBuf, void* readBuf, rt_size_t size)
  202. {
  203. RT_ASSERT(audio != RT_NULL);
  204. dma_channel_config_t ch_config = {0};
  205. dma_default_channel_config(dma_resource.base, &ch_config);
  206. ch_config.src_addr = core_local_mem_to_sys_address(HPM_CORE0, (uint32_t)writeBuf);
  207. ch_config.dst_addr = (uint32_t)&DAO_I2S->TXD[DAO_I2S_DATA_LINE];
  208. ch_config.src_width = DMA_TRANSFER_WIDTH_WORD;
  209. ch_config.dst_width = DMA_TRANSFER_WIDTH_WORD;
  210. ch_config.src_addr_ctrl = DMA_ADDRESS_CONTROL_INCREMENT;
  211. ch_config.dst_addr_ctrl = DMA_ADDRESS_CONTROL_FIXED;
  212. ch_config.size_in_byte = size;
  213. ch_config.dst_mode = DMA_HANDSHAKE_MODE_HANDSHAKE;
  214. ch_config.src_burst_size = DMA_NUM_TRANSFER_PER_BURST_1T;
  215. if (l1c_dc_is_enabled()) {
  216. /* cache writeback for sent buff */
  217. l1c_dc_writeback((uint32_t)writeBuf, size);
  218. }
  219. if (status_success != dma_setup_channel(dma_resource.base, dma_resource.channel, &ch_config, true)) {
  220. return 0;
  221. }
  222. return size;
  223. }
  224. static void hpm_dao_buffer_info(struct rt_audio_device* audio, struct rt_audio_buf_info* info)
  225. {
  226. RT_ASSERT(audio != RT_NULL);
  227. /**
  228. * AUD_FIFO
  229. * +----------------+----------------+
  230. * | block1 | block2 |
  231. * +----------------+----------------+
  232. * \ block_size /
  233. */
  234. info->buffer = hpm_dao_dev.tx_fifo;
  235. info->total_size = DAO_FIFO_SIZE;
  236. info->block_size = DAO_FIFO_SIZE / 2;
  237. info->block_count = 2;
  238. }
  239. static struct rt_audio_ops hpm_dao_ops =
  240. {
  241. .getcaps = hpm_dao_getcaps,
  242. .configure = hpm_dao_configure,
  243. .init = hpm_dao_init,
  244. .start = hpm_dao_start,
  245. .stop = hpm_dao_stop,
  246. .transmit = hpm_dao_transmit,
  247. .buffer_info = hpm_dao_buffer_info,
  248. };
  249. ATTR_ALIGN(HPM_L1C_CACHELINE_SIZE) uint8_t dao_tx_fifo[DAO_FIFO_SIZE];
  250. int rt_hw_dao_init(void)
  251. {
  252. hpm_dao_dev.tx_fifo = dao_tx_fifo;
  253. hpm_dao_dev.audio.ops = &hpm_dao_ops;
  254. LOG_I("audio dao registered.\n");
  255. LOG_I("!!!Note: dao depends on i2s1, they share clock.\n");
  256. rt_audio_register(&hpm_dao_dev.audio, "dao", RT_DEVICE_FLAG_WRONLY, &hpm_dao_dev);
  257. return RT_EOK;
  258. }
  259. INIT_DEVICE_EXPORT(rt_hw_dao_init);
  260. #endif /* BSP_USING_DAO */