Contiki 2.6
|
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 00043 #define ADC12MCTL_NO(adcno) ((unsigned char *) ADC12MCTL0_)[adcno] 00044 00045 static uint16_t adc_on; 00046 static uint16_t ready; 00047 /*---------------------------------------------------------------------------*/ 00048 static CC_INLINE void 00049 start(void) 00050 { 00051 uint16_t c, last; 00052 00053 /* Set up the ADC. */ 00054 P6DIR = 0xff; 00055 P6OUT = 0x00; 00056 00057 /* Setup ADC12, ref., sampling time */ 00058 /* XXX Note according to the specification a minimum of 17 ms should 00059 be allowed after turn on of the internal reference generator. */ 00060 ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC + REFON; 00061 /* Use sampling timer, repeat-sequence-of-channels */ 00062 ADC12CTL1 = SHP + CONSEQ_3; 00063 00064 last = 15; 00065 for(c = 0; c < 16; c++) { 00066 /* Clear all end-of-sequences */ 00067 ADC12MCTL_NO(c) &= ~EOS; 00068 if(adc_on & (1 << c)) { 00069 if(last == 15) { 00070 /* Set new start of sequence to lowest active memory holder */ 00071 ADC12CTL1 |= (c * CSTARTADD_1); 00072 } 00073 last = c; 00074 } 00075 } 00076 00077 /* Set highest end-of-sequence. */ 00078 ADC12MCTL_NO(last) |= EOS; 00079 00080 ADC12CTL0 |= ADC12ON; 00081 ADC12CTL0 |= ENC; /* enable conversion */ 00082 ADC12CTL0 |= ADC12SC; /* sample & convert */ 00083 } 00084 /*---------------------------------------------------------------------------*/ 00085 static CC_INLINE void 00086 stop(void) 00087 { 00088 /* stop converting immediately, turn off reference voltage, etc. */ 00089 00090 ADC12CTL0 &= ~ENC; 00091 /* need to remove CONSEQ_3 if not EOS is configured */ 00092 ADC12CTL1 &= ~CONSEQ_3; 00093 00094 /* wait for conversion to stop */ 00095 while(ADC12CTL1 & ADC12BUSY); 00096 00097 /* clear any pending interrupts */ 00098 ADC12IFG = 0; 00099 } 00100 /*---------------------------------------------------------------------------*/ 00101 int 00102 sky_sensors_status(uint16_t input, int type) 00103 { 00104 if(type == SENSORS_ACTIVE) { 00105 return (adc_on & input) == input; 00106 } 00107 if(type == SENSORS_READY) { 00108 ready |= ADC12IFG & adc_on & input; 00109 return (ready & adc_on & input) == input; 00110 } 00111 return 0; 00112 } 00113 /*---------------------------------------------------------------------------*/ 00114 int 00115 sky_sensors_configure(uint16_t input, uint8_t ref, int type, int value) 00116 { 00117 uint16_t c; 00118 00119 if(type == SENSORS_ACTIVE) { 00120 stop(); 00121 00122 if(value) { 00123 adc_on |= input; 00124 P6SEL |= input & 0xff; 00125 00126 /* Set ADC config */ 00127 for(c = 0; c < 16; c++) { 00128 if(input & (1 << c)) { 00129 ADC12MCTL_NO(c) = (c * INCH_1) | ref; 00130 } 00131 } 00132 00133 } else { 00134 adc_on &= ~input; 00135 ready &= ~input; 00136 P6SEL &= ~(input & 0xff); 00137 } 00138 00139 if(adc_on == 0) { 00140 P6DIR = 0x00; 00141 P6SEL = 0x00; 00142 00143 /* Turn off ADC and internal reference generator */ 00144 ADC12CTL0 = 0; 00145 ADC12CTL1 = 0; 00146 } else { 00147 start(); 00148 } 00149 return 1; 00150 } 00151 return 0; 00152 } 00153 /*---------------------------------------------------------------------------*/