You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by je...@apache.org on 2020/05/14 06:33:15 UTC

[incubator-nuttx] 15/23: stm32: move zerocross sensor initialization to common board logic

This is an automated email from the ASF dual-hosted git repository.

jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit cf1ff36e0ee59c4d34220443e194ed4e0d54ec3b
Author: Matias Nitsche <mn...@dc.uba.ar>
AuthorDate: Mon May 11 18:45:14 2020 -0300

    stm32: move zerocross sensor initialization to common board logic
---
 boards/arm/stm32/common/include/stm32_zerocross.h  |  73 ++++++++
 boards/arm/stm32/common/src/Make.defs              |   4 +
 .../src/stm32_zerocross.c                          |  16 +-
 boards/arm/stm32/stm32f103-minimum/include/board.h |   5 +
 boards/arm/stm32/stm32f103-minimum/src/Make.defs   |   4 -
 .../stm32/stm32f103-minimum/src/stm32_bringup.c    |   6 +-
 .../stm32f103-minimum/src/stm32f103_minimum.h      |  16 --
 boards/arm/stm32/stm32f4discovery/include/board.h  |   5 +
 boards/arm/stm32/stm32f4discovery/src/Make.defs    |   4 -
 .../arm/stm32/stm32f4discovery/src/stm32_bringup.c |   6 +-
 .../stm32/stm32f4discovery/src/stm32_zerocross.c   | 197 ---------------------
 .../stm32/stm32f4discovery/src/stm32f4discovery.h  |   4 -
 12 files changed, 103 insertions(+), 237 deletions(-)

diff --git a/boards/arm/stm32/common/include/stm32_zerocross.h b/boards/arm/stm32/common/include/stm32_zerocross.h
new file mode 100644
index 0000000..3e2d0c3
--- /dev/null
+++ b/boards/arm/stm32/common/include/stm32_zerocross.h
@@ -0,0 +1,73 @@
+/****************************************************************************
+ * /home/v01d/coding/nuttx_latest/nuttx/boards/arm/stm32/common/include/stm32_zerocross.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __STM32_ZEROCROSS_H
+#define __STM32_ZEROCROSS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: stm32_zerocross_initialize
+ *
+ * Description:
+ *   Initialize and register the zero cross driver
+ *
+ ****************************************************************************/
+
+int board_zerocross_initialize(int devno);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif // __STM32_ZEROCROSS_H
diff --git a/boards/arm/stm32/common/src/Make.defs b/boards/arm/stm32/common/src/Make.defs
index 9a0ab30..ee95f29 100644
--- a/boards/arm/stm32/common/src/Make.defs
+++ b/boards/arm/stm32/common/src/Make.defs
@@ -62,6 +62,10 @@ ifeq ($(CONFIG_SENSORS_APDS9960),y)
   CSRCS += stm32_apds9960.c
 endif
 
+ifeq ($(CONFIG_SENSORS_ZEROCROSS),y)
+  CSRCS += stm32_zerocross.c
+endif
+
 DEPPATH += --dep-path src
 VPATH += :src
 CFLAGS += $(shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src)
diff --git a/boards/arm/stm32/stm32f103-minimum/src/stm32_zerocross.c b/boards/arm/stm32/common/src/stm32_zerocross.c
similarity index 94%
rename from boards/arm/stm32/stm32f103-minimum/src/stm32_zerocross.c
rename to boards/arm/stm32/common/src/stm32_zerocross.c
index 3c28df8..3747a56 100644
--- a/boards/arm/stm32/stm32f103-minimum/src/stm32_zerocross.c
+++ b/boards/arm/stm32/common/src/stm32_zerocross.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/arm/stm32/stm32f4discovery/src/stm32_zerocross.c
+ * boards/arm/stm32/common/src/stm32_zerocross.c
  *
  *   Copyright (C) 2018 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <gn...@nuttx.org>
@@ -44,12 +44,10 @@
 
 #include <nuttx/irq.h>
 #include <nuttx/arch.h>
+#include <arch/board/board.h>
 #include <nuttx/sensors/zerocross.h>
 
 #include "stm32_gpio.h"
