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