Contiki 3.x
battery-sensor.c
Go to the documentation of this file.
1 /*
2  * Contiki SeedEye Platform project
3  *
4  * Copyright (c) 2012,
5  * Scuola Superiore Sant'Anna (http://www.sssup.it) and
6  * Consorzio Nazionale Interuniversitario per le Telecomunicazioni
7  * (http://www.cnit.it).
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the Institute nor the names of its contributors
20  * may be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 /**
38  * \addtogroup SeedEye Contiki SEEDEYE Platform
39  *
40  * @{
41  */
42 
43 /**
44  * \file platform/seedeye/dev/battery-sensor.c
45  * \brief Battery Sensor
46  * \author Giovanni Pellerano <giovanni.pellerano@evilaliv3.org>
47  * \date 2012-07-04
48  */
49 
50 #include <dev/battery-sensor.h>
51 
52 #include <p32xxxx.h>
53 
54 /*---------------------------------------------------------------------------*/
55 PROCESS(battery_process, "battery sensor");
56 /*---------------------------------------------------------------------------*/
57 
58 const struct sensors_sensor battery_sensor;
59 
60 #define BATTERY_SAMPLES 10
61 
62 static uint16_t battery_samples[BATTERY_SAMPLES];
63 static uint8_t counter = 0;
64 
65 static uint8_t sensor_status = 0;
66 
67 /*---------------------------------------------------------------------------*/
68 static int
69 value(int type)
70 {
71  uint32_t tmp = 0;
72  uint8_t i;
73  for(i = 0; i < BATTERY_SAMPLES; ++i) {
74  tmp += battery_samples[i];
75  }
76 
77  return tmp / BATTERY_SAMPLES;
78 }
79 /*---------------------------------------------------------------------------*/
80 static int
81 configure(int type, int c)
82 {
83  // all PORTB = Digital; RB10 = analog
84  AD1PCFG = 0b1111110111111111;
85 
86  // SSRC bit = 111 implies internal counter ends sampling and starts converting
87  AD1CON1 = 0b0000000011100000;
88 
89  AD1CHS = 0b00000000000010100000000000000000;
90 
91  AD1CSSL = 0;
92 
93  // Manual Sample, Tad = internal 6 TPB
94  AD1CON3 = 0x1F02;
95 
96  AD1CON2 = 0;
97 
98  // Turn ADC on
99  AD1CON1SET = 0b1000000000000000;
100 
101  process_start(&battery_process, NULL);
102 
103  sensor_status = 1;
104 
105  return 0;
106 }
107 /*---------------------------------------------------------------------------*/
108 static int
109 status(int type)
110 {
111  return sensor_status;
112 }
113 /*---------------------------------------------------------------------------*/
114 SENSORS_SENSOR(battery_sensor, BATTERY_SENSOR, value, configure, status);
115 /*---------------------------------------------------------------------------*/
116 PROCESS_THREAD(battery_process, ev, data)
117 {
118  PROCESS_BEGIN();
119 
120  while(1) {
121  static struct etimer et;
122 
123  etimer_set(&et, CLOCK_SECOND);
124 
126 
127  // start converting
128  AD1CON1SET = 0b0000000000000010;
129 
130  while(!(AD1CON1 & 0b0000000000000001)) {
131  ; // wait conversion finish
132  }
133 
134  // read the conversion result
135  battery_samples[counter] = ADC1BUF0;
136 
137  counter = (counter + 1) % BATTERY_SAMPLES;
138  }
139 
140  PROCESS_END();
141 }
142 
143 /** @} */
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:205
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define NULL
The null pointer.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
A timer.
Definition: etimer.h:76
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82