Standardize button interfaces

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3749 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2011-07-07 14:23:05 +00:00
parent 14047b6832
commit 0cce4c7bc3
5 changed files with 179 additions and 134 deletions

View File

@ -2,7 +2,7 @@
* configs/avr32dev1/include/board.h
* include/arch/board/board.h
*
* Copyright (C) 2010 Gregory Nutt. All rights reserved.
* Copyright (C) 2010-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
@ -158,8 +158,8 @@
/* Button definitions ***************************************************************/
/* The AVR32DEV1 board has 3 BUTTONs, two of which can be sensed through GPIO pins. */
#define BUTTON1 1 /* Bit 0: Button 1 */
#define BUTTON2 2 /* Bit 1: Button 2 */
#define BUTTON1 1 /* Bit 0: Button 1 */
#define BUTTON2 2 /* Bit 1: Button 2 */
/************************************************************************************
* Public Types
@ -198,10 +198,10 @@ EXTERN void avr32_boardinitialize(void);
* Name: up_buttoninit
*
* Description:
* up_buttoninit() must be called to initialize button resources. After that,
* up_buttons() may be called to collect the state of all buttons. up_buttons()
* returns an 8-bit bit set with each bit associated with a button. See the
* BUTTON* definitions above for the meaning of each bit in the returned value.
* up_buttoninit() must be called to initialize button resources. After
* that, up_buttons() may be called to collect the current state of all
* buttons or up_irqbutton() may be called to register button interrupt
* handlers.
*
* NOTE: Nothing in the "base" NuttX code calls up_buttoninit(). If you want button
* support in an application, your application startup code must call up_buttoninit()
@ -226,11 +226,12 @@ EXTERN void up_buttoninit(void);
EXTERN uint8_t up_buttons(void);
/************************************************************************************
* Name: up_irqbutton1/2
* Name: up_irqbutton
*
* Description:
* These functions may be called to register an interrupt handler that will be
* called when BUTTON1/2 is depressed. The previous interrupt handler value is
* This function may be called to register an interrupt handler that will be
* called when a button is depressed or released. The ID value is one of the
* BUTTON* definitions provided above. The previous interrupt handler address is
* returned (so that it may restored, if so desired).
*
* Configuration Notes:
@ -242,8 +243,7 @@ EXTERN uint8_t up_buttons(void);
************************************************************************************/
#ifdef CONFIG_AVR32_GPIOIRQ
EXTERN xcpt_t up_irqbutton1(xcpt_t irqhandler);
EXTERN xcpt_t up_irqbutton2(xcpt_t irqhandler);
EXTERN xcpt_t up_irqbutton(int id, xcpt_t irqhandler);
#endif
#endif /* CONFIG_ARCH_BUTTONS */

View File