-#include "stm32f103_minimum.h"
-
-#ifdef CONFIG_SENSORS_ZEROCROSS
 
 /****************************************************************************
  * Private Function Prototypes
@@ -110,7 +108,7 @@ static void zcross_enable(FAR const struct zc_lowerhalf_s *lower,
       g_zcrossarg     = arg;
     }
 
-  stm32_gpiosetevent(GPIO_ZEROCROSS, rising, falling,
+  stm32_gpiosetevent(BOARD_ZEROCROSS_GPIO, rising, falling,
                      true, zcross_interrupt, NULL);
 
   leave_critical_section(flags);
@@ -132,7 +130,7 @@ static void zcross_disable(void)
 
   flags = enter_critical_section();
 
-  stm32_gpiosetevent(GPIO_ZEROCROSS, false, false, false, NULL, NULL);
+  stm32_gpiosetevent(BOARD_ZEROCROSS_GPIO, false, false, false, NULL, NULL);
 
   leave_critical_section(flags);
 
@@ -173,13 +171,13 @@ static int zcross_interrupt(int irq, FAR void *context, FAR void *arg)
  *
  ****************************************************************************/
 
-int stm32_zerocross_initialize(void)
+int board_zerocross_initialize(int devno)
 {
   /* 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);
+  stm32_configgpio(BOARD_ZEROCROSS_GPIO);
 
   /* Make sure that all interrupts are disabled */
 
@@ -189,5 +187,3 @@ int stm32_zerocross_initialize(void)
 
   return zc_register("/dev/zc0", &g_zcrosslower);
 }
-
-#endif /* CONFIG_SENSORS_ZEROCROSS */
diff --git a/boards/arm/stm32/stm32f103-minimum/include/board.h b/boards/arm/stm32/stm32f103-minimum/include/board.h
index 3852016..95d2002 100644
--- a/boards/arm/stm32/stm32f103-minimum/include/board.h
+++ b/boards/arm/stm32/stm32f103-minimum/include/board.h
@@ -236,4 +236,9 @@
 
 #define BOARD_APDS9960_GPIO_INT GPIO_APDS9960_INT
 
+/* ZERO CROSS pin definition */
+
+#define BOARD_ZEROCROSS_GPIO \
+  (GPIO_INPUT|GPIO_CNF_INFLOAT|GPIO_PORTA|GPIO_PIN0)
+
 #endif /* __BOARDS_ARM_STM32_STM32F103_MINIMUM_INCLUDE_BOARD_H */
diff --git a/boards/arm/stm32/stm32f103-minimum/src/Make.defs b/boards/arm/stm32/stm32f103-minimum/src/Make.defs
index bc32755..87829af 100644
--- a/boards/arm/stm32/stm32f103-minimum/src/Make.defs
+++ b/boards/arm/stm32/stm32f103-minimum/src/Make.defs
@@ -65,10 +65,6 @@ ifeq ($(CONFIG_PWM),y)
   CSRCS += stm32_pwm.c
 endif
 
-ifeq ($(CONFIG_SENSORS_ZEROCROSS),y)
-  CSRCS += stm32_zerocross.c
-endif
-
 ifeq ($(CONFIG_LCD_BACKPACK),y)
   CSRCS += stm32_lcd_backpack.c
 endif
diff --git a/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c b/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c
index 1a7272a..b01e1ad 100644
--- a/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c
+++ b/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c
@@ -125,6 +125,10 @@
 #include "stm32_apds9960.h"
 #endif
 
+#ifdef CONFIG_SENSORS_ZEROCROSS
+#include "stm32_zerocross.h"
+#endif
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
@@ -219,7 +223,7 @@ int stm32_bringup(void)
 #ifdef CONFIG_SENSORS_ZEROCROSS
   /* Configure the zero-crossing driver */
 
