nrf_i2s.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /**
  2. * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_I2S_H__
  41. #define NRF_I2S_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_i2s_hal I2S HAL
  48. * @{
  49. * @ingroup nrf_i2s
  50. * @brief Hardware access layer for managing the Inter-IC Sound (I2S) peripheral.
  51. */
  52. /**
  53. * @brief This value can be provided as a parameter for the @ref nrf_i2s_pins_set
  54. * function call to specify that a given I2S signal (SDOUT, SDIN, or MCK)
  55. * shall not be connected to a physical pin.
  56. */
  57. #define NRF_I2S_PIN_NOT_CONNECTED 0xFFFFFFFF
  58. /**
  59. * @brief I2S tasks.
  60. */
  61. typedef enum
  62. {
  63. /*lint -save -e30*/
  64. NRF_I2S_TASK_START = offsetof(NRF_I2S_Type, TASKS_START), ///< Starts continuous I2S transfer. Also starts the MCK generator if this is enabled.
  65. NRF_I2S_TASK_STOP = offsetof(NRF_I2S_Type, TASKS_STOP) ///< Stops I2S transfer. Also stops the MCK generator.
  66. /*lint -restore*/
  67. } nrf_i2s_task_t;
  68. /**
  69. * @brief I2S events.
  70. */
  71. typedef enum
  72. {
  73. /*lint -save -e30*/
  74. NRF_I2S_EVENT_RXPTRUPD = offsetof(NRF_I2S_Type, EVENTS_RXPTRUPD), ///< The RXD.PTR register has been copied to internal double-buffers.
  75. NRF_I2S_EVENT_TXPTRUPD = offsetof(NRF_I2S_Type, EVENTS_TXPTRUPD), ///< The TXD.PTR register has been copied to internal double-buffers.
  76. NRF_I2S_EVENT_STOPPED = offsetof(NRF_I2S_Type, EVENTS_STOPPED) ///< I2S transfer stopped.
  77. /*lint -restore*/
  78. } nrf_i2s_event_t;
  79. /**
  80. * @brief I2S interrupts.
  81. */
  82. typedef enum
  83. {
  84. NRF_I2S_INT_RXPTRUPD_MASK = I2S_INTENSET_RXPTRUPD_Msk, ///< Interrupt on RXPTRUPD event.
  85. NRF_I2S_INT_TXPTRUPD_MASK = I2S_INTENSET_TXPTRUPD_Msk, ///< Interrupt on TXPTRUPD event.
  86. NRF_I2S_INT_STOPPED_MASK = I2S_INTENSET_STOPPED_Msk ///< Interrupt on STOPPED event.
  87. } nrf_i2s_int_mask_t;
  88. /**
  89. * @brief I2S modes of operation.
  90. */
  91. typedef enum
  92. {
  93. NRF_I2S_MODE_MASTER = I2S_CONFIG_MODE_MODE_Master, ///< Master mode.
  94. NRF_I2S_MODE_SLAVE = I2S_CONFIG_MODE_MODE_Slave ///< Slave mode.
  95. } nrf_i2s_mode_t;
  96. /**
  97. * @brief I2S master clock generator settings.
  98. */
  99. typedef enum
  100. {
  101. NRF_I2S_MCK_DISABLED = 0, ///< MCK disabled.
  102. // [conversion to 'int' needed to prevent compilers from complaining
  103. // that the provided value (0x80000000UL) is out of range of "int"]
  104. NRF_I2S_MCK_32MDIV2 = (int)I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV2, ///< 32 MHz / 2 = 16.0 MHz.
  105. NRF_I2S_MCK_32MDIV3 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV3, ///< 32 MHz / 3 = 10.6666667 MHz.
  106. NRF_I2S_MCK_32MDIV4 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV4, ///< 32 MHz / 4 = 8.0 MHz.
  107. NRF_I2S_MCK_32MDIV5 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV5, ///< 32 MHz / 5 = 6.4 MHz.
  108. NRF_I2S_MCK_32MDIV6 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV6, ///< 32 MHz / 6 = 5.3333333 MHz.
  109. NRF_I2S_MCK_32MDIV8 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV8, ///< 32 MHz / 8 = 4.0 MHz.
  110. NRF_I2S_MCK_32MDIV10 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV10, ///< 32 MHz / 10 = 3.2 MHz.
  111. NRF_I2S_MCK_32MDIV11 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV11, ///< 32 MHz / 11 = 2.9090909 MHz.
  112. NRF_I2S_MCK_32MDIV15 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV15, ///< 32 MHz / 15 = 2.1333333 MHz.
  113. NRF_I2S_MCK_32MDIV16 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV16, ///< 32 MHz / 16 = 2.0 MHz.
  114. NRF_I2S_MCK_32MDIV21 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV21, ///< 32 MHz / 21 = 1.5238095 MHz.
  115. NRF_I2S_MCK_32MDIV23 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV23, ///< 32 MHz / 23 = 1.3913043 MHz.
  116. NRF_I2S_MCK_32MDIV31 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV31, ///< 32 MHz / 31 = 1.0322581 MHz.
  117. NRF_I2S_MCK_32MDIV42 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV42, ///< 32 MHz / 42 = 0.7619048 MHz.
  118. NRF_I2S_MCK_32MDIV63 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV63, ///< 32 MHz / 63 = 0.5079365 MHz.
  119. NRF_I2S_MCK_32MDIV125 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV125 ///< 32 MHz / 125 = 0.256 MHz.
  120. } nrf_i2s_mck_t;
  121. /**
  122. * @brief I2S MCK/LRCK ratios.
  123. */
  124. typedef enum
  125. {
  126. NRF_I2S_RATIO_32X = I2S_CONFIG_RATIO_RATIO_32X, ///< LRCK = MCK / 32.
  127. NRF_I2S_RATIO_48X = I2S_CONFIG_RATIO_RATIO_48X, ///< LRCK = MCK / 48.
  128. NRF_I2S_RATIO_64X = I2S_CONFIG_RATIO_RATIO_64X, ///< LRCK = MCK / 64.
  129. NRF_I2S_RATIO_96X = I2S_CONFIG_RATIO_RATIO_96X, ///< LRCK = MCK / 96.
  130. NRF_I2S_RATIO_128X = I2S_CONFIG_RATIO_RATIO_128X, ///< LRCK = MCK / 128.
  131. NRF_I2S_RATIO_192X = I2S_CONFIG_RATIO_RATIO_192X, ///< LRCK = MCK / 192.
  132. NRF_I2S_RATIO_256X = I2S_CONFIG_RATIO_RATIO_256X, ///< LRCK = MCK / 256.
  133. NRF_I2S_RATIO_384X = I2S_CONFIG_RATIO_RATIO_384X, ///< LRCK = MCK / 384.
  134. NRF_I2S_RATIO_512X = I2S_CONFIG_RATIO_RATIO_512X ///< LRCK = MCK / 512.
  135. } nrf_i2s_ratio_t;
  136. /**
  137. * @brief I2S sample widths.
  138. */
  139. typedef enum
  140. {
  141. NRF_I2S_SWIDTH_8BIT = I2S_CONFIG_SWIDTH_SWIDTH_8Bit, ///< 8 bit.
  142. NRF_I2S_SWIDTH_16BIT = I2S_CONFIG_SWIDTH_SWIDTH_16Bit, ///< 16 bit.
  143. NRF_I2S_SWIDTH_24BIT = I2S_CONFIG_SWIDTH_SWIDTH_24Bit ///< 24 bit.
  144. } nrf_i2s_swidth_t;
  145. /**
  146. * @brief I2S alignments of sample within a frame.
  147. */
  148. typedef enum
  149. {
  150. NRF_I2S_ALIGN_LEFT = I2S_CONFIG_ALIGN_ALIGN_Left, ///< Left-aligned.
  151. NRF_I2S_ALIGN_RIGHT = I2S_CONFIG_ALIGN_ALIGN_Right ///< Right-aligned.
  152. } nrf_i2s_align_t;
  153. /**
  154. * @brief I2S frame formats.
  155. */
  156. typedef enum
  157. {
  158. NRF_I2S_FORMAT_I2S = I2S_CONFIG_FORMAT_FORMAT_I2S, ///< Original I2S format.
  159. NRF_I2S_FORMAT_ALIGNED = I2S_CONFIG_FORMAT_FORMAT_Aligned ///< Alternate (left- or right-aligned) format.
  160. } nrf_i2s_format_t;
  161. /**
  162. * @brief I2S enabled channels.
  163. */
  164. typedef enum
  165. {
  166. NRF_I2S_CHANNELS_STEREO = I2S_CONFIG_CHANNELS_CHANNELS_Stereo, ///< Stereo.
  167. NRF_I2S_CHANNELS_LEFT = I2S_CONFIG_CHANNELS_CHANNELS_Left, ///< Left only.
  168. NRF_I2S_CHANNELS_RIGHT = I2S_CONFIG_CHANNELS_CHANNELS_Right ///< Right only.
  169. } nrf_i2s_channels_t;
  170. /**
  171. * @brief Function for activating a specific I2S task.
  172. *
  173. * @param[in] p_i2s I2S instance.
  174. * @param[in] task Task to activate.
  175. */
  176. __STATIC_INLINE void nrf_i2s_task_trigger(NRF_I2S_Type * p_i2s,
  177. nrf_i2s_task_t task);
  178. /**
  179. * @brief Function for getting the address of a specific I2S task register.
  180. *
  181. * @param[in] p_i2s I2S instance.
  182. * @param[in] task Requested task.
  183. *
  184. * @return Address of the specified task register.
  185. */
  186. __STATIC_INLINE uint32_t nrf_i2s_task_address_get(NRF_I2S_Type const * p_i2s,
  187. nrf_i2s_task_t task);
  188. /**
  189. * @brief Function for clearing a specific I2S event.
  190. *
  191. * @param[in] p_i2s I2S instance.
  192. * @param[in] event Event to clear.
  193. */
  194. __STATIC_INLINE void nrf_i2s_event_clear(NRF_I2S_Type * p_i2s,
  195. nrf_i2s_event_t event);
  196. /**
  197. * @brief Function for checking the state of a specific I2S event.
  198. *
  199. * @param[in] p_i2s I2S instance.
  200. * @param[in] event Event to check.
  201. *
  202. * @retval true If the event is set.
  203. * @retval false If the event is not set.
  204. */
  205. __STATIC_INLINE bool nrf_i2s_event_check(NRF_I2S_Type const * p_i2s,
  206. nrf_i2s_event_t event);
  207. /**
  208. * @brief Function for getting the address of a specific I2S event register.
  209. *
  210. * @param[in] p_i2s I2S instance.
  211. * @param[in] event Requested event.
  212. *
  213. * @return Address of the specified event register.
  214. */
  215. __STATIC_INLINE uint32_t nrf_i2s_event_address_get(NRF_I2S_Type const * p_i2s,
  216. nrf_i2s_event_t event);
  217. /**
  218. * @brief Function for enabling specified interrupts.
  219. *
  220. * @param[in] p_i2s I2S instance.
  221. * @param[in] mask Interrupts to enable.
  222. */
  223. __STATIC_INLINE void nrf_i2s_int_enable(NRF_I2S_Type * p_i2s, uint32_t mask);
  224. /**
  225. * @brief Function for disabling specified interrupts.
  226. *
  227. * @param[in] p_i2s I2S instance.
  228. * @param[in] mask Interrupts to disable.
  229. */
  230. __STATIC_INLINE void nrf_i2s_int_disable(NRF_I2S_Type * p_i2s, uint32_t mask);
  231. /**
  232. * @brief Function for retrieving the state of a given interrupt.
  233. *
  234. * @param[in] p_i2s I2S instance.
  235. * @param[in] i2s_int Interrupt to check.
  236. *
  237. * @retval true If the interrupt is enabled.
  238. * @retval false If the interrupt is not enabled.
  239. */
  240. __STATIC_INLINE bool nrf_i2s_int_enable_check(NRF_I2S_Type const * p_i2s,
  241. nrf_i2s_int_mask_t i2s_int);
  242. /**
  243. * @brief Function for enabling the I2S peripheral.
  244. *
  245. * @param[in] p_i2s I2S instance.
  246. */
  247. __STATIC_INLINE void nrf_i2s_enable(NRF_I2S_Type * p_i2s);
  248. /**
  249. * @brief Function for disabling the I2S peripheral.
  250. *
  251. * @param[in] p_i2s I2S instance.
  252. */
  253. __STATIC_INLINE void nrf_i2s_disable(NRF_I2S_Type * p_i2s);
  254. /**
  255. * @brief Function for configuring I2S pins.
  256. *
  257. * Usage of the SDOUT, SDIN, and MCK signals is optional.
  258. * If a given signal is not needed, pass the @ref NRF_I2S_PIN_NOT_CONNECTED
  259. * value instead of its pin number.
  260. *
  261. * @param[in] p_i2s I2S instance.
  262. * @param[in] sck_pin SCK pin number.
  263. * @param[in] lrck_pin LRCK pin number.
  264. * @param[in] mck_pin MCK pin number.
  265. * @param[in] sdout_pin SDOUT pin number.
  266. * @param[in] sdin_pin SDIN pin number.
  267. */
  268. __STATIC_INLINE void nrf_i2s_pins_set(NRF_I2S_Type * p_i2s,
  269. uint32_t sck_pin,
  270. uint32_t lrck_pin,
  271. uint32_t mck_pin,
  272. uint32_t sdout_pin,
  273. uint32_t sdin_pin);
  274. /**
  275. * @brief Function for setting the I2S peripheral configuration.
  276. *
  277. * @param[in] p_i2s I2S instance.
  278. * @param[in] mode Mode of operation (master or slave).
  279. * @param[in] format I2S frame format.
  280. * @param[in] alignment Alignment of sample within a frame.
  281. * @param[in] sample_width Sample width.
  282. * @param[in] channels Enabled channels.
  283. * @param[in] mck_setup Master clock generator setup.
  284. * @param[in] ratio MCK/LRCK ratio.
  285. *
  286. * @retval true If the configuration has been set successfully.
  287. * @retval false If the requested configuration is not allowed.
  288. */
  289. __STATIC_INLINE bool nrf_i2s_configure(NRF_I2S_Type * p_i2s,
  290. nrf_i2s_mode_t mode,
  291. nrf_i2s_format_t format,
  292. nrf_i2s_align_t alignment,
  293. nrf_i2s_swidth_t sample_width,
  294. nrf_i2s_channels_t channels,
  295. nrf_i2s_mck_t mck_setup,
  296. nrf_i2s_ratio_t ratio);
  297. /**
  298. * @brief Function for setting up the I2S transfer.
  299. *
  300. * This function sets up the RX and TX buffers and enables reception and/or
  301. * transmission accordingly. If the transfer in a given direction is not
  302. * required, pass NULL instead of the pointer to the corresponding buffer.
  303. *
  304. * @param[in] p_i2s I2S instance.
  305. * @param[in] size Size of the buffers (in 32-bit words).
  306. * @param[in] p_rx_buffer Pointer to the receive buffer.
  307. * Pass NULL to disable reception.
  308. * @param[in] p_tx_buffer Pointer to the transmit buffer.
  309. * Pass NULL to disable transmission.
  310. */
  311. __STATIC_INLINE void nrf_i2s_transfer_set(NRF_I2S_Type * p_i2s,
  312. uint16_t size,
  313. uint32_t * p_rx_buffer,
  314. uint32_t const * p_tx_buffer);
  315. /**
  316. * @brief Function for setting the pointer to the receive buffer.
  317. *
  318. * @note The size of the buffer can be set only by calling
  319. * @ref nrf_i2s_transfer_set.
  320. *
  321. * @param[in] p_i2s I2S instance.
  322. * @param[in] p_buffer Pointer to the receive buffer.
  323. */
  324. __STATIC_INLINE void nrf_i2s_rx_buffer_set(NRF_I2S_Type * p_i2s,
  325. uint32_t * p_buffer);
  326. /**
  327. * @brief Function for getting the pointer to the receive buffer.
  328. *
  329. * @param[in] p_i2s I2S instance.
  330. *
  331. * @return Pointer to the receive buffer.
  332. */
  333. __STATIC_INLINE uint32_t * nrf_i2s_rx_buffer_get(NRF_I2S_Type const * p_i2s);
  334. /**
  335. * @brief Function for setting the pointer to the transmit buffer.
  336. *
  337. * @note The size of the buffer can be set only by calling
  338. * @ref nrf_i2s_transfer_set.
  339. *
  340. * @param[in] p_i2s I2S instance.
  341. * @param[in] p_buffer Pointer to the transmit buffer.
  342. */
  343. __STATIC_INLINE void nrf_i2s_tx_buffer_set(NRF_I2S_Type * p_i2s,
  344. uint32_t const * p_buffer);
  345. /**
  346. * @brief Function for getting the pointer to the transmit buffer.
  347. *
  348. * @param[in] p_i2s I2S instance.
  349. *
  350. * @return Pointer to the transmit buffer.
  351. */
  352. __STATIC_INLINE uint32_t * nrf_i2s_tx_buffer_get(NRF_I2S_Type const * p_i2s);
  353. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  354. __STATIC_INLINE void nrf_i2s_task_trigger(NRF_I2S_Type * p_i2s,
  355. nrf_i2s_task_t task)
  356. {
  357. *((volatile uint32_t *)((uint8_t *)p_i2s + (uint32_t)task)) = 0x1UL;
  358. }
  359. __STATIC_INLINE uint32_t nrf_i2s_task_address_get(NRF_I2S_Type const * p_i2s,
  360. nrf_i2s_task_t task)
  361. {
  362. return ((uint32_t)p_i2s + (uint32_t)task);
  363. }
  364. __STATIC_INLINE void nrf_i2s_event_clear(NRF_I2S_Type * p_i2s,
  365. nrf_i2s_event_t event)
  366. {
  367. *((volatile uint32_t *)((uint8_t *)p_i2s + (uint32_t)event)) = 0x0UL;
  368. #if __CORTEX_M == 0x04
  369. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_i2s + (uint32_t)event));
  370. (void)dummy;
  371. #endif
  372. }
  373. __STATIC_INLINE bool nrf_i2s_event_check(NRF_I2S_Type const * p_i2s,
  374. nrf_i2s_event_t event)
  375. {
  376. return (bool)*(volatile uint32_t *)((uint8_t *)p_i2s + (uint32_t)event);
  377. }
  378. __STATIC_INLINE uint32_t nrf_i2s_event_address_get(NRF_I2S_Type const * p_i2s,
  379. nrf_i2s_event_t event)
  380. {
  381. return ((uint32_t)p_i2s + (uint32_t)event);
  382. }
  383. __STATIC_INLINE void nrf_i2s_int_enable(NRF_I2S_Type * p_i2s, uint32_t mask)
  384. {
  385. p_i2s->INTENSET = mask;
  386. }
  387. __STATIC_INLINE void nrf_i2s_int_disable(NRF_I2S_Type * p_i2s, uint32_t mask)
  388. {
  389. p_i2s->INTENCLR = mask;
  390. }
  391. __STATIC_INLINE bool nrf_i2s_int_enable_check(NRF_I2S_Type const * p_i2s,
  392. nrf_i2s_int_mask_t i2s_int)
  393. {
  394. return (bool)(p_i2s->INTENSET & i2s_int);
  395. }
  396. __STATIC_INLINE void nrf_i2s_enable(NRF_I2S_Type * p_i2s)
  397. {
  398. p_i2s->ENABLE = (I2S_ENABLE_ENABLE_Enabled << I2S_ENABLE_ENABLE_Pos);
  399. }
  400. __STATIC_INLINE void nrf_i2s_disable(NRF_I2S_Type * p_i2s)
  401. {
  402. p_i2s->ENABLE = (I2S_ENABLE_ENABLE_Disabled << I2S_ENABLE_ENABLE_Pos);
  403. }
  404. __STATIC_INLINE void nrf_i2s_pins_set(NRF_I2S_Type * p_i2s,
  405. uint32_t sck_pin,
  406. uint32_t lrck_pin,
  407. uint32_t mck_pin,
  408. uint32_t sdout_pin,
  409. uint32_t sdin_pin)
  410. {
  411. p_i2s->PSEL.SCK = sck_pin;
  412. p_i2s->PSEL.LRCK = lrck_pin;
  413. p_i2s->PSEL.MCK = mck_pin;
  414. p_i2s->PSEL.SDOUT = sdout_pin;
  415. p_i2s->PSEL.SDIN = sdin_pin;
  416. }
  417. __STATIC_INLINE bool nrf_i2s_configure(NRF_I2S_Type * p_i2s,
  418. nrf_i2s_mode_t mode,
  419. nrf_i2s_format_t format,
  420. nrf_i2s_align_t alignment,
  421. nrf_i2s_swidth_t sample_width,
  422. nrf_i2s_channels_t channels,
  423. nrf_i2s_mck_t mck_setup,
  424. nrf_i2s_ratio_t ratio)
  425. {
  426. if (mode == NRF_I2S_MODE_MASTER)
  427. {
  428. // The MCK/LRCK ratio shall be a multiple of 2 * sample width.
  429. if (((sample_width == NRF_I2S_SWIDTH_16BIT) &&
  430. (ratio == NRF_I2S_RATIO_48X))
  431. ||
  432. ((sample_width == NRF_I2S_SWIDTH_24BIT) &&
  433. ((ratio == NRF_I2S_RATIO_32X) ||
  434. (ratio == NRF_I2S_RATIO_64X) ||
  435. (ratio == NRF_I2S_RATIO_128X) ||
  436. (ratio == NRF_I2S_RATIO_256X) ||
  437. (ratio == NRF_I2S_RATIO_512X))))
  438. {
  439. return false;
  440. }
  441. }
  442. p_i2s->CONFIG.MODE = mode;
  443. p_i2s->CONFIG.FORMAT = format;
  444. p_i2s->CONFIG.ALIGN = alignment;
  445. p_i2s->CONFIG.SWIDTH = sample_width;
  446. p_i2s->CONFIG.CHANNELS = channels;
  447. p_i2s->CONFIG.RATIO = ratio;
  448. if (mck_setup == NRF_I2S_MCK_DISABLED)
  449. {
  450. p_i2s->CONFIG.MCKEN =
  451. (I2S_CONFIG_MCKEN_MCKEN_Disabled << I2S_CONFIG_MCKEN_MCKEN_Pos);
  452. }
  453. else
  454. {
  455. p_i2s->CONFIG.MCKFREQ = mck_setup;
  456. p_i2s->CONFIG.MCKEN =
  457. (I2S_CONFIG_MCKEN_MCKEN_Enabled << I2S_CONFIG_MCKEN_MCKEN_Pos);
  458. }
  459. return true;
  460. }
  461. __STATIC_INLINE void nrf_i2s_transfer_set(NRF_I2S_Type * p_i2s,
  462. uint16_t size,
  463. uint32_t * p_buffer_rx,
  464. uint32_t const * p_buffer_tx)
  465. {
  466. p_i2s->RXTXD.MAXCNT = size;
  467. nrf_i2s_rx_buffer_set(p_i2s, p_buffer_rx);
  468. p_i2s->CONFIG.RXEN = (p_buffer_rx != NULL) ? 1 : 0;
  469. nrf_i2s_tx_buffer_set(p_i2s, p_buffer_tx);
  470. p_i2s->CONFIG.TXEN = (p_buffer_tx != NULL) ? 1 : 0;
  471. }
  472. __STATIC_INLINE void nrf_i2s_rx_buffer_set(NRF_I2S_Type * p_i2s,
  473. uint32_t * p_buffer)
  474. {
  475. p_i2s->RXD.PTR = (uint32_t)p_buffer;
  476. }
  477. __STATIC_INLINE uint32_t * nrf_i2s_rx_buffer_get(NRF_I2S_Type const * p_i2s)
  478. {
  479. return (uint32_t *)(p_i2s->RXD.PTR);
  480. }
  481. __STATIC_INLINE void nrf_i2s_tx_buffer_set(NRF_I2S_Type * p_i2s,
  482. uint32_t const * p_buffer)
  483. {
  484. p_i2s->TXD.PTR = (uint32_t)p_buffer;
  485. }
  486. __STATIC_INLINE uint32_t * nrf_i2s_tx_buffer_get(NRF_I2S_Type const * p_i2s)
  487. {
  488. return (uint32_t *)(p_i2s->TXD.PTR);
  489. }
  490. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  491. /** @} */
  492. #ifdef __cplusplus
  493. }
  494. #endif
  495. #endif // NRF_I2S_H__