Contiki 2.6

button-sensor.c

00001 /*
00002  * Copyright (c) 2005, 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 
00032 /*
00033  * This file contains ISRs: Keep it in the HOME bank.
00034  */
00035 #include "dev/port.h"
00036 #include "dev/button-sensor.h"
00037 #include "dev/watchdog.h"
00038 /*---------------------------------------------------------------------------*/
00039 static __data struct timer debouncetimer;
00040 /*---------------------------------------------------------------------------*/
00041 /* Button 1 - SmartRT and cc2531 USb Dongle */
00042 /*---------------------------------------------------------------------------*/
00043 static
00044 int value_b1(int type)
00045 {
00046   type;
00047   return BUTTON_READ(1) || !timer_expired(&debouncetimer);
00048 }
00049 /*---------------------------------------------------------------------------*/
00050 static
00051 int status_b1(int type)
00052 {
00053   switch (type) {
00054   case SENSORS_ACTIVE:
00055   case SENSORS_READY:
00056     return BUTTON_IRQ_ENABLED(1);
00057     }
00058   return 0;
00059 }
00060 /*---------------------------------------------------------------------------*/
00061 static
00062 int configure_b1(int type, int value)
00063 {
00064   switch(type) {
00065   case SENSORS_HW_INIT:
00066 #if !MODEL_CC2531
00067     P0INP |= 2; /* Tri-state */
00068 #endif
00069     BUTTON_IRQ_ON_PRESS(1);
00070     BUTTON_FUNC_GPIO(1);
00071     BUTTON_DIR_INPUT(1);
00072     return 1;
00073   case SENSORS_ACTIVE:
00074     if(value) {
00075       if(!BUTTON_IRQ_ENABLED(1)) {
00076         timer_set(&debouncetimer, 0);
00077         BUTTON_IRQ_FLAG_OFF(1);
00078         BUTTON_IRQ_ENABLE(1);
00079       }
00080     } else {
00081       BUTTON_IRQ_DISABLE(1);
00082     }
00083     return 1;
00084   }
00085   return 0;
00086 }
00087 /*---------------------------------------------------------------------------*/
00088 /* Button 2 - cc2531 USb Dongle only */
00089 /*---------------------------------------------------------------------------*/
00090 #if MODEL_CC2531
00091 static
00092 int value_b2(int type)
00093 {
00094   type;
00095   return BUTTON_READ(2) || !timer_expired(&debouncetimer);
00096 }
00097 /*---------------------------------------------------------------------------*/
00098 static
00099 int status_b2(int type)
00100 {
00101   switch (type) {
00102   case SENSORS_ACTIVE:
00103   case SENSORS_READY:
00104     return BUTTON_IRQ_ENABLED(2);
00105     }
00106   return 0;
00107 }
00108 /*---------------------------------------------------------------------------*/
00109 static
00110 int configure_b2(int type, int value)
00111 {
00112   switch(type) {
00113   case SENSORS_HW_INIT:
00114     BUTTON_IRQ_ON_PRESS(2);
00115     BUTTON_FUNC_GPIO(2);
00116     BUTTON_DIR_INPUT(2);
00117     return 1;
00118   case SENSORS_ACTIVE:
00119     if(value) {
00120       if(!BUTTON_IRQ_ENABLED(2)) {
00121         timer_set(&debouncetimer, 0);
00122         BUTTON_IRQ_FLAG_OFF(2);
00123         BUTTON_IRQ_ENABLE(2);
00124       }
00125     } else {
00126       BUTTON_IRQ_DISABLE(2);
00127     }
00128     return 1;
00129   }
00130   return 0;
00131 }
00132 #endif
00133 /*---------------------------------------------------------------------------*/
00134 /* ISRs */
00135 /*---------------------------------------------------------------------------*/
00136 #if MODEL_CC2531
00137 void
00138 port_1_isr(void) __interrupt(P1INT_VECTOR)
00139 {
00140   EA = 0;
00141   ENERGEST_ON(ENERGEST_TYPE_IRQ);
00142 
00143   /* This ISR is for the entire port. Check if the interrupt was caused by our
00144    * button's pin. */
00145   if(BUTTON_IRQ_CHECK(1)) {
00146     if(timer_expired(&debouncetimer)) {
00147       timer_set(&debouncetimer, CLOCK_SECOND / 8);
00148       sensors_changed(&button_1_sensor);
00149     }
00150   }
00151   if(BUTTON_IRQ_CHECK(2)) {
00152     if(timer_expired(&debouncetimer)) {
00153       timer_set(&debouncetimer, CLOCK_SECOND / 8);
00154 #if CC2531_CONF_B2_REBOOTS
00155       watchdog_reboot();
00156 #else /* General Purpose */
00157       sensors_changed(&button_2_sensor);
00158 #endif
00159     }
00160   }
00161 
00162   BUTTON_IRQ_FLAG_OFF(1);
00163   BUTTON_IRQ_FLAG_OFF(2);
00164 
00165   ENERGEST_OFF(ENERGEST_TYPE_IRQ);
00166   EA = 1;
00167 }
00168 #else
00169 void
00170 port_0_isr(void) __interrupt(P0INT_VECTOR)
00171 {
00172   EA = 0;
00173   ENERGEST_ON(ENERGEST_TYPE_IRQ);
00174 
00175   /* This ISR is for the entire port. Check if the interrupt was caused by our
00176    * button's pin. */
00177   if(BUTTON_IRQ_CHECK(1)) {
00178     if(timer_expired(&debouncetimer)) {
00179       timer_set(&debouncetimer, CLOCK_SECOND / 8);
00180       sensors_changed(&button_sensor);
00181     }
00182   }
00183 
00184   BUTTON_IRQ_FLAG_OFF(1);
00185 
00186   ENERGEST_OFF(ENERGEST_TYPE_IRQ);
00187   EA = 1;
00188 }
00189 #endif
00190 /*---------------------------------------------------------------------------*/
00191 SENSORS_SENSOR(button_1_sensor, BUTTON_SENSOR, value_b1, configure_b1, status_b1);
00192 #if MODEL_CC2531
00193 SENSORS_SENSOR(button_2_sensor, BUTTON_SENSOR, value_b2, configure_b2, status_b2);
00194 #endif