nrf_pdm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /**
  2. * Copyright (c) 2015 - 2018, 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. *((volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)pdm_task)) = 0x1UL;
  115. }
  116. /**
  117. * @brief Function for getting the address of a PDM task register.
  118. *
  119. * @param[in] pdm_task PDM task.
  120. *
  121. * @return Address of the specified PDM task.
  122. */
  123. __STATIC_INLINE uint32_t nrf_pdm_task_address_get(nrf_pdm_task_t pdm_task)
  124. {
  125. return (uint32_t)((uint8_t *)NRF_PDM + (uint32_t)pdm_task);
  126. }
  127. /**
  128. * @brief Function for getting the state of a PDM event.
  129. *
  130. * @param[in] pdm_event PDM event.
  131. *
  132. * @return State of the specified PDM event.
  133. */
  134. __STATIC_INLINE bool nrf_pdm_event_check(nrf_pdm_event_t pdm_event)
  135. {
  136. return (bool)*(volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)pdm_event);
  137. }
  138. /**
  139. * @brief Function for clearing a PDM event.
  140. *
  141. * @param[in] pdm_event PDM event.
  142. */
  143. __STATIC_INLINE void nrf_pdm_event_clear(nrf_pdm_event_t pdm_event)
  144. {
  145. *((volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)pdm_event)) = 0x0UL;
  146. #if __CORTEX_M == 0x04
  147. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)pdm_event));
  148. (void)dummy;
  149. #endif
  150. }
  151. /**
  152. * @brief Function for getting the address of a PDM event register.
  153. *
  154. * @param[in] pdm_event PDM event.
  155. *
  156. * @return Address of the specified PDM event.
  157. */
  158. __STATIC_INLINE volatile uint32_t * nrf_pdm_event_address_get(nrf_pdm_event_t pdm_event)
  159. {
  160. return (volatile uint32_t *)((uint8_t *)NRF_PDM + (uint32_t)pdm_event);
  161. }
  162. /**
  163. * @brief Function for enabling PDM interrupts.
  164. *
  165. * @param[in] pdm_int_mask Interrupts to enable.
  166. */
  167. __STATIC_INLINE void nrf_pdm_int_enable(uint32_t pdm_int_mask)
  168. {
  169. NRF_PDM->INTENSET = pdm_int_mask;
  170. }
  171. /**
  172. * @brief Function for retrieving the state of PDM interrupts.
  173. *
  174. * @param[in] pdm_int_mask Interrupts to check.
  175. *
  176. * @retval true If all specified interrupts are enabled.
  177. * @retval false If at least one of the given interrupts is not enabled.
  178. */
  179. __STATIC_INLINE bool nrf_pdm_int_enable_check(uint32_t pdm_int_mask)
  180. {
  181. return (bool)(NRF_PDM->INTENSET & pdm_int_mask);
  182. }
  183. /**
  184. * @brief Function for disabling interrupts.
  185. *
  186. * @param pdm_int_mask Interrupts to disable.
  187. */
  188. __STATIC_INLINE void nrf_pdm_int_disable(uint32_t pdm_int_mask)
  189. {
  190. NRF_PDM->INTENCLR = pdm_int_mask;
  191. }
  192. /**
  193. * @brief Function for enabling the PDM peripheral.
  194. *
  195. * The PDM peripheral must be enabled before use.
  196. */
  197. __STATIC_INLINE void nrf_pdm_enable(void)
  198. {
  199. NRF_PDM->ENABLE = (PDM_ENABLE_ENABLE_Enabled << PDM_ENABLE_ENABLE_Pos);
  200. }
  201. /**
  202. * @brief Function for disabling the PDM peripheral.
  203. */
  204. __STATIC_INLINE void nrf_pdm_disable(void)
  205. {
  206. NRF_PDM->ENABLE = (PDM_ENABLE_ENABLE_Disabled << PDM_ENABLE_ENABLE_Pos);
  207. }
  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. return (NRF_PDM->ENABLE == (PDM_ENABLE_ENABLE_Enabled << PDM_ENABLE_ENABLE_Pos));
  217. }
  218. /**
  219. * @brief Function for setting the PDM operation mode.
  220. *
  221. * @param[in] pdm_mode PDM operation mode.
  222. * @param[in] pdm_edge PDM sampling mode.
  223. */
  224. __STATIC_INLINE void nrf_pdm_mode_set(nrf_pdm_mode_t pdm_mode, nrf_pdm_edge_t pdm_edge)
  225. {
  226. NRF_PDM->MODE = ((pdm_mode << PDM_MODE_OPERATION_Pos) & PDM_MODE_OPERATION_Msk)
  227. | ((pdm_edge << PDM_MODE_EDGE_Pos) & PDM_MODE_EDGE_Msk);
  228. }
  229. /**
  230. * @brief Function for getting the PDM operation mode.
  231. *
  232. * @param[out] p_pdm_mode PDM operation mode.
  233. * @param[out] p_pdm_edge PDM sampling mode.
  234. */
  235. __STATIC_INLINE void nrf_pdm_mode_get(nrf_pdm_mode_t * p_pdm_mode, nrf_pdm_edge_t * p_pdm_edge)
  236. {
  237. uint32_t mode = NRF_PDM->MODE;
  238. *p_pdm_mode = (nrf_pdm_mode_t)((mode & PDM_MODE_OPERATION_Msk ) >> PDM_MODE_OPERATION_Pos);
  239. *p_pdm_edge = (nrf_pdm_edge_t)((mode & PDM_MODE_EDGE_Msk ) >> PDM_MODE_EDGE_Pos);
  240. }
  241. /**
  242. * @brief Function for setting the PDM clock frequency.
  243. *
  244. * @param[in] pdm_freq PDM clock frequency.
  245. */
  246. __STATIC_INLINE void nrf_pdm_clock_set(nrf_pdm_freq_t pdm_freq)
  247. {
  248. NRF_PDM->PDMCLKCTRL = ((pdm_freq << PDM_PDMCLKCTRL_FREQ_Pos) & PDM_PDMCLKCTRL_FREQ_Msk);
  249. }
  250. /**
  251. * @brief Function for getting the PDM clock frequency.
  252. */
  253. __STATIC_INLINE nrf_pdm_freq_t nrf_pdm_clock_get(void)
  254. {
  255. return (nrf_pdm_freq_t) ((NRF_PDM->PDMCLKCTRL << PDM_PDMCLKCTRL_FREQ_Pos) & PDM_PDMCLKCTRL_FREQ_Msk);
  256. }
  257. /**
  258. * @brief Function for setting up the PDM pins.
  259. *
  260. * @param[in] psel_clk CLK pin number.
  261. * @param[in] psel_din DIN pin number.
  262. */
  263. __STATIC_INLINE void nrf_pdm_psel_connect(uint32_t psel_clk, uint32_t psel_din)
  264. {
  265. NRF_PDM->PSEL.CLK = psel_clk;
  266. NRF_PDM->PSEL.DIN = psel_din;
  267. }
  268. /**
  269. * @brief Function for disconnecting the PDM pins.
  270. */
  271. __STATIC_INLINE void nrf_pdm_psel_disconnect()
  272. {
  273. NRF_PDM->PSEL.CLK = ((PDM_PSEL_CLK_CONNECT_Disconnected << PDM_PSEL_CLK_CONNECT_Pos)
  274. & PDM_PSEL_CLK_CONNECT_Msk);
  275. NRF_PDM->PSEL.DIN = ((PDM_PSEL_DIN_CONNECT_Disconnected << PDM_PSEL_DIN_CONNECT_Pos)
  276. & PDM_PSEL_DIN_CONNECT_Msk);
  277. }
  278. /**
  279. * @brief Function for setting the PDM gain.
  280. *
  281. * @param[in] gain_l Left channel gain.
  282. * @param[in] gain_r Right channel gain.
  283. */
  284. __STATIC_INLINE void nrf_pdm_gain_set(nrf_pdm_gain_t gain_l, nrf_pdm_gain_t gain_r)
  285. {
  286. NRF_PDM->GAINL = gain_l;
  287. NRF_PDM->GAINR = gain_r;
  288. }
  289. /**
  290. * @brief Function for getting the PDM gain.
  291. *
  292. * @param[out] p_gain_l Left channel gain.
  293. * @param[out] p_gain_r Right channel gain.
  294. */
  295. __STATIC_INLINE void nrf_pdm_gain_get(nrf_pdm_gain_t * p_gain_l, nrf_pdm_gain_t * p_gain_r)
  296. {
  297. *p_gain_l = NRF_PDM->GAINL;
  298. *p_gain_r = NRF_PDM->GAINR;
  299. }
  300. /**
  301. * @brief Function for setting the PDM sample buffer.
  302. *
  303. * @param[in] p_buffer Pointer to the RAM address where samples should be written with EasyDMA.
  304. * @param[in] num Number of samples to allocate memory for in EasyDMA mode.
  305. *
  306. * The amount of allocated RAM depends on the operation mode.
  307. * - For stereo mode: N 32-bit words.
  308. * - For mono mode: Ceil(N/2) 32-bit words.
  309. */
  310. __STATIC_INLINE void nrf_pdm_buffer_set(uint32_t * p_buffer, uint32_t num)
  311. {
  312. NRF_PDM->SAMPLE.PTR = (uint32_t)p_buffer;
  313. NRF_PDM->SAMPLE.MAXCNT = num;
  314. }
  315. /**
  316. * @brief Function for getting the current PDM sample buffer address.
  317. *
  318. * @return Pointer to the current sample buffer.
  319. */
  320. __STATIC_INLINE uint32_t * nrf_pdm_buffer_get()
  321. {
  322. return (uint32_t *)NRF_PDM->SAMPLE.PTR;
  323. }
  324. /** @} */
  325. #ifdef __cplusplus
  326. }
  327. #endif
  328. #endif /* NRF_PDM_H_ */