|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without modification, |
| 6 | + * are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * o Redistributions of source code must retain the above copyright notice, this list |
| 9 | + * of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * o Redistributions in binary form must reproduce the above copyright notice, this |
| 12 | + * list of conditions and the following disclaimer in the documentation and/or |
| 13 | + * other materials provided with the distribution. |
| 14 | + * |
| 15 | + * o Neither the name of Freescale Semiconductor, Inc. nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from this |
| 17 | + * software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +#ifndef __FSL_ADC_DRIVER_H__ |
| 32 | +#define __FSL_ADC_DRIVER_H__ |
| 33 | + |
| 34 | +#include "stdint.h" |
| 35 | +#include "stdbool.h" |
| 36 | +#include "string.h" |
| 37 | +#include "fsl_port_hal.h" |
| 38 | +#include "fsl_adc_hal.h" |
| 39 | + |
| 40 | +/*! |
| 41 | + * @addtogroup adc_driver |
| 42 | + * @{ |
| 43 | + */ |
| 44 | + |
| 45 | +/*! @file*/ |
| 46 | + |
| 47 | +/******************************************************************************* |
| 48 | + * Definitions |
| 49 | + ******************************************************************************/ |
| 50 | + |
| 51 | +/*! |
| 52 | + * @brief Define the parameter structure for calibration |
| 53 | + * |
| 54 | + * This structure is used to keep the calibration parameter after executing the |
| 55 | + * auto-calibration or filled by indicated ones. |
| 56 | + */ |
| 57 | +typedef struct adcCalibrationParam |
| 58 | +{ |
| 59 | + uint32_t PG; /*!< The value for PG register */ |
| 60 | + uint32_t MG; /*!< The value for MG register */ |
| 61 | +} adc_calibration_param_t; |
| 62 | + |
| 63 | +/*! |
| 64 | + * @brief Define ADC's extend configuration structure |
| 65 | + * |
| 66 | + * This structure is used when initializing the ADC device associated with |
| 67 | + * adc_init_extend(). It contains the advanced feature configuration when |
| 68 | + * necessary. |
| 69 | + */ |
| 70 | +typedef struct adcExtendConfig |
| 71 | +{ |
| 72 | + bool isLowPowerEnabled; /*!< Switcher to enable the low power mode*/ |
| 73 | + bool isLongSampleEnabled; /*!< Switcher to enable the long sample mode*/ |
| 74 | + adc_long_sample_mode_t hwLongSampleMode; /*!< Selection of long sample mode*/ |
| 75 | + bool isHighSpeedEnabled; /*!< Switcher to enable high speed sample mode*/ |
| 76 | + bool isAsynClockEnabled; /*!< Switcher to enable internal asynchonous clock at initialization*/ |
| 77 | + bool isHwTriggerEnabled; /*!< Switcher to enable hardware trigger*/ |
| 78 | + bool isHwCompareEnabled; /*!< Switcher to enable hardware compare*/ |
| 79 | + bool isHwCompareGreaterEnabled; /*!< Switcher to enable greater compare*/ |
| 80 | + bool isHwCompareRangeEnabled; /*!< Switcher to enable range compare*/ |
| 81 | + uint32_t hwCompareValue1; /*!< Low limit in hardware compare*/ |
| 82 | + uint32_t hwCompareValue2; /*!< High limit in hardware compare*/ |
| 83 | + bool isHwAverageEnabled; /*!< Switcher to enable hareware average*/ |
| 84 | + adc_hw_average_mode_t hwAverageSampleMode; /*!< Selection of hardware average time*/ |
| 85 | + bool isDmaEnabled; /*! < Switcher to enable DMA support*/ |
| 86 | +} adc_extend_config_t; |
| 87 | + |
| 88 | +/*! |
| 89 | + * @brief Define the channel's configuration structure |
| 90 | + * |
| 91 | + * This structure is used when setting the convertion channel associated with |
| 92 | + * adc_start_conversion(), adc_stop_conversion(), adc_is_conversion_completed() |
| 93 | + * and adc_get_conversion_value(). It contains all the information that can |
| 94 | + * identify an ADC channel. |
| 95 | + */ |
| 96 | +typedef struct adcChannelConfig |
| 97 | +{ |
| 98 | + /* |
| 99 | + * Corresponding to the ADCH bits of a 5-bit field , chennelId here selects |
| 100 | + * one of the input channels. The ADC will be turned off when the channel |
| 101 | + * select bits are all set as ADCH = 11111(kAdcChannelDisable). See to the |
| 102 | + * type definition of adc_channel_mode_t. |
| 103 | + */ |
| 104 | + adc_channel_mode_t channelId; /*!< Channel number*/ |
| 105 | + bool isDifferentialEnabled; /*!< The switcher to enable differential channel*/ |
| 106 | + bool isInterruptEnabled; /*!< The switcher to enable interrupt when conversion is completed*/ |
| 107 | + adc_group_mux_mode_t muxSelect; /*!< Selection mux to group A(0) or group B(1)*/ |
| 108 | +} adc_channel_config_t; |
| 109 | + |
| 110 | +/*! |
| 111 | + * @brief Define ADC's basic configuration structure |
| 112 | + * |
| 113 | + * This structure is used when initializing the ADC device associated with |
| 114 | + * adc_init(). It contains the basic feature configuration which are |
| 115 | + * necessary. |
| 116 | + */ |
| 117 | +typedef struct adcConfig |
| 118 | +{ |
| 119 | + adc_clock_source_mode_t clockSourceMode; /*!< the selection of ADC clock source, see to adc_clock_source_mode_t's type definition */ |
| 120 | + adc_clock_divider_mode_t clockSourceDividerMode; /*!< the selection of ADC clock divider, see to adc_clock_divider_mode_t's type defnition */ |
| 121 | + adc_resolution_mode_t resolutionMode; /*!< the selection of ADC resolution, see to adc_resolution_mode_t's type definition */ |
| 122 | + adc_reference_voltage_mode_t referenceVoltageMode; /*!< the selection of ref voltage source, see to adc_reference_voltage_mode_t's type definition*/ |
| 123 | + bool isContinuousEnabled; /*!< switcher to enable continuous conversion*/ |
| 124 | + adc_calibration_param_t *calibrationParam; /*!< NULL to enable auto-calibration or indicate mannually when initializing */ |
| 125 | +} adc_config_t; |
| 126 | + |
| 127 | +#if defined(__cplusplus) |
| 128 | +extern "C" { |
| 129 | +#endif |
| 130 | + |
| 131 | +/*! |
| 132 | + * @brief Define ADC ISR callback function. |
| 133 | + * |
| 134 | + * This type define the phototype of ADC ISR'callback function that can be |
| 135 | + * registered inside the ISR. |
| 136 | + */ |
| 137 | +typedef void (*adc_isr_callback_t)(void); |
| 138 | + |
| 139 | +/*! |
| 140 | + * @brief Get the parameters for calibration. |
| 141 | + * |
| 142 | + * This function is to get the calibration parameters in auto-calibrate mode. |
| 143 | + * It is recommended to execute this fucntion to obtain the parameter for |
| 144 | + * calibration during the initialzation, even though it will take a few time. |
| 145 | + * |
| 146 | + * @param instance ADC instance id. |
| 147 | + * @param paramPtr The pointer to a empty calibration parameter structure. |
| 148 | + * @return The execution status. |
| 149 | + */ |
| 150 | +adc_status_t adc_get_calibration_param(uint32_t instance, adc_calibration_param_t *paramPtr); |
| 151 | + |
| 152 | +/*! |
| 153 | + * @brief Set the parameters for calibration. |
| 154 | + * |
| 155 | + * This function is to set the calibration parameters when necessary. The |
| 156 | + * parameters could be generated from the auto-calibration by |
| 157 | + * adc_get_calibration_param() or created by mannually indicated ones. |
| 158 | + * |
| 159 | + * @param instance ADC instance id. |
| 160 | + * @param paramPtr The pointer to a filled calibration parameter structure. |
| 161 | + * @return The execution status. |
| 162 | + */ |
| 163 | +adc_status_t adc_set_calibration_param(uint32_t instance, adc_calibration_param_t *paramPtr); |
| 164 | +/*! |
| 165 | + * @brief Initialize the ADC with the basic configuration. |
| 166 | + * |
| 167 | + * This function is the necessary one that can make sure ADC can work at least |
| 168 | + * in the basic way. If application do not need more complex features, just call |
| 169 | + * this fucntion will be simple. |
| 170 | + * |
| 171 | + * @param instance ADC instance id. |
| 172 | + * @param cfgPtr The pointer to basic configuration structure. |
| 173 | + * @return The execution status. |
| 174 | + */ |
| 175 | +adc_status_t adc_init(uint32_t instance, adc_config_t *cfgPtr); |
| 176 | + |
| 177 | +/*! |
| 178 | + * @brief Initialize the ADC with the extend configuration when necessory. |
| 179 | + * |
| 180 | + * This function is an optional one that can provide advanced features according |
| 181 | + * to application when more complex configurations are needed. They are: |
| 182 | + * low power mode, long sample mode, high speed mode, asynchonous's work mode, |
| 183 | + * hardware trigger, hardware compare and hardwrare average. |
| 184 | + * |
| 185 | + * @param instance ADC instance id. |
| 186 | + * @param extendCfgPtr The pointer to extend configuration structure. |
| 187 | + * @return The execution status. |
| 188 | + */ |
| 189 | +adc_status_t adc_init_extend(uint32_t instance, adc_extend_config_t *extendCfgPtr); |
| 190 | + |
| 191 | +/*! |
| 192 | + * @brief Shutdown the ADC. |
| 193 | + * |
| 194 | + * Shutdown the ADC will cut off the clock to the indicated ADC device. |
| 195 | + * |
| 196 | + * @param instance ADC instance id. |
| 197 | + */ |
| 198 | +void adc_shutdown(uint32_t instance); |
| 199 | + |
| 200 | +/*! |
| 201 | + * @brief Start the conversion from indicated channel. |
| 202 | + * |
| 203 | + * Trigger the indicated channel's conversion. In single conversion mode, this |
| 204 | + * function should be called every time when triggering the conversion. In |
| 205 | + * continuous conversion mode, this function can be called only once at the |
| 206 | + * beginning of conversion, and then the ADC will execute conversion periodically |
| 207 | + * and automatically. |
| 208 | + * |
| 209 | + * @param instance ADC instance id. |
| 210 | + * @param channelCfgPtr The pointer to channel configuration structure. |
| 211 | + * @return The execution status. |
| 212 | + */ |
| 213 | +adc_status_t adc_start_conversion(uint32_t instance, adc_channel_config_t *channelCfgPtr); |
| 214 | + |
| 215 | +/*! |
| 216 | + * @brief Stop the conversion. |
| 217 | + * |
| 218 | + * Stop the ADC's conversion. In fact, this function will set ADC to a "NULL" |
| 219 | + * channel. And then, the ADC will not convert from any channel. It is different |
| 220 | + * from adc_shutdown() while ADC is still alive. |
| 221 | + * |
| 222 | + * @param instance ADC instance id. |
| 223 | + * @param channelCfgPtr The pointer to channel configuration structure. |
| 224 | + * @return The execution status. |
| 225 | + */ |
| 226 | +adc_status_t adc_stop_conversion(uint32_t instance, adc_channel_config_t *channelCfgPtr); |
| 227 | + |
| 228 | +/*! |
| 229 | + * @brief Check whether the conversion is completed. |
| 230 | + * |
| 231 | + * Check whether the current conversion is completed. As there are multiple |
| 232 | + * channels sharing the same conventer, the status is used to indicated the |
| 233 | + * converter's situation. |
| 234 | + * |
| 235 | + * @param instance ADC instance id. |
| 236 | + * @param channelCfgPtr The pointer to channel configuration structure. |
| 237 | + * @return True if the event is asserted. |
| 238 | + */ |
| 239 | +bool adc_is_conversion_completed(uint32_t instance, adc_channel_config_t *channelCfgPtr); |
| 240 | + |
| 241 | +/*! |
| 242 | + * @brief Get the value after the conversion. |
| 243 | + * |
| 244 | + * The value just comes from value register that may be eventually processed |
| 245 | + * according to application. when use polling mode, the value will be obtain |
| 246 | + * after the converions is completed. when use interrupt mode, the value will |
| 247 | + * come from the buffer that is updated by ADC ISR. |
| 248 | + * |
| 249 | + * @param instance ADC instance id. |
| 250 | + * @param channelCfgPtr The pointer to channel configuration structure. |
| 251 | + * @return the value of conversion. |
| 252 | + */ |
| 253 | +uint32_t adc_get_conversion_value(uint32_t instance, adc_channel_config_t *channelCfgPtr); |
| 254 | + |
| 255 | +/*! |
| 256 | + * @brief Privated to register the custom callback funcion of ADC ISR. |
| 257 | + * |
| 258 | + * Callback provides a friendly API for application to program ISR, when special |
| 259 | + * function piece need to be executed at the moment conversion is completed, |
| 260 | + * they can be inserted to ISR by call the registering function by user. |
| 261 | + * |
| 262 | + * @param instance ADC instance id. |
| 263 | + * @param func The pointer to user indicated callback function. |
| 264 | + */ |
| 265 | +void adc_register_user_callback_isr(uint32_t instance, adc_isr_callback_t func); |
| 266 | + |
| 267 | +#if defined(__cplusplus) |
| 268 | +extern } |
| 269 | +#endif |
| 270 | + |
| 271 | +/*! @}*/ |
| 272 | + |
| 273 | +#endif /* __FSL_ADC_DRIVER_H__ */ |
| 274 | +/******************************************************************************* |
| 275 | + * EOF |
| 276 | + ******************************************************************************/ |
0 commit comments