Updated the OpenMote-CC2538 platform and examples.

This commit is contained in:
Pere Tuset 2016-04-11 15:32:29 +02:00
parent 1d3c37d6da
commit 3e00ea55d1
30 changed files with 895 additions and 868 deletions

View file

@ -27,19 +27,18 @@
* 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 platform
* \addtogroup openmote-button-sensor
* @{
*
* \defgroup openmote
*
* \file
* Driver for the OpenMote-CC2538 buttons
*
* Driver for for the OpenMote-CC2538 user button
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "dev/nvic.h"
@ -47,6 +46,8 @@
#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>
@ -54,32 +55,44 @@
#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;
/*---------------------------------------------------------------------------*/
/**
* \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 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
config(uint32_t port_base, uint32_t pin_mask)
duration_exceeded_callback(void *data)
{
/* Software controlled */
GPIO_SOFTWARE_CONTROL(port_base, pin_mask);
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;
}
/* 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);
return 0;
}
/*---------------------------------------------------------------------------*/
/**
@ -95,42 +108,72 @@ btn_callback(uint8_t port, uint8_t pin)
if(!timer_expired(&debouncetimer)) {
return;
}
timer_set(&debouncetimer, CLOCK_SECOND / 8);
if(port == GPIO_C_NUM) {
sensors_changed(&button_user_sensor);
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);
}
/*---------------------------------------------------------------------------*/
/**
* \brief Init function for the select button.
* \brief Init function for the User button.
* \param type SENSORS_ACTIVE: Activate / Deactivate the sensor (value == 1
* or 0 respectively)
*
* 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
* \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)
{
config(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
switch(type) {
case SENSORS_HW_INIT:
button_press_duration_exceeded = process_alloc_event();
ioc_set_over(BUTTON_USER_PORT, BUTTON_USER_PIN, IOC_OVERRIDE_PUE);
/* Software controlled */
GPIO_SOFTWARE_CONTROL(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
nvic_interrupt_enable(BUTTON_USER_VECTOR);
/* 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_register_callback(btn_callback, BUTTON_USER_PORT, BUTTON_USER_PIN);
break;
case SENSORS_ACTIVE:
if(value) {
GPIO_ENABLE_INTERRUPT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
nvic_interrupt_enable(BUTTON_USER_VECTOR);
} else {
GPIO_DISABLE_INTERRUPT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK);
nvic_interrupt_disable(BUTTON_USER_VECTOR);
}
return value;
case BUTTON_SENSOR_CONFIG_TYPE_INTERVAL:
press_duration = (clock_time_t)value;
break;
default:
break;
}
gpio_register_callback(btn_callback, BUTTON_USER_PORT, BUTTON_USER_PIN);
return 1;
}
/*---------------------------------------------------------------------------*/
void
button_sensor_init()
{
timer_set(&debouncetimer, 0);
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(button_user_sensor, BUTTON_SENSOR, NULL, config_user, NULL);
SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, value, config_user, NULL);
/*---------------------------------------------------------------------------*/
/** @} */