Contiki-Inga 3.x
battery-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, TU Braunschweig.
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 copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /**
31  * \file
32  * Battery sensor implementation
33  * \author
34  * Enrico Joerns <e.joerns@tu-bs.de>
35  */
36 
37 #include "contiki.h"
38 #include "lib/sensors.h"
39 #include "adc.h"
40 #include "battery-sensor.h"
41 
42 /* VCC ADC channel */
43 #define PWR_MONITOR_VCC_ADC 2
44 /* ICC ADC channel */
45 #define PWR_MONITOR_ICC_ADC 3
46 
47 /* @2.56Vref: 2^10 => 4267mV; */
48 #define BATTERY_SENSOR_V_SCALE 25 / 6
49 /* @2.56Vref: 2^10 => 128mA; */
50 #define BATTERY_SENSOR_I_SCALE 1 / 4
51 
52 static uint8_t ready = 0;
53 static uint8_t initialized = 0;
54 
55 const struct sensors_sensor battery_sensor;
56 /*----------------------------------------------------------------------------*/
57 static int
58 value(int type)
59 {
60  switch (type) {
61  case BATTERY_VOLTAGE:
62  return adc_get_value_from(PWR_MONITOR_VCC_ADC) * BATTERY_SENSOR_V_SCALE;
63  case BATTERY_CURRENT:
64  return adc_get_value_from(PWR_MONITOR_ICC_ADC) * BATTERY_SENSOR_I_SCALE;
65  }
66  return 0;
67 }
68 /*----------------------------------------------------------------------------*/
69 static int
70 status(int type)
71 {
72  switch (type) {
73  case SENSORS_ACTIVE:
74  return initialized;
75  case SENSORS_READY:
76  return ready;
77  }
78  return 0;
79 }
80 /*----------------------------------------------------------------------------*/
81 static int
82 configure(int type, int c)
83 {
84  switch (type) {
85  case SENSORS_HW_INIT:
86  ready = 1;
87  return 1;
88  break;
89  case SENSORS_ACTIVE:
90  if (c) {
92  initialized = 1;
93  return 1;
94  } else {
95  adc_deinit();
96  initialized = 0;
97  return 1;
98  }
99  break;
100  default:
101  return 0;
102  break;
103  }
104  return 0;
105 }
106 /*----------------------------------------------------------------------------*/
107 SENSORS_SENSOR(battery_sensor, BATTERY_SENSOR, value, configure, status);
108