00001 /* 00002 * Copyright (c) 2010, Swedish Institute of Computer Science. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the Institute nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 * 00029 * This file is part of the Contiki operating system. 00030 * 00031 * $Id: sky-sensors.c,v 1.3 2010/08/25 19:30:53 nifi Exp $ 00032 * 00033 * ----------------------------------------------------------------- 00034 * 00035 * Author : Joakim Eriksson 00036 * Created : 2010-02-02 00037 * Updated : $Date: 2010/08/25 19:30:53 $ 00038 * $Revision: 1.3 $ 00039 */ 00040 #include "contiki.h" 00041 #include "lib/sensors.h" 00042 #include <io.h> 00043 00044 #define ADC12MCTL_NO(adcno) ((unsigned char *) ADC12MCTL0_)[adcno] 00045 00046 static uint16_t adc_on; 00047 static uint16_t ready; 00048 /*---------------------------------------------------------------------------*/ 00049 static CC_INLINE void 00050 start(void) 00051 { 00052 uint16_t c, last; 00053 00054 /* Set up the ADC. */ 00055 P6DIR = 0xff; 00056 P6OUT = 0x00; 00057 00058 /* Setup ADC12, ref., sampling time */ 00059 /* XXX Note according to the specification a minimum of 17 ms should 00060 be allowed after turn on of the internal reference generator. */ 00061 ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC + REFON; 00062 /* Use sampling timer, repeat-sequence-of-channels */ 00063 ADC12CTL1 = SHP + CONSEQ_3; 00064 00065 last = 15; 00066 for(c = 0; c < 16; c++) { 00067 /* Clear all end-of-sequences */ 00068 ADC12MCTL_NO(c) &= ~EOS; 00069 if(adc_on & (1 << c)) { 00070 if(last == 15) { 00071 /* Set new start of sequence to lowest active memory holder */ 00072 ADC12CTL1 |= (c * CSTARTADD_1); 00073 } 00074 last = c; 00075 } 00076 } 00077 00078 /* Set highest end-of-sequence. */ 00079 ADC12MCTL_NO(last) |= EOS; 00080 00081 ADC12CTL0 |= ADC12ON; 00082 ADC12CTL0 |= ENC; /* enable conversion */ 00083 ADC12CTL0 |= ADC12SC; /* sample & convert */ 00084 } 00085 /*---------------------------------------------------------------------------*/ 00086 static CC_INLINE void 00087 stop(void) 00088 { 00089 /* stop converting immediately, turn off reference voltage, etc. */ 00090 00091 ADC12CTL0 &= ~ENC; 00092 /* need to remove CONSEQ_3 if not EOS is configured */ 00093 ADC12CTL1 &= ~CONSEQ_3; 00094 00095 /* wait for conversion to stop */ 00096 while(ADC12CTL1 & ADC12BUSY); 00097 00098 /* clear any pending interrupts */ 00099 ADC12IFG = 0; 00100 } 00101 /*---------------------------------------------------------------------------*/ 00102 int 00103 sky_sensors_status(uint16_t input, int type) 00104 { 00105 if(type == SENSORS_ACTIVE) { 00106 return (adc_on & input) == input; 00107 } 00108 if(type == SENSORS_READY) { 00109 ready |= ADC12IFG & adc_on & input; 00110 return (ready & adc_on & input) == input; 00111 } 00112 return 0; 00113 } 00114 /*---------------------------------------------------------------------------*/ 00115 int 00116 sky_sensors_configure(uint16_t input, uint8_t ref, int type, int value) 00117 { 00118 uint16_t c; 00119 00120 if(type == SENSORS_ACTIVE) { 00121 stop(); 00122 00123 if(value) { 00124 adc_on |= input; 00125 P6SEL |= input & 0xff; 00126 00127 /* Set ADC config */ 00128 for(c = 0; c < 16; c++) { 00129 if(input & (1 << c)) { 00130 ADC12MCTL_NO(c) = (c * INCH_1) | ref; 00131 } 00132 } 00133 00134 } else { 00135 adc_on &= ~input; 00136 ready &= ~input; 00137 P6SEL &= ~(input & 0xff); 00138 } 00139 00140 if(adc_on == 0) { 00141 P6DIR = 0x00; 00142 P6SEL = 0x00; 00143 00144 /* Turn off ADC and internal reference generator */ 00145 ADC12CTL0 = 0; 00146 ADC12CTL1 = 0; 00147 } else { 00148 start(); 00149 } 00150 return 1; 00151 } 00152 return 0; 00153 } 00154 /*---------------------------------------------------------------------------*/