1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _FSL_HSADC_H_
10 #define _FSL_HSADC_H_
11 
12 #include "fsl_common.h"
13 
14 /*!
15  * @addtogroup hsadc
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 /*! @brief HSADC driver version. */
23 #define FSL_HSADC_DRIVER_VERSION (MAKE_VERSION(2, 0, 1)) /*!< Version 2.0.1 */
24 /*! @brief Converter index to mask for sample slot. */
25 #define HSADC_SAMPLE_MASK(index) (1U << (index))
26 
27 /*!
28  * @brief HSADC status flags.
29  */
30 enum _hsadc_status_flags
31 {
32     kHSADC_ZeroCrossingFlag               = (1U << 0U),  /*!< Zero crossing. */
33     kHSADC_HighLimitFlag                  = (1U << 1U),  /*!< High-limit. */
34     kHSADC_LowLimitFlag                   = (1U << 2U),  /*!< Low-limit. */
35     kHSADC_ConverterAEndOfScanFlag        = (1U << 3U),  /*!< End of Scan, converter A. */
36     kHSADC_ConverterBEndOfScanFlag        = (1U << 4U),  /*!< End of Scan, converter B. */
37     kHSADC_ConverterAEndOfCalibrationFlag = (1U << 5U),  /*!< End of Calibration, converter A. */
38     kHSADC_ConverterBEndOfCalibrationFlag = (1U << 6U),  /*!< End of Calibration, converter B. */
39     kHSADC_ConverterAConvertingFlag       = (1U << 7U),  /*!< Conversion in progress, converter A. */
40     kHSADC_ConverterBConvertingFlag       = (1U << 8U),  /*!< Conversion in progress, converter B. */
41     kHSADC_ConverterADummyConvertingFlag  = (1U << 9U),  /*!< Dummy conversion in progress, converter A. */
42     kHSADC_ConverterBDummyConvertingFlag  = (1U << 10U), /*!< Dummy conversion in progress, converter B. */
43     kHSADC_ConverterACalibratingFlag      = (1U << 11U), /*!< Calibration in progress, converter A. */
44     kHSADC_ConverterBCalibratingFlag      = (1U << 12U), /*!< Calibration in progress, converter B. */
45     kHSADC_ConverterAPowerDownFlag        = (1U << 13U), /*!< The converter is powered down, converter A. */
46     kHSADC_ConverterBPowerDownFlag        = (1U << 14U), /*!< The converter is powered down, converter B. */
47 };
48 
49 /*!
50  * @brief HSADC Interrupts.
51  */
52 enum _hsadc_interrupt_enable
53 {
54     kHSADC_ZeroCrossingInterruptEnable               = (1U << 0U), /*!< Zero crossing interrupt. */
55     kHSADC_HighLimitInterruptEnable                  = (1U << 1U), /*!< High-limit interrupt. */
56     kHSADC_LowLimitInterruptEnable                   = (1U << 2U), /*!< Low-limit interrupt. */
57     kHSADC_ConverterAEndOfScanInterruptEnable        = (1U << 3U), /*!< End of Scan interrupt, converter A. */
58     kHSADC_ConverterBEndOfScanInterruptEnable        = (1U << 4U), /*!< End of Scan interrupt, converter B.*/
59     kHSADC_ConverterAEndOfCalibrationInterruptEnable = (1U << 5U), /*!< End of Calibration, converter A. */
60     kHSADC_ConverterBEndOfCalibrationInterruptEnable = (1U << 6U), /*!< End of Calibration, converter B. */
61 };
62 
63 /*!
64  * @brief HSADC Converter identifier.
65  */
66 enum _hsadc_converter_id
67 {
68     kHSADC_ConverterA = (1U << 0U), /*!< Converter A. */
69     kHSADC_ConverterB = (1U << 1U), /*!< Converter B. */
70 };
71 
72 /*!
73  * @brief Defines the enumeration for dual converter scan mode.
74  */
75 typedef enum _hsadc_dual_converter_scan_mode
76 {
77     kHSADC_DualConverterWorkAsOnceSequential      = 0U, /*!< Once (single) sequential. */
78     kHSADC_DualConverterWorkAsOnceParallel        = 1U, /*!< Once parallel. */
79     kHSADC_DualConverterWorkAsLoopSequential      = 2U, /*!< Loop sequential. */
80     kHSADC_DualConverterWorkAsLoopParallel        = 3U, /*!< Loop parallel. */
81     kHSADC_DualConverterWorkAsTriggeredSequential = 4U, /*!< Triggered sequential. */
82     kHSADC_DualConverterWorkAsTriggeredParallel   = 5U, /*!< Triggered parallel. */
83 } hsadc_dual_converter_scan_mode_t;
84 
85 /*!
86  * @brief Defines the enumeration for the hsadc resolution.
87  */
88 typedef enum _hsadc_resolution
89 {
90     kHSADC_Resolution6Bit  = 0U, /*!< 6 bit resolution mode. */
91     kHSADC_Resolution8Bit  = 1U, /*!< 8 bit resolution mode. */
92     kHSADC_Resolution10Bit = 2U, /*!< 10 bit resolution mode. */
93     kHSADC_Resolution12Bit = 3U, /*!< 12 bit resolution mode. */
94 } hsadc_resolution_t;
95 
96 /*!
97  * @brief Defines the enumeration for the DMA trigger source.
98  */
99 typedef enum _hsadc_dma_trigger_source
100 {
101     kHSADC_DMATriggerSourceAsEndOfScan   = 0U, /*!< DMA trigger source is end of scan interrupt. */
102     kHSADC_DMATriggerSourceAsSampleReady = 1U, /*!< DMA trigger source is RDY bits. */
103 } hsadc_dma_trigger_source_t;
104 
105 /*!
106  * @brief Defines the enumeration for the sample slot's zero crossing event.
107  */
108 typedef enum _hsadc_zero_crossing_mode
109 {
110     kHSADC_ZeroCorssingDisabled          = 0U, /*!< Zero Crossing disabled. */
111     kHSADC_ZeroCorssingForPtoNSign       = 1U, /*!< Zero Crossing enabled for positive to negative sign change. */
112     kHSADC_ZeroCorssingForNtoPSign       = 2U, /*!< Zero Crossing enabled for negative to positive sign change. */
113     kHSADC_ZeroCorssingForAnySignChanged = 3U, /*!< Zero Crossing enabled for any sign change. */
114 } hsadc_zero_crossing_mode_t;
115 
116 /*!
117  * @brief Defines the enumeration for the converter's work mode in idle mode.
118  */
119 typedef enum _hsadc_idle_work_mode
120 {
121     kHSADC_IdleKeepNormal    = 0U, /*!< Keep normal. */
122     kHSADC_IdleAutoStandby   = 1U, /*!< Fall into standby mode automatically. */
123     kHSADC_IdleAutoPowerDown = 2U, /*!< Fall into power down mode automatically. */
124 } hsadc_idle_work_mode_t;
125 
126 /*!
127  * @brief Converter's calibration mode.
128  */
129 enum _hsadc_calibration_mode
130 {
131     kHSADC_CalibrationModeDifferential = (1U << 0U), /*!< Calibration request for differential mode. */
132     kHSADC_CalibrationModeSingleEnded  = (1U << 1U), /*!< Calibration request for single ended mode. */
133 };
134 
135 /*! @brief Bit mask of calibration value for converter A in single ended mode. */
136 #define HSADC_CALIBRATION_VALUE_A_SINGLE_ENDED_MASK HSADC_CALVAL_A_CALVSING_MASK
137 /*! @brief Bit shift of calibration value for converter A in single ended mode. */
138 #define HSADC_CALIBRATION_VALUE_A_SINGLE_ENDED_SHIFT HSADC_CALVAL_A_CALVSING_SHIFT
139 /*! @brief Bit mask of calibration value for converter A in differential mode. */
140 #define HSADC_CALIBRATION_VALUE_A_DIFFERENTIAL_MASK HSADC_CALVAL_A_CALVDIF_MASK
141 /*! @brief Bit shift of calibration value for converter A in differential mode. */
142 #define HSADC_CALIBRATION_VALUE_A_DIFFERENTIAL_SHIFT HSADC_CALVAL_A_CALVDIF_SHIFT
143 /*! @brief Bit mask of calibration value for converter B in single ended mode. */
144 #define HSADC_CALIBRATION_VALUE_B_SINGLE_ENDED_MASK (HSADC_CALVAL_B_CALVSING_MASK << 16U)
145 /*! @brief Bit shift of calibration value for converter B in single ended mode. */
146 #define HSADC_CALIBRATION_VALUE_B_SINGLE_ENDED_SHIFT (HSADC_CALVAL_B_CALVSING_SHIFT + 16U)
147 /*! @brief Bit mask of calibration value for converter B in differential mode. */
148 #define HSADC_CALIBRATION_VALUE_B_DIFFERENTIAL_MASK (HSADC_CALVAL_B_CALVDIF_MASK << 16U)
149 /*! @brief Bit shift of calibration value for converter B in differential mode. */
150 #define HSADC_CALIBRATION_VALUE_B_DIFFERENTIAL_SHIFT (HSADC_CALVAL_B_CALVDIF_SHIFT + 16U)
151 
152 /*!
153  * @brief Defines the structure for configuring the HSADC's common setting.
154  */
155 typedef struct _hsadc_config
156 {
157     hsadc_dual_converter_scan_mode_t dualConverterScanMode; /*!< Dual converter's scan mode. */
158     bool enableSimultaneousMode;                            /*!< Using Simultaneous mode. */
159     hsadc_resolution_t resolution;                          /*!< Resolution mode. */
160     hsadc_dma_trigger_source_t DMATriggerSoruce;            /*!< DMA trigger source. */
161     hsadc_idle_work_mode_t idleWorkMode;                    /*!< Converter's work mode when idle. */
162     uint16_t powerUpDelayCount; /*!< Delay count united as 32 clocks to wait for the clock to be stable. Available
163                                 range is 0-63. */
164 } hsadc_config_t;
165 
166 /*!
167  * @brief Defines the structure for configuring each converter.
168  */
169 typedef struct _hsadc_converter_config
170 {
171     uint16_t clockDivisor;      /*!< Converter's clock divisor for the clock source. Available range is 2-64. */
172     uint16_t samplingTimeCount; /*!< Sampling time count. The resultant sampling time is (1.5 + samplingTimeCount) x
173                                 clock period. Available range is 0-255. */
174     uint16_t powerUpCalibrationModeMask; /*!< Calibration mode mask in the power up period. See the
175                                      "_hsadc_calibration_mode". If this field isn't zero, call the function
176                                      HSADC_GetStatusFlags() to check whether the End of Calibration flag is set to wait
177                                      for sthe calibration process to complete. If this is zero, it indicates no
178                                      calibration is executed in power up period. */
179 } hsadc_converter_config_t;
180 
181 /*!
182  * @brief Defines the structure for configuring the sample slot.
183  *
184  * channelNumber, channel67MuxNumber, and enableDifferentialPair have following relationship:
185  * channelNumber equals 0~7 represents channel 0~7 of converter A. channelNumber equals 8~15 represents channel 0~7 of
186  * converter B.
187  * 1) When channelNumber = 6 and enableDifferentialPair = false, channel67MuxNumber represents converter A's channel
188  *    6's sub multiplex channel number.
189  * 2) When channelNumber = 6 and enableDifferentialPair = true, channel67MuxNumber represents converter A's channel
190  *    6 and channel 7's sub multiplex channel number.
191  * 3) When channelNumber = 7 and enableDifferentialPair = false, channel67MuxNumber represents converter A's channel
192  *    7's sub multiplex channel number.
193  * 4) When channelNumber = 7 and enableDifferentialPair = true, channel67MuxNumber represents converter A's channel
194  *    6 and channel 7's sub multiplex channel number.
195  * 5) When channelNumber = 14 and enableDifferentialPair = false, channel67MuxNumber represents converter B's channel
196  *    6's sub multiplex channel number.
197  * 6) When channelNumber = 14 and enableDifferentialPair = true, channel67MuxNumber represents converter B's channel
198  *    6 and channel 7's sub multiplex channel number.
199  * 7) When channelNumber = 15 and enableDifferentialPair = false, channel67MuxNumber represents converter B's channel
200  *    7's sub multiplex channel number.
201  * 8) When channelNumber = 15 and enableDifferentialPair = true, channel67MuxNumber represents converter B's channel
202  *    6 and channel 7's sub multiplex channel number.
203  * 9) In other cases, channel67MuxNumber won't be functional.
204  */
205 typedef struct _hsadc_sample_config
206 {
207     /* Conversion channel setting. */
208     uint16_t channelNumber;      /*!< Channel number. Available range is 0-15.  */
209     uint16_t channel67MuxNumber; /*!< Channel 6/7's sub multiplex channel number. When channelNumber = 6 or 14, its
210                                  available range is 0~6. When channelNumber = 7 or 15, its available range is 0~5. */
211     bool enableDifferentialPair; /*!< Use differential sample input or not. In differential mode, the sub multiplex
212                                  channel number of channel 6 and channel 7 must be configured to be same. */
213 
214     /* sample result setting. */
215     hsadc_zero_crossing_mode_t zeroCrossingMode; /*!< Zero crossing mode. */
216     uint16_t highLimitValue; /*!< High-limit value. Original value format as hardware register, with 3-bits left
217                              shifted. */
218     uint16_t lowLimitValue;  /*!< Low-limit value. Original value format as hardware register, with 3-bits left
219                              shifted. */
220     uint16_t offsetValue;    /*!< Offset value. Original value format as hardware register, with 3-bits left shifted. */
221     bool enableWaitSync;     /*!< Wait for sync input to launch this sample's conversion or not. */
222 } hsadc_sample_config_t;
223 
224 /*******************************************************************************
225  * API
226  ******************************************************************************/
227 #if defined(__cplusplus)
228 extern "C" {
229 #endif
230 
231 /*!
232  * @name HSADC Initialization and deinitialization.
233  * @{
234  */
235 
236 /*!
237  * @brief Initializes the HSADC module.
238  *
239  * This function initializes the HSADC module.
240  * The operations are:
241  *  - Enable the clock for HSADC.
242  *  - Set the global settings for HSADC converter.
243  *
244  * @param base   HSADC peripheral base address.
245  * @param config Pointer to configuration structure. See the "hsadc_config_t".
246  */
247 void HSADC_Init(HSADC_Type *base, const hsadc_config_t *config);
248 
249 /*!
250  * @brief Gets an available pre-defined settings for module's configuration.
251  *
252  * This function initializes the module's configuration structure with an available settings.
253  * The default value are:
254  * @code
255  *   config->dualConverterScanMode = kHSADC_DualConverterWorkAsTriggeredParallel;
256  *   config->enableSimultaneousMode = true;
257  *   config->resolution = kHSADC_Resolution12Bit;
258  *   config->DMATriggerSoruce = kHSADC_DMATriggerSourceAsEndOfScan;
259  *   config->idleWorkMode = kHSADC_IdleKeepNormal;
260  *   config->powerUpDelay = 18U;
261  * @endcode
262  * @param config Pointer to configuration structure. See the "hsadc_config_t"
263  */
264 void HSADC_GetDefaultConfig(hsadc_config_t *config);
265 
266 /*!
267  * @brief De-initializes the HSADC module.
268  *
269  * This function de-initializes the HSADC module.
270  * The operations are:
271  *  - Power down both converters.
272  *  - Disable the clock for HSADC.
273  *
274  * @param base HSADC peripheral base address.
275  */
276 void HSADC_Deinit(HSADC_Type *base);
277 
278 /* @} */
279 
280 /*!
281  * @name Converter.
282  * @{
283  */
284 
285 /*!
286  * @brief Configures the converter.
287  *
288  * @param base          HSADC peripheral base address.
289  * @param converterMask Mask for converters to be configured. See the "_hsadc_converter_id".
290  * @param config        Pointer to configuration structure. See the "hsadc_converter_config_t".
291  */
292 void HSADC_SetConverterConfig(HSADC_Type *base, uint16_t converterMask, const hsadc_converter_config_t *config);
293 
294 /*!
295  * @brief Gets an available pre-defined settings for each converter's configuration.
296  *
297  * This function initializes each converter's configuration structure with available settings.
298  * The default value are:
299  * @code
300  *   config->clockDivisor = 4U;
301  *   config->samplingTimeCount = 0U;
302  *   config->enablePowerUpCalibration = false;
303  *   config->powerUpCalibrationModeMask = kHSADC_CalibrationModeSingleEnded;
304  * @endcode
305  * @param config Pointer to configuration structure. See the "hsadc_converter_config_t"
306  */
307 void HSADC_GetDefaultConverterConfig(hsadc_converter_config_t *config);
308 
309 /*!
310  * @brief Enables the converter's conversion.
311  *
312  * This function enables the converter's conversion by making the converter exit stop mode. The conversion should
313  * only be launched after the converter is enabled. When this feature is asserted to be "false", the current scan is
314  * stopped and no further scans can start. All the software and hardware triggers are ignored.
315  *
316  * @param base          HSADC peripheral base address.
317  * @param converterMask Mask for converters to be operated. See the "_hsadc_converter_id".
318  * @param enable         Enable or disable the feature.
319  */
320 void HSADC_EnableConverter(HSADC_Type *base, uint16_t converterMask, bool enable);
321 
322 /*!
323  * @brief Enables the input of an external sync signal.
324  *
325  * This function enables the input of the external sync signal. The external sync signal could be used to trigger the
326  * conversion if the hardware trigger-related setting is used.
327  * Note: When in "Once" scan mode, this gate is off automatically after an available sync is received.
328  * Enable the input again manually if another sync signal is needed.
329  *
330  * @param base          HSADC peripheral base address.
331  * @param converterMask Mask for converters to be operated. See the "_hsadc_converter_id".
332  * @param enable        Enable or disable the feature.
333  */
334 void HSADC_EnableConverterSyncInput(HSADC_Type *base, uint16_t converterMask, bool enable);
335 
336 /*!
337  * @brief Enables power for the converter.
338  *
339  * This function enables the power for the converter. The converter should be powered on before conversion. Once
340  * this API is called, the converter is powered on after a few moments (so-called power up delay) to make the
341  * power stable.
342  *
343  * @param base          HSADC peripheral base address.
344  * @param converterMask Mask for converters to be operated. See the "_hsadc_converter_id".
345  * @param enable        Enable or disable the feature.
346  */
347 void HSADC_EnableConverterPower(HSADC_Type *base, uint16_t converterMask, bool enable);
348 
349 /*!
350  * @brief Triggers the converter by using the software trigger.
351  *
352  * This function triggers  the converter using a software trigger. The software trigger can be used to start a
353  * conversion sequence.
354  *
355  * @param base          HSADC peripheral base address.
356  * @param converterMask Mask for converters to be operated. See the "_hsadc_converter_id".
357  */
358 void HSADC_DoSoftwareTriggerConverter(HSADC_Type *base, uint16_t converterMask);
359 
360 /*!
361  * @brief Enables the DMA feature.
362  *
363  * @param base          HSADC peripheral base address.
364  * @param converterMask Mask for converters to be operated. See the "_hsadc_converter_id".
365  * @param enable        Enable or disable the feature.
366  */
367 void HSADC_EnableConverterDMA(HSADC_Type *base, uint16_t converterMask, bool enable);
368 
369 /*!
370  * @brief Enables the interrupts.
371  *
372  * @param base HSADC peripheral base address.
373  * @param mask Mask value for interrupt events. See the "_hsadc_interrupt_enable".
374  */
375 void HSADC_EnableInterrupts(HSADC_Type *base, uint16_t mask);
376 
377 /*!
378  * @brief Disables the interrupts.
379  *
380  * @param base HSADC peripheral base address.
381  * @param mask Mask value for interrupt events. See the "_hsadc_interrupt_enable".
382  */
383 void HSADC_DisableInterrupts(HSADC_Type *base, uint16_t mask);
384 
385 /*!
386  * @brief  Gets the status flags.
387  *
388  * @param  base HSADC peripheral base address.
389  *
390  * @return      Mask value for the event flags. See the "_hsadc_status_flags".
391  */
392 uint16_t HSADC_GetStatusFlags(HSADC_Type *base);
393 
394 /*!
395  * @brief Clears the status flags.
396  *
397  * @param base  HSADC peripheral base address.
398  * @param mask Mask value for the event flags to be cleared. See the "_hsadc_status_flags".
399  */
400 void HSADC_ClearStatusFlags(HSADC_Type *base, uint16_t mask);
401 
402 /* @} */
403 
404 /*!
405  * @name Sample.
406  * @{
407  */
408 
409 /*!
410  * @brief Configures the sample slot.
411  *
412  * A sample list in this module works like a conversion sequence. Each sample slot can be used to designate to sample
413  * which channel is in converter A and converter B. The detail mapping relationship between sample slot and converter's
414  * channel can be found in the SoC reference manual.
415  *
416  * @param base        HSADC peripheral base address.
417  * @param sampleIndex Index of sample slot in conversion sequence. Available range is 0-15.
418  * @param config      Pointer to configuration structure. See the "hsadc_sample_config_t".
419  */
420 void HSADC_SetSampleConfig(HSADC_Type *base, uint16_t sampleIndex, const hsadc_sample_config_t *config);
421 
422 /*!
423  * @brief Gets the default sample configuration.
424  *
425  * This function initializes each sample's configuration structure with an available settings.
426  * The default values are:
427  * @code
428  *   config->channelNumber = 0U;
429  *   config->channel6MuxNumber = 0U;
430  *   config->channel7MuxNumber = 0U;
431  *   config->enableDifferentialPair = false;
432  *   config->zeroCrossingMode = kHSADC_ZeroCorssingDisabled;
433  *   config->highLimitValue = 0x7FF8U;
434  *   config->lowLimitValue = 0U;
435  *   config->offsetValue = 0U;
436  *   config->enableWaitSync = false;
437  * @endcode
438  * @param config Pointer to configuration structure. See the "hsadc_sample_config_t".
439  */
440 void HSADC_GetDefaultSampleConfig(hsadc_sample_config_t *config);
441 
442 /*!
443  * @brief Enables the sample slot.
444  *
445  * This function enables the sample slot. Only the enabled sample slot can join the conversion sequence.
446  *
447  * @param base       HSADC peripheral base address.
448  * @param sampleMask Mask value of sample slots in conversion sequence. Each bit corresponds to a sample slot.
449  * @param enable     Enable or disable the feature.
450  */
HSADC_EnableSample(HSADC_Type * base,uint16_t sampleMask,bool enable)451 static inline void HSADC_EnableSample(HSADC_Type *base, uint16_t sampleMask, bool enable)
452 {
453     if (enable)
454     {
455         base->SDIS &= ~HSADC_SDIS_DS(sampleMask);
456     }
457     else
458     {
459         base->SDIS |= HSADC_SDIS_DS(sampleMask);
460     }
461 }
462 
463 /*!
464  * @brief Enables the interrupt for each sample slot when its result is ready.
465  *
466  * @param base       HSADC peripheral base address.
467  * @param sampleMask Mask value of sample slots in conversion sequence. Each bit is corresponding to a sample slot.
468  * @param enable     Enable or disable the feature.
469  */
HSADC_EnableSampleResultReadyInterrupts(HSADC_Type * base,uint16_t sampleMask,bool enable)470 static inline void HSADC_EnableSampleResultReadyInterrupts(HSADC_Type *base, uint16_t sampleMask, bool enable)
471 {
472     if (enable)
473     {
474         base->SCINTEN |= HSADC_SCINTEN_SCINTEN(sampleMask);
475     }
476     else
477     {
478         base->SCINTEN &= ~HSADC_SCINTEN_SCINTEN(sampleMask);
479     }
480 }
481 
482 /*!
483  * @brief  Returns the sample ready flags of sample slots.
484  *
485  * @param  base HSADC peripheral base address.
486  *
487  * @return      Mask value for the sample slots if their result are ready.
488  */
HSADC_GetSampleReadyStatusFlags(HSADC_Type * base)489 static inline uint16_t HSADC_GetSampleReadyStatusFlags(HSADC_Type *base)
490 {
491     return (HSADC_RDY_RDY_MASK & base->RDY);
492 }
493 
494 /*!
495  * @brief  Gets the low-limit flags of sample slots.
496  *
497  * @param  base HSADC peripheral base address.
498  *
499  * @return      Mask value for the sample slots if their results exceed the low limit.
500  */
HSADC_GetSampleLowLimitStatusFlags(HSADC_Type * base)501 static inline uint16_t HSADC_GetSampleLowLimitStatusFlags(HSADC_Type *base)
502 {
503     return (HSADC_LOLIMSTAT_LLS_MASK & base->LOLIMSTAT);
504 }
505 
506 /*!
507  * @brief Clears low-limit flags of sample slots.
508  *
509  * @param base       HSADC peripheral base address.
510  * @param sampleMask Mask value for the sample slots' flags to be cleared.
511  */
HSADC_ClearSampleLowLimitStatusFlags(HSADC_Type * base,uint16_t sampleMask)512 static inline void HSADC_ClearSampleLowLimitStatusFlags(HSADC_Type *base, uint16_t sampleMask)
513 {
514     base->LOLIMSTAT = HSADC_LOLIMSTAT_LLS(sampleMask);
515 }
516 
517 /*!
518  * @brief  Gets the high-limit flags of sample slots.
519  *
520  * @param  base HSADC peripheral base address.
521  *
522  * @return      Mask value for the sample slots if their results exceed the high limit.
523  */
HSADC_GetSampleHighLimitStatusFlags(HSADC_Type * base)524 static inline uint16_t HSADC_GetSampleHighLimitStatusFlags(HSADC_Type *base)
525 {
526     return (HSADC_HILIMSTAT_HLS_MASK & base->HILIMSTAT);
527 }
528 
529 /*!
530  * @brief Clears high-limit flags of sample slots.
531  *
532  * @param base       HSADC peripheral base address.
533  * @param sampleMask Mask value for the sample slots to be cleared flags.
534  */
HSADC_ClearSampleHighLimitStatusFlags(HSADC_Type * base,uint16_t sampleMask)535 static inline void HSADC_ClearSampleHighLimitStatusFlags(HSADC_Type *base, uint16_t sampleMask)
536 {
537     base->HILIMSTAT = HSADC_HILIMSTAT_HLS(sampleMask);
538 }
539 /*!
540  * @brief  Gets the zero crossing flags of sample slots.
541  *
542  * @param  base HSADC peripheral base address.
543  *
544  * @return      Mask value for the sample slots if their results cause the zero crossing event.
545  */
HSADC_GetSampleZeroCrossingStatusFlags(HSADC_Type * base)546 static inline uint16_t HSADC_GetSampleZeroCrossingStatusFlags(HSADC_Type *base)
547 {
548     return (HSADC_ZXSTAT_ZCS_MASK & base->ZXSTAT);
549 }
550 
551 /*!
552  * @brief Clears zero crossing flags of sample slots.
553  *
554  * @param base       HSADC peripheral base address.
555  * @param sampleMask Mask value for the sample slots to be cleared flags.
556  */
HSADC_ClearSampleZeroCrossingStatusFlags(HSADC_Type * base,uint16_t sampleMask)557 static inline void HSADC_ClearSampleZeroCrossingStatusFlags(HSADC_Type *base, uint16_t sampleMask)
558 {
559     base->ZXSTAT = HSADC_ZXSTAT_ZCS(sampleMask);
560 }
561 
562 /*!
563  * @brief  Gets the sample result value.
564  *
565  * This function gets the sample result value. This returned value keeps its original formation just like in the
566  * hardware result register. It includes the sign bit as the MSB and 3-bit left shifted value.
567  *
568  * @param  base        HSADC peripheral base address.
569  * @param  sampleIndex Index of sample slot.
570  *
571  * @return             Sample's conversion value.
572  */
HSADC_GetSampleResultValue(HSADC_Type * base,uint16_t sampleIndex)573 static inline uint16_t HSADC_GetSampleResultValue(HSADC_Type *base, uint16_t sampleIndex)
574 {
575     return base->RSLT[sampleIndex];
576 }
577 
578 /* @} */
579 
580 /*!
581  * @name Calibration.
582  * @{
583  */
584 
585 /*!
586  * @brief Starts the hardware calibration.
587  *
588  * This function starts the single ended calibration and differential calibration for converter A and converter B
589  * at the same time.
590  * Note that this is a non blocking function. End of Scan flag and End of Calibration flag are both be set after the
591  * calibration process. As a result, the user should check these two flags by using the function HSADC_GetStatusFlags()
592  * to wait for the calibration process to complete.
593  *
594  * @param base                HSADC peripheral base address.
595  * @param converterMask       Mask for converters to be operated. See the "_hsadc_converter_id".
596  * @param calibrationModeMask Mask for calibration mode to be operated. See the "_hsadc_calibration_mode". Shouldn't be
597  * zero.
598  */
599 void HSADC_DoAutoCalibration(HSADC_Type *base, uint16_t converterMask, uint16_t calibrationModeMask);
600 
601 /*!
602  * @brief Gets the calibration result value.
603  *
604  * This function returns the single ended calibration value and differential calibration value for converter A and
605  * converter B. The calibration value of each calibration mode for each converter can be received from this function's
606  * return value by using the mask and shift definition from HSADC_CALIBRATION_VALUE_A_SINGLE_ENDED_MASK to
607  * HSADC_CALIBRATION_VALUE_B_DIFFERENTIAL_SHIFT.
608  *
609  * @param base                HSADC peripheral base address.
610  * @return                    Calibration value for converter A and converter B.
611  */
612 uint32_t HSADC_GetCalibrationResultValue(HSADC_Type *base);
613 
614 /*!
615  * @brief Enables or disables the calibration result value.
616  *
617  * This function enables or disables converter A and converter B to use the calibration values to obtain the final
618  * conversion result by calibration sum operation.
619  *
620  * @param base          HSADC peripheral base address.
621  * @param converterMask Mask for converters to be operated. See the "_hsadc_converter_id".
622  * @param enable        Enable or disable the feature.
623  */
624 void HSADC_EnableCalibrationResultValue(HSADC_Type *base, uint16_t converterMask, bool enable);
625 
626 /* @} */
627 #if defined(__cplusplus)
628 }
629 #endif
630 
631 /*! @}*/
632 
633 #endif /* _FSL_HSADC_H_ */
634