Contiki-NG
z1-phidgets.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/11/05 10:31:57 $
37  * $Revision: 1.3 $
38  */
39 
40 #include "contiki.h"
41 #include "lib/sensors.h"
42 #include "dev/z1-phidgets.h"
43 
44 static uint8_t adc_on;
45 static uint8_t active;
46 /*---------------------------------------------------------------------------*/
47 static void
48 sensors_activate(uint8_t type)
49 {
50  uint8_t pre = adc_on;
51 
52  adc_on |= type;
53 
54  if(pre == 0 && adc_on > 0) {
55  P6DIR = 0xff;
56  P6OUT = 0x00;
57  P6SEL |= 0x8b; /* bit 7 + 3 + 1 + 0 */
58 
59  /* if nothing was started before, start up the ADC system */
60  /* Set up the ADC. */
61  ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC; /* Setup ADC12, ref., sampling time */
62  ADC12CTL1 = SHP + CONSEQ_3 + CSTARTADD_0; /* Use sampling timer, repeat-sequenc-of-channels */
63  /* convert up to MEM4 */
64  ADC12MCTL4 |= EOS;
65 
66  ADC12CTL0 |= ADC12ON + REFON;
67  ADC12CTL0 |= ENC; /* enable conversion */
68  ADC12CTL0 |= ADC12SC; /* sample & convert */
69  }
70 }
71 /*---------------------------------------------------------------------------*/
72 static void
73 sensors_deactivate(uint8_t type)
74 {
75  adc_on &= ~type;
76 
77  if(adc_on == 0) {
78  /* stop converting immediately, turn off reference voltage, etc. */
79  /* wait for conversion to stop */
80 
81  ADC12CTL0 &= ~ENC;
82  /* need to remove CONSEQ_3 if not EOS is configured */
83  ADC12CTL1 &= ~CONSEQ_3;
84 
85  while(ADC12CTL1 & ADC12BUSY);
86 
87  ADC12CTL0 = 0;
88  ADC12CTL1 = 0;
89 
90  P6DIR = 0x00;
91  P6OUT = 0x00;
92  P6SEL = 0x00;
93  }
94 }
95 /*---------------------------------------------------------------------------*/
96 static int
97 value(int type)
98 {
99  /* ADC0 corresponds to the port under the logo, ADC1 to the port over the logo,
100  ADC2 and ADC3 corresponds to port on the JCreate bottom expansion port) */
101  switch(type) {
102  case PHIDGET5V_1:
103  return ADC12MEM0;
104  case PHIDGET5V_2:
105  return ADC12MEM1;
106  case PHIDGET3V_1:
107  return ADC12MEM2;
108  case PHIDGET3V_2:
109  return ADC12MEM3;
110  }
111  return 0;
112 }
113 /*---------------------------------------------------------------------------*/
114 static int
115 status(int type)
116 {
117  switch(type) {
118  case SENSORS_ACTIVE:
119  case SENSORS_READY:
120  return active;
121  }
122  return 0;
123 }
124 /*---------------------------------------------------------------------------*/
125 static int
126 configure(int type, int c)
127 {
128  switch(type) {
129  case SENSORS_ACTIVE:
130  if(c) {
131  if(!status(SENSORS_ACTIVE)) {
132  /* SREF_1 is Vref+ */
133  /* MemReg6 == P6.0/A0 == 5V 1 */
134  ADC12MCTL0 = (INCH_0 + SREF_0);
135  /* MemReg7 == P6.3/A3 == 5V 2 */
136  ADC12MCTL1 = (INCH_3 + SREF_0);
137  /* MemReg8 == P6.1/A1 == 3V 1 */
138  ADC12MCTL2 = (INCH_1 + SREF_0);
139  /* MemReg9 == P6.7/A7 == 3V_2 */
140  ADC12MCTL3 = (INCH_7 + SREF_0);
141 
142  sensors_activate(0x0F);
143  active = 1;
144  }
145  } else {
146  sensors_deactivate(0x0F);
147  active = 0;
148  }
149  }
150  return 0;
151 }
152 /*---------------------------------------------------------------------------*/
153 SENSORS_SENSOR(phidgets, "Phidgets", value, configure, status);