Contiki 2.6

button-sensor.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, STMicroelectronics.
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
00011  *    copyright notice, this list of conditions and the following
00012  *    disclaimer in the documentation and/or other materials provided
00013  *    with the distribution.
00014  * 3. The name of the author may not be used to endorse or promote
00015  *    products derived from this software without specific prior
00016  *    written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00019  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00022  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00024  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * This file is part of the Contiki OS
00031  *
00032  * $Id: button-sensor.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $
00033  */
00034 /*---------------------------------------------------------------------------*/
00035 /**
00036 * \file
00037 *                       Button sensor.
00038 * \author
00039 *                       Salvatore Pitrulli <salvopitru@users.sourceforge.net>
00040 */
00041 /*---------------------------------------------------------------------------*/
00042 
00043 #include "dev/button-sensor.h"
00044 #include "hal/micro/micro-common.h"
00045 #include "hal/micro/cortexm3/micro-common.h"
00046 
00047 #include BOARD_HEADER
00048 
00049 #define DEBOUNCE 1
00050 
00051 /**
00052  * @brief Port and pin for BUTTON0.
00053  */
00054 #undef  BUTTON_S1
00055 #define BUTTON_S1             PORTA_PIN(7)
00056 #define BUTTON_S1_INPUT_GPIO  BUTTON_INPUT_GPIO(PORTA)
00057 #define BUTTON_S1_GPIO_PIN    7
00058 #define BUTTON_S1_OUTPUT_GPIO GPIO_PAOUT
00059 
00060 /**
00061  * @brief Point the proper IRQ at the desired pin for BUTTON0.
00062  */
00063 #define BUTTON_S1_SEL()       do { GPIO_IRQCSEL = BUTTON_S1; } while(0)
00064 /**
00065  * @brief The interrupt service routine for BUTTON_S1.
00066  */
00067 #define BUTTON_S1_ISR         halIrqCIsr
00068 /**
00069  * @brief The interrupt configuration register for BUTTON_S1.
00070  */
00071 #define BUTTON_S1_INTCFG      GPIO_INTCFGC
00072 /**
00073  * @brief The interrupt bit for BUTTON_S1.
00074  */
00075 #define BUTTON_S1_INT_EN_BIT  INT_IRQC
00076 /**
00077  * @brief The interrupt bit for BUTTON_S1.
00078  */
00079 #define BUTTON_S1_FLAG_BIT    INT_IRQCFLAG
00080 /**
00081  * @brief The missed interrupt bit for BUTTON_S1.
00082  */
00083 #define BUTTON_S1_MISS_BIT    INT_MISSIRQC
00084 
00085 #if DEBOUNCE
00086 static struct timer debouncetimer;
00087 #endif
00088 
00089 #define FALSE 0
00090 #define TRUE  1
00091 
00092 /*---------------------------------------------------------------------------*/
00093 static void
00094 init(void)
00095 {
00096   #if DEBOUNCE
00097   timer_set(&debouncetimer, 0);
00098   #endif
00099   
00100   /* Configure GPIO for BUTTONSs */
00101   
00102   //Input, pulled up or down (selected by GPIO_PxOUT: 0 = pull-down, 1 = pull-up).  
00103   halGpioConfig(BUTTON_S1,GPIOCFG_IN_PUD);
00104   BUTTON_S1_OUTPUT_GPIO |= GPIOOUT_PULLUP << BUTTON_S1_GPIO_PIN;
00105   
00106   
00107   BUTTON_S1_SEL();
00108   BUTTON_S1_INTCFG = 0x40;  // Falling edge triggered.  
00109   
00110 }
00111 /*---------------------------------------------------------------------------*/
00112 static void
00113 activate(void)
00114 {
00115   INT_CFGSET = BUTTON_S1_INT_EN_BIT;
00116 }
00117 /*---------------------------------------------------------------------------*/
00118 static void
00119 deactivate(void)
00120 {
00121   INT_CFGCLR = BUTTON_S1_INT_EN_BIT;
00122 }
00123 /*---------------------------------------------------------------------------*/
00124 static int
00125 active(void)
00126 {
00127   return (INT_CFGSET & BUTTON_S1_INT_EN_BIT) ? TRUE : FALSE ;
00128 }
00129 /*---------------------------------------------------------------------------*/
00130 static int
00131 value(int type)
00132 {
00133 #if DEBOUNCE
00134   return (BUTTON_S1_INPUT_GPIO & (1<<BUTTON_S1_GPIO_PIN)) || !timer_expired(&debouncetimer);
00135 #else
00136   return BUTTON_S1_INPUT_GPIO & (1<<BUTTON_S1_GPIO_PIN);
00137 #endif
00138 }
00139 /*---------------------------------------------------------------------------*/
00140 static int
00141 configure(int type, int value)
00142 {
00143   switch(type){
00144     case SENSORS_HW_INIT:
00145       init();
00146       return 1;
00147     case SENSORS_ACTIVE:
00148       if(value)        
00149         activate();
00150       else
00151         deactivate();
00152       return 1;
00153   }
00154        
00155   return 0;
00156 }
00157 /*---------------------------------------------------------------------------*/
00158 static int
00159 status(int type)
00160 {
00161   switch(type) {
00162     
00163     case SENSORS_READY:
00164       return active();
00165   }
00166   
00167   return 0;
00168 }
00169 /*---------------------------------------------------------------------------*/
00170 void BUTTON_S1_ISR(void)
00171 {
00172   
00173   ENERGEST_ON(ENERGEST_TYPE_IRQ);
00174   
00175   //sensors_handle_irq(IRQ_BUTTON);
00176   
00177    if(INT_GPIOFLAG & BUTTON_S1_FLAG_BIT) {
00178     
00179 #if DEBOUNCE
00180     if(timer_expired(&debouncetimer)) {
00181       timer_set(&debouncetimer, CLOCK_SECOND / 5);
00182       sensors_changed(&button_sensor);
00183     }
00184 #else
00185     sensors_changed(&button_sensor);
00186 #endif
00187     
00188   }
00189   
00190   INT_GPIOFLAG = BUTTON_S1_FLAG_BIT;  
00191   
00192   ENERGEST_OFF(ENERGEST_TYPE_IRQ);  
00193 }
00194 /*---------------------------------------------------------------------------*/
00195 SENSORS_SENSOR(button_sensor, BUTTON_SENSOR,
00196                value, configure, status);
00197