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