Contiki 2.5
button-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, STMicroelectronics.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the Contiki OS
31  *
32  * $Id: button-sensor.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $
33  */
34 /*---------------------------------------------------------------------------*/
35 /**
36 * \file
37 * Button sensor.
38 * \author
39 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
40 */
41 /*---------------------------------------------------------------------------*/
42 
43 #include "dev/button-sensor.h"
44 #include "hal/micro/micro-common.h"
46 
47 #include BOARD_HEADER
48 
49 #define DEBOUNCE 1
50 
51 /**
52  * @brief Port and pin for BUTTON0.
53  */
54 #undef BUTTON_S1
55 #define BUTTON_S1 PORTA_PIN(7)
56 #define BUTTON_S1_INPUT_GPIO BUTTON_INPUT_GPIO(PORTA)
57 #define BUTTON_S1_GPIO_PIN 7
58 #define BUTTON_S1_OUTPUT_GPIO GPIO_PAOUT
59 
60 /**
61  * @brief Point the proper IRQ at the desired pin for BUTTON0.
62  */
63 #define BUTTON_S1_SEL() do { GPIO_IRQCSEL = BUTTON_S1; } while(0)
64 /**
65  * @brief The interrupt service routine for BUTTON_S1.
66  */
67 #define BUTTON_S1_ISR halIrqCIsr
68 /**
69  * @brief The interrupt configuration register for BUTTON_S1.
70  */
71 #define BUTTON_S1_INTCFG GPIO_INTCFGC
72 /**
73  * @brief The interrupt bit for BUTTON_S1.
74  */
75 #define BUTTON_S1_INT_EN_BIT INT_IRQC
76 /**
77  * @brief The interrupt bit for BUTTON_S1.
78  */
79 #define BUTTON_S1_FLAG_BIT INT_IRQCFLAG
80 /**
81  * @brief The missed interrupt bit for BUTTON_S1.
82  */
83 #define BUTTON_S1_MISS_BIT INT_MISSIRQC
84 
85 #if DEBOUNCE
86 static struct timer debouncetimer;
87 #endif
88 
89 #define FALSE 0
90 #define TRUE 1
91 
92 /*---------------------------------------------------------------------------*/
93 static void
94 init(void)
95 {
96  #if DEBOUNCE
97  timer_set(&debouncetimer, 0);
98  #endif
99 
100  /* Configure GPIO for BUTTONSs */
101 
102  //Input, pulled up or down (selected by GPIO_PxOUT: 0 = pull-down, 1 = pull-up).
103  halGpioConfig(BUTTON_S1,GPIOCFG_IN_PUD);
104  BUTTON_S1_OUTPUT_GPIO |= GPIOOUT_PULLUP << BUTTON_S1_GPIO_PIN;
105 
106 
107  BUTTON_S1_SEL();
108  BUTTON_S1_INTCFG = 0x40; // Falling edge triggered.
109 
110 }
111 /*---------------------------------------------------------------------------*/
112 static void
113 activate(void)
114 {
115  INT_CFGSET = BUTTON_S1_INT_EN_BIT;
116 }
117 /*---------------------------------------------------------------------------*/
118 static void
119 deactivate(void)
120 {
121  INT_CFGCLR = BUTTON_S1_INT_EN_BIT;
122 }
123 /*---------------------------------------------------------------------------*/
124 static int
125 active(void)
126 {
127  return (INT_CFGSET & BUTTON_S1_INT_EN_BIT) ? TRUE : FALSE ;
128 }
129 /*---------------------------------------------------------------------------*/
130 static int
131 value(int type)
132 {
133 #if DEBOUNCE
134  return (BUTTON_S1_INPUT_GPIO & (1<<BUTTON_S1_GPIO_PIN)) || !timer_expired(&debouncetimer);
135 #else
136  return BUTTON_S1_INPUT_GPIO & (1<<BUTTON_S1_GPIO_PIN);
137 #endif
138 }
139 /*---------------------------------------------------------------------------*/
140 static int
141 configure(int type, int value)
142 {
143  switch(type){
144  case SENSORS_HW_INIT:
145  init();
146  return 1;
147  case SENSORS_ACTIVE:
148  if(value)
149  activate();
150  else
151  deactivate();
152  return 1;
153  }
154 
155  return 0;
156 }
157 /*---------------------------------------------------------------------------*/
158 static int
159 status(int type)
160 {
161  switch(type) {
162 
163  case SENSORS_READY:
164  return active();
165  }
166 
167  return 0;
168 }
169 /*---------------------------------------------------------------------------*/
170 void BUTTON_S1_ISR(void)
171 {
172 
173  ENERGEST_ON(ENERGEST_TYPE_IRQ);
174 
175  //sensors_handle_irq(IRQ_BUTTON);
176 
177  if(INT_GPIOFLAG & BUTTON_S1_FLAG_BIT) {
178 
179 #if DEBOUNCE
180  if(timer_expired(&debouncetimer)) {
181  timer_set(&debouncetimer, CLOCK_SECOND / 5);
182  sensors_changed(&button_sensor);
183  }
184 #else
185  sensors_changed(&button_sensor);
186 #endif
187 
188  }
189 
190  INT_GPIOFLAG = BUTTON_S1_FLAG_BIT;
191 
192  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
193 }
194 /*---------------------------------------------------------------------------*/
195 SENSORS_SENSOR(button_sensor, BUTTON_SENSOR,
196  value, configure, status);
197