drv_i2s.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-12-12 Wayne Lin First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(BSP_USING_I2S)
  14. #include <rtdevice.h>
  15. #include <drv_i2s.h>
  16. #include "NuMicro.h"
  17. /* Private define ---------------------------------------------------------------*/
  18. #define DBG_ENABLE
  19. #define DBG_LEVEL DBG_LOG
  20. #define DBG_SECTION_NAME "i2s"
  21. #define DBG_COLOR
  22. #include <rtdbg.h>
  23. #define I2S_RSR_R_DMA_RIA_IRQ_Pos (0)
  24. #define I2S_RSR_R_DMA_RIA_IRQ_Msk (1<<I2S_RSR_R_DMA_RIA_IRQ_Pos)
  25. #define I2S_RSR_R_DMA_RIA_SN_Pos (5)
  26. #define I2S_RSR_R_DMA_RIA_SN_Msk (7<<I2S_RSR_R_DMA_RIA_SN_Pos)
  27. #define I2S_GLBCON_P_DMA_IRQ_Pos (10)
  28. #define I2S_GLBCON_P_DMA_IRQ_Msk (1<<I2S_GLBCON_P_DMA_IRQ_Pos)
  29. #define I2S_GLBCON_R_DMA_IRQ_Pos (11)
  30. #define I2S_GLBCON_R_DMA_IRQ_Msk (1<<I2S_GLBCON_R_DMA_IRQ_Pos)
  31. #define I2S_PSP_P_DMA_RIA_I_Pos (0)
  32. #define I2S_PSP_P_DMA_RIA_I_Msk (1<<I2S_PSP_P_DMA_RIA_I_Pos)
  33. #define I2S_PSP_DMA_DATA_ZERO_IRQ_Pos (3)
  34. #define I2S_PSP_DMA_DATA_ZERO_IRQ_Msk (1<<I2S_PSP_DMA_DATA_ZERO_IRQ_Pos)
  35. #define I2S_PSP_DMA_CNTER_IRQ_Pos (4)
  36. #define I2S_PSP_DMA_CNTER_IRQ_Msk (1<<I2S_PSP_DMA_CNTER_IRQ_Pos)
  37. #define I2S_RESET_PLAY_Pos (5)
  38. #define I2S_RESET_PLAY_Msk (1<<I2S_RESET_PLAY_Pos)
  39. #define I2S_RESET_RECORD_Pos (6)
  40. #define I2S_RESET_RECORD_Msk (1<<I2S_RESET_RECORD_Pos)
  41. /* Private functions ------------------------------------------------------------*/
  42. static rt_err_t nu_i2s_getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps);
  43. static rt_err_t nu_i2s_configure(struct rt_audio_device *audio, struct rt_audio_caps *caps);
  44. static rt_err_t nu_i2s_init(struct rt_audio_device *audio);
  45. static rt_err_t nu_i2s_start(struct rt_audio_device *audio, int stream);
  46. static rt_err_t nu_i2s_stop(struct rt_audio_device *audio, int stream);
  47. static void nu_i2s_buffer_info(struct rt_audio_device *audio, struct rt_audio_buf_info *info);
  48. /* Public functions -------------------------------------------------------------*/
  49. rt_err_t nu_i2s_acodec_register(nu_acodec_ops_t);
  50. /* Private variables ------------------------------------------------------------*/
  51. static struct nu_i2s g_nu_i2s_dev =
  52. {
  53. .name = "sound0",
  54. .irqn = IRQ_ACTL,
  55. .rstidx = I2SRST,
  56. .clkidx = I2SCKEN,
  57. };
  58. static void nu_i2s_isr(int vector, void *param)
  59. {
  60. nu_i2s_t psNuI2s = (nu_i2s_t)param;
  61. volatile uint32_t u32RegAudCtl = inpw(REG_ACTL_CON);
  62. if (u32RegAudCtl & I2S_GLBCON_P_DMA_IRQ_Msk)
  63. {
  64. volatile uint32_t u32RegPlayStatus = inpw(REG_ACTL_PSR);
  65. outpw(REG_ACTL_CON, u32RegAudCtl | I2S_GLBCON_P_DMA_IRQ_Msk); //Clear TX INT
  66. if (u32RegPlayStatus & I2S_PSP_DMA_CNTER_IRQ_Msk)
  67. {
  68. outpw(REG_ACTL_PSR, I2S_PSP_DMA_CNTER_IRQ_Msk);
  69. rt_kprintf("\ndebug:DMA_COUNTER_IRQ occur\n");
  70. }
  71. if (u32RegPlayStatus & I2S_PSP_DMA_DATA_ZERO_IRQ_Msk)
  72. {
  73. outpw(REG_ACTL_PSR, I2S_PSP_DMA_DATA_ZERO_IRQ_Msk);
  74. rt_kprintf("\ndebug:DMA_DATA_ZERO_IRQ occur\n");
  75. }
  76. if (u32RegPlayStatus & I2S_PSP_P_DMA_RIA_I_Msk)
  77. {
  78. /* Clear Playback status of DMA reach indicate address interrupt. */
  79. outpw(REG_ACTL_PSR, I2S_PSP_P_DMA_RIA_I_Msk);
  80. rt_audio_tx_complete(&psNuI2s->audio);
  81. }
  82. }
  83. if (u32RegAudCtl & I2S_GLBCON_R_DMA_IRQ_Msk)
  84. {
  85. volatile uint32_t u32RegRecordStatus = inpw(REG_ACTL_RSR);
  86. outpw(REG_ACTL_CON, u32RegAudCtl | I2S_GLBCON_R_DMA_IRQ_Msk); //Clear RX INT
  87. /* Record DMA Reach Indicative Address Interrupt Request Bit */
  88. /* 0 = Record DMA address does not reach the indicative address by R_DMA_IRQ_SEL */
  89. /* 1 = Record DMA address does reach the indicative address by R_DMA_IRQ_SEL */
  90. /* Note: This bit is readable, and can only be cleared by writing '1' to it. */
  91. if (u32RegRecordStatus & I2S_RSR_R_DMA_RIA_IRQ_Msk)
  92. {
  93. nu_i2s_dai_t psNuI2sDai = &psNuI2s->i2s_dais[NU_I2S_DAI_CAPTURE];
  94. /*
  95. Record DMA Reach Indicative Address Section Number Bit (Read Only)
  96. R_DMA_IRQ_SEL (I2S_GLBCON[15:14]) = 01, R_DMA_RIA_SN[2:0]= 1, 0.
  97. R_DMA_IRQ_SEL (I2S_GLBCON[15:14]) = 10, R_DMA_RIA_SN[2:0]= 1, 2, 3, 0.
  98. R_DMA_IRQ_SEL (I2S_GLBCON[15:14]) = 11, R_DMA_RIA_SN[2:0]= 1, 2, 3, 4, 5, 6, 7, 0.
  99. */
  100. uint8_t u8FifoBlockIdx = (u32RegRecordStatus & I2S_RSR_R_DMA_RIA_SN_Msk) >> I2S_RSR_R_DMA_RIA_SN_Pos;
  101. rt_uint8_t *pbuf = (uint8_t *)((uint32_t)&psNuI2sDai->fifo[u8FifoBlockIdx * NU_I2S_DMA_BUF_BLOCK_SIZE] | NONCACHEABLE);
  102. /* Clear Record status of DMA reach indicate address interrupt. */
  103. outpw(REG_ACTL_RSR, I2S_RSR_R_DMA_RIA_IRQ_Msk);
  104. /* Report upper layer. */
  105. rt_audio_rx_done(&psNuI2s->audio, pbuf, NU_I2S_DMA_BUF_BLOCK_SIZE);
  106. }
  107. }
  108. }
  109. static rt_bool_t nu_i2s_capacity_check(struct rt_audio_configure *pconfig)
  110. {
  111. switch (pconfig->samplebits)
  112. {
  113. case 8:
  114. case 16:
  115. case 24:
  116. break;
  117. default:
  118. goto exit_nu_i2s_capacity_check;
  119. }
  120. switch (pconfig->channels)
  121. {
  122. case 1:
  123. case 2:
  124. break;
  125. default:
  126. goto exit_nu_i2s_capacity_check;
  127. }
  128. return RT_TRUE;
  129. exit_nu_i2s_capacity_check:
  130. return RT_FALSE;
  131. }
  132. static rt_err_t nu_i2s_dai_setup(nu_i2s_t psNuI2s, struct rt_audio_configure *pconfig)
  133. {
  134. rt_err_t result = RT_EOK;
  135. nu_acodec_ops_t pNuACodecOps = RT_NULL;
  136. RT_ASSERT(psNuI2s->AcodecOps != RT_NULL);
  137. pNuACodecOps = psNuI2s->AcodecOps;
  138. /* Open I2S */
  139. if (nu_i2s_capacity_check(pconfig) == RT_TRUE)
  140. {
  141. /* Reset audio codec */
  142. if (pNuACodecOps->nu_acodec_reset)
  143. result = pNuACodecOps->nu_acodec_reset();
  144. if (result != RT_EOK)
  145. goto exit_nu_i2s_dai_setup;
  146. /* Setup audio codec */
  147. if (pNuACodecOps->nu_acodec_init)
  148. result = pNuACodecOps->nu_acodec_init();
  149. if (!pNuACodecOps->nu_acodec_init || result != RT_EOK)
  150. goto exit_nu_i2s_dai_setup;
  151. /* Setup acodec samplerate/samplebit/channel */
  152. if (pNuACodecOps->nu_acodec_dsp_control)
  153. result = pNuACodecOps->nu_acodec_dsp_control(pconfig);
  154. if (!pNuACodecOps->nu_acodec_dsp_control || result != RT_EOK)
  155. goto exit_nu_i2s_dai_setup;
  156. /* Open I2S */
  157. if (i2sOpen() != 0)
  158. goto exit_nu_i2s_dai_setup;
  159. /* Select I2S function */
  160. i2sIoctl(I2S_SELECT_BLOCK, I2S_BLOCK_I2S, 0);
  161. /* Select Data width */
  162. i2sIoctl(I2S_SELECT_BIT, ((pconfig->samplebits / 8) - 1), 0);
  163. if (pconfig->channels > 1)
  164. {
  165. /* Set to stereo */
  166. i2sIoctl(I2S_SET_CHANNEL, I2S_PLAY, I2S_CHANNEL_P_I2S_TWO);
  167. i2sIoctl(I2S_SET_CHANNEL, I2S_REC, I2S_CHANNEL_R_I2S_TWO);
  168. }
  169. else
  170. {
  171. /* Set to mono */
  172. i2sIoctl(I2S_SET_CHANNEL, I2S_PLAY, I2S_CHANNEL_P_I2S_ONE);
  173. i2sIoctl(I2S_SET_CHANNEL, I2S_REC, I2S_CHANNEL_R_I2S_LEFT_PCM_SLOT0);
  174. }
  175. /* Set DMA interrupt selection to half of DMA buffer */
  176. switch (NU_I2S_DMA_BUF_BLOCK_NUMBER)
  177. {
  178. case 2:
  179. i2sIoctl(I2S_SET_PLAY_DMA_INT_SEL, I2S_DMA_INT_HALF, 0);
  180. i2sIoctl(I2S_SET_REC_DMA_INT_SEL, I2S_DMA_INT_HALF, 0);
  181. break;
  182. case 4:
  183. i2sIoctl(I2S_SET_PLAY_DMA_INT_SEL, I2S_DMA_INT_QUARTER, 0);
  184. i2sIoctl(I2S_SET_REC_DMA_INT_SEL, I2S_DMA_INT_QUARTER, 0);
  185. break;
  186. case 8:
  187. i2sIoctl(I2S_SET_PLAY_DMA_INT_SEL, I2S_DMA_INT_EIGHTH, 0);
  188. i2sIoctl(I2S_SET_REC_DMA_INT_SEL, I2S_DMA_INT_EIGHTH, 0);
  189. break;
  190. default:
  191. RT_ASSERT(0);
  192. break;
  193. }
  194. /* Set DMA buffer address */
  195. i2sIoctl(I2S_SET_DMA_ADDRESS, I2S_PLAY, (uint32_t)&psNuI2s->i2s_dais[NU_I2S_DAI_PLAYBACK].fifo[0]);
  196. i2sIoctl(I2S_SET_DMA_ADDRESS, I2S_REC, (uint32_t)&psNuI2s->i2s_dais[NU_I2S_DAI_CAPTURE].fifo[0]);
  197. /* Set DMA buffer length */
  198. i2sIoctl(I2S_SET_DMA_LENGTH, I2S_PLAY, NU_I2S_DMA_FIFO_SIZE);
  199. i2sIoctl(I2S_SET_DMA_LENGTH, I2S_REC, NU_I2S_DMA_FIFO_SIZE);
  200. /* Select I2S format */
  201. i2sIoctl(I2S_SET_I2S_FORMAT, I2S_FORMAT_I2S, 0);
  202. if (psNuI2s->AcodecOps->role == NU_ACODEC_ROLE_MASTER)
  203. {
  204. // Set as slave, source clock is XIN (12MHz)
  205. i2sIoctl(I2S_SET_MODE, I2S_MODE_SLAVE, 0);
  206. }
  207. else
  208. {
  209. if (pconfig->samplerate % 11025)
  210. {
  211. // 12.288MHz ==> APLL=98.4MHz / 8 = 12.3MHz
  212. // APLL is 98.4MHz
  213. /*
  214. FB_DV = 0x28 -> N=FB_DV+1 -> N=41
  215. IN_DV = 0 -> M=IN_DV+1 -> M=1
  216. OUT_DV = 4 -> P=4+1 -> P=5
  217. Fpllout = 12MHz * N / (M*P) -> Fpllout = 12MHz * 41 / (5*1) = 98.4 MHz
  218. */
  219. outpw(REG_CLK_APLLCON, 0xC0008028);
  220. // Select APLL as I2S source and divider is (7+1)
  221. outpw(REG_CLK_DIVCTL1, (inpw(REG_CLK_DIVCTL1) & ~0x001f0000) | (0x2 << 19) | (0x7 << 24));
  222. // Set sampleing rate, data width, channel
  223. i2sSetSampleRate(12300000, pconfig->samplerate, pconfig->samplebits, pconfig->channels);
  224. }
  225. else
  226. {
  227. // 11.2896MHz ==> APLL=90MHz / 8 = 11.25MHz
  228. // APLL is 90MHz
  229. /*
  230. FB_DV = 0x2D -> N=FB_DV+1 -> N=45
  231. IN_DV = 0 -> M=IN_DV+1 -> M=1
  232. OUT_DV = 5 -> P=5+1 -> P=6
  233. Fpllout = 12MHz * N / (M*P) -> Fpllout = 12MHz * 45 / (6*1) = 90 MHz
  234. */
  235. outpw(REG_CLK_APLLCON, 0xC000A02D);
  236. // Select APLL as I2S source and divider is (7+1)
  237. outpw(REG_CLK_DIVCTL1, (inpw(REG_CLK_DIVCTL1) & ~0x001f0000) | (0x2 << 19) | (0x7 << 24));
  238. // Set sampleing rate, data width, channel
  239. i2sSetSampleRate(11250000, pconfig->samplerate, pconfig->samplebits, pconfig->channels);
  240. }
  241. // Set as master
  242. i2sIoctl(I2S_SET_MODE, I2S_MODE_MASTER, 0);
  243. }
  244. LOG_I("Open I2S.");
  245. /* Set unmute */
  246. if (pNuACodecOps->nu_acodec_mixer_control)
  247. pNuACodecOps->nu_acodec_mixer_control(AUDIO_MIXER_MUTE, RT_FALSE);
  248. }
  249. else
  250. result = -RT_EINVAL;
  251. exit_nu_i2s_dai_setup:
  252. return result;
  253. }
  254. static rt_err_t nu_i2s_getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  255. {
  256. rt_err_t result = RT_EOK;
  257. nu_i2s_t psNuI2s;
  258. nu_acodec_ops_t pNuACodecOps = RT_NULL;
  259. RT_ASSERT(audio != RT_NULL);
  260. RT_ASSERT(caps != RT_NULL);
  261. psNuI2s = (nu_i2s_t)audio;
  262. RT_ASSERT(psNuI2s->AcodecOps != RT_NULL);
  263. pNuACodecOps = psNuI2s->AcodecOps;
  264. switch (caps->main_type)
  265. {
  266. case AUDIO_TYPE_QUERY:
  267. switch (caps->sub_type)
  268. {
  269. case AUDIO_TYPE_QUERY:
  270. caps->udata.mask = AUDIO_TYPE_INPUT | AUDIO_TYPE_OUTPUT | AUDIO_TYPE_MIXER;
  271. break;
  272. default:
  273. result = -RT_ERROR;
  274. break;
  275. } // switch (caps->sub_type)
  276. break;
  277. case AUDIO_TYPE_MIXER:
  278. if (pNuACodecOps->nu_acodec_mixer_query)
  279. {
  280. switch (caps->sub_type)
  281. {
  282. case AUDIO_MIXER_QUERY:
  283. return pNuACodecOps->nu_acodec_mixer_query(AUDIO_MIXER_QUERY, &caps->udata.mask);
  284. default:
  285. return pNuACodecOps->nu_acodec_mixer_query(caps->sub_type, (rt_uint32_t *)&caps->udata.value);
  286. } // switch (caps->sub_type)
  287. } // if (pNuACodecOps->nu_acodec_mixer_query)
  288. result = -RT_ERROR;
  289. break;
  290. case AUDIO_TYPE_INPUT:
  291. case AUDIO_TYPE_OUTPUT:
  292. switch (caps->sub_type)
  293. {
  294. case AUDIO_DSP_PARAM:
  295. caps->udata.config.channels = psNuI2s->config.channels;
  296. caps->udata.config.samplebits = psNuI2s->config.samplebits;
  297. caps->udata.config.samplerate = psNuI2s->config.samplerate;
  298. break;
  299. case AUDIO_DSP_SAMPLERATE:
  300. caps->udata.config.samplerate = psNuI2s->config.samplerate;
  301. break;
  302. case AUDIO_DSP_CHANNELS:
  303. caps->udata.config.channels = psNuI2s->config.channels;
  304. break;
  305. case AUDIO_DSP_SAMPLEBITS:
  306. caps->udata.config.samplebits = psNuI2s->config.samplebits;
  307. break;
  308. default:
  309. result = -RT_ERROR;
  310. break;
  311. } // switch (caps->sub_type)
  312. break;
  313. default:
  314. result = -RT_ERROR;
  315. break;
  316. } // switch (caps->main_type)
  317. return result;
  318. }
  319. static rt_err_t nu_i2s_configure(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  320. {
  321. rt_err_t result = RT_EOK;
  322. nu_i2s_t psNuI2s;
  323. nu_acodec_ops_t pNuACodecOps = RT_NULL;
  324. int stream = -1;
  325. RT_ASSERT(audio != RT_NULL);
  326. RT_ASSERT(caps != RT_NULL);
  327. psNuI2s = (nu_i2s_t)audio;
  328. RT_ASSERT(psNuI2s->AcodecOps != RT_NULL);
  329. pNuACodecOps = psNuI2s->AcodecOps;
  330. switch (caps->main_type)
  331. {
  332. case AUDIO_TYPE_MIXER:
  333. if (psNuI2s->AcodecOps->nu_acodec_mixer_control)
  334. psNuI2s->AcodecOps->nu_acodec_mixer_control(caps->sub_type, caps->udata.value);
  335. break;
  336. case AUDIO_TYPE_INPUT:
  337. stream = AUDIO_STREAM_RECORD;
  338. case AUDIO_TYPE_OUTPUT:
  339. {
  340. rt_bool_t bNeedReset = RT_FALSE;
  341. if (stream < 0)
  342. stream = AUDIO_STREAM_REPLAY;
  343. switch (caps->sub_type)
  344. {
  345. case AUDIO_DSP_PARAM:
  346. if (rt_memcmp(&psNuI2s->config, &caps->udata.config, sizeof(struct rt_audio_configure)) != 0)
  347. {
  348. rt_memcpy(&psNuI2s->config, &caps->udata.config, sizeof(struct rt_audio_configure));
  349. bNeedReset = RT_TRUE;
  350. }
  351. break;
  352. case AUDIO_DSP_SAMPLEBITS:
  353. if (psNuI2s->config.samplerate != caps->udata.config.samplebits)
  354. {
  355. psNuI2s->config.samplerate = caps->udata.config.samplebits;
  356. bNeedReset = RT_TRUE;
  357. }
  358. break;
  359. case AUDIO_DSP_CHANNELS:
  360. if (psNuI2s->config.channels != caps->udata.config.channels)
  361. {
  362. pNuACodecOps->config.channels = caps->udata.config.channels;
  363. bNeedReset = RT_TRUE;
  364. }
  365. break;
  366. case AUDIO_DSP_SAMPLERATE:
  367. if (psNuI2s->config.samplerate != caps->udata.config.samplerate)
  368. {
  369. psNuI2s->config.samplerate = caps->udata.config.samplerate;
  370. bNeedReset = RT_TRUE;
  371. }
  372. break;
  373. default:
  374. result = -RT_ERROR;
  375. break;
  376. } // switch (caps->sub_type)
  377. if (bNeedReset)
  378. {
  379. return nu_i2s_start(audio, stream);
  380. }
  381. }
  382. break;
  383. default:
  384. result = -RT_ERROR;
  385. break;
  386. } // switch (caps->main_type)
  387. return result;
  388. }
  389. static rt_err_t nu_i2s_init(struct rt_audio_device *audio)
  390. {
  391. rt_err_t result = RT_EOK;
  392. nu_i2s_t psNuI2s;
  393. RT_ASSERT(audio != RT_NULL);
  394. psNuI2s = (nu_i2s_t)audio;
  395. /* Enable IP engine clock */
  396. nu_sys_ipclk_enable(psNuI2s->clkidx);
  397. /* Reset IP engine */
  398. nu_sys_ip_reset(psNuI2s->rstidx);
  399. /* Enable interrupt */
  400. rt_hw_interrupt_umask(psNuI2s->irqn);
  401. return -(result);
  402. }
  403. static rt_err_t nu_i2s_start(struct rt_audio_device *audio, int stream)
  404. {
  405. nu_i2s_t psNuI2s;
  406. RT_ASSERT(audio != RT_NULL);
  407. psNuI2s = (nu_i2s_t)audio;
  408. /* Restart all: I2S and codec. */
  409. nu_i2s_stop(audio, stream);
  410. if (nu_i2s_dai_setup(psNuI2s, &psNuI2s->config) != RT_EOK)
  411. return -RT_ERROR;
  412. switch (stream)
  413. {
  414. case AUDIO_STREAM_REPLAY:
  415. {
  416. i2sIoctl(I2S_SET_PLAY, I2S_START_PLAY, 0);
  417. LOG_I("Start replay.");
  418. }
  419. break;
  420. case AUDIO_STREAM_RECORD:
  421. {
  422. i2sIoctl(I2S_SET_RECORD, I2S_START_REC, 0);
  423. LOG_I("Start record.");
  424. }
  425. break;
  426. default:
  427. return -RT_ERROR;
  428. }
  429. return RT_EOK;
  430. }
  431. static rt_err_t nu_i2s_stop(struct rt_audio_device *audio, int stream)
  432. {
  433. nu_i2s_t psNuI2s;
  434. nu_i2s_dai_t psNuI2sDai = RT_NULL;
  435. RT_ASSERT(audio != RT_NULL);
  436. psNuI2s = (nu_i2s_t)audio;
  437. switch (stream)
  438. {
  439. case AUDIO_STREAM_REPLAY:
  440. psNuI2sDai = &psNuI2s->i2s_dais[NU_I2S_DAI_PLAYBACK];
  441. i2sIoctl(I2S_SET_PLAY, I2S_STOP_PLAY, 0);
  442. LOG_I("Stop replay.");
  443. break;
  444. case AUDIO_STREAM_RECORD:
  445. psNuI2sDai = &psNuI2s->i2s_dais[NU_I2S_DAI_CAPTURE];
  446. i2sIoctl(I2S_SET_RECORD, I2S_STOP_REC, 0);
  447. LOG_I("Stop record.");
  448. break;
  449. default:
  450. return -RT_EINVAL;
  451. }
  452. /* Close I2S if record and playback path. */
  453. if (!((inpw(REG_ACTL_RESET)&I2S_RESET_PLAY_Msk) || (inpw(REG_ACTL_RESET)&I2S_RESET_RECORD_Msk)))
  454. {
  455. i2sClose();
  456. LOG_I("Close I2S.");
  457. }
  458. /* Silence */
  459. rt_memset((void *)psNuI2sDai->fifo, 0, NU_I2S_DMA_FIFO_SIZE);
  460. return RT_EOK;
  461. }
  462. static void nu_i2s_buffer_info(struct rt_audio_device *audio, struct rt_audio_buf_info *info)
  463. {
  464. nu_i2s_t psNuI2s;
  465. RT_ASSERT(audio != RT_NULL);
  466. RT_ASSERT(info != RT_NULL);
  467. psNuI2s = (nu_i2s_t)audio;
  468. /* Define it a NONCACHEABLE address. */
  469. info->buffer = (rt_uint8_t *)((uint32_t)psNuI2s->i2s_dais[NU_I2S_DAI_PLAYBACK].fifo | NONCACHEABLE) ;
  470. info->total_size = NU_I2S_DMA_FIFO_SIZE;
  471. info->block_size = NU_I2S_DMA_BUF_BLOCK_SIZE;
  472. info->block_count = NU_I2S_DMA_BUF_BLOCK_NUMBER;
  473. return;
  474. }
  475. static struct rt_audio_ops nu_i2s_audio_ops =
  476. {
  477. .getcaps = nu_i2s_getcaps,
  478. .configure = nu_i2s_configure,
  479. .init = nu_i2s_init,
  480. .start = nu_i2s_start,
  481. .stop = nu_i2s_stop,
  482. .transmit = RT_NULL,
  483. .buffer_info = nu_i2s_buffer_info
  484. };
  485. int rt_hw_i2s_init(void)
  486. {
  487. int i = 0;
  488. nu_i2s_dai_t psNuI2sDai;
  489. for (i = 0; i < NU_I2S_DAI_CNT; i++)
  490. {
  491. psNuI2sDai = &g_nu_i2s_dev.i2s_dais[i];
  492. /* Allocate playback and record FIFO buffer. */
  493. psNuI2sDai->fifo = (uint8_t *)rt_malloc_align(NU_I2S_DMA_FIFO_SIZE, 32);
  494. RT_ASSERT(psNuI2sDai->fifo != RT_NULL);
  495. rt_memset(psNuI2sDai->fifo, 0, NU_I2S_DMA_FIFO_SIZE);
  496. }
  497. /* Register ops of audio device */
  498. g_nu_i2s_dev.audio.ops = &nu_i2s_audio_ops;
  499. /* Register device, RW: it is with replay and record functions. */
  500. rt_audio_register(&g_nu_i2s_dev.audio, g_nu_i2s_dev.name, RT_DEVICE_FLAG_RDWR, &g_nu_i2s_dev);
  501. /* Register I2S ISR */
  502. rt_hw_interrupt_install(g_nu_i2s_dev.irqn, nu_i2s_isr, &g_nu_i2s_dev, g_nu_i2s_dev.name);
  503. return RT_EOK;
  504. }
  505. INIT_DEVICE_EXPORT(rt_hw_i2s_init);
  506. #endif //#if defined(BSP_USING_I2S)