nrf_pdm.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 NRF_PDM_H_
  41. #define NRF_PDM_H_
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_pdm_hal PDM HAL
  48. * @{
  49. * @ingroup nrf_pdm
  50. * @brief Hardware access layer for managing the Pulse Density Modulation (PDM) peripheral.
  51. */
  52. /** @brief Minimum value of PDM gain. */
  53. #define NRF_PDM_GAIN_MINIMUM 0x00
  54. /** @brief Default value of PDM gain. */
  55. #define NRF_PDM_GAIN_DEFAULT 0x28
  56. /** @brief Maximum value of PDM gain. */
  57. #define NRF_PDM_GAIN_MAXIMUM 0x50
  58. /** @brief PDM gain type. */
  59. typedef uint8_t nrf_pdm_gain_t;
  60. /** @brief PDM tasks. */
  61. typedef enum
  62. {
  63. NRF_PDM_TASK_START = offsetof(NRF_PDM_Type, TASKS_START), ///< Starts continuous PDM transfer.
  64. NRF_PDM_TASK_STOP = offsetof(NRF_PDM_Type, TASKS_STOP) ///< Stops PDM transfer.
  65. } nrf_pdm_task_t;
  66. /** @brief PDM events. */
  67. typedef enum
  68. {
  69. NRF_PDM_EVENT_STARTED = offsetof(NRF_PDM_Type, EVENTS_STARTED), ///< PDM transfer is started.
  70. NRF_PDM_EVENT_STOPPED = offsetof(NRF_PDM_Type, EVENTS_STOPPED), ///< PDM transfer is finished.
  71. 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.
  72. } nrf_pdm_event_t;
  73. /** @brief PDM interrupt masks. */
  74. typedef enum
  75. {
  76. NRF_PDM_INT_STARTED = PDM_INTENSET_STARTED_Msk, ///< Interrupt on EVENTS_STARTED event.
  77. NRF_PDM_INT_STOPPED = PDM_INTENSET_STOPPED_Msk, ///< Interrupt on EVENTS_STOPPED event.
  78. NRF_PDM_INT_END = PDM_INTENSET_END_Msk ///< Interrupt on EVENTS_END event.
  79. } nrf_pdm_int_mask_t;
  80. /** @brief PDM clock frequency. */
  81. typedef enum
  82. {
  83. NRF_PDM_FREQ_1000K = PDM_PDMCLKCTRL_FREQ_1000K, ///< PDM_CLK = 1.000 MHz.
  84. NRF_PDM_FREQ_1032K = PDM_PDMCLKCTRL_FREQ_Default, ///< PDM_CLK = 1.032 MHz.
  85. NRF_PDM_FREQ_1067K = PDM_PDMCLKCTRL_FREQ_1067K ///< PDM_CLK = 1.067 MHz.
  86. } nrf_pdm_freq_t;
  87. /** @brief PDM operation mode. */
  88. typedef enum
  89. {
  90. NRF_PDM_MODE_STEREO = PDM_MODE_OPERATION_Stereo, ///< Sample and store one pair (Left + Right) of 16-bit samples per RAM word.
  91. NRF_PDM_MODE_MONO = PDM_MODE_OPERATION_Mono ///< Sample and store two successive Left samples (16 bit each) per RAM word.
  92. } nrf_pdm_mode_t;
  93. /** @brief PDM sampling mode. */
  94. typedef enum
  95. {
  96. NRF_PDM_EDGE_LEFTFALLING = PDM_MODE_EDGE_LeftFalling, ///< Left (or mono) is sampled on falling edge of PDM_CLK.
  97. NRF_PDM_EDGE_LEFTRISING = PDM_MODE_EDGE_LeftRising ///< Left (or mono) is sampled on rising edge of PDM_CLK.
  98. } nrf_pdm_edge_t;
  99. /**
  100. * @brief Function for triggering a PDM task.
  101. *
  102. * @param[in] task PDM task.
  103. */
  104. __STATIC_INLINE void nrf_pdm_task_trigger(nrf_pdm_task_t task);
  105. /**
  106. * @brief Function for getting the address of a PDM task register.
  107. *
  108. * @param[in] task PDM task.
  109. *
  110. * @return Address of the specified PDM task.
  111. */
  112. __STATIC_INLINE uint32_t nrf_pdm_task_address_get(nrf_pdm_task_t task);
  113. /**
  114. * @brief Function for retrieving the state of the PDM event.
  115. *
  116. * @param[in] event Event to be checked.
  117. *
  118. * @retval true The event has been generated.
  119. * @retval false The event has not been generated.
  120. */
  121. __STATIC_INLINE bool nrf_pdm_event_check(nrf_pdm_event_t event);
  122. /**
  123. * @brief Function for clearing a PDM event.
  124. *
  125. * @param[in] event PDM event.
  126. */
  127. __STATIC_INLINE void nrf_pdm_event_clear(nrf_pdm_event_t event);
  128. /**
  129. * @brief Function for getting the address of a PDM event register.
  130. *
  131. * @param[in] event PDM event.
  132. *
  133. * @return Address of the specified PDM event.
  134. */
  135. __STATIC_INLINE volatile uint32_t * nrf_pdm_event_address_get(nrf_pdm_event_t event);
  136. /**
  137. * @brief Function for enabling PDM interrupts.
  138. *
  139. * @param[in] int_mask Mask of interrupts to be enabled.
  140. */
  141. __STATIC_INLINE void nrf_pdm_int_enable(uint32_t int_mask);
  142. /**
  143. * @brief Function for retrieving the state of PDM interrupts.
  144. *
  145. * @param[in] int_mask Mask of interrupts to be checked.
  146. *
  147. * @retval true All specified interrupts are enabled.
  148. * @retval false At least one of the given interrupts is not enabled.
  149. */
  150. __STATIC_INLINE bool nrf_pdm_int_enable_check(uint32_t int_mask);
  151. /**
  152. * @brief Function for disabling interrupts.
  153. *
  154. * @param[in] int_mask Mask of interrupts to be disabled.
  155. */
  156. __STATIC_INLINE void nrf_pdm_int_disable(uint32_t int_mask);
  157. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  158. /**
  159. * @brief Function for setting the subscribe configuration for a given
  160. * PDM task.
  161. *
  162. * @param[in] task Task for which to set the configuration.
  163. * @param[in] channel Channel through which to subscribe events.
  164. */
  165. __STATIC_INLINE void nrf_pdm_subscribe_set(nrf_pdm_task_t task,
  166. uint8_t channel);
  167. /**
  168. * @brief Function for clearing the subscribe configuration for a given
  169. * PDM task.
  170. *
  171. * @param[in] task Task for which to clear the configuration.
  172. */
  173. __STATIC_INLINE void nrf_pdm_subscribe_clear(nrf_pdm_task_t task);
  174. /**
  175. * @brief Function for setting the publish configuration for a given
  176. * PDM event.
  177. *
  178. * @param[in] event Event for which to set the configuration.
  179. * @param[in] channel Channel through which to publish the event.
  180. */
  181. __STATIC_INLINE void nrf_pdm_publish_set(nrf_pdm_event_t event,
  182. uint8_t channel);
  183. /**
  184. * @brief Function for clearing the publish configuration for a given
  185. * PDM event.
  186. *
  187. * @param[in] event Event for which to clear the configuration.
  188. */
  189. __STATIC_INLINE void nrf_pdm_publish_clear(nrf_pdm_event_t event);
  190. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  191. /**
  192. * @brief Function for enabling the PDM peripheral.
  193. *
  194. * The PDM peripheral must be enabled before use.
  195. */
  196. __STATIC_INLINE void nrf_pdm_enable(void);
  197. /** @brief Function for disabling the PDM peripheral. */
  198. __STATIC_INLINE void nrf_pdm_disable(void);
  199. /**
  200. * @brief Function for checking if the PDM peripheral is enabled.
  201. *
  202. * @retval true The PDM peripheral is enabled.
  203. * @retval false The PDM peripheral is not enabled.
  204. */
  205. __STATIC_INLINE bool nrf_pdm_enable_check(void);
  206. /**
  207. * @brief Function for setting the PDM operation mode.
  208. *
  209. * @param[in] pdm_mode PDM operation mode.
  210. * @param[in] pdm_edge PDM sampling mode.
  211. */
  212. __STATIC_INLINE void nrf_pdm_mode_set(nrf_pdm_mode_t pdm_mode, nrf_pdm_edge_t pdm_edge);
  213. /**
  214. * @brief Function for getting the PDM operation mode.
  215. *
  216. * @param[out] p_pdm_mode PDM operation mode.
  217. * @param[out] p_pdm_edge PDM sampling mode.
  218. */
  219. __STATIC_INLINE void nrf_pdm_mode_get(nrf_pdm_mode_t * p_pdm_mode, nrf_pdm_edge_t * p_pdm_edge);
  220. /**
  221. * @brief Function for setting the PDM clock frequency.
  222. *
  223. * @param[in] pdm_freq PDM clock frequency.
  224. */
  225. __STATIC_INLINE void nrf_pdm_clock_set(nrf_pdm_freq_t pdm_freq);
  226. /**
  227. * @brief Function for getting the PDM clock frequency.
  228. *
  229. * @return PDM clock frequency.
  230. */
  231. __STATIC_INLINE nrf_pdm_freq_t nrf_pdm_clock_get(void);
  232. /**
  233. * @brief Function for setting up the PDM pins.
  234. *
  235. * @param[in] psel_clk CLK pin number.
  236. * @param[in] psel_din DIN pin number.
  237. */
  238. __STATIC_INLINE void nrf_pdm_psel_connect(uint32_t psel_clk, uint32_t psel_din);
  239. /** @brief Function for disconnecting the PDM pins. */
  240. __STATIC_INLINE void nrf_pdm_psel_disconnect(void);
  241. /**
  242. * @brief Function for setting the PDM gain.
  243. *
  244. * @param[in] gain_l Left channel gain.
  245. * @param[in] gain_r Right channel gain.
  246. */
  247. __STATIC_INLINE void nrf_pdm_gain_set(nrf_pdm_gain_t gain_l, nrf_pdm_gain_t gain_r);
  248. /**
  249. * @brief Function for getting the PDM gain.
  250. *
  251. * @param[out] p_gain_l Left channel gain.
  252. * @param[out] p_gain_r Right channel gain.
  253. */
  254. __STATIC_INLINE void nrf_pdm_gain_get(nrf_pdm_gain_t * p_gain_l, nrf_pdm_gain_t * p_gain_r);
  255. /**
  256. * @brief Function for setting the PDM sample buffer.
  257. *
  258. * The amount of allocated RAM depends on the operation mode.
  259. * - For stereo mode: N 32-bit words.
  260. * - For mono mode: Ceil(N/2) 32-bit words.
  261. *
  262. * @param[in] p_buffer Pointer to the RAM address where samples are to be written with EasyDMA.
  263. * @param[in] num Number of samples to allocate memory for in EasyDMA mode.
  264. */
  265. __STATIC_INLINE void nrf_pdm_buffer_set(uint32_t * p_buffer, uint32_t num);
  266. /**
  267. * @brief Function for getting the current PDM sample buffer address.
  268. *
  269. * @return Pointer to the current sample buffer.
  270. */
  271. __STATIC_INLINE uint32_t * nrf_pdm_buffer_get(void);
  272. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  273. __STATIC_INLINE void nrf_pdm_task_trigger(nrf_pdm_task_t task)
  274. {
  275. *((volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)task)) = 0x1UL;
  276. }
  277. __STATIC_INLINE uint32_t nrf_pdm_task_address_get(nrf_pdm_task_t task)
  278. {
  279. return (uint32_t)((uint8_t *)NRF_PDM + (uint32_t)task);
  280. }
  281. __STATIC_INLINE bool nrf_pdm_event_check(nrf_pdm_event_t event)
  282. {
  283. return (bool)*(volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)event);
  284. }
  285. __STATIC_INLINE void nrf_pdm_event_clear(nrf_pdm_event_t event)
  286. {
  287. *((volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)event)) = 0x0UL;
  288. #if __CORTEX_M == 0x04
  289. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)event));
  290. (void)dummy;
  291. #endif
  292. }
  293. __STATIC_INLINE volatile uint32_t * nrf_pdm_event_address_get(nrf_pdm_event_t event)
  294. {
  295. return (volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)event);
  296. }
  297. __STATIC_INLINE void nrf_pdm_int_enable(uint32_t int_mask)
  298. {
  299. NRF_PDM->INTENSET = int_mask;
  300. }
  301. __STATIC_INLINE bool nrf_pdm_int_enable_check(uint32_t int_mask)
  302. {
  303. return (bool)(NRF_PDM->INTENSET & int_mask);
  304. }
  305. __STATIC_INLINE void nrf_pdm_int_disable(uint32_t int_mask)
  306. {
  307. NRF_PDM->INTENCLR = int_mask;
  308. }
  309. #if defined(DPPI_PRESENT)
  310. __STATIC_INLINE void nrf_pdm_subscribe_set(nrf_pdm_task_t task,
  311. uint8_t channel)
  312. {
  313. *((volatile uint32_t *) ((uint8_t *) NRF_PDM + (uint32_t) task + 0x80uL)) =
  314. ((uint32_t)channel | PDM_SUBSCRIBE_START_EN_Msk);
  315. }
  316. __STATIC_INLINE void nrf_pdm_subscribe_clear(nrf_pdm_task_t task)
  317. {
  318. *((volatile uint32_t *) ((uint8_t *) NRF_PDM + (uint32_t) task + 0x80uL)) = 0;
  319. }
  320. __STATIC_INLINE void nrf_pdm_publish_set(nrf_pdm_event_t event,
  321. uint8_t channel)
  322. {
  323. *((volatile uint32_t *) ((uint8_t *) NRF_PDM + (uint32_t) event + 0x80uL)) =
  324. ((uint32_t)channel | PDM_PUBLISH_STARTED_EN_Msk);
  325. }
  326. __STATIC_INLINE void nrf_pdm_publish_clear(nrf_pdm_event_t event)
  327. {
  328. *((volatile uint32_t *) ((uint8_t *) NRF_PDM + (uint32_t) event + 0x80uL)) = 0;
  329. }
  330. #endif // defined(DPPI_PRESENT)
  331. __STATIC_INLINE void nrf_pdm_enable(void)
  332. {
  333. NRF_PDM->ENABLE = (PDM_ENABLE_ENABLE_Enabled << PDM_ENABLE_ENABLE_Pos);
  334. }
  335. __STATIC_INLINE void nrf_pdm_disable(void)
  336. {
  337. NRF_PDM->ENABLE = (PDM_ENABLE_ENABLE_Disabled << PDM_ENABLE_ENABLE_Pos);
  338. }
  339. __STATIC_INLINE bool nrf_pdm_enable_check(void)
  340. {
  341. return (NRF_PDM->ENABLE == (PDM_ENABLE_ENABLE_Enabled << PDM_ENABLE_ENABLE_Pos));
  342. }
  343. __STATIC_INLINE void nrf_pdm_mode_set(nrf_pdm_mode_t pdm_mode, nrf_pdm_edge_t pdm_edge)
  344. {
  345. NRF_PDM->MODE = ((pdm_mode << PDM_MODE_OPERATION_Pos) & PDM_MODE_OPERATION_Msk)
  346. | ((pdm_edge << PDM_MODE_EDGE_Pos) & PDM_MODE_EDGE_Msk);
  347. }
  348. __STATIC_INLINE void nrf_pdm_mode_get(nrf_pdm_mode_t * p_pdm_mode, nrf_pdm_edge_t * p_pdm_edge)
  349. {
  350. uint32_t mode = NRF_PDM->MODE;
  351. *p_pdm_mode = (nrf_pdm_mode_t)((mode & PDM_MODE_OPERATION_Msk ) >> PDM_MODE_OPERATION_Pos);
  352. *p_pdm_edge = (nrf_pdm_edge_t)((mode & PDM_MODE_EDGE_Msk ) >> PDM_MODE_EDGE_Pos);
  353. }
  354. __STATIC_INLINE void nrf_pdm_clock_set(nrf_pdm_freq_t pdm_freq)
  355. {
  356. NRF_PDM->PDMCLKCTRL = ((pdm_freq << PDM_PDMCLKCTRL_FREQ_Pos) & PDM_PDMCLKCTRL_FREQ_Msk);
  357. }
  358. __STATIC_INLINE nrf_pdm_freq_t nrf_pdm_clock_get(void)
  359. {
  360. return (nrf_pdm_freq_t) ((NRF_PDM->PDMCLKCTRL << PDM_PDMCLKCTRL_FREQ_Pos) & PDM_PDMCLKCTRL_FREQ_Msk);
  361. }
  362. __STATIC_INLINE void nrf_pdm_psel_connect(uint32_t psel_clk, uint32_t psel_din)
  363. {
  364. NRF_PDM->PSEL.CLK = psel_clk;
  365. NRF_PDM->PSEL.DIN = psel_din;
  366. }
  367. __STATIC_INLINE void nrf_pdm_psel_disconnect(void)
  368. {
  369. NRF_PDM->PSEL.CLK = ((PDM_PSEL_CLK_CONNECT_Disconnected << PDM_PSEL_CLK_CONNECT_Pos)
  370. & PDM_PSEL_CLK_CONNECT_Msk);
  371. NRF_PDM->PSEL.DIN = ((PDM_PSEL_DIN_CONNECT_Disconnected << PDM_PSEL_DIN_CONNECT_Pos)
  372. & PDM_PSEL_DIN_CONNECT_Msk);
  373. }
  374. __STATIC_INLINE void nrf_pdm_gain_set(nrf_pdm_gain_t gain_l, nrf_pdm_gain_t gain_r)
  375. {
  376. NRF_PDM->GAINL = gain_l;
  377. NRF_PDM->GAINR = gain_r;
  378. }
  379. __STATIC_INLINE void nrf_pdm_gain_get(nrf_pdm_gain_t * p_gain_l, nrf_pdm_gain_t * p_gain_r)
  380. {
  381. *p_gain_l = NRF_PDM->GAINL;
  382. *p_gain_r = NRF_PDM->GAINR;
  383. }
  384. __STATIC_INLINE void nrf_pdm_buffer_set(uint32_t * p_buffer, uint32_t num)
  385. {
  386. NRF_PDM->SAMPLE.PTR = (uint32_t)p_buffer;
  387. NRF_PDM->SAMPLE.MAXCNT = num;
  388. }
  389. __STATIC_INLINE uint32_t * nrf_pdm_buffer_get(void)
  390. {
  391. return (uint32_t *)NRF_PDM->SAMPLE.PTR;
  392. }
  393. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  394. /** @} */
  395. #ifdef __cplusplus
  396. }
  397. #endif
  398. #endif // NRF_PDM_H_