From 6a808053ce93d8545d3c2b6da42ee567b09f361c Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Thu, 22 Dec 2011 21:37:42 +0000 Subject: [PATCH] cc2531 Button 2 driver --- platform/cc2530dk/dev/button-sensor.c | 121 ++++++++++++++++++++---- platform/cc2530dk/dev/button-sensor.h | 55 +++++++---- platform/cc2530dk/dev/smartrf-sensors.c | 5 +- 3 files changed, 139 insertions(+), 42 deletions(-) diff --git a/platform/cc2530dk/dev/button-sensor.c b/platform/cc2530dk/dev/button-sensor.c index 8ae66c5fd..385439701 100644 --- a/platform/cc2530dk/dev/button-sensor.c +++ b/platform/cc2530dk/dev/button-sensor.c @@ -35,76 +35,155 @@ #include "dev/port.h" #include "dev/button-sensor.h" /*---------------------------------------------------------------------------*/ -#if BUTTON_SENSOR_ON static __data struct timer debouncetimer; /*---------------------------------------------------------------------------*/ +/* Button 1 - SmartRT and cc2531 USb Dongle */ +/*---------------------------------------------------------------------------*/ static -int value(int type) +int value_b1(int type) { - return BUTTON_READ() || !timer_expired(&debouncetimer); + type; + return BUTTON_READ(1) || !timer_expired(&debouncetimer); } /*---------------------------------------------------------------------------*/ static -int status(int type) +int status_b1(int type) { switch (type) { case SENSORS_ACTIVE: case SENSORS_READY: - return BUTTON_IRQ_ENABLED(); + return BUTTON_IRQ_ENABLED(1); } return 0; } /*---------------------------------------------------------------------------*/ static -int configure(int type, int value) +int configure_b1(int type, int value) { switch(type) { case SENSORS_HW_INIT: +#if !MODEL_CC2531 P0INP |= 2; /* Tri-state */ - BUTTON_IRQ_ON_PRESS(); - BUTTON_FUNC_GPIO(); - BUTTON_DIR_INPUT(); +#endif + BUTTON_IRQ_ON_PRESS(1); + BUTTON_FUNC_GPIO(1); + BUTTON_DIR_INPUT(1); return 1; case SENSORS_ACTIVE: if(value) { - if(!BUTTON_IRQ_ENABLED()) { + if(!BUTTON_IRQ_ENABLED(1)) { timer_set(&debouncetimer, 0); - BUTTON_IRQ_FLAG_OFF(); - BUTTON_IRQ_ENABLE(); + BUTTON_IRQ_FLAG_OFF(1); + BUTTON_IRQ_ENABLE(1); } } else { - BUTTON_IRQ_DISABLE(); + BUTTON_IRQ_DISABLE(1); } return 1; } return 0; } /*---------------------------------------------------------------------------*/ +/* Button 2 - cc2531 USb Dongle only */ +/*---------------------------------------------------------------------------*/ +#if MODEL_CC2531 +static +int value_b2(int type) +{ + type; + return BUTTON_READ(2) || !timer_expired(&debouncetimer); +} +/*---------------------------------------------------------------------------*/ +static +int status_b2(int type) +{ + switch (type) { + case SENSORS_ACTIVE: + case SENSORS_READY: + return BUTTON_IRQ_ENABLED(2); + } + return 0; +} +/*---------------------------------------------------------------------------*/ +static +int configure_b2(int type, int value) +{ + switch(type) { + case SENSORS_HW_INIT: + BUTTON_IRQ_ON_PRESS(2); + BUTTON_FUNC_GPIO(2); + BUTTON_DIR_INPUT(2); + return 1; + case SENSORS_ACTIVE: + if(value) { + if(!BUTTON_IRQ_ENABLED(2)) { + timer_set(&debouncetimer, 0); + BUTTON_IRQ_FLAG_OFF(2); + BUTTON_IRQ_ENABLE(2); + } + } else { + BUTTON_IRQ_DISABLE(2); + } + return 1; + } + return 0; +} +#endif +/*---------------------------------------------------------------------------*/ +/* ISRs */ +/*---------------------------------------------------------------------------*/ #if MODEL_CC2531 void port_1_isr(void) __interrupt(P1INT_VECTOR) -#else -void -port_0_isr(void) __interrupt(P0INT_VECTOR) -#endif { EA = 0; ENERGEST_ON(ENERGEST_TYPE_IRQ); /* This ISR is for the entire port. Check if the interrupt was caused by our * button's pin. */ - if(BUTTON_IRQ_CHECK()) { + if(BUTTON_IRQ_CHECK(1)) { + if(timer_expired(&debouncetimer)) { + timer_set(&debouncetimer, CLOCK_SECOND / 8); + sensors_changed(&button_1_sensor); + } + } + if(BUTTON_IRQ_CHECK(2)) { + if(timer_expired(&debouncetimer)) { + timer_set(&debouncetimer, CLOCK_SECOND / 8); + sensors_changed(&button_2_sensor); + } + } + + BUTTON_IRQ_FLAG_OFF(1); + BUTTON_IRQ_FLAG_OFF(2); + + ENERGEST_OFF(ENERGEST_TYPE_IRQ); + EA = 1; +} +#else +void +port_0_isr(void) __interrupt(P0INT_VECTOR) +{ + EA = 0; + ENERGEST_ON(ENERGEST_TYPE_IRQ); + + /* This ISR is for the entire port. Check if the interrupt was caused by our + * button's pin. */ + if(BUTTON_IRQ_CHECK(1)) { if(timer_expired(&debouncetimer)) { timer_set(&debouncetimer, CLOCK_SECOND / 8); sensors_changed(&button_sensor); } } - BUTTON_IRQ_FLAG_OFF(); + BUTTON_IRQ_FLAG_OFF(1); ENERGEST_OFF(ENERGEST_TYPE_IRQ); EA = 1; } +#endif /*---------------------------------------------------------------------------*/ -SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, value, configure, status); -#endif /* BUTTON_SENSOR_ON */ +SENSORS_SENSOR(button_1_sensor, BUTTON_SENSOR, value_b1, configure_b1, status_b1); +#if MODEL_CC2531 +SENSORS_SENSOR(button_2_sensor, BUTTON_SENSOR, value_b2, configure_b2, status_b2); +#endif diff --git a/platform/cc2530dk/dev/button-sensor.h b/platform/cc2530dk/dev/button-sensor.h index 15966507b..ad9bcc27d 100644 --- a/platform/cc2530dk/dev/button-sensor.h +++ b/platform/cc2530dk/dev/button-sensor.h @@ -55,41 +55,56 @@ * */ #if MODEL_CC2531 -#define BUTTON_PORT 1 -#define BUTTON_PIN 2 +#define BUTTON1_PORT 1 +#define BUTTON1_PIN 2 +#define BUTTON2_PORT 1 +#define BUTTON2_PIN 3 #else -#define BUTTON_PORT 0 -#define BUTTON_PIN 1 +#define BUTTON1_PORT 0 +#define BUTTON1_PIN 1 #endif #ifdef BUTTON_SENSOR_CONF_ON #define BUTTON_SENSOR_ON BUTTON_SENSOR_CONF_ON #endif /* BUTTON_SENSOR_CONF_ON */ +#define button_sensor button_1_sensor + #if BUTTON_SENSOR_ON -extern const struct sensors_sensor button_sensor; +/* Common */ +extern const struct sensors_sensor button_1_sensor; + #if MODEL_CC2531 -/* Button 1: P1_2 - Port 1 ISR needed */ +/* USB Dongle */ +extern const struct sensors_sensor button_2_sensor; +/* Buttons: P1_2 & P1_3 - Port 1 ISR needed */ void port_1_isr(void) __interrupt(P1INT_VECTOR); -#else +#define BUTTON_SENSOR_ACTIVATE() do { \ + button_1_sensor.configure(SENSORS_ACTIVE, 1); \ + button_2_sensor.configure(SENSORS_ACTIVE, 1); \ +} while(0) + +#else /* MODEL_CC2531 */ +/* SmartRF */ /* Button 1: P0_1 - Port 0 ISR needed */ void port_0_isr(void) __interrupt(P0INT_VECTOR); -#endif #define BUTTON_SENSOR_ACTIVATE() button_sensor.configure(SENSORS_ACTIVE, 1) -#else +#endif /* MODEL_CC2531 */ + +#else /* BUTTON_SENSOR_ON */ #define BUTTON_SENSOR_ACTIVATE() #endif /* BUTTON_SENSOR_ON */ -/* Define macros for button 1 */ -#define BUTTON_READ() PORT_READ(BUTTON_PORT, BUTTON_PIN) -#define BUTTON_FUNC_GPIO() PORT_FUNC_GPIO(BUTTON_PORT, BUTTON_PIN) -#define BUTTON_DIR_INPUT() PORT_DIR_INPUT(BUTTON_PORT, BUTTON_PIN) -#define BUTTON_IRQ_ENABLED() PORT_IRQ_ENABLED(BUTTON_PORT, BUTTON_PIN) -#define BUTTON_IRQ_CHECK() PORT_IRQ_CHECK(BUTTON_PORT, BUTTON_PIN) -#define BUTTON_IRQ_ENABLE() PORT_IRQ_ENABLE(BUTTON_PORT, BUTTON_PIN) -#define BUTTON_IRQ_DISABLE() PORT_IRQ_DISABLE(BUTTON_PORT, BUTTON_PIN) -#define BUTTON_IRQ_FLAG_OFF() PORT_IRQ_FLAG_OFF(BUTTON_PORT, BUTTON_PIN) -#define BUTTON_IRQ_ON_PRESS() PORT_IRQ_EDGE_RISE(BUTTON_PORT, BUTTON_PIN) -#define BUTTON_IRQ_ON_RELEASE() PORT_IRQ_EDGE_FALL(BUTTON_PORT, BUTTON_PIN) +/* Define macros for buttons */ +#define BUTTON_READ(b) PORT_READ(BUTTON##b##_PORT, BUTTON##b##_PIN) +#define BUTTON_FUNC_GPIO(b) PORT_FUNC_GPIO(BUTTON##b##_PORT, BUTTON##b##_PIN) +#define BUTTON_DIR_INPUT(b) PORT_DIR_INPUT(BUTTON##b##_PORT, BUTTON##b##_PIN) +#define BUTTON_IRQ_ENABLED(b) PORT_IRQ_ENABLED(BUTTON##b##_PORT, BUTTON##b##_PIN) +#define BUTTON_IRQ_CHECK(b) PORT_IRQ_CHECK(BUTTON##b##_PORT, BUTTON##b##_PIN) +#define BUTTON_IRQ_ENABLE(b) PORT_IRQ_ENABLE(BUTTON##b##_PORT, BUTTON##b##_PIN) +#define BUTTON_IRQ_DISABLE(b) PORT_IRQ_DISABLE(BUTTON##b##_PORT, BUTTON##b##_PIN) +#define BUTTON_IRQ_FLAG_OFF(b) PORT_IRQ_FLAG_OFF(BUTTON##b##_PORT, BUTTON##b##_PIN) +#define BUTTON_IRQ_ON_PRESS(b) PORT_IRQ_EDGE_RISE(BUTTON##b##_PORT, BUTTON##b##_PIN) +#define BUTTON_IRQ_ON_RELEASE(b) PORT_IRQ_EDGE_FALL(BUTTON##b##_PORT, BUTTON##b##_PIN) #endif /* __BUTTON_SENSOR_H__ */ diff --git a/platform/cc2530dk/dev/smartrf-sensors.c b/platform/cc2530dk/dev/smartrf-sensors.c index c40cf101b..87d4935cc 100644 --- a/platform/cc2530dk/dev/smartrf-sensors.c +++ b/platform/cc2530dk/dev/smartrf-sensors.c @@ -46,7 +46,10 @@ const struct sensors_sensor *sensors[] = { &adc_sensor, #endif #if BUTTON_SENSOR_ON - &button_sensor, + &button_1_sensor, +#if MODEL_CC2531 + &button_2_sensor, +#endif #endif 0 };