1 /***************************************************************************//**
2 * \file cy_adcmic.h
3 * \version 1.10
4 *
5 * Provides an API declaration of the ADCMic driver.
6 *
7 ********************************************************************************
8 * \copyright
9 * Copyright 2022 Cypress Semiconductor Corporation
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 *     http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *******************************************************************************/
24 
25 /**
26 * \addtogroup group_adcmic
27 * \{
28 * ADCMic driver is used to process analog and digital microphone signal and
29 * DC signal with the mxs40adcmic IP. This IP interfaces with Delta-Sigma modulator part
30 * of the s40adcmic and implements CIC, decimation (FIR) and biquad filters.
31 * The ADC result is read by the CPU or the DMA from the FIFO of mxs40adcmic
32 * (and from CIC register for DC measurement). Instead of taking modulator data
33 * from s40adcmic, mxs40adcmic can also be configured to take PDM input
34 * directly from an external digital microphone.
35 *
36 * \image html ADCMicBlockDiagram.png
37 *
38 * Consult the datasheet of your device for details of the
39 * clocking system.
40 *
41 * The high level features of the subsystem are:
42 *   - Analog (MIC) and digital (PDM) microphones support.
43 *   - Sample rate 8/16 ksps for audio (MIC and PDM modes).
44 *   - Configurable biquad filter for custom audio equalization.
45 *   - Selectable DC measurement range.
46 *   - Interrupt & trigger generation.
47 *   - Built-in FIFO buffer (for audio only).
48 *
49 * \section group_adcmic_usage Usage
50 *
51 * The high level steps to use this driver are:
52 *
53 *   -# \ref group_adcmic_initialization
54 *   -# \ref group_adcmic_clock
55 *   -# \ref group_adcmic_trigger
56 *   -# \ref group_adcmic_timer
57 *   -# \ref group_adcmic_interrupt
58 *   -# \ref group_adcmic_dc_measurement
59 *   -# \ref group_adcmic_fifo
60 *
61 * \section group_adcmic_initialization Initialization and Enabling
62 *
63 * To configure the ADCMic subsystem call \ref Cy_ADCMic_Init.
64 * Pass in a pointer to the \ref MXS40ADCMIC_Type structure for the base hardware register address,
65 * pass in the configuration structure \ref cy_stc_adcmic_config_t, and
66 * pass in the operation mode.
67 *
68 * After initialization, call \ref Cy_ADCMic_Enable to enable the block.
69 *
70 * The configuration can be defined as follows:
71 * \snippet adcmic_snippet/snippet.c SNIPPET_ADCMIC_CFG
72 *
73 * \subsection group_adcmic_mic Analog Microphone (MIC) Mode
74 *
75 * \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_MIC_OP
76 *
77 * Usually the MIC mode is used with FIFO, see \ref group_adcmic_fifo
78 *
79 * \subsection group_adcmic_pdm Digital Microphone (PDM) Mode
80 *
81 * \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_PDM_OP
82 *
83 * Usually the MIC mode is used with FIFO, see \ref group_adcmic_fifo
84 *
85 * \subsection group_adcmic_dc DC Measurement (DC) Mode
86 *
87 * \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_DC_OP
88 *
89 * \subsection group_adcmic_biquad Biquad filter Initialization
90 *
91 * The biquad filter usually is used to the audio stream equalization (in MIC or PDM modes):
92 * \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_BIQUAD
93 * \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_BIQUAD_INIT
94 *
95 * \section group_adcmic_clock Clocks
96 *
97 * The ADCMic requires two input clocks:
98 * - clk_sys - recommended frequency is 96MHz, input for the \ref group_adcmic_timer, usually is routed from one of the \ref group_sysclk_clk_hf
99 * - clk_hf - recommended frequency is 24MHz, the audio (MIC and PDM modes) reference clock, usually is routed from one of the \ref group_sysclk_clk_hf
100 *
101 * For more exact information on the ADCMic clock routing, refer to the datasheet for your device.
102 *
103 * \section group_adcmic_trigger Triggering
104 *
105 * The ADCMic subsystem has two output triggers: from the timer and from the FIFO,
106 * the timer generates trigger always if enabled, the FIFO trigger could be
107 * configured separately for MIC and PDM modes by the \ref cy_stc_adcmic_mic_config_t::fifoTrigger and
108 * \ref cy_stc_adcmic_pdm_config_t::fifoTrigger respectively.
109 *
110 * Also, they could be routed to any periphery using \ref group_trigmux driver, e.g. to DW block:
111 * \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_TRIGGER
112 *
113 * \section group_adcmic_timer Timer
114 *
115 * The Timer is used for DC measurement for two purposes:
116 * - generate periodic events based on ADCMic clk_sys clock.
117 * - count the CIC data update events.
118 *
119 * The timer period and input signal source are configured by the \ref cy_stc_adcmic_dc_config_t::timerPeriod and
120 * \ref cy_stc_adcmic_dc_config_t::timerInput fields correspondingly.
121 *
122 * \section group_adcmic_interrupt Handling Interrupts
123 *
124 * The ADCMic subsystem has two interrupt sources: the timer and the FIFO.
125 * The FIFO interrupt can have several reasons, see \ref group_adcmic_macros_fifo_status
126 *
127 * The ADCMic interrupt to the NVIC is raised any time the intersection (logic and) of the interrupt
128 * flags and the corresponding interrupt masks are non-zero.
129 *
130 * Implement an interrupt routine and assign it to the ADCMic interrupt.
131 * Use the pre-defined enumeration, adcmic_interrupt_adcmic_IRQn, as the interrupt source for the ADCMic.
132 *
133 * The following code snippet demonstrates how to implement a routine to handle the interrupt.
134 * The routine gets called when any one of the ADCMic interrupts are triggered.
135 * When servicing an interrupt, the user must clear the interrupt so that subsequent
136 * interrupts can be handled.
137 *
138 * The following code snippet demonstrates how to configure and enable the interrupt.
139 * \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_ISR
140 * \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_CONFIG_INTR
141 *
142 * Alternately, instead of handling the interrupts, the \ref Cy_ADCMic_IsEndConversion function
143 * allows for firmware polling of the end of DC conversion status.
144 *
145 * \section group_adcmic_fifo FIFO Usage
146 * The ADCMic subsystem in the MIC and PDM modes stores the audio data into the FIFO.
147 * It can be configured separately for the MIC and PDM modes using
148 * \ref cy_stc_adcmic_mic_config_t::fifoFull,
149 * \ref cy_stc_adcmic_mic_config_t::fifoEmpty and
150 * \ref cy_stc_adcmic_pdm_config_t::fifoFull,
151 * \ref cy_stc_adcmic_pdm_config_t::fifoEmpty
152 * respectively and served either by ISR:
153 * \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_ISR
154 * Or by DMA:
155 * \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_TRIGGER
156 *
157 * \section group_adcmic_dc_measurement DC Voltage Measurement Accuracy
158 *
159 * * The Offset is practically a raw count of the measured Ground voltage.
160 * * The Gain is essentially an amount of raw counts per 1 Volt of input voltage.
161 *   It depends on the DC measurement range \ref cy_en_adcmic_dc_range_t.
162 *
163 * The default Offset \ref CY_ADCMIC_DC_OFFSET and
164 * Gain for both ranges \ref CY_ADCMIC_DC_1_8_GAIN and \ref CY_ADCMIC_DC_3_6_GAIN are precalculated
165 * based on the theory of the ADCMic operation \ref group_adcmic_macros_dc_measurement.
166 *
167 * So basically the raw count retrieved using \ref Cy_ADCMic_GetDcResult for the desired DC input
168 * can be directly feed into any of the \ref Cy_ADCMic_CountsTo_Volts, \ref Cy_ADCMic_CountsTo_mVolts,
169 * or \ref Cy_ADCMic_CountsTo_uVolts functions and have for some extend accurate result.
170 *
171 * However, to increase the accuracy the real ADCMic Gain and Offset can be defined by physically
172 * measuring the Reference Ground \ref CY_ADCMIC_REFGND and the Reference BandGap \ref CY_ADCMIC_BGREF.
173 *
174 * For example:
175 * \snippet adcmic_snippet/snippet.c SNIPPET_ADCMIC_DC_MEASURE_AVG
176 * \note this code snippet is not the only valid way to use this driver - it is just one possible example how it can be used.
177 *
178 * \subsection group_adcmic_dc_vbg_calib BandGap Calibration
179 * For further increase the measurement accuracy, the real BandGap voltage can be defined
180 * using external precise reference with exact known voltage:
181 *
182 * Real BandGap voltage = external reference exact voltage * (BandGap counts - GND counts) / (external reference counts - GND counts).
183 *
184 * And then use it in the Gain calculation as shown above instead of the nominal BandGap voltage \ref CY_ADCMIC_DC_VBG.
185 *
186 * \subsection group_adcmic_dc_double_sampling Correlated Double Sampling
187 * To further improve the DC measurement accuracy, the low frequency Noise could be dynamically
188 * removed by performing two measurements for each input channel sequentially:
189 * one measuring the desired DC input and the other one measuring the \ref CY_ADCMIC_REFGND.
190 * The second measurement of the\ref CY_ADCMIC_REFGND contains only the low frequency Noise (Flick Noise) information.
191 * It is important to perform these two measurements one after another as close as possible.
192 * The final true ADC output value is then obtained by subtracting the ADC output of the
193 * second measurement from the ADC output of the first measurement.
194 *
195 * For example:
196 * \snippet adcmic_snippet/snippet.c SNIPPET_ADCMIC_DC_MEASURE_DS
197 * \note this code snippet is not the only valid way to use this driver - it is just one possible example how it can be used.
198 *
199 * \section group_adcmic_more_information More Information
200 * For more information on the ADCMic ADC subsystem, refer to the datasheet of your device.
201 *
202 * \section group_adcmic_changelog Changelog
203 * <table class="doxtable">
204 *   <tr><th>Version</th><th>Changes</th><th>Reason for Change</th></tr>
205 *   <tr>
206 *     <td>1.10</td>
207 *     <td>The ADCMic PDL driver ID \ref CY_ADCMIC_ID is updated</td>
208 *     <td>Avoiding possible driver ID collisions</td>
209 *   </tr>
210 *   <tr>
211 *     <td>1.0</td>
212 *     <td>
213 *       The cy_en_adcmic_source_t is renamed to \ref cy_en_adcmic_mode_t.<br>
214 *       The CY_ADCMIC_480KSPS item is removed from the \ref cy_en_adcmic_sample_rate_t.<br>
215 *       The cy_en_adcmic_dc_conv_time_t is removed.<br>
216 *       The cy_stc_adcmic_fifo_config_t is removed.<br>
217 *       The cy_en_adcmic_dc_result_latch_mode_t is removed.<br>
218 *       The cy_stc_adcmic_audio_analog_path_config_t is renamed to \ref cy_stc_adcmic_mic_config_t.<br>
219 *       The cy_stc_adcmic_audio_digital_path_config_t is renamed to \ref cy_stc_adcmic_pdm_config_t.<br>
220 *       The cy_stc_adcmic_dc_path_config_t is renamed to \ref cy_stc_adcmic_dc_config_t.<br>
221 *
222 *       The micPd parameter is removed from the \ref cy_stc_adcmic_mic_config_t<br>
223 *
224 *       The next parameters are added into \ref cy_stc_adcmic_mic_config_t :<br>
225 *       * \ref cy_stc_adcmic_mic_config_t::sampleRate,<br>
226 *       * \ref cy_stc_adcmic_mic_config_t::biQuadConfig,<br>
227 *       * \ref cy_stc_adcmic_mic_config_t::fifoTrigger,<br>
228 *       * \ref cy_stc_adcmic_mic_config_t::fifoFull,<br>
229 *       * \ref cy_stc_adcmic_mic_config_t::fifoEmpty.<br>
230 *
231 *       The clockDiv parameter is removed from the \ref cy_stc_adcmic_pdm_config_t<br>
232 *
233 *       The next parameters are added into \ref cy_stc_adcmic_pdm_config_t :<br>
234 *       * \ref cy_stc_adcmic_pdm_config_t::sampleRate,<br>
235 *       * \ref cy_stc_adcmic_pdm_config_t::biQuadConfig,<br>
236 *       * \ref cy_stc_adcmic_pdm_config_t::fifoTrigger,<br>
237 *       * \ref cy_stc_adcmic_pdm_config_t::fifoFull,<br>
238 *       * \ref cy_stc_adcmic_pdm_config_t::fifoEmpty.<br>
239 *
240 *       The next parameters are removed from the \ref cy_stc_adcmic_dc_config_t :<br>
241 *       * tmrLatch,<br>
242 *       * time.<br>
243 *
244 *       The next parameter is renamed in the \ref cy_stc_adcmic_dc_config_t :<br>
245 *       * input -> \ref cy_stc_adcmic_dc_config_t::channel.<br>
246 *
247 *       The next parameters are added into \ref cy_stc_adcmic_dc_config_t :<br>
248 *       * \ref cy_stc_adcmic_dc_config_t::timerPeriod,<br>
249 *       * \ref cy_stc_adcmic_dc_config_t::timerInput,<br>
250 *       * \ref cy_stc_adcmic_dc_config_t::context.<br>
251 *
252 *       The next parameters are removed from the cy_stc_adcmic_config_t :<br>
253 *       * clockDiv,<br>
254 *       * source,<br>
255 *       * sampleRate,<br>
256 *       * biQuadConfig,<br>
257 *       * fifoConfig,<br>
258 *       * tmrTrgConfig.<br>
259 *
260 *       The next parameters are renamed in the cy_stc_adcmic_config_t :<br>
261 *       * anaConfig -> \ref cy_stc_adcmic_config_t::micConfig,<br>
262 *       * digConfig -> \ref cy_stc_adcmic_config_t::pdmConfig.<br>
263 *
264 *       The new parameter 'mode' is added to the \ref Cy_ADCMic_Init.<br>
265 *       The \ref Cy_ADCMic_Init functions if fixed to proper process the \ref cy_stc_adcmic_mic_config_t::micClamp setting.<br>
266 *       The Cy_ADCMic_StartConvert and Cy_ADCMic_StopConvert API functions are removed.<br>
267 *       The \ref Cy_ADCMic_Enable and \ref Cy_ADCMic_Disable are updated to inherit the Start/StopConvert functionality.<br>
268 *       The Cy_ADCMic_SleepMic and Cy_ADCMic_WakeUpMic API functions are removed.<br>
269 *       The \ref Cy_ADCMic_Enable and \ref Cy_ADCMic_Disable are updated to power down all the analog subsystems at disabling and power up them at enabling.<br>
270 *       The parameter dcChannel of the \ref Cy_ADCMic_SelectDcChannel is renamed to channel.<br>
271 *       The function Cy_ADCMic_SetDcConvTime is removed.<br>
272 *       The interface of \ref Cy_ADCMic_SetDcOffset, \ref Cy_ADCMic_SetDcGain, \ref Cy_ADCMic_CountsTo_mVolts, \ref Cy_ADCMic_CountsTo_uVolts,<br>
273 *       and \ref Cy_ADCMic_CountsTo_Volts functions is changed: the 'base' parameters are removed, and the 'context' parameters are added.<br>
274 *       The documentation is enhanced with code snippets: \ref group_adcmic_dc_measurement, \ref group_adcmic_dc_double_sampling.
275 *     </td>
276 *     <td>Usability review</td>
277 *   </tr>
278 *   <tr>
279 *     <td>0.1</td>
280 *     <td>This is a pre-production driver release. The driver is not recommended for production use, unless the functionality is delivered in Cypress-provided applications.</td>
281 *     <td>Pre-production support of the CAT1B Devices</td>
282 *   </tr>
283 * </table>
284 *
285 * \defgroup group_adcmic_macros Macros
286 * \{
287 *   \defgroup group_adcmic_macros_interrupt        Interrupt Masks
288 *   \defgroup group_adcmic_macros_triggers         Triggers
289 *   \defgroup group_adcmic_macros_reg_pointers     Register Pointers
290 *   \defgroup group_adcmic_macros_fifo_status      FIFO Status Masks
291 *   \defgroup group_adcmic_macros_fifo_samples     FIFO Samples definitions
292 *   \defgroup group_adcmic_macros_dc_measurement   DC measurement definitions
293 * \}
294 * \defgroup group_adcmic_enums Enumerated Types
295 * \defgroup group_adcmic_data_structures Data Structures
296 * \defgroup group_adcmic_functions Functions
297 * \{
298 *   \defgroup group_adcmic_functions_basic         Initialization and Basic Functions
299 *   \defgroup group_adcmic_functions_interrupt     Interrupt Functions
300 *   \defgroup group_adcmic_functions_fifo          FIFO Functions
301 *   \defgroup group_adcmic_functions_dc            DC Measurement Functions
302 *   \defgroup group_adcmic_functions_timer         Timer Functions
303 *   \defgroup group_adcmic_functions_mic           Analog Microphone (MIC) Functions
304 * \}
305 */
306 
307 #if !defined (CY_ADCMIC_H)
308 #define CY_ADCMIC_H
309 
310 #include "cy_device_headers.h"
311 
312 #if defined (CY_IP_MXS40ADCMIC)
313 
314 #include "cy_syslib.h"
315 
316 #if defined(__cplusplus)
317 extern "C" {
318 #endif
319 
320 /**
321 * \addtogroup group_adcmic_macros
322 * \{
323 */
324 /** Driver major version */
325 #define CY_ADCMIC_DRV_VERSION_MAJOR            1
326 
327 /** Driver minor version */
328 #define CY_ADCMIC_DRV_VERSION_MINOR            10
329 
330 /** ADCMic PDL ID */
331 #define CY_ADCMIC_ID                           CY_PDL_DRV_ID(0x4CU)
332 
333 /**
334 * \addtogroup group_adcmic_macros_interrupt
335 * \{
336 */
337 
338 #define CY_ADCMIC_INTR_DC     (MXS40ADCMIC_ADCMIC_INTR_INTERRUPT_DC_Msk)   /**< DC interrupt mask */
339 #define CY_ADCMIC_INTR_DATA   (MXS40ADCMIC_ADCMIC_INTR_INTERRUPT_DATA_Msk) /**< DATA interrupt mask */
340 #define CY_ADCMIC_INTR        (CY_ADCMIC_INTR_DC | CY_ADCMIC_INTR_DATA)    /**< Combined interrupt mask */
341 
342 /** \} group_adcmic_macros_interrupt */
343 
344 /**
345 * \addtogroup group_adcmic_macros_triggers
346 * \{
347 */
348 
349 #define CY_ADCMIC_TRIG_DC     (MXS40ADCMIC_ADCMIC_TRIGGER_CLR_TR_DC_Msk)   /**< DC trigger mask, to be used with \ref Cy_ADCMic_ClearTrigger */
350 #define CY_ADCMIC_TRIG_DATA   (MXS40ADCMIC_ADCMIC_TRIGGER_CLR_TR_DATA_Msk) /**< DATA trigger mask, to be used with \ref Cy_ADCMic_ClearTrigger */
351 #define CY_ADCMIC_TRIG        (CY_ADCMIC_TRIG_DC | CY_ADCMIC_TRIG_DATA)    /**< Combined interrupt mask, to be used with \ref Cy_ADCMic_ClearTrigger */
352 
353 /** \} group_adcmic_macros_triggers */
354 
355 /**
356 * \addtogroup group_adcmic_macros_reg_pointers
357 * \{
358 */
359 
360 /** The pointer to the FIFO data read register, to be used for the DMA data acquisition */
361 #define CY_ADCMIC_FIFO_DATA_REG_PTR(base)   ((void*)&((base)->ADCMIC_DATA))
362 
363 /** The pointer to the trigger clearing register, to be used for the DMA data acquisition */
364 #define CY_ADCMIC_TRIGGER_CLR_REG_PTR(base) ((void*)&((base)->ADCMIC_TRIGGER_CLR))
365 
366 /** \} group_adcmic_macros_reg_pointers */
367 
368 /**
369 * \addtogroup group_adcmic_macros_fifo_status
370 * \{
371 */
372 #define CY_ADCMIC_FIFO_OVERFLOW     (0x01U)   /**< FIFO overflow status mask */
373 #define CY_ADCMIC_FIFO_PRG_FULL     (0x02U)   /**< FIFO programmable full status mask */
374 #define CY_ADCMIC_FIFO_ALMOST_FULL  (0x04U)   /**< FIFO almost full status mask */
375 #define CY_ADCMIC_FIFO_FULL         (0x08U)   /**< FIFO full status mask */
376 #define CY_ADCMIC_FIFO_UNDERFLOW    (0x10U)   /**< FIFO underflow status mask */
377 #define CY_ADCMIC_FIFO_PRG_EMPTY    (0x20U)   /**< FIFO programmable empty status mask */
378 #define CY_ADCMIC_FIFO_ALMOST_EMPTY (0x40U)   /**< FIFO almost empty status mask */
379 #define CY_ADCMIC_FIFO_EMPTY        (0x80U)   /**< FIFO empty status mask */
380 /** \} group_adcmic_macros_fifo_status */
381 
382 /**
383 * \addtogroup group_adcmic_macros_fifo_samples
384 * To be used with low-level API \ref Cy_ADCMic_ReadFifo.
385 * \{
386 */
387 #define CY_ADCMIC_FIFO_FIRST_SAMPLE_Pos     (0U)           /**< the ADCMIC_DATA first sample position */
388 #define CY_ADCMIC_FIFO_FIRST_SAMPLE_Msk     (0x0000FFFFUL) /**< the ADCMIC_DATA first sample mask */
389 #define CY_ADCMIC_FIFO_SECOND_SAMPLE_Pos    (16U)          /**< the ADCMIC_DATA second sample position */
390 #define CY_ADCMIC_FIFO_SECOND_SAMPLE_Msk    (0xFFFF0000UL) /**< the ADCMIC_DATA second sample mask */
391 /** \} group_adcmic_macros_fifo_samples */
392 
393 /**
394 * \addtogroup group_adcmic_macros_dc_measurement
395 * To be used for DC measurement voltage calculation/calibration.
396 * \{
397 */
398 /** The Full Scale counts */
399 #define CY_ADCMIC_DC_FS         (0xFFFFUL)
400 /** The coefficient to calculate lower range limit count */
401 #define CY_ADCMIC_DC_LO         (3U)
402 /** The coefficient to calculate upper range limit count, scaled by 10000 to avoid fractional math */
403 #define CY_ADCMIC_DC_HI         (3725U)
404 /** The default raw count offset for DC measurement */
405 #define CY_ADCMIC_DC_OFFSET     (-((int16_t)CY_SYSLIB_DIV_ROUND(CY_ADCMIC_DC_FS, CY_ADCMIC_DC_LO)))
406 /** The upper range limit count */
407 #define CY_ADCMIC_DC_HI_CNT     ((int16_t)CY_SYSLIB_DIV_ROUND(CY_ADCMIC_DC_FS * CY_ADCMIC_DC_HI, 10000U))
408 /** The full range counts, scaled by 10 to avoid fractional math */
409 #define CY_ADCMIC_DC_FS_CNT     ((uint32_t)(CY_ADCMIC_DC_HI_CNT - CY_ADCMIC_DC_OFFSET) * 10U)
410 /** The default amount of raw counts per 1 volt for 3.6V range */
411 #define CY_ADCMIC_DC_3_6_GAIN   ((int16_t)CY_SYSLIB_DIV_ROUND(CY_ADCMIC_DC_FS_CNT, 36U))
412 /** The default amount of raw counts per 1 volt for 1.8V range */
413 #define CY_ADCMIC_DC_1_8_GAIN   ((int16_t)CY_SYSLIB_DIV_ROUND(CY_ADCMIC_DC_FS_CNT, 18U))
414 /** The BandGap reference nominal voltage in millivolts */
415 #define CY_ADCMIC_DC_VBG        (850L)
416 /** \} group_adcmic_macros_dc_measurement */
417 /** \} group_adcmic_macros */
418 
419 /** \addtogroup group_adcmic_enums
420 * \{
421 */
422 
423 /** The ADC error codes */
424 typedef enum
425 {
426     CY_ADCMIC_SUCCESS                 = 0x00UL,                                      /**< Successful */
427     CY_ADCMIC_BAD_PARAM               = CY_ADCMIC_ID | CY_PDL_STATUS_ERROR | 0x01UL, /**< One or more invalid parameters */
428     CY_ADCMIC_TIMEOUT                 = CY_ADCMIC_ID | CY_PDL_STATUS_ERROR | 0x02UL, /**< Timeout */
429     CY_ADCMIC_CONVERSION_NOT_COMPLETE = CY_ADCMIC_ID | CY_PDL_STATUS_INFO  | 0x03UL, /**< Conversion is not complete */
430     CY_ADCMIC_UNKNOWN                 = CY_ADCMIC_ID | CY_PDL_STATUS_ERROR | 0xFFUL, /**< Unknown failure */
431 } cy_en_adcmic_status_t;
432 
433 
434 /** The PGA gain settings */
435 typedef enum
436 {
437     CY_ADCMIC_PGA_GAIN_0  = 0U,  /**<  0 db */
438     CY_ADCMIC_PGA_GAIN_1  = 1U,  /**<  1 db */
439     CY_ADCMIC_PGA_GAIN_2  = 2U,  /**<  2 db */
440     CY_ADCMIC_PGA_GAIN_3  = 3U,  /**<  3 db */
441     CY_ADCMIC_PGA_GAIN_4  = 4U,  /**<  4 db */
442     CY_ADCMIC_PGA_GAIN_5  = 5U,  /**<  5 db */
443     CY_ADCMIC_PGA_GAIN_6  = 6U,  /**<  6 db */
444     CY_ADCMIC_PGA_GAIN_7  = 7U,  /**<  7 db */
445     CY_ADCMIC_PGA_GAIN_8  = 8U,  /**<  8 db */
446     CY_ADCMIC_PGA_GAIN_9  = 9U,  /**<  9 db */
447     CY_ADCMIC_PGA_GAIN_10 = 10U, /**< 10 db */
448     CY_ADCMIC_PGA_GAIN_11 = 11U, /**< 11 db */
449     CY_ADCMIC_PGA_GAIN_12 = 12U, /**< 12 db */
450     CY_ADCMIC_PGA_GAIN_13 = 13U, /**< 13 db */
451     CY_ADCMIC_PGA_GAIN_14 = 14U, /**< 14 db */
452     CY_ADCMIC_PGA_GAIN_15 = 15U, /**< 15 db */
453     CY_ADCMIC_PGA_GAIN_16 = 16U, /**< 16 db */
454     CY_ADCMIC_PGA_GAIN_17 = 17U, /**< 17 db */
455     CY_ADCMIC_PGA_GAIN_18 = 18U, /**< 18 db */
456     CY_ADCMIC_PGA_GAIN_19 = 19U, /**< 19 db */
457     CY_ADCMIC_PGA_GAIN_20 = 20U, /**< 20 db */
458     CY_ADCMIC_PGA_GAIN_21 = 21U, /**< 21 db */
459     CY_ADCMIC_PGA_GAIN_22 = 22U, /**< 22 db */
460     CY_ADCMIC_PGA_GAIN_23 = 23U, /**< 23 db */
461     CY_ADCMIC_PGA_GAIN_24 = 24U, /**< 24 db */
462     CY_ADCMIC_PGA_GAIN_25 = 25U, /**< 25 db */
463     CY_ADCMIC_PGA_GAIN_26 = 26U, /**< 26 db */
464     CY_ADCMIC_PGA_GAIN_27 = 27U, /**< 27 db */
465     CY_ADCMIC_PGA_GAIN_28 = 28U, /**< 28 db */
466     CY_ADCMIC_PGA_GAIN_29 = 29U, /**< 29 db */
467     CY_ADCMIC_PGA_GAIN_30 = 30U, /**< 30 db */
468     CY_ADCMIC_PGA_GAIN_31 = 31U, /**< 31 db */
469     CY_ADCMIC_PGA_GAIN_32 = 32U, /**< 32 db */
470     CY_ADCMIC_PGA_GAIN_33 = 33U, /**< 33 db */
471     CY_ADCMIC_PGA_GAIN_34 = 34U, /**< 34 db */
472     CY_ADCMIC_PGA_GAIN_35 = 35U, /**< 35 db */
473     CY_ADCMIC_PGA_GAIN_36 = 36U, /**< 36 db */
474     CY_ADCMIC_PGA_GAIN_37 = 37U, /**< 37 db */
475     CY_ADCMIC_PGA_GAIN_38 = 38U, /**< 38 db */
476     CY_ADCMIC_PGA_GAIN_39 = 39U, /**< 39 db */
477     CY_ADCMIC_PGA_GAIN_40 = 40U, /**< 40 db */
478     CY_ADCMIC_PGA_GAIN_41 = 41U, /**< 41 db */
479     CY_ADCMIC_PGA_GAIN_42 = 42U  /**< 42 db */
480 } cy_en_adcmic_pga_gain_t;
481 
482 /** The operation mode */
483 typedef enum
484 {
485     CY_ADCMIC_DC  = 1U, /**< DC voltage measurement */
486     CY_ADCMIC_MIC = 3U, /**< Analog microphone with PGA */
487     CY_ADCMIC_PDM = 4U  /**< PDM digital microphone */
488 } cy_en_adcmic_mode_t;
489 
490 /** The sample rate for MIC and PDM modes */
491 typedef enum
492 {
493     CY_ADCMIC_8KSPS,  /**< 8 ksps, for microphone only */
494     CY_ADCMIC_16KSPS  /**< 16 ksps, for microphone only */
495 } cy_en_adcmic_sample_rate_t;
496 
497 /** The DC channel
498  * \note Some options may be unavailable on particular devices.
499  */
500 typedef enum
501 {
502     CY_ADCMIC_REFGND = 0x0FU, /**< ADC reference ground */
503     CY_ADCMIC_BGREF  = 0x0EU, /**< ADC BG REF */
504     CY_ADCMIC_VDDC   = 0x0DU, /**< Core supply */
505     CY_ADCMIC_VDDIO  = 0x0CU, /**< Battery/IO supply */
506     CY_ADCMIC_GPIO7  = 0x17U, /**< GPIO 7  */
507     CY_ADCMIC_GPIO6  = 0x16U, /**< GPIO 6  */
508     CY_ADCMIC_GPIO5  = 0x15U, /**< GPIO 5  */
509     CY_ADCMIC_GPIO4  = 0x14U, /**< GPIO 4  */
510     CY_ADCMIC_GPIO3  = 0x13U, /**< GPIO 3  */
511     CY_ADCMIC_GPIO2  = 0x12U, /**< GPIO 2  */
512     CY_ADCMIC_GPIO1  = 0x11U, /**< GPIO 1  */
513     CY_ADCMIC_GPIO0  = 0x10U  /**< GPIO 0  */
514 } cy_en_adcmic_dc_channel_t;
515 
516 /** Definitions for the return mode used in \ref Cy_ADCMic_IsEndConversion */
517 typedef enum
518 {
519     CY_ADCMIC_RETURN_STATUS      = 0U,    /**< Immediately returns the conversion status. */
520     CY_ADCMIC_WAIT_FOR_RESULT    = 1U     /**< Does not return a result until the conversion is complete. This mode is blocking. */
521 } cy_en_adcmic_return_mode_t;
522 
523 
524 /** Definitions for the microphone bias settings */
525 typedef enum
526 {
527     CY_ADCMIC_BIAS_1_12_REF      = 0U,    /**< 1.12 X Reference Voltage */
528     CY_ADCMIC_BIAS_1_14_REF      = 1U,    /**< 1.14 X Reference Voltage */
529     CY_ADCMIC_BIAS_1_17_REF      = 2U,    /**< 1.12 X Reference Voltage */
530     CY_ADCMIC_BIAS_1_10_REF      = 3U     /**< 1.10 X Reference Voltage */
531 } cy_en_adcmic_bias_t;
532 
533 /** Definitions for the PGA input common mode settings */
534 typedef enum
535 {
536     CY_ADCMIC_INCM_0_4           = 0U,    /**< 0.4 X aVdd */
537     CY_ADCMIC_INCM_0_45          = 1U,    /**< 0.45 X aVdd */
538     CY_ADCMIC_INCM_0_35          = 2U     /**< 0.35 X aVdd */
539 } cy_en_adcmic_pga_incm_t;
540 
541 /** Definitions for the PGA input common mode settings */
542 typedef enum
543 {
544     CY_ADCMIC_OUTCM_0_6          = 0U,    /**< 0.6 */
545     CY_ADCMIC_OUTCM_0_7          = 1U,    /**< 0.7 */
546     CY_ADCMIC_OUTCM_0_5          = 2U     /**< 0.5 */
547 } cy_en_adcmic_pga_outcm_t;
548 
549 
550 /** Definitions for the DC input range settings */
551 typedef enum
552 {
553     CY_ADCMIC_DC_RANGE_3_6V      = 0U,    /**< DC range 0..3.6V */
554     CY_ADCMIC_DC_RANGE_1_8V      = 1U     /**< DC range 0..1.8V */
555 } cy_en_adcmic_dc_range_t;
556 
557 /** Definitions for the timer count input settings */
558 typedef enum
559 {
560     CY_ADCMIC_TIMER_COUNT_INPUT_CIC_UPDATE = 0U, /**< Counts when CIC is updated */
561     CY_ADCMIC_TIMER_COUNT_INPUT_CLK_SYS    = 1U  /**< Counts up every clk_sys cycle */
562 } cy_en_adcmic_timer_count_input_t;
563 
564 /** \} group_adcmic_enums */
565 
566 /** \addtogroup group_adcmic_data_structures
567 * \{
568 */
569 
570 /** The biquad filter configuration structure */
571 typedef struct
572 {
573     uint16_t bq0_num1_coeff; /**< The numerator 1 for biquad filter stage 0 */
574     uint16_t bq0_num2_coeff; /**< The numerator 2 for biquad filter stage 0 */
575     uint16_t bq0_num3_coeff; /**< The numerator 3 for biquad filter stage 0 */
576     uint16_t bq0_den2_coeff; /**< The denominator 2 for biquad filter stage 0 */
577     uint16_t bq0_den3_coeff; /**< The denominator 3 for biquad filter stage 0 */
578     uint16_t bq1_num1_coeff; /**< The numerator 1 for biquad filter stage 1 */
579     uint16_t bq1_num2_coeff; /**< The numerator 2 for biquad filter stage 1 */
580     uint16_t bq1_num3_coeff; /**< The numerator 3 for biquad filter stage 1 */
581     uint16_t bq1_den2_coeff; /**< The denominator 2 for biquad filter stage 1 */
582     uint16_t bq1_den3_coeff; /**< The denominator 3 for biquad filter stage 1 */
583     uint16_t bq2_num1_coeff; /**< The numerator 1 for biquad filter stage 2 */
584     uint16_t bq2_num2_coeff; /**< The numerator 2 for biquad filter stage 2 */
585     uint16_t bq2_num3_coeff; /**< The numerator 3 for biquad filter stage 2 */
586     uint16_t bq2_den2_coeff; /**< The denominator 2 for biquad filter stage 2 */
587     uint16_t bq2_den3_coeff; /**< The denominator 3 for biquad filter stage 2 */
588     uint16_t bq3_num1_coeff; /**< The numerator 1 for biquad filter stage 3 */
589     uint16_t bq3_num2_coeff; /**< The numerator 2 for biquad filter stage 3 */
590     uint16_t bq3_num3_coeff; /**< The numerator 3 for biquad filter stage 3 */
591     uint16_t bq3_den2_coeff; /**< The denominator 2 for biquad filter stage 3 */
592     uint16_t bq3_den3_coeff; /**< The denominator 3 for biquad filter stage 3 */
593     uint16_t bq4_num1_coeff; /**< The numerator 1 for biquad filter stage 4 */
594     uint16_t bq4_num2_coeff; /**< The numerator 2 for biquad filter stage 4 */
595     uint16_t bq4_num3_coeff; /**< The numerator 3 for biquad filter stage 4 */
596     uint16_t bq4_den2_coeff; /**< The denominator 2 for biquad filter stage 4 */
597     uint16_t bq4_den3_coeff; /**< The denominator 3 for biquad filter stage 4 */
598 } cy_stc_adcmic_biquad_config_t;
599 
600 /** The analog microphone configuration structure */
601 typedef struct
602 {
603     cy_en_adcmic_sample_rate_t        sampleRate; /**< Sample rate */
604     cy_en_adcmic_bias_t                  micBias; /**< MIC bias selection */
605     bool                               micBiasLz; /**< MIC bias output impedance during power down: false - hight Z, true - low Z */
606     bool                                micClamp; /**< Enable MIC PGA clamping */
607     cy_en_adcmic_pga_gain_t              pgaGain; /**< PGA gain 0...42 dB */
608     cy_en_adcmic_pga_incm_t              pgaInCm; /**< PGA input common mode voltage selection */
609     cy_en_adcmic_pga_outcm_t            pgaOutCm; /**< PGA output common mode voltage selection */
610     cy_stc_adcmic_biquad_config_t * biQuadConfig; /**< The pointer to the biquad filter configuration structure */
611     bool                             fifoTrigger; /**< Trigger out on valid FIFO data */
612     uint8_t                             fifoFull; /**< Count for FIFO full condition */
613     uint8_t                            fifoEmpty; /**< Count for FIFO empty condition */
614 } cy_stc_adcmic_mic_config_t;
615 
616 /** The PDM path configuration structure */
617 typedef struct
618 {
619     cy_en_adcmic_sample_rate_t        sampleRate; /**< Sample rate */
620     bool                                clockInv; /**< Inverts the clock */
621     uint8_t                           latchDelay; /**< the PDM data latch delay,
622                                                    *   when clockInv is false, the valid range is 1...8
623                                                    *   when clockInv is true, the valid range is 2...9
624                                                    */
625     cy_stc_adcmic_biquad_config_t * biQuadConfig; /**< The pointer to the biquad filter configuration structure */
626     bool                             fifoTrigger; /**< Trigger out on valid FIFO data */
627     uint8_t                             fifoFull; /**< Count for FIFO full condition */
628     uint8_t                            fifoEmpty; /**< Count for FIFO empty condition */
629 } cy_stc_adcmic_pdm_config_t;
630 
631 /** The ADCMic driver context structure - it stores the gain and offset values for DC voltage measurement. */
632 typedef struct
633 {
634     int16_t offset; /**< The storage for the offset calibration value */
635     int16_t gain;   /**< The storage for the gain calibration value */
636 } cy_stc_adcmic_context_t;
637 
638 /** The DC measurement path configuration structure */
639 typedef struct
640 {
641     cy_en_adcmic_dc_range_t               range; /**< Selects the DC measurement range */
642     cy_en_adcmic_dc_channel_t           channel; /**< DC channel to be measured */
643     uint16_t                        timerPeriod; /**< Timer period */
644     cy_en_adcmic_timer_count_input_t timerInput; /**< Timer count input */
645     cy_stc_adcmic_context_t           * context; /**< The pointer to the context structure.
646                                                   *   If none of the \ref Cy_ADCMic_CountsTo_uVolts,
647                                                   *                  \ref Cy_ADCMic_CountsTo_mVolts,
648                                                   *              nor \ref Cy_ADCMic_CountsTo_Volts
649                                                   *   functions is used, this pointer can be NULL.
650                                                   */
651 } cy_stc_adcmic_dc_config_t;
652 
653 /** The ADCMic driver configuration structure. */
654 typedef struct
655 {
656     cy_stc_adcmic_mic_config_t * micConfig; /**< The pointer to the audio analog path configuration structure */
657     cy_stc_adcmic_pdm_config_t * pdmConfig; /**< The pointer to the audio digital path configuration structure */
658     cy_stc_adcmic_dc_config_t  * dcConfig;  /**< The pointer to the DC measurement path configuration structure */
659 } cy_stc_adcmic_config_t;
660 
661 /** \} group_adcmic_data_structures */
662 
663 /** \addtogroup group_adcmic_functions
664 * \{
665 */
666 
667 /** \addtogroup group_adcmic_functions_basic
668 * This set of functions is for initialization and basic usage
669 * \{
670 */
671 
672 /*******************************************************************************
673 * Function Name: Cy_ADCMic_Init
674 ****************************************************************************//**
675 *
676 * Initializes the ADCMic block from a pre-initialized configuration structure.
677 *
678 * \param base
679 * The pointer to the hardware ADCMic block.
680 *
681 * \param config
682 * The pointer to the configuration structure \ref cy_stc_adcmic_config_t.
683 *
684 * \param mode
685 * The ADCMic operation mode /ref cy_en_adcmic_mode_t.
686 *
687 * \return
688 * The status \ref cy_en_adcmic_status_t.
689 *
690 * \funcusage
691 * \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_MIC_OP
692 *
693 *******************************************************************************/
694 cy_en_adcmic_status_t Cy_ADCMic_Init(MXS40ADCMIC_Type * base, cy_stc_adcmic_config_t const * config, cy_en_adcmic_mode_t mode);
695 
696 
697 /*******************************************************************************
698 * Function Name: Cy_ADCMic_InitBiquad
699 ****************************************************************************//**
700 *
701 * Initializes the biquad filter coefficients.
702 * Usually is called from the \ref Cy_ADCMic_Init.
703 *
704 * \param base
705 * The pointer to the hardware ADCMic block.
706 *
707 * \param biQuadConfig
708 * The pointer to the biquad filter configuration structure \ref cy_stc_adcmic_biquad_config_t.
709 *
710 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_BIQUAD_INIT
711 *
712 *******************************************************************************/
713 void Cy_ADCMic_InitBiquad(MXS40ADCMIC_Type * base, cy_stc_adcmic_biquad_config_t const * biQuadConfig);
714 
715 
716 /*******************************************************************************
717 * Function Name: Cy_ADCMic_BiquadBypass
718 ****************************************************************************//**
719 *
720 * Bypasses/unbypasses the biquad filter.
721 * Usually is called from the \ref Cy_ADCMic_Init.
722 *
723 * \param base
724 * The pointer to the hardware ADCMic block.
725 *
726 * \param bypass
727 * The boolean specifier to bypass(true) or unbypass(false) the biquad filter.
728 *
729 *******************************************************************************/
Cy_ADCMic_BiquadBypass(MXS40ADCMIC_Type * base,bool bypass)730 __STATIC_INLINE void Cy_ADCMic_BiquadBypass(MXS40ADCMIC_Type * base, bool bypass)
731 {
732     CY_REG32_CLR_SET(base->AUXADC_CTRL, MXS40ADCMIC_AUXADC_CTRL_BIQUAD_BYPASS, (bypass) ? 1UL : 0UL);
733 }
734 
735 
736 /*******************************************************************************
737 * Function Name: Cy_ADCMic_Enable
738 ****************************************************************************//**
739 *
740 * Enables the ADCMic block operation.
741 *
742 * \param base
743 * The pointer to the hardware ADCMic block.
744 *
745 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_MIC_OP
746 *
747 *******************************************************************************/
748 void Cy_ADCMic_Enable(MXS40ADCMIC_Type * base);
749 
750 
751 /*******************************************************************************
752 * Function Name: Cy_ADCMic_Disable
753 ****************************************************************************//**
754 *
755 * Disables the ADCMic block operation.
756 *
757 * \param base
758 * The pointer to the hardware ADCMic block.
759 *
760 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_STOP
761 *
762 *******************************************************************************/
763 void Cy_ADCMic_Disable(MXS40ADCMIC_Type * base);
764 
765 
766 /*******************************************************************************
767 * Function Name: Cy_ADCMic_SetSampleRate
768 ****************************************************************************//**
769 *
770 * Sets the specified ADC sample rate. Applicable for MIC/PDM modes only.
771 *
772 * \param base
773 * The pointer to the hardware ADCMic block.
774 *
775 * \param sampleRate
776 * The source value \ref cy_en_adcmic_sample_rate_t.
777 *
778 * \return
779 * The status \ref cy_en_adcmic_status_t.
780 *
781 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_MIC_OP
782 *
783 *******************************************************************************/
784 cy_en_adcmic_status_t Cy_ADCMic_SetSampleRate(MXS40ADCMIC_Type * base, cy_en_adcmic_sample_rate_t sampleRate);
785 
786 
787 /*******************************************************************************
788 * Function Name: Cy_ADCMic_ClearTrigger
789 ****************************************************************************//**
790 *
791 * Clears the specified trigger signal.
792 *
793 * \param base
794 * The pointer to the hardware ADC block.
795 *
796 * \param trigMask
797 * The mask of triggers to clear.
798 * Select one or more masks from \ref group_adcmic_macros_triggers and "OR" them together:
799 * - \ref CY_ADCMIC_TRIG_DC
800 * - \ref CY_ADCMIC_TRIG_DATA
801 *
802 *******************************************************************************/
Cy_ADCMic_ClearTrigger(MXS40ADCMIC_Type * base,uint32_t trigMask)803 __STATIC_INLINE void Cy_ADCMic_ClearTrigger(MXS40ADCMIC_Type * base, uint32_t trigMask)
804 {
805     base->ADCMIC_TRIGGER_CLR = trigMask & CY_ADCMIC_TRIG;
806 }
807 /** \} group_adcmic_functions_basic */
808 
809 /** \addtogroup group_adcmic_functions_mic
810 * This set of functions is for analog microphone resources control
811 * \{
812 */
813 
814 /*******************************************************************************
815 * Function Name: Cy_ADCMic_SetPgaGain
816 ****************************************************************************//**
817 *
818 * Configures the PGA gain (0dB to 42dB in steps of 1dB)
819 *
820 * \param base
821 * The pointer to the hardware ADCMic block.
822 *
823 * \param gain
824 * The gain value \ref cy_en_adcmic_pga_gain_t.
825 *
826 * \return
827 * The status \ref cy_en_adcmic_status_t.
828 *
829 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_GAIN
830 *
831 *******************************************************************************/
832 cy_en_adcmic_status_t Cy_ADCMic_SetPgaGain(MXS40ADCMIC_Type * base, cy_en_adcmic_pga_gain_t gain);
833 
834 /** \} group_adcmic_functions_mic */
835 
836 /** \addtogroup group_adcmic_functions_timer
837 * This set of functions is for Timer control
838 * \{
839 */
840 
841 /*******************************************************************************
842 * Function Name: Cy_ADCMic_EnableTimer
843 ****************************************************************************//**
844 *
845 * Enables the timer.
846 *
847 * \param base
848 * The pointer to the hardware ADCMic block.
849 *
850 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_TIMER
851 *
852 *******************************************************************************/
Cy_ADCMic_EnableTimer(MXS40ADCMIC_Type * base)853 __STATIC_INLINE void Cy_ADCMic_EnableTimer(MXS40ADCMIC_Type * base)
854 {
855     base->ADCMIC_TRIG_INTRPT_TIMER_CTRL &= ~MXS40ADCMIC_ADCMIC_TRIG_INTRPT_TIMER_CTRL_TIMER_CLR_Msk;
856     (void)base->ADCMIC_TRIG_INTRPT_TIMER_CTRL; /* Dummy read to ensure write is done before return */
857 }
858 
859 
860 /*******************************************************************************
861 * Function Name: Cy_ADCMic_DisableTimer
862 ****************************************************************************//**
863 *
864 * Disables the timer.
865 *
866 * \param base
867 * The pointer to the hardware ADCMic block.
868 *
869 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_TIMER
870 *
871 *******************************************************************************/
Cy_ADCMic_DisableTimer(MXS40ADCMIC_Type * base)872 __STATIC_INLINE void Cy_ADCMic_DisableTimer(MXS40ADCMIC_Type * base)
873 {
874     base->ADCMIC_TRIG_INTRPT_TIMER_CTRL |= MXS40ADCMIC_ADCMIC_TRIG_INTRPT_TIMER_CTRL_TIMER_CLR_Msk;
875     (void)base->ADCMIC_TRIG_INTRPT_TIMER_CTRL; /* Dummy read to ensure write is done before return */
876 }
877 
878 
879 /*******************************************************************************
880 * Function Name: Cy_ADCMic_SetTimerPeriod
881 ****************************************************************************//**
882 *
883 * Sets the timer period.
884 *
885 * \param base
886 * The pointer to the hardware ADCMic block.
887 *
888 * \param period
889 * The 16-bit timer period.
890 *
891 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_TIMER
892 *
893 *******************************************************************************/
Cy_ADCMic_SetTimerPeriod(MXS40ADCMIC_Type * base,uint16_t period)894 __STATIC_INLINE void Cy_ADCMic_SetTimerPeriod(MXS40ADCMIC_Type * base, uint16_t period)
895 {
896     CY_REG32_CLR_SET(base->ADCMIC_TRIG_INTRPT_TIMER_CTRL, MXS40ADCMIC_ADCMIC_TRIG_INTRPT_TIMER_CTRL_TIMER_LIMIT, period);
897 }
898 /** \} group_adcmic_functions_timer */
899 
900 /** \addtogroup group_adcmic_functions_dc
901 * This set of functions is for DC measurement mode
902 * \{
903 */
904 
905 /*******************************************************************************
906 * Function Name: Cy_ADCMic_SelectDcChannel
907 ****************************************************************************//**
908 *
909 * Sets the specified DC measurement channel.
910 *
911 * \param base
912 * The pointer to the hardware ADCMic block.
913 *
914 * \param channel
915 * The DC measurement channel \ref cy_en_adcmic_dc_channel_t.
916 *
917 * \note This function is useful for DC measurement only.
918 *
919 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_DC_OP
920 *
921 *******************************************************************************/
922 void Cy_ADCMic_SelectDcChannel(MXS40ADCMIC_Type * base, cy_en_adcmic_dc_channel_t channel);
923 
924 
925 /*******************************************************************************
926 * Function Name: Cy_ADCMic_IsEndConversion
927 ****************************************************************************//**
928 *
929 * Immediately return the status of the conversion or does not return (blocking)
930 * until the conversion completes, depending on the retMode parameter.
931 * In blocking mode, there is a time out of about 10 seconds for a CPU speed of
932 * 100 MHz.
933 *
934 * \param base
935 * Pointer to structure describing registers
936 *
937 * \param retMode
938 * A value of the enum \ref cy_en_adcmic_return_mode_t
939 *
940 * \return
941 * - \ref CY_ADCMIC_SUCCESS : the last conversion is complete
942 * - \ref CY_ADCMIC_CONVERSION_NOT_COMPLETE : the conversion has not completed
943 * - \ref CY_ADCMIC_TIMEOUT : the watchdog timer has expired in blocking mode
944 *
945 * \sideeffect
946 * This function reads the end of conversion status interrupt bit and clears it after.
947 *
948 * \note
949 * \ref CY_ADCMIC_WAIT_FOR_RESULT return modes are not recommended
950 * for use in RTOS environment.
951 *
952 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_DC_OP
953 *
954 *******************************************************************************/
955 cy_en_adcmic_status_t Cy_ADCMic_IsEndConversion (MXS40ADCMIC_Type * base, cy_en_adcmic_return_mode_t retMode);
956 
957 
958 /*******************************************************************************
959 * Function Name: Cy_ADCMic_GetDcResult
960 ****************************************************************************//**
961 *
962 * Returns the DC conversion result from the CIC status register.
963 *
964 * \param base
965 * The pointer to the hardware ADCMic block.
966 *
967 * \return
968 * The signed DC conversion result.
969 *
970 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_DC_OP
971 *
972 *******************************************************************************/
Cy_ADCMic_GetDcResult(MXS40ADCMIC_Type * base)973 __STATIC_INLINE int16_t Cy_ADCMic_GetDcResult(MXS40ADCMIC_Type * base)
974 {
975     return ((int16_t)((uint16_t)_FLD2VAL(MXS40ADCMIC_AUXADC_CIC_STATUS_CIC, base->AUXADC_CIC_STATUS)));
976 }
977 
978 
979 /*******************************************************************************
980 * Function Name: Cy_ADCMic_SetDcOffset
981 ****************************************************************************//**
982 *
983 * Sets the offset value for DC measurement calibration.
984 *
985 * \param offset
986 * The offset value.
987 *
988 * \param context
989 * The pointer to the context structure \ref cy_stc_adcmic_context_t.
990 *
991 *******************************************************************************/
992 void Cy_ADCMic_SetDcOffset(int16_t offset, cy_stc_adcmic_context_t * context);
993 
994 
995 /*******************************************************************************
996 * Function Name: Cy_ADCMic_SetDcGain
997 ****************************************************************************//**
998 *
999 * Sets the gain value for DC measurement calibration.
1000 *
1001 * \param gain
1002 * The gain value.
1003 *
1004 * \param context
1005 * The pointer to the context structure \ref cy_stc_adcmic_context_t.
1006 *
1007 *******************************************************************************/
1008 void Cy_ADCMic_SetDcGain(int16_t gain, cy_stc_adcmic_context_t * context);
1009 
1010 
1011 /*******************************************************************************
1012 * Function Name: Cy_ADCMic_CountsTo_mVolts
1013 ****************************************************************************//**
1014 *
1015 * Convert the ADC output to millivolts as an int16. For example, if the ADC
1016 * measured 0.534 volts, the return value would be 534.
1017 * The calculation of voltage depends on the DC range.
1018 * The equation used is:
1019 *
1020 *     mV = (RawCounts - Offset) * 1000 / Gain
1021 *
1022 * where,
1023 * - RawCounts: Raw counts returned by the \ref Cy_ADCMic_GetDcResult.
1024 * - Offset - the offset value, can be adjusted by the \ref Cy_ADCMic_SetDcOffset,
1025 *   essentially it is a raw count value of the \ref CY_ADCMIC_REFGND voltage measurement.
1026 * - 1000 - amount of millivolts in 1 volt
1027 * - Gain: the gain value, can be adjusted by the \ref Cy_ADCMic_SetDcGain, depends on DC range,
1028 *   essentially it is an amount of raw counts per 1 volt of input voltage.
1029 *
1030 * \note
1031 * This function is for DC measurement only.
1032 *
1033 * \param adcCounts
1034 * Conversion result from \ref Cy_ADCMic_GetDcResult
1035 *
1036 * \param context
1037 * The pointer to the context structure \ref cy_stc_adcmic_context_t.
1038 *
1039 * \return
1040 * Result in millivolts.
1041 *
1042 * \funcusage \snippet adcmic_snippet/snippet.c SNIPPET_ADCMIC_DC_MEASURE
1043 *
1044 *******************************************************************************/
1045 int16_t Cy_ADCMic_CountsTo_mVolts(int16_t adcCounts, cy_stc_adcmic_context_t const * context);
1046 
1047 
1048 /*******************************************************************************
1049 * Function Name: Cy_ADCMic_CountsTo_uVolts
1050 ****************************************************************************//**
1051 *
1052 * Convert the ADC output to microvolts as an int32. For example, if the ADC
1053 * measured 0.534 volts, the return value would be 534000.
1054 * The calculation of voltage depends on the DC range.
1055 * The equation used is:
1056 *
1057 *     uV = (RawCounts - Offset) * 1000000 / Gain
1058 *
1059 * where,
1060 * - RawCounts: Raw counts returned by the \ref Cy_ADCMic_GetDcResult.
1061 * - Offset - the offset value, can be adjusted by the \ref Cy_ADCMic_SetDcOffset,
1062 *   essentially it is a raw count value of the \ref CY_ADCMIC_REFGND voltage measurement.
1063 * - 1000000 - amount of microvolts in 1 volt
1064 * - Gain: the gain value, can be adjusted by the \ref Cy_ADCMic_SetDcGain, depends on DC range,
1065 *   essentially it is an amount of raw counts per 1 volt of input voltage.
1066 *
1067 * \note
1068 * This function is for DC measurement only.
1069 *
1070 * \param adcCounts
1071 * Conversion result from \ref Cy_ADCMic_GetDcResult
1072 *
1073 * \param context
1074 * The pointer to the context structure \ref cy_stc_adcmic_context_t.
1075 *
1076 * \return
1077 * Result in microvolts.
1078 *
1079 * \funcusage \snippet adcmic_snippet/snippet.c SNIPPET_ADCMIC_DC_MEASURE
1080 *
1081 *******************************************************************************/
1082 int32_t Cy_ADCMic_CountsTo_uVolts(int16_t adcCounts, cy_stc_adcmic_context_t const * context);
1083 
1084 
1085 /*******************************************************************************
1086 * Function Name: Cy_ADCMic_CountsTo_Volts
1087 ****************************************************************************//**
1088 *
1089 * Convert the ADC output to volts as a float. For example, if the ADC
1090 * measured 0.534 volts, the return value would be 0.534.
1091 * The calculation of voltage depends on the DC range.
1092 * The equation used is:
1093 *
1094 *     V = (RawCounts - Offset) / Gain
1095 *
1096 * where,
1097 * - RawCounts: Raw counts returned by the \ref Cy_ADCMic_GetDcResult.
1098 * - Offset - the offset value, can be adjusted by the \ref Cy_ADCMic_SetDcOffset,
1099 *   essentially it is a raw count value of the \ref CY_ADCMIC_REFGND voltage measurement.
1100 * - Gain: the gain value, can be adjusted by the \ref Cy_ADCMic_SetDcGain, depends on DC range,
1101 *   essentially it is an amount of raw counts per 1 volt of input voltage.
1102 *
1103 * \note
1104 * This function is for DC measurement only.
1105 *
1106 * \param adcCounts
1107 * Conversion result from \ref Cy_ADCMic_GetDcResult
1108 *
1109 * \param context
1110 * The pointer to the context structure \ref cy_stc_adcmic_context_t.
1111 *
1112 * \return
1113 * Result in volts.
1114 *
1115 * \funcusage \snippet adcmic_snippet/snippet.c SNIPPET_ADCMIC_DC_MEASURE
1116 *
1117 *******************************************************************************/
1118 float Cy_ADCMic_CountsTo_Volts(int16_t adcCounts, cy_stc_adcmic_context_t const * context);
1119 /** \} group_adcmic_functions_dc */
1120 
1121 /** \addtogroup group_adcmic_functions_interrupt
1122 * This set of functions are related to ADCMic interrupts.
1123 * \{
1124 */
1125 
1126 /*******************************************************************************
1127 * Function Name: Cy_ADCMic_EnableInterrupt
1128 ****************************************************************************//**
1129 *
1130 * Sets the specified interrupt bit in the interrupt mask register.
1131 *
1132 * \param base
1133 * The pointer to the hardware ADCMic block.
1134 *
1135 * \param intrMask
1136 * The mask of interrupts to enable.
1137 * Select one or more values from \ref group_adcmic_macros_interrupt and "OR" them together.
1138 * - \ref CY_ADCMIC_INTR_DC
1139 * - \ref CY_ADCMIC_INTR_DATA
1140 *
1141 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_CONFIG_INTR
1142 *
1143 *******************************************************************************/
1144 void Cy_ADCMic_EnableInterrupt(MXS40ADCMIC_Type * base, uint32_t intrMask);
1145 
1146 
1147 /*******************************************************************************
1148 * Function Name: Cy_ADCMic_DisableInterrupt
1149 ****************************************************************************//**
1150 *
1151 * Clears the specified interrupt bit in the interrupt mask register.
1152 *
1153 * \param base
1154 * The pointer to the hardware ADCMic block.
1155 *
1156 * \param intrMask
1157 * The mask of interrupts to disable.
1158 * Select one or more values from \ref group_adcmic_macros_interrupt and "OR" them together.
1159 * - \ref CY_ADCMIC_INTR_DC
1160 * - \ref CY_ADCMIC_INTR_DATA
1161 *
1162 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_CONFIG_INTR
1163 *
1164 *******************************************************************************/
1165 void Cy_ADCMic_DisableInterrupt(MXS40ADCMIC_Type * base, uint32_t intrMask);
1166 
1167 
1168 /*******************************************************************************
1169 * Function Name: CY_ADCMIC_GetInterruptStatus
1170 ****************************************************************************//**
1171 *
1172 * Returns the interrupt status of the specified channel.
1173 *
1174 * \param base
1175 * The pointer to the hardware ADCMic block.
1176 *
1177 * \return
1178 * The interrupt status.
1179 *
1180 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_CONFIG_INTR
1181 *
1182 *******************************************************************************/
Cy_ADCMic_GetInterruptStatus(MXS40ADCMIC_Type const * base)1183 __STATIC_INLINE uint32_t Cy_ADCMic_GetInterruptStatus(MXS40ADCMIC_Type const * base)
1184 {
1185     return (base->ADCMIC_INTR & CY_ADCMIC_INTR);
1186 }
1187 
1188 
1189 /*******************************************************************************
1190 * Function Name: Cy_ADCMic_ClearInterrupt
1191 ****************************************************************************//**
1192 *
1193 * Clears the interrupt status.
1194 *
1195 * \param base
1196 * The pointer to the hardware ADC block.
1197 *
1198 * \param intrMask
1199 * The mask of interrupts to clear. Typically this will be the value returned
1200 * from \ref Cy_ADCMic_GetInterruptStatus.
1201 * Alternately, select one or more values from \ref group_adcmic_macros_interrupt and "OR" them together.
1202 * - \ref CY_ADCMIC_INTR_DC
1203 * - \ref CY_ADCMIC_INTR_DATA
1204 *
1205 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_CONFIG_INTR
1206 *
1207 *******************************************************************************/
Cy_ADCMic_ClearInterrupt(MXS40ADCMIC_Type * base,uint32_t intrMask)1208 __STATIC_INLINE void Cy_ADCMic_ClearInterrupt(MXS40ADCMIC_Type * base, uint32_t intrMask)
1209 {
1210     base->ADCMIC_INTR = intrMask & CY_ADCMIC_INTR;
1211     /* This dummy reading is necessary here. It provides a guarantee that interrupt is cleared at returning from this function. */
1212     (void) base->ADCMIC_INTR;
1213 }
1214 
1215 
1216 /*******************************************************************************
1217 * Function Name: Cy_ADCMic_SetInterrupt
1218 ****************************************************************************//**
1219 *
1220 * Sets the interrupt for the specified channel.
1221 * Intended mostly for debugging.
1222 *
1223 * \param base
1224 * The pointer to the hardware ADCMic block.
1225 *
1226 * \param intrMask
1227 * The mask of interrupts to set.
1228 * Select one or more values from \ref group_adcmic_macros_interrupt and "OR" them together.
1229 * - \ref CY_ADCMIC_INTR_DC
1230 * - \ref CY_ADCMIC_INTR_DATA
1231 *
1232 *******************************************************************************/
Cy_ADCMic_SetInterrupt(MXS40ADCMIC_Type * base,uint32_t intrMask)1233 __STATIC_INLINE void Cy_ADCMic_SetInterrupt(MXS40ADCMIC_Type * base, uint32_t intrMask)
1234 {
1235     base->ADCMIC_INTR_SET = intrMask & CY_ADCMIC_INTR;
1236 }
1237 
1238 
1239 /*******************************************************************************
1240 * Function Name: CY_ADCMIC_GetInterruptMask
1241 ****************************************************************************//**
1242 *
1243 * Returns the interrupt mask value of the specified channel.
1244 *
1245 * \param base
1246 * The pointer to the hardware ADCMic block.
1247 *
1248 * \return
1249 * The interrupt mask value.
1250 *
1251 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_GET_INTERRUPT_MASK
1252 *
1253 *******************************************************************************/
Cy_ADCMic_GetInterruptMask(MXS40ADCMIC_Type const * base)1254 __STATIC_INLINE uint32_t Cy_ADCMic_GetInterruptMask(MXS40ADCMIC_Type const * base)
1255 {
1256     return (base->ADCMIC_INTR_MASK & CY_ADCMIC_INTR);
1257 }
1258 
1259 
1260 /*******************************************************************************
1261 * Function Name: CY_ADCMIC_SetInterruptMask
1262 ****************************************************************************//**
1263 *
1264 * Sets an interrupt mask value for the specified channel.
1265 *
1266 * \param base
1267 * The pointer to the hardware ADCMic block.
1268 *
1269 * \param intrMask
1270 * The mask of interrupts.
1271 * Select one or more values from \ref group_adcmic_macros_interrupt and "OR" them together.
1272 * - \ref CY_ADCMIC_INTR_DC
1273 * - \ref CY_ADCMIC_INTR_DATA
1274 *
1275 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_SET_INTERRUPT_MASK
1276 *
1277 *******************************************************************************/
Cy_ADCMic_SetInterruptMask(MXS40ADCMIC_Type * base,uint32_t intrMask)1278 __STATIC_INLINE void Cy_ADCMic_SetInterruptMask(MXS40ADCMIC_Type * base, uint32_t intrMask)
1279 {
1280     base->ADCMIC_INTR_MASK = intrMask & CY_ADCMIC_INTR;
1281 }
1282 
1283 
1284 /*******************************************************************************
1285 * Function Name: CY_ADCMIC_GetInterruptStatusMasked
1286 ****************************************************************************//**
1287 *
1288 * Returns the logical AND of the corresponding INTR and INTR_MASK fields
1289 * in a single-load operation.
1290 *
1291 * \param base
1292 * The pointer to the hardware ADCMic block.
1293 *
1294 * \return
1295 * The masked interrupt status.
1296 *
1297 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_CONFIG_INTR
1298 *
1299 *******************************************************************************/
Cy_ADCMic_GetInterruptStatusMasked(MXS40ADCMIC_Type const * base)1300 __STATIC_INLINE uint32_t Cy_ADCMic_GetInterruptStatusMasked(MXS40ADCMIC_Type const * base)
1301 {
1302     return (base->ADCMIC_INTR_MASKED & CY_ADCMIC_INTR);
1303 }
1304 /** \} group_adcmic_functions_interrupt */
1305 
1306 /** \addtogroup group_adcmic_functions_fifo
1307 * This set of functions are related to ADCMic FIFO buffer.
1308 * \{
1309 */
1310 
1311 /*******************************************************************************
1312 * Function Name: Cy_ADCMic_ReadFifoAll
1313 ****************************************************************************//**
1314 *
1315 * Reads whole the FIFO payload into the data array.
1316 *
1317 * \param base
1318 * The pointer to the hardware ADCMic block.
1319 *
1320 * \param data
1321 * The pointer to the uint16_t data array for the FIFO payload.
1322 *
1323 * \return
1324 * The 8-bit FIFO data count.
1325 *
1326 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_ISR
1327 *
1328 *******************************************************************************/
1329 uint8_t Cy_ADCMic_ReadFifoAll(MXS40ADCMIC_Type const * base, uint16_t * data);
1330 
1331 
1332 /*******************************************************************************
1333 * Function Name: Cy_ADCMic_ReadFifoStatus
1334 ****************************************************************************//**
1335 *
1336 * Returns the fifo status.
1337 *
1338 * \param base
1339 * The pointer to the hardware ADCMic block.
1340 *
1341 * \return
1342 * The 8-bit fifo status, see \ref group_adcmic_macros_fifo_status.
1343 *
1344 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_ADCMIC_ISR
1345 *
1346 *******************************************************************************/
Cy_ADCMic_GetFifoStatus(MXS40ADCMIC_Type const * base)1347 __STATIC_INLINE uint8_t Cy_ADCMic_GetFifoStatus(MXS40ADCMIC_Type const * base)
1348 {
1349     return ((uint8_t)_FLD2VAL(MXS40ADCMIC_ADCMIC_FIFO_CTRL_FIFO_STATUS, base->ADCMIC_FIFO_CTRL));
1350 }
1351 
1352 
1353 /*******************************************************************************
1354 * Function Name: Cy_ADCMic_ReadFifo
1355 ****************************************************************************//**
1356 *
1357 * Low-level API which returns the ADCMIC_DATA register value
1358 * that contains \b two subsequent FIFO data samples.
1359 *
1360 * \param base
1361 * The pointer to the hardware ADCMic block.
1362 *
1363 * \return
1364 * The combined 32-bit value of two 16-bit samples.
1365 * Use \ref group_adcmic_macros_fifo_samples to parse it.
1366 *
1367 * \funcusage \snippet adcmic/snippet/adcmic_snippet.c SNIPPET_READ_FIFO
1368 *
1369 *******************************************************************************/
Cy_ADCMic_ReadFifo(MXS40ADCMIC_Type const * base)1370 __STATIC_INLINE uint32_t Cy_ADCMic_ReadFifo(MXS40ADCMIC_Type const * base)
1371 {
1372     return (base->ADCMIC_DATA);
1373 }
1374 
1375 /** \} group_adcmic_functions_fifo */
1376 
1377 /** \} group_adcmic_functions */
1378 
1379 #if defined(__cplusplus)
1380 }
1381 #endif
1382 
1383 #endif /* CY_IP_MXS40ADCMIC */
1384 
1385 #endif  /* (CY_ADCMIC_H) */
1386 
1387 /** \} group_adcmic */
1388 
1389 /* [] END OF FILE */
1390