nu_i2s.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /**************************************************************************//**
  2. * @file i2s.c
  3. * @version V1.00
  4. * $Revision: 4 $
  5. * $Date: 18/08/05 2:12p $
  6. * @brief I2S driver source file
  7. *
  8. * @note
  9. * SPDX-License-Identifier: Apache-2.0
  10. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  11. *****************************************************************************/
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "nuc980.h"
  16. #include "nu_sys.h"
  17. #include "nu_i2s.h"
  18. /** @addtogroup Standard_Driver Standard Driver
  19. @{
  20. */
  21. /** @addtogroup I2S_Driver I2S Driver
  22. @{
  23. */
  24. /** @addtogroup I2S_EXPORTED_CONSTANTS I2S Exported Constants
  25. @{
  26. */
  27. /// @cond HIDDEN_SYMBOLS
  28. typedef uint32_t (AU_CB_FUNC_T)(uint32_t);
  29. static AU_CB_FUNC_T *g_fnPlayCallBack;
  30. static AU_CB_FUNC_T *g_fnRecCallBack;
  31. static uint8_t i2sOpened = 0;
  32. /// @endcond /* HIDDEN_SYMBOLS */
  33. /*@}*/ /* end of group ARM9_I2S_EXPORTED_CONSTANTS */
  34. /** @addtogroup ARM9_I2S_EXPORTED_FUNCTIONS I2S Exported Functions
  35. @{
  36. */
  37. /// @cond HIDDEN_SYMBOLS
  38. /**
  39. * @brief Start to play
  40. * @param None
  41. * @return None
  42. */
  43. static void i2sStartPlay(void)
  44. {
  45. /* start playing */
  46. printf("IIS start playing...\n");
  47. outpw(REG_I2S_PSR, 0x1);
  48. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) | (1 << 5));
  49. }
  50. /**
  51. * @brief Stop to play
  52. * @param None
  53. * @return None
  54. */
  55. static void i2sStopPlay(void)
  56. {
  57. printf("IIS stop playing\n");
  58. /* stop playing */
  59. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) & ~(1 << 5));
  60. }
  61. /**
  62. * @brief Start to record
  63. * @param None
  64. * @return None
  65. */
  66. static void i2sStartRecord(void)
  67. {
  68. /* start recording */
  69. printf("IIS start recording...\n");
  70. outpw(REG_I2S_RSR, 0x1);
  71. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) | (1 << 6));
  72. }
  73. /**
  74. * @brief Stop to record
  75. * @param None
  76. * @return None
  77. */
  78. static void i2sStopRecord(void)
  79. {
  80. printf("I2S stop recording\n");
  81. /* stop recording */
  82. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) & ~(1 << 6));
  83. }
  84. /**
  85. * @brief Delay function
  86. * @param None
  87. * @return None
  88. */
  89. static void Delay(int nCnt)
  90. {
  91. int volatile loop;
  92. for (loop = 0; loop < nCnt * 10; loop++);
  93. }
  94. /**
  95. * @brief Interrupt service routine for i2s
  96. * @param None
  97. * @return None
  98. */
  99. static void i2sISR(void)
  100. {
  101. uint8_t u8SN;
  102. if (inpw(REG_I2S_CON) & (1 << 10))
  103. {
  104. outpw(REG_I2S_CON, inpw(REG_I2S_CON) | (1 << 10)); //Clear TX INT
  105. if (inpw(REG_I2S_PSR) & (1 << 4))
  106. {
  107. outpw(REG_I2S_PSR, (1 << 4));
  108. printf("\ndebug:DMA_COUNTER_IRQ occur");
  109. }
  110. if (inpw(REG_I2S_PSR) & (1 << 3))
  111. {
  112. outpw(REG_I2S_PSR, (1 << 3));
  113. printf("\ndebug:DMA_DATA_ZERO_IRQ occur");
  114. }
  115. if (inpw(REG_I2S_PSR) & 0x1)
  116. {
  117. outpw(REG_I2S_PSR, 0x1);
  118. u8SN = (inpw(REG_I2S_PSR) >> 5) & 0x7;
  119. g_fnPlayCallBack(u8SN);
  120. }
  121. }
  122. if (inpw(REG_I2S_CON) & (1 << 11))
  123. {
  124. outpw(REG_I2S_CON, inpw(REG_I2S_CON) | (1 << 11)); //Clear RX INT
  125. if (inpw(REG_I2S_RSR) & 0x1)
  126. {
  127. outpw(REG_I2S_RSR, 0x1);
  128. u8SN = (inpw(REG_I2S_RSR) >> 5) & 0x7;
  129. g_fnRecCallBack(u8SN);
  130. }
  131. }
  132. }
  133. /// @endcond /* HIDDEN_SYMBOLS */
  134. /**
  135. * @brief Open i2s interface
  136. * @return open status
  137. * @retval I2S_ERR_BUSY error.
  138. * @retval 0 success.
  139. */
  140. int32_t i2sOpen(void)
  141. {
  142. if (i2sOpened)
  143. return I2S_ERR_BUSY;
  144. /* reset audio interface */
  145. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) | (1 << 16));
  146. Delay(100);
  147. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) & ~(1 << 16));
  148. Delay(100);
  149. /* reset IIS interface */
  150. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) | 0x1);
  151. Delay(100);
  152. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) & ~0x1);
  153. Delay(100);
  154. outpw(REG_I2S_CON, inpw(REG_I2S_CON) | (1 << 21) | (1 << 20));
  155. i2sOpened = 1;
  156. return 0;
  157. }
  158. /**
  159. * @brief Close i2s interface
  160. * @return None
  161. */
  162. void i2sClose(void)
  163. {
  164. // reset some variables
  165. i2sOpened = 0;
  166. g_fnPlayCallBack = NULL;
  167. g_fnRecCallBack = NULL;
  168. // reset i2s interface
  169. outpw(REG_SYS_AHBIPRST, inpw(REG_SYS_AHBIPRST) | (1 << 8));
  170. outpw(REG_SYS_AHBIPRST, inpw(REG_SYS_AHBIPRST) & ~(1 << 8));
  171. }
  172. /**
  173. * @brief Initialize i2s interface and setup interrupt
  174. * @return None
  175. */
  176. void i2sInit(void)
  177. {
  178. // enable i2s engine clock
  179. outpw(REG_CLK_HCLKEN, inpw(REG_CLK_HCLKEN) | (1 << 24));
  180. // enable interrupt and set ISR
  181. sysInstallISR(IRQ_LEVEL_1, IRQ_I2S, (PVOID)i2sISR);
  182. sysEnableInterrupt(IRQ_I2S);
  183. sysSetLocalInterrupt(ENABLE_IRQ);
  184. }
  185. /**
  186. * @brief IO control for i2s interface
  187. * @param[in] cmd command for io control, value could be
  188. * - \ref I2S_SET_PLAY
  189. * - \ref I2S_SET_RECORD
  190. * - \ref I2S_SELECT_BLOCK
  191. * - \ref I2S_SELECT_BIT
  192. * - \ref I2S_SET_PLAY_DMA_INT_SEL
  193. * - \ref I2S_SET_REC_DMA_INT_SEL
  194. * - \ref I2S_SET_ZEROCROSS
  195. * - \ref I2S_SET_DMACOUNTER
  196. * - \ref I2S_SET_CHANNEL
  197. * - \ref I2S_SET_MODE
  198. * - \ref I2S_SET_SPLITDATA
  199. * - \ref I2S_SET_DMA_ADDRESS
  200. * - \ref I2S_SET_DMA_LENGTH
  201. * - \ref I2S_GET_DMA_CUR_ADDRESS
  202. * - \ref I2S_SET_I2S_FORMAT
  203. * - \ref I2S_SET_I2S_CALLBACKFUN
  204. * - \ref I2S_SET_PCMSLOT
  205. * @param[in] arg0 argument 0 for io control
  206. * @param[in] arg1 argument 1 for io control
  207. * @retval I2S_ERR_IO error.
  208. * @retval 0 success.
  209. */
  210. int32_t i2sIoctl(uint32_t cmd, uint32_t arg0, uint32_t arg1)
  211. {
  212. uint32_t *buf;
  213. AU_CB_FUNC_T *ptr;
  214. switch (cmd)
  215. {
  216. // #define I2S_START_PLAY 0
  217. // #define I2S_STOP_PLAY 1
  218. case I2S_SET_PLAY:
  219. if (arg0 == I2S_START_PLAY)
  220. i2sStartPlay();
  221. else
  222. i2sStopPlay();
  223. break;
  224. // #define I2S_START_REC 0
  225. // #define I2S_STOP_REC 1
  226. case I2S_SET_RECORD:
  227. if (arg0 == I2S_START_REC)
  228. i2sStartRecord();
  229. else
  230. i2sStopRecord();
  231. break;
  232. // #define I2S_BLOCK_I2S 0
  233. // #define I2S_BLOCK_PCM 1
  234. case I2S_SELECT_BLOCK:
  235. if (arg0 == I2S_BLOCK_I2S)
  236. outpw(REG_I2S_CON, (inpw(REG_I2S_CON) & ~0x3) | 0x1);
  237. else
  238. outpw(REG_I2S_CON, (inpw(REG_I2S_CON) & ~0x3) | 0x2);
  239. break;
  240. // #define I2S_BIT_WIDTH_8 0
  241. // #define I2S_BIT_WIDTH_16 1
  242. // #define I2S_BIT_WIDTH_24 2
  243. case I2S_SELECT_BIT:
  244. outpw(REG_I2S_CON, (inpw(REG_I2S_CON) & ~0x300) | (arg0 << 8));
  245. break;
  246. // #define I2S_DMA_INT_END 0
  247. // #define I2S_DMA_INT_HALF 1
  248. // #define I2S_DMA_INT_QUARTER 2
  249. // #define I2S_DMA_INT_EIGTH 3
  250. case I2S_SET_PLAY_DMA_INT_SEL:
  251. outpw(REG_I2S_CON, (inpw(REG_I2S_CON) & ~0x3000) | (arg0 << 12));
  252. break;
  253. case I2S_SET_REC_DMA_INT_SEL:
  254. outpw(REG_I2S_CON, (inpw(REG_I2S_CON) & ~0xc000) | (arg0 << 14));
  255. break;
  256. case I2S_SET_ZEROCROSS:
  257. if (arg0 == I2S_ENABLE)
  258. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) | 0x8);
  259. else
  260. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) & ~0x8);
  261. break;
  262. case I2S_SET_DMACOUNTER:
  263. if (arg0 == I2S_ENABLE)
  264. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) | 0x10);
  265. else
  266. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) & ~0x10);
  267. break;
  268. // #define I2S_CHANNEL_I2S_ONE 2
  269. // #define I2S_CHANNEL_I2S_TWO 3
  270. // #define I2S_CHANNEL_PCM_TWO 3
  271. // #define I2S_CHANNEL_PCM_TWO_SLOT1 0
  272. // #define I2S_CHANNEL_PCM_TWO_SLOT0 1
  273. // #define I2S_CHANNEL_PCM_ONE_SLOT0 2
  274. case I2S_SET_CHANNEL:
  275. if (arg0 == I2S_PLAY)
  276. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) & ~(0x3 << 12) | (arg1 << 12));
  277. else
  278. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) & ~(0x3 << 14) | (arg1 << 14));
  279. break;
  280. // #define I2S_MODE_MASTER 0
  281. // #define I2S_MODE_SLAVE 1
  282. case I2S_SET_MODE:
  283. if (arg0 == I2S_MODE_MASTER)
  284. outpw(REG_I2S_I2SCON, inpw(REG_I2S_I2SCON) & ~(0x1 << 20));
  285. else
  286. outpw(REG_I2S_I2SCON, inpw(REG_I2S_I2SCON) | (0x1 << 20));
  287. break;
  288. case I2S_SET_SPLITDATA:
  289. if (arg0 == I2S_ENABLE)
  290. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) | (0x1 << 20));
  291. else
  292. outpw(REG_I2S_RESET, inpw(REG_I2S_RESET) & ~(0x1 << 20));
  293. break;
  294. case I2S_SET_DMA_ADDRESS:
  295. if (arg0 == I2S_PLAY)
  296. outpw(REG_I2S_PDESB, arg1 | 0x80000000);
  297. else if (arg0 == I2S_REC)
  298. outpw(REG_I2S_RDESB, arg1 | 0x80000000);
  299. else if (arg0 == PCM_PLAY)
  300. outpw(REG_I2S_PDESB2, arg1 | 0x80000000);
  301. else
  302. outpw(REG_I2S_RDESB2, arg1 | 0x80000000);
  303. break;
  304. case I2S_SET_DMA_LENGTH:
  305. if (arg0 == I2S_PLAY)
  306. outpw(REG_I2S_PDES_LENGTH, arg1);
  307. else
  308. outpw(REG_I2S_RDES_LENGTH, arg1);
  309. break;
  310. case I2S_GET_DMA_CUR_ADDRESS:
  311. buf = (uint32_t *)arg0;
  312. if (arg0 == I2S_PLAY)
  313. *buf = inpw(REG_I2S_PDESC);
  314. else
  315. *buf = inpw(REG_I2S_RDESC);
  316. break;
  317. // #define I2S_FORMAT_I2S 0
  318. // #define I2S_FORMAT_MSB 1
  319. case I2S_SET_I2S_FORMAT:
  320. if (arg0 == I2S_FORMAT_I2S)
  321. outpw(REG_I2S_I2SCON, inpw(REG_I2S_I2SCON) & ~ 0x8);
  322. else
  323. outpw(REG_I2S_I2SCON, inpw(REG_I2S_I2SCON) | 0x8);
  324. break;
  325. case I2S_SET_I2S_CALLBACKFUN:
  326. ptr = (AU_CB_FUNC_T *)arg1;
  327. if (arg0 == I2S_PLAY)
  328. g_fnPlayCallBack = ptr;
  329. else
  330. g_fnRecCallBack = ptr;
  331. break;
  332. // #define PCM_SLOT1_IN 0
  333. // #define PCM_SLOT1_OUT 1
  334. // #define PCM_SLOT2_IN 2
  335. // #define PCM_SLOT2_OUT 3
  336. case I2S_SET_PCMSLOT:
  337. if (arg0 == PCM_SLOT1_IN)
  338. outpw(REG_I2S_PCMS1ST, (inpw(REG_I2S_PCMS1ST) & ~0x3ff) | (arg1 & 0x3ff));
  339. else if (arg0 == PCM_SLOT1_OUT)
  340. outpw(REG_I2S_PCMS1ST, (inpw(REG_I2S_PCMS1ST) & ~0x3ff0000) | ((arg1 & 0x3ff) << 16));
  341. else if (arg0 == PCM_SLOT2_IN)
  342. outpw(REG_I2S_PCMS2ST, (inpw(REG_I2S_PCMS2ST) & ~0x3ff) | (arg1 & 0x3ff));
  343. else
  344. outpw(REG_I2S_PCMS2ST, (inpw(REG_I2S_PCMS2ST) & ~0x3ff0000) | ((arg1 & 0x3ff) << 16));
  345. break;
  346. case I2S_SET_PCM_FS_PERIOD:
  347. outpw(REG_I2S_PCMCON, (inpw(REG_I2S_PCMCON) & ~0x03FF0000 | (((arg0 - 1) & 0x3ff) << 16)));
  348. break;
  349. default:
  350. return I2S_ERR_IO;
  351. }
  352. return 0;
  353. }
  354. /**
  355. * @brief Configure sampling rate for audio
  356. * @param[in] u32SourceClockRate source speed to i2s interface
  357. * @param[in] u32SampleRate sampling rate
  358. * @param[in] u32DataBit data width
  359. * @param[in] u32Channel channel number
  360. * @return None
  361. */
  362. void i2sSetSampleRate(uint32_t u32SourceClockRate, uint32_t u32SampleRate, uint32_t u32DataBit, uint32_t u32Channel)
  363. {
  364. uint32_t u32BCLKDiv;
  365. uint32_t u32MCLK, u32MCLKDiv;
  366. u32MCLK = (u32SampleRate * 256);
  367. u32MCLKDiv = u32SourceClockRate / u32MCLK;
  368. outpw(REG_I2S_I2SCON, (inpw(REG_I2S_I2SCON) & ~0x000F0000) | (u32MCLKDiv - 1) << 16);
  369. u32BCLKDiv = u32MCLK / (u32SampleRate * u32DataBit * u32Channel);
  370. u32BCLKDiv = u32BCLKDiv / 2 - 1;
  371. outpw(REG_I2S_I2SCON, (inpw(REG_I2S_I2SCON) & ~0xF0) | u32BCLKDiv << 5);
  372. }
  373. /**
  374. * @brief Configure MCLK frequency (master mode)
  375. * @param[in] u32SourceClockRate source clock rate
  376. * @param[in] u32SampleRate sampling rate
  377. * @return None
  378. */
  379. void i2sSetMCLKFrequency(uint32_t u32SourceClockRate, uint32_t u32SampleRate)
  380. {
  381. uint32_t u32MCLK, u32MCLKDiv;
  382. u32MCLK = (u32SampleRate * 256);
  383. u32MCLKDiv = u32SourceClockRate / u32MCLK;
  384. outpw(REG_I2S_I2SCON, (inpw(REG_I2S_I2SCON) & ~0x000F0000) | (u32MCLKDiv - 1) << 16);
  385. }
  386. /**
  387. * @brief Configure PCM BCLK frequency (master mode)
  388. * @param[in] u32SourceClockRate source clock rate
  389. * @param[in] u32Rate target rate
  390. * @return None
  391. */
  392. void i2sSetPCMBCLKFrequency(uint32_t u32SourceClockRate, uint32_t u32Rate)
  393. {
  394. uint32_t u32BCLKDiv;
  395. u32BCLKDiv = (u32SourceClockRate / (2 * u32Rate)) - 1;
  396. outpw(REG_I2S_PCMCON, (inpw(REG_I2S_PCMCON) & ~0x0000FF00) | (u32BCLKDiv << 8));
  397. }
  398. /*@}*/ /* end of group I2S_EXPORTED_FUNCTIONS */
  399. /*@}*/ /* end of group I2S_Driver */
  400. /*@}*/ /* end of group Standard_Driver */
  401. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/