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  uint16_t scale=1;
80 
81  halStartAdcConversion(ADC_USER_APP, ADC_REF_INT, ADC_SOURCE(halGetADCChannelFromGPIO(TEMPERATURE_SENSOR_GPIO),ADC_MUX_VREF2), ADC_CONVERSION_TIME_US_4096);
82 
83  halReadAdcBlocking(ADC_USER_APP, &ADCvalue); // This blocks for a while, about 4ms.
84 
85 
86  // 100 uVolts
87  volts = boardDescription->temperatureSensor->div*halConvertValueToVolts(ADCvalue) + SUPPLY_OFFSET;
88 
89 
90  //return ((18641 - (int32_t)volts)*100)/1171; // +- 0.23 degC in the range (-10;65) degC
91  return ((18663 - (int32_t)volts)*100)/1169; // +- 0.004 degC in the range (20;30) degC
92 
93 }
94 /*---------------------------------------------------------------------------*/
95 static int
96 configure(int type, int value)
97 {
98  switch(type){
99  case SENSORS_HW_INIT:
100  init();
101  return 1;
102  case SENSORS_ACTIVE:
103  return 1;
104  }
105 
106  return 0;
107 }
108 /*---------------------------------------------------------------------------*/
109 static int
110 status(int type)
111 {
112  switch(type) {
113 
114  case SENSORS_READY:
115  return 1;
116  }
117 
118  return 0;
119 }
120 /*---------------------------------------------------------------------------*/
121 SENSORS_SENSOR(temperature_sensor, TEMPERATURE_SENSOR,
122  value, configure, status);
123 /** @} */
struct sensors_sensor temperature_sensor
NOTE: For the temperature measurement, the ADC extended range mode is needed; but this is inaccurate ...
#define TEMPERATURE_SENSOR_GPIO
Description temperature sensor GPIO.
Definition: board.h:226
#define TRUE
An alias for one, used for clarity.
Header for A/D converter.
#define SUPPLY_OFFSET
NOTE: For the temperature measurement, the ADC extended range mode is needed; but this is inaccurate ...
Generic set of HAL includes for all platforms.
Return codes for API functions and module definitions.
const TempSensorResourceType * temperatureSensor
Board infrared temeprature sensor description.
Definition: board.h:182
uint8_t div
Voltage divider network from sensor vout and GPIO.
Definition: board.h:232