2015-10-13 21:08:11 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/input/button_lower.c
|
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2015-10-13 21:08:11 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-10-13 21:08:11 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2015-10-13 21:08:11 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
2020-11-21 08:28:08 +08:00
|
|
|
#include <inttypes.h>
|
2015-10-13 21:08:11 +08:00
|
|
|
|
|
|
|
#include <nuttx/board.h>
|
|
|
|
#include <nuttx/input/buttons.h>
|
|
|
|
|
2016-02-14 21:32:58 +08:00
|
|
|
#include <nuttx/irq.h>
|
2015-10-13 23:29:23 +08:00
|
|
|
|
2021-04-08 17:11:14 +08:00
|
|
|
#if CONFIG_INPUT_BUTTONS_LOWER
|
2015-10-13 21:08:11 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-01 02:43:08 +08:00
|
|
|
static btn_buttonset_t
|
|
|
|
btn_supported(FAR const struct btn_lowerhalf_s *lower);
|
2015-10-13 21:08:11 +08:00
|
|
|
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 21:35:34 +08:00
|
|
|
static int btn_interrupt(int irq, FAR void *context, FAR void *arg);
|
2015-10-13 21:08:11 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-06-05 17:13:48 +08:00
|
|
|
static uint32_t g_btnnum;
|
|
|
|
|
2015-10-13 21:08:11 +08:00
|
|
|
/* This is the button button lower half driver interface */
|
|
|
|
|
|
|
|
static const struct btn_lowerhalf_s g_btnlower =
|
|
|
|
{
|
2022-03-27 04:51:22 +08:00
|
|
|
btn_supported, /* bl_supported */
|
|
|
|
btn_buttons, /* bl_buttons */
|
|
|
|
btn_enable, /* bl_enable */
|
|
|
|
NULL /* bl_write */
|
2015-10-13 21:08:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2020-11-21 08:28:08 +08:00
|
|
|
iinfo("NUM_BUTTONS: %02" PRIx32 "\n", g_btnnum);
|
2020-06-05 17:13:48 +08:00
|
|
|
return (btn_buttonset_t)((1 << g_btnnum) - 1);
|
2015-10-13 21:08:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
2020-06-05 17:13:48 +08:00
|
|
|
uint32_t id;
|
2015-10-13 21:08:11 +08:00
|
|
|
|
|
|
|
/* Start with all interrupts disabled */
|
|
|
|
|
2016-02-14 21:32:58 +08:00
|
|
|
flags = enter_critical_section();
|
2015-10-13 21:08:11 +08:00
|
|
|
btn_disable();
|
|
|
|
|
2020-11-21 08:28:08 +08:00
|
|
|
iinfo("press: %02" PRIx32 " release: %02" PRIx32 " handler: %p arg: %p\n",
|
2016-06-21 01:59:15 +08:00
|
|
|
press, release, handler, arg);
|
2015-10-13 21:08:11 +08: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 */
|
|
|
|
|
2020-06-05 17:13:48 +08:00
|
|
|
for (id = 0; id < g_btnnum; id++)
|
2015-10-13 21:08:11 +08:00
|
|
|
{
|
|
|
|
mask = (1 << id);
|
|
|
|
if ((either & mask) != 0)
|
|
|
|
{
|
2020-01-03 00:49:34 +08:00
|
|
|
board_button_irq(id, btn_interrupt, NULL);
|
2015-10-13 21:08:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 21:32:58 +08:00
|
|
|
leave_critical_section(flags);
|
2015-10-13 21:08:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btn_disable
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Disable all button interrupts
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void btn_disable(void)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
2020-06-05 17:13:48 +08:00
|
|
|
uint32_t id;
|
2015-10-13 21:08:11 +08:00
|
|
|
|
|
|
|
/* Disable each button interrupt */
|
|
|
|
|
2016-02-14 21:32:58 +08:00
|
|
|
flags = enter_critical_section();
|
2020-06-05 17:13:48 +08:00
|
|
|
for (id = 0; id < g_btnnum; id++)
|
2015-10-13 21:08:11 +08:00
|
|
|
{
|
2020-01-03 00:49:34 +08:00
|
|
|
board_button_irq(id, NULL, NULL);
|
2015-10-13 21:08:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Nullify the handler and argument */
|
|
|
|
|
|
|
|
g_btnhandler = NULL;
|
|
|
|
g_btnarg = NULL;
|
2016-02-14 21:32:58 +08:00
|
|
|
leave_critical_section(flags);
|
2015-10-13 21:08:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btn_interrupt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Discrete button interrupt handler (all buttons)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-27 21:35:34 +08:00
|
|
|
static int btn_interrupt(int irq, FAR void *context, FAR void *arg)
|
2015-10-13 21:08:11 +08: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)
|
|
|
|
{
|
2020-06-05 17:13:48 +08:00
|
|
|
g_btnnum = board_button_initialize();
|
2015-10-13 21:08:11 +08:00
|
|
|
return btn_register(devname, &g_btnlower);
|
|
|
|
}
|
|
|
|
|
2021-04-08 17:11:14 +08:00
|
|
|
#endif /* CONFIG_INPUT_BUTTONS_LOWER */
|