nrf_i2s.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /**
  2. * Copyright (c) 2015 - 2019, 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. #if defined(I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV2) || defined(__NRFX_DOXYGEN__)
  103. // [conversion to 'int' needed to prevent compilers from complaining
  104. // that the provided value (0x80000000UL) is out of range of "int"]
  105. NRF_I2S_MCK_32MDIV2 = (int)I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV2, ///< 32 MHz / 2 = 16.0 MHz.
  106. #endif
  107. #if defined(I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV3) || defined(__NRFX_DOXYGEN__)
  108. NRF_I2S_MCK_32MDIV3 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV3, ///< 32 MHz / 3 = 10.6666667 MHz.
  109. #endif
  110. #if defined(I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV4) || defined(__NRFX_DOXYGEN__)
  111. NRF_I2S_MCK_32MDIV4 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV4, ///< 32 MHz / 4 = 8.0 MHz.
  112. #endif
  113. #if defined(I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV5) || defined(__NRFX_DOXYGEN__)
  114. NRF_I2S_MCK_32MDIV5 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV5, ///< 32 MHz / 5 = 6.4 MHz.
  115. #endif
  116. #if defined(I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV6) || defined(__NRFX_DOXYGEN__)
  117. NRF_I2S_MCK_32MDIV6 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV6, ///< 32 MHz / 6 = 5.3333333 MHz.
  118. #endif
  119. NRF_I2S_MCK_32MDIV8 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV8, ///< 32 MHz / 8 = 4.0 MHz.
  120. NRF_I2S_MCK_32MDIV10 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV10, ///< 32 MHz / 10 = 3.2 MHz.
  121. NRF_I2S_MCK_32MDIV11 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV11, ///< 32 MHz / 11 = 2.9090909 MHz.
  122. NRF_I2S_MCK_32MDIV15 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV15, ///< 32 MHz / 15 = 2.1333333 MHz.
  123. NRF_I2S_MCK_32MDIV16 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV16, ///< 32 MHz / 16 = 2.0 MHz.
  124. NRF_I2S_MCK_32MDIV21 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV21, ///< 32 MHz / 21 = 1.5238095 MHz.
  125. NRF_I2S_MCK_32MDIV23 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV23, ///< 32 MHz / 23 = 1.3913043 MHz.
  126. NRF_I2S_MCK_32MDIV31 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV31, ///< 32 MHz / 31 = 1.0322581 MHz.
  127. NRF_I2S_MCK_32MDIV42 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV42, ///< 32 MHz / 42 = 0.7619048 MHz.
  128. NRF_I2S_MCK_32MDIV63 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV63, ///< 32 MHz / 63 = 0.5079365 MHz.
  129. NRF_I2S_MCK_32MDIV125 = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV125 ///< 32 MHz / 125 = 0.256 MHz.
  130. } nrf_i2s_mck_t;
  131. /**
  132. * @brief I2S MCK/LRCK ratios.
  133. */
  134. typedef enum
  135. {
  136. NRF_I2S_RATIO_32X = I2S_CONFIG_RATIO_RATIO_32X, ///< LRCK = MCK / 32.
  137. NRF_I2S_RATIO_48X = I2S_CONFIG_RATIO_RATIO_48X, ///< LRCK = MCK / 48.
  138. NRF_I2S_RATIO_64X = I2S_CONFIG_RATIO_RATIO_64X, ///< LRCK = MCK / 64.
  139. NRF_I2S_RATIO_96X = I2S_CONFIG_RATIO_RATIO_96X, ///< LRCK = MCK / 96.
  140. NRF_I2S_RATIO_128X = I2S_CONFIG_RATIO_RATIO_128X, ///< LRCK = MCK / 128.
  141. NRF_I2S_RATIO_192X = I2S_CONFIG_RATIO_RATIO_192X, ///< LRCK = MCK / 192.
  142. NRF_I2S_RATIO_256X = I2S_CONFIG_RATIO_RATIO_256X, ///< LRCK = MCK / 256.
  143. NRF_I2S_RATIO_384X = I2S_CONFIG_RATIO_RATIO_384X, ///< LRCK = MCK / 384.
  144. NRF_I2S_RATIO_512X = I2S_CONFIG_RATIO_RATIO_512X ///< LRCK = MCK / 512.
  145. } nrf_i2s_ratio_t;
  146. /**
  147. * @brief I2S sample widths.
  148. */
  149. typedef enum
  150. {
  151. NRF_I2S_SWIDTH_8BIT = I2S_CONFIG_SWIDTH_SWIDTH_8Bit, ///< 8 bit.
  152. NRF_I2S_SWIDTH_16BIT = I2S_CONFIG_SWIDTH_SWIDTH_16Bit, ///< 16 bit.
  153. NRF_I2S_SWIDTH_24BIT = I2S_CONFIG_SWIDTH_SWIDTH_24Bit ///< 24 bit.
  154. } nrf_i2s_swidth_t;
  155. /**
  156. * @brief I2S alignments of sample within a frame.
  157. */
  158. typedef enum
  159. {
  160. NRF_I2S_ALIGN_LEFT = I2S_CONFIG_ALIGN_ALIGN_Left, ///< Left-aligned.
  161. NRF_I2S_ALIGN_RIGHT = I2S_CONFIG_ALIGN_ALIGN_Right ///< Right-aligned.
  162. } nrf_i2s_align_t;
  163. /**
  164. * @brief I2S frame formats.
  165. */
  166. typedef enum
  167. {
  168. NRF_I2S_FORMAT_I2S = I2S_CONFIG_FORMAT_FORMAT_I2S, ///< Original I2S format.
  169. NRF_I2S_FORMAT_ALIGNED = I2S_CONFIG_FORMAT_FORMAT_Aligned ///< Alternate (left- or right-aligned) format.
  170. } nrf_i2s_format_t;
  171. /**
  172. * @brief I2S enabled channels.
  173. */
  174. typedef enum
  175. {
  176. NRF_I2S_CHANNELS_STEREO = I2S_CONFIG_CHANNELS_CHANNELS_Stereo, ///< Stereo.
  177. NRF_I2S_CHANNELS_LEFT = I2S_CONFIG_CHANNELS_CHANNELS_Left, ///< Left only.
  178. NRF_I2S_CHANNELS_RIGHT = I2S_CONFIG_CHANNELS_CHANNELS_Right ///< Right only.
  179. } nrf_i2s_channels_t;
  180. /**
  181. * @brief Function for activating a specific I2S task.
  182. *
  183. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  184. * @param[in] task Task to activate.
  185. */
  186. __STATIC_INLINE void nrf_i2s_task_trigger(NRF_I2S_Type * p_reg,
  187. nrf_i2s_task_t task);
  188. /**
  189. * @brief Function for getting the address of a specific I2S task register.
  190. *
  191. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  192. * @param[in] task Requested task.
  193. *
  194. * @return Address of the specified task register.
  195. */
  196. __STATIC_INLINE uint32_t nrf_i2s_task_address_get(NRF_I2S_Type const * p_reg,
  197. nrf_i2s_task_t task);
  198. /**
  199. * @brief Function for clearing a specific I2S event.
  200. *
  201. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  202. * @param[in] event Event to clear.
  203. */
  204. __STATIC_INLINE void nrf_i2s_event_clear(NRF_I2S_Type * p_reg,
  205. nrf_i2s_event_t event);
  206. /**
  207. * @brief Function for checking the state of a specific I2S event.
  208. *
  209. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  210. * @param[in] event Event to check.
  211. *
  212. * @retval true If the event is set.
  213. * @retval false If the event is not set.
  214. */
  215. __STATIC_INLINE bool nrf_i2s_event_check(NRF_I2S_Type const * p_reg,
  216. nrf_i2s_event_t event);
  217. /**
  218. * @brief Function for getting the address of a specific I2S event register.
  219. *
  220. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  221. * @param[in] event Requested event.
  222. *
  223. * @return Address of the specified event register.
  224. */
  225. __STATIC_INLINE uint32_t nrf_i2s_event_address_get(NRF_I2S_Type const * p_reg,
  226. nrf_i2s_event_t event);
  227. /**
  228. * @brief Function for enabling specified interrupts.
  229. *
  230. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  231. * @param[in] mask Interrupts to enable.
  232. */
  233. __STATIC_INLINE void nrf_i2s_int_enable(NRF_I2S_Type * p_reg, uint32_t mask);
  234. /**
  235. * @brief Function for disabling specified interrupts.
  236. *
  237. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  238. * @param[in] mask Interrupts to disable.
  239. */
  240. __STATIC_INLINE void nrf_i2s_int_disable(NRF_I2S_Type * p_reg, uint32_t mask);
  241. /**
  242. * @brief Function for retrieving the state of a given interrupt.
  243. *
  244. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  245. * @param[in] i2s_int Interrupt to check.
  246. *
  247. * @retval true If the interrupt is enabled.
  248. * @retval false If the interrupt is not enabled.
  249. */
  250. __STATIC_INLINE bool nrf_i2s_int_enable_check(NRF_I2S_Type const * p_reg,
  251. nrf_i2s_int_mask_t i2s_int);
  252. /**
  253. * @brief Function for enabling the I2S peripheral.
  254. *
  255. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  256. */
  257. __STATIC_INLINE void nrf_i2s_enable(NRF_I2S_Type * p_reg);
  258. /**
  259. * @brief Function for disabling the I2S peripheral.
  260. *
  261. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  262. */
  263. __STATIC_INLINE void nrf_i2s_disable(NRF_I2S_Type * p_reg);
  264. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  265. /**
  266. * @brief Function for setting the subscribe configuration for a given
  267. * I2S task.
  268. *
  269. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  270. * @param[in] task Task for which to set the configuration.
  271. * @param[in] channel Channel through which to subscribe events.
  272. */
  273. __STATIC_INLINE void nrf_i2s_subscribe_set(NRF_I2S_Type * p_reg,
  274. nrf_i2s_task_t task,
  275. uint8_t channel);
  276. /**
  277. * @brief Function for clearing the subscribe configuration for a given
  278. * I2S task.
  279. *
  280. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  281. * @param[in] task Task for which to clear the configuration.
  282. */
  283. __STATIC_INLINE void nrf_i2s_subscribe_clear(NRF_I2S_Type * p_reg,
  284. nrf_i2s_task_t task);
  285. /**
  286. * @brief Function for setting the publish configuration for a given
  287. * I2S event.
  288. *
  289. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  290. * @param[in] event Event for which to set the configuration.
  291. * @param[in] channel Channel through which to publish the event.
  292. */
  293. __STATIC_INLINE void nrf_i2s_publish_set(NRF_I2S_Type * p_reg,
  294. nrf_i2s_event_t event,
  295. uint8_t channel);
  296. /**
  297. * @brief Function for clearing the publish configuration for a given
  298. * I2S event.
  299. *
  300. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  301. * @param[in] event Event for which to clear the configuration.
  302. */
  303. __STATIC_INLINE void nrf_i2s_publish_clear(NRF_I2S_Type * p_reg,
  304. nrf_i2s_event_t event);
  305. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  306. /**
  307. * @brief Function for configuring I2S pins.
  308. *
  309. * Usage of the SDOUT, SDIN, and MCK signals is optional.
  310. * If a given signal is not needed, pass the @ref NRF_I2S_PIN_NOT_CONNECTED
  311. * value instead of its pin number.
  312. *
  313. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  314. * @param[in] sck_pin SCK pin number.
  315. * @param[in] lrck_pin LRCK pin number.
  316. * @param[in] mck_pin MCK pin number.
  317. * @param[in] sdout_pin SDOUT pin number.
  318. * @param[in] sdin_pin SDIN pin number.
  319. */
  320. __STATIC_INLINE void nrf_i2s_pins_set(NRF_I2S_Type * p_reg,
  321. uint32_t sck_pin,
  322. uint32_t lrck_pin,
  323. uint32_t mck_pin,
  324. uint32_t sdout_pin,
  325. uint32_t sdin_pin);
  326. /**
  327. * @brief Function for setting the I2S peripheral configuration.
  328. *
  329. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  330. * @param[in] mode Mode of operation (master or slave).
  331. * @param[in] format I2S frame format.
  332. * @param[in] alignment Alignment of sample within a frame.
  333. * @param[in] sample_width Sample width.
  334. * @param[in] channels Enabled channels.
  335. * @param[in] mck_setup Master clock generator setup.
  336. * @param[in] ratio MCK/LRCK ratio.
  337. *
  338. * @retval true If the configuration has been set successfully.
  339. * @retval false If the requested configuration is not allowed.
  340. */
  341. __STATIC_INLINE bool nrf_i2s_configure(NRF_I2S_Type * p_reg,
  342. nrf_i2s_mode_t mode,
  343. nrf_i2s_format_t format,
  344. nrf_i2s_align_t alignment,
  345. nrf_i2s_swidth_t sample_width,
  346. nrf_i2s_channels_t channels,
  347. nrf_i2s_mck_t mck_setup,
  348. nrf_i2s_ratio_t ratio);
  349. /**
  350. * @brief Function for setting up the I2S transfer.
  351. *
  352. * This function sets up the RX and TX buffers and enables reception and/or
  353. * transmission accordingly. If the transfer in a given direction is not
  354. * required, pass NULL instead of the pointer to the corresponding buffer.
  355. *
  356. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  357. * @param[in] size Size of the buffers (in 32-bit words).
  358. * @param[in] p_rx_buffer Pointer to the receive buffer.
  359. * Pass NULL to disable reception.
  360. * @param[in] p_tx_buffer Pointer to the transmit buffer.
  361. * Pass NULL to disable transmission.
  362. */
  363. __STATIC_INLINE void nrf_i2s_transfer_set(NRF_I2S_Type * p_reg,
  364. uint16_t size,
  365. uint32_t * p_rx_buffer,
  366. uint32_t const * p_tx_buffer);
  367. /**
  368. * @brief Function for setting the pointer to the receive buffer.
  369. *
  370. * @note The size of the buffer can be set only by calling
  371. * @ref nrf_i2s_transfer_set.
  372. *
  373. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  374. * @param[in] p_buffer Pointer to the receive buffer.
  375. */
  376. __STATIC_INLINE void nrf_i2s_rx_buffer_set(NRF_I2S_Type * p_reg,
  377. uint32_t * p_buffer);
  378. /**
  379. * @brief Function for getting the pointer to the receive buffer.
  380. *
  381. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  382. *
  383. * @return Pointer to the receive buffer.
  384. */
  385. __STATIC_INLINE uint32_t * nrf_i2s_rx_buffer_get(NRF_I2S_Type const * p_reg);
  386. /**
  387. * @brief Function for setting the pointer to the transmit buffer.
  388. *
  389. * @note The size of the buffer can be set only by calling
  390. * @ref nrf_i2s_transfer_set.
  391. *
  392. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  393. * @param[in] p_buffer Pointer to the transmit buffer.
  394. */
  395. __STATIC_INLINE void nrf_i2s_tx_buffer_set(NRF_I2S_Type * p_reg,
  396. uint32_t const * p_buffer);
  397. /**
  398. * @brief Function for getting the pointer to the transmit buffer.
  399. *
  400. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  401. *
  402. * @return Pointer to the transmit buffer.
  403. */
  404. __STATIC_INLINE uint32_t * nrf_i2s_tx_buffer_get(NRF_I2S_Type const * p_reg);
  405. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  406. __STATIC_INLINE void nrf_i2s_task_trigger(NRF_I2S_Type * p_reg,
  407. nrf_i2s_task_t task)
  408. {
  409. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  410. }
  411. __STATIC_INLINE uint32_t nrf_i2s_task_address_get(NRF_I2S_Type const * p_reg,
  412. nrf_i2s_task_t task)
  413. {
  414. return ((uint32_t)p_reg + (uint32_t)task);
  415. }
  416. __STATIC_INLINE void nrf_i2s_event_clear(NRF_I2S_Type * p_reg,
  417. nrf_i2s_event_t event)
  418. {
  419. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  420. #if __CORTEX_M == 0x04
  421. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  422. (void)dummy;
  423. #endif
  424. }
  425. __STATIC_INLINE bool nrf_i2s_event_check(NRF_I2S_Type const * p_reg,
  426. nrf_i2s_event_t event)
  427. {
  428. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  429. }
  430. __STATIC_INLINE uint32_t nrf_i2s_event_address_get(NRF_I2S_Type const * p_reg,
  431. nrf_i2s_event_t event)
  432. {
  433. return ((uint32_t)p_reg + (uint32_t)event);
  434. }
  435. __STATIC_INLINE void nrf_i2s_int_enable(NRF_I2S_Type * p_reg, uint32_t mask)
  436. {
  437. p_reg->INTENSET = mask;
  438. }
  439. __STATIC_INLINE void nrf_i2s_int_disable(NRF_I2S_Type * p_reg, uint32_t mask)
  440. {
  441. p_reg->INTENCLR = mask;
  442. }
  443. __STATIC_INLINE bool nrf_i2s_int_enable_check(NRF_I2S_Type const * p_reg,
  444. nrf_i2s_int_mask_t i2s_int)
  445. {
  446. return (bool)(p_reg->INTENSET & i2s_int);
  447. }
  448. __STATIC_INLINE void nrf_i2s_enable(NRF_I2S_Type * p_reg)
  449. {
  450. p_reg->ENABLE = (I2S_ENABLE_ENABLE_Enabled << I2S_ENABLE_ENABLE_Pos);
  451. }
  452. __STATIC_INLINE void nrf_i2s_disable(NRF_I2S_Type * p_reg)
  453. {
  454. p_reg->ENABLE = (I2S_ENABLE_ENABLE_Disabled << I2S_ENABLE_ENABLE_Pos);
  455. }
  456. #if defined(DPPI_PRESENT)
  457. __STATIC_INLINE void nrf_i2s_subscribe_set(NRF_I2S_Type * p_reg,
  458. nrf_i2s_task_t task,
  459. uint8_t channel)
  460. {
  461. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
  462. ((uint32_t)channel | I2S_SUBSCRIBE_START_EN_Msk);
  463. }
  464. __STATIC_INLINE void nrf_i2s_subscribe_clear(NRF_I2S_Type * p_reg,
  465. nrf_i2s_task_t task)
  466. {
  467. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
  468. }
  469. __STATIC_INLINE void nrf_i2s_publish_set(NRF_I2S_Type * p_reg,
  470. nrf_i2s_event_t event,
  471. uint8_t channel)
  472. {
  473. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
  474. ((uint32_t)channel | I2S_PUBLISH_STOPPED_EN_Msk);
  475. }
  476. __STATIC_INLINE void nrf_i2s_publish_clear(NRF_I2S_Type * p_reg,
  477. nrf_i2s_event_t event)
  478. {
  479. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
  480. }
  481. #endif // defined(DPPI_PRESENT)
  482. __STATIC_INLINE void nrf_i2s_pins_set(NRF_I2S_Type * p_reg,
  483. uint32_t sck_pin,
  484. uint32_t lrck_pin,
  485. uint32_t mck_pin,
  486. uint32_t sdout_pin,
  487. uint32_t sdin_pin)
  488. {
  489. p_reg->PSEL.SCK = sck_pin;
  490. p_reg->PSEL.LRCK = lrck_pin;
  491. p_reg->PSEL.MCK = mck_pin;
  492. p_reg->PSEL.SDOUT = sdout_pin;
  493. p_reg->PSEL.SDIN = sdin_pin;
  494. }
  495. __STATIC_INLINE bool nrf_i2s_configure(NRF_I2S_Type * p_reg,
  496. nrf_i2s_mode_t mode,
  497. nrf_i2s_format_t format,
  498. nrf_i2s_align_t alignment,
  499. nrf_i2s_swidth_t sample_width,
  500. nrf_i2s_channels_t channels,
  501. nrf_i2s_mck_t mck_setup,
  502. nrf_i2s_ratio_t ratio)
  503. {
  504. if (mode == NRF_I2S_MODE_MASTER)
  505. {
  506. // The MCK/LRCK ratio shall be a multiple of 2 * sample width.
  507. if (((sample_width == NRF_I2S_SWIDTH_16BIT) &&
  508. (ratio == NRF_I2S_RATIO_48X))
  509. ||
  510. ((sample_width == NRF_I2S_SWIDTH_24BIT) &&
  511. ((ratio == NRF_I2S_RATIO_32X) ||
  512. (ratio == NRF_I2S_RATIO_64X) ||
  513. (ratio == NRF_I2S_RATIO_128X) ||
  514. (ratio == NRF_I2S_RATIO_256X) ||
  515. (ratio == NRF_I2S_RATIO_512X))))
  516. {
  517. return false;
  518. }
  519. }
  520. p_reg->CONFIG.MODE = mode;
  521. p_reg->CONFIG.FORMAT = format;
  522. p_reg->CONFIG.ALIGN = alignment;
  523. p_reg->CONFIG.SWIDTH = sample_width;
  524. p_reg->CONFIG.CHANNELS = channels;
  525. p_reg->CONFIG.RATIO = ratio;
  526. if (mck_setup == NRF_I2S_MCK_DISABLED)
  527. {
  528. p_reg->CONFIG.MCKEN =
  529. (I2S_CONFIG_MCKEN_MCKEN_Disabled << I2S_CONFIG_MCKEN_MCKEN_Pos);
  530. }
  531. else
  532. {
  533. p_reg->CONFIG.MCKFREQ = mck_setup;
  534. p_reg->CONFIG.MCKEN =
  535. (I2S_CONFIG_MCKEN_MCKEN_Enabled << I2S_CONFIG_MCKEN_MCKEN_Pos);
  536. }
  537. return true;
  538. }
  539. __STATIC_INLINE void nrf_i2s_transfer_set(NRF_I2S_Type * p_reg,
  540. uint16_t size,
  541. uint32_t * p_buffer_rx,
  542. uint32_t const * p_buffer_tx)
  543. {
  544. p_reg->RXTXD.MAXCNT = size;
  545. nrf_i2s_rx_buffer_set(p_reg, p_buffer_rx);
  546. p_reg->CONFIG.RXEN = (p_buffer_rx != NULL) ? 1 : 0;
  547. nrf_i2s_tx_buffer_set(p_reg, p_buffer_tx);
  548. p_reg->CONFIG.TXEN = (p_buffer_tx != NULL) ? 1 : 0;
  549. }
  550. __STATIC_INLINE void nrf_i2s_rx_buffer_set(NRF_I2S_Type * p_reg,
  551. uint32_t * p_buffer)
  552. {
  553. p_reg->RXD.PTR = (uint32_t)p_buffer;
  554. }
  555. __STATIC_INLINE uint32_t * nrf_i2s_rx_buffer_get(NRF_I2S_Type const * p_reg)
  556. {
  557. return (uint32_t *)(p_reg->RXD.PTR);
  558. }
  559. __STATIC_INLINE void nrf_i2s_tx_buffer_set(NRF_I2S_Type * p_reg,
  560. uint32_t const * p_buffer)
  561. {
  562. p_reg->TXD.PTR = (uint32_t)p_buffer;
  563. }
  564. __STATIC_INLINE uint32_t * nrf_i2s_tx_buffer_get(NRF_I2S_Type const * p_reg)
  565. {
  566. return (uint32_t *)(p_reg->TXD.PTR);
  567. }
  568. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  569. /** @} */
  570. #ifdef __cplusplus
  571. }
  572. #endif
  573. #endif // NRF_I2S_H__