-  ret = stm32_zerocross_initialize();
+  ret = board_zerocross_initialize(0);
   if (ret < 0)
     {
       syslog(LOG_ERR, "Failed to initialize Zero-Cross, error %d\n", ret);
diff --git a/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h b/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h
index ad0ed38..6031556 100644
--- a/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h
+++ b/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h
@@ -133,10 +133,6 @@
 #define MAX_IRQBUTTON     BUTTON_USER2
 #define NUM_IRQBUTTONS    (BUTTON_USER1 - BUTTON_USER2 + 1)
 
-/* ZERO CROSS pin definition */
-
-#define GPIO_ZEROCROSS    (GPIO_INPUT|GPIO_CNF_INFLOAT|GPIO_PORTA|GPIO_PIN0)
-
 /* SPI chip selects */
 
 #define FLASH_SPI1_CS     (GPIO_OUTPUT|GPIO_CNF_OUTPP|GPIO_MODE_50MHz|\
@@ -240,18 +236,6 @@ 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
  *
  * Description:
diff --git a/boards/arm/stm32/stm32f4discovery/include/board.h b/boards/arm/stm32/stm32f4discovery/include/board.h
index 02e348c..b4cf157 100644
--- a/boards/arm/stm32/stm32f4discovery/include/board.h
+++ b/boards/arm/stm32/stm32f4discovery/include/board.h
@@ -420,4 +420,9 @@
 
 #define DMAMAP_SDIO DMAMAP_SDIO_1
 
+/* ZERO CROSS pin definition */
+
+#define BOARD_ZEROCROSS_GPIO \
+  (GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|GPIO_PORTD|GPIO_PIN0)
+
 #endif /* __BOARDS_ARM_STM32_STM32F4DISCOVERY_INCLUDE_BOARD_H */
diff --git a/boards/arm/stm32/stm32f4discovery/src/Make.defs b/boards/arm/stm32/stm32f4discovery/src/Make.defs
index 2169b99..0346411 100644
--- a/boards/arm/stm32/stm32f4discovery/src/Make.defs
+++ b/boards/arm/stm32/stm32f4discovery/src/Make.defs
@@ -152,10 +152,6 @@ ifeq ($(CONFIG_PM_BUTTONS),y)
 CSRCS += stm32_pmbuttons.c
 endif
 
-ifeq ($(CONFIG_SENSORS_ZEROCROSS),y)
-CSRCS += stm32_zerocross.c
-endif
-
 ifeq ($(CONFIG_ARCH_IDLE_CUSTOM),y)
 CSRCS += stm32_idle.c
 endif
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c b/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
index 094b279..0c0ef80 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
@@ -94,6 +94,10 @@
 #include "stm32_nunchuck.h"
 #endif
 
+#ifdef CONFIG_SENSORS_ZEROCROSS
+#include "stm32_zerocross.h"
+#endif
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -198,7 +202,7 @@ int stm32_bringup(void)
 #ifdef CONFIG_SENSORS_ZEROCROSS
   /* Configure the zero-crossing driver */
 
-  stm32_zerocross_initialize();
+  board_zerocross_initialize(0);
 #endif
 
 #ifdef CONFIG_LEDS_MAX7219
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_zerocross.c b/boards/arm/stm32/stm32f4discovery/src/stm32_zerocross.c
deleted file mode 100644
index 6949971..0000000
--- a/boards/arm/stm32/stm32f4discovery/src/stm32_zerocross.c
+++ /dev/null
@@ -1,197 +0,0 @@
-/****************************************************************************
- * boards/arm/stm32/stm32f4discovery/src/stm32_zerocross.c
- *
- *   Copyright (C) 2015-2016 Gregory Nutt. All rights reserved.
- *   Author: Gregory Nutt <gn...@nuttx.org>
- *
- * 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 <nuttx/config.h>
-
-#include <stdint.h>
-#include <debug.h>
-
-#include <nuttx/irq.h>
-#include <nuttx/arch.h>
-#include <nuttx/sensors/zerocross.h>
-
-#include "stm32_gpio.h"
-#include "stm32f4discovery.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);
-
-/****************************************************************************
- * 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 = enter_critical_section();
-  zcross_disable();
-
-  sninfo("handler: %p arg: %p\n", handler, arg);
-
-  if (handler)
-    {
-      g_zcrosshandler = handler;
-      g_zcrossarg     = arg;
-    }
-
-  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;
-  int i;
-
-  /* Disable zero cross pin interrupt */
-
-  flags = enter_critical_section();
-
-  stm32_gpiosetevent(GPIO_ZEROCROSS, false, false, false, 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)
-{
-  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_SENSORS_ZEROCROSS */
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h b/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
index 67b8b73..ff95944 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
@@ -236,10 +236,6 @@
 
 #define GPIO_BTN_USER   (GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|GPIO_PORTA|GPIO_PIN0)
 
-/* ZERO CROSS pin definition */
-
-#define GPIO_ZEROCROSS  (GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|GPIO_PORTD|GPIO_PIN0)
-
 #define GPIO_CS43L22_RESET  (GPIO_OUTPUT|GPIO_SPEED_50MHz|GPIO_PORTD|GPIO_PIN4)
 
 /* LoRa SX127x */