Merge pull request #169 from g-oikonomou/wip/button-hal

Add a button HAL and corresponding drivers
This commit is contained in:
Joakim Eriksson 2018-03-21 07:39:12 +01:00 committed by GitHub
commit 27aa381403
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
70 changed files with 1053 additions and 2228 deletions

View File

@ -87,6 +87,7 @@ gpio_hal_arch_pin_cfg_set(gpio_hal_pin_t pin, gpio_hal_pin_cfg_t cfg)
GPIO_DISABLE_INTERRUPT(port_base, pin_mask);
} else if(tmp == GPIO_HAL_PIN_CFG_INT_ENABLE) {
GPIO_ENABLE_INTERRUPT(port_base, pin_mask);
NVIC_EnableIRQ(port);
}
GPIO_SOFTWARE_CONTROL(port_base, pin_mask);

View File

@ -55,8 +55,10 @@
#define PIN_TO_PORT(pin) (pin >> 3)
#define PIN_TO_PORT_BASE(pin) GPIO_PORT_TO_BASE(PIN_TO_PORT(pin))
/*---------------------------------------------------------------------------*/
#define gpio_hal_arch_interrupt_enable(p) \
GPIO_ENABLE_INTERRUPT(PIN_TO_PORT_BASE(p), GPIO_PIN_MASK((p) % 8))
#define gpio_hal_arch_interrupt_enable(p) do { \
GPIO_ENABLE_INTERRUPT(PIN_TO_PORT_BASE(p), GPIO_PIN_MASK((p) % 8)); \
NVIC_EnableIRQ(PIN_TO_PORT(p)); \
} while(0);
#define gpio_hal_arch_interrupt_disable(p) \
GPIO_DISABLE_INTERRUPT(PIN_TO_PORT_BASE(p), GPIO_PIN_MASK((p) % 8))

View File

@ -9,7 +9,7 @@ CONTIKI_TARGET_DIRS = . dev
CONTIKI_TARGET_SOURCEFILES += leds-arch.c
CONTIKI_TARGET_SOURCEFILES += platform.c
CONTIKI_TARGET_SOURCEFILES += sensors.c smartrf-sensors.c
CONTIKI_TARGET_SOURCEFILES += button-sensor.c als-sensor.c
CONTIKI_TARGET_SOURCEFILES += board-buttons.c als-sensor.c
CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES)

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup cc2538-smartrf
* @{
*
* \defgroup cc2538-smartrf-buttons SmartRF06EB Buttons
*
* Generic module controlling buttons on the SmartRF06EB
* @{
*
* \file
* Defines SmartRF06EB buttons for use with the button HAL
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "dev/button-hal.h"
/*---------------------------------------------------------------------------*/
BUTTON_HAL_BUTTON(key_left, "Key Left", \
GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_LEFT_PORT, BUTTON_LEFT_PIN), \
GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_BUTTON_ZERO, true);
BUTTON_HAL_BUTTON(key_right, "Key Right", \
GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_RIGHT_PORT, BUTTON_RIGHT_PIN), \
GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_BUTTON_ONE, true);
BUTTON_HAL_BUTTON(key_up, "Key Up", \
GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_UP_PORT, BUTTON_UP_PIN), \
GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_BUTTON_TWO, true);
BUTTON_HAL_BUTTON(key_down, "Key Down", \
GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_DOWN_PORT, BUTTON_DOWN_PIN), \
GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_BUTTON_THREE, true);
BUTTON_HAL_BUTTON(key_select, "Key Select", \
GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_SELECT_PORT, BUTTON_SELECT_PIN), \
GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_BUTTON_FOUR, true);
/*---------------------------------------------------------------------------*/
BUTTON_HAL_BUTTONS(&key_left, &key_right, &key_up, &key_down, &key_select);
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
*/

View File

