nrf_drv_i2s.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**@file
  13. * @addtogroup nrf_i2s I2S HAL and driver
  14. * @ingroup nrf_drivers
  15. * @brief @tagAPI52 Inter-IC Sound (I2S) interface APIs.
  16. *
  17. * @defgroup nrf_drv_i2s I2S driver
  18. * @{
  19. * @ingroup nrf_i2s
  20. * @brief @tagAPI52 Inter-IC Sound (I2S) interface driver.
  21. */
  22. #ifndef NRF_DRV_I2S_H__
  23. #define NRF_DRV_I2S_H__
  24. #include "nordic_common.h"
  25. #include "nrf_drv_config.h"
  26. #include "nrf_i2s.h"
  27. #include "sdk_errors.h"
  28. /**
  29. * @brief This value can be provided instead of a pin number for the signals
  30. * SDOUT, SDIN, and MCK to specify that a given signal is not used
  31. * and therefore does not need to be connected to a pin.
  32. */
  33. #define NRF_DRV_I2S_PIN_NOT_USED 0xFF
  34. /**
  35. * @brief Flag indicating that calls to the data handler for RX and TX should
  36. * be synchronized, thus always combined into one call.
  37. *
  38. * Use this flag when calling @ref nrf_drv_i2s_start to force a common call
  39. * to the @ref nrf_drv_i2s_data_handler_t "data handler" for RX and TX data.
  40. * This is useful, for example, when received data should be processed and
  41. * then be sent back. Obviously, this flag is only applicable when both
  42. * directions (RX and TX) are enabled.
  43. */
  44. #define NRF_DRV_I2S_FLAG_SYNCHRONIZED_MODE 0x01
  45. /**
  46. * @brief I2S driver configuration structure.
  47. */
  48. typedef struct
  49. {
  50. uint8_t sck_pin; ///< SCK pin number.
  51. uint8_t lrck_pin; ///< LRCK pin number.
  52. uint8_t mck_pin; ///< MCK pin number.
  53. /**< Optional. Use @ref NRF_DRV_I2S_PIN_NOT_USED
  54. * if this signal is not needed. */
  55. uint8_t sdout_pin; ///< SDOUT pin number.
  56. /**< Optional. Use @ref NRF_DRV_I2S_PIN_NOT_USED
  57. * if this signal is not needed. */
  58. uint8_t sdin_pin; ///< SDIN pin number.
  59. /**< Optional. Use @ref NRF_DRV_I2S_PIN_NOT_USED
  60. * if this signal is not needed. */
  61. uint8_t irq_priority; ///< Interrupt priority.
  62. nrf_i2s_mode_t mode; ///< Mode of operation.
  63. nrf_i2s_format_t format; ///< Frame format.
  64. nrf_i2s_align_t alignment; ///< Alignment of sample within a frame.
  65. nrf_i2s_swidth_t sample_width; ///< Sample width.
  66. nrf_i2s_channels_t channels; ///< Enabled channels.
  67. nrf_i2s_mck_t mck_setup; ///< Master clock setup.
  68. nrf_i2s_ratio_t ratio; ///< MCK/LRCK ratio.
  69. } nrf_drv_i2s_config_t;
  70. /**
  71. * @brief I2S driver default configuration.
  72. */
  73. #define NRF_DRV_I2S_DEFAULT_CONFIG \
  74. { \
  75. .sck_pin = I2S_CONFIG_SCK_PIN, \
  76. .lrck_pin = I2S_CONFIG_LRCK_PIN, \
  77. .mck_pin = I2S_CONFIG_MCK_PIN, \
  78. .sdout_pin = I2S_CONFIG_SDOUT_PIN, \
  79. .sdin_pin = I2S_CONFIG_SDIN_PIN, \
  80. .irq_priority = I2S_CONFIG_IRQ_PRIORITY, \
  81. .mode = I2S_CONFIG_MASTER, \
  82. .format = I2S_CONFIG_FORMAT, \
  83. .alignment = I2S_CONFIG_ALIGN, \
  84. .sample_width = I2S_CONFIG_SWIDTH, \
  85. .channels = I2S_CONFIG_CHANNELS, \
  86. .mck_setup = I2S_CONFIG_MCK_SETUP, \
  87. .ratio = I2S_CONFIG_RATIO, \
  88. }
  89. /**
  90. * @brief I2S driver data handler type.
  91. *
  92. * A data handling function of this type must be specified during initialization
  93. * of the driver. The driver will call this function when a new portion of data
  94. * is received or a new portion of data should be prepared for transmission.
  95. * The first case is indicated by a non-NULL value in the @p p_data_received
  96. * parameter (which points to the memory containing the received data).
  97. * Similarly, the second case is indicated by a non-NULL value in the
  98. * @p p_data_to_send parameter (which points to where the data to be transmitted
  99. * should be placed).
  100. *
  101. * @note The two cases mentioned above may be indicated separately or combined
  102. * into one call (depending on the environment in which the driver is
  103. * used). Therefore, both parameters should be checked and handled
  104. * properly in every call. @ref NRF_DRV_I2S_FLAG_SYNCHRONIZED_MODE
  105. * "Synchronized mode" can be used to always combine these indications.
  106. *
  107. * @param[in] p_data_received Pointer to the buffer with received data,
  108. * or NULL if the handler is called to prepare
  109. * transmission only.
  110. * @param[out] p_data_to_send Pointer to the buffer where data to be sent
  111. * should be written, or NULL if the handler is
  112. * called for received data only.
  113. * @param[in] number_of_words Length of data received and/or to be written
  114. * (in 32-bit words). This value is always equal to
  115. * half the size of the buffers set by the call
  116. * to the @ref nrf_drv_i2s_start function.
  117. */
  118. typedef void (* nrf_drv_i2s_data_handler_t)(uint32_t const * p_data_received,
  119. uint32_t * p_data_to_send,
  120. uint16_t number_of_words);
  121. /**
  122. * @brief Function for initializing the I2S driver.
  123. *
  124. * @param[in] p_config Pointer to the structure with initial configuration.
  125. * If NULL, the default configuration is used.
  126. * @param[in] handler Data handler provided by the user. Must not be NULL.
  127. *
  128. * @retval NRF_SUCCESS If initialization was successful.
  129. * @retval NRF_ERROR_INVALID_STATE If the driver was already initialized.
  130. * @retval NRF_ERROR_INVALID_PARAM If the requested combination of configuration
  131. * options is not allowed by the I2S peripheral.
  132. */
  133. ret_code_t nrf_drv_i2s_init(nrf_drv_i2s_config_t const * p_config,
  134. nrf_drv_i2s_data_handler_t handler);
  135. /**
  136. * @brief Function for uninitializing the I2S driver.
  137. */
  138. void nrf_drv_i2s_uninit(void);
  139. /**
  140. * @brief Function for starting the continuous I2S transfer.
  141. *
  142. * The I2S data transfer can be performed in one of three modes: RX (reception)
  143. * only, TX (transmission) only, or in both directions simultaneously.
  144. * The mode is selected by specifying a proper buffer for a given direction
  145. * in the call to this function or by passing NULL instead if this direction
  146. * should be disabled.
  147. *
  148. * The length of the buffer (which is a common value for RX and TX if both
  149. * directions are enabled) is specified in 32-bit words. One 32-bit memory
  150. * word can either contain four 8-bit samples, two 16-bit samples, or one
  151. * right-aligned 24-bit sample sign-extended to a 32-bit value.
  152. * For a detailed memory mapping for different supported configurations,
  153. * see the @linkProductSpecification52.
  154. *
  155. * The provided buffers are logically divided into two parts of equal size.
  156. * One of them is in use by the peripheral (for storing received data or for
  157. * getting data to be transmitted, respectively). The other part is provided
  158. * to the application via a call to the defined @ref nrf_drv_i2s_data_handler_t
  159. * "data handling function", so that the application can process the received
  160. * data or prepare the next portion of data to be sent. The two parts are
  161. * swapped every time @p buffer_size/2 data words are received or transmitted.
  162. *
  163. * Additional options are provided using the @p flags parameter:
  164. * - @ref NRF_DRV_I2S_FLAG_SYNCHRONIZED_MODE - the calls to data handler should
  165. * be done in a synchronized manner (one common call for TX and RX).
  166. * Applicable only when both RX and TX are enabled.
  167. *
  168. * @attention All data exchange is done in the data handler only. In particular,
  169. * no data should be written to the transmit buffer before calling
  170. * this function (a proper call to the data handler to get the first
  171. * portion of data to be sent will be done before the actual transfer
  172. * starts).
  173. *
  174. * @note Peripherals using EasyDMA (like I2S) require the transfer buffers
  175. * to be placed in the Data RAM region. If this condition is not met,
  176. * this function will fail with the error code NRF_ERROR_INVALID_ADDR.
  177. *
  178. * @param[in] p_rx_buffer Pointer to the receive buffer.
  179. * Pass NULL if reception is not required.
  180. * @param[in] p_tx_buffer Pointer to the transmit buffer.
  181. * Pass NULL if transmission is not required.
  182. * @param[in] buffer_size Size of the buffers (in 32-bit words).
  183. * The size must be an even number greater than 0.
  184. * @param[in] flags Transfer options (0 for default settings).
  185. *
  186. * @retval NRF_SUCCESS If the operation was successful.
  187. * @retval NRF_ERROR_INVALID_STATE If a transfer was already started or
  188. * the driver has not been initialized.
  189. * @retval NRF_ERROR_INVALID_ADDR If the provided buffers are not placed
  190. * in the Data RAM region.
  191. */
  192. ret_code_t nrf_drv_i2s_start(uint32_t * p_rx_buffer,
  193. uint32_t * p_tx_buffer,
  194. uint16_t buffer_size,
  195. uint8_t flags);
  196. /**
  197. * @brief Function for stopping the I2S transfer.
  198. */
  199. void nrf_drv_i2s_stop(void);
  200. #endif // NRF_DRV_I2S_H__
  201. /** @} */