Add button sensor support (BOOT button)

This commit is contained in:
Jim Paris 2014-06-25 19:36:46 -04:00
parent 4cb97a3311
commit 63692ba96d
4 changed files with 110 additions and 2 deletions

View file

@ -41,7 +41,7 @@ CONTIKI_TARGET_MAIN = ${CONTIKI_CORE}.o
CONTIKI_TARGET_SOURCEFILES += $(ARCH)
CONTIKI_TARGET_SOURCEFILES += contiki-main.c
CONTIKI_TARGET_SOURCEFILES += slip.c
CONTIKI_TARGET_SOURCEFILES += sensors.c
CONTIKI_TARGET_SOURCEFILES += button-sensor.c
CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES)

View file

@ -0,0 +1,104 @@
/**
* Copyright (c) 2014, Analog Devices, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the
* disclaimer below) provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - 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.
*
* - Neither the name of Analog Devices, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
* GRANTED BY THIS LICENSE. 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 OWNER 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.
*/
/**
* \author Jim Paris <jim.paris@rigado.com>
*/
#include "lib/sensors.h"
#include "dev/button-sensor.h"
#include "platform-conf.h"
#include <signal.h>
const struct sensors_sensor button_sensor;
static struct timer debouncetimer;
/* e.g. pADI_GP0 */
#define GPIO CC_CONCAT(pADI_GP, BUTTON_GPIO)
/* e.g. Ext_Int2_Handler */
#define HANDLER CC_CONCAT(CC_CONCAT(Ext_Int, BUTTON_INT), _Handler)
/* e.g. EINT2_IRQn */
#define IRQN CC_CONCAT(CC_CONCAT(EINT, BUTTON_INT), _IRQn)
void
HANDLER(void)
{
pADI_INTERRUPT->EICLR = (1 << BUTTON_INT);
if(!timer_expired(&debouncetimer)) {
return;
}
timer_set(&debouncetimer, CLOCK_SECOND / 8);
sensors_changed(&button_sensor);
}
static int
config(int type, int c)
{
switch(type) {
case SENSORS_HW_INIT:
timer_set(&debouncetimer, 0);
return 1;
case SENSORS_ACTIVE:
/* Set button as a GPIO input with pullup */
GPIO->GPCON &= ~(3UL << (BUTTON_PIN * 2));
GPIO->GPOEN &= ~(1UL << BUTTON_PIN);
GPIO->GPPUL |= (1UL << BUTTON_PIN);
/* Enable interrupt for the button (rise and fall) */
#if BUTTON_INT <= 3
pADI_INTERRUPT->EI0CFG |= (0xaUL << (4 * (BUTTON_INT - 0)));
#elif BUTTON_INT <= 7
pADI_INTERRUPT->EI1CFG |= (0xaUL << (4 * (BUTTON_INT - 4)));
#endif
pADI_INTERRUPT->EICLR = (1 << BUTTON_INT);
NVIC_EnableIRQ(IRQN);
return 1;
}
return 0;
}
static int
status(int type)
{
switch(type) {
case SENSORS_ACTIVE:
case SENSORS_READY:
if(GPIO->GPIN & (1UL << BUTTON_PIN)) {
return 1;
}
return 0;
}
return 0;
}
SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, NULL, config, status);

View file

@ -162,6 +162,7 @@ main(int argc, char **argv)
#endif
serial_line_init();
process_start(&sensors_process, NULL);
autostart_start(autostart_processes);

View file

@ -36,4 +36,7 @@
*/
#define LED1 pADI_GP4, BIT2
#define BUTTON1 pADI_GP0, BIT6
#define BUTTON_GPIO 0
#define BUTTON_PIN 6
#define BUTTON_INT 2