nrfx_i2s.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 NRFX_I2S_H__
  41. #define NRFX_I2S_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_i2s.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_i2s I2S driver
  49. * @{
  50. * @ingroup nrf_i2s
  51. * @brief Inter-IC Sound (I2S) peripheral driver.
  52. */
  53. /**
  54. * @brief This value can be provided instead of a pin number for the signals
  55. * SDOUT, SDIN, and MCK to specify that a given signal is not used
  56. * and therefore does not need to be connected to a pin.
  57. */
  58. #define NRFX_I2S_PIN_NOT_USED 0xFF
  59. /** @brief I2S driver configuration structure. */
  60. typedef struct
  61. {
  62. uint8_t sck_pin; ///< SCK pin number.
  63. uint8_t lrck_pin; ///< LRCK pin number.
  64. uint8_t mck_pin; ///< MCK pin number.
  65. /**< Optional. Use @ref NRFX_I2S_PIN_NOT_USED
  66. * if this signal is not needed. */
  67. uint8_t sdout_pin; ///< SDOUT pin number.
  68. /**< Optional. Use @ref NRFX_I2S_PIN_NOT_USED
  69. * if this signal is not needed. */
  70. uint8_t sdin_pin; ///< SDIN pin number.
  71. /**< Optional. Use @ref NRFX_I2S_PIN_NOT_USED
  72. * if this signal is not needed. */
  73. uint8_t irq_priority; ///< Interrupt priority.
  74. nrf_i2s_mode_t mode; ///< Mode of operation.
  75. nrf_i2s_format_t format; ///< Frame format.
  76. nrf_i2s_align_t alignment; ///< Alignment of sample within a frame.
  77. nrf_i2s_swidth_t sample_width; ///< Sample width.
  78. nrf_i2s_channels_t channels; ///< Enabled channels.
  79. nrf_i2s_mck_t mck_setup; ///< Master clock setup.
  80. nrf_i2s_ratio_t ratio; ///< MCK/LRCK ratio.
  81. } nrfx_i2s_config_t;
  82. /** @brief I2S driver buffers structure. */
  83. typedef struct
  84. {
  85. uint32_t * p_rx_buffer; ///< Pointer to the buffer for received data.
  86. uint32_t const * p_tx_buffer; ///< Pointer to the buffer with data to be sent.
  87. } nrfx_i2s_buffers_t;
  88. /** @brief I2S driver default configuration. */
  89. #define NRFX_I2S_DEFAULT_CONFIG \
  90. { \
  91. .sck_pin = NRFX_I2S_CONFIG_SCK_PIN, \
  92. .lrck_pin = NRFX_I2S_CONFIG_LRCK_PIN, \
  93. .mck_pin = NRFX_I2S_CONFIG_MCK_PIN, \
  94. .sdout_pin = NRFX_I2S_CONFIG_SDOUT_PIN, \
  95. .sdin_pin = NRFX_I2S_CONFIG_SDIN_PIN, \
  96. .irq_priority = NRFX_I2S_CONFIG_IRQ_PRIORITY, \
  97. .mode = (nrf_i2s_mode_t)NRFX_I2S_CONFIG_MASTER, \
  98. .format = (nrf_i2s_format_t)NRFX_I2S_CONFIG_FORMAT, \
  99. .alignment = (nrf_i2s_align_t)NRFX_I2S_CONFIG_ALIGN, \
  100. .sample_width = (nrf_i2s_swidth_t)NRFX_I2S_CONFIG_SWIDTH, \
  101. .channels = (nrf_i2s_channels_t)NRFX_I2S_CONFIG_CHANNELS, \
  102. .mck_setup = (nrf_i2s_mck_t)NRFX_I2S_CONFIG_MCK_SETUP, \
  103. .ratio = (nrf_i2s_ratio_t)NRFX_I2S_CONFIG_RATIO, \
  104. }
  105. #define NRFX_I2S_STATUS_NEXT_BUFFERS_NEEDED (1UL << 0)
  106. /**< The application must provide buffers that are to be used in the next
  107. * part of the transfer. A call to @ref nrfx_i2s_next_buffers_set must
  108. * be done before the currently used buffers are completely processed
  109. * (that is, the time remaining for supplying the next buffers depends on
  110. * the used size of the buffers). */
  111. /**
  112. * @brief I2S driver data handler type.
  113. *
  114. * A data handling function of this type must be specified during the initialization
  115. * of the driver. The driver will call this function when it finishes using
  116. * buffers passed to it by the application, and when it needs to be provided
  117. * with buffers for the next part of the transfer.
  118. *
  119. * @note The @c p_released pointer passed to this function is temporary and
  120. * will be invalid after the function returns, hence it cannot be stored
  121. * and used later. If needed, the pointed content (that is, buffers pointers)
  122. * must be copied instead.
  123. *
  124. * @param[in] p_released Pointer to a structure with pointers to buffers
  125. * passed previously to the driver that will no longer
  126. * be accessed by it (they can be now safely released or
  127. * used for another purpose, in particular for a next
  128. * part of the transfer).
  129. * This pointer will be NULL if the application did not
  130. * supply the buffers for the next part of the transfer
  131. * (via a call to @ref nrfx_i2s_next_buffers_set) since
  132. * the previous time the data handler signaled such need.
  133. * This means that data corruption occurred (the previous
  134. * buffers are used for the second time) and no buffers
  135. * can be released at the moment.
  136. * Both pointers in this structure are NULL when the
  137. * handler is called for the first time after a transfer
  138. * is started, because no data has been transferred yet
  139. * at this point. In all successive calls the pointers
  140. * specify what has been sent (TX) and what has been
  141. * received (RX) in the part of transfer that has just
  142. * been completed (provided that a given direction is
  143. * enabled, see @ref nrfx_i2s_start).
  144. * @param[in] status Bit field describing the current status of the transfer.
  145. * It can be 0 or a combination of the following flags:
  146. * - @ref NRFX_I2S_STATUS_NEXT_BUFFERS_NEEDED
  147. */
  148. typedef void (* nrfx_i2s_data_handler_t)(nrfx_i2s_buffers_t const * p_released,
  149. uint32_t status);
  150. /**
  151. * @brief Function for initializing the I2S driver.
  152. *
  153. * @param[in] p_config Pointer to the structure with the initial configuration.
  154. * @param[in] handler Data handler provided by the user. Must not be NULL.
  155. *
  156. * @retval NRFX_SUCCESS Initialization was successful.
  157. * @retval NRFX_ERROR_INVALID_STATE The driver was already initialized.
  158. * @retval NRFX_ERROR_INVALID_PARAM The requested combination of configuration
  159. * options is not allowed by the I2S peripheral.
  160. */
  161. nrfx_err_t nrfx_i2s_init(nrfx_i2s_config_t const * p_config,
  162. nrfx_i2s_data_handler_t handler);
  163. /** @brief Function for uninitializing the I2S driver. */
  164. void nrfx_i2s_uninit(void);
  165. /**
  166. * @brief Function for starting the continuous I2S transfer.
  167. *
  168. * The I2S data transfer can be performed in one of three modes: RX (reception)
  169. * only, TX (transmission) only, or in both directions simultaneously.
  170. * The mode is selected by specifying a proper buffer for a given direction
  171. * in the call to this function or by passing NULL instead if this direction
  172. * is to be disabled.
  173. *
  174. * The length of the buffer (which is a common value for RX and TX if both
  175. * directions are enabled) is specified in 32-bit words. One 32-bit memory
  176. * word can either contain four 8-bit samples, two 16-bit samples, or one
  177. * right-aligned 24-bit sample sign-extended to a 32-bit value.
  178. * For a detailed memory mapping for different supported configurations,
  179. * see the @linkProductSpecification52.
  180. *
  181. * @note Peripherals using EasyDMA (including I2S) require the transfer buffers
  182. * to be placed in the Data RAM region. If this condition is not met,
  183. * this function will fail with the error code NRFX_ERROR_INVALID_ADDR.
  184. *
  185. * @param[in] p_initial_buffers Pointer to a structure specifying the buffers
  186. * to be used in the initial part of the transfer
  187. * (buffers for all consecutive parts are provided
  188. * through the data handler).
  189. * @param[in] buffer_size Size of the buffers (in 32-bit words).
  190. * Must not be 0.
  191. * @param[in] flags Transfer options (0 for default settings).
  192. * Currently, no additional flags are available.
  193. *
  194. * @retval NRFX_SUCCESS The operation was successful.
  195. * @retval NRFX_ERROR_INVALID_STATE Transfer was already started or
  196. * the driver has not been initialized.
  197. * @retval NRFX_ERROR_INVALID_ADDR The provided buffers are not placed
  198. * in the Data RAM region.
  199. */
  200. nrfx_err_t nrfx_i2s_start(nrfx_i2s_buffers_t const * p_initial_buffers,
  201. uint16_t buffer_size,
  202. uint8_t flags);
  203. /**
  204. * @brief Function for supplying the buffers to be used in the next part of
  205. * the transfer.
  206. *
  207. * The application must call this function when the data handler receives
  208. * @ref NRFX_I2S_STATUS_NEXT_BUFFERS_NEEDED in the @c status parameter.
  209. * The call can be done immediately from the data handler function or later,
  210. * but it has to be done before the I2S peripheral finishes processing the
  211. * buffers supplied previously. Otherwise, data corruption will occur.
  212. *
  213. * @sa nrfx_i2s_data_handler_t
  214. *
  215. * @retval NRFX_SUCCESS If the operation was successful.
  216. * @retval NRFX_ERROR_INVALID_STATE If the buffers were already supplied or
  217. * the peripheral is currently being stopped.
  218. */
  219. nrfx_err_t nrfx_i2s_next_buffers_set(nrfx_i2s_buffers_t const * p_buffers);
  220. /** @brief Function for stopping the I2S transfer. */
  221. void nrfx_i2s_stop(void);
  222. /** @} */
  223. void nrfx_i2s_irq_handler(void);
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227. #endif // NRFX_I2S_H__