Contiki 2.5
temperature-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, STMicroelectronics.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * 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  * This file is part of the Contiki OS
31  *
32  * $Id: temperature-sensor.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $
33  */
34 /*---------------------------------------------------------------------------*/
35 /**
36 * \file
37 * Temperature sensor.
38 * \author
39 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
40 */
41 /*---------------------------------------------------------------------------*/
42 
43 /**
44  * NOTE:
45  * For the temperature measurement, the ADC extended range mode is needed;
46  * but this is inaccurate due to the high voltage mode bug of the general purpose ADC
47  * (see STM32W108 errata).
48  */
49 
50 
51 #include PLATFORM_HEADER
52 #include BOARD_HEADER
53 #include "hal/error.h"
54 #include "hal/hal.h"
55 #include "micro/adc.h"
56 
57 #include "dev/temperature-sensor.h"
58 
59 
60 /*---------------------------------------------------------------------------*/
61 static void
62 init(void)
63 {
64  halGpioConfig(TEMPERATURE_SENSOR_GPIO,GPIOCFG_ANALOG);
65  halInternalInitAdc();
66  halAdcSetRange(TRUE);
67 }
68 /*---------------------------------------------------------------------------*/
69 static int
70 value(int type)
71 {
72  static int16u ADCvalue;
73  static int16s volts;
74 
75  halStartAdcConversion(ADC_USER_APP, ADC_REF_INT, ADC_SOURCE(halGetADCChannelFromGPIO(TEMPERATURE_SENSOR_GPIO),ADC_MUX_VREF2), ADC_CONVERSION_TIME_US_4096);
76 
77  halReadAdcBlocking(ADC_USER_APP, &ADCvalue); // This blocks for a while, about 4ms.
78 
79  // 100 uVolts
80  volts = halConvertValueToVolts(ADCvalue);
81 
82  //return ((18641 - (int32s)volts)*100)/1171; // +- 0.23 degC in the range (-10;65) degC
83  return ((18663 - (int32s)volts)*100)/1169; // +- 0.004 degC in the range (20;30) degC
84 
85 }
86 /*---------------------------------------------------------------------------*/
87 static int
88 configure(int type, int value)
89 {
90  switch(type){
91  case SENSORS_HW_INIT:
92  init();
93  return 1;
94  case SENSORS_ACTIVE:
95  return 1;
96  }
97 
98  return 0;
99 }
100 /*---------------------------------------------------------------------------*/
101 static int
102 status(int type)
103 {
104  switch(type) {
105 
106  case SENSORS_READY:
107  return 1;
108  }
109 
110  return 0;
111 }
112 /*---------------------------------------------------------------------------*/
113 SENSORS_SENSOR(temperature_sensor, TEMPERATURE_SENSOR,
114  value, configure, status);