i2s.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. //*****************************************************************************
  2. //
  3. // i2s.c - Driver for the I2S controller.
  4. //
  5. // Copyright (c) 2008-2011 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 8264 of the Stellaris Peripheral Driver Library.
  22. //
  23. //*****************************************************************************
  24. //*****************************************************************************
  25. //
  26. //! \addtogroup i2s_api
  27. //! @{
  28. //
  29. //*****************************************************************************
  30. #include "inc/hw_i2s.h"
  31. #include "inc/hw_ints.h"
  32. #include "inc/hw_memmap.h"
  33. #include "inc/hw_types.h"
  34. #include "driverlib/debug.h"
  35. #include "driverlib/i2s.h"
  36. #include "driverlib/interrupt.h"
  37. //*****************************************************************************
  38. //
  39. //! Enables the I2S transmit module for operation.
  40. //!
  41. //! \param ulBase is the I2S module base address.
  42. //!
  43. //! This function enables the transmit module for operation. The module
  44. //! should be enabled after configuration. When the module is disabled,
  45. //! no data or clocks are generated on the I2S signals.
  46. //!
  47. //! \return None.
  48. //
  49. //*****************************************************************************
  50. void
  51. I2STxEnable(unsigned long ulBase)
  52. {
  53. //
  54. // Check the arguments.
  55. //
  56. ASSERT(ulBase == I2S0_BASE);
  57. //
  58. // Enable the tx FIFO service request.
  59. //
  60. HWREG(ulBase + I2S_O_TXISM) = I2S_TXISM_FFM;
  61. //
  62. // Read-modify-write the enable bit.
  63. //
  64. HWREG(ulBase + I2S_O_CFG) |= I2S_CFG_TXEN;
  65. }
  66. //*****************************************************************************
  67. //
  68. //! Disables the I2S transmit module for operation.
  69. //!
  70. //! \param ulBase is the I2S module base address.
  71. //!
  72. //! This function disables the transmit module for operation. The module
  73. //! should be disabled before configuration. When the module is disabled,
  74. //! no data or clocks are generated on the I2S signals.
  75. //!
  76. //! \return None.
  77. //
  78. //*****************************************************************************
  79. void
  80. I2STxDisable(unsigned long ulBase)
  81. {
  82. //
  83. // Check the arguments.
  84. //
  85. ASSERT(ulBase == I2S0_BASE);
  86. //
  87. // Read-modify-write the enable bit.
  88. //
  89. HWREG(ulBase + I2S_O_CFG) &= ~I2S_CFG_TXEN;
  90. }
  91. //*****************************************************************************
  92. //
  93. //! Writes data samples to the I2S transmit FIFO with blocking.
  94. //!
  95. //! \param ulBase is the I2S module base address.
  96. //! \param ulData is the single- or dual-channel I2S data.
  97. //!
  98. //! This function writes a single-channel sample or combined left-right
  99. //! samples to the I2S transmit FIFO. The format of the sample is determined
  100. //! by the configuration that was used with the function I2STxConfigSet().
  101. //! If the transmit mode is \b I2S_MODE_DUAL_STEREO then the \e ulData
  102. //! parameter contains either the left or right sample. The left and right
  103. //! sample alternate with each write to the FIFO, left sample first. If the
  104. //! transmit mode is \b I2S_MODE_COMPACT_STEREO_16 or
  105. //! \b I2S_MODE_COMPACT_STEREO_8, then the \e ulData parameter contains both
  106. //! the left and right samples. If the transmit mode is
  107. //! \b I2S_MODE_SINGLE_MONO then the \e ulData parameter contains the single
  108. //! channel sample.
  109. //!
  110. //! For the compact modes, both the left and right samples are written at
  111. //! the same time. If 16-bit compact mode is used, then the least significant
  112. //! 16 bits contain the left sample, and the most significant 16 bits contain
  113. //! the right sample. If 8-bit compact mode is used, then the lower 8 bits
  114. //! contain the left sample, and the next 8 bits contain the right sample,
  115. //! with the upper 16 bits unused.
  116. //!
  117. //! If there is no room in the transmit FIFO, then this function waits
  118. //! in a polling loop until the data can be written.
  119. //!
  120. //! \return None.
  121. //
  122. //*****************************************************************************
  123. void
  124. I2STxDataPut(unsigned long ulBase, unsigned long ulData)
  125. {
  126. //
  127. // Check the arguments.
  128. //
  129. ASSERT(ulBase == I2S0_BASE);
  130. //
  131. // Wait until there is space.
  132. //
  133. while(HWREG(ulBase + I2S_O_TXLEV) >= 16)
  134. {
  135. }
  136. //
  137. // Write the data to the I2S.
  138. //
  139. HWREG(ulBase + I2S_O_TXFIFO) = ulData;
  140. }
  141. //*****************************************************************************
  142. //
  143. //! Writes data samples to the I2S transmit FIFO without blocking.
  144. //!
  145. //! \param ulBase is the I2S module base address.
  146. //! \param ulData is the single- or dual-channel I2S data.
  147. //!
  148. //! This function writes a single-channel sample or combined left-right
  149. //! samples to the I2S transmit FIFO. The format of the sample is determined
  150. //! by the configuration that was used with the function I2STxConfigSet().
  151. //! If the transmit mode is \b I2S_MODE_DUAL_STEREO then the \e ulData
  152. //! parameter contains either the left or right sample. The left and right
  153. //! sample alternate with each write to the FIFO, left sample first. If the
  154. //! transmit mode is \b I2S_MODE_COMPACT_STEREO_16 or
  155. //! \b I2S_MODE_COMPACT_STEREO_8, then the \e ulData parameter contains both
  156. //! the left and right samples. If the transmit mode is
  157. //! \b I2S_MODE_SINGLE_MONO then the \e ulData parameter contains the single-
  158. //! channel sample.
  159. //!
  160. //! For the compact modes, both the left and right samples are written at
  161. //! the same time. If 16-bit compact mode is used, then the least significant
  162. //! 16 bits contain the left sample, and the most significant 16 bits contain
  163. //! the right sample. If 8-bit compact mode is used, then the lower 8 bits
  164. //! contain the left sample, and the next 8 bits contain the right sample,
  165. //! with the upper 16 bits unused.
  166. //!
  167. //! If there is no room in the transmit FIFO, then this function returns
  168. //! immediately without writing any data to the FIFO.
  169. //!
  170. //! \return The number of elements written to the I2S transmit FIFO (1 or 0).
  171. //
  172. //*****************************************************************************
  173. long
  174. I2STxDataPutNonBlocking(unsigned long ulBase, unsigned long ulData)
  175. {
  176. //
  177. // Check the arguments.
  178. //
  179. ASSERT(ulBase == I2S0_BASE);
  180. //
  181. // Check for space to write.
  182. //
  183. if(HWREG(ulBase + I2S_O_TXLEV) < 16)
  184. {
  185. HWREG(ulBase + I2S_O_TXFIFO) = ulData;
  186. return(1);
  187. }
  188. else
  189. {
  190. return(0);
  191. }
  192. }
  193. //*****************************************************************************
  194. //
  195. //! Configures the I2S transmit module.
  196. //!
  197. //! \param ulBase is the I2S module base address.
  198. //! \param ulConfig is the logical OR of the configuration options.
  199. //!
  200. //! This function is used to configure the options for the I2S transmit
  201. //! channel. The parameter \e ulConfig is the logical OR of the following
  202. //! options:
  203. //!
  204. //! - \b I2S_CONFIG_FORMAT_I2S for standard I2S format,
  205. //! \b I2S_CONFIG_FORMAT_LEFT_JUST for left justified format, or
  206. //! \b I2S_CONFIG_FORMAT_RIGHT_JUST for right justified format.
  207. //! - \b I2S_CONFIG_SCLK_INVERT to invert the polarity of the serial bit clock.
  208. //! - \b I2S_CONFIG_MODE_DUAL for dual channel stereo,
  209. //! \b I2S_CONFIG_MODE_COMPACT_16 for 16-bit compact stereo mode,
  210. //! \b I2S_CONFIG_MODE_COMPACT_8 for 8-bit compact stereo mode, or
  211. //! \b I2S_CONFIG_MODE_MONO for single channel mono format.
  212. //! - \b I2S_CONFIG_CLK_MASTER or \b I2S_CONFIG_CLK_SLAVE to select whether
  213. //! the I2S transmitter is the clock master or slave.
  214. //! - \b I2S_CONFIG_SAMPLE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
  215. //! to select the number of bits per sample.
  216. //! - \b I2S_CONFIG_WIRE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
  217. //! to select the number of bits per word that are transferred on the data
  218. //! line.
  219. //! - \b I2S_CONFIG_EMPTY_ZERO or \b I2S_CONFIG_EMPTY_REPEAT to select whether
  220. //! the module transmits zeroes or repeats the last sample when the FIFO is
  221. //! empty.
  222. //!
  223. //! \return None.
  224. //
  225. //*****************************************************************************
  226. void
  227. I2STxConfigSet(unsigned long ulBase, unsigned long ulConfig)
  228. {
  229. //
  230. // Check the arguments.
  231. //
  232. ASSERT(ulBase == I2S0_BASE);
  233. ASSERT((ulConfig & (I2S_CONFIG_FORMAT_MASK | I2S_CONFIG_MODE_MASK |
  234. I2S_CONFIG_EMPTY_MASK | I2S_CONFIG_CLK_MASK |
  235. I2S_CONFIG_SAMPLE_SIZE_MASK |
  236. I2S_CONFIG_WIRE_SIZE_MASK)) == ulConfig);
  237. //
  238. // Check to see if a compact mode is used.
  239. //
  240. if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_COMPACT_8)
  241. {
  242. //
  243. // If compact 8 mode is used, then need to adjust some bits
  244. // before writing the config register. Also set the FIFO
  245. // config register for 8-bit compact samples.
  246. //
  247. ulConfig &= ~I2S_CONFIG_MODE_MONO;
  248. HWREG(ulBase + I2S_O_TXFIFOCFG) = I2S_TXFIFOCFG_CSS;
  249. }
  250. else
  251. {
  252. //
  253. // If compact 8 mode is not used, then set the FIFO config
  254. // register for 16 bit. This setting is okay if a compact
  255. // mode is not used.
  256. //
  257. HWREG(ulBase + I2S_O_TXFIFOCFG) = 0;
  258. }
  259. //
  260. // Write the configuration register. Because all the fields are
  261. // specified by the configuration parameter, it is not necessary
  262. // to do a read-modify-write.
  263. //
  264. HWREG(ulBase + I2S_O_TXCFG) = ulConfig;
  265. }
  266. //*****************************************************************************
  267. //
  268. //! Sets the FIFO level at which a service request is generated.
  269. //!
  270. //! \param ulBase is the I2S module base address.
  271. //! \param ulLevel is the FIFO service request limit.
  272. //!
  273. //! This function is used to set the transmit FIFO fullness level at which a
  274. //! service request occurs. The service request is used to generate an
  275. //! interrupt or a DMA transfer request. The transmit FIFO generates a
  276. //! service request when the number of items in the FIFO is less than the level
  277. //! specified in the \e ulLevel parameter. For example, if \e ulLevel is 8,
  278. //! then a service request is generated when there are less than 8 samples
  279. //! remaining in the transmit FIFO.
  280. //!
  281. //! For the purposes of counting the FIFO level, a left-right sample pair
  282. //! counts as 2, whether the mode is dual or compact stereo. When mono mode is
  283. //! used, internally the mono sample is still treated as a sample pair, so a
  284. //! single mono sample counts as 2. Because the FIFO always deals with sample
  285. //! pairs, the level must be an even number from 0 to 16. The maximum value is
  286. //! 16, which causes a service request when there is any room in the FIFO.
  287. //! The minimum value is 0, which disables the service request.
  288. //!
  289. //! \return None.
  290. //
  291. //*****************************************************************************
  292. void
  293. I2STxFIFOLimitSet(unsigned long ulBase, unsigned long ulLevel)
  294. {
  295. //
  296. // Check the arguments.
  297. //
  298. ASSERT(ulBase == I2S0_BASE);
  299. ASSERT(ulLevel <= 16);
  300. //
  301. // Write the FIFO limit
  302. //
  303. HWREG(ulBase + I2S_O_TXLIMIT) = ulLevel;
  304. }
  305. //*****************************************************************************
  306. //
  307. //! Gets the current setting of the FIFO service request level.
  308. //!
  309. //! \param ulBase is the I2S module base address.
  310. //!
  311. //! This function is used to get the value of the transmit FIFO service
  312. //! request level. This value is set using the I2STxFIFOLimitSet()
  313. //! function.
  314. //!
  315. //! \return Returns the current value of the FIFO service request limit.
  316. //
  317. //*****************************************************************************
  318. unsigned long
  319. I2STxFIFOLimitGet(unsigned long ulBase)
  320. {
  321. //
  322. // Check the arguments.
  323. //
  324. ASSERT(ulBase == I2S0_BASE);
  325. //
  326. // Read and return the FIFO limit
  327. //
  328. return(HWREG(ulBase + I2S_O_TXLIMIT));
  329. }
  330. //*****************************************************************************
  331. //
  332. //! Gets the number of samples in the transmit FIFO.
  333. //!
  334. //! \param ulBase is the I2S module base address.
  335. //!
  336. //! This function is used to get the number of samples in the transmit FIFO.
  337. //! For the purposes of measuring the FIFO level, a left-right sample pair
  338. //! counts as 2, whether the mode is dual or compact stereo. When mono mode is
  339. //! used, internally the mono sample is still treated as a sample pair, so a
  340. //! single mono sample counts as 2. Because the FIFO always deals with sample
  341. //! pairs, normally the level is an even number from 0 to 16. If dual stereo
  342. //! mode is used and only the left sample has been written without the matching
  343. //! right sample, then the FIFO level is an odd value. If the FIFO level is
  344. //! odd, it indicates a left-right sample mismatch.
  345. //!
  346. //! \return Returns the number of samples in the transmit FIFO, which is
  347. //! normally an even number.
  348. //
  349. //*****************************************************************************
  350. unsigned long
  351. I2STxFIFOLevelGet(unsigned long ulBase)
  352. {
  353. //
  354. // Check the arguments.
  355. //
  356. ASSERT(ulBase == I2S0_BASE);
  357. //
  358. // Read and return the transmit FIFO level.
  359. //
  360. return(HWREG(ulBase + I2S_O_TXLEV));
  361. }
  362. //*****************************************************************************
  363. //
  364. //! Enables the I2S receive module for operation.
  365. //!
  366. //! \param ulBase is the I2S module base address.
  367. //!
  368. //! This function enables the receive module for operation. The module should
  369. //! be enabled after configuration. When the module is disabled, no data is
  370. //! clocked in regardless of the signals on the I2S interface.
  371. //!
  372. //! \return None.
  373. //
  374. //*****************************************************************************
  375. void
  376. I2SRxEnable(unsigned long ulBase)
  377. {
  378. //
  379. // Check the arguments.
  380. //
  381. ASSERT(ulBase == I2S0_BASE);
  382. //
  383. // Enable the tx FIFO service request.
  384. //
  385. HWREG(ulBase + I2S_O_RXISM) = I2S_RXISM_FFM;
  386. //
  387. // Read-modify-write the enable bit.
  388. //
  389. HWREG(ulBase + I2S_O_CFG) |= I2S_CFG_RXEN;
  390. }
  391. //*****************************************************************************
  392. //
  393. //! Disables the I2S receive module for operation.
  394. //!
  395. //! \param ulBase is the I2S module base address.
  396. //!
  397. //! This function disables the receive module for operation. The module should
  398. //! be disabled before configuration. When the module is disabled, no data is
  399. //! clocked in regardless of the signals on the I2S interface.
  400. //!
  401. //! \return None.
  402. //
  403. //*****************************************************************************
  404. void
  405. I2SRxDisable(unsigned long ulBase)
  406. {
  407. //
  408. // Check the arguments.
  409. //
  410. ASSERT(ulBase == I2S0_BASE);
  411. //
  412. // Read-modify-write the enable bit.
  413. //
  414. HWREG(ulBase + I2S_O_CFG) &= ~I2S_CFG_RXEN;
  415. }
  416. //*****************************************************************************
  417. //
  418. //! Reads data samples from the I2S receive FIFO with blocking.
  419. //!
  420. //! \param ulBase is the I2S module base address.
  421. //! \param pulData points to storage for the returned I2S sample data.
  422. //!
  423. //! This function reads a single channel sample or combined left-right
  424. //! samples from the I2S receive FIFO. The format of the sample is determined
  425. //! by the configuration that was used with the function I2SRxConfigSet().
  426. //! If the receive mode is \b I2S_MODE_DUAL_STEREO then the returned value
  427. //! contains either the left or right sample. The left and right sample
  428. //! alternate with each read from the FIFO, left sample first. If the receive
  429. //! mode is \b I2S_MODE_COMPACT_STEREO_16 or \b I2S_MODE_COMPACT_STEREO_8, then
  430. //! the returned data contains both the left and right samples. If the
  431. //! receive mode is \b I2S_MODE_SINGLE_MONO then the returned data
  432. //! contains the single channel sample.
  433. //!
  434. //! For the compact modes, both the left and right samples are read at
  435. //! the same time. If 16-bit compact mode is used, then the least significant
  436. //! 16 bits contain the left sample, and the most significant 16 bits contain
  437. //! the right sample. If 8-bit compact mode is used, then the lower 8 bits
  438. //! contain the left sample, and the next 8 bits contain the right sample,
  439. //! with the upper 16 bits unused.
  440. //!
  441. //! If there is no data in the receive FIFO, then this function waits
  442. //! in a polling loop until data is available.
  443. //!
  444. //! \return None.
  445. //
  446. //*****************************************************************************
  447. void
  448. I2SRxDataGet(unsigned long ulBase, unsigned long *pulData)
  449. {
  450. //
  451. // Check the arguments.
  452. //
  453. ASSERT(ulBase == I2S0_BASE);
  454. //
  455. // Wait until there is data available.
  456. //
  457. while(HWREG(ulBase + I2S_O_RXLEV) == 0)
  458. {
  459. }
  460. //
  461. // Read data from the I2S receive FIFO.
  462. //
  463. *pulData = HWREG(ulBase + I2S_O_RXFIFO);
  464. }
  465. //*****************************************************************************
  466. //
  467. //! Reads data samples from the I2S receive FIFO without blocking.
  468. //!
  469. //! \param ulBase is the I2S module base address.
  470. //! \param pulData points to storage for the returned I2S sample data.
  471. //!
  472. //! This function reads a single channel sample or combined left-right
  473. //! samples from the I2S receive FIFO. The format of the sample is determined
  474. //! by the configuration that was used with the function I2SRxConfigSet().
  475. //! If the receive mode is \b I2S_MODE_DUAL_STEREO then the received data
  476. //! contains either the left or right sample. The left and right sample
  477. //! alternate with each read from the FIFO, left sample first. If the receive
  478. //! mode is \b I2S_MODE_COMPACT_STEREO_16 or \b I2S_MODE_COMPACT_STEREO_8, then
  479. //! the received data contains both the left and right samples. If the
  480. //! receive mode is \b I2S_MODE_SINGLE_MONO then the received data
  481. //! contains the single channel sample.
  482. //!
  483. //! For the compact modes, both the left and right samples are read at
  484. //! the same time. If 16-bit compact mode is used, then the least significant
  485. //! 16 bits contain the left sample, and the most significant 16 bits contain
  486. //! the right sample. If 8-bit compact mode is used, then the lower 8 bits
  487. //! contain the left sample, and the next 8 bits contain the right sample,
  488. //! with the upper 16 bits unused.
  489. //!
  490. //! If there is no data in the receive FIFO, then this function returns
  491. //! immediately without reading any data from the FIFO.
  492. //!
  493. //! \return The number of elements read from the I2S receive FIFO (1 or 0).
  494. //
  495. //*****************************************************************************
  496. long
  497. I2SRxDataGetNonBlocking(unsigned long ulBase, unsigned long *pulData)
  498. {
  499. //
  500. // Check the arguments.
  501. //
  502. ASSERT(ulBase == I2S0_BASE);
  503. //
  504. // Check for available samples.
  505. //
  506. if(HWREG(ulBase + I2S_O_RXLEV) != 0)
  507. {
  508. *pulData = HWREG(ulBase + I2S_O_RXFIFO);
  509. return(1);
  510. }
  511. else
  512. {
  513. return(0);
  514. }
  515. }
  516. //*****************************************************************************
  517. //
  518. //! Configures the I2S receive module.
  519. //!
  520. //! \param ulBase is the I2S module base address.
  521. //! \param ulConfig is the logical OR of the configuration options.
  522. //!
  523. //! This function is used to configure the options for the I2S receive
  524. //! channel. The parameter \e ulConfig is the logical OR of the following
  525. //! options:
  526. //!
  527. //! - \b I2S_CONFIG_FORMAT_I2S for standard I2S format,
  528. //! \b I2S_CONFIG_FORMAT_LEFT_JUST for left justified format, or
  529. //! \b I2S_CONFIG_FORMAT_RIGHT_JUST for right justified format.
  530. //! - \b I2S_CONFIG_SCLK_INVERT to invert the polarity of the serial bit clock.
  531. //! - \b I2S_CONFIG_MODE_DUAL for dual channel stereo,
  532. //! \b I2S_CONFIG_MODE_COMPACT_16 for 16-bit compact stereo mode,
  533. //! \b I2S_CONFIG_MODE_COMPACT_8 for 8-bit compact stereo mode, or
  534. //! \b I2S_CONFIG_MODE_MONO for single channel mono format.
  535. //! - \b I2S_CONFIG_CLK_MASTER or \b I2S_CONFIG_CLK_SLAVE to select whether
  536. //! the I2S receiver is the clock master or slave.
  537. //! - \b I2S_CONFIG_SAMPLE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
  538. //! to select the number of bits per sample.
  539. //! - \b I2S_CONFIG_WIRE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
  540. //! to select the number of bits per word that are transferred on the data
  541. //! line.
  542. //!
  543. //! \return None.
  544. //
  545. //*****************************************************************************
  546. void
  547. I2SRxConfigSet(unsigned long ulBase, unsigned long ulConfig)
  548. {
  549. //
  550. // Check the arguments.
  551. //
  552. ASSERT(ulBase == I2S0_BASE);
  553. ASSERT((ulConfig & (I2S_CONFIG_FORMAT_MASK | I2S_CONFIG_MODE_MASK |
  554. I2S_CONFIG_CLK_MASK | I2S_CONFIG_SAMPLE_SIZE_MASK |
  555. I2S_CONFIG_WIRE_SIZE_MASK)) == ulConfig);
  556. //
  557. // Clear out any prior config of the RX FIFO config register.
  558. //
  559. HWREG(ulBase + I2S_O_RXFIFOCFG) = 0;
  560. //
  561. // If mono mode is used, then the FMM bit needs to be set.
  562. //
  563. if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_MONO)
  564. {
  565. HWREG(ulBase + I2S_O_RXFIFOCFG) |= I2S_RXFIFOCFG_FMM;
  566. }
  567. //
  568. // If a compact mode is used, then the CSS bit needs to be set.
  569. //
  570. else if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_COMPACT_8)
  571. {
  572. HWREG(ulBase + I2S_O_RXFIFOCFG) |= I2S_RXFIFOCFG_CSS;
  573. }
  574. //
  575. // The "mono" bits must be removed from the configuration word
  576. // prior to writing to hardware, because the RX configuration register
  577. // does not actually use these bits.
  578. //
  579. ulConfig &= ~I2S_CONFIG_MODE_MONO;
  580. //
  581. // Write the configuration register. Because all the fields are
  582. // specified by the configuration parameter, it is not necessary
  583. // to do a read-modify-write.
  584. //
  585. HWREG(ulBase + I2S_O_RXCFG) = ulConfig;
  586. }
  587. //*****************************************************************************
  588. //
  589. //! Sets the FIFO level at which a service request is generated.
  590. //!
  591. //! \param ulBase is the I2S module base address.
  592. //! \param ulLevel is the FIFO service request limit.
  593. //!
  594. //! This function is used to set the receive FIFO fullness level at which a
  595. //! service request occurs. The service request is used to generate an
  596. //! interrupt or a DMA transfer request. The receive FIFO generates a
  597. //! service request when the number of items in the FIFO is greater than the
  598. //! level specified in the \e ulLevel parameter. For example, if \e ulLevel is
  599. //! 4, then a service request is generated when there are more than 4 samples
  600. //! available in the receive FIFO.
  601. //!
  602. //! For the purposes of counting the FIFO level, a left-right sample pair
  603. //! counts as 2, whether the mode is dual or compact stereo. When mono mode is
  604. //! used, internally the mono sample is still treated as a sample pair, so a
  605. //! single mono sample counts as 2. Because the FIFO always deals with sample
  606. //! pairs, the level must be an even number from 0 to 16. The minimum value is
  607. //! 0, which causes a service request when there is any data available in
  608. //! the FIFO. The maximum value is 16, which disables the service request
  609. //! (because there cannot be more than 16 items in the FIFO).
  610. //!
  611. //! \return None.
  612. //
  613. //*****************************************************************************
  614. void
  615. I2SRxFIFOLimitSet(unsigned long ulBase, unsigned long ulLevel)
  616. {
  617. //
  618. // Check the arguments.
  619. //
  620. ASSERT(ulBase == I2S0_BASE);
  621. ASSERT(ulLevel <= 16);
  622. //
  623. // Write the FIFO limit
  624. //
  625. HWREG(ulBase + I2S_O_RXLIMIT) = ulLevel;
  626. }
  627. //*****************************************************************************
  628. //
  629. //! Gets the current setting of the FIFO service request level.
  630. //!
  631. //! \param ulBase is the I2S module base address.
  632. //!
  633. //! This function is used to get the value of the receive FIFO service
  634. //! request level. This value is set using the I2SRxFIFOLimitSet()
  635. //! function.
  636. //!
  637. //! \return Returns the current value of the FIFO service request limit.
  638. //
  639. //*****************************************************************************
  640. unsigned long
  641. I2SRxFIFOLimitGet(unsigned long ulBase)
  642. {
  643. //
  644. // Check the arguments.
  645. //
  646. ASSERT(ulBase == I2S0_BASE);
  647. //
  648. // Read and return the FIFO limit. The lower bit is masked
  649. // because it always reads as 1 and has no meaning.
  650. //
  651. return(HWREG(ulBase + I2S_O_RXLIMIT) & 0xFFFE);
  652. }
  653. //*****************************************************************************
  654. //
  655. //! Gets the number of samples in the receive FIFO.
  656. //!
  657. //! \param ulBase is the I2S module base address.
  658. //!
  659. //! This function is used to get the number of samples in the receive FIFO.
  660. //! For the purposes of measuring the FIFO level, a left-right sample pair
  661. //! counts as 2, whether the mode is dual or compact stereo. When mono mode is
  662. //! used, internally the mono sample is still treated as a sample pair, so a
  663. //! single mono sample counts as 2. Because the FIFO always deals with sample
  664. //! pairs, normally the level is an even number from 0 to 16. If dual stereo
  665. //! mode is used and only the left sample has been read without reading the
  666. //! matching right sample, then the FIFO level is an odd value. If the FIFO
  667. //! level is odd, it indicates a left-right sample mismatch.
  668. //!
  669. //! \return Returns the number of samples in the transmit FIFO, which is
  670. //! normally an even number.
  671. //
  672. //*****************************************************************************
  673. unsigned long
  674. I2SRxFIFOLevelGet(unsigned long ulBase)
  675. {
  676. //
  677. // Check the arguments.
  678. //
  679. ASSERT(ulBase == I2S0_BASE);
  680. //
  681. // Read and return the receive FIFO level.
  682. //
  683. return(HWREG(ulBase + I2S_O_RXLEV));
  684. }
  685. //*****************************************************************************
  686. //
  687. //! Enables the I2S transmit and receive modules for operation.
  688. //!
  689. //! \param ulBase is the I2S module base address.
  690. //!
  691. //! This function simultaneously enables the transmit and receive modules for
  692. //! operation, providing a synchronized SCLK and LRCLK. The module should be
  693. //! enabled after configuration. When the module is disabled, no data or
  694. //! clocks are generated on the I2S signals.
  695. //!
  696. //! \return None.
  697. //
  698. //*****************************************************************************
  699. void
  700. I2STxRxEnable(unsigned long ulBase)
  701. {
  702. //
  703. // Check the arguments.
  704. //
  705. ASSERT(ulBase == I2S0_BASE);
  706. //
  707. // Enable the Tx FIFO service request.
  708. //
  709. HWREG(ulBase + I2S_O_TXISM) = I2S_TXISM_FFM;
  710. //
  711. // Enable the Rx FIFO service request.
  712. //
  713. HWREG(ulBase + I2S_O_RXISM) = I2S_RXISM_FFM;
  714. //
  715. // Enable the transmit and receive modules.
  716. //
  717. HWREG(ulBase + I2S_O_CFG) |= I2S_CFG_TXEN | I2S_CFG_RXEN;
  718. }
  719. //*****************************************************************************
  720. //
  721. //! Disables the I2S transmit and receive modules.
  722. //!
  723. //! \param ulBase is the I2S module base address.
  724. //!
  725. //! This function simultaneously disables the transmit and receive modules.
  726. //! When the module is disabled, no data or clocks are generated on the I2S
  727. //! signals.
  728. //!
  729. //! \return None.
  730. //
  731. //*****************************************************************************
  732. void
  733. I2STxRxDisable(unsigned long ulBase)
  734. {
  735. //
  736. // Check the arguments.
  737. //
  738. ASSERT(ulBase == I2S0_BASE);
  739. //
  740. // Disable the transmit and receive modules.
  741. //
  742. HWREG(ulBase + I2S_O_CFG) &= ~(I2S_CFG_TXEN | I2S_CFG_RXEN);
  743. }
  744. //*****************************************************************************
  745. //
  746. //! Configures the I2S transmit and receive modules.
  747. //!
  748. //! \param ulBase is the I2S module base address.
  749. //! \param ulConfig is the logical OR of the configuration options.
  750. //!
  751. //! This function is used to configure the options for the I2S transmit and
  752. //! receive channels with identical parameters. The parameter \e ulConfig is
  753. //! the logical OR of the following options:
  754. //!
  755. //! - \b I2S_CONFIG_FORMAT_I2S for standard I2S format,
  756. //! \b I2S_CONFIG_FORMAT_LEFT_JUST for left justified format, or
  757. //! \b I2S_CONFIG_FORMAT_RIGHT_JUST for right justified format.
  758. //! - \b I2S_CONFIG_SCLK_INVERT to invert the polarity of the serial bit clock.
  759. //! - \b I2S_CONFIG_MODE_DUAL for dual-channel stereo,
  760. //! \b I2S_CONFIG_MODE_COMPACT_16 for 16-bit compact stereo mode,
  761. //! \b I2S_CONFIG_MODE_COMPACT_8 for 8-bit compact stereo mode, or
  762. //! \b I2S_CONFIG_MODE_MONO for single-channel mono format.
  763. //! - \b I2S_CONFIG_CLK_MASTER or \b I2S_CONFIG_CLK_SLAVE to select whether
  764. //! the I2S transmitter is the clock master or slave.
  765. //! - \b I2S_CONFIG_SAMPLE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
  766. //! to select the number of bits per sample.
  767. //! - \b I2S_CONFIG_WIRE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
  768. //! to select the number of bits per word that are transferred on the data
  769. //! line.
  770. //! - \b I2S_CONFIG_EMPTY_ZERO or \b I2S_CONFIG_EMPTY_REPEAT to select whether
  771. //! the module transmits zeroes or repeats the last sample when the FIFO is
  772. //! empty.
  773. //!
  774. //! \return None.
  775. //
  776. //*****************************************************************************
  777. void
  778. I2STxRxConfigSet(unsigned long ulBase, unsigned long ulConfig)
  779. {
  780. //
  781. // Check the arguments.
  782. //
  783. ASSERT(ulBase == I2S0_BASE);
  784. ASSERT((ulConfig & (I2S_CONFIG_FORMAT_MASK | I2S_CONFIG_MODE_MASK |
  785. I2S_CONFIG_EMPTY_MASK | I2S_CONFIG_CLK_MASK |
  786. I2S_CONFIG_SAMPLE_SIZE_MASK |
  787. I2S_CONFIG_WIRE_SIZE_MASK)) == ulConfig);
  788. //
  789. // Clear out any prior configuration of the FIFO config registers.
  790. //
  791. HWREG(ulBase + I2S_O_TXFIFOCFG) = 0;
  792. HWREG(ulBase + I2S_O_RXFIFOCFG) = 0;
  793. //
  794. // If mono mode is used, then the FMM bit needs to be set.
  795. //
  796. if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_MONO)
  797. {
  798. HWREG(ulBase + I2S_O_RXFIFOCFG) |= I2S_RXFIFOCFG_FMM;
  799. ulConfig &= ~(I2S_CONFIG_MODE_MONO);
  800. }
  801. //
  802. // If a compact mode is used, then the CSS bit needs to be set.
  803. //
  804. if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_COMPACT_8)
  805. {
  806. HWREG(ulBase + I2S_O_TXFIFOCFG) |= I2S_TXFIFOCFG_CSS;
  807. HWREG(ulBase + I2S_O_RXFIFOCFG) |= I2S_RXFIFOCFG_CSS;
  808. }
  809. //
  810. // Write the configuration register. Because all the fields are specified
  811. // by the configuration parameter, it is not necessary to do a
  812. // read-modify-write.
  813. //
  814. HWREG(ulBase + I2S_O_TXCFG) = ulConfig;
  815. HWREG(ulBase + I2S_O_RXCFG) = ulConfig;
  816. }
  817. //*****************************************************************************
  818. //
  819. //! Selects the source of the master clock, internal or external.
  820. //!
  821. //! \param ulBase is the I2S module base address.
  822. //! \param ulMClock is the logical OR of the master clock configuration
  823. //! choices.
  824. //!
  825. //! This function selects whether the master clock is sourced from the device
  826. //! internal PLL or comes from an external pin. The I2S serial bit clock
  827. //! (SCLK) and left-right word clock (LRCLK) are derived from the I2S master
  828. //! clock. The transmit and receive modules can be configured independently.
  829. //! The \e ulMClock parameter is chosen from the following:
  830. //!
  831. //! - one of \b I2S_TX_MCLK_EXT or \b I2S_TX_MCLK_INT
  832. //! - one of \b I2S_RX_MCLK_EXT or \b I2S_RX_MCLK_INT
  833. //!
  834. //! \return None.
  835. //
  836. //*****************************************************************************
  837. void
  838. I2SMasterClockSelect(unsigned long ulBase, unsigned long ulMClock)
  839. {
  840. unsigned long ulConfig;
  841. //
  842. // Check the arguments.
  843. //
  844. ASSERT(ulBase == I2S0_BASE);
  845. ASSERT((ulMClock & (I2S_TX_MCLK_EXT | I2S_RX_MCLK_EXT)) == ulMClock);
  846. //
  847. // Set the clock selection bits in the configuation word.
  848. //
  849. ulConfig = HWREG(ulBase + I2S_O_CFG) &
  850. ~(I2S_TX_MCLK_EXT | I2S_RX_MCLK_EXT);
  851. HWREG(ulBase + I2S_O_CFG) = ulConfig | ulMClock;
  852. }
  853. //*****************************************************************************
  854. //
  855. //! Enables I2S interrupt sources.
  856. //!
  857. //! \param ulBase is the I2S module base address.
  858. //! \param ulIntFlags is a bit mask of the interrupt sources to be enabled.
  859. //!
  860. //! This function enables the specified I2S sources to generate interrupts.
  861. //! The \e ulIntFlags parameter can be the logical OR of any of the following
  862. //! values:
  863. //!
  864. //! - \b I2S_INT_RXERR for receive errors
  865. //! - \b I2S_INT_RXREQ for receive FIFO service requests
  866. //! - \b I2S_INT_TXERR for transmit errors
  867. //! - \b I2S_INT_TXREQ for transmit FIFO service requests
  868. //!
  869. //! \return None.
  870. //
  871. //*****************************************************************************
  872. void
  873. I2SIntEnable(unsigned long ulBase, unsigned long ulIntFlags)
  874. {
  875. //
  876. // Check the arguments.
  877. //
  878. ASSERT(ulBase == I2S0_BASE);
  879. ASSERT((ulIntFlags & (I2S_INT_RXERR | I2S_INT_RXREQ |
  880. I2S_INT_TXERR | I2S_INT_TXREQ)) == ulIntFlags);
  881. //
  882. // Enable the specified interrupts.
  883. //
  884. HWREG(ulBase + I2S_O_IM) |= ulIntFlags;
  885. }
  886. //*****************************************************************************
  887. //
  888. //! Disables I2S interrupt sources.
  889. //!
  890. //! \param ulBase is the I2S module base address.
  891. //! \param ulIntFlags is a bit mask of the interrupt sources to be disabled.
  892. //!
  893. //! This function disables the specified I2S sources for interrupt
  894. //! generation. The \e ulIntFlags parameter can be the logical OR
  895. //! of any of the following values: \b I2S_INT_RXERR, \b I2S_INT_RXREQ,
  896. //! \b I2S_INT_TXERR, or \b I2S_INT_TXREQ.
  897. //!
  898. //! \return None.
  899. //
  900. //*****************************************************************************
  901. void
  902. I2SIntDisable(unsigned long ulBase, unsigned long ulIntFlags)
  903. {
  904. //
  905. // Check the arguments.
  906. //
  907. ASSERT(ulBase == I2S0_BASE);
  908. ASSERT((ulIntFlags & (I2S_INT_RXERR | I2S_INT_RXREQ |
  909. I2S_INT_TXERR | I2S_INT_TXREQ)) == ulIntFlags);
  910. //
  911. // Enable the specified interrupts.
  912. //
  913. HWREG(ulBase + I2S_O_IM) &= ~ulIntFlags;
  914. }
  915. //*****************************************************************************
  916. //
  917. //! Gets the I2S interrupt status.
  918. //!
  919. //! \param ulBase is the I2S module base address.
  920. //! \param bMasked is set \b true to get the masked interrupt status, or
  921. //! \b false to get the raw interrupt status.
  922. //!
  923. //! This function returns the I2S interrupt status. It can return either
  924. //! the raw or masked interrupt status.
  925. //!
  926. //! \return Returns the masked or raw I2S interrupt status, as a bit field
  927. //! of any of the following values: \b I2S_INT_RXERR, \b I2S_INT_RXREQ,
  928. //! \b I2S_INT_TXERR, or \b I2S_INT_TXREQ
  929. //
  930. //*****************************************************************************
  931. unsigned long
  932. I2SIntStatus(unsigned long ulBase, tBoolean bMasked)
  933. {
  934. //
  935. // Check the arguments.
  936. //
  937. ASSERT(ulBase == I2S0_BASE);
  938. //
  939. // Return either the interrupt status or the raw interrupt status as
  940. // requested.
  941. //
  942. if(bMasked)
  943. {
  944. return(HWREG(ulBase + I2S_O_MIS));
  945. }
  946. else
  947. {
  948. return(HWREG(ulBase + I2S_O_RIS));
  949. }
  950. }
  951. //*****************************************************************************
  952. //
  953. //! Clears pending I2S interrupt sources.
  954. //!
  955. //! \param ulBase is the I2S module base address.
  956. //! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.
  957. //!
  958. //! This function clears the specified pending I2S interrupts. This function
  959. //! must be called in the interrupt handler to keep the interrupt from being
  960. //! triggered again immediately upon exit. The \e ulIntFlags parameter can be
  961. //! the logical OR of any of the following values: \b I2S_INT_RXERR,
  962. //! \b I2S_INT_RXREQ, \b I2S_INT_TXERR, or \b I2S_INT_TXREQ.
  963. //!
  964. //! \note Because there is a write buffer in the Cortex-M processor, it may
  965. //! take several clock cycles before the interrupt source is actually cleared.
  966. //! Therefore, it is recommended that the interrupt source be cleared early in
  967. //! the interrupt handler (as opposed to the very last action) to avoid
  968. //! returning from the interrupt handler before the interrupt source is
  969. //! actually cleared. Failure to do so may result in the interrupt handler
  970. //! being immediately reentered (because the interrupt controller still sees
  971. //! the interrupt source asserted).
  972. //!
  973. //! \return None.
  974. //
  975. //*****************************************************************************
  976. void
  977. I2SIntClear(unsigned long ulBase, unsigned long ulIntFlags)
  978. {
  979. //
  980. // Check the arguments.
  981. //
  982. ASSERT(ulBase == I2S0_BASE);
  983. ASSERT((ulIntFlags & (I2S_INT_RXERR | I2S_INT_RXREQ |
  984. I2S_INT_TXERR | I2S_INT_TXREQ)) == ulIntFlags);
  985. //
  986. // Clear the requested interrupt sources.
  987. //
  988. HWREG(ulBase + I2S_O_IC) = ulIntFlags;
  989. }
  990. //*****************************************************************************
  991. //
  992. //! Registers an interrupt handler for the I2S controller.
  993. //!
  994. //! \param ulBase is the I2S module base address.
  995. //! \param pfnHandler is a pointer to the function to be called when the
  996. //! interrupt is activated.
  997. //!
  998. //! This function sets and enables the handler to be called when the I2S
  999. //! controller generates an interrupt. Specific I2S interrupts must still be
  1000. //! enabled with the I2SIntEnable() function. It is the responsibility of the
  1001. //! interrupt handler to clear any pending interrupts with I2SIntClear().
  1002. //!
  1003. //! \sa IntRegister() for important information about registering interrupt
  1004. //! handlers.
  1005. //!
  1006. //! \return None.
  1007. //
  1008. //*****************************************************************************
  1009. void
  1010. I2SIntRegister(unsigned long ulBase, void (*pfnHandler)(void))
  1011. {
  1012. //
  1013. // Check the arguments.
  1014. //
  1015. ASSERT(ulBase == I2S0_BASE);
  1016. ASSERT(pfnHandler);
  1017. //
  1018. // Register the interrupt handler.
  1019. //
  1020. IntRegister(INT_I2S0, pfnHandler);
  1021. //
  1022. // Enable the I2S interface interrupt.
  1023. //
  1024. IntEnable(INT_I2S0);
  1025. }
  1026. //*****************************************************************************
  1027. //
  1028. //! Unregisters an interrupt handler for the I2S controller.
  1029. //!
  1030. //! \param ulBase is the I2S module base address.
  1031. //!
  1032. //! This function disables and clears the handler to be called when the
  1033. //! I2S interrupt occurs.
  1034. //!
  1035. //! \sa IntRegister() for important information about registering interrupt
  1036. //! handlers.
  1037. //!
  1038. //! \return None.
  1039. //
  1040. //*****************************************************************************
  1041. void
  1042. I2SIntUnregister(unsigned long ulBase)
  1043. {
  1044. //
  1045. // Check the arguments.
  1046. //
  1047. ASSERT(ulBase == I2S0_BASE);
  1048. //
  1049. // Disable the I2S interface interrupt.
  1050. //
  1051. IntDisable(INT_I2S0);
  1052. //
  1053. // Unregister the interrupt handler.
  1054. //
  1055. IntUnregister(INT_I2S0);
  1056. }
  1057. //*****************************************************************************
  1058. //
  1059. // Close the Doxygen group.
  1060. //! @}
  1061. //
  1062. //*****************************************************************************