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 #undef TEMPERATURE_SENSOR_GPIO
60 #define TEMPERATURE_SENSOR_GPIO PORTB_PIN(7)
61 
62 
63 /*---------------------------------------------------------------------------*/
64 static void
65 init(void)
66 {
67  halGpioConfig(TEMPERATURE_SENSOR_GPIO,GPIOCFG_ANALOG);
68  halInternalInitAdc();
69  halAdcSetRange(TRUE);
70 }
71 /*---------------------------------------------------------------------------*/
72 static int
73 value(int type)
74 {
75  static int16u ADCvalue;
76  static int16s volts;
77 
78  halStartAdcConversion(ADC_USER_APP, ADC_REF_INT, ADC_SOURCE_ADC2_VREF2, ADC_CONVERSION_TIME_US_4096);
79 
80  halReadAdcBlocking(ADC_USER_APP, &ADCvalue); // This blocks for a while, about 4ms.
81 
82  // 100 uVolts
83  volts = halConvertValueToVolts(ADCvalue);
84 
85  //return ((18641 - (int32s)volts)*100)/1171; // +- 0.23 degC in the range (-10;65) degC
86  return ((18663 - (int32s)volts)*100)/1169; // +- 0.004 degC in the range (20;30) degC
87 
88 }
89 /*---------------------------------------------------------------------------*/
90 static int
91 configure(int type, int value)
92 {
93  switch(type){
94  case SENSORS_HW_INIT:
95  init();
96  return 1;
97  case SENSORS_ACTIVE:
98  return 1;
99  }
100 
101  return 0;
102 }
103 /*---------------------------------------------------------------------------*/
104 static int
105 status(int type)
106 {
107  switch(type) {
108 
109  case SENSORS_READY:
110  return 1;
111  }
112 
113  return 0;
114 }
115 /*---------------------------------------------------------------------------*/
116 SENSORS_SENSOR(temperature_sensor, TEMPERATURE_SENSOR,
117  value, configure, status);