Contiki 2.6

temperature-sensor.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, STMicroelectronics.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above
00011  *    copyright notice, this list of conditions and the following
00012  *    disclaimer in the documentation and/or other materials provided
00013  *    with the distribution.
00014  * 3. The name of the author may not be used to endorse or promote
00015  *    products derived from this software without specific prior
00016  *    written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00019  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00022  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00024  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * This file is part of the Contiki OS
00031  *
00032  * $Id: temperature-sensor.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $
00033  */
00034 /*---------------------------------------------------------------------------*/
00035 /**
00036 * \file
00037 *                       Temperature sensor.
00038 * \author
00039 *                       Salvatore Pitrulli <salvopitru@users.sourceforge.net>
00040 */
00041 /*---------------------------------------------------------------------------*/
00042 
00043 /**
00044  *  NOTE: 
00045  *  For the temperature measurement, the ADC extended range mode is needed;
00046  *  but this is inaccurate due to the high voltage mode bug of the general purpose ADC 
00047  *  (see STM32W108 errata).
00048  */
00049 
00050 
00051 #include PLATFORM_HEADER
00052 #include BOARD_HEADER
00053 #include "hal/error.h"
00054 #include "hal/hal.h"
00055 #include "micro/adc.h"
00056 
00057 #include "dev/temperature-sensor.h"
00058 
00059 
00060 /*---------------------------------------------------------------------------*/
00061 static void
00062 init(void)
00063 {
00064   halGpioConfig(TEMPERATURE_SENSOR_GPIO,GPIOCFG_ANALOG);
00065   halInternalInitAdc();
00066   halAdcSetRange(TRUE);
00067 }
00068 /*---------------------------------------------------------------------------*/
00069 static int
00070 value(int type)
00071 {
00072   static int16u ADCvalue;
00073   static int16s volts;
00074   
00075   halStartAdcConversion(ADC_USER_APP, ADC_REF_INT, ADC_SOURCE(halGetADCChannelFromGPIO(TEMPERATURE_SENSOR_GPIO),ADC_MUX_VREF2), ADC_CONVERSION_TIME_US_4096);
00076   
00077   halReadAdcBlocking(ADC_USER_APP, &ADCvalue); // This blocks for a while, about 4ms.
00078   
00079   // 100 uVolts
00080   volts = halConvertValueToVolts(ADCvalue);
00081   
00082   //return ((18641 - (int32s)volts)*100)/1171;  // +- 0.23 degC in the range (-10;65) degC
00083   return ((18663 - (int32s)volts)*100)/1169;   // +- 0.004 degC in the range (20;30) degC
00084   
00085 }
00086 /*---------------------------------------------------------------------------*/
00087 static int
00088 configure(int type, int value)
00089 {
00090   switch(type){
00091     case SENSORS_HW_INIT:
00092       init();
00093       return 1;
00094     case SENSORS_ACTIVE:
00095       return 1;
00096   }
00097        
00098   return 0;
00099 }
00100 /*---------------------------------------------------------------------------*/
00101 static int
00102 status(int type)
00103 {
00104   switch(type) {
00105     
00106     case SENSORS_READY:
00107       return 1;
00108   }
00109   
00110   return 0;
00111 }
00112 /*---------------------------------------------------------------------------*/
00113 SENSORS_SENSOR(temperature_sensor, TEMPERATURE_SENSOR,
00114                value, configure, status);