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