Contiki-NG
sky-sensors.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  * This file is part of the Contiki operating system.
30  *
31  *
32  * -----------------------------------------------------------------
33  *
34  * Author : Joakim Eriksson
35  * Created : 2010-02-02
36  * Updated : $Date: 2010/08/25 19:30:53 $
37  * $Revision: 1.3 $
38  */
39 #include "contiki.h"
40 #include "lib/sensors.h"
41 
42 #define ADC12MCTL_NO(adcno) ((unsigned char *) ADC12MCTL0_)[adcno]
43 
44 static uint16_t adc_on;
45 static uint16_t ready;
46 /*---------------------------------------------------------------------------*/
47 static CC_INLINE void
48 start(void)
49 {
50  uint16_t c, last;
51 
52  /* Set up the ADC. */
53  P6DIR = 0xff;
54  P6OUT = 0x00;
55 
56  /* Setup ADC12, ref., sampling time */
57  /* XXX Note according to the specification a minimum of 17 ms should
58  be allowed after turn on of the internal reference generator. */
59  ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC + REFON;
60  /* Use sampling timer, repeat-sequence-of-channels */
61  ADC12CTL1 = SHP + CONSEQ_3;
62 
63  last = 15;
64  for(c = 0; c < 16; c++) {
65  /* Clear all end-of-sequences */
66  ADC12MCTL_NO(c) &= ~EOS;
67  if(adc_on & (1 << c)) {
68  if(last == 15) {
69  /* Set new start of sequence to lowest active memory holder */
70  ADC12CTL1 |= (c * CSTARTADD_1);
71  }
72  last = c;
73  }
74  }
75 
76  /* Set highest end-of-sequence. */
77  ADC12MCTL_NO(last) |= EOS;
78 
79  ADC12CTL0 |= ADC12ON;
80  ADC12CTL0 |= ENC; /* enable conversion */
81  ADC12CTL0 |= ADC12SC; /* sample & convert */
82 }
83 /*---------------------------------------------------------------------------*/
84 static CC_INLINE void
85 stop(void)
86 {
87  /* stop converting immediately, turn off reference voltage, etc. */
88 
89  ADC12CTL0 &= ~ENC;
90  /* need to remove CONSEQ_3 if not EOS is configured */
91  ADC12CTL1 &= ~CONSEQ_3;
92 
93  /* wait for conversion to stop */
94  while(ADC12CTL1 & ADC12BUSY);
95 
96  /* clear any pending interrupts */
97  ADC12IFG = 0;
98 }
99 /*---------------------------------------------------------------------------*/
100 int
101 sky_sensors_status(uint16_t input, int type)
102 {
103  if(type == SENSORS_ACTIVE) {
104  return (adc_on & input) == input;
105  }
106  if(type == SENSORS_READY) {
107  ready |= ADC12IFG & adc_on & input;
108  return (ready & adc_on & input) == input;
109  }
110  return 0;
111 }
112 /*---------------------------------------------------------------------------*/
113 int
114 sky_sensors_configure(uint16_t input, uint8_t ref, int type, int value)
115 {
116  uint16_t c;
117 
118  if(type == SENSORS_ACTIVE) {
119  stop();
120 
121  if(value) {
122  adc_on |= input;
123  P6SEL |= input & 0xff;
124 
125  /* Set ADC config */
126  for(c = 0; c < 16; c++) {
127  if(input & (1 << c)) {
128  ADC12MCTL_NO(c) = (c * INCH_1) | ref;
129  }
130  }
131 
132  } else {
133  adc_on &= ~input;
134  ready &= ~input;
135  P6SEL &= ~(input & 0xff);
136  }
137 
138  if(adc_on == 0) {
139  P6DIR = 0x00;
140  P6SEL = 0x00;
141 
142  /* Turn off ADC and internal reference generator */
143  ADC12CTL0 = 0;
144  ADC12CTL1 = 0;
145  } else {
146  start();
147  }
148  return 1;
149  }
150  return 0;
151 }
152 /*---------------------------------------------------------------------------*/
static void start(void)
Start measurement.
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1802