nrfx_pdm.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_PDM_H__
  41. #define NRFX_PDM_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_pdm.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_pdm PDM driver
  49. * @{
  50. * @ingroup nrf_pdm
  51. * @brief Pulse Density Modulation (PDM) peripheral driver.
  52. */
  53. /** @brief Maximum supported PDM buffer size. */
  54. #define NRFX_PDM_MAX_BUFFER_SIZE 32767
  55. /** @brief PDM error type. */
  56. typedef enum
  57. {
  58. NRFX_PDM_NO_ERROR = 0, ///< No error.
  59. NRFX_PDM_ERROR_OVERFLOW = 1 ///< Overflow error.
  60. } nrfx_pdm_error_t;
  61. /** @brief PDM event structure. */
  62. typedef struct
  63. {
  64. bool buffer_requested; ///< Buffer request flag.
  65. int16_t * buffer_released; ///< Pointer to the released buffer. Can be NULL.
  66. nrfx_pdm_error_t error; ///< Error type.
  67. } nrfx_pdm_evt_t;
  68. /** @brief PDM interface driver configuration structure. */
  69. typedef struct
  70. {
  71. nrf_pdm_mode_t mode; ///< Interface operation mode.
  72. nrf_pdm_edge_t edge; ///< Sampling mode.
  73. uint8_t pin_clk; ///< CLK pin.
  74. uint8_t pin_din; ///< DIN pin.
  75. nrf_pdm_freq_t clock_freq; ///< Clock frequency.
  76. nrf_pdm_gain_t gain_l; ///< Left channel gain.
  77. nrf_pdm_gain_t gain_r; ///< Right channel gain.
  78. uint8_t interrupt_priority; ///< Interrupt priority.
  79. } nrfx_pdm_config_t;
  80. /**
  81. * @brief Macro for setting @ref nrfx_pdm_config_t to default settings
  82. * in the single-ended mode.
  83. *
  84. * @param _pin_clk CLK output pin.
  85. * @param _pin_din DIN input pin.
  86. */
  87. #define NRFX_PDM_DEFAULT_CONFIG(_pin_clk, _pin_din) \
  88. { \
  89. .mode = (nrf_pdm_mode_t)NRFX_PDM_CONFIG_MODE, \
  90. .edge = (nrf_pdm_edge_t)NRFX_PDM_CONFIG_EDGE, \
  91. .pin_clk = _pin_clk, \
  92. .pin_din = _pin_din, \
  93. .clock_freq = (nrf_pdm_freq_t)NRFX_PDM_CONFIG_CLOCK_FREQ, \
  94. .gain_l = NRF_PDM_GAIN_DEFAULT, \
  95. .gain_r = NRF_PDM_GAIN_DEFAULT, \
  96. .interrupt_priority = NRFX_PDM_CONFIG_IRQ_PRIORITY \
  97. }
  98. /**
  99. * @brief Handler for the PDM interface ready events.
  100. *
  101. * This event handler is called on a buffer request, an error or when a buffer
  102. * is full and ready to be processed.
  103. *
  104. * @param[in] p_evt Pointer to the PDM event structure.
  105. */
  106. typedef void (*nrfx_pdm_event_handler_t)(nrfx_pdm_evt_t const * const p_evt);
  107. /**
  108. * @brief Function for initializing the PDM interface.
  109. *
  110. * @param[in] p_config Pointer to the structure with the initial configuration.
  111. * @param[in] event_handler Event handler provided by the user. Cannot be NULL.
  112. *
  113. * @retval NRFX_SUCCESS Initialization was successful.
  114. * @retval NRFX_ERROR_INVALID_STATE The driver is already initialized.
  115. * @retval NRFX_ERROR_INVALID_PARAM Invalid configuration was specified.
  116. */
  117. nrfx_err_t nrfx_pdm_init(nrfx_pdm_config_t const * p_config,
  118. nrfx_pdm_event_handler_t event_handler);
  119. /**
  120. * @brief Function for uninitializing the PDM interface.
  121. *
  122. * This function stops PDM sampling, if it is in progress.
  123. */
  124. void nrfx_pdm_uninit(void);
  125. /**
  126. * @brief Function for getting the address of a PDM interface task.
  127. *
  128. * @param[in] task Task.
  129. *
  130. * @return Task address.
  131. */
  132. __STATIC_INLINE uint32_t nrfx_pdm_task_address_get(nrf_pdm_task_t task)
  133. {
  134. return nrf_pdm_task_address_get(task);
  135. }
  136. /**
  137. * @brief Function for getting the state of the PDM interface.
  138. *
  139. * @retval true The PDM interface is enabled.
  140. * @retval false The PDM interface is disabled.
  141. */
  142. __STATIC_INLINE bool nrfx_pdm_enable_check(void)
  143. {
  144. return nrf_pdm_enable_check();
  145. }
  146. /**
  147. * @brief Function for starting the PDM sampling.
  148. *
  149. * @retval NRFX_SUCCESS Sampling was started successfully or was already in progress.
  150. * @retval NRFX_ERROR_BUSY Previous start/stop operation is in progress.
  151. */
  152. nrfx_err_t nrfx_pdm_start(void);
  153. /**
  154. * @brief Function for stopping the PDM sampling.
  155. *
  156. * When this function is called, the PDM interface is stopped after finishing
  157. * the current frame.
  158. * The event handler function might be called once more after calling this function.
  159. *
  160. * @retval NRFX_SUCCESS Sampling was stopped successfully or was already stopped before.
  161. * @retval NRFX_ERROR_BUSY Previous start/stop operation is in progress.
  162. */
  163. nrfx_err_t nrfx_pdm_stop(void);
  164. /**
  165. * @brief Function for supplying the sample buffer.
  166. *
  167. * Call this function after every buffer request event.
  168. *
  169. * @param[in] buffer Pointer to the receive buffer. Cannot be NULL.
  170. * @param[in] buffer_length Length of the receive buffer in 16-bit words.
  171. *
  172. * @retval NRFX_SUCCESS The buffer was applied successfully.
  173. * @retval NRFX_ERROR_BUSY The buffer was already supplied or the peripheral is currently being stopped.
  174. * @retval NRFX_ERROR_INVALID_STATE The driver was not initialized.
  175. * @retval NRFX_ERROR_INVALID_PARAM Invalid parameters were provided.
  176. */
  177. nrfx_err_t nrfx_pdm_buffer_set(int16_t * buffer, uint16_t buffer_length);
  178. /** @} */
  179. void nrfx_pdm_irq_handler(void);
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif // NRFX_PDM_H__