Contiki 3.x
button-sensor.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
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 copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  */
31 
32 /*
33  * This file contains ISRs: Keep it in the HOME bank.
34  */
35 #include "dev/port.h"
36 #include "dev/button-sensor.h"
37 #include "dev/watchdog.h"
38 /*---------------------------------------------------------------------------*/
39 static CC_AT_DATA struct timer debouncetimer;
40 /*---------------------------------------------------------------------------*/
41 /* Button 1 - SmartRF and cc2531 USB Dongle */
42 /*---------------------------------------------------------------------------*/
43 static int
44 value_b1(int type)
45 {
46  type;
47  return BUTTON_READ(1) || !timer_expired(&debouncetimer);
48 }
49 /*---------------------------------------------------------------------------*/
50 static int
51 status_b1(int type)
52 {
53  switch(type) {
54  case SENSORS_ACTIVE:
55  case SENSORS_READY:
56  return BUTTON_IRQ_ENABLED(1);
57  }
58  return 0;
59 }
60 /*---------------------------------------------------------------------------*/
61 static int
62 configure_b1(int type, int value)
63 {
64  switch(type) {
65  case SENSORS_HW_INIT:
66 #if !MODELS_CONF_CC2531_USB_STICK
67  P0INP |= 2; /* Tri-state */
68 #endif
69  BUTTON_IRQ_ON_PRESS(1);
70  BUTTON_FUNC_GPIO(1);
71  BUTTON_DIR_INPUT(1);
72  return 1;
73  case SENSORS_ACTIVE:
74  if(value) {
75  if(!BUTTON_IRQ_ENABLED(1)) {
76  timer_set(&debouncetimer, 0);
77  BUTTON_IRQ_FLAG_OFF(1);
78  BUTTON_IRQ_ENABLE(1);
79  }
80  } else {
81  BUTTON_IRQ_DISABLE(1);
82  }
83  return 1;
84  }
85  return 0;
86 }
87 /*---------------------------------------------------------------------------*/
88 /* Button 2 - cc2531 USb Dongle only */
89 /*---------------------------------------------------------------------------*/
90 #if MODELS_CONF_CC2531_USB_STICK
91 static int
92 value_b2(int type)
93 {
94  type;
95  return BUTTON_READ(2) || !timer_expired(&debouncetimer);
96 }
97 /*---------------------------------------------------------------------------*/
98 static int
99 status_b2(int type)
100 {
101  switch(type) {
102  case SENSORS_ACTIVE:
103  case SENSORS_READY:
104  return BUTTON_IRQ_ENABLED(2);
105  }
106  return 0;
107 }
108 /*---------------------------------------------------------------------------*/
109 static int
110 configure_b2(int type, int value)
111 {
112  switch(type) {
113  case SENSORS_HW_INIT:
114  BUTTON_IRQ_ON_PRESS(2);
115  BUTTON_FUNC_GPIO(2);
116  BUTTON_DIR_INPUT(2);
117  return 1;
118  case SENSORS_ACTIVE:
119  if(value) {
120  if(!BUTTON_IRQ_ENABLED(2)) {
121  timer_set(&debouncetimer, 0);
122  BUTTON_IRQ_FLAG_OFF(2);
123  BUTTON_IRQ_ENABLE(2);
124  }
125  } else {
126  BUTTON_IRQ_DISABLE(2);
127  }
128  return 1;
129  }
130  return 0;
131 }
132 #endif
133 /*---------------------------------------------------------------------------*/
134 /* ISRs */
135 /*---------------------------------------------------------------------------*/
136 /* avoid referencing bits, we don't call code which use them */
137 #pragma save
138 #if CC_CONF_OPTIMIZE_STACK_SIZE
139 #pragma exclude bits
140 #endif
141 #if MODELS_CONF_CC2531_USB_STICK
142 void
143 port_1_isr(void) __interrupt(P1INT_VECTOR)
144 {
145  EA = 0;
146  ENERGEST_ON(ENERGEST_TYPE_IRQ);
147 
148  /* This ISR is for the entire port. Check if the interrupt was caused by our
149  * button's pin. */
150  if(BUTTON_IRQ_CHECK(1)) {
151  if(timer_expired(&debouncetimer)) {
152  timer_set(&debouncetimer, CLOCK_SECOND / 8);
153  sensors_changed(&button_1_sensor);
154  }
155  }
156  if(BUTTON_IRQ_CHECK(2)) {
157  if(timer_expired(&debouncetimer)) {
158  timer_set(&debouncetimer, CLOCK_SECOND / 8);
159 #if CC2531_CONF_B2_REBOOTS
160  watchdog_reboot();
161 #else /* General Purpose */
162  sensors_changed(&button_2_sensor);
163 #endif
164  }
165  }
166 
167  BUTTON_IRQ_FLAG_OFF(1);
168  BUTTON_IRQ_FLAG_OFF(2);
169 
170  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
171  EA = 1;
172 }
173 #else
174 void
175 port_0_isr(void) __interrupt(P0INT_VECTOR)
176 {
177  EA = 0;
178  ENERGEST_ON(ENERGEST_TYPE_IRQ);
179 
180  /* This ISR is for the entire port. Check if the interrupt was caused by our
181  * button's pin. */
182  if(BUTTON_IRQ_CHECK(1)) {
183  if(timer_expired(&debouncetimer)) {
184  timer_set(&debouncetimer, CLOCK_SECOND / 8);
185  sensors_changed(&button_sensor);
186  }
187  }
188 
189  BUTTON_IRQ_FLAG_OFF(1);
190 
191  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
192  EA = 1;
193 }
194 #endif
195 #pragma restore
196 /*---------------------------------------------------------------------------*/
197 SENSORS_SENSOR(button_1_sensor, BUTTON_SENSOR, value_b1, configure_b1, status_b1);
198 #if MODELS_CONF_CC2531_USB_STICK
199 SENSORS_SENSOR(button_2_sensor, BUTTON_SENSOR, value_b2, configure_b2, status_b2);
200 #endif
A timer.
Definition: timer.h:86
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
void watchdog_reboot(void)
Keeps control until the WDT throws a reset signal.
Definition: watchdog.c:128
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:121
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82