adc.h

Go to the documentation of this file.
00001 /** @file /hal/micro/adc.h
00002  * @brief Header for A/D converter.
00003  *
00004  * <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved.        -->
00005  */
00006 /** @addtogroup adc
00007  * Sample A/D converter driver.
00008  *
00009  * See adc.h for source code.
00010  *
00011  * @note Stm32w108xx ADC driver support is preliminary and essentailly untested -
00012  * please do not attempt to use this ADC driver on this platform.
00013  *
00014  * @note Except for the Stm32w108xx, the StZNet stack does use these functions.
00015  *
00016  * To use the ADC system, include this file and ensure that
00017  * ::halInternalInitAdc() is called whenever the microcontroller is
00018  * started.  
00019  *
00020  * A "user" is a separate thread of execution and usage.  That is,
00021  * internal St code is one user and clients are a different user.
00022  * But a client that is calling the ADC in two different functions
00023  * constitutes only one user, as long as the ADC access is not
00024  * interleaved.
00025  *
00026  * @note This code does not allow access to the continuous reading mode of
00027  * the ADC, which some clients may require.
00028  *
00029  * Many functions in this file return an ::StStatus value.  See
00030  * error-def.h for definitions of all ::StStatus return values.
00031  *
00032  *@{
00033  */
00034 #ifndef __ADC_H__
00035 #define __ADC_H__
00036 
00037 // A type for the ADC User enumeration.
00038 typedef int8u ADCUser;
00039 enum
00040 {
00041   /** LQI User ID. */
00042   ADC_USER_LQI = 0,
00043   /** Application User ID */
00044   ADC_USER_APP = 1,
00045   /** Application User ID */
00046   ADC_USER_APP2 = 2
00047 };
00048 
00049 /** @brief Be sure to update ::NUM_ADC_USERS if additional users are added
00050  * to the ::ADCUser list.
00051  */
00052 #define NUM_ADC_USERS 3 // make sure to update if the above is adjusted
00053 
00054 
00055 // A type for the reference enumeration.
00056 typedef int8u ADCReferenceType;
00057 enum
00058 {
00059   /** AREF pin reference. */
00060   ADC_REF_AREF = 0x00,
00061   /** AVCC pin reference. */
00062   ADC_REF_AVCC = 0x40,
00063   /** Internal reference. */
00064   ADC_REF_INT  = 0xC0
00065 };
00066 
00067 // A type for the rate enumeration.
00068 typedef int8u ADCRateType;
00069 enum
00070 {
00071   /** Rate 32 us, 5 effective bits in ADC_DATA[15:11] */
00072   ADC_CONVERSION_TIME_US_32   = 0x0,
00073   /** Rate 64 us, 6 effective bits in ADC_DATA[15:10] */
00074   ADC_CONVERSION_TIME_US_64   = 0x1,
00075   /** Rate 128 us, 7 effective bits in ADC_DATA[15:9] */
00076   ADC_CONVERSION_TIME_US_128  = 0x2,
00077   /** Rate 256 us, 8 effective bits in ADC_DATA[15:8] */
00078   ADC_CONVERSION_TIME_US_256  = 0x3,
00079   /** Rate 512 us, 9 effective bits in ADC_DATA[15:7] */
00080   ADC_CONVERSION_TIME_US_512  = 0x4,
00081   /** Rate 1024 us, 10 effective bits in ADC_DATA[15:6] */
00082   ADC_CONVERSION_TIME_US_1024 = 0x5,
00083   /** Rate 2048 us, 11 effective bits in ADC_DATA[15:5] */
00084   ADC_CONVERSION_TIME_US_2048 = 0x6,
00085   /** Rate 4096 us, 12 effective bits in ADC_DATA[15:4] */
00086   ADC_CONVERSION_TIME_US_4096 = 0x7,
00087 };
00088 
00089 
00090 #if defined (CORTEXM3)
00091   /** Channel 0 : ADC0 on PB5 */
00092 #define ADC_MUX_ADC0    0x0
00093   /** Channel 1 : ADC0 on PB6 */
00094 #define ADC_MUX_ADC1    0x1
00095   /** Channel 2 : ADC0 on PB7 */
00096 #define ADC_MUX_ADC2    0x2
00097   /** Channel 3 : ADC0 on PC1 */
00098 #define ADC_MUX_ADC3    0x3
00099   /** Channel 4 : ADC0 on PA4 */
00100 #define ADC_MUX_ADC4    0x4
00101   /** Channel 5 : ADC0 on PA5 */
00102 #define ADC_MUX_ADC5    0x5
00103   /** Channel 8 : VSS (0V) - not for high voltage range */
00104 #define ADC_MUX_GND     0x8
00105   /** Channel 9 : VREF/2 (0.6V) */
00106 #define ADC_MUX_VREF2   0x9
00107   /** Channel A : VREF (1.2V)*/
00108 #define ADC_MUX_VREF    0xA
00109   /** Channel B : Regulator/2 (0.9V) - not for high voltage range */
00110 #define ADC_MUX_VREG2   0xB
00111 
00112 // ADC_SOURCE_<pos>_<neg> selects <pos> as the positive input and <neg> as  
00113 // the negative input.
00114 enum
00115 {
00116   ADC_SOURCE_ADC0_VREF2  = ((ADC_MUX_ADC0  <<ADC_MUXN_BITS) + ADC_MUX_VREF2),
00117   ADC_SOURCE_ADC0_GND    = ((ADC_MUX_ADC0  <<ADC_MUXN_BITS) + ADC_MUX_GND),
00118 
00119   ADC_SOURCE_ADC1_VREF2  = ((ADC_MUX_ADC1  <<ADC_MUXN_BITS) + ADC_MUX_VREF2),
00120   ADC_SOURCE_ADC1_GND    = ((ADC_MUX_ADC1  <<ADC_MUXN_BITS) + ADC_MUX_GND),
00121 
00122   ADC_SOURCE_ADC2_VREF2  = ((ADC_MUX_ADC2  <<ADC_MUXN_BITS) + ADC_MUX_VREF2),
00123   ADC_SOURCE_ADC2_GND    = ((ADC_MUX_ADC2  <<ADC_MUXN_BITS) + ADC_MUX_GND),
00124 
00125   ADC_SOURCE_ADC3_VREF2  = ((ADC_MUX_ADC3  <<ADC_MUXN_BITS) + ADC_MUX_VREF2),
00126   ADC_SOURCE_ADC3_GND    = ((ADC_MUX_ADC3  <<ADC_MUXN_BITS) + ADC_MUX_GND),
00127 
00128   ADC_SOURCE_ADC4_VREF2  = ((ADC_MUX_ADC4  <<ADC_MUXN_BITS) + ADC_MUX_GND),
00129 
00130   ADC_SOURCE_ADC5_VREF2  = ((ADC_MUX_ADC5  <<ADC_MUXN_BITS) + ADC_MUX_GND),
00131 
00132   ADC_SOURCE_ADC1_ADC0   = ((ADC_MUX_ADC1  <<ADC_MUXN_BITS) + ADC_MUX_ADC0),
00133   ADC_SOURCE_ADC0_ADC1   = ((ADC_MUX_ADC1  <<ADC_MUXN_BITS) + ADC_MUX_ADC0),
00134 
00135   ADC_SOURCE_ADC3_ADC2   = ((ADC_MUX_ADC3  <<ADC_MUXN_BITS) + ADC_MUX_ADC2),
00136   ADC_SOURCE_ADC2_ADC3   = ((ADC_MUX_ADC3  <<ADC_MUXN_BITS) + ADC_MUX_ADC2),
00137 
00138   ADC_SOURCE_ADC5_ADC4   = ((ADC_MUX_ADC5  <<ADC_MUXN_BITS) + ADC_MUX_ADC4),
00139 
00140   ADC_SOURCE_GND_VREF2   = ((ADC_MUX_GND   <<ADC_MUXN_BITS) + ADC_MUX_VREF2),
00141   ADC_SOURCE_VGND        = ((ADC_MUX_GND   <<ADC_MUXN_BITS) + ADC_MUX_GND),
00142 
00143   ADC_SOURCE_VREF_VREF2  = ((ADC_MUX_VREF  <<ADC_MUXN_BITS) + ADC_MUX_VREF2),
00144   ADC_SOURCE_VREF        = ((ADC_MUX_VREF  <<ADC_MUXN_BITS) + ADC_MUX_GND),
00145 /* Modified the original ADC driver for enabling the ADC extended range mode required for 
00146    supporting the STLM20 temperature sensor.
00147    NOTE: 
00148    The ADC extended range is inaccurate due to the high voltage mode bug of the general purpose ADC 
00149    (see STM32W108 errata). As consequence, it is not reccomended to use this ADC driver for getting
00150    the temperature values 
00151 */
00152 #ifdef ENABLE_ADC_EXTENDED_RANGE_BROKEN
00153   ADC_SOURCE_VREF2_VREF2  = ((ADC_MUX_VREF2  <<ADC_MUXN_BITS) + ADC_MUX_VREF2),
00154   ADC_SOURCE_VREF2        = ((ADC_MUX_VREF2  <<ADC_MUXN_BITS) + ADC_MUX_GND),
00155 #endif /* ENABLE_ADC_EXTENDED_RANGE_BROKEN */
00156 
00157   ADC_SOURCE_VREG2_VREF2 = ((ADC_MUX_VREG2 <<ADC_MUXN_BITS) + ADC_MUX_VREF2),
00158   ADC_SOURCE_VDD_GND     = ((ADC_MUX_VREG2 <<ADC_MUXN_BITS) + ADC_MUX_GND)
00159 };
00160 #endif // defined (CORTEXM3)
00161 
00162 
00163 /** @brief A type for the channel enumeration 
00164  * (such as ::ADC_SOURCE_ADC0_GND)
00165  */
00166 typedef int8u ADCChannelType;
00167 
00168 
00169 /** @brief Initializes and powers-up the ADC. 
00170  */
00171 void halInternalInitAdc(void);
00172 
00173 /** @brief Starts an ADC conversion for the user specified by \c id.
00174  *
00175  * @appusage The application must set \c reference to the voltage
00176  * reference desired (see the ADC references enum), 
00177  * set \c channel to the channel number
00178  * required (see the ADC channel enum), and set \c rate to reflect the
00179  * number of bits of accuracy desired (see the ADC rates enum)
00180  *
00181  * @param id        An ADC user.
00182  * 
00183  * @param reference Voltage reference to use, chosen from enum
00184  * 
00185  * @param channel   Microprocessor channel number.  
00186  * 
00187  * @param rate      rate number (see the ADC rate enum).
00188  * 
00189  * @return One of the following: 
00190  * - ADC_CONVERSION_DEFERRED   if the conversion is still waiting 
00191  * to start.
00192  * - ADC_CONVERSION_BUSY       if the conversion is currently taking 
00193  * place.
00194  * - ST_ERR_FATAL                 if a passed parameter is invalid.
00195  */
00196 StStatus halStartAdcConversion(ADCUser id,
00197                                   ADCReferenceType reference,
00198                                   ADCChannelType channel,
00199                                   ADCRateType rate);
00200 
00201 /** @brief Returns the status of a pending conversion
00202  * previously started by ::halStartAdcConversion().  If the conversion
00203  * is complete, writes the raw register value of the conversion (the unaltered
00204  * value taken directly from the ADC's data register) into \c value.
00205  *
00206  * @param id     An ADC user.
00207  *
00208  * @param value  Pointer to an int16u to be loaded with the new value. 
00209  * 
00210  * @return One of the following: 
00211  * - ::ST_ADC_CONVERSION_DONE       if the conversion is complete.
00212  * - ::ST_ADC_CONVERSION_DEFERRED   if the conversion is still waiting 
00213  * to start.
00214  * - ::ST_ADC_CONVERSION_BUSY       if the conversion is currently taking 
00215  * place.
00216  * - ::ST_ADC_NO_CONVERSION_PENDING if \c id does not have a pending 
00217  * conversion.
00218  */
00219 StStatus halRequestAdcData(ADCUser id, int16u *value);
00220 
00221 
00222 /** @brief Waits for the user's request to complete and then,
00223  * if a conversion was done, writes the raw register value of the conversion
00224  * (the unaltered value taken directly from the ADC's data register) into
00225  * \c value and returns ::ADC_CONVERSION_DONE, or immediately
00226  * returns ::ADC_NO_CONVERSION_PENDING.
00227  *
00228  * @param id     An ADC user.
00229  *
00230  * @param value  Pointer to an int16u to be loaded with the new value. 
00231  * 
00232  * @return One of the following: 
00233  * - ::ST_ADC_CONVERSION_DONE        if the conversion is complete.
00234  * - ::ST_ADC_NO_CONVERSION_PENDING  if \c id does not have a pending 
00235  * conversion.
00236  */
00237 StStatus halReadAdcBlocking(ADCUser id, int16u *value);
00238 
00239 
00240 /** @brief Calibrates or recalibrates the ADC system. 
00241  *
00242  * @appusage Use this function to (re)calibrate as needed. This function is
00243  * intended for the microcontroller, which requires proper calibration to calculate
00244  * a human readible value (a value in volts).  If the app does not call this
00245  * function, the first time (and only the first time) the function 
00246  * ::halConvertValueToVolts() is called, this function is invoked.  To
00247  * maintain accurate volt calculations, the application should call this
00248  * whenever it expects the temperature of the micro to change.
00249  *
00250  * @param id  An ADC user.
00251  *
00252  * @return One of the following: 
00253  * - ::ST_ADC_CONVERSION_DONE        if the calibration is complete.
00254  * - ::ST_ERR_FATAL                  if the calibration failed.
00255  */
00256 StStatus halAdcCalibrate(ADCUser id);
00257 
00258 
00259 /** @brief Convert the raw register value (the unaltered value taken
00260  * directly from the ADC's data register) into a signed fixed point value with
00261  * units 10^-4 Volts.  The returned value will be in the range -12000 to
00262  * +12000 (-1.2000 volts to +1.2000 volts).
00263  *
00264  * @appusage Use this function to get a human useful value.
00265  *
00266  * @param value  An int16u to be converted.
00267  * 
00268  * @return Volts as signed fixed point with units 10^-4 Volts. 
00269  */
00270 int16s halConvertValueToVolts(int16u value);
00271 
00272 
00273 /** @brief Calibrates Vref to be 1.2V +/-10mV.
00274  *
00275  *  @appusage This function must be called from halInternalInitAdc() before
00276  *  making ADC readings.  This function is not intended to be called from any
00277  *  function other than halInternalInitAdc().  This function ensures that the
00278  *  master cell voltage and current bias values are calibrated before
00279  *  calibrating Vref.
00280  */
00281 void stCalibrateVref(void);
00282 
00283 #ifdef CORTEXM3
00284 void halAdcSetClock(boolean fast);
00285 void halAdcSetRange(boolean high);
00286 boolean halAdcGetClock(void);
00287 boolean halAdcGetRange(void);
00288 #endif
00289 
00290 #endif // __ADC_H__
00291 
00292 /** @} // END addtogroup
00293  */
00294 
00295 

Generated on Mon Apr 11 14:23:38 2011 for Contiki 2.5 by  doxygen 1.6.1