2015-10-13 15:08:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/input/button_lower.c
|
|
|
|
*
|
2017-02-27 21:21:30 +01:00
|
|
|
* Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
|
2015-10-13 15:08:11 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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 NuttX 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 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/board.h>
|
|
|
|
#include <nuttx/input/buttons.h>
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2015-10-13 17:29:23 +02:00
|
|
|
|
|
|
|
#undef __KERNEL__
|
2015-10-13 15:08:11 +02:00
|
|
|
#include <arch/board/board.h>
|
|
|
|
|
|
|
|
#if CONFIG_BUTTONS_LOWER
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static btn_buttonset_t btn_supported(FAR const struct btn_lowerhalf_s *lower);
|
|
|
|
static btn_buttonset_t btn_buttons(FAR const struct btn_lowerhalf_s *lower);
|
|
|
|
static void btn_enable(FAR const struct btn_lowerhalf_s *lower,
|
|
|
|
btn_buttonset_t press, btn_buttonset_t release,
|
|
|
|
btn_handler_t handler, FAR void *arg);
|
|
|
|
|
|
|
|
static void btn_disable(void);
|
2017-02-27 14:35:34 +01:00
|
|
|
static int btn_interrupt(int irq, FAR void *context, FAR void *arg);
|
2015-10-13 15:08:11 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This is the button button lower half driver interface */
|
|
|
|
|
|
|
|
static const struct btn_lowerhalf_s g_btnlower =
|
|
|
|
{
|
|
|
|
.bl_supported = btn_supported,
|
|
|
|
.bl_buttons = btn_buttons,
|
|
|
|
.bl_enable = btn_enable,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Current interrupt handler and argument */
|
|
|
|
|
|
|
|
static btn_handler_t g_btnhandler;
|
|
|
|
static FAR void *g_btnarg;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btn_supported
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the set of buttons supported
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static btn_buttonset_t btn_supported(FAR const struct btn_lowerhalf_s *lower)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("NUM_BUTTONS: %02x\n", NUM_BUTTONS);
|
2015-10-13 15:08:11 +02:00
|
|
|
return (btn_buttonset_t)((1 << NUM_BUTTONS) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btn_buttons
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the current state of button data
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static btn_buttonset_t btn_buttons(FAR const struct btn_lowerhalf_s *lower)
|
|
|
|
{
|
|
|
|
return board_buttons();
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btn_enable
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Enable interrupts on the selected set of buttons. And empty set or
|
|
|
|
* a NULL handler will disable all interrupts.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void btn_enable(FAR const struct btn_lowerhalf_s *lower,
|
|
|
|
btn_buttonset_t press, btn_buttonset_t release,
|
|
|
|
btn_handler_t handler, FAR void *arg)
|
|
|
|
{
|
|
|
|
btn_buttonset_t mask;
|
|
|
|
btn_buttonset_t either = press | release;
|
|
|
|
irqstate_t flags;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
/* Start with all interrupts disabled */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-10-13 15:08:11 +02:00
|
|
|
btn_disable();
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
iinfo("press: %02x release: %02x handler: %p arg: %p\n",
|
|
|
|
press, release, handler, arg);
|
2015-10-13 15:08:11 +02:00
|
|
|
|
|
|
|
/* If no events are indicated or if no handler is provided, then this
|
|
|
|
* must really be a request to disable interrupts.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (either && handler)
|
|
|
|
{
|
|
|
|
/* Save the new the handler and argument */
|
|
|
|
|
|
|
|
g_btnhandler = handler;
|
|
|
|
g_btnarg = arg;
|
|
|
|
|
|
|
|
/* Attach and enable each button interrupt */
|
|
|
|
|
|
|
|
for (id = 0; id < NUM_BUTTONS; id++)
|
|
|
|
{
|
|
|
|
mask = (1 << id);
|
|
|
|
if ((either & mask) != 0)
|
|
|
|
{
|
2017-02-27 21:21:30 +01:00
|
|
|
(void)board_button_irq(id, btn_interrupt, NULL);
|
2015-10-13 15:08:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-10-13 15:08:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btn_disable
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Disable all button interrupts
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void btn_disable(void)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
/* Disable each button interrupt */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-10-13 15:08:11 +02:00
|
|
|
for (id = 0; id < NUM_BUTTONS; id++)
|
|
|
|
{
|
2017-02-27 21:21:30 +01:00
|
|
|
(void)board_button_irq(id, NULL, NULL);
|
2015-10-13 15:08:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Nullify the handler and argument */
|
|
|
|
|
|
|
|
g_btnhandler = NULL;
|
|
|
|
g_btnarg = NULL;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-10-13 15:08:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btn_interrupt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Discrete button interrupt handler (all buttons)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-27 14:35:34 +01:00
|
|
|
static int btn_interrupt(int irq, FAR void *context, FAR void *arg)
|
2015-10-13 15:08:11 +02:00
|
|
|
{
|
|
|
|
DEBUGASSERT(g_btnhandler);
|
|
|
|
|
|
|
|
if (g_btnhandler)
|
|
|
|
{
|
|
|
|
g_btnhandler(&g_btnlower, g_btnarg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btn_lower_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the generic button lower half driver, bind it and register
|
|
|
|
* it with the upper half button driver as devname.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int btn_lower_initialize(FAR const char *devname)
|
|
|
|
{
|
|
|
|
board_button_initialize();
|
|
|
|
return btn_register(devname, &g_btnlower);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_BUTTONS_LOWER */
|