am_hal_pdm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //*****************************************************************************
  2. //
  3. // am_hal_pdm.c
  4. //! @file
  5. //!
  6. //! @brief Functions for interfacing with Pulse Density Modulation (PDM).
  7. //!
  8. //! @addtogroup pdm2 DMEMS Microphon3 (PDM)
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #include <stdint.h>
  48. #include <stdbool.h>
  49. #include "am_mcu_apollo.h"
  50. //*****************************************************************************
  51. //
  52. //! @brief Configure the PDM module.
  53. //!
  54. //! This function reads the an \e am_hal_pdm_config_t structure and uses it to
  55. //! set up the PDM module.
  56. //!
  57. //! Please see the information on the am_hal_pdm_config_t configuration
  58. //! structure, found in am_hal_pdm.h, for more information on the parameters
  59. //! that may be set by this function.
  60. //!
  61. //! @return None.
  62. //
  63. //*****************************************************************************
  64. void
  65. am_hal_pdm_config(am_hal_pdm_config_t *psConfig)
  66. {
  67. //
  68. // setup the PDM PCFG register
  69. //
  70. AM_REG(PDM, PCFG) = psConfig->ui32PDMConfigReg;
  71. //
  72. // setup the PDM VCFG register
  73. //
  74. AM_REG(PDM, VCFG) = psConfig->ui32VoiceConfigReg;
  75. //
  76. // setup the PDM FIFO Threshold register
  77. //
  78. AM_REG(PDM, FTHR) = psConfig->ui32FIFOThreshold;
  79. //
  80. // Flush the FIFO for good measure.
  81. //
  82. am_hal_pdm_fifo_flush();
  83. }
  84. //*****************************************************************************
  85. //
  86. //! @brief Enable the PDM module.
  87. //!
  88. //! This function enables the PDM module in the mode previously defined by
  89. //! am_hal_pdm_config().
  90. //!
  91. //! @return None.
  92. //
  93. //*****************************************************************************
  94. void
  95. am_hal_pdm_enable(void)
  96. {
  97. AM_REG(PDM, PCFG) |= AM_REG_PDM_PCFG_PDMCORE_EN;
  98. AM_REG(PDM, VCFG) |= ( AM_REG_PDM_VCFG_IOCLKEN_EN |
  99. AM_REG_PDM_VCFG_PDMCLK_EN |
  100. AM_REG_PDM_VCFG_RSTB_NORM );
  101. }
  102. //*****************************************************************************
  103. //
  104. //! @brief Disable the PDM module.
  105. //!
  106. //! This function disables the PDM module.
  107. //!
  108. //! @return None.
  109. //
  110. //*****************************************************************************
  111. void
  112. am_hal_pdm_disable(void)
  113. {
  114. AM_REG(PDM, PCFG) &= ~ AM_REG_PDM_PCFG_PDMCORE_EN;
  115. AM_REG(PDM, VCFG) &= ~ ( AM_REG_PDM_VCFG_IOCLKEN_EN |
  116. AM_REG_PDM_VCFG_PDMCLK_EN |
  117. AM_REG_PDM_VCFG_RSTB_NORM );
  118. }
  119. //*****************************************************************************
  120. //
  121. //! @brief Return the PDM Interrupt status.
  122. //!
  123. //! @param bEnabledOnly - return only the enabled interrupts.
  124. //!
  125. //! Use this function to get the PDM interrupt status.
  126. //!
  127. //! @return intrrupt status
  128. //
  129. //*****************************************************************************
  130. uint32_t
  131. am_hal_pdm_int_status_get(bool bEnabledOnly)
  132. {
  133. if ( bEnabledOnly )
  134. {
  135. uint32_t u32RetVal = AM_REG(PDM, INTSTAT);
  136. return u32RetVal & AM_REG(PDM, INTEN);
  137. }
  138. else
  139. {
  140. return AM_REG(PDM, INTSTAT);
  141. }
  142. }
  143. //*****************************************************************************
  144. //
  145. // End the doxygen group
  146. //! @}
  147. //
  148. //*****************************************************************************