nrf_pdm.h 15 KB

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