From 3648563ed87b7a10f255d96c3410a68e0a3d2985 Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Sun, 11 Feb 2018 10:30:24 -0600 Subject: [PATCH] configs/stm32f103-minimum: Add zerocross support to STM32F103-Minimum board --- configs/stm32f103-minimum/src/Makefile | 4 + configs/stm32f103-minimum/src/stm32_bringup.c | 11 + .../stm32f103-minimum/src/stm32_zerocross.c | 194 ++++++++++++++++++ .../stm32f103-minimum/src/stm32f103_minimum.h | 16 ++ 4 files changed, 225 insertions(+) create mode 100644 configs/stm32f103-minimum/src/stm32_zerocross.c diff --git a/configs/stm32f103-minimum/src/Makefile b/configs/stm32f103-minimum/src/Makefile index 90105c8668..0e21166bce 100644 --- a/configs/stm32f103-minimum/src/Makefile +++ b/configs/stm32f103-minimum/src/Makefile @@ -65,6 +65,10 @@ ifeq ($(CONFIG_PWM),y) CSRCS += stm32_pwm.c endif +ifeq ($(CONFIG_SENSORS_ZEROCROSS),y) + CSRCS += stm32_zerocross.c +endif + ifeq ($(CONFIG_LEDS_APA102),y) CSRCS += stm32_apa102.c endif diff --git a/configs/stm32f103-minimum/src/stm32_bringup.c b/configs/stm32f103-minimum/src/stm32_bringup.c index e0f3f55972..2c47270fc7 100644 --- a/configs/stm32f103-minimum/src/stm32_bringup.c +++ b/configs/stm32f103-minimum/src/stm32_bringup.c @@ -167,6 +167,17 @@ int stm32_bringup(void) } #endif +#ifdef CONFIG_SENSORS_ZEROCROSS + /* Configure the zero-crossing driver */ + + ret = stm32_zerocross_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize Zero-Cross, error %d\n", ret); + return ret; + } +#endif + #ifdef CONFIG_MMCSD ret = stm32_mmcsd_initialize(MMCSD_MINOR); if (ret < 0) diff --git a/configs/stm32f103-minimum/src/stm32_zerocross.c b/configs/stm32f103-minimum/src/stm32_zerocross.c new file mode 100644 index 0000000000..da6fda354e --- /dev/null +++ b/configs/stm32f103-minimum/src/stm32_zerocross.c @@ -0,0 +1,194 @@ +/**************************************************************************** + * configs/stm32f4discovery/src/stm32_zerocross.c + * + * Copyright (C) 2018 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 + +#include "stm32_gpio.h" +#include "stm32f103_minimum.h" + +#ifdef CONFIG_SENSORS_ZEROCROSS + +/**************************************************************************** + * 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, FAR void *arg); + +/**************************************************************************** + * 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 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; + + /* Start with all interrupts disabled */ + + flags = enter_critical_section(); + zcross_disable(); + + sninfo("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, NULL); + + leave_critical_section(flags); +} + +/**************************************************************************** + * Name: zcross_disable + * + * Description: + * Disable all joystick interrupts + * + ****************************************************************************/ + +static void zcross_disable(void) +{ + irqstate_t flags; + + /* Disable zero cross pin interrupt */ + + flags = enter_critical_section(); + + (void)stm32_gpiosetevent(GPIO_ZEROCROSS, false, false, false, NULL, NULL); + + leave_critical_section(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, FAR void *arg) +{ + DEBUGASSERT(g_zcrosshandler != NULL); + 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) +{ + /* 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_SENSORS_ZEROCROSS */ + diff --git a/configs/stm32f103-minimum/src/stm32f103_minimum.h b/configs/stm32f103-minimum/src/stm32f103_minimum.h index 69427573d2..9841a5a4c7 100644 --- a/configs/stm32f103-minimum/src/stm32f103_minimum.h +++ b/configs/stm32f103-minimum/src/stm32f103_minimum.h @@ -110,6 +110,10 @@ #define MAX_IRQBUTTON BUTTON_USER2 #define NUM_IRQBUTTONS (BUTTON_USER1 - BUTTON_USER2 + 1) +/* ZERO CROSS pin definiton */ + +#define GPIO_ZEROCROSS (GPIO_INPUT|GPIO_CNF_INFLOAT|GPIO_PORTA|GPIO_PIN0) + /* Pins config to use with HC-SR04 sensor */ #define GPIO_HCSR04_INT (GPIO_INPUT|GPIO_CNF_INFLOAT|GPIO_PORTA|GPIO_PIN0) @@ -228,6 +232,18 @@ int stm32_bringup(void); int stm32_gpio_initialize(void); #endif +/**************************************************************************** + * Name: stm32_zerocross_initialize + * + * Description: + * Initialize and register the zero cross driver + * + ****************************************************************************/ + +#ifdef CONFIG_SENSORS_ZEROCROSS +int stm32_zerocross_initialize(void); +#endif + /************************************************************************************ * Name: stm32_adc_setup *