fsl_dmic.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder 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. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _FSL_DMIC_H_
  35. #define _FSL_DMIC_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup dmic_driver
  39. * @{
  40. */
  41. /*! @file*/
  42. /*******************************************************************************
  43. * Definitions
  44. ******************************************************************************/
  45. /*!
  46. * @name DMIC version
  47. * @{
  48. */
  49. /*! @brief DMIC driver version 2.0.0. */
  50. #define FSL_DMIC_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  51. /*@}*/
  52. /*! @brief DMIC different operation modes. */
  53. typedef enum _operation_mode
  54. {
  55. kDMIC_OperationModePoll = 0U, /*!< Polling mode */
  56. kDMIC_OperationModeInterrupt = 1U, /*!< Interrupt mode */
  57. kDMIC_OperationModeDma = 2U, /*!< DMA mode */
  58. } operation_mode_t;
  59. /*! @brief DMIC left/right values. */
  60. typedef enum _stereo_side
  61. {
  62. kDMIC_Left = 0U, /*!< Left Stereo channel */
  63. kDMIC_Right = 1U, /*!< Right Stereo channel */
  64. } stereo_side_t;
  65. /*! @brief DMIC Clock pre-divider values. */
  66. typedef enum
  67. {
  68. kDMIC_PdmDiv1 = 0U, /*!< DMIC pre-divider set in divide by 1 */
  69. kDMIC_PdmDiv2 = 1U, /*!< DMIC pre-divider set in divide by 2 */
  70. kDMIC_PdmDiv3 = 2U, /*!< DMIC pre-divider set in divide by 3 */
  71. kDMIC_PdmDiv4 = 3U, /*!< DMIC pre-divider set in divide by 4 */
  72. kDMIC_PdmDiv6 = 4U, /*!< DMIC pre-divider set in divide by 6 */
  73. kDMIC_PdmDiv8 = 5U, /*!< DMIC pre-divider set in divide by 8 */
  74. kDMIC_PdmDiv12 = 6U, /*!< DMIC pre-divider set in divide by 12 */
  75. kDMIC_PdmDiv16 = 7U, /*!< DMIC pre-divider set in divide by 16*/
  76. kDMIC_PdmDiv24 = 8U, /*!< DMIC pre-divider set in divide by 24*/
  77. kDMIC_PdmDiv32 = 9U, /*!< DMIC pre-divider set in divide by 32 */
  78. kDMIC_PdmDiv48 = 10U, /*!< DMIC pre-divider set in divide by 48 */
  79. kDMIC_PdmDiv64 = 11U, /*!< DMIC pre-divider set in divide by 64*/
  80. kDMIC_PdmDiv96 = 12U, /*!< DMIC pre-divider set in divide by 96*/
  81. kDMIC_PdmDiv128 = 13U, /*!< DMIC pre-divider set in divide by 128 */
  82. } pdm_div_t;
  83. /*! @brief Pre-emphasis Filter coefficient value for 2FS and 4FS modes. */
  84. typedef enum _compensation
  85. {
  86. kDMIC_CompValueZero = 0U, /*!< Compensation 0 */
  87. kDMIC_CompValueNegativePoint16 = 1U, /*!< Compensation -0.16 */
  88. kDMIC_CompValueNegativePoint15 = 2U, /*!< Compensation -0.15 */
  89. kDMIC_CompValueNegativePoint13 = 3U, /*!< Compensation -0.13 */
  90. } compensation_t;
  91. /*! @brief DMIC DC filter control values. */
  92. typedef enum _dc_removal
  93. {
  94. kDMIC_DcNoRemove = 0U, /*!< Flat response no filter */
  95. kDMIC_DcCut155 = 1U, /*!< Cut off Frequency is 155 Hz */
  96. kDMIC_DcCut78 = 2U, /*!< Cut off Frequency is 78 Hz */
  97. kDMIC_DcCut39 = 3U, /*!< Cut off Frequency is 39 Hz */
  98. } dc_removal_t;
  99. /*! @brief DMIC IO configiration. */
  100. typedef enum _dmic_io
  101. {
  102. kDMIC_PdmDual = 0U, /*!< Two separate pairs of PDM wires */
  103. kDMIC_PdmStereo = 4U, /*!< Stereo Mic */
  104. kDMIC_PdmBypass = 3U, /*!< Clk Bypass clocks both channels */
  105. kDMIC_PdmBypassClk0 = 1U, /*!< Clk Bypass clocks only channel0 */
  106. kDMIC_PdmBypassClk1 = 2U, /*!< Clk Bypas clocks only channel1 */
  107. } dmic_io_t;
  108. /*! @brief DMIC Channel number. */
  109. typedef enum _dmic_channel
  110. {
  111. kDMIC_Channel0 = 0U, /*!< DMIC channel 0 */
  112. kDMIC_Channel1 = 1U, /*!< DMIC channel 1 */
  113. } dmic_channel_t;
  114. /*! @brief DMIC and decimator sample rates. */
  115. typedef enum _dmic_phy_sample_rate
  116. {
  117. kDMIC_PhyFullSpeed = 0U, /*!< Decimator gets one sample per each chosen clock edge of PDM interface */
  118. kDMIC_PhyHalfSpeed = 1U, /*!< PDM clock to Microphone is halved, decimator receives each sample twice */
  119. } dmic_phy_sample_rate_t;
  120. /*! @brief DMIC transfer status.*/
  121. enum _dmic_status
  122. {
  123. kStatus_DMIC_Busy = MAKE_STATUS(kStatusGroup_DMIC, 0), /*!< DMIC is busy */
  124. kStatus_DMIC_Idle = MAKE_STATUS(kStatusGroup_DMIC, 1), /*!< DMIC is idle */
  125. kStatus_DMIC_OverRunError = MAKE_STATUS(kStatusGroup_DMIC, 2), /*!< DMIC over run Error */
  126. kStatus_DMIC_UnderRunError = MAKE_STATUS(kStatusGroup_DMIC, 3), /*!< DMIC under run Error */
  127. };
  128. /*! @brief DMIC Channel configuration structure. */
  129. typedef struct _dmic_channel_config
  130. {
  131. pdm_div_t divhfclk; /*!< DMIC Clock pre-divider values */
  132. uint32_t osr; /*!< oversampling rate(CIC decimation rate) for PCM */
  133. int32_t gainshft; /*!< 4FS PCM data gain control */
  134. compensation_t preac2coef; /*!< Pre-emphasis Filter coefficient value for 2FS */
  135. compensation_t preac4coef; /*!< Pre-emphasis Filter coefficient value for 4FS */
  136. dc_removal_t dc_cut_level; /*!< DMIC DC filter control values. */
  137. uint32_t post_dc_gain_reduce; /*!< Fine gain adjustment in the form of a number of bits to downshift */
  138. dmic_phy_sample_rate_t sample_rate; /*!< DMIC and decimator sample rates */
  139. bool saturate16bit; /*!< Selects 16-bit saturation. 0 means results roll over if out range and do not saturate.
  140. 1 means if the result overflows, it saturates at 0xFFFF for positive overflow and
  141. 0x8000 for negative overflow.*/
  142. } dmic_channel_config_t;
  143. /*! @brief DMIC Callback function. */
  144. typedef void (*dmic_callback_t)(void);
  145. /*! @brief HWVAD Callback function. */
  146. typedef void (*dmic_hwvad_callback_t)(void);
  147. /*******************************************************************************
  148. * Definitions
  149. ******************************************************************************/
  150. /*******************************************************************************
  151. * API
  152. ******************************************************************************/
  153. #ifdef __cplusplus
  154. extern "C" {
  155. #endif
  156. /*!
  157. * @brief Get the DMIC instance from peripheral base address.
  158. *
  159. * @param base DMIC peripheral base address.
  160. * @return DMIC instance.
  161. */
  162. uint32_t DMIC_GetInstance(DMIC_Type *base);
  163. /*!
  164. * @brief Turns DMIC Clock on
  165. * @param base : DMIC base
  166. * @return Nothing
  167. */
  168. void DMIC_Init(DMIC_Type *base);
  169. /*!
  170. * @brief Turns DMIC Clock off
  171. * @param base : DMIC base
  172. * @return Nothing
  173. */
  174. void DMIC_DeInit(DMIC_Type *base);
  175. /*!
  176. * @brief Configure DMIC io
  177. * @param base : The base address of DMIC interface
  178. * @param config : DMIC io configuration
  179. * @return Nothing
  180. */
  181. void DMIC_ConfigIO(DMIC_Type *base, dmic_io_t config);
  182. /*!
  183. * @brief Set DMIC operating mode
  184. * @param base : The base address of DMIC interface
  185. * @param mode : DMIC mode
  186. * @return Nothing
  187. */
  188. void DMIC_SetOperationMode(DMIC_Type *base, operation_mode_t mode);
  189. /*!
  190. * @brief Configure DMIC channel
  191. * @param base : The base address of DMIC interface
  192. * @param channel : DMIC channel
  193. * @param side : stereo_side_t, choice of left or right
  194. * @param channel_config : Channel configuration
  195. * @return Nothing
  196. */
  197. void DMIC_ConfigChannel(DMIC_Type *base,
  198. dmic_channel_t channel,
  199. stereo_side_t side,
  200. dmic_channel_config_t *channel_config);
  201. /*!
  202. * @brief Configure DMIC channel
  203. * @param base : The base address of DMIC interface
  204. * @param channel : DMIC channel
  205. * @param dc_cut_level : dc_removal_t, Cut off Frequency
  206. * @param post_dc_gain_reduce : Fine gain adjustment in the form of a number of bits to downshift.
  207. * @param saturate16bit : If selects 16-bit saturation.
  208. */
  209. void DMIC_CfgChannelDc(DMIC_Type *base,
  210. dmic_channel_t channel,
  211. dc_removal_t dc_cut_level,
  212. uint32_t post_dc_gain_reduce,
  213. bool saturate16bit);
  214. /*!
  215. * @brief Configure Clock scaling
  216. * @param base : The base address of DMIC interface
  217. * @param use2fs : clock scaling
  218. * @return Nothing
  219. */
  220. void DMIC_Use2fs(DMIC_Type *base, bool use2fs);
  221. /*!
  222. * @brief Enable a particualr channel
  223. * @param base : The base address of DMIC interface
  224. * @param channelmask : Channel selection
  225. * @return Nothing
  226. */
  227. void DMIC_EnableChannnel(DMIC_Type *base, uint32_t channelmask);
  228. /*!
  229. * @brief Configure fifo settings for DMIC channel
  230. * @param base : The base address of DMIC interface
  231. * @param channel : DMIC channel
  232. * @param trig_level : FIFO trigger level
  233. * @param enable : FIFO level
  234. * @param resetn : FIFO reset
  235. * @return Nothing
  236. */
  237. void DMIC_FifoChannel(DMIC_Type *base, uint32_t channel, uint32_t trig_level, uint32_t enable, uint32_t resetn);
  238. /*!
  239. * @brief Get FIFO status
  240. * @param base : The base address of DMIC interface
  241. * @param channel : DMIC channel
  242. * @return FIFO status
  243. */
  244. static inline uint32_t DMIC_FifoGetStatus(DMIC_Type *base, uint32_t channel)
  245. {
  246. return base->CHANNEL[channel].FIFO_STATUS;
  247. }
  248. /*!
  249. * @brief Clear FIFO status
  250. * @param base : The base address of DMIC interface
  251. * @param channel : DMIC channel
  252. * @param mask : Bits to be cleared
  253. * @return FIFO status
  254. */
  255. static inline void DMIC_FifoClearStatus(DMIC_Type *base, uint32_t channel, uint32_t mask)
  256. {
  257. base->CHANNEL[channel].FIFO_STATUS = mask;
  258. }
  259. /*!
  260. * @brief Get FIFO data
  261. * @param base : The base address of DMIC interface
  262. * @param channel : DMIC channel
  263. * @return FIFO data
  264. */
  265. static inline uint32_t DMIC_FifoGetData(DMIC_Type *base, uint32_t channel)
  266. {
  267. return base->CHANNEL[channel].FIFO_DATA;
  268. }
  269. /*!
  270. * @brief Enable callback.
  271. * This function enables the interrupt for the selected DMIC peripheral.
  272. * The callback function is not enabled until this function is called.
  273. *
  274. * @param base Base address of the DMIC peripheral.
  275. * @param cb callback Pointer to store callback function.
  276. * @retval None.
  277. */
  278. void DMIC_EnableIntCallback(DMIC_Type *base, dmic_callback_t cb);
  279. /*!
  280. * @brief Disable callback.
  281. * This function disables the interrupt for the selected DMIC peripheral.
  282. *
  283. * @param base Base address of the DMIC peripheral.
  284. * @param cb callback Pointer to store callback function..
  285. * @retval None.
  286. */
  287. void DMIC_DisableIntCallback(DMIC_Type *base, dmic_callback_t cb);
  288. /**
  289. * @}
  290. */
  291. /*!
  292. * @name hwvad
  293. * @{
  294. */
  295. /*!
  296. * @brief Sets the gain value for the noise estimator.
  297. *
  298. * @param base DMIC base pointer
  299. * @param value gain value for the noise estimator.
  300. * @retval None.
  301. */
  302. static inline void DMIC_SetGainNoiseEstHwvad(DMIC_Type *base, uint32_t value)
  303. {
  304. assert(NULL != base);
  305. base->HWVADTHGN = value & 0xFu;
  306. }
  307. /*!
  308. * @brief Sets the gain value for the signal estimator.
  309. *
  310. * @param base DMIC base pointer
  311. * @param value gain value for the signal estimator.
  312. * @retval None.
  313. */
  314. static inline void DMIC_SetGainSignalEstHwvad(DMIC_Type *base, uint32_t value)
  315. {
  316. assert(NULL != base);
  317. base->HWVADTHGS = value & 0xFu;
  318. }
  319. /*!
  320. * @brief Sets the hwvad filter cutoff frequency parameter.
  321. *
  322. * @param base DMIC base pointer
  323. * @param value cut off frequency value.
  324. * @retval None.
  325. */
  326. static inline void DMIC_SetFilterCtrlHwvad(DMIC_Type *base, uint32_t value)
  327. {
  328. assert(NULL != base);
  329. base->HWVADHPFS = value & 0x3u;
  330. }
  331. /*!
  332. * @brief Sets the input gain of hwvad.
  333. *
  334. * @param base DMIC base pointer
  335. * @param value input gain value for hwvad.
  336. * @retval None.
  337. */
  338. static inline void DMIC_SetInputGainHwvad(DMIC_Type *base, uint32_t value)
  339. {
  340. assert(NULL != base);
  341. base->HWVADGAIN = value & 0xFu;
  342. }
  343. /*!
  344. * @brief Clears hwvad internal interrupt flag.
  345. *
  346. * @param base DMIC base pointer
  347. * @param st10 bit value.
  348. * @retval None.
  349. */
  350. static inline void DMIC_CtrlClrIntrHwvad(DMIC_Type *base, bool st10)
  351. {
  352. assert(NULL != base);
  353. base->HWVADST10 = (st10) ? 0x1 : 0x0;
  354. }
  355. /*!
  356. * @brief Resets hwvad filters.
  357. *
  358. * @param base DMIC base pointer
  359. * @param rstt Reset bit value.
  360. * @retval None.
  361. */
  362. static inline void DMIC_FilterResetHwvad(DMIC_Type *base, bool rstt)
  363. {
  364. assert(NULL != base);
  365. base->HWVADRSTT = (rstt) ? 0x1 : 0x0;
  366. }
  367. /*!
  368. * @brief Gets the value from output of the filter z7.
  369. *
  370. * @param base DMIC base pointer
  371. * @retval output of filter z7.
  372. */
  373. static inline uint16_t DMIC_GetNoiseEnvlpEst(DMIC_Type *base)
  374. {
  375. assert(NULL != base);
  376. return (base->HWVADLOWZ & 0xFFFFu);
  377. }
  378. /*!
  379. * @brief Enable hwvad callback.
  380. * This function enables the hwvad interrupt for the selected DMIC peripheral.
  381. * The callback function is not enabled until this function is called.
  382. *
  383. * @param base Base address of the DMIC peripheral.
  384. * @param vadcb callback Pointer to store callback function.
  385. * @retval None.
  386. */
  387. void DMIC_HwvadEnableIntCallback(DMIC_Type *base, dmic_hwvad_callback_t vadcb);
  388. /*!
  389. * @brief Disable callback.
  390. * This function disables the hwvad interrupt for the selected DMIC peripheral.
  391. *
  392. * @param base Base address of the DMIC peripheral.
  393. * @param vadcb callback Pointer to store callback function..
  394. * @retval None.
  395. */
  396. void DMIC_HwvadDisableIntCallback(DMIC_Type *base, dmic_hwvad_callback_t vadcb);
  397. /*! @} */
  398. #ifdef __cplusplus
  399. }
  400. #endif
  401. /*! @}*/
  402. #endif /* __FSL_DMIC_H */