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