nrf_i2s.h 24 KB

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