@ -1,7 +1,7 @@
/****************************************************************************
* configs/sam3u-ek/src/up_leds.c
*
* Copyright (C) 2010 Gregory Nutt. All rights reserved.
* Copyright (C) 2010-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
@ -65,12 +65,54 @@
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: up_irqbuttonx
*
* Description:
* This function implements the core of the up_irqbutton() logic.
*
****************************************************************************/
#if defined(CONFIG_AVR32_GPIOIRQ) && \
(defined(CONFIG_AVR32DEV_BUTTON1_IRQ) || defined(CONFIG_AVR32DEV_BUTTON2_IRQ))
static xcpt_t up_irqbuttonx(int irq, xcpt_t irqhandler)
{
xcpt_t oldhandler;
/* Attach the handler */
gpio_irqattach(irq, irqhandler, &oldhandler);
/* Enable/disable the interrupt */
if (irqhandler)
{
gpio_irqenable(irq);
}
else
{
gpio_irqdisable(irq);
}
/* Return the old button handler (so that it can be restored) */
return oldhandler;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_buttoninit
*
* Description:
* up_buttoninit() must be called to initialize button resources. After
* that, up_buttons() may be called to collect the current state of all
* buttons or up_irqbutton() may be called to register button interrupt
* handlers.
*
****************************************************************************/
void up_buttoninit(void)
@ -81,6 +123,13 @@ void up_buttoninit(void)
/****************************************************************************
* Name: up_buttons
*
* Description:
* After up_buttoninit() has been called, up_buttons() may be called to
* collect the state of all buttons. up_buttons() returns an 8-bit bit set
* with each bit associated with a button. See the BUTTON* definitions
* above for the meaning of each bit in the returned value.
*
****************************************************************************/
uint8_t up_buttons(void)
@ -94,71 +143,43 @@ uint8_t up_buttons(void)
}
/****************************************************************************
* Name: up_irqbutton1
* Name: up_irqbutton
*
* Description:
* This function may be called to register an interrupt handler that will
* be called when a button is depressed or released. The ID value is one
* of the BUTTON* definitions provided above. The previous interrupt
* handler address isreturned (so that it may restored, if so desired).
*
* Configuration Notes:
* Configuration CONFIG_AVR32_GPIOIRQ must be selected to enable the
* overall GPIO IRQ feature and CONFIG_AVR32_GPIOIRQSETA and/or
* CONFIG_AVR32_GPIOIRQSETB must be enabled to select GPIOs to support
* interrupts on. For button support, bits 2 and 3 must be set in
* CONFIG_AVR32_GPIOIRQSETB (PB2 and PB3).
*
****************************************************************************/
#ifdef CONFIG_AVR32_GPIOIRQ
xcpt_t up_irqbutton1(xcpt_t irqhandler)
xcpt_t up_irqbutton(int id, xcpt_t irqhandler)
{
#ifdef CONFIG_AVR32DEV_BUTTON1_IRQ
xcpt_t oldhandler;
/* Attach the handler */
gpio_irqattach(GPIO_BUTTON1_IRQ, irqhandler, &oldhandler);
/* Enable/disable the interrupt */
if (irqhandler)
if (id == BUTTON1)
{
gpio_irqenable(GPIO_BUTTON1_IRQ);
}
return up_irqbuttonx(GPIO_BUTTON1_IRQ, irqhandler);
}
else
{
gpio_irqdisable(GPIO_BUTTON1_IRQ);
}
/* Return the old button handler (so that it can be restored) */
return oldhandler;
#else
return NULL;
#endif
}
#endif
/****************************************************************************
* Name: up_irqbutton2
****************************************************************************/
#ifdef CONFIG_AVR32_GPIOIRQ
xcpt_t up_irqbutton2(xcpt_t irqhandler)
{
#ifdef CONFIG_AVR32DEV_BUTTON2_IRQ
xcpt_t oldhandler;
/* Attach the handler */
gpio_irqattach(GPIO_BUTTON2_IRQ, irqhandler, &oldhandler);
/* Enable/disable the interrupt */
if (irqhandler)
if (id == BUTTON2)
{
gpio_irqenable(GPIO_BUTTON2_IRQ);
}
return up_irqbuttonx(GPIO_BUTTON2_IRQ, irqhandler);
}
else
{
gpio_irqdisable(GPIO_BUTTON2_IRQ);
}
/* Return the old button handler (so that it can be restored) */
return oldhandler;
#else
return NULL;
#endif
{
return NULL;
}
}
#endif
#endif /* CONFIG_ARCH_BUTTONS */

View File

@ -2,7 +2,7 @@
* configs/sam3u-ek/include/board.h
* include/arch/board/board.h
*
* Copyright (C) 2009-2010 Gregory Nutt. All rights reserved.
* Copyright (C) 2009-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
@ -160,9 +160,8 @@ EXTERN void sam3u_boardinitialize(void);
*
* Description:
* up_buttoninit() must be called to initialize button resources. After that,
* up_buttons() may be called to collect the state of all buttons. up_buttons()
* returns an 8-bit bit set with each bit associated with a button. See the
* BUTTON* definitions above for the meaning of each bit in the returned value.
* up_buttons() may be called to collect the current state of all buttons or
* up_irqbutton() may be called to register button interrupt handlers.
*
************************************************************************************/
@ -183,18 +182,18 @@ EXTERN void up_buttoninit(void);
EXTERN uint8_t up_buttons(void);
/************************************************************************************
* Name: up_irqbutton1/2
* Name: up_irqbutton
*
* Description:
* These functions may be called to register an interrupt handler that will be
* called when BUTTON1/2 is depressed. The previous interrupt handler value is
* This function may be called to register an interrupt handler that will be
* called when a button is depressed or released. The ID value is one of the
* BUTTON* definitions provided above. The previous interrupt handler address is
* returned (so that it may restored, if so desired).
*
************************************************************************************/
#ifdef CONFIG_GPIOA_IRQ
EXTERN xcpt_t up_irqbutton1(xcpt_t irqhandler);
EXTERN xcpt_t up_irqbutton2(xcpt_t irqhandler);
EXTERN xcpt_t up_irqbutton(int id, xcpt_t irqhandler);
#endif
#endif /* CONFIG_ARCH_BUTTONS */

View File

