Contiki 3.x
temperature-sensor.c
Go to the documentation of this file.
1 /**
2  * \addtogroup mbxxx-platform
3  *
4  * @{
5  */
6 /*
7  * Copyright (c) 2010, STMicroelectronics.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * 3. The name of the author may not be used to endorse or promote
20  * products derived from this software without specific prior
21  * written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * This file is part of the Contiki OS
36  *
37  */
38 /*---------------------------------------------------------------------------*/
39 /**
40 * \file
41 * Temperature sensor.
42 * \author
43 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
44 */
45 /*---------------------------------------------------------------------------*/
46 
47 /**
48  * NOTE:
49  * For the temperature measurement, the ADC extended range mode is needed;
50  * but this is inaccurate due to the high voltage mode bug of the general purpose ADC
51  * (see STM32W108 errata).
52  */
53 
54 
55 #include PLATFORM_HEADER
56 #include BOARD_HEADER
57 #include "hal/error.h"
58 #include "hal/hal.h"
59 #include "micro/adc.h"
60 
61 #include "dev/temperature-sensor.h"
62 
63 #define SUPPLY_OFFSET 500
64 /*---------------------------------------------------------------------------*/
65 static void
66 init(void)
67 {
68  //halGpioConfig(TEMPERATURE_SENSOR_GPIO,GPIOCFG_ANALOG);
69  halInternalInitAdc();
70  halAdcSetRange(TRUE);
71 
72 }
73 /*---------------------------------------------------------------------------*/
74 static int
75 value(int type)
76 {
77  static uint16_t ADCvalue;
78  static int16_t volts;
79 
80  halStartAdcConversion(ADC_USER_APP, ADC_REF_INT, ADC_SOURCE(halGetADCChannelFromGPIO(TEMPERATURE_SENSOR_GPIO),ADC_MUX_VREF2), ADC_CONVERSION_TIME_US_4096);
81 
82  halReadAdcBlocking(ADC_USER_APP, &ADCvalue); // This blocks for a while, about 4ms.
83 
84 
85  // 100 uVolts
86  volts = boardDescription->temperatureSensor->div*halConvertValueToVolts(ADCvalue) + SUPPLY_OFFSET;
87 
88 
89  //return ((18641 - (int32_t)volts)*100)/1171; // +- 0.23 degC in the range (-10;65) degC
90  return ((18663 - (int32_t)volts)*100)/1169; // +- 0.004 degC in the range (20;30) degC
91 
92 }
93 /*---------------------------------------------------------------------------*/
94 static int
95 configure(int type, int value)
96 {
97  switch(type){
98  case SENSORS_HW_INIT:
99  init();
100  return 1;
101  case SENSORS_ACTIVE:
102  return 1;
103  }
104 
105  return 0;
106 }
107 /*---------------------------------------------------------------------------*/
108 static int
109 status(int type)
110 {
111  switch(type) {
112 
113  case SENSORS_READY:
114  return 1;
115  }
116 
117  return 0;
118 }
119 /*---------------------------------------------------------------------------*/
120 SENSORS_SENSOR(temperature_sensor, TEMPERATURE_SENSOR,
121  value, configure, status);
122 /** @} */
#define TRUE
An alias for one, used for clarity.
uint8_t div
Voltage divider network from sensor vout and GPIO.
Definition: board.h:232
static int value(int type)
Returns device temperature.
Generic set of HAL includes for all platforms.
Return codes for API functions and module definitions.
#define TEMPERATURE_SENSOR_GPIO
Description temperature sensor GPIO.
Definition: board.h:226
const TempSensorResourceType * temperatureSensor
Board infrared temeprature sensor description.
Definition: board.h:182
const struct sensors_sensor temperature_sensor
NOTE: For the temperature measurement, the ADC extended range mode is needed; but this is inaccurate ...
#define SUPPLY_OFFSET
NOTE: For the temperature measurement, the ADC extended range mode is needed; but this is inaccurate ...
Header for A/D converter.