Contiki 2.6

adc-sensor.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, George Oikonomou - <oikonomou@users.sourceforge.net>
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  * \file
00034  *         ADC sensor module for TI SmartRF05EB devices.
00035  *
00036  * \author
00037  *         George Oikonomou - <oikonomou@users.sourceforge.net>
00038  */
00039 #include "sfr-bits.h"
00040 #include "cc253x.h"
00041 #include "adc-sensor.h"
00042 
00043 #if ADC_SENSOR_ON
00044 /*---------------------------------------------------------------------------*/
00045 static int
00046 value(int type)
00047 {
00048   uint16_t reading;
00049   /*
00050    * For single-shot AD conversions, we may only write to ADCCON3[3:0] once
00051    * (This write triggers the conversion). We thus use the variable 'command'
00052    * to store intermediate steps (reference, decimation rate, input channel)
00053    */
00054   uint8_t command;
00055 
00056   /* 1.25V ref, max decimation rate */
00057   command = ADCCON3_EDIV1 | ADCCON3_EDIV0;
00058 
00059   /* Clear the Interrupt Flag */
00060   ADCIF = 0;
00061 
00062   /* Depending on the desired reading, append the input bits to 'command' and
00063    * enable the corresponding input channel in ADCCFG if necessary */
00064   switch(type) {
00065 #if TEMP_SENSOR_ON
00066   case ADC_SENSOR_TYPE_TEMP:
00067     command |= ADCCON3_ECH3 | ADCCON3_ECH2 | ADCCON3_ECH1;
00068     break;
00069 #endif
00070 #if VDD_SENSOR_ON
00071   case ADC_SENSOR_TYPE_VDD:
00072     command |= ADCCON3_ECH3 | ADCCON3_ECH2 | ADCCON3_ECH1 | ADCCON3_ECH0;
00073     break;
00074 #endif
00075   default:
00076     /* If the sensor is not present or disabled in conf, return -1 */
00077     return -1;
00078   }
00079 
00080   /* Writing in bits 3:0 of ADCCON3 will trigger a single conversion */
00081   ADCCON3 = command;
00082 
00083   /*
00084    * When the conversion is complete, the ADC interrupt flag is set. We don't
00085    * use an ISR here, we just wait on the flag and clear it afterwards.
00086    */
00087   while(!ADCIF);
00088 
00089   /* Clear the Interrupt Flag */
00090   ADCIF = 0;
00091 
00092   reading = ADCL;
00093   reading |= (((uint8_t) ADCH) << 8);
00094   /* 12-bit decimation rate: 4 LS bits are noise */
00095   reading >>= 4;
00096 
00097   return reading;
00098 }
00099 /*---------------------------------------------------------------------------*/
00100 static int
00101 status(int type)
00102 {
00103   return 1;
00104 }
00105 /*---------------------------------------------------------------------------*/
00106 static int
00107 configure(int type, int value)
00108 {
00109   switch(type) {
00110   case SENSORS_HW_INIT:
00111 #if TEMP_SENSOR_ON
00112     /* Connect temperature sensor to the SoC */
00113     ATEST = 1;
00114     TESTREG0 = 1;
00115 #endif
00116     APCFG = 0; /* Disables Input Channels */
00117     break;
00118   }
00119   return 1;
00120 }
00121 /*---------------------------------------------------------------------------*/
00122 SENSORS_SENSOR(adc_sensor, ADC_SENSOR, value, configure, status);
00123 #endif /* ADC_SENSOR_ON */