Contiki 3.x
acc-sensor.c
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
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  * Author : Adam Dunkels, Joakim Eriksson, Niclas Finne
32  * Created : 2005-11-01
33  */
34 
35 #include "dev/acc-sensor.h"
36 
37 const struct sensors_sensor acc_sensor;
38 static uint8_t active;
39 
40 /*---------------------------------------------------------------------------*/
41 static void
42 activate(void)
43 {
44  /* This assumes that some other sensor system already did setup the ADC
45  * (in the case of the sky platform it is sensors_light_init that does it) */
46  #if 0
47  P6SEL |= 0x70;
48  P6DIR = 0x00;
49  P6OUT = 0x00;
50 
51  P2DIR |= 0x48;
52  P2OUT |= 0x48;
53 
54 
55  /* stop converting immediately */
56  ADC12CTL0 &= ~ENC;
57  ADC12CTL1 &= ~CONSEQ_3;
58 
59  /* Configure ADC12_2 to sample channel 11 (voltage) and use
60  * the Vref+ as reference (SREF_1) since it is a stable reference */
61  ADC12MCTL2 = (INCH_4 + SREF_1);
62  ADC12MCTL3 = (INCH_5 + SREF_1);
63  ADC12MCTL4 = (INCH_6 + SREF_1);
64  /* internal temperature can be read as value(3) */
65  ADC12MCTL5 = (INCH_10 + SREF_1);
66 
67  ADC12CTL1 |= CONSEQ_3;
68  ADC12CTL0 |= ENC | ADC12SC;
69 
70  Irq_adc12_activate(&acc_sensor, 6, (INCH_11 + SREF_1));
71  #endif /* 0 */
72  active = 1;
73 }
74 /*---------------------------------------------------------------------------*/
75 static void
76 deactivate(void)
77 {
78  #if 0
79  irq_adc12_deactivate(&acc_sensor, 6);
80  acc_value = 0;
81  #endif /* 0 */
82  active = 0;
83 }
84 /*---------------------------------------------------------------------------*/
85 static int
86 value(int type)
87 {
88  #if 0
89  switch(type) {
90  case 0:
91  return ADC12MEM2;
92  case 1:
93  return ADC12MEM3;
94  case 2:
95  return ADC12MEM4;
96  case 3:
97  return ADC12MEM5;
98  }
99  #endif /* 0 */
100  return 0;
101 }
102 /*---------------------------------------------------------------------------*/
103 static int
104 configure(int type, int value)
105 {
106  if(type == SENSORS_ACTIVE) {
107  if(value) {
108  activate();
109  } else {
110  deactivate();
111  }
112  return 1;
113  }
114  return 0;
115 }
116 /*---------------------------------------------------------------------------*/
117 static int
118 status(int type)
119 {
120  switch (type) {
121  case SENSORS_ACTIVE:
122  case SENSORS_READY:
123  return active;
124  default:
125  return 0;
126  }
127 }
128 /*---------------------------------------------------------------------------*/
129 SENSORS_SENSOR(acc_sensor, ACC_SENSOR, value, configure, status);