Contiki 3.x
button-sensor.c
1 /**
2  * Copyright (c) 2014, Analog Devices, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted (subject to the limitations in the
6  * disclaimer below) provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * - Neither the name of Analog Devices, Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
21  * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /**
35  * \author Jim Paris <jim.paris@rigado.com>
36  */
37 
38 #include "lib/sensors.h"
39 #include "dev/button-sensor.h"
40 #include "platform-conf.h"
41 
42 #include <signal.h>
43 
44 const struct sensors_sensor button_sensor;
45 static struct timer debouncetimer;
46 
47 /* e.g. pADI_GP0 */
48 #define GPIO CC_CONCAT(pADI_GP, BUTTON_GPIO)
49 
50 /* e.g. Ext_Int2_Handler */
51 #define HANDLER CC_CONCAT(CC_CONCAT(Ext_Int, BUTTON_INT), _Handler)
52 
53 /* e.g. EINT2_IRQn */
54 #define IRQN CC_CONCAT(CC_CONCAT(EINT, BUTTON_INT), _IRQn)
55 
56 void
57 HANDLER(void)
58 {
59  pADI_INTERRUPT->EICLR = (1 << BUTTON_INT);
60  if(!timer_expired(&debouncetimer)) {
61  return;
62  }
63  timer_set(&debouncetimer, CLOCK_SECOND / 8);
64  sensors_changed(&button_sensor);
65 }
66 static int
67 config(int type, int c)
68 {
69  switch(type) {
70  case SENSORS_HW_INIT:
71  timer_set(&debouncetimer, 0);
72  return 1;
73  case SENSORS_ACTIVE:
74  /* Set button as a GPIO input with pullup */
75  GPIO->GPCON &= ~(3UL << (BUTTON_PIN * 2));
76  GPIO->GPOEN &= ~(1UL << BUTTON_PIN);
77  GPIO->GPPUL |= (1UL << BUTTON_PIN);
78 
79  /* Enable interrupt for the button (rise and fall) */
80 #if BUTTON_INT <= 3
81  pADI_INTERRUPT->EI0CFG |= (0xaUL << (4 * (BUTTON_INT - 0)));
82 #elif BUTTON_INT <= 7
83  pADI_INTERRUPT->EI1CFG |= (0xaUL << (4 * (BUTTON_INT - 4)));
84 #endif
85  pADI_INTERRUPT->EICLR = (1 << BUTTON_INT);
86  NVIC_EnableIRQ(IRQN);
87  return 1;
88  }
89  return 0;
90 }
91 static int
92 status(int type)
93 {
94  switch(type) {
95  case SENSORS_ACTIVE:
96  case SENSORS_READY:
97  if(GPIO->GPIN & (1UL << BUTTON_PIN)) {
98  return 1;
99  }
100  return 0;
101  }
102  return 0;
103 }
104 SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, NULL, config, status);
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
Enable External Interrupt.
Definition: core_cm0.h:535
A timer.
Definition: timer.h:86
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
#define NULL
The null pointer.
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