i2s.c 40 KB

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