nrfx_pdm.h 7.1 KB

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