From 6aaedb5fc741627830baa841728c504b62e047a6 Mon Sep 17 00:00:00 2001 From: patacongo Date: Thu, 7 Jul 2011 14:23:05 +0000 Subject: Standardize button interfaces git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3749 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/configs/avr32dev1/include/board.h | 24 +++--- nuttx/configs/avr32dev1/src/up_buttons.c | 129 +++++++++++++++++------------ nuttx/configs/sam3u-ek/include/board.h | 17 ++-- nuttx/configs/sam3u-ek/src/up_buttons.c | 136 ++++++++++++++++++------------- nuttx/configs/stm3210e-eval/src/up_lcd.c | 3 +- nuttx/include/nuttx/arch.h | 62 ++++++++++++++ 6 files changed, 239 insertions(+), 132 deletions(-) diff --git a/nuttx/configs/avr32dev1/include/board.h b/nuttx/configs/avr32dev1/include/board.h index d7d933ae6..c09adc56f 100755 --- a/nuttx/configs/avr32dev1/include/board.h +++ b/nuttx/configs/avr32dev1/include/board.h @@ -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 * * 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 */ diff --git a/nuttx/configs/avr32dev1/src/up_buttons.c b/nuttx/configs/avr32dev1/src/up_buttons.c index 3c35f59de..9d5a8ebf8 100755 --- a/nuttx/configs/avr32dev1/src/up_buttons.c +++ b/nuttx/configs/avr32dev1/src/up_buttons.c @@ -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 * * 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 */ diff --git a/nuttx/configs/sam3u-ek/include/board.h b/nuttx/configs/sam3u-ek/include/board.h index 45335e6b0..7721d0f88 100755 --- a/nuttx/configs/sam3u-ek/include/board.h +++ b/nuttx/configs/sam3u-ek/include/board.h @@ -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 * * 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 */ diff --git a/nuttx/configs/sam3u-ek/src/up_buttons.c b/nuttx/configs/sam3u-ek/src/up_buttons.c index ea8fe0aaf..211e1b300 100755 --- a/nuttx/configs/sam3u-ek/src/up_buttons.c +++ b/nuttx/configs/sam3u-ek/src/up_buttons.c @@ -67,57 +67,35 @@ static xcpt_t g_irqbutton2; ****************************************************************************/ /**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: up_buttoninit - ****************************************************************************/ - -void up_buttoninit(void) -{ - (void)sam3u_configgpio(GPIO_BUTTON1); - (void)sam3u_configgpio(GPIO_BUTTON2); -} - -/**************************************************************************** - * Name: up_buttons - ****************************************************************************/ - -uint8_t up_buttons(void) -{ - uint8_t retval; - - retval = sam3u_gpioread(GPIO_BUTTON1) ? 0 : GPIO_BUTTON1; - retval |= sam3u_gpioread(GPIO_BUTTON2) ? 0 : GPIO_BUTTON2; - - return retval; -} - -/**************************************************************************** - * Name: up_irqbutton1 + * Name: up_irqbuttonx + * + * Description: + * This function implements the core of the up_irqbutton() logic. + * ****************************************************************************/ #ifdef CONFIG_GPIOA_IRQ -xcpt_t up_irqbutton1(xcpt_t irqhandler) +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 */ + /* Disable interrupts until we are done. This guarantees that the following + * operations are atomic. + */ - flags = irqsave(); + flags = irqsave(); /* Get the old button interrupt handler and save the new one */ - oldhandler = g_irqbutton1; - g_irqbutton1 = irqhandler; + oldhandler = *store; + *store = irqhandler; /* Configure the interrupt */ - sam3u_gpioirq(IRQ_BUTTON1); - (void)irq_attach(IRQ_BUTTON1, irqhandler); - sam3u_gpioirqenable(IRQ_BUTTON1); + sam3u_gpioirq(irq); + (void)irq_attach(irq, irqhandler); + sam3u_gpioirqenable(irq); irqrestore(flags); /* Return the old button handler (so that it can be restored) */ @@ -127,34 +105,80 @@ xcpt_t up_irqbutton1(xcpt_t irqhandler) #endif /**************************************************************************** - * Name: up_irqbutton2 + * Public Functions ****************************************************************************/ -#ifdef CONFIG_GPIOA_IRQ -xcpt_t up_irqbutton2(xcpt_t irqhandler) -{ - xcpt_t oldhandler; - irqstate_t flags; - - /* Disable interrupts until we are done */ +/**************************************************************************** + * 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. + * + ****************************************************************************/ - flags = irqsave(); +void up_buttoninit(void) +{ + (void)sam3u_configgpio(GPIO_BUTTON1); + (void)sam3u_configgpio(GPIO_BUTTON2); +} - /* Get the old button interrupt handler and save the new one */ +/************************************************************************************ + * 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. + * + ************************************************************************************/ - oldhandler = g_irqbutton2; - g_irqbutton2 = irqhandler; +uint8_t up_buttons(void) +{ + uint8_t retval; - /* Configure the interrupt */ + retval = sam3u_gpioread(GPIO_BUTTON1) ? 0 : GPIO_BUTTON1; + retval |= sam3u_gpioread(GPIO_BUTTON2) ? 0 : GPIO_BUTTON2; - sam3u_gpioirq(IRQ_BUTTON2); - (void)irq_attach(IRQ_BUTTON2, irqhandler); - sam3u_gpioirqenable(IRQ_BUTTON2); - irqrestore(flags); + return retval; +} - /* Return the old button handler (so that it can be restored) */ +/**************************************************************************** + * 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). + * + ****************************************************************************/ - return oldhandler; +#ifdef CONFIG_GPIOA_IRQ +xcpt_t up_irqbutton(int id, xcpt_t irqhandler) +{ + 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 diff --git a/nuttx/configs/stm3210e-eval/src/up_lcd.c b/nuttx/configs/stm3210e-eval/src/up_lcd.c index 83c8fa693..b759aa031 100755 --- a/nuttx/configs/stm3210e-eval/src/up_lcd.c +++ b/nuttx/configs/stm3210e-eval/src/up_lcd.c @@ -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); diff --git a/nuttx/include/nuttx/arch.h b/nuttx/include/nuttx/arch.h index 448af1105..0eaf25e7d 100644 --- a/nuttx/include/nuttx/arch.h +++ b/nuttx/include/nuttx/arch.h @@ -531,6 +531,68 @@ EXTERN void sched_process_timer(void); EXTERN void irq_dispatch(int irq, FAR void *context); +/**************************************************************************** + * Board-specific button interfaces exported by the board-specific logic + ****************************************************************************/ + +/**************************************************************************** + * 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. + * + * NOTE: This interface may or may not be supported by board-specific + * logic. If the board supports button interfaces, then CONFIG_ARCH_BUTTONS + * will be defined. + * + ****************************************************************************/ + +#ifdef CONFIG_ARCH_BUTTONS +EXTERN void up_buttoninit(void); +#endif + +/**************************************************************************** + * 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. The meaning of the each button + * bit is board-specific. + * + * NOTE: This interface may or may not be supported by board-specific + * logic. If the board supports button interfaces, then CONFIG_ARCH_BUTTONS + * will be defined + * + ****************************************************************************/ + +#ifdef CONFIG_ARCH_BUTTONS +EXTERN uint8_t up_buttons(void); +#endif + +/**************************************************************************** + * 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 a + * button enumeration value that uniquely identifies a button resource. + * The previous interrupt handler address is returned (so that it may + * restored, if so desired). + * + * NOTE: This interface may or may not be supported by board-specific + * logic. If the board supports button interfaces, then CONFIG_ARCH_BUTTONS + * will be defined + * + ****************************************************************************/ + +#ifdef CONFIG_ARCH_BUTTONS +EXTERN xcpt_t up_irqbutton(int id, xcpt_t irqhandler); +#endif + /**************************************************************************** * Debug interfaces exported by the architecture-specific logic ****************************************************************************/ -- cgit v1.2.3