nrf_drv_pdm.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /**
  13. * @addtogroup nrf_pdm PDM HAL and driver
  14. * @ingroup nrf_drivers
  15. * @brief @tagAPI52 Pulse density modulation (PDM) interface APIs.
  16. *
  17. * The PDM HAL provides basic APIs for accessing the registers of the PDM interface peripheral.
  18. * The PDM driver provides APIs on a higher level.
  19. *
  20. * @defgroup nrf_drv_pdm PDM driver
  21. * @{
  22. * @ingroup nrf_pdm
  23. *
  24. * @brief @tagAPI52 Pulse density modulation (PDM) interface driver.
  25. */
  26. #ifndef NRF_DRV_PDM_H__
  27. #define NRF_DRV_PDM_H__
  28. #include "nrf_drv_config.h"
  29. #include "nrf_pdm.h"
  30. #include "sdk_errors.h"
  31. #define NRF_PDM_MAX_BUFFER_SIZE 32768
  32. /**
  33. * @brief PDM interface driver configuration structure.
  34. */
  35. typedef struct
  36. {
  37. nrf_pdm_mode_t mode; ///< Interface operation mode.
  38. nrf_pdm_edge_t edge; ///< Sampling mode.
  39. uint8_t pin_clk; ///< CLK pin.
  40. uint8_t pin_din; ///< DIN pin.
  41. nrf_pdm_freq_t clock_freq; ///< Clock frequency.
  42. nrf_pdm_gain_t gain_l; ///< Left channel gain.
  43. nrf_pdm_gain_t gain_r; ///< Right channel gain.
  44. uint8_t interrupt_priority; ///< Interrupt priority.
  45. uint16_t buffer_length; ///< Length of a single buffer (in 16-bit words).
  46. int16_t * buffer_a; ///< Sample buffer A (filled first).
  47. int16_t * buffer_b; ///< Sample buffer B (filled after buffer A).
  48. } nrf_drv_pdm_config_t;
  49. /**
  50. * @brief Macro for setting @ref nrf_drv_pdm_config_t to default settings
  51. * in single ended mode.
  52. *
  53. * @param PIN_CLK CLK output pin.
  54. * @param PIN_DIN DIN input pin.
  55. * @param BUFF_A Sample buffer A (filled first).
  56. * @param BUFF_B Sample buffer B (filled after buffer A).
  57. * @param BUFF_LEN Length of a single buffer (in 16-bit words).
  58. */
  59. #define NRF_DRV_PDM_DEFAULT_CONFIG(PIN_CLK, PIN_DIN, BUFF_A, BUFF_B, BUFF_LEN) \
  60. { \
  61. .mode = PDM_CONFIG_MODE, \
  62. .edge = PDM_CONFIG_EDGE, \
  63. .pin_clk = PIN_CLK, \
  64. .pin_din = PIN_DIN, \
  65. .clock_freq = PDM_CONFIG_CLOCK_FREQ, \
  66. .gain_l = NRF_PDM_GAIN_DEFAULT, \
  67. .gain_r = NRF_PDM_GAIN_DEFAULT, \
  68. .interrupt_priority = PDM_CONFIG_IRQ_PRIORITY, \
  69. .buffer_length = BUFF_LEN, \
  70. .buffer_a = BUFF_A, \
  71. .buffer_b = BUFF_B \
  72. }
  73. /**
  74. * @brief Handler for PDM interface ready events.
  75. *
  76. * This event handler is called when a buffer is full and ready to be processed.
  77. *
  78. * @param[in] p_buffer Sample buffer pointer.
  79. * @param[in] length Buffer length in 16-bit words.
  80. */
  81. typedef void (*nrf_drv_pdm_event_handler_t)(uint32_t * buffer, uint16_t length);
  82. /**
  83. * @brief Function for initializing the PDM interface.
  84. *
  85. * @param[in] p_config Pointer to a configuration structure. If NULL, the default one is used.
  86. * @param[in] event_handler Event handler provided by the user.
  87. *
  88. * @retval NRF_SUCCESS If initialization was successful.
  89. * @retval NRF_ERROR_INVALID_STATE If the driver is already initialized.
  90. * @retval NRF_ERROR_INVALID_PARAM If invalid parameters were specified.
  91. */
  92. ret_code_t nrf_drv_pdm_init(nrf_drv_pdm_config_t const * p_config,
  93. nrf_drv_pdm_event_handler_t event_handler);
  94. /**
  95. * @brief Function for uninitializing the PDM interface.
  96. *
  97. * This function stops PDM sampling, if it is in progress.
  98. */
  99. void nrf_drv_pdm_uninit(void);
  100. /**
  101. * @brief Function for getting the address of a PDM interface task.
  102. *
  103. * @param[in] task Task.
  104. *
  105. * @return Task address.
  106. */
  107. __STATIC_INLINE uint32_t nrf_drv_pdm_task_address_get(nrf_pdm_task_t task)
  108. {
  109. return nrf_pdm_task_address_get(task);
  110. }
  111. /**
  112. * @brief Function for getting the state of the PDM interface.
  113. *
  114. * @retval TRUE If the PDM interface is enabled.
  115. * @retval FALSE If the PDM interface is disabled.
  116. */
  117. __STATIC_INLINE bool nrf_drv_pdm_enable_check()
  118. {
  119. return nrf_pdm_enable_check();
  120. }
  121. /**
  122. * @brief Function for starting PDM sampling.
  123. *
  124. * @retval NRF_SUCCESS If sampling was started successfully or was already in progress.
  125. * @retval NRF_ERROR_BUSY If a previous start/stop operation is in progress.
  126. */
  127. ret_code_t nrf_drv_pdm_start(void);
  128. /**
  129. * @brief Function for stopping PDM sampling.
  130. *
  131. * When this function is called, the PDM interface is stopped after finishing
  132. * the current frame.
  133. * The event handler function might be called once more after calling this function.
  134. *
  135. * @retval NRF_SUCCESS If sampling was stopped successfully or was already stopped before.
  136. * @retval NRF_ERROR_BUSY If a previous start/stop operation is in progress.
  137. */
  138. ret_code_t nrf_drv_pdm_stop(void);
  139. #endif // NRF_DRV_PDM_H__
  140. /** @} */