nrf_pdm.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #ifndef NRF_PDM_H_
  13. #define NRF_PDM_H_
  14. /**
  15. * @defgroup nrf_pdm_hal PDM HAL
  16. * @{
  17. * @ingroup nrf_pdm
  18. *
  19. * @brief @tagAPI52 Hardware abstraction layer for accessing the pulse density modulation (PDM) peripheral.
  20. */
  21. #include <stdbool.h>
  22. #include <stddef.h>
  23. #include "nrf.h"
  24. #include "nrf_assert.h"
  25. #define NRF_PDM_GAIN_MINIMUM 0x00
  26. #define NRF_PDM_GAIN_DEFAULT 0x28
  27. #define NRF_PDM_GAIN_MAXIMUM 0x50
  28. typedef uint8_t nrf_pdm_gain_t;
  29. /**
  30. * @brief PDM tasks.
  31. */
  32. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  33. {
  34. NRF_PDM_TASK_START = offsetof(NRF_PDM_Type, TASKS_START), ///< Starts continuous PDM transfer.
  35. NRF_PDM_TASK_STOP = offsetof(NRF_PDM_Type, TASKS_STOP) ///< Stops PDM transfer.
  36. } nrf_pdm_task_t;
  37. /**
  38. * @brief PDM events.
  39. */
  40. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  41. {
  42. NRF_PDM_EVENT_STARTED = offsetof(NRF_PDM_Type, EVENTS_STARTED), ///< PDM transfer has started.
  43. NRF_PDM_EVENT_STOPPED = offsetof(NRF_PDM_Type, EVENTS_STOPPED), ///< PDM transfer has finished.
  44. NRF_PDM_EVENT_END = offsetof(NRF_PDM_Type, EVENTS_END) ///< The PDM has written the last sample specified by SAMPLE.MAXCNT (or the last sample after a STOP task has been received) to Data RAM.
  45. } nrf_pdm_event_t;
  46. /**
  47. * @brief PDM interrupt masks.
  48. */
  49. typedef enum
  50. {
  51. NRF_PDM_INT_STARTED = PDM_INTENSET_STARTED_Msk, ///< Interrupt on EVENTS_STARTED event.
  52. NRF_PDM_INT_STOPPED = PDM_INTENSET_STOPPED_Msk, ///< Interrupt on EVENTS_STOPPED event.
  53. NRF_PDM_INT_END = PDM_INTENSET_END_Msk ///< Interrupt on EVENTS_END event.
  54. } nrf_pdm_int_mask_t;
  55. /**
  56. * @brief PDM clock frequency.
  57. */
  58. typedef enum
  59. {
  60. NRF_PDM_FREQ_1000K = PDM_PDMCLKCTRL_FREQ_1000K, ///< PDM_CLK = 1.000 MHz.
  61. NRF_PDM_FREQ_1032K = PDM_PDMCLKCTRL_FREQ_Default, ///< PDM_CLK = 1.032 MHz.
  62. NRF_PDM_FREQ_1067K = PDM_PDMCLKCTRL_FREQ_1067K ///< PDM_CLK = 1.067 MHz.
  63. } nrf_pdm_freq_t;
  64. /**
  65. * @brief PDM operation mode.
  66. */
  67. typedef enum
  68. {
  69. NRF_PDM_MODE_STEREO = PDM_MODE_OPERATION_Stereo, ///< Sample and store one pair (Left + Right) of 16-bit samples per RAM word.
  70. NRF_PDM_MODE_MONO = PDM_MODE_OPERATION_Mono ///< Sample and store two successive Left samples (16 bit each) per RAM word.
  71. } nrf_pdm_mode_t;
  72. /**
  73. * @brief PDM sampling mode.
  74. */
  75. typedef enum
  76. {
  77. NRF_PDM_EDGE_LEFTFALLING = PDM_MODE_EDGE_LeftFalling, ///< Left (or mono) is sampled on falling edge of PDM_CLK.
  78. NRF_PDM_EDGE_LEFTRISING = PDM_MODE_EDGE_LeftRising ///< Left (or mono) is sampled on rising edge of PDM_CLK.
  79. } nrf_pdm_edge_t;
  80. /**
  81. * @brief Function for triggering a PDM task.
  82. *
  83. * @param[in] pdm_task PDM task.
  84. */
  85. __STATIC_INLINE void nrf_pdm_task_trigger(nrf_pdm_task_t pdm_task)
  86. {
  87. *((volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)pdm_task)) = 0x1UL;
  88. }
  89. /**
  90. * @brief Function for getting the address of a PDM task register.
  91. *
  92. * @param[in] pdm_task PDM task.
  93. *
  94. * @return Address of the specified PDM task.
  95. */
  96. __STATIC_INLINE uint32_t nrf_pdm_task_address_get(nrf_pdm_task_t pdm_task)
  97. {
  98. return (uint32_t)((uint8_t *)NRF_PDM + (uint32_t)pdm_task);
  99. }
  100. /**
  101. * @brief Function for getting the state of a PDM event.
  102. *
  103. * @param[in] pdm_event PDM event.
  104. *
  105. * @return State of the specified PDM event.
  106. */
  107. __STATIC_INLINE bool nrf_pdm_event_check(nrf_pdm_event_t pdm_event)
  108. {
  109. return (bool)*(volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)pdm_event);
  110. }
  111. /**
  112. * @brief Function for clearing a PDM event.
  113. *
  114. * @param[in] pdm_event PDM event.
  115. */
  116. __STATIC_INLINE void nrf_pdm_event_clear(nrf_pdm_event_t pdm_event)
  117. {
  118. *((volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)pdm_event)) = 0x0UL;
  119. }
  120. /**
  121. * @brief Function for getting the address of a PDM event register.
  122. *
  123. * @param[in] pdm_event PDM event.
  124. *
  125. * @return Address of the specified PDM event.
  126. */
  127. __STATIC_INLINE volatile uint32_t * nrf_pdm_event_address_get(nrf_pdm_event_t pdm_event)
  128. {
  129. return (volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)pdm_event);
  130. }
  131. /**
  132. * @brief Function for enabling PDM interrupts.
  133. *
  134. * @param[in] pdm_int_mask Interrupts to enable.
  135. */
  136. __STATIC_INLINE void nrf_pdm_int_enable(uint32_t pdm_int_mask)
  137. {
  138. NRF_PDM->INTENSET = pdm_int_mask;
  139. }
  140. /**
  141. * @brief Function for retrieving the state of PDM interrupts.
  142. *
  143. * @param[in] pdm_int_mask Interrupts to check.
  144. *
  145. * @retval true If all specified interrupts are enabled.
  146. * @retval false If at least one of the given interrupts is not enabled.
  147. */
  148. __STATIC_INLINE bool nrf_pdm_int_enable_check(uint32_t pdm_int_mask)
  149. {
  150. return (bool)(NRF_PDM->INTENSET & pdm_int_mask);
  151. }
  152. /**
  153. * @brief Function for disabling interrupts.
  154. *
  155. * @param pdm_int_mask Interrupts to disable.
  156. */
  157. __STATIC_INLINE void nrf_pdm_int_disable(uint32_t pdm_int_mask)
  158. {
  159. NRF_PDM->INTENCLR = pdm_int_mask;
  160. }
  161. /**
  162. * @brief Function for enabling the PDM peripheral.
  163. *
  164. * The PDM peripheral must be enabled before use.
  165. */
  166. __STATIC_INLINE void nrf_pdm_enable(void)
  167. {
  168. NRF_PDM->ENABLE = (PDM_ENABLE_ENABLE_Enabled << PDM_ENABLE_ENABLE_Pos);
  169. }
  170. /**
  171. * @brief Function for disabling the PDM peripheral.
  172. */
  173. __STATIC_INLINE void nrf_pdm_disable(void)
  174. {
  175. NRF_PDM->ENABLE = (PDM_ENABLE_ENABLE_Disabled << PDM_ENABLE_ENABLE_Pos);
  176. }
  177. /**
  178. * @brief Function for checking if the PDM peripheral is enabled.
  179. *
  180. * @retval true If the PDM peripheral is enabled.
  181. * @retval false If the PDM peripheral is not enabled.
  182. */
  183. __STATIC_INLINE bool nrf_pdm_enable_check(void)
  184. {
  185. return (NRF_PDM->ENABLE == (PDM_ENABLE_ENABLE_Enabled << PDM_ENABLE_ENABLE_Pos));
  186. }
  187. /**
  188. * @brief Function for setting the PDM operation mode.
  189. *
  190. * @param[in] pdm_mode PDM operation mode.
  191. * @param[in] pdm_edge PDM sampling mode.
  192. */
  193. __STATIC_INLINE void nrf_pdm_mode_set(nrf_pdm_mode_t pdm_mode, nrf_pdm_edge_t pdm_edge)
  194. {
  195. NRF_PDM->MODE = ((pdm_mode << PDM_MODE_OPERATION_Pos) & PDM_MODE_OPERATION_Msk)
  196. | ((pdm_edge << PDM_MODE_EDGE_Pos) & PDM_MODE_EDGE_Msk);
  197. }
  198. /**
  199. * @brief Function for getting the PDM operation mode.
  200. *
  201. * @param[out] p_pdm_mode PDM operation mode.
  202. * @param[out] p_pdm_edge PDM sampling mode.
  203. */
  204. __STATIC_INLINE void nrf_pdm_mode_get(nrf_pdm_mode_t * p_pdm_mode, nrf_pdm_edge_t * p_pdm_edge)
  205. {
  206. uint32_t mode = NRF_PDM->MODE;
  207. *p_pdm_mode = (nrf_pdm_mode_t)((mode & PDM_MODE_OPERATION_Msk ) >> PDM_MODE_OPERATION_Pos);
  208. *p_pdm_edge = (nrf_pdm_edge_t)((mode & PDM_MODE_EDGE_Msk ) >> PDM_MODE_EDGE_Pos);
  209. }
  210. /**
  211. * @brief Function for setting the PDM clock frequency.
  212. *
  213. * @param[in] pdm_freq PDM clock frequency.
  214. */
  215. __STATIC_INLINE void nrf_pdm_clock_set(nrf_pdm_freq_t pdm_freq)
  216. {
  217. NRF_PDM->PDMCLKCTRL = ((pdm_freq << PDM_PDMCLKCTRL_FREQ_Pos) & PDM_PDMCLKCTRL_FREQ_Msk);
  218. }
  219. /**
  220. * @brief Function for getting the PDM clock frequency.
  221. */
  222. __STATIC_INLINE nrf_pdm_freq_t nrf_pdm_clock_get(void)
  223. {
  224. return (nrf_pdm_freq_t) ((NRF_PDM->PDMCLKCTRL << PDM_PDMCLKCTRL_FREQ_Pos) & PDM_PDMCLKCTRL_FREQ_Msk);
  225. }
  226. /**
  227. * @brief Function for setting up the PDM pins.
  228. *
  229. * @param[in] psel_clk CLK pin number.
  230. * @param[in] psel_din DIN pin number.
  231. */
  232. __STATIC_INLINE void nrf_pdm_psel_connect(uint32_t psel_clk, uint32_t psel_din)
  233. {
  234. NRF_PDM->PSEL.CLK = ((psel_clk << PDM_PSEL_CLK_PIN_Pos) & PDM_PSEL_CLK_PIN_Msk)
  235. | ((PDM_PSEL_CLK_CONNECT_Connected << PDM_PSEL_CLK_CONNECT_Pos) & PDM_PSEL_CLK_CONNECT_Msk);
  236. NRF_PDM->PSEL.DIN = ((psel_din << PDM_PSEL_DIN_PIN_Pos) & PDM_PSEL_DIN_PIN_Msk)
  237. | ((PDM_PSEL_DIN_CONNECT_Connected << PDM_PSEL_CLK_CONNECT_Pos) & PDM_PSEL_DIN_CONNECT_Msk);
  238. }
  239. /**
  240. * @brief Function for disconnecting the PDM pins.
  241. */
  242. __STATIC_INLINE void nrf_pdm_psel_disconnect()
  243. {
  244. NRF_PDM->PSEL.CLK = ((PDM_PSEL_CLK_CONNECT_Disconnected << PDM_PSEL_CLK_CONNECT_Pos)
  245. & PDM_PSEL_CLK_CONNECT_Msk);
  246. NRF_PDM->PSEL.DIN = ((PDM_PSEL_DIN_CONNECT_Disconnected << PDM_PSEL_DIN_CONNECT_Pos)
  247. & PDM_PSEL_DIN_CONNECT_Msk);
  248. }
  249. /**
  250. * @brief Function for setting the PDM gain.
  251. *
  252. * @param[in] gain_l Left channel gain.
  253. * @param[in] gain_r Right channel gain.
  254. */
  255. __STATIC_INLINE void nrf_pdm_gain_set(nrf_pdm_gain_t gain_l, nrf_pdm_gain_t gain_r)
  256. {
  257. NRF_PDM->GAINL = gain_l;
  258. NRF_PDM->GAINR = gain_r;
  259. }
  260. /**
  261. * @brief Function for getting the PDM gain.
  262. *
  263. * @param[out] p_gain_l Left channel gain.
  264. * @param[out] p_gain_r Right channel gain.
  265. */
  266. __STATIC_INLINE void nrf_pdm_gain_get(nrf_pdm_gain_t * p_gain_l, nrf_pdm_gain_t * p_gain_r)
  267. {
  268. *p_gain_l = NRF_PDM->GAINL;
  269. *p_gain_r = NRF_PDM->GAINR;
  270. }
  271. /**
  272. * @brief Function for setting the PDM sample buffer.
  273. *
  274. * @param[in] p_buffer Pointer to the RAM address where samples should be written with EasyDMA.
  275. * @param[in] num Number of samples to allocate memory for in EasyDMA mode.
  276. *
  277. * The amount of allocated RAM depends on the operation mode.
  278. * - For stereo mode: N 32-bit words.
  279. * - For mono mode: Ceil(N/2) 32-bit words.
  280. */
  281. __STATIC_INLINE void nrf_pdm_buffer_set(uint32_t * p_buffer, uint32_t num)
  282. {
  283. NRF_PDM->SAMPLE.PTR = (uint32_t)p_buffer;
  284. NRF_PDM->SAMPLE.MAXCNT = num;
  285. }
  286. /**
  287. * @brief Function for getting the current PDM sample buffer address.
  288. *
  289. * @return Pointer to the current sample buffer.
  290. */
  291. __STATIC_INLINE uint32_t * nrf_pdm_buffer_get()
  292. {
  293. return (uint32_t *)NRF_PDM->SAMPLE.PTR;
  294. }
  295. /**
  296. *@}
  297. **/
  298. #endif /* NRF_PDM_H_ */