Contiki 3.x
button-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Texas Instruments Incorporated - http://www.ti.com/
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 copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*---------------------------------------------------------------------------*/
31 /**
32  * \addtogroup launchpad-button-sensor
33  * @{
34  *
35  * \file
36  * Driver for LaunchPad buttons
37  */
38 /*---------------------------------------------------------------------------*/
39 #include "contiki.h"
40 #include "lib/sensors.h"
42 #include "gpio-interrupt.h"
43 #include "sys/timer.h"
44 #include "lpm.h"
45 
46 #include "ti-lib.h"
47 
48 #include <stdint.h>
49 /*---------------------------------------------------------------------------*/
50 #ifdef BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN
51 #define BUTTON_SENSOR_ENABLE_SHUTDOWN BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN
52 #else
53 #define BUTTON_SENSOR_ENABLE_SHUTDOWN 1
54 #endif
55 /*---------------------------------------------------------------------------*/
56 #define BUTTON_GPIO_CFG (IOC_CURRENT_2MA | IOC_STRENGTH_AUTO | \
57  IOC_IOPULL_UP | IOC_SLEW_DISABLE | \
58  IOC_HYST_DISABLE | IOC_BOTH_EDGES | \
59  IOC_INT_ENABLE | IOC_IOMODE_NORMAL | \
60  IOC_NO_WAKE_UP | IOC_INPUT_ENABLE)
61 /*---------------------------------------------------------------------------*/
62 #define DEBOUNCE_DURATION (CLOCK_SECOND >> 5)
63 
64 struct btn_timer {
65  struct timer debounce;
66  clock_time_t start;
67  clock_time_t duration;
68 };
69 
70 static struct btn_timer left_timer, right_timer;
71 /*---------------------------------------------------------------------------*/
72 static void
73 button_press_handler(uint8_t ioid)
74 {
75  if(ioid == BOARD_IOID_KEY_LEFT) {
76  if(!timer_expired(&left_timer.debounce)) {
77  return;
78  }
79 
80  timer_set(&left_timer.debounce, DEBOUNCE_DURATION);
81 
82  /*
83  * Start press duration counter on press (falling), notify on release
84  * (rising)
85  */
86  if(ti_lib_gpio_pin_read(BOARD_KEY_LEFT) == 0) {
87  left_timer.start = clock_time();
88  left_timer.duration = 0;
89  } else {
90  left_timer.duration = clock_time() - left_timer.start;
91  sensors_changed(&button_left_sensor);
92  }
93  }
94 
95  if(ioid == BOARD_IOID_KEY_RIGHT) {
96  if(BUTTON_SENSOR_ENABLE_SHUTDOWN == 0) {
97  if(!timer_expired(&right_timer.debounce)) {
98  return;
99  }
100 
101  timer_set(&right_timer.debounce, DEBOUNCE_DURATION);
102 
103  /*
104  * Start press duration counter on press (falling), notify on release
105  * (rising)
106  */
107  if(ti_lib_gpio_pin_read(BOARD_KEY_RIGHT) == 0) {
108  right_timer.start = clock_time();
109  right_timer.duration = 0;
110  } else {
111  right_timer.duration = clock_time() - right_timer.start;
112  sensors_changed(&button_right_sensor);
113  }
114  } else {
115  lpm_shutdown(BOARD_IOID_KEY_RIGHT, IOC_IOPULL_UP, IOC_WAKE_ON_LOW);
116  }
117  }
118 }
119 /*---------------------------------------------------------------------------*/
120 static void
121 config_buttons(int type, int c, uint32_t key)
122 {
123  switch(type) {
124  case SENSORS_HW_INIT:
125  ti_lib_gpio_event_clear(1 << key);
126  ti_lib_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG);
127  ti_lib_gpio_dir_mode_set((1 << key), GPIO_DIR_MODE_IN);
129  break;
130  case SENSORS_ACTIVE:
131  if(c) {
132  ti_lib_gpio_event_clear(1 << key);
133  ti_lib_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG);
134  ti_lib_gpio_dir_mode_set((1 << key), GPIO_DIR_MODE_IN);
135  ti_lib_ioc_int_enable(key);
136  } else {
137  ti_lib_ioc_int_disable(key);
138  }
139  break;
140  default:
141  break;
142  }
143 }
144 /*---------------------------------------------------------------------------*/
145 static int
146 config_left(int type, int value)
147 {
148  config_buttons(type, value, BOARD_IOID_KEY_LEFT);
149 
150  return 1;
151 }
152 /*---------------------------------------------------------------------------*/
153 static int
154 config_right(int type, int value)
155 {
156  config_buttons(type, value, BOARD_IOID_KEY_RIGHT);
157 
158  return 1;
159 }
160 /*---------------------------------------------------------------------------*/
161 static int
162 status(int type, uint32_t key_io_id)
163 {
164  switch(type) {
165  case SENSORS_ACTIVE:
166  case SENSORS_READY:
167  if(ti_lib_ioc_port_configure_get(key_io_id) & IOC_INT_ENABLE) {
168  return 1;
169  }
170  break;
171  default:
172  break;
173  }
174  return 0;
175 }
176 /*---------------------------------------------------------------------------*/
177 static int
178 value_left(int type)
179 {
180  if(type == BUTTON_SENSOR_VALUE_STATE) {
181  return ti_lib_gpio_pin_read(BOARD_KEY_LEFT) == 0 ?
182  BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
183  } else if(type == BUTTON_SENSOR_VALUE_DURATION) {
184  return (int)left_timer.duration;
185  }
186  return 0;
187 }
188 /*---------------------------------------------------------------------------*/
189 static int
190 value_right(int type)
191 {
192  if(type == BUTTON_SENSOR_VALUE_STATE) {
193  return ti_lib_gpio_pin_read(BOARD_KEY_RIGHT) == 0 ?
194  BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
195  } else if(type == BUTTON_SENSOR_VALUE_DURATION) {
196  return (int)right_timer.duration;
197  }
198  return 0;
199 }
200 /*---------------------------------------------------------------------------*/
201 static int
202 status_left(int type)
203 {
204  return status(type, BOARD_IOID_KEY_LEFT);
205 }
206 /*---------------------------------------------------------------------------*/
207 static int
208 status_right(int type)
209 {
210  return status(type, BOARD_IOID_KEY_RIGHT);
211 }
212 /*---------------------------------------------------------------------------*/
213 SENSORS_SENSOR(button_left_sensor, BUTTON_SENSOR, value_left, config_left,
214  status_left);
215 SENSORS_SENSOR(button_right_sensor, BUTTON_SENSOR, value_right, config_right,
216  status_right);
217 /*---------------------------------------------------------------------------*/
218 /** @} */
#define DEBOUNCE_DURATION
Delay before button state is assumed to be stable.
Definition: button-sensor.c:54
static void start(void)
Start measurement.
const struct sensors_sensor button_left_sensor
Exports a global symbol to be used by the sensor API.
Header file with macros which rename TI CC26xxware functions.
void lpm_shutdown(uint32_t wakeup_pin, uint32_t io_pull, uint32_t wake_on)
Put the chip in shutdown power mode.
Definition: lpm.c:91
Timer library header file.
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
#define BUTTON_SENSOR_VALUE_DURATION
Can be passed to value() function to get low state duration.
Definition: button-sensor.h:55
static void button_press_handler(uint8_t ioid)
Handler for Sensortag-CC26XX button presses.
Definition: button-sensor.c:76
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
A timer.
Definition: timer.h:86
Header file for the CC13xx/CC26xx GPIO interrupt management.
static int config_right(int type, int value)
Init function for the right button.
#define BUTTON_SENSOR_VALUE_STATE
Can be passed to value() function to get current button state.
Definition: button-sensor.h:52
static int status_left(int type)
Status function for the left button.
void gpio_interrupt_register_handler(uint8_t ioid, gpio_interrupt_handler_t f)
Register a GPIO interrupt handler.
Header file for the LaunchPad Button Driver.
static void config_buttons(int type, int c, uint32_t key)
Configuration function for the button sensor for all buttons.
static int config_left(int type, int value)
Init function for the left button.
static int value(int type, nrf_drv_gpiote_pin_t pin)
Return current state of a button.
static int status_right(int type)
Status function for the right button.
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:122