cc2531 Button 2 driver
This commit is contained in:
parent
5662b1f37d
commit
6a808053ce
3 changed files with 139 additions and 42 deletions
|
@ -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
|
||||
|
|
|
@ -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__ */
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue