diff options
author | patacongo <patacongo@7fd9a85b-ad96-42d3-883c-3090e2eb8679> | 2011-07-07 14:23:05 +0000 |
---|---|---|
committer | patacongo <patacongo@7fd9a85b-ad96-42d3-883c-3090e2eb8679> | 2011-07-07 14:23:05 +0000 |
commit | ee44e9ed8e2bb6e103c3d962c7b1a69bef220a17 (patch) | |
tree | 10086eb56660f593c9b81e6ce769aa7af7cb9fd0 /nuttx/configs/sam3u-ek | |
parent | 2b4258a9f9bad385abd0f4024b4cdf1591ea4830 (diff) |
Standardize button interfaces
git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@3749 7fd9a85b-ad96-42d3-883c-3090e2eb8679
Diffstat (limited to 'nuttx/configs/sam3u-ek')
-rwxr-xr-x | nuttx/configs/sam3u-ek/include/board.h | 17 | ||||
-rwxr-xr-x | nuttx/configs/sam3u-ek/src/up_buttons.c | 136 |
2 files changed, 88 insertions, 65 deletions
diff --git a/nuttx/configs/sam3u-ek/include/board.h b/nuttx/configs/sam3u-ek/include/board.h index 45335e6b0a..7721d0f889 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 <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 */ diff --git a/nuttx/configs/sam3u-ek/src/up_buttons.c b/nuttx/configs/sam3u-ek/src/up_buttons.c index ea8fe0aaf5..211e1b300f 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 |