My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

lpc17xx_i2s.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /**********************************************************************
  2. * $Id$ lpc17xx_i2s.c 2010-09-23
  3. *//**
  4. * @file lpc17xx_i2s.c
  5. * @brief Contains all functions support for I2S firmware
  6. * library on LPC17xx
  7. * @version 3.1
  8. * @date 23. Sep. 2010
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2010, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @addtogroup I2S
  34. * @{
  35. */
  36. /* Includes ------------------------------------------------------------------- */
  37. #include "lpc17xx_i2s.h"
  38. #include "lpc17xx_clkpwr.h"
  39. /* If this source file built with example, the LPC17xx FW library configuration
  40. * file in each example directory ("lpc17xx_libcfg.h") must be included,
  41. * otherwise the default FW library configuration file must be included instead
  42. */
  43. #ifdef __BUILD_WITH_EXAMPLE__
  44. #include "lpc17xx_libcfg.h"
  45. #else
  46. #include "lpc17xx_libcfg_default.h"
  47. #endif /* __BUILD_WITH_EXAMPLE__ */
  48. #ifdef _I2S
  49. /* Private Functions ---------------------------------------------------------- */
  50. static uint8_t i2s_GetWordWidth(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode);
  51. static uint8_t i2s_GetChannel(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode);
  52. /********************************************************************//**
  53. * @brief Get I2S wordwidth value
  54. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  55. * @param[in] TRMode is the I2S mode, should be:
  56. * - I2S_TX_MODE = 0: transmit mode
  57. * - I2S_RX_MODE = 1: receive mode
  58. * @return The wordwidth value, should be: 8,16 or 32
  59. *********************************************************************/
  60. static uint8_t i2s_GetWordWidth(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode) {
  61. uint8_t value;
  62. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  63. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  64. if (TRMode == I2S_TX_MODE) {
  65. value = (I2Sx->I2SDAO) & 0x03; /* get wordwidth bit */
  66. } else {
  67. value = (I2Sx->I2SDAI) & 0x03; /* get wordwidth bit */
  68. }
  69. switch (value) {
  70. case I2S_WORDWIDTH_8:
  71. return 8;
  72. case I2S_WORDWIDTH_16:
  73. return 16;
  74. default:
  75. return 32;
  76. }
  77. }
  78. /********************************************************************//**
  79. * @brief Get I2S channel value
  80. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  81. * @param[in] TRMode is the I2S mode, should be:
  82. * - I2S_TX_MODE = 0: transmit mode
  83. * - I2S_RX_MODE = 1: receive mode
  84. * @return The channel value, should be: 1(mono) or 2(stereo)
  85. *********************************************************************/
  86. static uint8_t i2s_GetChannel(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode) {
  87. uint8_t value;
  88. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  89. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  90. if (TRMode == I2S_TX_MODE) {
  91. value = ((I2Sx->I2SDAO) & 0x04)>>2; /* get bit[2] */
  92. } else {
  93. value = ((I2Sx->I2SDAI) & 0x04)>>2; /* get bit[2] */
  94. }
  95. if(value == I2S_MONO) return 1;
  96. return 2;
  97. }
  98. /* End of Private Functions --------------------------------------------------- */
  99. /* Public Functions ----------------------------------------------------------- */
  100. /** @addtogroup I2S_Public_Functions
  101. * @{
  102. */
  103. /********************************************************************//**
  104. * @brief Initialize I2S
  105. * - Turn on power and clock
  106. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  107. * @return none
  108. *********************************************************************/
  109. void I2S_Init(LPC_I2S_TypeDef *I2Sx) {
  110. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  111. // Turn on power and clock
  112. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCI2S, ENABLE);
  113. LPC_I2S->I2SDAI = LPC_I2S->I2SDAO = 0x00;
  114. }
  115. /********************************************************************//**
  116. * @brief Configuration I2S, setting:
  117. * - master/slave mode
  118. * - wordwidth value
  119. * - channel mode
  120. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  121. * @param[in] TRMode transmit/receive mode, should be:
  122. * - I2S_TX_MODE = 0: transmit mode
  123. * - I2S_RX_MODE = 1: receive mode
  124. * @param[in] ConfigStruct pointer to I2S_CFG_Type structure
  125. * which will be initialized.
  126. * @return none
  127. *********************************************************************/
  128. void I2S_Config(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode, I2S_CFG_Type* ConfigStruct)
  129. {
  130. uint32_t bps, config;
  131. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  132. CHECK_PARAM(PARAM_I2S_WORDWIDTH(ConfigStruct->wordwidth));
  133. CHECK_PARAM(PARAM_I2S_CHANNEL(ConfigStruct->mono));
  134. CHECK_PARAM(PARAM_I2S_STOP(ConfigStruct->stop));
  135. CHECK_PARAM(PARAM_I2S_RESET(ConfigStruct->reset));
  136. CHECK_PARAM(PARAM_I2S_WS_SEL(ConfigStruct->ws_sel));
  137. CHECK_PARAM(PARAM_I2S_MUTE(ConfigStruct->mute));
  138. /* Setup clock */
  139. bps = (ConfigStruct->wordwidth +1)*8;
  140. /* Calculate audio config */
  141. config = (bps - 1)<<6 | (ConfigStruct->ws_sel)<<5 | (ConfigStruct->reset)<<4 |
  142. (ConfigStruct->stop)<<3 | (ConfigStruct->mono)<<2 | (ConfigStruct->wordwidth);
  143. if(TRMode == I2S_RX_MODE){
  144. LPC_I2S->I2SDAI = config;
  145. }else{
  146. LPC_I2S->I2SDAO = config;
  147. }
  148. }
  149. /********************************************************************//**
  150. * @brief DeInitial both I2S transmit or receive
  151. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  152. * @return none
  153. *********************************************************************/
  154. void I2S_DeInit(LPC_I2S_TypeDef *I2Sx) {
  155. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  156. // Turn off power and clock
  157. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCI2S, DISABLE);
  158. }
  159. /********************************************************************//**
  160. * @brief Get I2S Buffer Level
  161. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  162. * @param[in] TRMode Transmit/receive mode, should be:
  163. * - I2S_TX_MODE = 0: transmit mode
  164. * - I2S_RX_MODE = 1: receive mode
  165. * @return current level of Transmit/Receive Buffer
  166. *********************************************************************/
  167. uint8_t I2S_GetLevel(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode)
  168. {
  169. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  170. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  171. if(TRMode == I2S_TX_MODE)
  172. {
  173. return ((I2Sx->I2SSTATE >> 16) & 0xFF);
  174. }
  175. else
  176. {
  177. return ((I2Sx->I2SSTATE >> 8) & 0xFF);
  178. }
  179. }
  180. /********************************************************************//**
  181. * @brief I2S Start: clear all STOP,RESET and MUTE bit, ready to operate
  182. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  183. * @return none
  184. *********************************************************************/
  185. void I2S_Start(LPC_I2S_TypeDef *I2Sx)
  186. {
  187. //Clear STOP,RESET and MUTE bit
  188. I2Sx->I2SDAO &= ~I2S_DAI_RESET;
  189. I2Sx->I2SDAI &= ~I2S_DAI_RESET;
  190. I2Sx->I2SDAO &= ~I2S_DAI_STOP;
  191. I2Sx->I2SDAI &= ~I2S_DAI_STOP;
  192. I2Sx->I2SDAO &= ~I2S_DAI_MUTE;
  193. }
  194. /********************************************************************//**
  195. * @brief I2S Send data
  196. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  197. * @param[in] BufferData pointer to uint32_t is the data will be send
  198. * @return none
  199. *********************************************************************/
  200. void I2S_Send(LPC_I2S_TypeDef *I2Sx, uint32_t BufferData) {
  201. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  202. I2Sx->I2STXFIFO = BufferData;
  203. }
  204. /********************************************************************//**
  205. * @brief I2S Receive Data
  206. * @param[in] I2Sx pointer to LPC_I2S_TypeDef
  207. * @return received value
  208. *********************************************************************/
  209. uint32_t I2S_Receive(LPC_I2S_TypeDef* I2Sx) {
  210. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  211. return (I2Sx->I2SRXFIFO);
  212. }
  213. /********************************************************************//**
  214. * @brief I2S Pause
  215. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  216. * @param[in] TRMode is transmit/receive mode, should be:
  217. * - I2S_TX_MODE = 0: transmit mode
  218. * - I2S_RX_MODE = 1: receive mode
  219. * @return none
  220. *********************************************************************/
  221. void I2S_Pause(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode) {
  222. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  223. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  224. if (TRMode == I2S_TX_MODE) //Transmit mode
  225. {
  226. I2Sx->I2SDAO |= I2S_DAO_STOP;
  227. } else //Receive mode
  228. {
  229. I2Sx->I2SDAI |= I2S_DAI_STOP;
  230. }
  231. }
  232. /********************************************************************//**
  233. * @brief I2S Mute
  234. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  235. * @param[in] TRMode is transmit/receive mode, should be:
  236. * - I2S_TX_MODE = 0: transmit mode
  237. * - I2S_RX_MODE = 1: receive mode
  238. * @return none
  239. *********************************************************************/
  240. void I2S_Mute(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode) {
  241. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  242. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  243. if (TRMode == I2S_TX_MODE) //Transmit mode
  244. {
  245. I2Sx->I2SDAO |= I2S_DAO_MUTE;
  246. } else //Receive mode
  247. {
  248. I2Sx->I2SDAI |= I2S_DAI_MUTE;
  249. }
  250. }
  251. /********************************************************************//**
  252. * @brief I2S Stop
  253. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  254. * @param[in] TRMode is transmit/receive mode, should be:
  255. * - I2S_TX_MODE = 0: transmit mode
  256. * - I2S_RX_MODE = 1: receive mode
  257. * @return none
  258. *********************************************************************/
  259. void I2S_Stop(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode) {
  260. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  261. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  262. if (TRMode == I2S_TX_MODE) //Transmit mode
  263. {
  264. I2Sx->I2SDAO &= ~I2S_DAO_MUTE;
  265. I2Sx->I2SDAO |= I2S_DAO_STOP;
  266. I2Sx->I2SDAO |= I2S_DAO_RESET;
  267. } else //Receive mode
  268. {
  269. I2Sx->I2SDAI |= I2S_DAI_STOP;
  270. I2Sx->I2SDAI |= I2S_DAI_RESET;
  271. }
  272. }
  273. /********************************************************************//**
  274. * @brief Set frequency for I2S
  275. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  276. * @param[in] Freq is the frequency for I2S will be set. It can range
  277. * from 16-96 kHz(16, 22.05, 32, 44.1, 48, 96kHz)
  278. * @param[in] TRMode is transmit/receive mode, should be:
  279. * - I2S_TX_MODE = 0: transmit mode
  280. * - I2S_RX_MODE = 1: receive mode
  281. * @return Status: ERROR or SUCCESS
  282. *********************************************************************/
  283. Status I2S_FreqConfig(LPC_I2S_TypeDef *I2Sx, uint32_t Freq, uint8_t TRMode) {
  284. uint32_t i2s_clk;
  285. uint8_t channel, wordwidth;
  286. uint32_t x, y;
  287. uint64_t divider;
  288. uint16_t dif;
  289. uint16_t x_divide, y_divide;
  290. uint16_t err, ErrorOptimal = 0xFFFF;
  291. uint32_t N;
  292. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  293. CHECK_PARAM(PRAM_I2S_FREQ(Freq));
  294. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  295. //Get the frequency of PCLK_I2S
  296. i2s_clk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_I2S);
  297. if(TRMode == I2S_TX_MODE)
  298. {
  299. channel = i2s_GetChannel(I2Sx,I2S_TX_MODE);
  300. wordwidth = i2s_GetWordWidth(I2Sx,I2S_TX_MODE);
  301. }
  302. else
  303. {
  304. channel = i2s_GetChannel(I2Sx,I2S_RX_MODE);
  305. wordwidth = i2s_GetWordWidth(I2Sx,I2S_RX_MODE);
  306. }
  307. /* Calculate X and Y divider
  308. * The MCLK rate for the I2S transmitter is determined by the value
  309. * in the I2STXRATE/I2SRXRATE register. The required I2STXRATE/I2SRXRATE
  310. * setting depends on the desired audio sample rate desired, the format
  311. * (stereo/mono) used, and the data size.
  312. * The formula is:
  313. * I2S_MCLK = PCLK_I2S * (X/Y) / 2
  314. * In that, Y must be greater than or equal to X. X should divides evenly
  315. * into Y.
  316. * We have:
  317. * I2S_MCLK = Freq * channel*wordwidth * (I2STXBITRATE+1);
  318. * So: (X/Y) = (Freq * channel*wordwidth * (I2STXBITRATE+1))*2/PCLK_I2S
  319. * We use a loop function to chose the most suitable X,Y value
  320. */
  321. /* divider is a fixed point number with 16 fractional bits */
  322. divider = (((uint64_t)Freq *channel*wordwidth * 2)<<16) / i2s_clk;
  323. /* find N that make x/y <= 1 -> divider <= 2^16 */
  324. for(N=64;N>0;N--){
  325. if((divider*N) < (1<<16)) break;
  326. }
  327. if(N == 0) return ERROR;
  328. divider *= N;
  329. for (y = 255; y > 0; y--) {
  330. x = y * divider;
  331. if(x & (0xFF000000)) continue;
  332. dif = x & 0xFFFF;
  333. if(dif>0x8000) err = 0x10000-dif;
  334. else err = dif;
  335. if (err == 0)
  336. {
  337. y_divide = y;
  338. break;
  339. }
  340. else if (err < ErrorOptimal)
  341. {
  342. ErrorOptimal = err;
  343. y_divide = y;
  344. }
  345. }
  346. x_divide = ((uint64_t)y_divide * Freq *(channel*wordwidth)* N * 2)/i2s_clk;
  347. if(x_divide >= 256) x_divide = 0xFF;
  348. if(x_divide == 0) x_divide = 1;
  349. if (TRMode == I2S_TX_MODE)// Transmitter
  350. {
  351. I2Sx->I2STXBITRATE = N-1;
  352. I2Sx->I2STXRATE = y_divide | (x_divide << 8);
  353. } else //Receiver
  354. {
  355. I2Sx->I2SRXBITRATE = N-1;
  356. I2Sx->I2STXRATE = y_divide | (x_divide << 8);
  357. }
  358. return SUCCESS;
  359. }
  360. /********************************************************************//**
  361. * @brief I2S set bitrate
  362. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  363. * @param[in] bitrate value will be set
  364. * bitrate value should be in range: 0 .. 63
  365. * @param[in] TRMode is transmit/receive mode, should be:
  366. * - I2S_TX_MODE = 0: transmit mode
  367. * - I2S_RX_MODE = 1: receive mode
  368. * @return none
  369. *********************************************************************/
  370. void I2S_SetBitRate(LPC_I2S_TypeDef *I2Sx, uint8_t bitrate, uint8_t TRMode)
  371. {
  372. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  373. CHECK_PARAM(PARAM_I2S_BITRATE(bitrate));
  374. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  375. if(TRMode == I2S_TX_MODE)
  376. {
  377. I2Sx->I2STXBITRATE = bitrate;
  378. }
  379. else
  380. {
  381. I2Sx->I2SRXBITRATE = bitrate;
  382. }
  383. }
  384. /********************************************************************//**
  385. * @brief Configuration operating mode for I2S
  386. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  387. * @param[in] ModeConfig pointer to I2S_MODEConf_Type will be used to
  388. * configure
  389. * @param[in] TRMode is transmit/receive mode, should be:
  390. * - I2S_TX_MODE = 0: transmit mode
  391. * - I2S_RX_MODE = 1: receive mode
  392. * @return none
  393. *********************************************************************/
  394. void I2S_ModeConfig(LPC_I2S_TypeDef *I2Sx, I2S_MODEConf_Type* ModeConfig,
  395. uint8_t TRMode)
  396. {
  397. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  398. CHECK_PARAM(PARAM_I2S_CLKSEL(ModeConfig->clksel));
  399. CHECK_PARAM(PARAM_I2S_4PIN(ModeConfig->fpin));
  400. CHECK_PARAM(PARAM_I2S_MCLK(ModeConfig->mcena));
  401. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  402. if (TRMode == I2S_TX_MODE) {
  403. I2Sx->I2STXMODE &= ~0x0F; //clear bit 3:0 in I2STXMODE register
  404. if (ModeConfig->clksel == I2S_CLKSEL_MCLK) {
  405. I2Sx->I2STXMODE |= 0x02;
  406. }
  407. if (ModeConfig->fpin == I2S_4PIN_ENABLE) {
  408. I2Sx->I2STXMODE |= (1 << 2);
  409. }
  410. if (ModeConfig->mcena == I2S_MCLK_ENABLE) {
  411. I2Sx->I2STXMODE |= (1 << 3);
  412. }
  413. } else {
  414. I2Sx->I2SRXMODE &= ~0x0F; //clear bit 3:0 in I2STXMODE register
  415. if (ModeConfig->clksel == I2S_CLKSEL_MCLK) {
  416. I2Sx->I2SRXMODE |= 0x02;
  417. }
  418. if (ModeConfig->fpin == I2S_4PIN_ENABLE) {
  419. I2Sx->I2SRXMODE |= (1 << 2);
  420. }
  421. if (ModeConfig->mcena == I2S_MCLK_ENABLE) {
  422. I2Sx->I2SRXMODE |= (1 << 3);
  423. }
  424. }
  425. }
  426. /********************************************************************//**
  427. * @brief Configure DMA operation for I2S
  428. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  429. * @param[in] DMAConfig pointer to I2S_DMAConf_Type will be used to configure
  430. * @param[in] TRMode is transmit/receive mode, should be:
  431. * - I2S_TX_MODE = 0: transmit mode
  432. * - I2S_RX_MODE = 1: receive mode
  433. * @return none
  434. *********************************************************************/
  435. void I2S_DMAConfig(LPC_I2S_TypeDef *I2Sx, I2S_DMAConf_Type* DMAConfig,
  436. uint8_t TRMode)
  437. {
  438. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  439. CHECK_PARAM(PARAM_I2S_DMA(DMAConfig->DMAIndex));
  440. CHECK_PARAM(PARAM_I2S_DMA_DEPTH(DMAConfig->depth));
  441. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  442. if (TRMode == I2S_RX_MODE) {
  443. if (DMAConfig->DMAIndex == I2S_DMA_1) {
  444. LPC_I2S->I2SDMA1 = (DMAConfig->depth) << 8;
  445. } else {
  446. LPC_I2S->I2SDMA2 = (DMAConfig->depth) << 8;
  447. }
  448. } else {
  449. if (DMAConfig->DMAIndex == I2S_DMA_1) {
  450. LPC_I2S->I2SDMA1 = (DMAConfig->depth) << 16;
  451. } else {
  452. LPC_I2S->I2SDMA2 = (DMAConfig->depth) << 16;
  453. }
  454. }
  455. }
  456. /********************************************************************//**
  457. * @brief Enable/Disable DMA operation for I2S
  458. * @param[in] I2Sx: I2S peripheral selected, should be: LPC_I2S
  459. * @param[in] DMAIndex chose what DMA is used, should be:
  460. * - I2S_DMA_1 = 0: DMA1
  461. * - I2S_DMA_2 = 1: DMA2
  462. * @param[in] TRMode is transmit/receive mode, should be:
  463. * - I2S_TX_MODE = 0: transmit mode
  464. * - I2S_RX_MODE = 1: receive mode
  465. * @param[in] NewState is new state of DMA operation, should be:
  466. * - ENABLE
  467. * - DISABLE
  468. * @return none
  469. *********************************************************************/
  470. void I2S_DMACmd(LPC_I2S_TypeDef *I2Sx, uint8_t DMAIndex, uint8_t TRMode,
  471. FunctionalState NewState)
  472. {
  473. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  474. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  475. CHECK_PARAM(PARAM_I2S_DMA(DMAIndex));
  476. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  477. if (TRMode == I2S_RX_MODE) {
  478. if (DMAIndex == I2S_DMA_1) {
  479. if (NewState == ENABLE)
  480. I2Sx->I2SDMA1 |= 0x01;
  481. else
  482. I2Sx->I2SDMA1 &= ~0x01;
  483. } else {
  484. if (NewState == ENABLE)
  485. I2Sx->I2SDMA2 |= 0x01;
  486. else
  487. I2Sx->I2SDMA2 &= ~0x01;
  488. }
  489. } else {
  490. if (DMAIndex == I2S_DMA_1) {
  491. if (NewState == ENABLE)
  492. I2Sx->I2SDMA1 |= 0x02;
  493. else
  494. I2Sx->I2SDMA1 &= ~0x02;
  495. } else {
  496. if (NewState == ENABLE)
  497. I2Sx->I2SDMA2 |= 0x02;
  498. else
  499. I2Sx->I2SDMA2 &= ~0x02;
  500. }
  501. }
  502. }
  503. /********************************************************************//**
  504. * @brief Configure IRQ for I2S
  505. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  506. * @param[in] TRMode is transmit/receive mode, should be:
  507. * - I2S_TX_MODE = 0: transmit mode
  508. * - I2S_RX_MODE = 1: receive mode
  509. * @param[in] level is the FIFO level that triggers IRQ request
  510. * @return none
  511. *********************************************************************/
  512. void I2S_IRQConfig(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode, uint8_t level) {
  513. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  514. CHECK_PARAM(PARAM_I2S_TRX(TRMode));
  515. CHECK_PARAM(PARAM_I2S_IRQ_LEVEL(level));
  516. if (TRMode == I2S_RX_MODE) {
  517. I2Sx->I2SIRQ |= (level << 8);
  518. } else {
  519. I2Sx->I2SIRQ |= (level << 16);
  520. }
  521. }
  522. /********************************************************************//**
  523. * @brief Enable/Disable IRQ for I2S
  524. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  525. * @param[in] TRMode is transmit/receive mode, should be:
  526. * - I2S_TX_MODE = 0: transmit mode
  527. * - I2S_RX_MODE = 1: receive mode
  528. * @param[in] NewState is new state of DMA operation, should be:
  529. * - ENABLE
  530. * - DISABLE
  531. * @return none
  532. *********************************************************************/
  533. void I2S_IRQCmd(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode, FunctionalState NewState) {
  534. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  535. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  536. if (TRMode == I2S_RX_MODE) {
  537. if (NewState == ENABLE)
  538. I2Sx->I2SIRQ |= 0x01;
  539. else
  540. I2Sx->I2SIRQ &= ~0x01;
  541. //Enable DMA
  542. } else {
  543. if (NewState == ENABLE)
  544. I2Sx->I2SIRQ |= 0x02;
  545. else
  546. I2Sx->I2SIRQ &= ~0x02;
  547. }
  548. }
  549. /********************************************************************//**
  550. * @brief Get I2S interrupt status
  551. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  552. * @param[in] TRMode is transmit/receive mode, should be:
  553. * - I2S_TX_MODE = 0: transmit mode
  554. * - I2S_RX_MODE = 1: receive mode
  555. * @return FunctionState should be:
  556. * - ENABLE: interrupt is enable
  557. * - DISABLE: interrupt is disable
  558. *********************************************************************/
  559. FunctionalState I2S_GetIRQStatus(LPC_I2S_TypeDef *I2Sx,uint8_t TRMode)
  560. {
  561. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  562. if(TRMode == I2S_TX_MODE)
  563. return ((FunctionalState)((I2Sx->I2SIRQ >> 1)&0x01));
  564. else
  565. return ((FunctionalState)((I2Sx->I2SIRQ)&0x01));
  566. }
  567. /********************************************************************//**
  568. * @brief Get I2S interrupt depth
  569. * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
  570. * @param[in] TRMode is transmit/receive mode, should be:
  571. * - I2S_TX_MODE = 0: transmit mode
  572. * - I2S_RX_MODE = 1: receive mode
  573. * @return depth of FIFO level on which to create an irq request
  574. *********************************************************************/
  575. uint8_t I2S_GetIRQDepth(LPC_I2S_TypeDef *I2Sx,uint8_t TRMode)
  576. {
  577. CHECK_PARAM(PARAM_I2Sx(I2Sx));
  578. if(TRMode == I2S_TX_MODE)
  579. return (((I2Sx->I2SIRQ)>>16)&0xFF);
  580. else
  581. return (((I2Sx->I2SIRQ)>>8)&0xFF);
  582. }
  583. /**
  584. * @}
  585. */
  586. #endif /* _I2S */
  587. /**
  588. * @}
  589. */
  590. /* --------------------------------- End Of File ------------------------------ */