diff options
Diffstat (limited to 'nuttx/configs/sam3u-ek/src/up_buttons.c')
-rwxr-xr-x | nuttx/configs/sam3u-ek/src/up_buttons.c | 136 |
1 files changed, 80 insertions, 56 deletions
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 |