diff --git a/configs/stm32f4discovery/src/Makefile b/configs/stm32f4discovery/src/Makefile index 31a8f3dd23..ef0011eb28 100644 --- a/configs/stm32f4discovery/src/Makefile +++ b/configs/stm32f4discovery/src/Makefile @@ -92,6 +92,10 @@ ifeq ($(CONFIG_PM_BUTTONS),y) CSRCS += stm32_pmbuttons.c endif +ifeq ($(CONFIG_ZEROCROSS),y) +CSRCS += stm32_zerocross.c +endif + ifeq ($(CONFIG_ARCH_IDLE_CUSTOM),y) CSRCS += stm32_idle.c endif diff --git a/configs/stm32f4discovery/src/stm32_bringup.c b/configs/stm32f4discovery/src/stm32_bringup.c index a21f360b63..fe0c7bdef3 100644 --- a/configs/stm32f4discovery/src/stm32_bringup.c +++ b/configs/stm32f4discovery/src/stm32_bringup.c @@ -97,6 +97,10 @@ int stm32_bringup(void) #endif int ret = OK; +#ifdef CONFIG_ZEROCROSS + stm32_zerocross_initialize(); +#endif + #ifdef HAVE_SDIO /* Initialize the SDIO block driver */ diff --git a/configs/stm32f4discovery/src/stm32_zerocross.c b/configs/stm32f4discovery/src/stm32_zerocross.c new file mode 100644 index 0000000000..1ed5d6877d --- /dev/null +++ b/configs/stm32f4discovery/src/stm32_zerocross.c @@ -0,0 +1,204 @@ +/**************************************************************************** + * configs/stm32f4discovery/src/stm32_zerocross.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include + +#include "stm32_gpio.h" +#include "stm32f4discovery.h" + +#ifdef CONFIG_ZEROCROSS + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void zcross_enable(FAR const struct zc_lowerhalf_s *lower, + zc_interrupt_t handler, FAR void *arg); + +static void zcross_disable(void); +static int zcross_interrupt(int irq, FAR void *context); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Current interrupt handler and argument */ + +static zc_interrupt_t g_zcrosshandler; +static FAR void *g_zcrossarg; + +/* This is the zero cross lower half driver interface */ + +static const struct zc_lowerhalf_s g_zcrosslower = +{ + .zc_enable = zcross_enable, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: zcross_enable + * + * Description: + * Enable interrupts on the selected zero cross pin. And empty + * set will disable all interrupts. + * + ****************************************************************************/ + +static void zcross_enable(FAR const struct zc_lowerhalf_s *lower, + zc_interrupt_t handler, FAR void *arg) +{ + irqstate_t flags; + bool rising = false; + bool falling = true; + int i; + + /* Start with all interrupts disabled */ + + flags = irqsave(); + zcross_disable(); + + snllvdbg("handler: %p arg: %p\n", handler, arg); + + if (handler) + { + g_zcrosshandler = handler; + g_zcrossarg = arg; + } + + (void)stm32_gpiosetevent(GPIO_ZEROCROSS, rising, falling, + true, zcross_interrupt); + + irqrestore(flags); +} + +/**************************************************************************** + * Name: zcross_disable + * + * Description: + * Disable all joystick interrupts + * + ****************************************************************************/ + +static void zcross_disable(void) +{ + irqstate_t flags; + int i; + + /* Disable zero cross pin interrupt */ + + flags = irqsave(); + + (void)stm32_gpiosetevent(GPIO_ZEROCROSS, false, false, false, NULL); + + irqrestore(flags); + + /* Nullify the handler and argument */ + + g_zcrosshandler = NULL; + g_zcrossarg = NULL; +} + +/**************************************************************************** + * Name: zcross_interrupt + * + * Description: + * Zero Cross interrupt handler + * + ****************************************************************************/ + +static int zcross_interrupt(int irq, FAR void *context) +{ + DEBUGASSERT(g_zcrosshandler); + if (g_zcrosshandler) + { + g_zcrosshandler(&g_zcrosslower, g_zcrossarg); + } + + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_zerocross_initialize + * + * Description: + * Initialize and register the zero cross driver + * + ****************************************************************************/ + +int stm32_zerocross_initialize(void) +{ + int i; + + /* Configure the GPIO pin as input. NOTE: This is unnecessary for + * interrupting pins since it will also be done by stm32_gpiosetevent(). + */ + + stm32_configgpio(GPIO_ZEROCROSS); + + /* Make sure that all interrupts are disabled */ + + zcross_disable(); + + /* Register the zero cross device as /dev/zc0 */ + + return zc_register("/dev/zc0", &g_zcrosslower); +} + +#endif /* CONFIG_ZEROCROSS */ diff --git a/configs/stm32f4discovery/src/stm32f4discovery.h b/configs/stm32f4discovery/src/stm32f4discovery.h index 9f766ea9a7..4b7a91fe00 100644 --- a/configs/stm32f4discovery/src/stm32f4discovery.h +++ b/configs/stm32f4discovery/src/stm32f4discovery.h @@ -191,6 +191,10 @@ #define GPIO_BTN_USER (GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|GPIO_PORTA|GPIO_PIN0) +/* ZERO CROSS pin definiton */ + +#define GPIO_ZEROCROSS (GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|GPIO_PORTD|GPIO_PIN0) + /* PWM * * The STM32F4 Discovery has no real on-board PWM devices, but the board can be @@ -515,5 +519,17 @@ int stm32_sdio_initialize(void); void weak_function stm32_netinitialize(void); #endif +/**************************************************************************** + * Name: stm32_zerocross_initialize + * + * Description: + * Initialize and register the zero cross driver + * + ****************************************************************************/ + +#ifdef CONFIG_ZEROCROSS +int stm32_zerocross_initialize(void); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __CONFIGS_STM32F4DISCOVERY_SRC_STM32F4DISCOVERY_H */