i2s.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. //*****************************************************************************
  2. //
  3. // i2s.c - Driver for the I2S controller.
  4. //
  5. // Copyright (c) 2008-2010 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 6459 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 will be 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 will be 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 I2S_MODE_DUAL_STEREO then the \e ulData parameter
  102. //! contains either the left or right sample. The left and right sample
  103. //! alternate with each write to the FIFO, left sample first. If the transmit
  104. //! mode is I2S_MODE_COMPACT_STEREO_16 or I2S_MODE_COMPACT_STEREO_8, then the
  105. //! \e ulData parameter contains both the left and right samples. If the
  106. //! transmit mode is I2S_MODE_SINGLE_MONO then the \e ulData parameter
  107. //! contains the single channel sample.
  108. //!
  109. //! For the compact modes, both the left and right samples are written at
  110. //! the same time. If 16-bit compact mode is used, then the least significant
  111. //! 16 bits contain the left sample, and the most significant 16 bits contain
  112. //! the right sample. If 8-bit compact mode is used, then the lower 8 bits
  113. //! contain the left sample, and the next 8 bits contain the right sample,
  114. //! with the upper 16 bits unused.
  115. //!
  116. //! If there is no room in the transmit FIFO, then this function will wait
  117. //! in a polling loop until the data can be written.
  118. //!
  119. //! \return None.
  120. //
  121. //*****************************************************************************
  122. void
  123. I2STxDataPut(unsigned long ulBase, unsigned long ulData)
  124. {
  125. //
  126. // Check the arguments.
  127. //
  128. ASSERT(ulBase == I2S0_BASE);
  129. //
  130. // Wait until there is space.
  131. //
  132. while(HWREG(ulBase + I2S_O_TXLEV) >= 16)
  133. {
  134. }
  135. //
  136. // Write the data to the I2S.
  137. //
  138. HWREG(ulBase + I2S_O_TXFIFO) = ulData;
  139. }
  140. //*****************************************************************************
  141. //
  142. //! Writes data samples to the I2S transmit FIFO without blocking.
  143. //!
  144. //! \param ulBase is the I2S module base address.
  145. //! \param ulData is the single or dual channel I2S data.
  146. //!
  147. //! This function writes a single channel sample or combined left-right
  148. //! samples to the I2S transmit FIFO. The format of the sample is determined
  149. //! by the configuration that was used with the function I2STxConfigSet().
  150. //! If the transmit mode is I2S_MODE_DUAL_STEREO then the \e ulData parameter
  151. //! contains either the left or right sample. The left and right sample
  152. //! alternate with each write to the FIFO, left sample first. If the transmit
  153. //! mode is I2S_MODE_COMPACT_STEREO_16 or I2S_MODE_COMPACT_STEREO_8, then the
  154. //! \e ulData parameter contains both the left and right samples. If the
  155. //! transmit mode is I2S_MODE_SINGLE_MONO then the \e ulData parameter
  156. //! contains the single channel sample.
  157. //!
  158. //! For the compact modes, both the left and right samples are written at
  159. //! the same time. If 16-bit compact mode is used, then the least significant
  160. //! 16 bits contain the left sample, and the most significant 16 bits contain
  161. //! the right sample. If 8-bit compact mode is used, then the lower 8 bits
  162. //! contain the left sample, and the next 8 bits contain the right sample,
  163. //! with the upper 16 bits unused.
  164. //!
  165. //! If there is no room in the transmit FIFO, then this function will return
  166. //! immediately without writing any data to the FIFO.
  167. //!
  168. //! \return The number of elements written to the I2S transmit FIFO (1 or 0).
  169. //
  170. //*****************************************************************************
  171. long
  172. I2STxDataPutNonBlocking(unsigned long ulBase, unsigned long ulData)
  173. {
  174. //
  175. // Check the arguments.
  176. //
  177. ASSERT(ulBase == I2S0_BASE);
  178. //
  179. // Check for space to write.
  180. //
  181. if(HWREG(ulBase + I2S_O_TXLEV) < 16)
  182. {
  183. HWREG(ulBase + I2S_O_TXFIFO) = ulData;
  184. return(1);
  185. }
  186. else
  187. {
  188. return(0);
  189. }
  190. }
  191. //*****************************************************************************
  192. //
  193. //! Configures the I2S transmit module.
  194. //!
  195. //! \param ulBase is the I2S module base address.
  196. //! \param ulConfig is the logical OR of the configuration options.
  197. //!
  198. //! This function is used to configure the options for the I2S transmit
  199. //! channel. The parameter \e ulConfig is the logical OR of the following
  200. //! options:
  201. //!
  202. //! - \b I2S_CONFIG_FORMAT_I2S for standard I2S format,
  203. //! \b I2S_CONFIG_FORMAT_LEFT_JUST for left justified format, or
  204. //! \b I2S_CONFIG_FORMAT_RIGHT_JUST for right justified format.
  205. //! - \b I2S_CONFIG_SCLK_INVERT to invert the polarity of the serial bit clock.
  206. //! - \b I2S_CONFIG_MODE_DUAL for dual channel stereo,
  207. //! \b I2S_CONFIG_MODE_COMPACT_16 for 16-bit compact stereo mode,
  208. //! \b I2S_CONFIG_MODE_COMPACT_8 for 8-bit compact stereo mode, or
  209. //! \b I2S_CONFIG_MODE_MONO for single channel mono format.
  210. //! - \b I2S_CONFIG_CLK_MASTER or \b I2S_CONFIG_CLK_SLAVE to select whether
  211. //! the I2S transmitter is the clock master or slave.
  212. //! - \b I2S_CONFIG_SAMPLE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
  213. //! to select the number of bits per sample.
  214. //! - \b I2S_CONFIG_WIRE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
  215. //! to select the number of bits per word that are transferred on the data
  216. //! line.
  217. //! - \b I2S_CONFIG_EMPTY_ZERO or \b I2S_CONFIG_EMPTY_REPEAT to select whether
  218. //! the module transmits zeroes or repeats the last sample when the FIFO is
  219. //! empty.
  220. //!
  221. //! \return None.
  222. //
  223. //*****************************************************************************
  224. void
  225. I2STxConfigSet(unsigned long ulBase, unsigned long ulConfig)
  226. {
  227. //
  228. // Check the arguments.
  229. //
  230. ASSERT(ulBase == I2S0_BASE);
  231. ASSERT((ulConfig & (I2S_CONFIG_FORMAT_MASK | I2S_CONFIG_MODE_MASK |
  232. I2S_CONFIG_EMPTY_MASK | I2S_CONFIG_CLK_MASK |
  233. I2S_CONFIG_SAMPLE_SIZE_MASK |
  234. I2S_CONFIG_WIRE_SIZE_MASK)) == ulConfig);
  235. //
  236. // Check to see if a compact mode is used.
  237. //
  238. if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_COMPACT_8)
  239. {
  240. //
  241. // If compact 8 mode is used, then need to adjust some bits
  242. // before writing the config register. Also set the FIFO
  243. // config register for 8 bit compact samples.
  244. //
  245. ulConfig &= ~I2S_CONFIG_MODE_MONO;
  246. HWREG(ulBase + I2S_O_TXFIFOCFG) = I2S_TXFIFOCFG_CSS;
  247. }
  248. else
  249. {
  250. //
  251. // If compact 8 mode is not used, then set the FIFO config
  252. // register for 16 bit. This is okay if a compact mode is
  253. // not used.
  254. //
  255. HWREG(ulBase + I2S_O_TXFIFOCFG) = 0;
  256. }
  257. //
  258. // Write the configuration register. Since all the fields are
  259. // specified by the configuration parameter, it is not necessary
  260. // to do a read-modify-write.
  261. //
  262. HWREG(ulBase + I2S_O_TXCFG) = ulConfig;
  263. }
  264. //*****************************************************************************
  265. //
  266. //! Sets the FIFO level at which a service request is generated.
  267. //!
  268. //! \param ulBase is the I2S module base address.
  269. //! \param ulLevel is the FIFO service request limit.
  270. //!
  271. //! This function is used to set the transmit FIFO fullness level at which
  272. //! a service request will occur. The service request is used to generate
  273. //! an interrupt or a DMA transfer request. The transmit FIFO will
  274. //! generate a service request when the number of items in the FIFO is
  275. //! less than the level specified in the \e ulLevel parameter. For example,
  276. //! if \e ulLevel is 8, then a service request will be generated when
  277. //! there are less than 8 samples remaining in the transmit FIFO.
  278. //!
  279. //! For the purposes of counting the FIFO level, a left-right sample pair
  280. //! counts as 2, whether the mode is dual or compact stereo. When mono
  281. //! mode is used, internally the mono sample is still treated as a sample
  282. //! pair, so a single mono sample counts as 2. Since the FIFO always deals
  283. //! with sample pairs, the level must be an even number from 0 to 16. The
  284. //! maximum value is 16, which will cause a service request when there
  285. //! is any room in the FIFO. The minimum value is 0, which disables the
  286. //! service request.
  287. //!
  288. //! \return None.
  289. //
  290. //*****************************************************************************
  291. void
  292. I2STxFIFOLimitSet(unsigned long ulBase, unsigned long ulLevel)
  293. {
  294. //
  295. // Check the arguments.
  296. //
  297. ASSERT(ulBase == I2S0_BASE);
  298. ASSERT(ulLevel <= 16);
  299. //
  300. // Write the FIFO limit
  301. //
  302. HWREG(ulBase + I2S_O_TXLIMIT) = ulLevel;
  303. }
  304. //*****************************************************************************
  305. //
  306. //! Gets the current setting of the FIFO service request level.
  307. //!
  308. //! \param ulBase is the I2S module base address.
  309. //!
  310. //! This function is used to get the value of the transmit FIFO service
  311. //! request level. This value is set using the I2STxFIFOLimitSet()
  312. //! function.
  313. //!
  314. //! \return Returns the current value of the FIFO service request limit.
  315. //
  316. //*****************************************************************************
  317. unsigned long
  318. I2STxFIFOLimitGet(unsigned long ulBase)
  319. {
  320. //
  321. // Check the arguments.
  322. //
  323. ASSERT(ulBase == I2S0_BASE);
  324. //
  325. // Read and return the FIFO limit
  326. //
  327. return(HWREG(ulBase + I2S_O_TXLIMIT));
  328. }
  329. //*****************************************************************************
  330. //
  331. //! Gets the number of samples in the transmit FIFO.
  332. //!
  333. //! \param ulBase is the I2S module base address.
  334. //!
  335. //! This function is used to get the number of samples in the transmit
  336. //! FIFO. For the purposes of measuring the FIFO level, a left-right sample
  337. //! pair counts as 2, whether the mode is dual or compact stereo. When mono
  338. //! mode is used, internally the mono sample is still treated as a sample
  339. //! pair, so a single mono sample counts as 2. Since the FIFO always deals
  340. //! with sample pairs, normally the level will be an even number from 0 to
  341. //! 16. If dual stereo mode is used and only the left sample has been
  342. //! written without the matching right sample, then the FIFO level will be an
  343. //! odd value. If the FIFO level is odd, it indicates a left-right sample
  344. //! mismatch.
  345. //!
  346. //! \return Returns the number of samples in the transmit FIFO, which will
  347. //! normally be 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
  369. //! should be enabled after configuration. When the module is disabled,
  370. //! no data will be 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
  398. //! should be disabled before configuration. When the module is disabled,
  399. //! no data will be 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 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 I2S_MODE_COMPACT_STEREO_16 or I2S_MODE_COMPACT_STEREO_8, then the
  430. //! returned data contains both the left and right samples. If the
  431. //! receive mode is 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 will wait
  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 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 I2S_MODE_COMPACT_STEREO_16 or I2S_MODE_COMPACT_STEREO_8, then the
  479. //! received data contains both the left and right samples. If the
  480. //! receive mode is 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 will return
  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 needs to 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. Since 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
  595. //! a service request will occur. The service request is used to generate
  596. //! an interrupt or a DMA transfer request. The receive FIFO will
  597. //! generate a service request when the number of items in the FIFO is
  598. //! greater than the level specified in the \e ulLevel parameter. For example,
  599. //! if \e ulLevel is 4, then a service request will be generated when
  600. //! there are more than 4 samples 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
  604. //! mode is used, internally the mono sample is still treated as a sample
  605. //! pair, so a single mono sample counts as 2. Since the FIFO always deals
  606. //! with sample pairs, the level must be an even number from 0 to 16. The
  607. //! minimum value is 0, which will cause a service request when there
  608. //! is any data available in the FIFO. The maximum value is 16, which
  609. //! disables the service request (because there cannot be more than 16
  610. //! items in the FIFO).
  611. //!
  612. //! \return None.
  613. //
  614. //*****************************************************************************
  615. void
  616. I2SRxFIFOLimitSet(unsigned long ulBase, unsigned long ulLevel)
  617. {
  618. //
  619. // Check the arguments.
  620. //
  621. ASSERT(ulBase == I2S0_BASE);
  622. ASSERT(ulLevel <= 16);
  623. //
  624. // Write the FIFO limit
  625. //
  626. HWREG(ulBase + I2S_O_RXLIMIT) = ulLevel;
  627. }
  628. //*****************************************************************************
  629. //
  630. //! Gets the current setting of the FIFO service request level.
  631. //!
  632. //! \param ulBase is the I2S module base address.
  633. //!
  634. //! This function is used to get the value of the receive FIFO service
  635. //! request level. This value is set using the I2SRxFIFOLimitSet()
  636. //! function.
  637. //!
  638. //! \return Returns the current value of the FIFO service request limit.
  639. //
  640. //*****************************************************************************
  641. unsigned long
  642. I2SRxFIFOLimitGet(unsigned long ulBase)
  643. {
  644. //
  645. // Check the arguments.
  646. //
  647. ASSERT(ulBase == I2S0_BASE);
  648. //
  649. // Read and return the FIFO limit. The lower bit is masked
  650. // because it always reads as 1, and has no meaning.
  651. //
  652. return(HWREG(ulBase + I2S_O_RXLIMIT) & 0xFFFE);
  653. }
  654. //*****************************************************************************
  655. //
  656. //! Gets the number of samples in the receive FIFO.
  657. //!
  658. //! \param ulBase is the I2S module base address.
  659. //!
  660. //! This function is used to get the number of samples in the receive
  661. //! FIFO. For the purposes of measuring the FIFO level, a left-right sample
  662. //! pair counts as 2, whether the mode is dual or compact stereo. When mono
  663. //! mode is used, internally the mono sample is still treated as a sample
  664. //! pair, so a single mono sample counts as 2. Since the FIFO always deals
  665. //! with sample pairs, normally the level will be an even number from 0 to
  666. //! 16. If dual stereo mode is used and only the left sample has been
  667. //! read without reading the matching right sample, then the FIFO level will
  668. //! be an odd value. If the FIFO level is odd, it indicates a left-right
  669. //! sample mismatch.
  670. //!
  671. //! \return Returns the number of samples in the transmit FIFO, which will
  672. //! normally be an even number.
  673. //
  674. //*****************************************************************************
  675. unsigned long
  676. I2SRxFIFOLevelGet(unsigned long ulBase)
  677. {
  678. //
  679. // Check the arguments.
  680. //
  681. ASSERT(ulBase == I2S0_BASE);
  682. //
  683. // Read and return the receive FIFO level.
  684. //
  685. return(HWREG(ulBase + I2S_O_RXLEV));
  686. }
  687. //*****************************************************************************
  688. //
  689. //! Enables the I2S transmit and receive modules for operation.
  690. //!
  691. //! \param ulBase is the I2S module base address.
  692. //!
  693. //! This function simultaneously enables the transmit and receive modules for
  694. //! operation, providing a synchronized SCLK and LRCLK. The module should be
  695. //! enabled after configuration. When the module is disabled, no data or
  696. //! clocks will be generated on the I2S signals.
  697. //!
  698. //! \return None.
  699. //
  700. //*****************************************************************************
  701. void
  702. I2STxRxEnable(unsigned long ulBase)
  703. {
  704. //
  705. // Check the arguments.
  706. //
  707. ASSERT(ulBase == I2S0_BASE);
  708. //
  709. // Enable the Tx FIFO service request.
  710. //
  711. HWREG(ulBase + I2S_O_TXISM) = I2S_TXISM_FFM;
  712. //
  713. // Enable the Rx FIFO service request.
  714. //
  715. HWREG(ulBase + I2S_O_RXISM) = I2S_RXISM_FFM;
  716. //
  717. // Enable the transmit and receive modules.
  718. //
  719. HWREG(ulBase + I2S_O_CFG) |= I2S_CFG_TXEN | I2S_CFG_RXEN;
  720. }
  721. //*****************************************************************************
  722. //
  723. //! Disables the I2S transmit and receive modules.
  724. //!
  725. //! \param ulBase is the I2S module base address.
  726. //!
  727. //! This function simultaneously disables the transmit and receive modules.
  728. //! When the module is disabled, no data or clocks will be generated on the I2S
  729. //! signals.
  730. //!
  731. //! \return None.
  732. //
  733. //*****************************************************************************
  734. void
  735. I2STxRxDisable(unsigned long ulBase)
  736. {
  737. //
  738. // Check the arguments.
  739. //
  740. ASSERT(ulBase == I2S0_BASE);
  741. //
  742. // Disable the transmit and receive modules.
  743. //
  744. HWREG(ulBase + I2S_O_CFG) &= ~(I2S_CFG_TXEN | I2S_CFG_RXEN);
  745. }
  746. //*****************************************************************************
  747. //
  748. //! Configures the I2S transmit and receive modules.
  749. //!
  750. //! \param ulBase is the I2S module base address.
  751. //! \param ulConfig is the logical OR of the configuration options.
  752. //!
  753. //! This function is used to configure the options for the I2S transmit and
  754. //! receive channels with identical parameters. The parameter \e ulConfig is
  755. //! the logical OR of the following options:
  756. //!
  757. //! - \b I2S_CONFIG_FORMAT_I2S for standard I2S format,
  758. //! \b I2S_CONFIG_FORMAT_LEFT_JUST for left justified format, or
  759. //! \b I2S_CONFIG_FORMAT_RIGHT_JUST for right justified format.
  760. //! - \b I2S_CONFIG_SCLK_INVERT to invert the polarity of the serial bit clock.
  761. //! - \b I2S_CONFIG_MODE_DUAL for dual channel stereo,
  762. //! \b I2S_CONFIG_MODE_COMPACT_16 for 16-bit compact stereo mode,
  763. //! \b I2S_CONFIG_MODE_COMPACT_8 for 8-bit compact stereo mode, or
  764. //! \b I2S_CONFIG_MODE_MONO for single channel mono format.
  765. //! - \b I2S_CONFIG_CLK_MASTER or \b I2S_CONFIG_CLK_SLAVE to select whether
  766. //! the I2S transmitter is the clock master or slave.
  767. //! - \b I2S_CONFIG_SAMPLE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
  768. //! to select the number of bits per sample.
  769. //! - \b I2S_CONFIG_WIRE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
  770. //! to select the number of bits per word that are transferred on the data
  771. //! line.
  772. //! - \b I2S_CONFIG_EMPTY_ZERO or \b I2S_CONFIG_EMPTY_REPEAT to select whether
  773. //! the module transmits zeroes or repeats the last sample when the FIFO is
  774. //! empty.
  775. //!
  776. //! \return None.
  777. //
  778. //*****************************************************************************
  779. void
  780. I2STxRxConfigSet(unsigned long ulBase, unsigned long ulConfig)
  781. {
  782. //
  783. // Check the arguments.
  784. //
  785. ASSERT(ulBase == I2S0_BASE);
  786. ASSERT((ulConfig & (I2S_CONFIG_FORMAT_MASK | I2S_CONFIG_MODE_MASK |
  787. I2S_CONFIG_EMPTY_MASK | I2S_CONFIG_CLK_MASK |
  788. I2S_CONFIG_SAMPLE_SIZE_MASK |
  789. I2S_CONFIG_WIRE_SIZE_MASK)) == ulConfig);
  790. //
  791. // Clear out any prior configuration of the FIFO config registers.
  792. //
  793. HWREG(ulBase + I2S_O_TXFIFOCFG) = 0;
  794. HWREG(ulBase + I2S_O_RXFIFOCFG) = 0;
  795. //
  796. // If mono mode is used, then the FMM bit needs to be set.
  797. //
  798. if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_MONO)
  799. {
  800. HWREG(ulBase + I2S_O_RXFIFOCFG) |= I2S_RXFIFOCFG_FMM;
  801. ulConfig &= ~(I2S_CONFIG_MODE_MONO);
  802. }
  803. //
  804. // If a compact mode is used, then the CSS bit needs to be set.
  805. //
  806. if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_COMPACT_8)
  807. {
  808. HWREG(ulBase + I2S_O_TXFIFOCFG) |= I2S_TXFIFOCFG_CSS;
  809. HWREG(ulBase + I2S_O_RXFIFOCFG) |= I2S_RXFIFOCFG_CSS;
  810. }
  811. //
  812. // Write the configuration register. Since all the fields are specified by
  813. // the configuration parameter, it is not necessary to do a
  814. // read-modify-write.
  815. //
  816. HWREG(ulBase + I2S_O_TXCFG) = ulConfig;
  817. HWREG(ulBase + I2S_O_RXCFG) = ulConfig;
  818. }
  819. //*****************************************************************************
  820. //
  821. //! Selects the source of the master clock, internal or external.
  822. //!
  823. //! \param ulBase is the I2S module base address.
  824. //! \param ulMClock is the logical OR of the master clock configuration
  825. //! choices.
  826. //!
  827. //! This function selects whether the master clock is sourced from the device
  828. //! internal PLL, or comes from an external pin. The I2S serial bit clock
  829. //! (SCLK) and left-right word clock (LRCLK) are derived from the I2S master
  830. //! clock. The transmit and receive modules can be configured independently.
  831. //! The \e ulMClock parameter is chosen from the following:
  832. //!
  833. //! - one of \b I2S_TX_MCLK_EXT or \b I2S_TX_MCLK_INT
  834. //! - one of \b I2S_RX_MCLK_EXT or \b I2S_RX_MCLK_INT
  835. //!
  836. //! \return Returns None.
  837. //
  838. //*****************************************************************************
  839. void
  840. I2SMasterClockSelect(unsigned long ulBase, unsigned long ulMClock)
  841. {
  842. unsigned long ulConfig;
  843. //
  844. // Check the arguments.
  845. //
  846. ASSERT(ulBase == I2S0_BASE);
  847. ASSERT((ulMClock & (I2S_TX_MCLK_EXT | I2S_RX_MCLK_EXT)) == ulMClock);
  848. //
  849. // Set the clock selection bits in the configuation word.
  850. //
  851. ulConfig = HWREG(ulBase + I2S_O_CFG) &
  852. ~(I2S_TX_MCLK_EXT | I2S_RX_MCLK_EXT);
  853. HWREG(ulBase + I2S_O_CFG) = ulConfig | ulMClock;
  854. }
  855. //*****************************************************************************
  856. //
  857. //! Enables I2S interrupt sources.
  858. //!
  859. //! \param ulBase is the I2S module base address.
  860. //! \param ulIntFlags is a bit mask of the interrupt sources to be enabled.
  861. //!
  862. //! This function enables the specified I2S sources to generate interrupts.
  863. //! The \e ulIntFlags parameter can be the logical OR of any of the following
  864. //! values:
  865. //!
  866. //! - \b I2S_INT_RXERR for receive errors
  867. //! - \b I2S_INT_RXREQ for receive FIFO service requests
  868. //! - \b I2S_INT_TXERR for transmit errors
  869. //! - \b I2S_INT_TXREQ for transmit FIFO service requests
  870. //!
  871. //! \return Returns None.
  872. //
  873. //*****************************************************************************
  874. void
  875. I2SIntEnable(unsigned long ulBase, unsigned long ulIntFlags)
  876. {
  877. //
  878. // Check the arguments.
  879. //
  880. ASSERT(ulBase == I2S0_BASE);
  881. ASSERT((ulIntFlags & (I2S_INT_RXERR | I2S_INT_RXREQ |
  882. I2S_INT_TXERR | I2S_INT_TXREQ)) == ulIntFlags);
  883. //
  884. // Enable the specified interrupts.
  885. //
  886. HWREG(ulBase + I2S_O_IM) |= ulIntFlags;
  887. }
  888. //*****************************************************************************
  889. //
  890. //! Disables I2S interrupt sources.
  891. //!
  892. //! \param ulBase is the I2S module base address.
  893. //! \param ulIntFlags is a bit mask of the interrupt sources to be disabled.
  894. //!
  895. //! This function disables the specified I2S sources for interrupt
  896. //! generation. The \e ulIntFlags parameter can be the logical OR
  897. //! of any of the following values: \b I2S_INT_RXERR, \b I2S_INT_RXREQ,
  898. //! \b I2S_INT_TXERR, or \b I2S_INT_TXREQ.
  899. //!
  900. //! \return Returns None.
  901. //
  902. //*****************************************************************************
  903. void
  904. I2SIntDisable(unsigned long ulBase, unsigned long ulIntFlags)
  905. {
  906. //
  907. // Check the arguments.
  908. //
  909. ASSERT(ulBase == I2S0_BASE);
  910. ASSERT((ulIntFlags & (I2S_INT_RXERR | I2S_INT_RXREQ |
  911. I2S_INT_TXERR | I2S_INT_TXREQ)) == ulIntFlags);
  912. //
  913. // Enable the specified interrupts.
  914. //
  915. HWREG(ulBase + I2S_O_IM) &= ~ulIntFlags;
  916. }
  917. //*****************************************************************************
  918. //
  919. //! Gets the I2S interrupt status.
  920. //!
  921. //! \param ulBase is the I2S module base address.
  922. //! \param bMasked is set \b true to get the masked interrupt status, or
  923. //! \b false to get the raw interrupt status.
  924. //!
  925. //! This function returns the I2S interrupt status. It can return either
  926. //! the raw or masked interrupt status.
  927. //!
  928. //! \return Returns the masked or raw I2S interrupt status, as a bit field
  929. //! of any of the following values: \b I2S_INT_RXERR, \b I2S_INT_RXREQ,
  930. //! \b I2S_INT_TXERR, or \b I2S_INT_TXREQ
  931. //
  932. //*****************************************************************************
  933. unsigned long
  934. I2SIntStatus(unsigned long ulBase, tBoolean bMasked)
  935. {
  936. //
  937. // Check the arguments.
  938. //
  939. ASSERT(ulBase == I2S0_BASE);
  940. //
  941. // Return either the interrupt status or the raw interrupt status as
  942. // requested.
  943. //
  944. if(bMasked)
  945. {
  946. return(HWREG(ulBase + I2S_O_MIS));
  947. }
  948. else
  949. {
  950. return(HWREG(ulBase + I2S_O_RIS));
  951. }
  952. }
  953. //*****************************************************************************
  954. //
  955. //! Clears pending I2S interrupt sources.
  956. //!
  957. //! \param ulBase is the I2S module base address.
  958. //! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.
  959. //!
  960. //! This function clears the specified pending I2S interrupts. This must
  961. //! be done in the interrupt handler to keep the handler from being called
  962. //! again immediately upon exit. The \e ulIntFlags parameter can be the
  963. //! logical OR of any of the following values: \b I2S_INT_RXERR,
  964. //! \b I2S_INT_RXREQ, \b I2S_INT_TXERR, or \b I2S_INT_TXREQ.
  965. //!
  966. //! \note Since there is a write buffer in the Cortex-M3 processor, it may take
  967. //! several clock cycles before the interrupt source is actually cleared.
  968. //! Therefore, it is recommended that the interrupt source be cleared early in
  969. //! the interrupt handler (as opposed to the very last action) to avoid
  970. //! returning from the interrupt handler before the interrupt source is
  971. //! actually cleared. Failure to do so may result in the interrupt handler
  972. //! being immediately reentered (since NVIC still sees the interrupt source
  973. //! asserted).
  974. //!
  975. //! \return Returns None.
  976. //
  977. //*****************************************************************************
  978. void
  979. I2SIntClear(unsigned long ulBase, unsigned long ulIntFlags)
  980. {
  981. //
  982. // Check the arguments.
  983. //
  984. ASSERT(ulBase == I2S0_BASE);
  985. ASSERT((ulIntFlags & (I2S_INT_RXERR | I2S_INT_RXREQ |
  986. I2S_INT_TXERR | I2S_INT_TXREQ)) == ulIntFlags);
  987. //
  988. // Clear the requested interrupt sources.
  989. //
  990. HWREG(ulBase + I2S_O_IC) = ulIntFlags;
  991. }
  992. //*****************************************************************************
  993. //
  994. //! Registers an interrupt handler for the I2S controller.
  995. //!
  996. //! \param ulBase is the I2S module base address.
  997. //! \param pfnHandler is a pointer to the function to be called when the
  998. //! interrupt is activated.
  999. //!
  1000. //! This sets and enables the handler to be called when the I2S controller
  1001. //! generates an interrupt. Specific I2S interrupts must still be enabled
  1002. //! with the I2SIntEnable() function. It is the responsibility of the
  1003. //! interrupt handler to clear any pending interrupts with I2SIntClear().
  1004. //!
  1005. //! \sa IntRegister() for important information about registering interrupt
  1006. //! handlers.
  1007. //!
  1008. //! \return None.
  1009. //
  1010. //*****************************************************************************
  1011. void
  1012. I2SIntRegister(unsigned long ulBase, void (*pfnHandler)(void))
  1013. {
  1014. //
  1015. // Check the arguments.
  1016. //
  1017. ASSERT(ulBase == I2S0_BASE);
  1018. ASSERT(pfnHandler);
  1019. //
  1020. // Register the interrupt handler.
  1021. //
  1022. IntRegister(INT_I2S0, pfnHandler);
  1023. //
  1024. // Enable the I2S interface interrupt.
  1025. //
  1026. IntEnable(INT_I2S0);
  1027. }
  1028. //*****************************************************************************
  1029. //
  1030. //! Unregisters an interrupt handler for the I2S controller.
  1031. //!
  1032. //! \param ulBase is the I2S module base address.
  1033. //!
  1034. //! This function will disable and clear the handler to be called when the
  1035. //! I2S interrupt occurs.
  1036. //!
  1037. //! \sa IntRegister() for important information about registering interrupt
  1038. //! handlers.
  1039. //!
  1040. //! \return None.
  1041. //
  1042. //*****************************************************************************
  1043. void
  1044. I2SIntUnregister(unsigned long ulBase)
  1045. {
  1046. //
  1047. // Check the arguments.
  1048. //
  1049. ASSERT(ulBase == I2S0_BASE);
  1050. //
  1051. // Disable the I2S interface interrupt.
  1052. //
  1053. IntDisable(INT_I2S0);
  1054. //
  1055. // Unregister the interrupt handler.
  1056. //
  1057. IntUnregister(INT_I2S0);
  1058. }
  1059. //*****************************************************************************
  1060. //
  1061. // Close the Doxygen group.
  1062. //! @}
  1063. //
  1064. //*****************************************************************************