@ -66,12 +66,57 @@ static xcpt_t g_irqbutton2;
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: up_irqbuttonx
*
* Description:
* This function implements the core of the up_irqbutton() logic.
*
****************************************************************************/
#ifdef CONFIG_GPIOA_IRQ
static xcpt_t up_irqbuttonx(int irq, xcpt_t irqhandler, xcpt_t *store)
{
xcpt_t oldhandler;
irqstate_t flags;
/* Disable interrupts until we are done. This guarantees that the following
* operations are atomic.
*/
flags = irqsave();
/* Get the old button interrupt handler and save the new one */
oldhandler = *store;
*store = irqhandler;
/* Configure the interrupt */
sam3u_gpioirq(irq);
(void)irq_attach(irq, irqhandler);
sam3u_gpioirqenable(irq);
irqrestore(flags);
/* Return the old button handler (so that it can be restored) */
return oldhandler;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_buttoninit
*
* Description:
* up_buttoninit() must be called to initialize button resources. After
* that, up_buttons() may be called to collect the current state of all
* buttons or up_irqbutton() may be called to register button interrupt
* handlers.
*
****************************************************************************/
void up_buttoninit(void)
@ -80,9 +125,16 @@ void up_buttoninit(void)
(void)sam3u_configgpio(GPIO_BUTTON2);
}
/****************************************************************************
/************************************************************************************
* Name: up_buttons
****************************************************************************/
*
* Description:
* After up_buttoninit() has been called, up_buttons() may be called to collect
* the state of all buttons. up_buttons() returns an 8-bit bit set with each bit
* associated with a button. See the BUTTON* definitions above for the meaning of
* each bit in the returned value.
*
************************************************************************************/
uint8_t up_buttons(void)
{
@ -95,66 +147,38 @@ uint8_t up_buttons(void)
}
/****************************************************************************
* Name: up_irqbutton1
* Name: up_irqbutton
*
* Description:
* This function may be called to register an interrupt handler that will
* be called when a button is depressed or released. The ID value is one
* of the BUTTON* definitions provided above. The previous interrupt
* handler address isreturned (so that it may restored, if so desired).
*
* Configuration Notes:
* Configuration CONFIG_AVR32_GPIOIRQ must be selected to enable the
* overall GPIO IRQ feature and CONFIG_AVR32_GPIOIRQSETA and/or
* CONFIG_AVR32_GPIOIRQSETB must be enabled to select GPIOs to support
* interrupts on. For button support, bits 2 and 3 must be set in
* CONFIG_AVR32_GPIOIRQSETB (PB2 and PB3).
*
****************************************************************************/
#ifdef CONFIG_GPIOA_IRQ
xcpt_t up_irqbutton1(xcpt_t irqhandler)
xcpt_t up_irqbutton(int id, xcpt_t irqhandler)
{
xcpt_t oldhandler;
irqstate_t flags;
/* Disable interrupts until we are done */
flags = irqsave();
/* Get the old button interrupt handler and save the new one */
oldhandler = g_irqbutton1;
g_irqbutton1 = irqhandler;
/* Configure the interrupt */
sam3u_gpioirq(IRQ_BUTTON1);
(void)irq_attach(IRQ_BUTTON1, irqhandler);
sam3u_gpioirqenable(IRQ_BUTTON1);
irqrestore(flags);
/* Return the old button handler (so that it can be restored) */
return oldhandler;
}
#endif
/****************************************************************************
* Name: up_irqbutton2
****************************************************************************/
#ifdef CONFIG_GPIOA_IRQ
xcpt_t up_irqbutton2(xcpt_t irqhandler)
{
xcpt_t oldhandler;
irqstate_t flags;
/* Disable interrupts until we are done */
flags = irqsave();
/* Get the old button interrupt handler and save the new one */
oldhandler = g_irqbutton2;
g_irqbutton2 = irqhandler;
/* Configure the interrupt */
sam3u_gpioirq(IRQ_BUTTON2);
(void)irq_attach(IRQ_BUTTON2, irqhandler);
sam3u_gpioirqenable(IRQ_BUTTON2);
irqrestore(flags);
/* Return the old button handler (so that it can be restored) */
return oldhandler;
if (id == BUTTON1)
{
return up_irqbuttonx(IRQ_BUTTON1, irqhandler, &g_irqbutton1);
}
else if (id == BUTTON2)
{
return up_irqbuttonx(IRQ_BUTTON2, irqhandler, &g_irqbutton2);
}
else
{
return NULL;
}
}
#endif

View File

@ -709,6 +709,7 @@ static int stm3210e_setpower(struct lcd_dev_s *dev, int power)
if (power > 0)
{
#ifdef CONFIG_LCD_BACKLIGHT
uint32_t duty;
/* Caclulate the new backlight duty. It is a faction of the timer1
@ -722,7 +723,7 @@ static int stm3210e_setpower(struct lcd_dev_s *dev, int power)
duty = LCD_BL_TIMER_PERIOD - 1;
}
putreg16((uint16_t)duty, STM32_TIM1_CCR1);
#endif
/* Then turn the display on */
stm3210e_writereg(LCD_REG_7, g_lcddev.spfd5408b ? 0x0112 : 0x0173);