Contiki 2.6

acc-sensor.c

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  * -----------------------------------------------------------------
00030  *
00031  * Author  : Adam Dunkels, Joakim Eriksson, Niclas Finne
00032  * Created : 2005-11-01
00033  */
00034 
00035 #include "dev/acc-sensor.h"
00036 
00037 const struct sensors_sensor acc_sensor;
00038 static uint8_t active;
00039 
00040 /*---------------------------------------------------------------------------*/
00041 static void
00042 activate(void)
00043 {
00044   /* This assumes that some other sensor system already did setup the ADC
00045   /* (in the case of the sky platform it is sensors_light_init that does it)
00046 
00047   P6SEL |= 0x70;
00048   P6DIR = 0x00;
00049   P6OUT = 0x00;
00050 
00051   P2DIR |= 0x48;
00052   P2OUT |= 0x48;
00053 
00054 
00055   /* stop converting immediately
00056   ADC12CTL0 &= ~ENC;
00057   ADC12CTL1 &= ~CONSEQ_3;
00058 
00059   /* Configure ADC12_2 to sample channel 11 (voltage) and use
00060   /* the Vref+ as reference (SREF_1) since it is a stable reference
00061   ADC12MCTL2 = (INCH_4 + SREF_1);
00062   ADC12MCTL3 = (INCH_5 + SREF_1);
00063   ADC12MCTL4 = (INCH_6 + SREF_1);
00064   /* internal temperature can be read as value(3)
00065   ADC12MCTL5 = (INCH_10 + SREF_1);
00066 
00067   ADC12CTL1 |= CONSEQ_3;
00068   ADC12CTL0 |= ENC | ADC12SC;
00069 
00070   /*  Irq_adc12_activate(&acc_sensor, 6, (INCH_11 + SREF_1)); */
00071   active = 1;
00072 }
00073 /*---------------------------------------------------------------------------*/
00074 static void
00075 deactivate(void)
00076 {
00077   /*  irq_adc12_deactivate(&acc_sensor, 6);
00078       acc_value = 0;*/
00079   active = 0;
00080 }
00081 /*---------------------------------------------------------------------------*/
00082 static int
00083 value(int type)
00084 {
00085 /*
00086   switch(type) {
00087   case 0:
00088     return ADC12MEM2;
00089   case 1:
00090     return ADC12MEM3;
00091   case 2:
00092     return ADC12MEM4;
00093   case 3:
00094     return ADC12MEM5;
00095   }*/
00096   return 0;
00097 }
00098 /*---------------------------------------------------------------------------*/
00099 static int
00100 configure(int type, int value)
00101 {
00102   if(type == SENSORS_ACTIVE) {
00103     if(value) {
00104       activate();
00105     } else {
00106       deactivate();
00107     }
00108     return 1;
00109   }
00110   return 0;
00111 }
00112 /*---------------------------------------------------------------------------*/
00113 static int
00114 status(int type)
00115 {
00116   switch (type) {
00117   case SENSORS_ACTIVE:
00118   case SENSORS_READY:
00119     return active;
00120   default:
00121     return 0;
00122   }
00123 }
00124 /*---------------------------------------------------------------------------*/
00125 SENSORS_SENSOR(acc_sensor, ACC_SENSOR,
00126                value, configure, status);