Contiki 2.6
|
00001 /* 00002 * Copyright (c) 2009, University of Colombo School of Computing 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 * @(#)$$ 00032 */ 00033 00034 /** 00035 * \file 00036 * Device drivers implementation for MTS300 sensor board. 00037 * \author 00038 * Kasun Hewage <kasun.ch@gmail.com> 00039 */ 00040 00041 #include "mts300.h" 00042 /*---------------------------------------------------------------------------*/ 00043 void 00044 sounder_on() 00045 { 00046 SOUNDER_DDR |= SOUNDER_MASK; 00047 SOUNDER_PORT &= ~SOUNDER_MASK; 00048 SOUNDER_PORT |= SOUNDER_MASK; 00049 } 00050 /*---------------------------------------------------------------------------*/ 00051 void 00052 sounder_off() 00053 { 00054 SOUNDER_PORT &= ~(SOUNDER_MASK); 00055 SOUNDER_DDR &= ~(SOUNDER_MASK); 00056 } 00057 /*---------------------------------------------------------------------------*/ 00058 void 00059 adc_init() 00060 { 00061 ADMUX = 0; 00062 /* AVCC with external capacitor at AREF pin. */ 00063 //ADMUX |= _BV(REFS0) 00064 /* Disable ADC interrupts. */ 00065 ADCSRA &= ~( _BV(ADIE) | _BV(ADIF) ); 00066 /* Set ADC prescaler to 64 and clear interrupt flag. */ 00067 ADCSRA |= _BV(ADPS2) | _BV(ADPS1) | _BV(ADIE); 00068 00069 } 00070 /*---------------------------------------------------------------------------*/ 00071 /* Poll based approach. The interrupt based adc is currently not used. 00072 The ADC result is right adjusted. First 8 bits(from left) are in ADCL and 00073 other two bits are in ADCH. See Atmega128 datasheet page 228. */ 00074 uint16_t 00075 get_adc(int channel) 00076 { 00077 uint16_t reading; 00078 00079 ADMUX |= (channel & 0x1F); 00080 00081 /* Disable ADC interrupts. */ 00082 ADCSRA &= ~_BV(ADIE); 00083 /* Clear previous interrupts. */ 00084 ADCSRA |= _BV(ADIF); 00085 /* Enable ADC and start conversion. */ 00086 ADCSRA |= _BV(ADEN) | _BV(ADSC); 00087 /* Wait until conversion is completed. */ 00088 while ( ADCSRA & _BV(ADSC) ); 00089 /* Get first 8 bits. */ 00090 reading = ADCL; 00091 /* Get last two bits. */ 00092 reading |= (ADCH & 3) << 8; 00093 /* Disable ADC. */ 00094 ADCSRA &= ~_BV(ADEN); 00095 return reading; 00096 } 00097 /*---------------------------------------------------------------------------*/ 00098 uint16_t 00099 get_light() 00100 { 00101 uint16_t reading; 00102 00103 /* Enable light sensor. */ 00104 LIGHT_PORT |= LIGHT_PIN_MASK; 00105 LIGHT_PORT_DDR |= LIGHT_PIN_MASK; 00106 /* Disable temperature sensor. */ 00107 TEMP_PORT_DDR &= ~TEMP_PIN_MASK; 00108 TEMP_PORT &= ~TEMP_PIN_MASK; 00109 /* Read ADC. */ 00110 reading = get_adc(LIGHT_ADC_CHANNEL); 00111 /* Disable light sensor. */ 00112 LIGHT_PORT &= ~LIGHT_PIN_MASK; 00113 LIGHT_PORT_DDR &= ~LIGHT_PIN_MASK; 00114 return reading; 00115 } 00116 /*---------------------------------------------------------------------------*/ 00117 uint16_t 00118 get_temp() 00119 { 00120 uint16_t reading; 00121 00122 /* Disable light sensor. */ 00123 LIGHT_PORT &= ~LIGHT_PIN_MASK; 00124 LIGHT_PORT_DDR &= ~LIGHT_PIN_MASK; 00125 /* Enable temperature sensor. */ 00126 TEMP_PORT_DDR |= TEMP_PIN_MASK; 00127 TEMP_PORT |= TEMP_PIN_MASK; 00128 /* Read ADC. */ 00129 reading = get_adc(TEMP_ADC_CHANNEL); 00130 /* Disable temperature sensor. */ 00131 TEMP_PORT_DDR &= ~TEMP_PIN_MASK; 00132 TEMP_PORT &= ~TEMP_PIN_MASK; 00133 00134 return reading; 00135 } 00136 /*---------------------------------------------------------------------------*/ 00137 uint16_t 00138 get_accx() 00139 { 00140 uint16_t reading; 00141 00142 /* Enable accelerometer. */ 00143 ACCEL_PORT_DDR |= ACCEL_PIN_MASK; 00144 ACCEL_PORT |= ACCEL_PIN_MASK; 00145 /* Read ADC. */ 00146 reading = get_adc(ACCELX_ADC_CHANNEL); 00147 /* Enable accelerometer. */ 00148 ACCEL_PORT_DDR &= ~ACCEL_PIN_MASK; 00149 ACCEL_PORT &= ~ACCEL_PIN_MASK; 00150 00151 return reading; 00152 } 00153 /*---------------------------------------------------------------------------*/ 00154 uint16_t 00155 get_accy() 00156 { 00157 uint16_t reading; 00158 00159 /* Enable accelerometer. */ 00160 ACCEL_PORT_DDR |= ACCEL_PIN_MASK; 00161 ACCEL_PORT |= ACCEL_PIN_MASK; 00162 /* Read ADC. */ 00163 reading = get_adc(ACCELY_ADC_CHANNEL); 00164 /* Enable accelerometer. */ 00165 ACCEL_PORT_DDR &= ~ACCEL_PIN_MASK; 00166 ACCEL_PORT &= ~ACCEL_PIN_MASK; 00167 00168 return reading; 00169 } 00170 /*---------------------------------------------------------------------------*/ 00171 uint16_t 00172 get_magx() 00173 { 00174 uint16_t reading; 00175 00176 /* Enable magnetometer. */ 00177 MAGNET_PORT_DDR |= MAGNET_PIN_MASK; 00178 MAGNET_PORT |= MAGNET_PIN_MASK; 00179 /* Read ADC. */ 00180 reading = get_adc(MAGNETX_ADC_CHANNEL); 00181 /* Enable magnetometer. */ 00182 MAGNET_PORT_DDR &= ~MAGNET_PIN_MASK; 00183 MAGNET_PORT &= ~MAGNET_PIN_MASK; 00184 00185 return reading; 00186 } 00187 /*---------------------------------------------------------------------------*/ 00188 uint16_t 00189 get_magy() 00190 { 00191 uint16_t reading; 00192 00193 /* Enable magnetometer. */ 00194 MAGNET_PORT_DDR |= MAGNET_PIN_MASK; 00195 MAGNET_PORT |= MAGNET_PIN_MASK; 00196 /* Read ADC. */ 00197 reading = get_adc(MAGNETY_ADC_CHANNEL); 00198 /* Enable magnetometer. */ 00199 MAGNET_PORT_DDR &= ~MAGNET_PIN_MASK; 00200 MAGNET_PORT &= ~MAGNET_PIN_MASK; 00201 00202 return reading; 00203 } 00204 /*---------------------------------------------------------------------------*/ 00205 uint16_t 00206 get_mic() 00207 { 00208 uint16_t reading; 00209 00210 /* Enable mic. */ 00211 MIC_PORT_DDR |= MIC_PIN_MASK; 00212 MIC_PORT |= MIC_PIN_MASK; 00213 /* Read ADC. */ 00214 reading = get_adc(MIC_ADC_CHANNEL); 00215 /* Enable mic. */ 00216 MIC_PORT_DDR &= ~MIC_PIN_MASK; 00217 MIC_PORT &= ~MIC_PIN_MASK; 00218 00219 return reading; 00220 } 00221 /*---------------------------------------------------------------------------*/ 00222