@ -163,6 +163,7 @@
/* Notify various examples that we have Buttons */
#define PLATFORM_HAS_BUTTON 1
#define PLATFORM_SUPPORTS_BUTTON_HAL 1
/** @} */
/*---------------------------------------------------------------------------*/
/**

View File

@ -1,265 +0,0 @@
/*
* Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* \addtogroup cc2538dk-button-sensor
* @{
*
* \file
* Driver for the SmartRF06EB buttons
*/
#include "contiki.h"
#include "dev/nvic.h"
#include "dev/ioc.h"
#include "dev/gpio.h"
#include "dev/button-sensor.h"
#include "sys/timer.h"
#include <stdint.h>
#include <string.h>
#define BUTTON_SELECT_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_SELECT_PORT)
#define BUTTON_SELECT_PIN_MASK GPIO_PIN_MASK(BUTTON_SELECT_PIN)
#define BUTTON_LEFT_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_LEFT_PORT)
#define BUTTON_LEFT_PIN_MASK GPIO_PIN_MASK(BUTTON_LEFT_PIN)
#define BUTTON_RIGHT_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_RIGHT_PORT)
#define BUTTON_RIGHT_PIN_MASK GPIO_PIN_MASK(BUTTON_RIGHT_PIN)
#define BUTTON_UP_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_UP_PORT)
#define BUTTON_UP_PIN_MASK GPIO_PIN_MASK(BUTTON_UP_PIN)
#define BUTTON_DOWN_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_DOWN_PORT)
#define BUTTON_DOWN_PIN_MASK GPIO_PIN_MASK(BUTTON_DOWN_PIN)
/*---------------------------------------------------------------------------*/
static struct timer debouncetimer;
/*---------------------------------------------------------------------------*/
/**
* \brief Common initialiser for all buttons
* \param port_base GPIO port's register offset
* \param pin_mask Pin mask corresponding to the button's pin
*/
static void
config(uint32_t port_base, uint32_t pin_mask)
{
/* Software controlled */
GPIO_SOFTWARE_CONTROL(port_base, pin_mask);
/* Set pin to input */
GPIO_SET_INPUT(port_base, pin_mask);
/* Enable edge detection */
GPIO_DETECT_EDGE(port_base, pin_mask);
/* Single edge */
GPIO_TRIGGER_SINGLE_EDGE(port_base, pin_mask);
/* Trigger interrupt on Falling edge */
GPIO_DETECT_RISING(port_base, pin_mask);
GPIO_ENABLE_INTERRUPT(port_base, pin_mask);
}
/*---------------------------------------------------------------------------*/
static void
button_press_handler(gpio_hal_pin_mask_t pin_mask)
{
if(!timer_expired(&debouncetimer)) {
return;
}
timer_set(&debouncetimer, CLOCK_SECOND / 8);
if(pin_mask &
(gpio_hal_pin_to_mask(BUTTON_SELECT_PIN) << (BUTTON_SELECT_PORT << 3))) {
sensors_changed(&button_select_sensor);
} else if(pin_mask &
(gpio_hal_pin_to_mask(BUTTON_LEFT_PIN) << (BUTTON_LEFT_PORT << 3))) {
sensors_changed(&button_left_sensor);
} else if(pin_mask &
(gpio_hal_pin_to_mask(BUTTON_RIGHT_PIN) << (BUTTON_RIGHT_PORT << 3))) {
sensors_changed(&button_right_sensor);
} else if(pin_mask &
(gpio_hal_pin_to_mask(BUTTON_UP_PIN) << (BUTTON_UP_PORT << 3))) {
sensors_changed(&button_up_sensor);
} else if(pin_mask &
(gpio_hal_pin_to_mask(BUTTON_DOWN_PIN) << (BUTTON_DOWN_PORT << 3))) {
sensors_changed(&button_down_sensor);
}
}
/*---------------------------------------------------------------------------*/
static gpio_hal_event_handler_t press_handler = {
.next = NULL,
.handler = button_press_handler,
.pin_mask = 0,
};
/*---------------------------------------------------------------------------*/
static void
register_btn_callback(uint8_t port_num, uint8_t pin)
{
press_handler.pin_mask |=
gpio_hal_pin_to_mask(pin) << (port_num << 3);
gpio_hal_register_handler(&press_handler);
}
/*---------------------------------------------------------------------------*/
/**
* \brief Init function for the select button.
*
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor api. The return value is also not required by
* the API but otherwise ignored.
*
* \param type ignored
* \param value ignored
* \return ignored
*/
static int
config_select(int type, int value)
{
config(BUTTON_SELECT_PORT_BASE, BUTTON_SELECT_PIN_MASK);
ioc_set_over(BUTTON_SELECT_PORT, BUTTON_SELECT_PIN, IOC_OVERRIDE_PUE);
NVIC_EnableIRQ(BUTTON_SELECT_VECTOR);
register_btn_callback(BUTTON_SELECT_PORT, BUTTON_SELECT_PIN);
return 1;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Init function for the left button.
*
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor api. The return value is also not required by
* the API but otherwise ignored.
*
* \param type ignored
* \param value ignored
* \return ignored
*/
static int
config_left(int type, int value)
{
config(BUTTON_LEFT_PORT_BASE, BUTTON_LEFT_PIN_MASK);
ioc_set_over(BUTTON_LEFT_PORT, BUTTON_LEFT_PIN, IOC_OVERRIDE_PUE);
NVIC_EnableIRQ(BUTTON_LEFT_VECTOR);
register_btn_callback(BUTTON_LEFT_PORT, BUTTON_LEFT_PIN);
return 1;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Init function for the right button.
*
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor api. The return value is also not required by
* the API but otherwise ignored.
*
* \param type ignored
* \param value ignored
* \return ignored
*/
static int
config_right(int type, int value)
{
config(BUTTON_RIGHT_PORT_BASE, BUTTON_RIGHT_PIN_MASK);
ioc_set_over(BUTTON_RIGHT_PORT, BUTTON_RIGHT_PIN, IOC_OVERRIDE_PUE);
NVIC_EnableIRQ(BUTTON_RIGHT_VECTOR);
register_btn_callback(BUTTON_RIGHT_PORT, BUTTON_RIGHT_PIN);
return 1;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Init function for the up button.
*
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor api. The return value is also not required by
* the API but otherwise ignored.
*
* \param type ignored
* \param value ignored
* \return ignored
*/
static int
config_up(int type, int value)
{
config(BUTTON_UP_PORT_BASE, BUTTON_UP_PIN_MASK);
ioc_set_over(BUTTON_UP_PORT, BUTTON_UP_PIN, IOC_OVERRIDE_PUE);
NVIC_EnableIRQ(BUTTON_UP_VECTOR);
register_btn_callback(BUTTON_UP_PORT, BUTTON_UP_PIN);
return 1;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Init function for the down button.
*
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor api. The return value is also not required by
* the API but otherwise ignored.
*
* \param type ignored
* \param value ignored
* \return ignored
*/
static int
config_down(int type, int value)
{
config(BUTTON_DOWN_PORT_BASE, BUTTON_DOWN_PIN_MASK);
ioc_set_over(BUTTON_DOWN_PORT, BUTTON_DOWN_PIN, IOC_OVERRIDE_PUE);
NVIC_EnableIRQ(BUTTON_DOWN_VECTOR);
register_btn_callback(BUTTON_DOWN_PORT, BUTTON_DOWN_PIN);
return 1;
}
/*---------------------------------------------------------------------------*/
void
button_sensor_init()
{
timer_set(&debouncetimer, 0);
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(button_select_sensor, BUTTON_SENSOR, NULL, config_select, NULL);
SENSORS_SENSOR(button_left_sensor, BUTTON_SENSOR, NULL, config_left, NULL);
SENSORS_SENSOR(button_right_sensor, BUTTON_SENSOR, NULL, config_right, NULL);
SENSORS_SENSOR(button_up_sensor, BUTTON_SENSOR, NULL, config_up, NULL);
SENSORS_SENSOR(button_down_sensor, BUTTON_SENSOR, NULL, config_down, NULL);
/** @} */

View File

@ -1,66 +0,0 @@
/*
* Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* \addtogroup cc2538-smartrf-sensors
* @{
*
* \defgroup cc2538dk-button-sensor cc2538dk Button Driver
*
* Driver for the SmartRF06EB buttons
* @{
*
* \file
* Header file for the cc2538dk Button Driver
*/
#ifndef BUTTON_SENSOR_H_
#define BUTTON_SENSOR_H_
#include "lib/sensors.h"
#include "dev/gpio.h"
#define BUTTON_SENSOR "Button"
#define button_sensor button_select_sensor
extern const struct sensors_sensor button_select_sensor;
extern const struct sensors_sensor button_left_sensor;
extern const struct sensors_sensor button_right_sensor;
extern const struct sensors_sensor button_up_sensor;
extern const struct sensors_sensor button_down_sensor;
/*---------------------------------------------------------------------------*/
#endif /* BUTTON_SENSOR_H_ */
/** \brief Common initialiser for all SmartRF Buttons */
void button_sensor_init();
/**
* @}
* @}
*/

View File

@ -41,16 +41,13 @@
* Implementation of a generic module controlling SmartRF06EB sensors
*/
#include "contiki.h"
#include "dev/button-sensor.h"
#include "dev/als-sensor.h"
#include "dev/cc2538-sensors.h"
#include <string.h>
/** \brief Exports a global symbol to be used by the sensor API */
SENSORS(&button_select_sensor, &button_left_sensor, &button_right_sensor,
&button_up_sensor, &button_down_sensor, &als_sensor,
&cc2538_temp_sensor, &vdd3_sensor);
SENSORS(&als_sensor, &cc2538_temp_sensor, &vdd3_sensor);
/**
* @}

View File

@ -46,14 +46,15 @@
#include "dev/adc.h"
#include "dev/leds.h"
#include "dev/uart.h"
#include "dev/button-sensor.h"
#include "dev/serial-line.h"
#include "dev/slip.h"
#include "dev/cc2538-rf.h"
#include "dev/udma.h"
#include "dev/crypto.h"
#include "dev/button-hal.h"
#include "usb/usb-serial.h"
#include "lib/random.h"
#include "lib/sensors.h"
#include "net/netstack.h"
#include "net/mac/framer/frame802154.h"
#include "net/linkaddr.h"
@ -121,8 +122,6 @@ platform_init_stage_one(void)
void
platform_init_stage_two()
{
button_sensor_init();
/*
* Character I/O Initialisation.
* When the UART receives a character it will call serial_line_input_byte to
@ -159,6 +158,8 @@ platform_init_stage_two()
/* Populate linkaddr_node_addr */
ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE);
button_hal_init();
INTERRUPTS_ENABLE();
fade(LEDS_GREEN);

View File

@ -15,7 +15,7 @@ PLATFORM_ROOT_DIR = $(CONTIKI)/arch/platform/$(TARGET)
### Include
CONTIKI_TARGET_SOURCEFILES += platform.c board.c
CONTIKI_TARGET_SOURCEFILES += leds-arch.c button-sensor.c openmote-sensors.c
CONTIKI_TARGET_SOURCEFILES += leds-arch.c board-buttons.c openmote-sensors.c
CONTIKI_TARGET_SOURCEFILES += antenna.c adxl346.c max44009.c sht21.c tps62730.c
CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES)

View File

@ -128,6 +128,7 @@
#define BUTTON_USER_VECTOR GPIO_C_IRQn
/* Notify various examples that we have Buttons */
#define PLATFORM_HAS_BUTTON 1
#define PLATFORM_SUPPORTS_BUTTON_HAL 1
/** @} */
/*---------------------------------------------------------------------------*/
/**

View File

@ -1,10 +1,11 @@
/*
* Copyright (c) 2015, Texas Instruments Incorporated - http://www.ti.com/
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
@ -29,35 +30,26 @@
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup launchpad-peripherals
* \addtogroup openmote-cc2538
* @{
*
* \defgroup launchpad-button-sensor LaunchPad Button Driver
* \defgroup openmote-cc2538-buttons OpenMote-CC2538 user button
*
* One of the buttons can be configured as general purpose or as an on/off key
* Generic module controlling the user button on the OpenMote-CC2538
* @{
*
* \file
* Header file for the LaunchPad Button Driver
* Defines the OpenMote-CC2538 user button for use with the button HAL
*/
/*---------------------------------------------------------------------------*/
#ifndef BUTTON_SENSOR_H_
#define BUTTON_SENSOR_H_
#include "contiki.h"
#include "dev/button-hal.h"
/*---------------------------------------------------------------------------*/
#include "lib/sensors.h"
BUTTON_HAL_BUTTON(button_user, "User button", \
GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_USER_PORT, BUTTON_USER_PIN), \
GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_USER_BUTTON, true);
/*---------------------------------------------------------------------------*/
#define BUTTON_SENSOR "Button"
/*---------------------------------------------------------------------------*/
#define BUTTON_SENSOR_VALUE_STATE 0
#define BUTTON_SENSOR_VALUE_DURATION 1
#define BUTTON_SENSOR_VALUE_RELEASED 0
#define BUTTON_SENSOR_VALUE_PRESSED 1
/*---------------------------------------------------------------------------*/
extern const struct sensors_sensor button_left_sensor;
extern const struct sensors_sensor button_right_sensor;
/*---------------------------------------------------------------------------*/
#endif /* BUTTON_SENSOR_H_ */
BUTTON_HAL_BUTTONS(&button_user);
/*---------------------------------------------------------------------------*/
/**
* @}

View File

@ -1,178 +0,0 @@
/*
* Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup openmote-button-sensor
* @{
*
* \file
* Driver for for the OpenMote-CC2538 user button
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "dev/nvic.h"
#include "dev/ioc.h"
#include "dev/gpio.h"
#include "dev/button-sensor.h"
#include "sys/timer.h"
#include "sys/ctimer.h"
#include "sys/process.h"
#include <stdint.h>
#include <string.h>
/*---------------------------------------------------------------------------*/
#define BUTTON_USER_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_USER_PORT)
#define BUTTON_USER_PIN_MASK GPIO_PIN_MASK(BUTTON_USER_PIN)
/*---------------------------------------------------------------------------*/
#define DEBOUNCE_DURATION (CLOCK_SECOND >> 4)
static struct timer debouncetimer;
/*---------------------------------------------------------------------------*/
static clock_time_t press_duration = 0;
static struct ctimer press_counter;
static uint8_t press_event_counter;
process_event_t button_press_duration_exceeded;
/*---------------------------------------------------------------------------*/
static void
duration_exceeded_callback(void *data)
{
press_event_counter++;
process_post(PROCESS_BROADCAST, button_press_duration_exceeded,
&press_event_counter);
ctimer_set(&press_counter, press_duration, duration_exceeded_callback,
NULL);
}
/*---------------------------------------------------------------------------*/
/**
* \brief Retrieves the value of the button pin
* \param type Returns the pin level or the counter of press duration events.
* type == BUTTON_SENSOR_VALUE_TYPE_LEVEL or
* type == BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION
* respectively
*/
static int
value(int type)
{
switch(type) {
case BUTTON_SENSOR_VALUE_TYPE_LEVEL:
return GPIO_READ_PIN(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
case BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION:
return press_event_counter;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static void
button_press_handler(gpio_hal_pin_mask_t pin_mask)
{
if(!timer_expired(&debouncetimer)) {
return;
}
timer_set(&debouncetimer, DEBOUNCE_DURATION);
if(press_duration) {
press_event_counter = 0;
if(value(BUTTON_SENSOR_VALUE_TYPE_LEVEL) == BUTTON_SENSOR_PRESSED_LEVEL) {
ctimer_set(&press_counter, press_duration, duration_exceeded_callback,
NULL);
} else {
ctimer_stop(&press_counter);
}
}
sensors_changed(&button_sensor);
}
/*---------------------------------------------------------------------------*/
static gpio_hal_event_handler_t press_handler = {
.next = NULL,
.handler = button_press_handler,
.pin_mask = gpio_hal_pin_to_mask(BUTTON_USER_PIN) << (BUTTON_USER_PORT << 3),
};
/*---------------------------------------------------------------------------*/
/**
* \brief Init function for the User button.
* \param type SENSORS_ACTIVE: Activate / Deactivate the sensor (value == 1
* or 0 respectively)
*
* \param value Depends on the value of the type argument
* \return Depends on the value of the type argument
*/
static int
config_user(int type, int value)
{
switch(type) {
case SENSORS_HW_INIT:
button_press_duration_exceeded = process_alloc_event();
/* Software controlled */
GPIO_SOFTWARE_CONTROL(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
/* Set pin to input */
GPIO_SET_INPUT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
/* Enable edge detection */
GPIO_DETECT_EDGE(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
/* Both Edges */
GPIO_TRIGGER_BOTH_EDGES(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
ioc_set_over(BUTTON_USER_PORT, BUTTON_USER_PIN, IOC_OVERRIDE_PUE);
gpio_hal_register_handler(&press_handler);
break;
case SENSORS_ACTIVE:
if(value) {
GPIO_ENABLE_INTERRUPT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
NVIC_EnableIRQ(BUTTON_USER_VECTOR);
} else {
GPIO_DISABLE_INTERRUPT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
NVIC_DisableIRQ(BUTTON_USER_VECTOR);
}
return value;
case BUTTON_SENSOR_CONFIG_TYPE_INTERVAL:
press_duration = (clock_time_t)value;
break;
default:
break;
}
return 1;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, value, config_user, NULL);
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -52,7 +52,7 @@
/**
* \brief Exports a global symbol to be used by the sensor API
*/
SENSORS(&button_sensor, &cc2538_temp_sensor);
SENSORS(&cc2538_temp_sensor);
/*---------------------------------------------------------------------------*/
/**
* @}

View File

@ -49,7 +49,6 @@
/*---------------------------------------------------------------------------*/
#include "lib/sensors.h"
#include "dev/cc2538-sensors.h"
#include "dev/button-sensor.h"
/*---------------------------------------------------------------------------*/
#endif /* OPENMOTE_SENSORS_H_ */
/*---------------------------------------------------------------------------*/

View File

@ -55,8 +55,10 @@
#include "dev/cc2538-rf.h"
#include "dev/udma.h"
#include "dev/crypto.h"
#include "dev/button-hal.h"
#include "usb/usb-serial.h"
#include "lib/random.h"
#include "lib/sensors.h"
#include "net/netstack.h"
#include "net/mac/framer/frame802154.h"
#include "net/linkaddr.h"
@ -157,6 +159,8 @@ platform_init_stage_two()
/* Populate linkaddr_node_addr */
ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE);
button_hal_init();
INTERRUPTS_ENABLE();
fade(LEDS_BLUE);
@ -175,8 +179,6 @@ platform_init_stage_three()
process_start(&sensors_process, NULL);
SENSORS_ACTIVATE(button_sensor);
fade(LEDS_GREEN);
}
/*---------------------------------------------------------------------------*/

View File

@ -59,6 +59,7 @@
/* Notify various examples that we have Buttons */
#define PLATFORM_HAS_BUTTON 1
#define PLATFORM_SUPPORTS_BUTTON_HAL 1
/*
* Override button symbols from dev/button-sensor.h, for the examples that

View File

@ -2,7 +2,7 @@ CFLAGS += -DBOARD_LAUNCHPAD=1
CONTIKI_TARGET_DIRS += launchpad common
BOARD_SOURCEFILES += board.c launchpad-sensors.c button-sensor.c xmem.c
BOARD_SOURCEFILES += board.c board-buttons.c xmem.c
BOARD_SOURCEFILES += ext-flash.c board-spi.c
### Signal that we can be programmed with cc2538-bsl

View File

@ -1,10 +1,11 @@
/*
* Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
@ -29,32 +30,26 @@
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup srf06-common-peripherals
* \addtogroup launchpad-cc26xx-peripherals
* @{
*
* \file
* Header file for the SmartRF06EB + CC13xx/CC26xxEM Button Driver
* Defines CC13xx/CC26xx Launchpad buttons for use with the button HAL
*/
/*---------------------------------------------------------------------------*/
#ifndef BUTTON_SENSOR_H_
#define BUTTON_SENSOR_H_
/*---------------------------------------------------------------------------*/
#include "lib/sensors.h"
/*---------------------------------------------------------------------------*/
#define BUTTON_SENSOR "Button"
/*---------------------------------------------------------------------------*/
#define BUTTON_SENSOR_VALUE_STATE 0
#define BUTTON_SENSOR_VALUE_DURATION 1
#include "contiki.h"
#include "dev/button-hal.h"
#define BUTTON_SENSOR_VALUE_RELEASED 0
#define BUTTON_SENSOR_VALUE_PRESSED 1
#include "ti-lib.h"
/*---------------------------------------------------------------------------*/
extern const struct sensors_sensor button_select_sensor;
extern const struct sensors_sensor button_left_sensor;
extern const struct sensors_sensor button_right_sensor;
extern const struct sensors_sensor button_up_sensor;
extern const struct sensors_sensor button_down_sensor;
BUTTON_HAL_BUTTON(key_left, "Key Left", BOARD_IOID_KEY_LEFT, \
GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_LEFT, \
true);
BUTTON_HAL_BUTTON(key_right, "Key Right", BOARD_IOID_KEY_RIGHT, \
GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_RIGHT, \
true);
/*---------------------------------------------------------------------------*/
#endif /* BUTTON_SENSOR_H_ */
BUTTON_HAL_BUTTONS(&key_left, &key_right);
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -48,6 +48,8 @@
/*---------------------------------------------------------------------------*/
#include "ext-flash.h"
/*---------------------------------------------------------------------------*/
#define BOARD_CONF_HAS_SENSORS 0
/*---------------------------------------------------------------------------*/
#endif /* BOARD_PERIPHERALS_H_ */
/*---------------------------------------------------------------------------*/
/**

View File

@ -37,7 +37,6 @@
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "lib/sensors.h"
#include "lpm.h"
#include "ti-lib.h"
#include "board-peripherals.h"

View File

@ -1,225 +0,0 @@
/*
* Copyright (c) 2015, Texas Instruments Incorporated - http://www.ti.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup launchpad-button-sensor
* @{
*
* \file
* Driver for LaunchPad buttons
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "lib/sensors.h"
#include "launchpad/button-sensor.h"
#include "gpio-hal.h"
#include "sys/timer.h"
#include "lpm.h"
#include "ti-lib.h"
#include <stdint.h>
/*---------------------------------------------------------------------------*/
#ifdef BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN
#define BUTTON_SENSOR_ENABLE_SHUTDOWN BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN
#else
#define BUTTON_SENSOR_ENABLE_SHUTDOWN 1
#endif
/*---------------------------------------------------------------------------*/
#define BUTTON_GPIO_CFG (IOC_CURRENT_2MA | IOC_STRENGTH_AUTO | \
IOC_IOPULL_UP | IOC_SLEW_DISABLE | \
IOC_HYST_DISABLE | IOC_BOTH_EDGES | \
IOC_INT_ENABLE | IOC_IOMODE_NORMAL | \
IOC_NO_WAKE_UP | IOC_INPUT_ENABLE)
/*---------------------------------------------------------------------------*/
#define DEBOUNCE_DURATION (CLOCK_SECOND >> 5)
struct btn_timer {
struct timer debounce;
clock_time_t start;
clock_time_t duration;
};
static struct btn_timer left_timer, right_timer;
/*---------------------------------------------------------------------------*/
static void
button_press_handler(gpio_hal_pin_mask_t pin_mask)
{
if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_LEFT)) {
if(!timer_expired(&left_timer.debounce)) {
return;
}
timer_set(&left_timer.debounce, DEBOUNCE_DURATION);
/*
* Start press duration counter on press (falling), notify on release
* (rising)
*/
if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0) {
left_timer.start = clock_time();
left_timer.duration = 0;
} else {
left_timer.duration = clock_time() - left_timer.start;
sensors_changed(&button_left_sensor);
}
}
if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_RIGHT)) {
if(BUTTON_SENSOR_ENABLE_SHUTDOWN == 0) {
if(!timer_expired(&right_timer.debounce)) {
return;
}
timer_set(&right_timer.debounce, DEBOUNCE_DURATION);
/*
* Start press duration counter on press (falling), notify on release
* (rising)
*/
if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0) {
right_timer.start = clock_time();
right_timer.duration = 0;
} else {
right_timer.duration = clock_time() - right_timer.start;
sensors_changed(&button_right_sensor);
}
} else {
lpm_shutdown(BOARD_IOID_KEY_RIGHT, IOC_IOPULL_UP, IOC_WAKE_ON_LOW);
}
}
}
/*---------------------------------------------------------------------------*/
static gpio_hal_event_handler_t press_handler = {
.next = NULL,
.handler = button_press_handler,
.pin_mask = 0,
};
/*---------------------------------------------------------------------------*/
static void
config_buttons(int type, int c, uint32_t key)
{
switch(type) {
case SENSORS_HW_INIT:
ti_lib_gpio_clear_event_dio(key);
ti_lib_rom_ioc_pin_type_gpio_input(key);
ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG);
press_handler.pin_mask |= gpio_hal_pin_to_mask(key);
gpio_hal_register_handler(&press_handler);
break;
case SENSORS_ACTIVE:
if(c) {
ti_lib_gpio_clear_event_dio(key);
ti_lib_rom_ioc_pin_type_gpio_input(key);
ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG);
ti_lib_rom_ioc_int_enable(key);
} else {
ti_lib_rom_ioc_int_disable(key);
}
break;
default:
break;
}
}
/*---------------------------------------------------------------------------*/
static int
config_left(int type, int value)
{
config_buttons(type, value, BOARD_IOID_KEY_LEFT);
return 1;
}
/*---------------------------------------------------------------------------*/
static int
config_right(int type, int value)
{
config_buttons(type, value, BOARD_IOID_KEY_RIGHT);
return 1;
}
/*---------------------------------------------------------------------------*/
static int
status(int type, uint32_t key_io_id)
{
switch(type) {
case SENSORS_ACTIVE:
case SENSORS_READY:
if(ti_lib_rom_ioc_port_configure_get(key_io_id) & IOC_INT_ENABLE) {
return 1;
}
break;
default:
break;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static int
value_left(int type)
{
if(type == BUTTON_SENSOR_VALUE_STATE) {
return ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0 ?
BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
} else if(type == BUTTON_SENSOR_VALUE_DURATION) {
return (int)left_timer.duration;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static int
value_right(int type)
{
if(type == BUTTON_SENSOR_VALUE_STATE) {
return ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0 ?
BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
} else if(type == BUTTON_SENSOR_VALUE_DURATION) {
return (int)right_timer.duration;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static int
status_left(int type)
{
return status(type, BOARD_IOID_KEY_LEFT);
}
/*---------------------------------------------------------------------------*/
static int
status_right(int type)
{
return status(type, BOARD_IOID_KEY_RIGHT);
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(button_left_sensor, BUTTON_SENSOR, value_left, config_left,
status_left);
SENSORS_SENSOR(button_right_sensor, BUTTON_SENSOR, value_right, config_right,
status_right);
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -188,6 +188,16 @@
}
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \brief Board indices for the button HAL
*
* Those values are not meant to be modified by the user
* @{
*/
#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00
#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \name Device string used on startup
* @{

View File

@ -205,6 +205,16 @@
}
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \brief Board indices for the button HAL
*
* Those values are not meant to be modified by the user
* @{
*/
#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00
#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \name Device string used on startup
* @{

View File

@ -189,6 +189,16 @@
}
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \brief Board indices for the button HAL
*
* Those values are not meant to be modified by the user
* @{
*/
#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00
#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \name Device string used on startup
* @{

View File

@ -68,7 +68,9 @@
#include "lib/sensors.h"
#include "button-sensor.h"
#include "dev/serial-line.h"
#include "dev/button-hal.h"
#include "net/mac/framer/frame802154.h"
#include "board-peripherals.h"
#include "driverlib/driverlib_release.h"
@ -84,6 +86,12 @@ unsigned short node_id = 0;
/** \brief Board specific iniatialisation */
void board_init(void);
/*---------------------------------------------------------------------------*/
#ifdef BOARD_CONF_HAS_SENSORS
#define BOARD_HAS_SENSORS BOARD_CONF_HAS_SENSORS
#else
#define BOARD_HAS_SENSORS 1
#endif
/*---------------------------------------------------------------------------*/
static void
fade(leds_mask_t l)
{
@ -188,6 +196,9 @@ platform_init_stage_two()
#else
ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE);
#endif
button_hal_init();
fade(LEDS_GREEN);
}
/*---------------------------------------------------------------------------*/
@ -218,7 +229,10 @@ platform_init_stage_three()
LOG_INFO(" Node ID: %d\n", node_id);
#if BOARD_HAS_SENSORS
process_start(&sensors_process, NULL);
#endif
fade(LEDS_ORANGE);
}
/*---------------------------------------------------------------------------*/

View File

@ -3,8 +3,8 @@ CFLAGS += -DBACKDOOR_IOID=0x00000000
CONTIKI_TARGET_DIRS += sensortag common
BOARD_SOURCEFILES += sensortag-sensors.c sensor-common.c
BOARD_SOURCEFILES += sensortag-sensors.c board-buttons.c sensor-common.c
BOARD_SOURCEFILES += bmp-280-sensor.c tmp-007-sensor.c opt-3001-sensor.c
BOARD_SOURCEFILES += hdc-1000-sensor.c mpu-9250-sensor.c button-sensor.c xmem.c
BOARD_SOURCEFILES += reed-relay.c ext-flash.c buzzer.c
BOARD_SOURCEFILES += hdc-1000-sensor.c mpu-9250-sensor.c xmem.c
BOARD_SOURCEFILES += ext-flash.c buzzer.c
BOARD_SOURCEFILES += board.c board-spi.c board-i2c.c

View File

@ -1,10 +1,11 @@
/*
* Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
@ -32,34 +33,30 @@
* \addtogroup sensortag-cc26xx-peripherals
* @{
*
* \defgroup sensortag-cc26xx-button-sensor SensorTag 2.0 Button Sensor
*
* One of the buttons can be configured as general purpose or as an on/off key
* @{
*
* \file
* Header file for the Sensortag Button Driver
* Defines Sensortag buttons for use with the button HAL
*/
/*---------------------------------------------------------------------------*/
#ifndef BUTTON_SENSOR_H_
#define BUTTON_SENSOR_H_
/*---------------------------------------------------------------------------*/
#include "lib/sensors.h"
/*---------------------------------------------------------------------------*/
#define BUTTON_SENSOR "Button"
/*---------------------------------------------------------------------------*/
#define BUTTON_SENSOR_VALUE_STATE 0
#define BUTTON_SENSOR_VALUE_DURATION 1
#include "contiki.h"
#include "dev/gpio-hal.h"
#include "dev/button-hal.h"
#define BUTTON_SENSOR_VALUE_RELEASED 0
#define BUTTON_SENSOR_VALUE_PRESSED 1
#include "ti-lib.h"
#include <stdbool.h>
/*---------------------------------------------------------------------------*/
extern const struct sensors_sensor button_left_sensor;
extern const struct sensors_sensor button_right_sensor;
BUTTON_HAL_BUTTON(reed_relay, "Reed Relay", BOARD_IOID_REED_RELAY, \
GPIO_HAL_PIN_CFG_PULL_DOWN, \
BOARD_BUTTON_HAL_INDEX_REED_RELAY, true);
BUTTON_HAL_BUTTON(key_left, "Key Left", BOARD_IOID_KEY_LEFT, \
GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_LEFT, \
true);
BUTTON_HAL_BUTTON(key_right, "Key Right", BOARD_IOID_KEY_RIGHT, \
GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_RIGHT, \
true);
/*---------------------------------------------------------------------------*/
#endif /* BUTTON_SENSOR_H_ */
BUTTON_HAL_BUTTONS(&reed_relay, &key_left, &key_right);
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
*/
/** @} */

View File

@ -53,7 +53,6 @@
#include "opt-3001-sensor.h"
#include "hdc-1000-sensor.h"
#include "mpu-9250-sensor.h"
#include "reed-relay.h"
#include "buzzer.h"
#include "ext-flash.h"
/*---------------------------------------------------------------------------*/

View File

@ -1,288 +0,0 @@
/*
* Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup sensortag-cc26xx-button-sensor
* @{
*
* \file
* Driver for Sensortag buttons
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "lib/sensors.h"
#include "sensortag/button-sensor.h"
#include "gpio-hal.h"
#include "sys/timer.h"
#include "lpm.h"
#include "ti-lib.h"
#include <stdint.h>
/*---------------------------------------------------------------------------*/
#ifdef BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN
#define BUTTON_SENSOR_ENABLE_SHUTDOWN BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN
#else
#define BUTTON_SENSOR_ENABLE_SHUTDOWN 1
#endif
/*---------------------------------------------------------------------------*/
#define BUTTON_GPIO_CFG (IOC_CURRENT_2MA | IOC_STRENGTH_AUTO | \
IOC_IOPULL_UP | IOC_SLEW_DISABLE | \
IOC_HYST_DISABLE | IOC_BOTH_EDGES | \
IOC_INT_ENABLE | IOC_IOMODE_NORMAL | \
IOC_NO_WAKE_UP | IOC_INPUT_ENABLE)
/*---------------------------------------------------------------------------*/
#define DEBOUNCE_DURATION (CLOCK_SECOND >> 5)
struct btn_timer {
struct timer debounce;
clock_time_t start;
clock_time_t duration;
};
static struct btn_timer left_timer, right_timer;
/*---------------------------------------------------------------------------*/
/**
* \brief Handler for Sensortag-CC26XX button presses
*/
static void
button_press_handler(gpio_hal_pin_mask_t pin_mask)
{
if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_LEFT)) {
if(!timer_expired(&left_timer.debounce)) {
return;
}
timer_set(&left_timer.debounce, DEBOUNCE_DURATION);
/*
* Start press duration counter on press (falling), notify on release
* (rising)
*/
if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0) {
left_timer.start = clock_time();
left_timer.duration = 0;
} else {
left_timer.duration = clock_time() - left_timer.start;
sensors_changed(&button_left_sensor);
}
}
if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_RIGHT)) {
if(BUTTON_SENSOR_ENABLE_SHUTDOWN == 0) {
if(!timer_expired(&right_timer.debounce)) {
return;
}
timer_set(&right_timer.debounce, DEBOUNCE_DURATION);
/*
* Start press duration counter on press (falling), notify on release
* (rising)
*/
if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0) {
right_timer.start = clock_time();
right_timer.duration = 0;
} else {
right_timer.duration = clock_time() - right_timer.start;
sensors_changed(&button_right_sensor);
}
} else {
lpm_shutdown(BOARD_IOID_KEY_RIGHT, IOC_IOPULL_UP, IOC_WAKE_ON_LOW);
}
}
}
/*---------------------------------------------------------------------------*/
static gpio_hal_event_handler_t press_handler = {
.next = NULL,
.handler = button_press_handler,
.pin_mask = 0,
};
/*---------------------------------------------------------------------------*/
/**
* \brief Configuration function for the button sensor for all buttons.
*
* \param type This function does nothing unless type == SENSORS_ACTIVE
* \param c 0: disable the button, non-zero: enable
* \param key: One of BOARD_KEY_LEFT, BOARD_KEY_RIGHT etc
*/
static void
config_buttons(int type, int c, uint32_t key)
{
switch(type) {
case SENSORS_HW_INIT:
ti_lib_gpio_clear_event_dio(key);
ti_lib_rom_ioc_pin_type_gpio_input(key);
ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG);
press_handler.pin_mask |= gpio_hal_pin_to_mask(key);
gpio_hal_register_handler(&press_handler);
break;
case SENSORS_ACTIVE:
if(c) {
ti_lib_gpio_clear_event_dio(key);
ti_lib_rom_ioc_pin_type_gpio_input(key);
ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG);
ti_lib_rom_ioc_int_enable(key);
} else {
ti_lib_rom_ioc_int_disable(key);
}
break;
default:
break;
}
}
/*---------------------------------------------------------------------------*/
/**
* \brief Configuration function for the left button.
*
* Parameters are passed onto config_buttons, which does the actual
* configuration
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor API. The return value is also required by
* the API but otherwise ignored.
*
* \param type passed to config_buttons as-is
* \param value passed to config_buttons as-is
*
* \return ignored
*/
static int
config_left(int type, int value)
{
config_buttons(type, value, BOARD_IOID_KEY_LEFT);
return 1;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Configuration function for the right button.
*
* Parameters are passed onto config_buttons, which does the actual
* configuration
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor api. The return value is also required by
* the API but otherwise ignored.
*
* \param type passed to config_buttons as-is
* \param value passed to config_buttons as-is
*
* \return ignored
*/
static int
config_right(int type, int value)
{
config_buttons(type, value, BOARD_IOID_KEY_RIGHT);
return 1;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Status function for all buttons
* \param type SENSORS_ACTIVE or SENSORS_READY
* \param key_io_id BOARD_IOID_KEY_LEFT, BOARD_IOID_KEY_RIGHT etc
* \return 1 if the button's port interrupt is enabled (edge detect)
*
* This function will only be called by status_left, status_right and the
* called will pass the correct key_io_id
*/
static int
status(int type, uint32_t key_io_id)
{
switch(type) {
case SENSORS_ACTIVE:
case SENSORS_READY:
if(ti_lib_ioc_port_configure_get(key_io_id) & IOC_INT_ENABLE) {
return 1;
}
break;
default:
break;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static int
value_left(int type)
{
if(type == BUTTON_SENSOR_VALUE_STATE) {
return ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0 ?
BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
} else if(type == BUTTON_SENSOR_VALUE_DURATION) {
return (int)left_timer.duration;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static int
value_right(int type)
{
if(type == BUTTON_SENSOR_VALUE_STATE) {
return ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0 ?
BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
} else if(type == BUTTON_SENSOR_VALUE_DURATION) {
return (int)right_timer.duration;
}
return 0;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Status function for the left button.
* \param type SENSORS_ACTIVE or SENSORS_READY
* \return 1 if the button's port interrupt is enabled (edge detect)
*
* This function will call status. It will pass type verbatim and it will also
* pass the correct key_io_id
*/
static int
status_left(int type)
{
return status(type, BOARD_IOID_KEY_LEFT);
}
/*---------------------------------------------------------------------------*/
/**
* \brief Status function for the right button.
* \param type SENSORS_ACTIVE or SENSORS_READY
* \return 1 if the button's port interrupt is enabled (edge detect)
*
* This function will call status. It will pass type verbatim and it will also
* pass the correct key_io_id
*/
static int
status_right(int type)
{
return status(type, BOARD_IOID_KEY_RIGHT);
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(button_left_sensor, BUTTON_SENSOR, value_left, config_left,
status_left);
SENSORS_SENSOR(button_right_sensor, BUTTON_SENSOR, value_right, config_right,
status_right);
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -238,6 +238,17 @@
#define SMARTRF_SETTINGS_CONF_OVERRIDE_TRIM_OFFSET 0x00018883
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \brief Board indices for the button HAL
*
* Those values are not meant to be modified by the user
* @{
*/
#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00
#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01
#define BOARD_BUTTON_HAL_INDEX_REED_RELAY 0xFF
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \name Device string used on startup
* @{

View File

@ -218,6 +218,17 @@
#define BOARD_IOID_AUDIO_CLK IOID_11
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \brief Board indices for the button HAL
*
* Those values are not meant to be modified by the user
* @{
*/
#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00
#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01
#define BOARD_BUTTON_HAL_INDEX_REED_RELAY 0xFF
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \name Device string used on startup
* @{

View File

@ -1,145 +0,0 @@
/*
* Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup sensortag-cc26xx-reed-relay
* @{
*
* \file
* Driver for the Sensortag Reed Relay
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "sys/clock.h"
#include "sys/timer.h"
#include "dev/gpio-hal.h"
#include "lib/sensors.h"
#include "sensortag/reed-relay.h"
#include "sys/timer.h"
#include "ti-lib.h"
#include <stdint.h>
/*---------------------------------------------------------------------------*/
static struct timer debouncetimer;
/*---------------------------------------------------------------------------*/
#define REED_IO_CFG (IOC_CURRENT_2MA | IOC_STRENGTH_AUTO | \
IOC_IOPULL_DOWN | IOC_SLEW_DISABLE | \
IOC_HYST_DISABLE | IOC_BOTH_EDGES | \
IOC_INT_DISABLE | IOC_IOMODE_NORMAL | \
IOC_NO_WAKE_UP | IOC_INPUT_ENABLE)
/*---------------------------------------------------------------------------*/
/**
* \brief Handler for Sensortag-CC26XX reed interrupts
*/
static void
reed_interrupt_handler(gpio_hal_pin_mask_t pin_mask)
{
if(!timer_expired(&debouncetimer)) {
return;
}
sensors_changed(&reed_relay_sensor);
timer_set(&debouncetimer, CLOCK_SECOND / 2);
}
/*---------------------------------------------------------------------------*/
static int
value(int type)
{
return (int)ti_lib_gpio_read_dio(BOARD_IOID_REED_RELAY);
}
/*---------------------------------------------------------------------------*/
static gpio_hal_event_handler_t event_handler = {
.next = NULL,
.handler = reed_interrupt_handler,
.pin_mask = gpio_hal_pin_to_mask(BOARD_IOID_REED_RELAY),
};
/*---------------------------------------------------------------------------*/
/**
* \brief Configuration function for the button sensor for all buttons.
*
* \param type SENSORS_HW_INIT: Initialise. SENSORS_ACTIVE: Enables/Disables
* depending on 'value'
* \param value 0: disable, non-zero: enable
* \return Always returns 1
*/
static int
configure(int type, int value)
{
switch(type) {
case SENSORS_HW_INIT:
ti_lib_ioc_int_disable(BOARD_IOID_REED_RELAY);
ti_lib_gpio_clear_event_dio(BOARD_IOID_REED_RELAY);
/* Enable the GPIO clock when the CM3 is running */
ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_GPIO);
/* S/W control, input, pull-down */
ti_lib_ioc_port_configure_set(BOARD_IOID_REED_RELAY, IOC_PORT_GPIO,
REED_IO_CFG);
gpio_hal_register_handler(&event_handler);
break;
case SENSORS_ACTIVE:
if(value) {
ti_lib_ioc_int_enable(BOARD_IOID_REED_RELAY);
} else {
ti_lib_ioc_int_disable(BOARD_IOID_REED_RELAY);
}
break;
default:
break;
}
return 1;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Status function for the reed
* \param type SENSORS_ACTIVE or SENSORS_READY
* \return 1 Interrupt enabled, 0: Disabled
*/
static int
status(int type)
{
switch(type) {
case SENSORS_ACTIVE:
case SENSORS_READY:
return (ti_lib_ioc_port_configure_get(BOARD_IOID_REED_RELAY)
& IOC_INT_ENABLE) == IOC_INT_ENABLE;
break;
default:
break;
}
return 0;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(reed_relay_sensor, "REED", value, configure, status);
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -37,19 +37,16 @@
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "sensortag/button-sensor.h"
#include "sensortag/bmp-280-sensor.h"
#include "sensortag/tmp-007-sensor.h"
#include "sensortag/opt-3001-sensor.h"
#include "sensortag/hdc-1000-sensor.h"
#include "sensortag/mpu-9250-sensor.h"
#include "sensortag/reed-relay.h"
#include <string.h>
/*---------------------------------------------------------------------------*/
/** \brief Exports a global symbol to be used by the sensor API */
SENSORS(&button_left_sensor, &button_right_sensor,
&bmp_280_sensor, &tmp_007_sensor, &opt_3001_sensor, &hdc_1000_sensor,
&mpu_9250_sensor, &reed_relay_sensor);
SENSORS(&bmp_280_sensor, &tmp_007_sensor, &opt_3001_sensor, &hdc_1000_sensor,
&mpu_9250_sensor);
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -2,7 +2,7 @@ CFLAGS += -DBOARD_SMARTRF06EB=1
CONTIKI_TARGET_DIRS += srf06
BOARD_SOURCEFILES += srf06-sensors.c button-sensor.c board.c
BOARD_SOURCEFILES += srf06-sensors.c board-buttons.c board.c
BOARD_SOURCEFILES += als-sensor.c
### Signal that we can be programmed with cc2538-bsl

View File

@ -1,16 +1,16 @@
/*
* Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
@ -27,48 +27,37 @@
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup openmote-cc2538
* @{
*
* \defgroup openmote-button-sensor OpenMote-CC2538 user button driver
*
* The user button will generate a sensors_changed event on press as
* well as on release.
*
* \addtogroup srf06-common-peripherals
* @{
*
* \file
* Header for the OpenMote-CC2538 button driver
* Defines SmarfRF06 + CC13xxEM/CC26xxEM buttons for use with the button HAL
*/
/*---------------------------------------------------------------------------*/
#ifndef BUTTON_SENSOR_H_
#define BUTTON_SENSOR_H_
/*---------------------------------------------------------------------------*/
#include "lib/sensors.h"
/*---------------------------------------------------------------------------*/
#define BUTTON_SENSOR "Button"
#include "contiki.h"
#include "dev/button-hal.h"
extern const struct sensors_sensor button_sensor;
#include "ti-lib.h"
/*---------------------------------------------------------------------------*/
extern process_event_t button_press_duration_exceeded;
BUTTON_HAL_BUTTON(key_left, "Key Left", BOARD_IOID_KEY_LEFT, \
GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_LEFT, \
true);
BUTTON_HAL_BUTTON(key_right, "Key Right", BOARD_IOID_KEY_RIGHT, \
GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_RIGHT, \
true);
BUTTON_HAL_BUTTON(key_up, "Key Up", BOARD_IOID_KEY_UP, \
GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_UP, \
true);
BUTTON_HAL_BUTTON(key_down, "Key Down", BOARD_IOID_KEY_DOWN, \
GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_DOWN, \
true);
BUTTON_HAL_BUTTON(key_select, "Key Select", BOARD_IOID_KEY_SELECT, \
GPIO_HAL_PIN_CFG_PULL_UP, \
BOARD_BUTTON_HAL_INDEX_KEY_SELECT, true);
/*---------------------------------------------------------------------------*/
#define BUTTON_SENSOR_CONFIG_TYPE_INTERVAL 0x0100
#define BUTTON_SENSOR_VALUE_TYPE_LEVEL 0
#define BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION 1
#define BUTTON_SENSOR_PRESSED_LEVEL 0
#define BUTTON_SENSOR_RELEASED_LEVEL 8
BUTTON_HAL_BUTTONS(&key_left, &key_right, &key_up, &key_down, &key_select);
/*---------------------------------------------------------------------------*/
#endif /* BUTTON_SENSOR_H_ */
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
*/
/** @} */

View File

@ -1,498 +0,0 @@
/*
* Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup srf06-common-peripherals
* @{
*
* \file
* Driver for the SmartRF06EB buttons when a CC13xx/CC26xxEM is mounted on it
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "lib/sensors.h"
#include "srf06/button-sensor.h"
#include "gpio-hal.h"
#include "sys/timer.h"
#include "lpm.h"
#include "ti-lib.h"
#include <stdint.h>
/*---------------------------------------------------------------------------*/
#ifdef BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN
#define BUTTON_SENSOR_ENABLE_SHUTDOWN BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN
#else
#define BUTTON_SENSOR_ENABLE_SHUTDOWN 1
#endif
/*---------------------------------------------------------------------------*/
#define BUTTON_GPIO_CFG (IOC_CURRENT_2MA | IOC_STRENGTH_AUTO | \
IOC_IOPULL_UP | IOC_SLEW_DISABLE | \
IOC_HYST_DISABLE | IOC_BOTH_EDGES | \
IOC_INT_ENABLE | IOC_IOMODE_NORMAL | \
IOC_NO_WAKE_UP | IOC_INPUT_ENABLE)
/*---------------------------------------------------------------------------*/
#define DEBOUNCE_DURATION (CLOCK_SECOND >> 5)
struct btn_timer {
struct timer debounce;
clock_time_t start;
clock_time_t duration;
};
static struct btn_timer sel_timer, left_timer, right_timer, up_timer,
down_timer;
/*---------------------------------------------------------------------------*/
/**
* \brief Handler for SmartRF button presses
*/
static void
button_press_handler(gpio_hal_pin_mask_t pin_mask)
{
if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_SELECT)) {
if(!timer_expired(&sel_timer.debounce)) {
return;
}
timer_set(&sel_timer.debounce, DEBOUNCE_DURATION);
/*
* Start press duration counter on press (falling), notify on release
* (rising)
*/
if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_SELECT) == 0) {
sel_timer.start = clock_time();
sel_timer.duration = 0;
} else {
sel_timer.duration = clock_time() - sel_timer.start;
sensors_changed(&button_select_sensor);
}
}
if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_LEFT)) {
if(!timer_expired(&left_timer.debounce)) {
return;
}
timer_set(&left_timer.debounce, DEBOUNCE_DURATION);
/*
* Start press duration counter on press (falling), notify on release
* (rising)
*/
if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0) {
left_timer.start = clock_time();
left_timer.duration = 0;
} else {
left_timer.duration = clock_time() - left_timer.start;
sensors_changed(&button_left_sensor);
}
}
if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_RIGHT)) {
if(BUTTON_SENSOR_ENABLE_SHUTDOWN == 0) {
if(!timer_expired(&right_timer.debounce)) {
return;
}
timer_set(&right_timer.debounce, DEBOUNCE_DURATION);
/*
* Start press duration counter on press (falling), notify on release
* (rising)
*/
if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0) {
right_timer.start = clock_time();
right_timer.duration = 0;
} else {
right_timer.duration = clock_time() - right_timer.start;
sensors_changed(&button_right_sensor);
}
} else {
lpm_shutdown(BOARD_IOID_KEY_RIGHT, IOC_IOPULL_UP, IOC_WAKE_ON_LOW);
}
}
if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_UP)) {
if(!timer_expired(&up_timer.debounce)) {
return;
}
timer_set(&up_timer.debounce, DEBOUNCE_DURATION);
/*
* Start press duration counter on press (falling), notify on release
* (rising)
*/
if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_UP) == 0) {
up_timer.start = clock_time();
up_timer.duration = 0;
} else {
up_timer.duration = clock_time() - up_timer.start;
sensors_changed(&button_up_sensor);
}
}
if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_DOWN)) {
if(!timer_expired(&down_timer.debounce)) {
return;
}
timer_set(&down_timer.debounce, DEBOUNCE_DURATION);
/*
* Start press duration counter on press (falling), notify on release
* (rising)
*/
if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_DOWN) == 0) {
down_timer.start = clock_time();
down_timer.duration = 0;
} else {
down_timer.duration = clock_time() - down_timer.start;
sensors_changed(&button_down_sensor);
}
}
}
/*---------------------------------------------------------------------------*/
static gpio_hal_event_handler_t press_handler = {
.next = NULL,
.handler = button_press_handler,
.pin_mask = 0,
};
/*---------------------------------------------------------------------------*/
/**
* \brief Configuration function for the button sensor for all buttons.
*
* \param type This function does nothing unless type == SENSORS_ACTIVE
* \param c 0: disable the button, non-zero: enable
* \param key: One of BOARD_KEY_LEFT, BOARD_KEY_RIGHT etc
*/
static void
config_buttons(int type, int c, uint32_t key)
{
switch(type) {
case SENSORS_HW_INIT:
ti_lib_gpio_clear_event_dio(key);
ti_lib_rom_ioc_pin_type_gpio_input(key);
ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG);
press_handler.pin_mask |= gpio_hal_pin_to_mask(key);
gpio_hal_register_handler(&press_handler);
break;
case SENSORS_ACTIVE:
if(c) {
ti_lib_gpio_clear_event_dio(key);
ti_lib_rom_ioc_pin_type_gpio_input(key);
ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG);
ti_lib_rom_ioc_int_enable(key);
} else {
ti_lib_rom_ioc_int_disable(key);
}
break;
default:
break;
}
}
/*---------------------------------------------------------------------------*/
/**
* \brief Configuration function for the select button.
*
* Parameters are passed onto config_buttons, which does the actual
* configuration
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor api. The return value is also required by
* the API but otherwise ignored.
*
* \param type passed to config_buttons as-is
* \param value passed to config_buttons as-is
*
* \return ignored
*/
static int
config_select(int type, int value)
{
config_buttons(type, value, BOARD_IOID_KEY_SELECT);
return 1;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Configuration function for the left button.
*
* Parameters are passed onto config_buttons, which does the actual
* configuration
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor api. The return value is also required by
* the API but otherwise ignored.
*
* \param type passed to config_buttons as-is
* \param value passed to config_buttons as-is
*
* \return ignored
*/
static int
config_left(int type, int value)
{
config_buttons(type, value, BOARD_IOID_KEY_LEFT);
return 1;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Configuration function for the right button.
*
* Parameters are passed onto config_buttons, which does the actual
* configuration
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor api. The return value is also required by
* the API but otherwise ignored.
*
* \param type passed to config_buttons as-is
* \param value passed to config_buttons as-is
*
* \return ignored
*/
static int
config_right(int type, int value)
{
config_buttons(type, value, BOARD_IOID_KEY_RIGHT);
return 1;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Configuration function for the up button.
*
* Parameters are passed onto config_buttons, which does the actual
* configuration
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor api. The return value is also required by
* the API but otherwise ignored.
*
* \param type passed to config_buttons as-is
* \param value passed to config_buttons as-is
*
* \return ignored
*/
static int
config_up(int type, int value)
{
config_buttons(type, value, BOARD_IOID_KEY_UP);
return 1;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Configuration function for the down button.
*
* Parameters are passed onto config_buttons, which does the actual
* configuration
* Parameters are ignored. They have been included because the prototype is
* dictated by the core sensor api. The return value is also required by
* the API but otherwise ignored.
*
* \param type passed to config_buttons as-is
* \param value passed to config_buttons as-is
*
* \return ignored
*/
static int
config_down(int type, int value)
{
config_buttons(type, value, BOARD_IOID_KEY_DOWN);
return 1;
}
/*---------------------------------------------------------------------------*/
static int
value_select(int type)
{
if(type == BUTTON_SENSOR_VALUE_STATE) {
return ti_lib_gpio_read_dio(BOARD_IOID_KEY_SELECT) == 0 ?
BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
} else if(type == BUTTON_SENSOR_VALUE_DURATION) {
return (int)sel_timer.duration;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static int
value_left(int type)
{
if(type == BUTTON_SENSOR_VALUE_STATE) {
return ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0 ?
BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
} else if(type == BUTTON_SENSOR_VALUE_DURATION) {
return (int)left_timer.duration;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static int
value_right(int type)
{
if(type == BUTTON_SENSOR_VALUE_STATE) {
return ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0 ?
BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
} else if(type == BUTTON_SENSOR_VALUE_DURATION) {
return (int)right_timer.duration;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static int
value_up(int type)
{
if(type == BUTTON_SENSOR_VALUE_STATE) {
return ti_lib_gpio_read_dio(BOARD_IOID_KEY_UP) == 0 ?
BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
} else if(type == BUTTON_SENSOR_VALUE_DURATION) {
return (int)up_timer.duration;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static int
value_down(int type)
{
if(type == BUTTON_SENSOR_VALUE_STATE) {
return ti_lib_gpio_read_dio(BOARD_IOID_KEY_DOWN) == 0 ?
BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED;
} else if(type == BUTTON_SENSOR_VALUE_DURATION) {
return (int)down_timer.duration;
}
return 0;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Status function for all buttons
* \param type SENSORS_ACTIVE or SENSORS_READY
* \param key_io_id BOARD_IOID_KEY_LEFT, BOARD_IOID_KEY_RIGHT etc
* \return 1 if the button's port interrupt is enabled (edge detect)
*
* This function will only be called by status_left, status_right and the
* called will pass the correct key_io_id
*/
static int
status(int type, uint32_t key_io_id)
{
switch(type) {
case SENSORS_ACTIVE:
case SENSORS_READY:
if(ti_lib_ioc_port_configure_get(key_io_id) & IOC_INT_ENABLE) {
return 1;
}
break;
default:
break;
}
return 0;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Status function for the select button.
* \param type SENSORS_ACTIVE or SENSORS_READY
* \return 1 if the button's port interrupt is enabled (edge detect)
*
* This function will call status. It will pass type verbatim and it will also
* pass the correct key_io_id
*/
static int
status_select(int type)
{
return status(type, BOARD_IOID_KEY_SELECT);
}
/*---------------------------------------------------------------------------*/
/**
* \brief Status function for the left button.
* \param type SENSORS_ACTIVE or SENSORS_READY
* \return 1 if the button's port interrupt is enabled (edge detect)
*
* This function will call status. It will pass type verbatim and it will also
* pass the correct key_io_id
*/
static int
status_left(int type)
{
return status(type, BOARD_IOID_KEY_LEFT);
}
/*---------------------------------------------------------------------------*/
/**
* \brief Status function for the right button.
* \param type SENSORS_ACTIVE or SENSORS_READY
* \return 1 if the button's port interrupt is enabled (edge detect)
*
* This function will call status. It will pass type verbatim and it will also
* pass the correct key_io_id
*/
static int
status_right(int type)
{
return status(type, BOARD_IOID_KEY_RIGHT);
}
/*---------------------------------------------------------------------------*/
/**
* \brief Status function for the up button.
* \param type SENSORS_ACTIVE or SENSORS_READY
* \return 1 if the button's port interrupt is enabled (edge detect)
*
* This function will call status. It will pass type verbatim and it will also
* pass the correct key_io_id
*/
static int
status_up(int type)
{
return status(type, BOARD_IOID_KEY_UP);
}
/*---------------------------------------------------------------------------*/
/**
* \brief Status function for the down button.
* \param type SENSORS_ACTIVE or SENSORS_READY
* \return 1 if the button's port interrupt is enabled (edge detect)
*
* This function will call status. It will pass type verbatim and it will also
* pass the correct key_io_id
*/
static int
status_down(int type)
{
return status(type, BOARD_IOID_KEY_DOWN);
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(button_select_sensor, BUTTON_SENSOR, value_select,
config_select, status_select);
SENSORS_SENSOR(button_left_sensor, BUTTON_SENSOR, value_left, config_left,
status_left);
SENSORS_SENSOR(button_right_sensor, BUTTON_SENSOR, value_right, config_right,
status_right);
SENSORS_SENSOR(button_up_sensor, BUTTON_SENSOR, value_up, config_up, status_up);
SENSORS_SENSOR(button_down_sensor, BUTTON_SENSOR, value_down, config_down,
status_down);
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -232,6 +232,19 @@
#endif
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \brief Board indices for the button HAL
*
* Those values are not meant to be modified by the user
* @{
*/
#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00
#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01
#define BOARD_BUTTON_HAL_INDEX_KEY_UP 0x02
#define BOARD_BUTTON_HAL_INDEX_KEY_DOWN 0x03
#define BOARD_BUTTON_HAL_INDEX_KEY_SELECT 0x04
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \name Device string used on startup
* @{

View File

@ -232,6 +232,19 @@
#endif
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \brief Board indices for the button HAL
*
* Those values are not meant to be modified by the user
* @{
*/
#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00
#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01
#define BOARD_BUTTON_HAL_INDEX_KEY_UP 0x02
#define BOARD_BUTTON_HAL_INDEX_KEY_DOWN 0x03
#define BOARD_BUTTON_HAL_INDEX_KEY_SELECT 0x04
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \name Device string used on startup
* @{

View File

@ -38,13 +38,11 @@
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "srf06/button-sensor.h"
#include "srf06/als-sensor.h"
#include <string.h>
/*---------------------------------------------------------------------------*/
/** \brief Exports a global symbol to be used by the sensor API */
SENSORS(&button_select_sensor, &button_left_sensor, &button_right_sensor,
&button_up_sensor, &button_down_sensor, &als_sensor);
SENSORS(&als_sensor);
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -33,7 +33,7 @@ PLATFORM_ROOT_DIR = $(CONTIKI)/arch/platform/$(TARGET)
### Include
CONTIKI_TARGET_SOURCEFILES += platform.c leds-arch.c
CONTIKI_TARGET_SOURCEFILES += leds.c cc1200-zoul-arch.c
CONTIKI_TARGET_SOURCEFILES += adc-zoul.c button-sensor.c zoul-sensors.c
CONTIKI_TARGET_SOURCEFILES += adc-zoul.c board-buttons.c zoul-sensors.c
CONTIKI_TARGET_SOURCEFILES += $(BOARD_SOURCEFILES)
CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES)

View File

@ -86,6 +86,8 @@
/*---------------------------------------------------------------------------*/
/* board.h assumes that basic configuration is done */
#include "board.h"
#define PLATFORM_SUPPORTS_BUTTON_HAL PLATFORM_HAS_BUTTON
/*---------------------------------------------------------------------------*/
/**
* \name Radio Configuration

View File

@ -1,10 +1,11 @@
/*
* Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
@ -29,29 +30,30 @@
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup sensortag-cc26xx-peripherals
* \addtogroup zoul
* @{
*
* \defgroup sensortag-cc26xx-reed-relay SensorTag 2.0 Reed Relay
*
* The reed relay acts like a button without a button. To trigger the reed,
* approach a magnet to the sensortag and a sensors_changed event will be
* generated, in a fashion similar to as if a button had been pressed
* \defgroup zoul-buttons Zoul user button
*
* Generic module controlling the user button on the Zoul
* @{
*
* \file
* Header file for the Sensortag Reed Relay
* Defines the Zoul user button for use with the button HAL
*/
/*---------------------------------------------------------------------------*/
#ifndef REED_RELAY_H
#define REED_RELAY_H
#include "contiki.h"
#include "dev/button-hal.h"
/*---------------------------------------------------------------------------*/
#include "lib/sensors.h"
#if PLATFORM_HAS_BUTTON
/*---------------------------------------------------------------------------*/
extern const struct sensors_sensor reed_relay_sensor;
BUTTON_HAL_BUTTON(button_user, "User button", \
GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_USER_PORT, BUTTON_USER_PIN), \
GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_USER_BUTTON, true);
/*---------------------------------------------------------------------------*/
#endif /* REED_RELAY_H */
BUTTON_HAL_BUTTONS(&button_user);
/*---------------------------------------------------------------------------*/
#endif
/*---------------------------------------------------------------------------*/
/**
* @}

View File

@ -1,178 +0,0 @@
/*
* Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
* Copyright (c) 2015, Zolertia - http://www.zolertia.com
* Copyright (c) 2015, University of Bristol - http://www.bristol.ac.uk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup zoul-button-sensor
* @{
*
* \file
* Driver for the Zoul user button
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "dev/nvic.h"
#include "dev/ioc.h"
#include "dev/gpio.h"
#include "dev/gpio-hal.h"
#include "dev/button-sensor.h"
#include "sys/timer.h"
#include "sys/ctimer.h"
#include "sys/process.h"
#include <stdint.h>
#include <string.h>
/*---------------------------------------------------------------------------*/
#define BUTTON_USER_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_USER_PORT)
#define BUTTON_USER_PIN_MASK GPIO_PIN_MASK(BUTTON_USER_PIN)
/*---------------------------------------------------------------------------*/
#define DEBOUNCE_DURATION (CLOCK_SECOND >> 4)
static struct timer debouncetimer;
/*---------------------------------------------------------------------------*/
static clock_time_t press_duration = 0;
static struct ctimer press_counter;
static uint8_t press_event_counter;
process_event_t button_press_duration_exceeded;
/*---------------------------------------------------------------------------*/
static void
duration_exceeded_callback(void *data)
{
press_event_counter++;
process_post(PROCESS_BROADCAST, button_press_duration_exceeded,
&press_event_counter);
ctimer_set(&press_counter, press_duration, duration_exceeded_callback,
NULL);
}
/*---------------------------------------------------------------------------*/
/**
* \brief Retrieves the value of the button pin
* \param type Returns the pin level or the counter of press duration events.
* type == BUTTON_SENSOR_VALUE_TYPE_LEVEL or
* type == BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION
* respectively
*/
static int
value(int type)
{
switch(type) {
case BUTTON_SENSOR_VALUE_TYPE_LEVEL:
return GPIO_READ_PIN(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
case BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION:
return press_event_counter;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static void
button_press_handler(gpio_hal_pin_mask_t pin_mask)
{
if(!timer_expired(&debouncetimer)) {
return;
}
timer_set(&debouncetimer, DEBOUNCE_DURATION);
if(press_duration) {
press_event_counter = 0;
if(value(BUTTON_SENSOR_VALUE_TYPE_LEVEL) == BUTTON_SENSOR_PRESSED_LEVEL) {
ctimer_set(&press_counter, press_duration, duration_exceeded_callback,
NULL);
} else {
ctimer_stop(&press_counter);
}
}
sensors_changed(&button_sensor);
}
/*---------------------------------------------------------------------------*/
static gpio_hal_event_handler_t press_handler = {
.next = NULL,
.handler = button_press_handler,
.pin_mask = gpio_hal_pin_to_mask(BUTTON_USER_PIN) << (BUTTON_USER_PORT << 3),
};
/*---------------------------------------------------------------------------*/
/**
* \brief Init function for the User button.
* \param type SENSORS_ACTIVE: Activate / Deactivate the sensor (value == 1
* or 0 respectively)
*
* \param value Depends on the value of the type argument
* \return Depends on the value of the type argument
*/
static int
config_user(int type, int value)
{
switch(type) {
case SENSORS_HW_INIT:
button_press_duration_exceeded = process_alloc_event();
/* Software controlled */
GPIO_SOFTWARE_CONTROL(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
/* Set pin to input */
GPIO_SET_INPUT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
/* Enable edge detection */
GPIO_DETECT_EDGE(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
/* Both Edges */
GPIO_TRIGGER_BOTH_EDGES(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
ioc_set_over(BUTTON_USER_PORT, BUTTON_USER_PIN, IOC_OVERRIDE_PUE);
gpio_hal_register_handler(&press_handler);
break;
case SENSORS_ACTIVE:
if(value) {
GPIO_ENABLE_INTERRUPT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
NVIC_EnableIRQ(BUTTON_USER_VECTOR);
} else {
GPIO_DISABLE_INTERRUPT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
NVIC_DisableIRQ(BUTTON_USER_VECTOR);
}
return value;
case BUTTON_SENSOR_CONFIG_TYPE_INTERVAL:
press_duration = (clock_time_t)value;
break;
default:
break;
}
return 1;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, value, config_user, NULL);
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -1,83 +0,0 @@
/*
* Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
* Copyright (c) 2015, Zolertia - http://www.zolertia.com
* Copyright (c) 2015, University of Bristol - http://www.bristol.ac.uk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup zoul-sensors
* @{
*
* \defgroup zoul-button-sensor Zoul User Button Driver
*
* Driver for the Zoul user button
*
* The user button (on Zoul-based platforms like the RE-Mote and the Firefly)
* will generate a sensors_changed event on press as well as on release.
*
* Unlike many other platforms, the user button has the ability to
* generate events when the user keeps the button pressed. The user can
* configure the button driver with a timer interval in clock ticks. When the
* button is kept pressed, the driver will then generate a broadcast event
* each time the interval passes. For example the driver can be configured to
* generate an event every second while the button is kept pressed. This
* functionality can be enabled through the configure() function, by passing
* BUTTON_SENSOR_CONFIG_TYPE_INTERVAL as the type argument.
* @{
*
* \file
* Header file for the Zoul User Button Driver
*/
/*---------------------------------------------------------------------------*/
#ifndef BUTTON_SENSOR_H_
#define BUTTON_SENSOR_H_
/*---------------------------------------------------------------------------*/
#include "lib/sensors.h"
/*---------------------------------------------------------------------------*/
#define BUTTON_SENSOR "Button"
extern const struct sensors_sensor button_sensor;
/*---------------------------------------------------------------------------*/
extern process_event_t button_press_duration_exceeded;
/*---------------------------------------------------------------------------*/
#define BUTTON_SENSOR_CONFIG_TYPE_INTERVAL 0x0100
#define BUTTON_SENSOR_VALUE_TYPE_LEVEL 0
#define BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION 1
#define BUTTON_SENSOR_PRESSED_LEVEL 0
#define BUTTON_SENSOR_RELEASED_LEVEL 8
/*---------------------------------------------------------------------------*/
#endif /* BUTTON_SENSOR_H_ */
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
*/

View File

@ -47,12 +47,7 @@
#include <string.h>
/*---------------------------------------------------------------------------*/
/** \brief Exports global symbols for the sensor API */
SENSORS(&vdd3_sensor,
#if PLATFORM_HAS_BUTTON
&button_sensor,
#endif
&cc2538_temp_sensor
);
SENSORS(&vdd3_sensor, &cc2538_temp_sensor);
/*---------------------------------------------------------------------------*/
/**
* @}

View File

@ -48,7 +48,6 @@
/*---------------------------------------------------------------------------*/
#include "lib/sensors.h"
#include "dev/cc2538-sensors.h"
#include "dev/button-sensor.h"
/*---------------------------------------------------------------------------*/
/**
* \name Zoul sensor constants

View File

@ -54,8 +54,10 @@
#include "dev/udma.h"
#include "dev/crypto.h"
#include "dev/rtcc.h"
#include "dev/button-hal.h"
#include "usb/usb-serial.h"
#include "lib/random.h"
#include "lib/sensors.h"
#include "net/netstack.h"
#include "net/mac/framer/frame802154.h"
#include "net/linkaddr.h"
@ -221,6 +223,10 @@ platform_init_stage_two()
/* Populate linkaddr_node_addr */
ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE);
#if PLATFORM_HAS_BUTTON
button_hal_init();
#endif
INTERRUPTS_ENABLE();
fade(LEDS_BLUE);
@ -241,10 +247,6 @@ platform_init_stage_three()
process_start(&sensors_process, NULL);
#if PLATFORM_HAS_BUTTON
SENSORS_ACTIVATE(button_sensor);
#endif
fade(LEDS_GREEN);
}
/*---------------------------------------------------------------------------*/

View File

@ -43,7 +43,11 @@
#include "contiki-net.h"
#include "coap-engine.h"
#include "coap-blocking-api.h"
#if PLATFORM_SUPPORTS_BUTTON_HAL
#include "dev/button-hal.h"
#else
#include "dev/button-sensor.h"
#endif
/* Log configuration */
#include "coap-log.h"
@ -94,9 +98,11 @@ PROCESS_THREAD(er_example_client, ev, data)
etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND);
#if PLATFORM_HAS_BUTTON
#if !PLATFORM_SUPPORTS_BUTTON_HAL
SENSORS_ACTIVATE(button_sensor);
printf("Press a button to request %s\n", service_urls[uri_switch]);
#endif
printf("Press a button to request %s\n", service_urls[uri_switch]);
#endif /* PLATFORM_HAS_BUTTON */
while(1) {
PROCESS_YIELD();
@ -122,7 +128,11 @@ PROCESS_THREAD(er_example_client, ev, data)
etimer_reset(&et);
#if PLATFORM_HAS_BUTTON
#if PLATFORM_SUPPORTS_BUTTON_HAL
} else if(ev == button_hal_release_event) {
#else
} else if(ev == sensors_event && data == &button_sensor) {
#endif
/* send a request to notify the end of the process */
@ -140,7 +150,7 @@ PROCESS_THREAD(er_example_client, ev, data)
printf("\n--Done--\n");
uri_switch = (uri_switch + 1) % NUMBER_OF_URLS;
#endif
#endif /* PLATFORM_HAS_BUTTON */
}
}

View File

@ -42,8 +42,11 @@
#include "contiki.h"
#include "contiki-net.h"
#include "coap-engine.h"
#if PLATFORM_SUPPORTS_BUTTON_HAL
#include "dev/button-hal.h"
#else
#include "dev/button-sensor.h"
#endif
/*----------------------------------------------------------------------------*/
#define DEBUG 0
#if DEBUG
@ -147,9 +150,12 @@ PROCESS_THREAD(er_example_observe_client, ev, data)
/* init timer and button (if available) */
etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND);
#if PLATFORM_HAS_BUTTON
#if !PLATFORM_SUPPORTS_BUTTON_HAL
SENSORS_ACTIVATE(button_sensor);
printf("Press a button to start/stop observation of remote resource\n");
#endif
printf("Press a button to start/stop observation of remote resource\n");
#endif /* PLATFORM_HAS_BUTTON */
/* toggle observation every time the timer elapses or the button is pressed */
while(1) {
PROCESS_YIELD();
@ -159,11 +165,15 @@ PROCESS_THREAD(er_example_observe_client, ev, data)
printf("\n--Done--\n");
etimer_reset(&et);
#if PLATFORM_HAS_BUTTON
#if PLATFORM_SUPPORTS_BUTTON_HAL
} else if(ev == button_hal_release_event) {
#else
} else if(ev == sensors_event && data == &button_sensor) {
#endif
printf("--Toggle tutton--\n");
toggle_observation();
printf("\n--Done--\n");
#endif
#endif /* PLATFORM_HAS_BUTTON */
}
}
PROCESS_END();

View File

@ -42,7 +42,9 @@
#include "contiki.h"
#include "coap-engine.h"
#if PLATFORM_HAS_BUTTON
#if PLATFORM_SUPPORTS_BUTTON_HAL
#include "dev/button-hal.h"
#else
#include "dev/button-sensor.h"
#endif
@ -165,7 +167,11 @@ PROCESS_THREAD(er_example_server, ev, data)
while(1) {
PROCESS_WAIT_EVENT();
#if PLATFORM_HAS_BUTTON
#if PLATFORM_SUPPORTS_BUTTON_HAL
if(ev == button_hal_release_event) {
#else
if(ev == sensors_event && data == &button_sensor) {
#endif
PRINTF("*******BUTTON*******\n");
/* Call the event_handler for this application-specific event. */

View File

@ -0,0 +1,8 @@
CONTIKI_PROJECT = button-hal-example
CONTIKI = ../../..
all: $(CONTIKI_PROJECT)
PLATFORMS_ONLY = srf06-cc26xx cc2538dk openmote-cc2538 zoul
include $(CONTIKI)/Makefile.include

View File

@ -0,0 +1,15 @@
# Button HAL Example
This example demonstrates and tests the functionality of the Button HAL.
You can use this example to:
* Understand the logic of the button HAL.
* Test your implementation of arch-specific button HAL components if you are
developing a new port.
This example assumes a device with at least one switch (button or simiar).
# Supported devices
This example is expected to work off-the-shelf on the following boards:
* All CC13xx/CC26xx devices
* All CC2538 devices

View File

@ -0,0 +1,83 @@
/*
* Copyright (c) 2018, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "dev/button-hal.h"
#include <stdio.h>
/*---------------------------------------------------------------------------*/
PROCESS(button_hal_example, "Button HAL Example");
AUTOSTART_PROCESSES(&button_hal_example);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(button_hal_example, ev, data)
{
button_hal_button_t *btn;
PROCESS_BEGIN();
btn = button_hal_get_by_index(0);
printf("Button HAL example.\n");
printf("Device button count: %u.\n", button_hal_button_count);
printf("%s on pin %u with ID=0, Logic=%s, Pull=%s\n",
BUTTON_HAL_GET_DESCRIPTION(btn), btn->pin,
btn->negative_logic ? "Negative" : "Positive",
btn->pull == GPIO_HAL_PIN_CFG_PULL_UP ? "Pull Up" : "Pull Down");
while(1) {
PROCESS_YIELD();
if(ev == button_hal_press_event) {
btn = (button_hal_button_t *)data;
printf("Press event (%s)\n", BUTTON_HAL_GET_DESCRIPTION(btn));
if(btn == button_hal_get_by_id(BUTTON_HAL_ID_BUTTON_ZERO)) {
printf("This was button 0, on pin %u\n", btn->pin);
}
} else if(ev == button_hal_release_event) {
btn = (button_hal_button_t *)data;
printf("Release event (%s)\n", BUTTON_HAL_GET_DESCRIPTION(btn));
} else if(ev == button_hal_periodic_event) {
btn = (button_hal_button_t *)data;
printf("Periodic event, %u seconds (%s)\n", btn->press_duration_seconds,
BUTTON_HAL_GET_DESCRIPTION(btn));
if(btn->press_duration_seconds > 5) {
printf("%s pressed for more than 5 secs. Do custom action\n",
BUTTON_HAL_GET_DESCRIPTION(btn));
}
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/

View File

@ -1,10 +1,11 @@
/*
* Copyright (c) 2015, Texas Instruments Incorporated - http://www.ti.com/
* Copyright (c) 2018, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
@ -28,20 +29,11 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup launchpad-peripherals
* @{
*
* \file
* Generic module controlling LaunchPad sensors
*/
#ifndef PROJECT_CONF_H_
#define PROJECT_CONF_H_
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "launchpad/button-sensor.h"
#include <string.h>
/* Force button descriptions */
#define BUTTON_HAL_CONF_WITH_DESCRIPTION 1
/*---------------------------------------------------------------------------*/
/** \brief Exports a global symbol to be used by the sensor API */
SENSORS(&button_left_sensor, &button_right_sensor);
#endif /* PROJECT_CONF_H_ */
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -33,7 +33,7 @@
#include "dev/gpio-hal.h"
#include "sys/etimer.h"
#include "lib/sensors.h"
#include "dev/button-sensor.h"
#include "dev/button-hal.h"
#include <stdio.h>
/*---------------------------------------------------------------------------*/
@ -129,8 +129,9 @@ PROCESS_THREAD(gpio_hal_example, ev, data)
counter++;
etimer_set(&et, CLOCK_SECOND);
} else if(ev == sensors_event && data == &button_sensor) {
printf("Button event\n");
} else if(ev == button_hal_release_event) {
printf("Button release event %s\n",
BUTTON_HAL_GET_DESCRIPTION((button_hal_button_t *)data));
}
}

View File

@ -50,7 +50,7 @@
#include "sys/etimer.h"
#include "sys/ctimer.h"
#include "lib/sensors.h"
#include "dev/button-sensor.h"
#include "dev/button-hal.h"
#include "dev/leds.h"
#include "dev/cc2538-sensors.h"
@ -138,7 +138,7 @@ static uint8_t state;
#define DEFAULT_RSSI_MEAS_INTERVAL (CLOCK_SECOND * 30)
/*---------------------------------------------------------------------------*/
/* Take a sensor reading on button press */
#define PUBLISH_TRIGGER &button_sensor
#define PUBLISH_TRIGGER BUTTON_HAL_ID_USER_BUTTON
/* Payload length of ICMPv6 echo requests used to measure RSSI with def rt */
#define ECHO_REQ_PAYLOAD_LEN 20
@ -704,7 +704,8 @@ PROCESS_THREAD(mqtt_demo_process, ev, data)
PROCESS_YIELD();
if(ev == sensors_event && data == PUBLISH_TRIGGER) {
if(ev == button_hal_release_event &&
((button_hal_button_t *)data)->unique_id == PUBLISH_TRIGGER) {
if(state == STATE_ERROR) {
connect_attempt = 1;
state = STATE_REGISTERED;
@ -713,7 +714,8 @@ PROCESS_THREAD(mqtt_demo_process, ev, data)
if((ev == PROCESS_EVENT_TIMER && data == &publish_periodic_timer) ||
ev == PROCESS_EVENT_POLL ||
(ev == sensors_event && data == PUBLISH_TRIGGER)) {
(ev == button_hal_release_event &&
((button_hal_button_t *)data)->unique_id == PUBLISH_TRIGGER)) {
state_machine();
}

View File

@ -45,7 +45,6 @@
/* User configuration */
#define MQTT_DEMO_STATUS_LED LEDS_GREEN
#define MQTT_DEMO_PUBLISH_TRIGGER &button_right_sensor
/* If undefined, the demo will attempt to connect to IBM's quickstart */
#define MQTT_DEMO_BROKER_IP_ADDR "fd00::1"

View File

@ -62,8 +62,8 @@
* - sensors : Some sensortag sensors are read asynchronously (see sensor
* documentation). For those, this example will print out
* readings in a staggered fashion at a random interval
* - Buttons : CC26XX_DEMO_SENSOR_1 button will toggle CC26XX_DEMO_LEDS_BUTTON
* - CC26XX_DEMO_SENSOR_2 turns on LEDS_REBOOT and causes a
* - Buttons : CC26XX_DEMO_TRIGGER_1 button will toggle CC26XX_DEMO_LEDS_BUTTON
* - CC26XX_DEMO_TRIGGER_2 turns on LEDS_REBOOT and causes a
* watchdog reboot
* - The remaining buttons will just print something
* - The example also shows how to retrieve the duration of a
@ -81,6 +81,7 @@
#include "sys/ctimer.h"
#include "dev/leds.h"
#include "dev/watchdog.h"
#include "dev/button-hal.h"
#include "random.h"
#include "button-sensor.h"
#include "batmon-sensor.h"
@ -97,23 +98,11 @@
#define CC26XX_DEMO_LEDS_BUTTON LEDS_RED
#define CC26XX_DEMO_LEDS_REBOOT LEDS_ALL
/*---------------------------------------------------------------------------*/
#define CC26XX_DEMO_SENSOR_NONE (void *)0xFFFFFFFF
#define CC26XX_DEMO_SENSOR_1 &button_left_sensor
#define CC26XX_DEMO_SENSOR_2 &button_right_sensor
#define CC26XX_DEMO_TRIGGER_1 BOARD_BUTTON_HAL_INDEX_KEY_LEFT
#define CC26XX_DEMO_TRIGGER_2 BOARD_BUTTON_HAL_INDEX_KEY_RIGHT
#if BOARD_SENSORTAG
#define CC26XX_DEMO_SENSOR_3 CC26XX_DEMO_SENSOR_NONE
#define CC26XX_DEMO_SENSOR_4 CC26XX_DEMO_SENSOR_NONE
#define CC26XX_DEMO_SENSOR_5 &reed_relay_sensor
#elif BOARD_LAUNCHPAD
#define CC26XX_DEMO_SENSOR_3 CC26XX_DEMO_SENSOR_NONE
#define CC26XX_DEMO_SENSOR_4 CC26XX_DEMO_SENSOR_NONE
#define CC26XX_DEMO_SENSOR_5 CC26XX_DEMO_SENSOR_NONE
#else
#define CC26XX_DEMO_SENSOR_3 &button_up_sensor
#define CC26XX_DEMO_SENSOR_4 &button_down_sensor
#define CC26XX_DEMO_SENSOR_5 &button_select_sensor
#define CC26XX_DEMO_TRIGGER_3 BOARD_BUTTON_HAL_INDEX_REED_RELAY
#endif
/*---------------------------------------------------------------------------*/
static struct etimer et;
@ -343,10 +332,6 @@ get_sync_sensor_readings(void)
static void
init_sensors(void)
{
#if BOARD_SENSORTAG
SENSORS_ACTIVATE(reed_relay_sensor);
#endif
SENSORS_ACTIVATE(batmon_sensor);
}
/*---------------------------------------------------------------------------*/
@ -392,46 +377,41 @@ PROCESS_THREAD(cc26xx_demo_process, ev, data)
etimer_set(&et, CC26XX_DEMO_LOOP_INTERVAL);
}
} else if(ev == sensors_event) {
if(data == CC26XX_DEMO_SENSOR_1) {
printf("Left: Pin %d, press duration %d clock ticks\n",
(CC26XX_DEMO_SENSOR_1)->value(BUTTON_SENSOR_VALUE_STATE),
(CC26XX_DEMO_SENSOR_1)->value(BUTTON_SENSOR_VALUE_DURATION));
} else if(ev == button_hal_periodic_event) {
button_hal_button_t *button = data;
if((CC26XX_DEMO_SENSOR_1)->value(BUTTON_SENSOR_VALUE_DURATION) >
CLOCK_SECOND) {
printf("Long button press!\n");
}
printf("%s periodic event, duration %d seconds\n",
BUTTON_HAL_GET_DESCRIPTION(button),
button->press_duration_seconds);
} else if(ev == button_hal_release_event) {
button_hal_button_t *btn = (button_hal_button_t *)data;
printf("%s release event\n", BUTTON_HAL_GET_DESCRIPTION(btn));
if(btn->unique_id== CC26XX_DEMO_TRIGGER_1) {
leds_toggle(CC26XX_DEMO_LEDS_BUTTON);
} else if(data == CC26XX_DEMO_SENSOR_2) {
} else if(btn->unique_id == CC26XX_DEMO_TRIGGER_2) {
leds_on(CC26XX_DEMO_LEDS_REBOOT);
watchdog_reboot();
} else if(data == CC26XX_DEMO_SENSOR_3) {
printf("Up\n");
} else if(data == CC26XX_DEMO_SENSOR_4) {
printf("Down\n");
} else if(data == CC26XX_DEMO_SENSOR_5) {
#if BOARD_SENSORTAG
} else if(btn->unique_id == CC26XX_DEMO_TRIGGER_3) {
if(buzzer_state()) {
buzzer_stop();
} else {
buzzer_start(1000);
}
} else if(ev == sensors_event && data == &bmp_280_sensor) {
}
} else if(ev == sensors_event) {
if(data == &bmp_280_sensor) {
get_bmp_reading();
} else if(ev == sensors_event && data == &opt_3001_sensor) {
} else if(data == &opt_3001_sensor) {
get_light_reading();
} else if(ev == sensors_event && data == &hdc_1000_sensor) {
} else if(data == &hdc_1000_sensor) {
get_hdc_reading();
} else if(ev == sensors_event && data == &tmp_007_sensor) {
} else if(data == &tmp_007_sensor) {
get_tmp_reading();
} else if(ev == sensors_event && data == &mpu_9250_sensor) {
} else if(data == &mpu_9250_sensor) {
get_mpu_reading();
#elif BOARD_SMARTRF06EB
printf("Sel: Pin %d, press duration %d clock ticks\n",
button_select_sensor.value(BUTTON_SENSOR_VALUE_STATE),
button_select_sensor.value(BUTTON_SENSOR_VALUE_DURATION));
#endif
}
}

View File

@ -45,7 +45,7 @@
#include "lib/list.h"
#include "sys/process.h"
#include "net/ipv6/sicslowpan.h"
#include "button-sensor.h"
#include "dev/button-hal.h"
#include "batmon-sensor.h"
#include "httpd-simple.h"
#include "cc26xx-web-demo.h"
@ -882,8 +882,6 @@ init_sensors(void)
list_add(sensor_list, &mpu_gyro_x_reading);
list_add(sensor_list, &mpu_gyro_y_reading);
list_add(sensor_list, &mpu_gyro_z_reading);
SENSORS_ACTIVATE(reed_relay_sensor);
#endif
}
/*---------------------------------------------------------------------------*/
@ -977,16 +975,16 @@ PROCESS_THREAD(cc26xx_web_demo_process, ev, data)
}
#endif
if(ev == sensors_event && data == CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER) {
if((CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER)->value(
BUTTON_SENSOR_VALUE_DURATION) > CLOCK_SECOND * 5) {
printf("Restoring defaults!\n");
cc26xx_web_demo_restore_defaults();
} else {
init_sensor_readings();
process_post(PROCESS_BROADCAST, cc26xx_web_demo_publish_event, NULL);
}
if(ev == button_hal_release_event &&
((button_hal_button_t *)data)->unique_id ==
CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER) {
init_sensor_readings();
process_post(PROCESS_BROADCAST, cc26xx_web_demo_publish_event, NULL);
} else if(ev == button_hal_periodic_event &&
((button_hal_button_t *)data)->unique_id ==
CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER) {
printf("Restoring defaults!\n");
cc26xx_web_demo_restore_defaults();
} else if(ev == httpd_simple_event_new_config) {
save_config();
#if BOARD_SENSORTAG

View File

@ -98,18 +98,18 @@
/*---------------------------------------------------------------------------*/
/* User configuration */
/* Take a sensor reading on button press */
#define CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER &button_left_sensor
#define CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER BOARD_BUTTON_HAL_INDEX_KEY_LEFT
/* Payload length of ICMPv6 echo requests used to measure RSSI with def rt */
#define CC26XX_WEB_DEMO_ECHO_REQ_PAYLOAD_LEN 20
#if BOARD_SENSORTAG
/* Force an MQTT publish on sensor event */
#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER &reed_relay_sensor
#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER BOARD_BUTTON_HAL_INDEX_REED_RELAY
#elif BOARD_LAUNCHPAD
#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER &button_left_sensor
#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER BOARD_BUTTON_HAL_INDEX_KEY_LEFT
#else
#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER &button_down_sensor
#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER BOARD_BUTTON_HAL_INDEX_KEY_DOWN
#endif
#define CC26XX_WEB_DEMO_STATUS_LED LEDS_GREEN

View File

@ -43,7 +43,7 @@
#include "sys/etimer.h"
#include "sys/ctimer.h"
#include "lib/sensors.h"
#include "button-sensor.h"
#include "dev/button-hal.h"
#include "board-peripherals.h"
#include "cc26xx-web-demo.h"
#include "dev/leds.h"
@ -882,10 +882,14 @@ PROCESS_THREAD(mqtt_client_process, ev, data)
PROCESS_YIELD();
if(ev == sensors_event && data == CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER) {
if(state == MQTT_CLIENT_STATE_ERROR) {
connect_attempt = 1;
state = MQTT_CLIENT_STATE_REGISTERED;
if(ev == button_hal_release_event) {
button_hal_button_t *btn = (button_hal_button_t *)data;
if(btn->unique_id == CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER) {
if(state == MQTT_CLIENT_STATE_ERROR) {
connect_attempt = 1;
state = MQTT_CLIENT_STATE_REGISTERED;
}
}
}
@ -901,7 +905,9 @@ PROCESS_THREAD(mqtt_client_process, ev, data)
if((ev == PROCESS_EVENT_TIMER && data == &publish_periodic_timer) ||
ev == PROCESS_EVENT_POLL ||
ev == cc26xx_web_demo_publish_event ||
(ev == sensors_event && data == CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER)) {
(ev == button_hal_release_event &&
((button_hal_button_t *)data)->unique_id ==
CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER)) {
state_machine();
}

View File

@ -31,9 +31,6 @@
#ifndef PROJECT_CONF_H_
#define PROJECT_CONF_H_
/*---------------------------------------------------------------------------*/
/* Disable button shutdown functionality */
#define BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN 0
/*---------------------------------------------------------------------------*/
/* Enable the ROM bootloader */
#define ROM_BOOTLOADER_ENABLE 1
/*---------------------------------------------------------------------------*/

View File

@ -34,7 +34,7 @@
#include "sys/process.h"
#include "dev/leds.h"
#include "dev/watchdog.h"
#include "button-sensor.h"
#include "dev/button-hal.h"
#include "batmon-sensor.h"
#include "board-peripherals.h"
#include "net/netstack.h"
@ -63,6 +63,8 @@
#define VERY_SLEEPY_MODE_OFF 0
#define VERY_SLEEPY_MODE_ON 1
/*---------------------------------------------------------------------------*/
#define BUTTON_TRIGGER BOARD_BUTTON_HAL_INDEX_KEY_LEFT
/*---------------------------------------------------------------------------*/
#define MAC_CAN_BE_TURNED_OFF 0
#define MAC_MUST_STAY_ON 1
@ -358,7 +360,8 @@ PROCESS_THREAD(very_sleepy_demo_process, ev, data)
PROCESS_YIELD();
if(ev == sensors_event && data == &button_left_sensor) {
if(ev == button_hal_release_event &&
((button_hal_button_t *)data)->unique_id == BUTTON_TRIGGER) {
switch_to_normal();
}
@ -368,7 +371,8 @@ PROCESS_THREAD(very_sleepy_demo_process, ev, data)
}
if((ev == PROCESS_EVENT_TIMER && data == &et_periodic) ||
(ev == sensors_event && data == &button_left_sensor) ||
(ev == button_hal_release_event &&
((button_hal_button_t *)data)->unique_id == BUTTON_TRIGGER) ||
(ev == event_new_config)) {
/*

View File

@ -34,7 +34,7 @@
#include "ipv6/ip64-addr.h"
#include "dev/leds.h"
#include "net/routing/routing.h"
#include "dev/button-sensor.h"
#include "dev/button-hal.h"
#include <stdio.h>
/*---------------------------------------------------------------------------*/
static struct http_socket s;
@ -138,13 +138,10 @@ PROCESS_THREAD(http_example_process, ev, data)
while(1) {
PROCESS_YIELD();
if((ev == sensors_event) && (data == &button_sensor)) {
if(button_sensor.value(BUTTON_SENSOR_VALUE_TYPE_LEVEL) ==
BUTTON_SENSOR_PRESSED_LEVEL) {
leds_on(LEDS_GREEN);
printf("Button pressed! sending a POST to IFTTT\n");
http_socket_post(&s, url_buffer, NULL, 0, NULL, callback, NULL);
}
if(ev == button_hal_release_event) {
leds_on(LEDS_GREEN);
printf("Button pressed! sending a POST to IFTTT\n");
http_socket_post(&s, url_buffer, NULL, 0, NULL, callback, NULL);
}
}

View File

@ -47,7 +47,7 @@
#include <stdio.h>
#include "contiki.h"
#include "dev/rgb-bl-lcd.h"
#include "dev/button-sensor.h"
#include "dev/button-hal.h"
/*---------------------------------------------------------------------------*/
#define SCROLL_PERIOD (CLOCK_SECOND / 6)
/*---------------------------------------------------------------------------*/
@ -104,18 +104,13 @@ PROCESS_THREAD(remote_lcd_process, ev, data)
printf("Counter: %05u\n", counter);
counter++;
etimer_restart(&et);
} else if(ev == sensors_event) {
if(data == &button_sensor) {
if(button_sensor.value(BUTTON_SENSOR_VALUE_TYPE_LEVEL) ==
BUTTON_SENSOR_PRESSED_LEVEL) {
printf("Button pressed!!\n");
lcd_set_cursor(0, LCD_RGB_1ST_ROW);
lcd_write("Button pressed!!");
} else {
lcd_set_cursor(0, LCD_RGB_1ST_ROW);
lcd_write("Press the button!");
}
}
} else if(ev == button_hal_press_event) {
printf("Button pressed!!\n");
lcd_set_cursor(0, LCD_RGB_1ST_ROW);
lcd_write("Button pressed!!");
} else if(ev == button_hal_release_event) {
lcd_set_cursor(0, LCD_RGB_1ST_ROW);
lcd_write("Press the button!");
}
}

231
os/dev/button-hal.c Normal file
View File

@ -0,0 +1,231 @@
/*
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup button_hal
* @{
*
* \file
* Platform-independent button driver.
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "sys/process.h"
#include "sys/ctimer.h"
#include "sys/critical.h"
#include "dev/gpio-hal.h"
#include "dev/button-hal.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
/*---------------------------------------------------------------------------*/
PROCESS(button_hal_process, "Button HAL process");
/*---------------------------------------------------------------------------*/
process_event_t button_hal_press_event;
process_event_t button_hal_release_event;
process_event_t button_hal_periodic_event;
/*---------------------------------------------------------------------------*/
/* A mask of all pins that have changed state since the last process poll */
static volatile gpio_hal_pin_mask_t pmask;
/*---------------------------------------------------------------------------*/
extern button_hal_button_t *button_hal_buttons[];
/*---------------------------------------------------------------------------*/
/* Common handler for all handler events, and register it with the GPIO HAL */
static gpio_hal_event_handler_t button_event_handler;
/*---------------------------------------------------------------------------*/
static void
duration_exceeded_callback(void *btn)
{
button_hal_button_t *button = (button_hal_button_t *)btn;
button->press_duration_seconds++;
ctimer_set(&button->duration_ctimer, CLOCK_SECOND,
duration_exceeded_callback, button);
process_post(PROCESS_BROADCAST, button_hal_periodic_event, button);
}
/*---------------------------------------------------------------------------*/
static void
debounce_handler(void *btn)
{
button_hal_button_t *button;
int expired;
uint8_t button_state;
button = (button_hal_button_t *)btn;
/*
* A new debounce may have been triggered after expiration of the previous
* one but before we got called.
*/
if(!ctimer_expired(&button->debounce_ctimer)) {
return;
}
expired = ctimer_expired(&button->duration_ctimer);
button_state = button_hal_get_state(button);
/*
* A debounce timer expired. Inspect the button's state. If the button's
* state is the same as it was before, then we ignore this as noise.
*/
if(button_state == BUTTON_HAL_STATE_PRESSED && expired) {
/*
* Button is pressed and no tick counter running. Treat as new press.
* Include the debounce duration in the first periodic, so that the
* callback will happen 1 second after the button press, not 1 second
* after the end of the debounce. Notify process about the press event.
*/
button->press_duration_seconds = 0;
ctimer_set(&button->duration_ctimer,
CLOCK_SECOND - BUTTON_HAL_DEBOUNCE_DURATION,
duration_exceeded_callback, button);
process_post(PROCESS_BROADCAST, button_hal_press_event, button);
} else if(button_state == BUTTON_HAL_STATE_RELEASED && expired == 0) {
/*
* Button is released and there is a duration_ctimer running. Treat this
* as a new release and notify processes.
*/
ctimer_stop(&button->duration_ctimer);
process_post(PROCESS_BROADCAST, button_hal_release_event, button);
}
}
/*---------------------------------------------------------------------------*/
static void
press_release_handler(gpio_hal_pin_mask_t pin_mask)
{
pmask |= pin_mask;
process_poll(&button_hal_process);
}
/*---------------------------------------------------------------------------*/
button_hal_button_t *
button_hal_get_by_id(uint8_t unique_id)
{
button_hal_button_t **button;
for(button = button_hal_buttons; *button != NULL; button++) {
if((*button)->unique_id == unique_id) {
return *button;
}
}
return NULL;
}
/*---------------------------------------------------------------------------*/
button_hal_button_t *
button_hal_get_by_index(uint8_t index)
{
if(index >= button_hal_button_count) {
return NULL;
}
return button_hal_buttons[index];
}
/*---------------------------------------------------------------------------*/
uint8_t
button_hal_get_state(button_hal_button_t *button)
{
uint8_t pin_state = gpio_hal_arch_read_pin(button->pin);
if((pin_state == 0 && button->negative_logic == true) ||
(pin_state == 1 && button->negative_logic == false)) {
return BUTTON_HAL_STATE_PRESSED;
}
return BUTTON_HAL_STATE_RELEASED;
}
/*---------------------------------------------------------------------------*/
void
button_hal_init()
{
button_hal_button_t **button;
gpio_hal_pin_cfg_t cfg;
button_hal_press_event = process_alloc_event();
button_hal_release_event = process_alloc_event();
button_hal_periodic_event = process_alloc_event();
button_event_handler.pin_mask = 0;
button_event_handler.handler = press_release_handler;
for(button = button_hal_buttons; *button != NULL; button++) {
cfg = GPIO_HAL_PIN_CFG_EDGE_BOTH | GPIO_HAL_PIN_CFG_INT_ENABLE |
(*button)->pull;
gpio_hal_arch_pin_set_input((*button)->pin);
gpio_hal_arch_pin_cfg_set((*button)->pin, cfg);
gpio_hal_arch_interrupt_enable((*button)->pin);
button_event_handler.pin_mask |= gpio_hal_pin_to_mask((*button)->pin);
}
process_start(&button_hal_process, NULL);
gpio_hal_register_handler(&button_event_handler);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(button_hal_process, ev, data)
{
int_master_status_t status;
gpio_hal_pin_mask_t pins;
button_hal_button_t **button;
PROCESS_BEGIN();
while(1) {
PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
status = critical_enter();
pins = pmask;
pmask = 0;
critical_exit(status);
for(button = button_hal_buttons; *button != NULL; button++) {
if(gpio_hal_pin_to_mask((*button)->pin) & pins) {
/* Ignore all button presses/releases during its debounce */
if(ctimer_expired(&(*button)->debounce_ctimer)) {
/*
* Here we merely set a debounce timer. At the end of the debounce we
* will inspect the button's state and we will take action only if it
* has changed.
*
* This is to prevent erroneous edge detections due to interference.
*/
ctimer_set(&(*button)->debounce_ctimer, BUTTON_HAL_DEBOUNCE_DURATION,
debounce_handler, *button);
}
}
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/** @} */

270
os/dev/button-hal.h Normal file
View File

@ -0,0 +1,270 @@
/*
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup dev
* @{
*/
/*---------------------------------------------------------------------------*/
/**
* \defgroup button_hal Button HAL
*
* Hardware abstraction layer for user buttons.
*
* This HAL enables an abstraction of general-purpose / user buttons or
* similar peripherals (e.g. a reed relay can also be abstracted through this
* HAL). The HAL handles software debounce timers internally, therefore the
* platform-specific button driver does not need to worry about debouncing.
*
* The platform developer needs to define a variable of type
* \c button_hal_button_t for each user button. Within this variable, the
* developer needs to specify the GPIO pin where the button is attached,
* whether the button uses negative logic, and whether the GPIO pin should
* be configured with internal pullup/down. The developer also needs to provide
* a unique index for each button, as well as a description.
*
* With those in place, the HAL will generate the following process events:
*
* - button_hal_press_event: Upon press of the button
* - button_hal_release_event: Upon release of the button
* - button_hal_periodic_event: Generated every second that the user button is
* kept pressed.
*
* With those events in place, an application can perform an action:
*
* - Immediately after the button gets pressed.
* - After the button has been pressed for N seconds.
* - Immediately upon release of the button. This action can vary depending
* on how long the button had been pressed for.
*
* A platform with user buttons can either implement this API (recommended) or
* the older button_sensor API. Some examples will not work if this API is not
* implemented.
*
* This API requires the platform to first support the GPIO HAL API.
* @{
*
* \file
* Header file for the button HAL
*/
/*---------------------------------------------------------------------------*/
#ifndef BUTTON_HAL_H_
#define BUTTON_HAL_H_
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "dev/gpio-hal.h"
#include "sys/clock.h"
#include "sys/ctimer.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
/*---------------------------------------------------------------------------*/
/**
* \brief Controls the software debounce timer duration.
*
* The platform can provide a more suitable value. This value will apply to
* all buttons.
*/
#ifdef BUTTON_HAL_CONF_DEBOUNCE_DURATION
#define BUTTON_HAL_DEBOUNCE_DURATION BUTTON_HAL_CONF_DEBOUNCE_DURATION
#else
#define BUTTON_HAL_DEBOUNCE_DURATION (CLOCK_SECOND >> 6)
#endif
/*---------------------------------------------------------------------------*/
/**
* \brief Controls whether buttons will have human-readable names
*
* Define this to zero to save code space
*/
#if BUTTON_HAL_CONF_WITH_DESCRIPTION
#define BUTTON_HAL_WITH_DESCRIPTION BUTTON_HAL_CONF_WITH_DESCRIPTION
#else
#define BUTTON_HAL_WITH_DESCRIPTION 1
#endif
/*---------------------------------------------------------------------------*/
#define BUTTON_HAL_STATE_RELEASED 0
#define BUTTON_HAL_STATE_PRESSED 1
/*---------------------------------------------------------------------------*/
/**
* Optional button IDs
*/
#define BUTTON_HAL_ID_BUTTON_ZERO 0x00
#define BUTTON_HAL_ID_BUTTON_ONE 0x01
#define BUTTON_HAL_ID_BUTTON_TWO 0x02
#define BUTTON_HAL_ID_BUTTON_THREE 0x03
#define BUTTON_HAL_ID_BUTTON_FOUR 0x04
#define BUTTON_HAL_ID_BUTTON_FIVE 0x05
#define BUTTON_HAL_ID_USER_BUTTON BUTTON_HAL_ID_BUTTON_ZERO
/*---------------------------------------------------------------------------*/
/**
* \brief A logical representation of a user button
*/
typedef struct button_hal_button_s button_hal_button_t;
struct button_hal_button_s {
/** Used by the s/w debounce functionality */
struct ctimer debounce_ctimer;
/** A callback timer used to count duration of button presses */
struct ctimer duration_ctimer;
#if BUTTON_HAL_WITH_DESCRIPTION
/**
* \brief A textual description of the button
*
* This field may only be accessed using the BUTTON_HAL_GET_DESCRIPTION()
* macro.
*/
const char *description;
#endif
/** True if the button uses negative logic (active: low) */
const bool negative_logic;
/** The gpio pin connected to the button */
const gpio_hal_pin_t pin;
/** The pin's pull configuration */
const gpio_hal_pin_cfg_t pull;
/** A counter of the duration (in seconds) of a button press */
uint8_t press_duration_seconds;
/**
* \brief A unique identifier for this button.
*
* The platform code is responsible of setting unique values here. This can
* be used later to determine which button generated an event. Many examples
* assume the existence of a button with ID == BUTTON_HAL_ID_BUTTON_ZERO,
* so it is good idea to use this ID for one of your platform's buttons.
*/
const uint8_t unique_id;
};
/*---------------------------------------------------------------------------*/
#if BUTTON_HAL_WITH_DESCRIPTION
/**
* \brief Define a button to be used by the HAL
* \param name The variable name for the button
* \param descr A textual description
* \param p The pin connected to the button
* \param nl True if the button is connected using negative logic
* \param u The button's pull configuration
* \param id A unique numeric identifier
*/
#define BUTTON_HAL_BUTTON(name, descr, p, u, id, nl) \
static button_hal_button_t name = { \
.description = descr, \
.pin = p, \
.pull = u, \
.unique_id = id, \
.negative_logic = nl, \
}
/**
* \brief Retrieve the textual description of a button
* \param b A pointer to the button button_hal_button_t
*
* This macro will return the value of the description field for b. If
* BUTTON_HAL_WITH_DESCRIPTION is 0 then this macro will return ""
*/
#define BUTTON_HAL_GET_DESCRIPTION(b) (b)->description
#else
#define BUTTON_HAL_BUTTON(name, descr, p, u, id, nl) \
static button_hal_button_t name = { \
.pin = p, \
.pull = u, \
.unique_id = id, \
.negative_logic = nl, \
}
#define BUTTON_HAL_GET_DESCRIPTION(b) ""
#endif
/*---------------------------------------------------------------------------*/
#define BUTTON_HAL_BUTTONS(...) \
button_hal_button_t *button_hal_buttons[] = {__VA_ARGS__, NULL}; \
const uint8_t button_hal_button_count = \
(sizeof(button_hal_buttons) / sizeof(button_hal_buttons[0])) - 1;
/*---------------------------------------------------------------------------*/
/**
* \brief The number of buttons on a device
*/
extern const uint8_t button_hal_button_count;
/*---------------------------------------------------------------------------*/
/**
* \brief A broadcast event generated when a button gets pressed
*/
extern process_event_t button_hal_press_event;
/**
* \brief A broadcast event generated when a button gets released
*/
extern process_event_t button_hal_release_event;
/**
* \brief A broadcast event generated every second while a button is kept pressed
*/
extern process_event_t button_hal_periodic_event;
/*---------------------------------------------------------------------------*/
/**
* \brief Initialise the button HAL
*/
void button_hal_init(void);
/**
* \brief Retrieve a button by ID
* \param unique_id The button unique ID to search for
* \return A pointer to the button or NULL if not found
*/
button_hal_button_t *button_hal_get_by_id(uint8_t unique_id);
/**
* \brief Retrieve a button by its index
* \param index The button's index (0, 1, ... button_hal_button_count - 1)
* \return A pointer to the button or NULL if not found
*/
button_hal_button_t *button_hal_get_by_index(uint8_t index);
/**
* \brief Get the state of a button (pressed / released)
* \param button A pointer to the button
* \retval BUTTON_HAL_STATE_RELEASED The button is currently released
* \retval BUTTON_HAL_STATE_PRESSED The button is currently pressed
*/
uint8_t button_hal_get_state(button_hal_button_t *button);
/*---------------------------------------------------------------------------*/
#endif /* BUTTON_HAL_H_ */
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
*/

View File

@ -56,20 +56,18 @@
#define IPSO_INPUT_STATE 5500
#define IPSO_INPUT_COUNTER 5501
#define IPSO_INPUT_POLARITY 5502
#define IPSO_INPUT_DEBOUNCE 5503
#define IPSO_INPUT_EDGE_SEL 5504
#define IPSO_INPUT_CTR_RESET 5505
#define IPSO_INPUT_SENSOR_TYPE 5751
#if PLATFORM_HAS_BUTTON
#include "dev/button-sensor.h"
#if BOARD_SENSORTAG
#include "sensortag/button-sensor.h"
#define IPSO_BUTTON_SENSOR button_left_sensor
#if PLATFORM_SUPPORTS_BUTTON_HAL
#include "dev/button-hal.h"
#else
#include "dev/button-sensor.h"
#define IPSO_BUTTON_SENSOR button_sensor
static struct etimer timer;
#endif
PROCESS(ipso_button_process, "ipso-button");
@ -80,13 +78,12 @@ static lwm2m_status_t lwm2m_callback(lwm2m_object_instance_t *object,
lwm2m_context_t *ctx);
static int input_state = 0;
static int polarity = 0;
static int32_t counter = 0;
static int32_t edge_selection = 3; /* both */
static int32_t debounce_time = 10;
static const lwm2m_resource_id_t resources[] = {
RO(IPSO_INPUT_STATE), RO(IPSO_INPUT_COUNTER), RW(IPSO_INPUT_POLARITY),
RO(IPSO_INPUT_STATE), RO(IPSO_INPUT_COUNTER),
RW(IPSO_INPUT_DEBOUNCE), RW(IPSO_INPUT_EDGE_SEL), EX(IPSO_INPUT_CTR_RESET),
RO(IPSO_INPUT_SENSOR_TYPE)
};
@ -104,15 +101,8 @@ static lwm2m_object_instance_t reg_object = {
static int
read_state(void)
{
int value;
if(polarity == 0) {
value = input_state ? 1 : 0;
} else {
value = input_state ? 0 : 1;
}
PRINTF("Read button state (polarity=%d, state=%d): %d\n",
polarity, input_state, value);
return value;
PRINTF("Read button state: %d\n", input_state);
return input_state;
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
@ -128,9 +118,6 @@ lwm2m_callback(lwm2m_object_instance_t *object,
case IPSO_INPUT_COUNTER:
lwm2m_object_write_int(ctx, counter);
break;
case IPSO_INPUT_POLARITY:
lwm2m_object_write_int(ctx, polarity);
break;
case IPSO_INPUT_DEBOUNCE:
lwm2m_object_write_int(ctx, debounce_time);
break;
@ -168,18 +155,33 @@ ipso_button_init(void)
#if PLATFORM_HAS_BUTTON
PROCESS_THREAD(ipso_button_process, ev, data)
{
static struct etimer timer;
int32_t time;
PROCESS_BEGIN();
#if !PLATFORM_SUPPORTS_BUTTON_HAL
SENSORS_ACTIVATE(IPSO_BUTTON_SENSOR);
#endif
while(1) {
PROCESS_WAIT_EVENT();
#if PLATFORM_SUPPORTS_BUTTON_HAL
if(ev == button_hal_press_event) {
input_state = 1;
counter++;
if((edge_selection & 2) != 0) {
lwm2m_notify_object_observers(&reg_object, IPSO_INPUT_STATE);
}
lwm2m_notify_object_observers(&reg_object, IPSO_INPUT_COUNTER);
} else if(ev == button_hal_release_event) {
input_state = 0;
if((edge_selection & 1) != 0) {
lwm2m_notify_object_observers(&reg_object, IPSO_INPUT_STATE);
}
}
#else /* PLATFORM_SUPPORTS_BUTTON_HAL */
if(ev == sensors_event && data == &IPSO_BUTTON_SENSOR) {
if(!input_state) {
int32_t time;
input_state = 1;
counter++;
if((edge_selection & 2) != 0) {
@ -206,6 +208,7 @@ PROCESS_THREAD(ipso_button_process, ev, data)
}
}
}
#endif /* PLATFORM_SUPPORTS_BUTTON_HAL */
}
PROCESS_END();

View File

@ -37,7 +37,11 @@
#include "contiki.h"
#include "net/routing/routing.h"
#if PLATFORM_SUPPORTS_BUTTON_HAL
#include "dev/button-hal.h"
#else
#include "dev/button-sensor.h"
#endif
#include "dev/slip.h"
#include "rpl-border-router.h"
@ -68,7 +72,9 @@ PROCESS_THREAD(border_router_process, ev, data)
PROCESS_PAUSE();
#if !PLATFORM_SUPPORTS_BUTTON_HAL
SENSORS_ACTIVATE(button_sensor);
#endif
LOG_INFO("RPL-Border router started\n");
@ -86,7 +92,11 @@ PROCESS_THREAD(border_router_process, ev, data)
while(1) {
PROCESS_YIELD();
#if PLATFORM_SUPPORTS_BUTTON_HAL
if(ev == button_hal_release_event) {
#else
if(ev == sensors_event && data == &button_sensor) {
#endif
LOG_INFO("Initiating global repair\n");
NETSTACK_ROUTING.global_repair("Button press");
}