i2s.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. /**
  2. * \file
  3. *
  4. * \brief SAM I2S - Inter-IC Sound Controller
  5. *
  6. * Copyright (c) 2014-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef I2S_H_INCLUDED
  47. #define I2S_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_sam0_i2s_group SAM Inter-IC Sound Controller (I2S) Driver
  50. *
  51. * This driver for Atmel&reg; | SMART ARM&reg;-based microcontrollers provides
  52. * an interface for the configuration and management of the device's Inter-IC
  53. * Sound Controller functionality.
  54. *
  55. * The following driver API modes are covered by this manual:
  56. * - Polled APIs
  57. * \if I2S_CALLBACK_MODE
  58. * - Callback APIs
  59. * \endif
  60. *
  61. * The following peripheral is used by this module:
  62. * - I<SUP>2</SUP>S (Inter-IC Sound Controller)
  63. *
  64. * The following devices can use this module:
  65. * - Atmel | SMART SAM D21
  66. * - Atmel | SMART SAM DA1
  67. *
  68. * The outline of this documentation is as follows:
  69. * - \ref asfdoc_sam0_i2s_prerequisites
  70. * - \ref asfdoc_sam0_i2s_module_overview
  71. * - \ref asfdoc_sam0_i2s_special_considerations
  72. * - \ref asfdoc_sam0_i2s_extra_info
  73. * - \ref asfdoc_sam0_i2s_examples
  74. * - \ref asfdoc_sam0_i2s_api_overview
  75. *
  76. * \section asfdoc_sam0_i2s_prerequisites Prerequisites
  77. *
  78. * There are no prerequisites for this module.
  79. *
  80. * \section asfdoc_sam0_i2s_module_overview Module Overview
  81. *
  82. * The I<SUP>2</SUP>S provides bidirectional, synchronous, digital audio link with
  83. * external audio devices through these signal pins:
  84. * - Serial Data (SDm)
  85. * - Frame Sync (FSn)
  86. * - Serial Clock (SCKn)
  87. * - Master Clock (MCKn)
  88. *
  89. * The I<SUP>2</SUP>S consists of two Clock Units and two Serializers, which can be
  90. * separately configured and enabled, to provide varies functionalities as follow:
  91. * - Communicate to Audio CODECs as Master or Slave, or provides clock and
  92. * frame sync signals as Controller
  93. * - Communicate to DAC or ADC through dedicated I<SUP>2</SUP>S serial interface
  94. * - Communicate to multi-slot or multiple stereo DACs or ADCs, via
  95. * Time Division Multiplexed (TDM) format
  96. * - Reading mono or stereo MEMS microphones, using the Pulse Density
  97. * Modulation (PDM) interface
  98. *
  99. * The I<SUP>2</SUP>S supports compact stereo data word, where left channel data bits are
  100. * in lower half and right channel data bits are in upper half. It reduces the
  101. * number of data words for stereo audio data and the DMA bandwidth.
  102. *
  103. * In master mode, the frame is configured by number of slots and slot size, and
  104. * allows range covering 16fs to 1024fs MCK, to provide oversampling clock to an
  105. * external audio CODEC or digital signal processor (DSP).
  106. *
  107. * A block diagram of the I<SUP>2</SUP>S can be seen in
  108. * \ref asfdoc_sam0_i2s_module_block_diagram "the figure below".
  109. *
  110. * \anchor asfdoc_sam0_i2s_module_block_diagram
  111. * \image html i2s_blocks.svg "I2S Block Diagram"
  112. *
  113. * This driver for I<SUP>2</SUP>S module provides an interface to:
  114. * - Initialize and control I<SUP>2</SUP>S module
  115. * - Configure and control the I<SUP>2</SUP>S Clock Unit and Serializer
  116. * - Transmit/receive data through I<SUP>2</SUP>S Serializer
  117. *
  118. * \subsection asfdoc_sam0_i2s_module_overview_clocks Clocks
  119. *
  120. * To use I<SUP>2</SUP>S module, the I<SUP>2</SUP>S bus interface clock (clk_i2s)
  121. * must be enabled via Power Manager.
  122. *
  123. * For each I<SUP>2</SUP>S Clock Unit, a generic clock (gclk_i2s_n) is connnected.
  124. * When I<SUP>2</SUP>S works in master mode the generic clock is used. It should
  125. * be prepared before clock unit is used. In master mode the input generic clock
  126. * will be used as MCK for SCKn and FSn generation, in addition, the MCK could be
  127. * devided and output to I<SUP>2</SUP>S MCKn pin, as oversampling clock to
  128. * external audio device.
  129. *
  130. * The I<SUP>2</SUP>S Serializer uses clock and control signal from Clock Unit to handle
  131. * transfer. Select different clock unit with different configurations allows
  132. * the I<SUP>2</SUP>S to work as master or slave, to work on non-related clocks.
  133. *
  134. * When using the driver with ASF, enabling the register interface is normally
  135. * done by the \c init function.
  136. * The Generic Clock Controller (GCLK) source for the asynchronous domain is
  137. * normally configured and set through the _configuration
  138. * struct_ / _init_ function.
  139. * If GCLK source != 0 is used, this source has to be configured and enabled
  140. * through invoking the system_gclk driver function when needed, or modifying
  141. * conf_clock.h to enable it at the beginning.
  142. *
  143. * \subsection asfdoc_sam0_i2s_module_overview_frame Audio Frame Generation
  144. *
  145. * Audio sample data for all channels are sent in frames, one frame can consist
  146. * 1 - 8 slots where each slot can be configured to a size 8-bit, 16-bit, 24-bit,
  147. * or 32-bit. The audio frame synch clock is generated by the I<SUP>2</SUP>S
  148. * Clock unit in the master/controller mode. The frame rate (or frame sync
  149. * frequency) is calculated as follows:
  150. *
  151. * FS = SCK / number_of_slots / number_of_bits_in_slot
  152. *
  153. * The serial clock (SCK) source is either an external source (slave mode) or
  154. * generated by the I<SUP>2</SUP>S clock unit (controller or master mode) using
  155. * the MCK as source.
  156. *
  157. * SCK = MCK / sck_div
  158. * \note SCK generation division value is MCKDIV in register.
  159. *
  160. * MCK is either an external source or generated using the GCLK input from a
  161. * generic clock generator.
  162. *
  163. * \subsection asfdoc_sam0_i2s_module_overview_mode Master, Controller, and Slave Modes
  164. *
  165. * The I<SUP>2</SUP>S module has three modes: master, controller, and slave.
  166. *
  167. * \subsubsection asfdoc_sam0_i2s_module_overview_mode_mst Master
  168. * In master mode the module will control the data flow on the I<SUP>2</SUP>S bus and can
  169. * be responsible for clock generation. The Serializers are enabled and will
  170. * transmit/receive data. On a bus with only master and slave the SCK, and FS
  171. * clock signal will be outputted on the SCK and FS pin on the master module.
  172. * MCK can optionally be outputted on the MCK pin, if there is a controller
  173. * module on the bus the SCK, FS, and optionally the MCK clock is sourced from
  174. * the same pins. Serial data will be trancieved on the SD pin in both
  175. * scenarios.
  176. *
  177. * \subsubsection asfdoc_sam0_i2s_module_overview_mode_ctl Controller
  178. * In controller mode the module will generate the clock signals, but the
  179. * Serializers are disabled and no data will be transmitted/received by the
  180. * module in this mode. The clock signals is outputted on the SCK, FS and
  181. * optionally the MCK pin.
  182. *
  183. * \subsubsection asfdoc_sam0_i2s_module_overview_mode_slv Slave
  184. * In slave mode the module will use the SCK and FS clock source from the master
  185. * or the controller which is received on the SCK and FS pin. The MCK can
  186. * optionally be sourced externally on the MCK pin. The Serializers are enabled
  187. * and will tranceive data on the SD pin. All data flow is controlled by the
  188. * master.
  189. *
  190. * \subsubsection asfdoc_sam0_i2s_module_overview_mode_chg Switch Modes
  191. * The mode switching between master, controller, and slave modes are actually
  192. * done by modifying the source mode of I<SUP>2</SUP>S pins.
  193. * The source mode of I<SUP>2</SUP>S pins are selected by writing corresponding
  194. * bits in CLKCTRLn.
  195. * Since source mode switching changes the direction of pin, the mode must be
  196. * changed when the I<SUP>2</SUP>S Clock Unit is stopped.
  197. *
  198. * \subsection asfdoc_sam0_i2s_module_overview_data Data Stream Reception/Transmission
  199. *
  200. * The I<SUP>2</SUP>S module support several data stream formats:
  201. * - I<SUP>2</SUP>S format
  202. * - Time Division Multiplexed (TDM) format
  203. * - Pulse Density Modulation (PDM) format (reception only)
  204. *
  205. * Basically the I<SUP>2</SUP>S module can send several words within each frame,
  206. * it's more like TDM format. With adjust to the number of data words in a frame,
  207. * the FS width, the FS to data bits delay, etc., the module is able to handle
  208. * I<SUP>2</SUP>S compliant data stream.
  209. *
  210. * Also the Serializer can receive PDM format data stream, which allows the
  211. * I<SUP>2</SUP>S module receive 1 PDM data on each SCK edge.
  212. *
  213. * \subsubsection asfdoc_sam0_i2s_module_overview_data_i2s I2S Stream Reception/Transmission
  214. *
  215. * For 2-channel I<SUP>2</SUP>S compliant data stream format the I<SUP>2</SUP>S
  216. * module uses the FS line as word select (WS) signal and will send left channel
  217. * data word on low WS level and right channel data word on high WS level as
  218. * specified in the I<SUP>2</SUP>S standard. The supported word sizes are 8-,
  219. * 16-, 18-, 20-, 24-, and 32- bit.
  220. *
  221. * Thus for I<SUP>2</SUP>S stream, the following settings should be applied to the module:
  222. * - Data starting delay after FS transition : one SCK period
  223. * - FS width : half of frame
  224. * - Data bits adjust in word : left-adjusted
  225. * - Bit transmitting order : MSB first
  226. *
  227. * Following is an example for I<SUP>2</SUP>S application connections and waveforms. See
  228. * the figure below.
  229. *
  230. * \anchor asfdoc_sam0_i2s_module_i2s_example_diagram
  231. * \image html i2s_example.svg "I2S Example Diagram"
  232. *
  233. * \subsubsection asfdoc_sam0_i2s_module_overview_data_tdm TDM Stream Reception/Transmission
  234. * In TDM format, the module sends several data words in each frame. For this
  235. * data stream format most of the configurations could be adjusted:
  236. * - Main Frame related settings are as follow:
  237. * - Frame Sync (FS) options:
  238. * - The active edge of the FS (or if FS is inverted before use)
  239. * - The width of the FS
  240. * - The delay between FS to first data bit
  241. * - Data alignment in slot
  242. * - The number of slots and slot size can be adjusted, it has been mentioned
  243. * in \ref asfdoc_sam0_i2s_module_overview_frame
  244. * - The data word size is controlled by Serializer, it can be chosen among
  245. * 8, 16, 18, 20, 24, and 32 bits.
  246. *
  247. * The general TDM waveform generation is as follows:
  248. *
  249. * \anchor asfdoc_sam0_i2s_module_tdm_wave_diagram
  250. * \image html tdm_wave.svg "TDM Waveform Generation"
  251. *
  252. * Some other settings could also be found to set up clock, data formatting and
  253. * pin multiplexer (MUX).
  254. * Refer to \ref i2s_clock_unit_config "Clock Unit Configurations"
  255. * and \ref i2s_serializer_config "Serializer Configurations" for more
  256. * details.
  257. *
  258. * Following is examples for different application use cases.
  259. *
  260. * See \ref asfdoc_sam0_i2s_module_tdm_timeslot_example_diagram "here" for
  261. * the Time Slot Application connection and waveform example.
  262. *
  263. * \anchor asfdoc_sam0_i2s_module_tdm_timeslot_example_diagram
  264. * \image html tdm_timeslot_example.svg "Codec Example Diagram"
  265. *
  266. * See \ref asfdoc_sam0_i2s_module_tdm_codec_example_diagram "here" for the
  267. * Codec Application connection and waveform example.
  268. *
  269. * \anchor asfdoc_sam0_i2s_module_tdm_codec_example_diagram
  270. * \image html tdm_codec_example.svg "Time Slot Example Diagram"
  271. *
  272. * \subsubsection asfdoc_sam0_i2s_module_overview_data_pdm PDM Reception
  273. * The I<SUP>2</SUP>S Serializer integrates PDM reception feature, to use this feature,
  274. * simply select PDM2 mode in Serializer configuration. In PDM2 mode, it assumes
  275. * two microphones are input for stereo stream. The left microphone bits will
  276. * be stored in lower half and right microphone bits in upper half of the data
  277. * word, like in compact stereo format.
  278. *
  279. * See \ref asfdoc_sam0_i2s_module_pdm_example_diagram "following figure" for an
  280. * example of PDM Microphones Application with both left and right channel
  281. * microphone connected.
  282. *
  283. * \anchor asfdoc_sam0_i2s_module_pdm_example_diagram
  284. * \image html pdm_example.svg "Time PDM2 Example Diagram"
  285. *
  286. * \subsubsection asfdoc_sam0_i2s_module_overview_data_fmt MONO and Compact Data
  287. * The I<SUP>2</SUP>S Serializer can accept some pre-defined data format and generates
  288. * the data stream in specified way.
  289. *
  290. * When transmitting data, the Serializer can work in MONO mode: assum input
  291. * is single channel mono data on left channel and copy it to right channel
  292. * automatically.
  293. *
  294. * Also the I<SUP>2</SUP>S Serializer can support compact stereo data word. The data word
  295. * size of the Serializer can be set to \ref I2S_DATA_SIZE_16BIT_COMPACT
  296. * "16-bit compact" or \ref I2S_DATA_SIZE_8BIT_COMPACT "8-bit compact", with
  297. * these option I<SUP>2</SUP>S Serializer will compact left channel data and right channel
  298. * data together, the left channel data will take lower bytes and right channel
  299. * data take higher bytes.
  300. *
  301. * \subsection asfdoc_sam0_i2s_module_overview_loop Loop-back Mode
  302. * The I<SUP>2</SUP>S can be configured to loop back the Transmitter to Receiver. In this
  303. * mode Serializer's input will be connected to another Serializer's output
  304. * internally.
  305. *
  306. * \subsection asfdoc_sam0_i2s_module_overview_sleep Sleep Modes
  307. * The I<SUP>2</SUP>S will continue to operate in any sleep mode, where the selected source
  308. * clocks are running.
  309. *
  310. * \section asfdoc_sam0_i2s_special_considerations Special Considerations
  311. *
  312. * There is no special considerations for I<SUP>2</SUP>S module.
  313. *
  314. * \section asfdoc_sam0_i2s_extra_info Extra Information
  315. *
  316. * For extra information see \ref asfdoc_sam0_i2s_extra. This includes:
  317. * - \ref asfdoc_sam0_i2s_extra_acronyms
  318. * - \ref asfdoc_sam0_i2s_extra_dependencies
  319. * - \ref asfdoc_sam0_i2s_extra_errata
  320. * - \ref asfdoc_sam0_i2s_extra_history
  321. *
  322. * \section asfdoc_sam0_i2s_examples Examples
  323. *
  324. * For a list of examples related to this driver, see
  325. * \ref asfdoc_sam0_i2s_exqsg.
  326. *
  327. *
  328. * \section asfdoc_sam0_i2s_api_overview API Overview
  329. * @{
  330. */
  331. #ifdef __cplusplus
  332. extern "C" {
  333. #endif
  334. #include <compiler.h>
  335. #include <system.h>
  336. #if I2S_CALLBACK_MODE == true
  337. # include <system_interrupt.h>
  338. #if !defined(__DOXYGEN__)
  339. extern struct i2s_module *_i2s_instances[I2S_INST_NUM];
  340. #endif
  341. /** Forward definition of the device instance */
  342. struct i2s_module;
  343. /** Type of the callback functions. */
  344. typedef void (*i2s_serializer_callback_t)
  345. (struct i2s_module *const module);
  346. /**
  347. * \brief I<SUP>2</SUP>S Serializer Callback enum
  348. */
  349. enum i2s_serializer_callback {
  350. /** Callback for buffer read/write finished */
  351. I2S_SERIALIZER_CALLBACK_BUFFER_DONE,
  352. /** Callback for Serializer overrun/underrun */
  353. I2S_SERIALIZER_CALLBACK_OVER_UNDER_RUN,
  354. # if !defined(__DOXYGEN__)
  355. I2S_SERIALIZER_CALLBACK_N
  356. # endif
  357. };
  358. #endif /* #if I2S_CALLBACK_MODE == true */
  359. /**
  360. * \name Module Status Flags
  361. *
  362. * I<SUP>2</SUP>S status flags, returned by \ref i2s_get_status() and cleared by
  363. * \ref i2s_clear_status().
  364. *
  365. * @{
  366. */
  367. /** Module Serializer x (0~1) Transmit Underrun. */
  368. #define I2S_STATUS_TRANSMIT_UNDERRUN(x) (1u << ((x)+0))
  369. /** Module Serializer x (0~1) is ready to accept new data to be transmitted. */
  370. #define I2S_STATUS_TRANSMIT_READY(x) (1u << ((x)+2))
  371. /** Module Serializer x (0~1) Receive Overrun. */
  372. #define I2S_STATUS_RECEIVE_OVERRUN(x) (1u << ((x)+4))
  373. /** Module Serializer x (0~1) has received a new data. */
  374. #define I2S_STATUS_RECEIVE_READY(x) (1u << ((x)+6))
  375. /** Module is busy on synchronization. */
  376. #define I2S_STATUS_SYNC_BUSY (1u << 8)
  377. /** @} */
  378. /**
  379. * Master Clock (MCK) source selection.
  380. */
  381. enum i2s_master_clock_source {
  382. /** Master Clock (MCK) is from general clock */
  383. I2S_MASTER_CLOCK_SOURCE_GCLK,
  384. /** Master Clock (MCK) is from MCK input pin */
  385. I2S_MASTER_CLOCK_SOURCE_MCKPIN
  386. };
  387. /**
  388. * Serial Clock (SCK) source selection.
  389. */
  390. enum i2s_serial_clock_source {
  391. /** Serial Clock (SCK) is divided from Master Clock */
  392. I2S_SERIAL_CLOCK_SOURCE_MCKDIV,
  393. /** Serial Clock (SCK) is input from SCK input pin */
  394. I2S_SERIAL_CLOCK_SOURCE_SCKPIN
  395. };
  396. /**
  397. * Data delay from Frame Sync (FS).
  398. */
  399. enum i2s_data_delay {
  400. /** Left Justified (no delay) */
  401. I2S_DATA_DELAY_0,
  402. /** I<SUP>2</SUP>S data delay (1-bit delay) */
  403. I2S_DATA_DELAY_1,
  404. /** Left Justified (no delay) */
  405. I2S_DATA_DELAY_LEFT_JUSTIFIED = I2S_DATA_DELAY_0,
  406. /** I<SUP>2</SUP>S data delay (1-bit delay) */
  407. I2S_DATA_DELAY_I2S = I2S_DATA_DELAY_1
  408. };
  409. /**
  410. * Frame Sync (FS) source.
  411. */
  412. enum i2s_frame_sync_source {
  413. /** Frame Sync (FS) is divided from I<SUP>2</SUP>S Serial Clock */
  414. I2S_FRAME_SYNC_SOURCE_SCKDIV,
  415. /** Frame Sync (FS) is input from FS input pin */
  416. I2S_FRAME_SYNC_SOURCE_FSPIN
  417. };
  418. /**
  419. * Frame Sync (FS) output pulse width.
  420. */
  421. enum i2s_frame_sync_width {
  422. /** Frame Sync (FS) Pulse is one slot width */
  423. I2S_FRAME_SYNC_WIDTH_SLOT,
  424. /** Frame Sync (FS) Pulse is half a frame width */
  425. I2S_FRAME_SYNC_WIDTH_HALF_FRAME,
  426. /** Frame Sync (FS) Pulse is one bit width */
  427. I2S_FRAME_SYNC_WIDTH_BIT,
  428. /** 1-bit wide Frame Sync (FS) per Data sample, only used when Data transfer
  429. * is requested */
  430. I2S_FRAME_SYNC_WIDTH_BURST
  431. };
  432. /**
  433. * Time Slot Size in number of I<SUP>2</SUP>S serial clocks (bits).
  434. */
  435. enum i2s_slot_size {
  436. /** 8-bit slot */
  437. I2S_SLOT_SIZE_8_BIT,
  438. /** 16-bit slot */
  439. I2S_SLOT_SIZE_16_BIT,
  440. /** 24-bit slot */
  441. I2S_SLOT_SIZE_24_BIT,
  442. /** 32-bit slot */
  443. I2S_SLOT_SIZE_32_BIT
  444. };
  445. /**
  446. * DMA channels usage for I<SUP>2</SUP>S.
  447. */
  448. enum i2s_dma_usage {
  449. /** Single DMA channel for all I<SUP>2</SUP>S channels */
  450. I2S_DMA_USE_SINGLE_CHANNEL_FOR_ALL,
  451. /** One DMA channel per data channel */
  452. I2S_DMA_USE_ONE_CHANNEL_PER_DATA_CHANNEL
  453. };
  454. /**
  455. * I<SUP>2</SUP>S data format, to extend mono data to two channels.
  456. */
  457. enum i2s_data_format {
  458. /** Normal mode, keep data to its right channel */
  459. I2S_DATA_FORMAT_STEREO,
  460. /** Assume input is mono data for left channel, the data is duplicated to
  461. * right channel */
  462. I2S_DATA_FORMAT_MONO
  463. };
  464. /**
  465. * I<SUP>2</SUP>S data bit order.
  466. */
  467. enum i2s_bit_order {
  468. /** Transfer Data Most Significant Bit first
  469. * (Default for I<SUP>2</SUP>S protocol)
  470. */
  471. I2S_BIT_ORDER_MSB_FIRST,
  472. /** Transfer Data Least Significant Bit first */
  473. I2S_BIT_ORDER_LSB_FIRST
  474. };
  475. /**
  476. * I<SUP>2</SUP>S data bit padding.
  477. */
  478. enum i2s_bit_padding {
  479. /** Padding with 0 */
  480. I2S_BIT_PADDING_0,
  481. /** Padding with 1 */
  482. I2S_BIT_PADDING_1,
  483. /** Padding with MSBit */
  484. I2S_BIT_PADDING_MSB,
  485. /** Padding with LSBit */
  486. I2S_BIT_PADDING_LSB,
  487. };
  488. /**
  489. * I<SUP>2</SUP>S data word adjust.
  490. */
  491. enum i2s_data_adjust {
  492. /** Data is right adjusted in word */
  493. I2S_DATA_ADJUST_RIGHT,
  494. /** Data is left adjusted in word */
  495. I2S_DATA_ADJUST_LEFT
  496. };
  497. /**
  498. * I<SUP>2</SUP>S data word size.
  499. */
  500. enum i2s_data_size {
  501. /** 32-bit */
  502. I2S_DATA_SIZE_32BIT,
  503. /** 24-bit */
  504. I2S_DATA_SIZE_24BIT,
  505. /** 20-bit */
  506. I2S_DATA_SIZE_20BIT,
  507. /** 18-bit */
  508. I2S_DATA_SIZE_18BIT,
  509. /** 16-bit */
  510. I2S_DATA_SIZE_16BIT,
  511. /** 16-bit compact stereo */
  512. I2S_DATA_SIZE_16BIT_COMPACT,
  513. /** 8-bit */
  514. I2S_DATA_SIZE_8BIT,
  515. /** 8-bit compact stereo */
  516. I2S_DATA_SIZE_8BIT_COMPACT
  517. };
  518. /**
  519. * I<SUP>2</SUP>S data slot adjust.
  520. */
  521. enum i2s_slot_adjust {
  522. /** Data is right adjusted in slot */
  523. I2S_SLOT_ADJUST_RIGHT,
  524. /** Data is left adjusted in slot */
  525. I2S_SLOT_ADJUST_LEFT
  526. };
  527. /**
  528. * I<SUP>2</SUP>S data padding.
  529. */
  530. enum i2s_data_padding {
  531. /** Padding 0 in case of under-run */
  532. I2S_DATA_PADDING_0,
  533. /** Padding last data in case of under-run */
  534. I2S_DATA_PADDING_SAME_AS_LAST,
  535. /** Padding last data in case of under-run
  536. * (abbr. \c I2S_DATA_PADDING_SAME_AS_LAST) */
  537. I2S_DATA_PADDING_LAST = I2S_DATA_PADDING_SAME_AS_LAST,
  538. /** Padding last data in case of under-run
  539. * (abbr. \c I2S_DATA_PADDING_SAME_AS_LAST) */
  540. I2S_DATA_PADDING_SAME = I2S_DATA_PADDING_SAME_AS_LAST
  541. };
  542. /**
  543. * I<SUP>2</SUP>S line default value when slot disabled.
  544. */
  545. enum i2s_line_default_state {
  546. /** Output default value is 0 */
  547. I2S_LINE_DEFAULT_0,
  548. /** Output default value is 1 */
  549. I2S_LINE_DEFAULT_1,
  550. /** Output default value is high impedance */
  551. I2S_LINE_DEFAULT_HIGH_IMPEDANCE = 3,
  552. /** Output default value is high impedance
  553. * (abbr. \c I2S_LINE_DEFAULT_HIGH_IMPEDANCE) */
  554. I2S_LINE_DEFAULT_HIZ = I2S_LINE_DEFAULT_HIGH_IMPEDANCE
  555. };
  556. /**
  557. * I<SUP>2</SUP>S Serializer mode.
  558. */
  559. enum i2s_serializer_mode {
  560. /** Serializer is used to receive data */
  561. I2S_SERIALIZER_RECEIVE,
  562. /** Serializer is used to transmit data */
  563. I2S_SERIALIZER_TRANSMIT,
  564. /** Serializer is used to receive PDM data on each clock edge */
  565. I2S_SERIALIZER_PDM2
  566. };
  567. /**
  568. * I<SUP>2</SUP>S clock unit selection.
  569. */
  570. enum i2s_clock_unit {
  571. /** Clock Unit channel 0 */
  572. I2S_CLOCK_UNIT_0,
  573. /** Clock Unit channel 1 */
  574. I2S_CLOCK_UNIT_1,
  575. /** Number of Clock Unit channels */
  576. I2S_CLOCK_UNIT_N
  577. };
  578. /**
  579. * I<SUP>2</SUP>S Serializer selection.
  580. */
  581. enum i2s_serializer {
  582. /** Serializer channel 0 */
  583. I2S_SERIALIZER_0,
  584. /** Serializer channel 1 */
  585. I2S_SERIALIZER_1,
  586. /** Number of Serializer channels */
  587. I2S_SERIALIZER_N
  588. };
  589. /**
  590. * Configure for I<SUP>2</SUP>S pin.
  591. */
  592. struct i2s_pin_config {
  593. /** GPIO index to access the pin */
  594. uint8_t gpio;
  595. /** Pin function MUX */
  596. uint8_t mux;
  597. /** Enable this pin for I<SUP>2</SUP>S module */
  598. bool enable;
  599. };
  600. /**
  601. * Configure for I<SUP>2</SUP>S clock (SCK).
  602. */
  603. struct i2s_clock_config {
  604. /** Divide generic clock to master clock output (1~32, 0,1 means no div) */
  605. uint8_t mck_out_div;
  606. /** Divide generic clock to serial clock (1~32, 0,1 means no div) */
  607. uint8_t sck_div;
  608. /** Clock source selection */
  609. enum gclk_generator gclk_src;
  610. /** Master clock source selection: generated or input from pin */
  611. enum i2s_master_clock_source mck_src;
  612. /** Serial clock source selection: generated or input from pin */
  613. enum i2s_serial_clock_source sck_src;
  614. /** Invert master clock output */
  615. bool mck_out_invert;
  616. /** Invert serial clock output */
  617. bool sck_out_invert;
  618. /** Generate MCK clock output */
  619. bool mck_out_enable;
  620. };
  621. /**
  622. * Configure for I<SUP>2</SUP>S frame sync (FS).
  623. */
  624. struct i2s_frame_sync_config {
  625. /** Frame Sync (FS) generated or input from pin */
  626. enum i2s_frame_sync_source source;
  627. /** Frame Sync (FS) width */
  628. enum i2s_frame_sync_width width;
  629. /** Invert Frame Sync (FS) signal before use */
  630. bool invert_use;
  631. /** Invert Frame Sync (FS) signal before output */
  632. bool invert_out;
  633. };
  634. /**
  635. * Configure for I<SUP>2</SUP>S frame.
  636. */
  637. struct i2s_frame_config {
  638. /** Number of slots in a frame (1~8, 0,1 means minimum 1) */
  639. uint8_t number_slots;
  640. /** Size of each slot in frame */
  641. enum i2s_slot_size slot_size;
  642. /** Data delay from Frame Sync (FS) to first data bit */
  643. enum i2s_data_delay data_delay;
  644. /** Frame sync (FS) */
  645. struct i2s_frame_sync_config frame_sync;
  646. };
  647. /**
  648. * Configure for I<SUP>2</SUP>S clock unit.
  649. */
  650. struct i2s_clock_unit_config {
  651. /** Configure clock generation */
  652. struct i2s_clock_config clock;
  653. /** Configure frame generation */
  654. struct i2s_frame_config frame;
  655. /** Configure master clock pin */
  656. struct i2s_pin_config mck_pin;
  657. /** Configure serial clock pin */
  658. struct i2s_pin_config sck_pin;
  659. /** Configure frame sync pin */
  660. struct i2s_pin_config fs_pin;
  661. };
  662. /**
  663. * Configure for I<SUP>2</SUP>S Serializer.
  664. */
  665. struct i2s_serializer_config {
  666. /** Configure Serializer data pin */
  667. struct i2s_pin_config data_pin;
  668. /** Set to \c true to loop-back output to input pin for test */
  669. bool loop_back;
  670. /** Set to \c true to assumes mono input and duplicate it (left channel) to
  671. * right channel */
  672. bool mono_mode;
  673. /** Disable data slot */
  674. bool disable_data_slot[8];
  675. /** Set to \c true to transfer LSB first, \c false to transfer MSB first */
  676. bool transfer_lsb_first;
  677. /** Data Word Formatting Adjust,
  678. * set to \c true to adjust bits in word to left */
  679. bool data_adjust_left_in_word;
  680. /** Data Slot Formatting Adjust,
  681. * set to \c true to adjust words in slot to left */
  682. bool data_adjust_left_in_slot;
  683. /** Data Word Size */
  684. enum i2s_data_size data_size;
  685. /** Data Formatting Bit Extension */
  686. enum i2s_bit_padding bit_padding;
  687. /** Data padding when under-run */
  688. enum i2s_data_padding data_padding;
  689. /** DMA usage */
  690. enum i2s_dma_usage dma_usage;
  691. /** Clock unit selection */
  692. enum i2s_clock_unit clock_unit;
  693. /** Line default state where slot is disabled */
  694. enum i2s_line_default_state line_default_state;
  695. /** Serializer Mode */
  696. enum i2s_serializer_mode mode;
  697. };
  698. /**
  699. * \brief I<SUP>2</SUP>S Serializer instance struct.
  700. */
  701. struct i2s_serializer_module {
  702. #if I2S_CALLBACK_MODE == true
  703. /** Callbacks list for Serializer */
  704. i2s_serializer_callback_t callback[I2S_SERIALIZER_CALLBACK_N];
  705. /** Job buffer */
  706. void *job_buffer;
  707. /** Requested data words to read/write */
  708. uint32_t requested_words;
  709. /** Transferred data words for read/write */
  710. uint32_t transferred_words;
  711. /** Callback mask for registered callbacks */
  712. uint8_t registered_callback_mask;
  713. /** Callback mask for enabled callbacks */
  714. uint8_t enabled_callback_mask;
  715. /** Status of the ongoing or last transfer job */
  716. enum status_code job_status;
  717. #endif
  718. /** Serializer mode */
  719. enum i2s_serializer_mode mode;
  720. /** Serializer data word size */
  721. enum i2s_data_size data_size;
  722. };
  723. /**
  724. * \brief I<SUP>2</SUP>S Software Module instance struct.
  725. */
  726. struct i2s_module {
  727. /** Module HW register access base */
  728. I2s *hw;
  729. /** Module Serializer used */
  730. struct i2s_serializer_module serializer[2];
  731. };
  732. /**
  733. * \brief Determines if the hardware module(s) are currently synchronizing to the bus.
  734. *
  735. * Checks to see if the underlying hardware peripheral module(s) are currently
  736. * synchronizing across multiple clock domains to the hardware bus, This
  737. * function can be used to delay further operations on a module until such time
  738. * that it is ready, to prevent blocking delays for synchronization in the
  739. * user application.
  740. *
  741. * \param[in] module_inst Pointer to the software module instance struct
  742. *
  743. * \return Synchronization status of the underlying hardware module(s).
  744. *
  745. * \retval false If the module has completed synchronization
  746. * \retval true If the module synchronization is ongoing
  747. */
  748. static inline bool i2s_is_syncing(
  749. const struct i2s_module *const module_inst)
  750. {
  751. /* Sanity check arguments */
  752. Assert(module_inst);
  753. Assert(module_inst->hw);
  754. return (module_inst->hw->SYNCBUSY.reg > 0);
  755. }
  756. /**
  757. * \name Driver Initialization
  758. * @{
  759. */
  760. enum status_code i2s_init(
  761. struct i2s_module *const module_inst,
  762. I2s *hw);
  763. /** @} */
  764. /**
  765. * \name Enable/Disable/Reset
  766. * @{
  767. */
  768. /**
  769. * \brief Enable the I<SUP>2</SUP>S module.
  770. *
  771. * Enables a I<SUP>2</SUP>S module that has been previously initialized.
  772. *
  773. * \param[in] module_inst Pointer to the software module instance struct
  774. */
  775. static inline void i2s_enable(const struct i2s_module *const module_inst)
  776. {
  777. Assert(module_inst);
  778. Assert(module_inst->hw);
  779. while (module_inst->hw->SYNCBUSY.reg & I2S_SYNCBUSY_ENABLE) {
  780. /* Sync wait */
  781. }
  782. module_inst->hw->CTRLA.reg |= I2S_SYNCBUSY_ENABLE;
  783. }
  784. /**
  785. * \brief Disables the I<SUP>2</SUP>S module.
  786. *
  787. * Disables a I<SUP>2</SUP>S module.
  788. *
  789. * \param[in] module_inst Pointer to the software module instance struct
  790. */
  791. static inline void i2s_disable(const struct i2s_module *const module_inst)
  792. {
  793. Assert(module_inst);
  794. Assert(module_inst->hw);
  795. while (module_inst->hw->SYNCBUSY.reg & I2S_SYNCBUSY_ENABLE) {
  796. /* Sync wait */
  797. }
  798. module_inst->hw->INTENCLR.reg = I2S_INTENCLR_MASK;
  799. module_inst->hw->INTFLAG.reg = I2S_INTFLAG_MASK;
  800. module_inst->hw->CTRLA.reg &= ~I2S_SYNCBUSY_ENABLE;
  801. }
  802. /**
  803. * \brief Resets the I<SUP>2</SUP>S module.
  804. *
  805. * Resets the I<SUP>2</SUP>S module, restoring all hardware module registers to their
  806. * default values and disabling the module. The I<SUP>2</SUP>S module will not be
  807. * accessible while the reset is being performed.
  808. *
  809. * \param[in] module_inst Pointer to the software module instance struct
  810. */
  811. static inline void i2s_reset(const struct i2s_module *const module_inst)
  812. {
  813. Assert(module_inst);
  814. Assert(module_inst->hw);
  815. /* Disable the module if it is running */
  816. if (module_inst->hw->CTRLA.reg & I2S_CTRLA_ENABLE) {
  817. i2s_disable(module_inst);
  818. while (i2s_is_syncing(module_inst)) {
  819. /* Sync wait */
  820. }
  821. }
  822. /* Reset the HW module */
  823. module_inst->hw->CTRLA.reg = I2S_CTRLA_SWRST;
  824. }
  825. /** @} */
  826. /**
  827. * \name Clock Unit Initialization and Configuration
  828. * @{
  829. */
  830. /**
  831. * \brief Initializes config with predefined default values for I<SUP>2</SUP>S clock unit.
  832. *
  833. * This function will initialize a given I<SUP>2</SUP>S Clock Unit configuration structure
  834. * to a set of known default values. This function should be called on any new
  835. * instance of the configuration structures before being modified by the user
  836. * application.
  837. *
  838. * The default configuration is as follows:
  839. * - The clock unit does not generate output clocks (MCK, SCK, and FS)
  840. * - The pins (MCK, SCK, and FS) and MUX configurations are not set
  841. *
  842. * \param[out] config Pointer to a I<SUP>2</SUP>S module clock unit configuration struct
  843. * to set
  844. */
  845. static inline void i2s_clock_unit_get_config_defaults(
  846. struct i2s_clock_unit_config *const config)
  847. {
  848. Assert(config);
  849. config->clock.mck_out_enable = false;
  850. config->clock.gclk_src = GCLK_GENERATOR_0;
  851. config->clock.mck_src = I2S_MASTER_CLOCK_SOURCE_GCLK;
  852. config->clock.mck_out_div = 1;
  853. config->clock.mck_out_invert = false;
  854. config->clock.sck_src = I2S_SERIAL_CLOCK_SOURCE_MCKDIV;
  855. config->clock.sck_div = 1;
  856. config->clock.sck_out_invert = false;
  857. config->frame.number_slots = 1;
  858. config->frame.slot_size = I2S_SLOT_SIZE_32_BIT;
  859. config->frame.data_delay = I2S_DATA_DELAY_I2S;
  860. config->frame.frame_sync.source = I2S_FRAME_SYNC_SOURCE_SCKDIV;
  861. config->frame.frame_sync.width = I2S_FRAME_SYNC_WIDTH_HALF_FRAME;
  862. config->frame.frame_sync.invert_use = false;
  863. config->frame.frame_sync.invert_out = false;
  864. config->mck_pin.enable = false;
  865. config->mck_pin.mux = 0;
  866. config->mck_pin.gpio = 0;
  867. config->sck_pin.enable = false;
  868. config->sck_pin.mux = 0;
  869. config->sck_pin.gpio = 0;
  870. config->fs_pin.enable = false;
  871. config->fs_pin.mux = 0;
  872. config->fs_pin.gpio = 0;
  873. }
  874. enum status_code i2s_clock_unit_set_config(
  875. struct i2s_module *const module_inst,
  876. const enum i2s_clock_unit clock_unit,
  877. const struct i2s_clock_unit_config *config);
  878. /** @} */
  879. /**
  880. * \name Clock Unit Enable/Disable
  881. * @{
  882. */
  883. /**
  884. * \brief Enable the Specified Clock Unit of I<SUP>2</SUP>S module.
  885. *
  886. * Enables a Clock Unit in I<SUP>2</SUP>S module that has been previously initialized.
  887. *
  888. * \param[in] module_inst Pointer to the software module instance struct
  889. * \param[in] clock_unit I<SUP>2</SUP>S Clock Unit to enable
  890. */
  891. static inline void i2s_clock_unit_enable(
  892. const struct i2s_module *const module_inst,
  893. const enum i2s_clock_unit clock_unit)
  894. {
  895. uint32_t cken_bit;
  896. Assert(module_inst);
  897. Assert(module_inst->hw);
  898. cken_bit = I2S_CTRLA_CKEN0 << clock_unit;
  899. while (module_inst->hw->SYNCBUSY.reg & cken_bit) {
  900. /* Sync wait */
  901. }
  902. module_inst->hw->CTRLA.reg |= cken_bit;
  903. }
  904. /**
  905. * \brief Disable the Specified Clock Unit of I<SUP>2</SUP>S module.
  906. *
  907. * Disables a Clock Unit in I<SUP>2</SUP>S module that has been previously initialized.
  908. *
  909. * \param[in] module_inst Pointer to the software module instance struct
  910. * \param[in] clock_unit I<SUP>2</SUP>S Clock Unit to disable
  911. */
  912. static inline void i2s_clock_unit_disable(
  913. const struct i2s_module *const module_inst,
  914. const enum i2s_clock_unit clock_unit)
  915. {
  916. uint32_t cken_bit;
  917. Assert(module_inst);
  918. Assert(module_inst->hw);
  919. cken_bit = I2S_CTRLA_CKEN0 << clock_unit;
  920. while (module_inst->hw->SYNCBUSY.reg & cken_bit) {
  921. /* Sync wait */
  922. }
  923. module_inst->hw->CTRLA.reg &= ~cken_bit;
  924. }
  925. /** @} */
  926. /**
  927. * \name Serializer Initialization and Configuration
  928. * @{
  929. */
  930. /**
  931. * \brief Initializes config with predefined default values for I<SUP>2</SUP>S Serializer.
  932. *
  933. * This function will initialize a given I<SUP>2</SUP>S Clock Unit configuration structure
  934. * to a set of known default values. This function should be called on any new
  935. * instance of the configuration structures before being modified by the user
  936. * application.
  937. *
  938. * The default configuration is as follows:
  939. * - Output data does not internally loopback to input line
  940. * - Does not extend mono data (left channel) to right channel
  941. * - None of the data slot is disabled
  942. * - MSB of I<SUP>2</SUP>S data is transferred first
  943. * - In data word data is adjusted right
  944. * - In slot data word is adjusted left
  945. * - The data size is 16-bit width
  946. * - I<SUP>2</SUP>S will padd 0 to not defined bits
  947. * - I<SUP>2</SUP>S will padd 0 to not defined words
  948. * - I<SUP>2</SUP>S will use single DMA channel for all data channels
  949. * - I<SUP>2</SUP>S will use clock unit 0 to serve as clock
  950. * - The default data line state is 0, when there is no data
  951. * - I<SUP>2</SUP>S will transmit data to output line
  952. * - The data pin and MUX configuration are not set
  953. *
  954. * \param[out] config Pointer to a I<SUP>2</SUP>S module Serializer configuration struct
  955. * to set
  956. */
  957. static inline void i2s_serializer_get_config_defaults(
  958. struct i2s_serializer_config *const config)
  959. {
  960. config->loop_back = false;
  961. config->mono_mode = false;
  962. config->disable_data_slot[0] = false;
  963. config->disable_data_slot[1] = false;
  964. config->disable_data_slot[2] = false;
  965. config->disable_data_slot[3] = false;
  966. config->disable_data_slot[4] = false;
  967. config->disable_data_slot[5] = false;
  968. config->disable_data_slot[6] = false;
  969. config->disable_data_slot[7] = false;
  970. config->transfer_lsb_first = false;
  971. config->data_adjust_left_in_word = false;
  972. config->data_adjust_left_in_slot = true;
  973. config->data_size = I2S_DATA_SIZE_16BIT;
  974. config->bit_padding = I2S_BIT_PADDING_0;
  975. config->data_padding = I2S_DATA_PADDING_0;
  976. config->dma_usage = I2S_DMA_USE_SINGLE_CHANNEL_FOR_ALL;
  977. config->clock_unit = I2S_CLOCK_UNIT_0;
  978. config->line_default_state = I2S_LINE_DEFAULT_0;
  979. config->mode = I2S_SERIALIZER_TRANSMIT;
  980. config->data_pin.enable = false;
  981. config->data_pin.gpio = 0;
  982. config->data_pin.mux = 0;
  983. }
  984. enum status_code i2s_serializer_set_config(
  985. struct i2s_module *const module_inst,
  986. const enum i2s_serializer serializer,
  987. const struct i2s_serializer_config *config);
  988. /** @} */
  989. /**
  990. * \name Serializer Enable/Disable
  991. * @{
  992. */
  993. /**
  994. * \brief Enable the Specified Serializer of I<SUP>2</SUP>S module.
  995. *
  996. * Enables a Serializer in I<SUP>2</SUP>S module that has been previously initialized.
  997. *
  998. * \param[in] module_inst Pointer to the software module instance struct
  999. * \param[in] serializer I<SUP>2</SUP>S Serializer to enable
  1000. */
  1001. static inline void i2s_serializer_enable(
  1002. const struct i2s_module *const module_inst,
  1003. const enum i2s_serializer serializer)
  1004. {
  1005. uint32_t seren_bit;
  1006. Assert(module_inst);
  1007. Assert(module_inst->hw);
  1008. seren_bit = I2S_CTRLA_SEREN0 << serializer;
  1009. while (module_inst->hw->SYNCBUSY.reg & seren_bit) {
  1010. /* Sync wait */
  1011. }
  1012. module_inst->hw->CTRLA.reg |= seren_bit;
  1013. }
  1014. /**
  1015. * \brief Disable the Specified Serializer of I<SUP>2</SUP>S module.
  1016. *
  1017. * Disables a Serializer in I<SUP>2</SUP>S module that has been previously initialized.
  1018. *
  1019. * \param[in] module_inst Pointer to the software module instance struct
  1020. * \param[in] serializer I<SUP>2</SUP>S Serializer to disable
  1021. */
  1022. static inline void i2s_serializer_disable(
  1023. const struct i2s_module *const module_inst,
  1024. const enum i2s_serializer serializer)
  1025. {
  1026. uint32_t seren_bit;
  1027. Assert(module_inst);
  1028. Assert(module_inst->hw);
  1029. seren_bit = I2S_CTRLA_SEREN0 << serializer;
  1030. while (module_inst->hw->SYNCBUSY.reg & seren_bit) {
  1031. /* Sync wait */
  1032. }
  1033. module_inst->hw->CTRLA.reg &= ~seren_bit;
  1034. }
  1035. /** @} */
  1036. /**
  1037. * \name Status Management
  1038. * @{
  1039. */
  1040. uint32_t i2s_get_status(
  1041. const struct i2s_module *const module_inst);
  1042. void i2s_clear_status(
  1043. const struct i2s_module *const module_inst,
  1044. uint32_t status);
  1045. enum status_code i2s_enable_status_interrupt(
  1046. struct i2s_module *const module_inst,
  1047. uint32_t status);
  1048. void i2s_disable_status_interrupt(
  1049. struct i2s_module *const module_inst,
  1050. uint32_t status);
  1051. /** @}*/
  1052. /**
  1053. * \name Data Read/Write
  1054. * @{
  1055. */
  1056. /**
  1057. * \brief Write a data word to the specified Serializer of I<SUP>2</SUP>S module
  1058. *
  1059. * \param[in] module_inst Pointer to the software module instance struct
  1060. * \param[in] serializer The Serializer to write to
  1061. * \param[in] data The data to write
  1062. *
  1063. */
  1064. static inline void i2s_serializer_write_wait(
  1065. const struct i2s_module *const module_inst,
  1066. enum i2s_serializer serializer,
  1067. uint32_t data)
  1068. {
  1069. uint32_t sync_bit, ready_bit;
  1070. Assert(module_inst);
  1071. Assert(module_inst->hw);
  1072. ready_bit = I2S_INTFLAG_TXRDY0 << serializer;
  1073. while (!(module_inst->hw->INTFLAG.reg & ready_bit)) {
  1074. /* Wait until ready to transmit */
  1075. }
  1076. sync_bit = I2S_SYNCBUSY_DATA0 << serializer;
  1077. while (module_inst->hw->SYNCBUSY.reg & sync_bit) {
  1078. /* Wait sync */
  1079. }
  1080. /* Write data */
  1081. module_inst->hw->DATA[serializer].reg = data;
  1082. module_inst->hw->INTFLAG.reg = ready_bit;
  1083. }
  1084. /**
  1085. * \brief Read a data word from the specified Serializer of I<SUP>2</SUP>S module
  1086. *
  1087. * \param[in] module_inst Pointer to the software module instance struct
  1088. * \param[in] serializer The Serializer to read
  1089. */
  1090. static inline uint32_t i2s_serializer_read_wait(
  1091. const struct i2s_module *const module_inst,
  1092. enum i2s_serializer serializer)
  1093. {
  1094. uint32_t sync_bit, ready_bit;
  1095. uint32_t data;
  1096. Assert(module_inst);
  1097. Assert(module_inst->hw);
  1098. ready_bit = I2S_INTFLAG_RXRDY0 << serializer;
  1099. while (!(module_inst->hw->INTFLAG.reg & ready_bit)) {
  1100. /* Wait until ready to transmit */
  1101. }
  1102. sync_bit = I2S_SYNCBUSY_DATA0 << serializer;
  1103. while (module_inst->hw->SYNCBUSY.reg & sync_bit) {
  1104. /* Wait sync */
  1105. }
  1106. /* Read data */
  1107. data = module_inst->hw->DATA[serializer].reg;
  1108. module_inst->hw->INTFLAG.reg = ready_bit;
  1109. return data;
  1110. }
  1111. enum status_code i2s_serializer_write_buffer_wait(
  1112. const struct i2s_module *const module_inst,
  1113. enum i2s_serializer serializer,
  1114. void *buffer, uint32_t size);
  1115. enum status_code i2s_serializer_read_buffer_wait(
  1116. const struct i2s_module *const module_inst,
  1117. enum i2s_serializer serializer,
  1118. void *buffer, uint32_t size);
  1119. /** @} */
  1120. #ifdef __cplusplus
  1121. }
  1122. #endif
  1123. /** @} */
  1124. /**
  1125. * \page asfdoc_sam0_i2s_extra Extra Information for I2S Driver
  1126. *
  1127. * \section asfdoc_sam0_i2s_extra_acronyms Acronyms
  1128. * Below is a table listing the acronyms used in this module, along with their
  1129. * intended meanings.
  1130. *
  1131. * <table>
  1132. * <tr>
  1133. * <th>Acronym</th>
  1134. * <th>Description</th>
  1135. * </tr>
  1136. * <tr>
  1137. * <td>I<SUP>2</SUP>S, IIS</td>
  1138. * <td>Inter-IC Sound Controller</td>
  1139. * </tr>
  1140. * <tr>
  1141. * <td>MCK</td>
  1142. * <td>Master Clock</td>
  1143. * </tr>
  1144. * <tr>
  1145. * <td>SCK</td>
  1146. * <td>Serial Clock</td>
  1147. * </tr>
  1148. * <tr>
  1149. * <td>FS</td>
  1150. * <td>Frame Sync</td>
  1151. * </tr>
  1152. * <tr>
  1153. * <td>SD</td>
  1154. * <td>Serial Data</td>
  1155. * </tr>
  1156. * <tr>
  1157. * <td>ADC</td>
  1158. * <td>Analog-to-Digital Converter</td>
  1159. * </tr>
  1160. * <tr>
  1161. * <td>DAC</td>
  1162. * <td>Digital-to-Analog Converter</td>
  1163. * </tr>
  1164. * <tr>
  1165. * <td>TDM</td>
  1166. * <td>Time Division Multiplexed</td>
  1167. * </tr>
  1168. * <tr>
  1169. * <td>PDM</td>
  1170. * <td>Pulse Density Modulation</td>
  1171. * </tr>
  1172. * <tr>
  1173. * <td>LSB</td>
  1174. * <td>Least Significant Bit</td>
  1175. * </tr>
  1176. * <tr>
  1177. * <td>MSB</td>
  1178. * <td>Most Significant Bit</td>
  1179. * </tr>
  1180. * <tr>
  1181. * <td>DSP</td>
  1182. * <td>Digital Signal Processor</td>
  1183. * </tr>
  1184. * </table>
  1185. *
  1186. *
  1187. * \section asfdoc_sam0_i2s_extra_dependencies Dependencies
  1188. * This driver has the following dependencies:
  1189. *
  1190. * - \ref asfdoc_sam0_system_pinmux_group "System Pin Multiplexer Driver"
  1191. *
  1192. *
  1193. * \section asfdoc_sam0_i2s_extra_errata Errata
  1194. * There are no errata related to this driver.
  1195. *
  1196. *
  1197. * \section asfdoc_sam0_i2s_extra_history Module History
  1198. * An overview of the module history is presented in the table below, with
  1199. * details on the enhancements and fixes made to the module since its first
  1200. * release. The current version of this corresponds to the newest version in
  1201. * the table.
  1202. *
  1203. * <table>
  1204. * <tr>
  1205. * <th>Changelog</th>
  1206. * </tr>
  1207. * <tr>
  1208. * <td>Initial Release</td>
  1209. * </tr>
  1210. * </table>
  1211. */
  1212. /**
  1213. * \page asfdoc_sam0_i2s_exqsg Examples for I2S Driver
  1214. *
  1215. * This is a list of the available Quick Start guides (QSGs) and example
  1216. * applications for \ref asfdoc_sam0_i2s_group. QSGs are simple examples with
  1217. * step-by-step instructions to configure and use this driver in a selection of
  1218. * use cases. Note that QSGs can be compiled as a standalone application or be
  1219. * added to the user application.
  1220. *
  1221. * - \subpage asfdoc_sam0_i2s_basic_use_case
  1222. * \if I2S_CALLBACK_MODE
  1223. * - \subpage asfdoc_sam0_i2s_callback_use_case
  1224. * \endif
  1225. * - \subpage asfdoc_sam0_i2s_dma_use_case
  1226. *
  1227. * \page asfdoc_sam0_i2s_document_revision_history Document Revision History
  1228. *
  1229. * <table>
  1230. * <tr>
  1231. * <th>Doc. Rev.</th>
  1232. * <th>Date</th>
  1233. * <th>Comments</th>
  1234. * </tr>
  1235. * <tr>
  1236. * <td>42255B</td>
  1237. * <td>12/2015</td>
  1238. * <td>Added support for SAM DA1</td>
  1239. * </tr>
  1240. * <tr>
  1241. * <td>42255A</td>
  1242. * <td>01/2014</td>
  1243. * <td>Initial release</td>
  1244. * </tr>
  1245. * </table>
  1246. */
  1247. #endif /* #ifndef I2S_H_INCLUDED */