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:13 UTC

[incubator-nuttx] 13/23: stm32: move HCSR04 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 beba7a8f0ba19429e430b86f38cd05927964f64d
Author: Matias Nitsche <mn...@dc.uba.ar>
AuthorDate: Mon May 11 18:14:56 2020 -0300

    stm32: move HCSR04 initialization to common board logic
---
 boards/arm/stm32/common/include/stm32_hcsr04.h     | 81 ++++++++++++++++++++++
 boards/arm/stm32/common/src/Make.defs              |  4 ++
 .../src/stm32_hcsr04.c                             | 44 +++++-------
 boards/arm/stm32/stm32f103-minimum/include/board.h | 12 ++++
 boards/arm/stm32/stm32f103-minimum/src/Make.defs   |  4 --
 .../stm32/stm32f103-minimum/src/stm32_bringup.c    |  6 +-
 .../stm32f103-minimum/src/stm32f103_minimum.h      | 40 -----------
 7 files changed, 120 insertions(+), 71 deletions(-)

diff --git a/boards/arm/stm32/common/include/stm32_hcsr04.h b/boards/arm/stm32/common/include/stm32_hcsr04.h
new file mode 100644
index 0000000..4ee4055
--- /dev/null
+++ b/boards/arm/stm32/common/include/stm32_hcsr04.h
@@ -0,0 +1,81 @@
+/****************************************************************************
+ * boards/arm/stm32/common/include/stm32_hcsr04.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_HCSR04_H
+#define __STM32_HCSR04_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_hcsr04_initialize
+ *
+ * Description:
+ *   This function is called by application-specific, setup logic to
+ *   configure the HC-SR04 sensor.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/distN
+ *
+ * Returned Value:
+ *   Zero is returned on success.  Otherwise, a negated errno value is
+ *   returned to indicate the nature of the failure.
+ *
+ ****************************************************************************/
+
+int board_hcsr04_initialize(int devno);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif // __STM32_HCSR04_H
diff --git a/boards/arm/stm32/common/src/Make.defs b/boards/arm/stm32/common/src/Make.defs
index eefbe31..53b9345 100644
--- a/boards/arm/stm32/common/src/Make.defs
+++ b/boards/arm/stm32/common/src/Make.defs
@@ -54,6 +54,10 @@ ifeq ($(CONFIG_WL_NRF24L01),y)
   CSRCS += stm32_nrf24l01.c
 endif
 
+ifeq ($(CONFIG_SENSORS_HCSR04),y)
+  CSRCS += stm32_hcsr04.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_hcsr04.c b/boards/arm/stm32/common/src/stm32_hcsr04.c
similarity index 88%
rename from boards/arm/stm32/stm32f103-minimum/src/stm32_hcsr04.c
rename to boards/arm/stm32/common/src/stm32_hcsr04.c
index 964d723..0aa54c3 100644
--- a/boards/arm/stm32/stm32f103-minimum/src/stm32_hcsr04.c
+++ b/boards/arm/stm32/common/src/stm32_hcsr04.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/arm/stm32/stm32f103-minimum/src/stm32_hcsr04.c
+ * boards/arm/stm32/common/src/stm32_hcsr04.c
  *
  *   Copyright (C) 2017 Gregory Nutt. All rights reserved.
  *   Copyright (C) 2017 Alan Carvalho de Assis. All rights reserved.
@@ -41,29 +41,24 @@
 #include <nuttx/config.h>
 #include <nuttx/arch.h>
 
+#include <stdio.h>
 #include <errno.h>
 #include <debug.h>
 
 #include <nuttx/board.h>
+#include <arch/board/board.h>
 #include <nuttx/sensors/hc_sr04.h>
 
 #include "stm32.h"
 #include "stm32_freerun.h"
-#include "stm32f103_minimum.h"
-
-#if defined(CONFIG_STM32_FREERUN) && defined (CONFIG_SENSORS_HCSR04)
-
-#if !defined(CONFIG_STM32_TIM1)
-# error STM32 TIM1 is not defined
-#endif
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Use TIM1 as free running timer for HC-SR04 sensor */
-
-#define HCSR04_FRTIMER  1
+#ifndef CONFIG_STM32_FREERUN
+#error "This implementation requires support for free running timers"
+#endif
 
 /****************************************************************************
  * Private Types
@@ -150,7 +145,7 @@ static int hcsr04_irq_attach(FAR struct hcsr04_config_s *state, xcpt_t isr,
   priv->isr     = isr;
   priv->arg     = arg;
 
-  stm32_gpiosetevent(GPIO_HCSR04_INT, priv->rising, priv->falling, true,
+  stm32_gpiosetevent(BOARD_HCSR04_GPIO_INT, priv->rising, priv->falling, true,
                      isr, arg);
 
   leave_critical_section(flags);
@@ -187,7 +182,7 @@ static void hcsr04_irq_enable(FAR const struct hcsr04_config_s *state,
 
   iinfo("%d\n", enable);
 
-  stm32_gpiosetevent(GPIO_HCSR04_INT, priv->rising, priv->falling, true,
+  stm32_gpiosetevent(BOARD_HCSR04_GPIO_INT, priv->rising, priv->falling, true,
                      enable ? priv->isr : NULL, priv->arg);
 }
 
@@ -202,7 +197,7 @@ static void hcsr04_irq_clear(FAR const struct hcsr04_config_s *state)
 
 static void hcsr04_set_trigger(FAR const struct hcsr04_config_s *state, bool on)
 {
-  stm32_gpiowrite(GPIO_HCSR04_TRIG, on);
+  stm32_gpiowrite(BOARD_HCSR04_GPIO_TRIG, on);
 }
 
 /* Return the current Free Running clock tick */
@@ -227,11 +222,10 @@ static int64_t hcsr04_get_clock(FAR const struct hcsr04_config_s *state)
  *
  * Description:
  *   This function is called by application-specific, setup logic to
- *   configure the HC-SR04 sensor.  This function will register the driver
- *   as /dev/dist0 or any other name passed at *devname.
+ *   configure the HC-SR04 sensor.
  *
  * Input Parameters:
- *   devname   - The device name to register (i.e. "/dev/dist0").
+ *   devno - The device number, used to build the device path as /dev/distN
  *
  * Returned Value:
  *   Zero is returned on success.  Otherwise, a negated errno value is
@@ -239,30 +233,28 @@ static int64_t hcsr04_get_clock(FAR const struct hcsr04_config_s *state)
  *
  ****************************************************************************/
 
-int stm32_hcsr04_initialize(FAR const char *devname)
+int board_hcsr04_initialize(int devno)
 {
   int ret;
-
-  sinfo("devname = %s\n", devname);
+  char devpath[12];
 
   /* Configure the PIO interrupt */
 
-  stm32_configgpio(GPIO_HCSR04_INT);
+  stm32_configgpio(BOARD_HCSR04_GPIO_INT);
 
   /* Configure the Trigger pin */
 
-  stm32_configgpio(GPIO_HCSR04_TRIG);
+  stm32_configgpio(BOARD_HCSR04_GPIO_TRIG);
 
   /* Initialize the free-running timer with 1uS resolution */
 
-  ret = stm32_freerun_initialize(&g_freerun, HCSR04_FRTIMER, 1);
+  ret = stm32_freerun_initialize(&g_freerun, BOARD_HCSR04_FRTIMER, 1);
   if (ret < 0)
     {
       serr("Failed to initialize the free running timer! Err = %d\n", ret);
       return -ENODEV;
     }
 
-  return hcsr04_register(devname, &g_hcsr04config.config);
+  snprintf(devpath, 12, "/dev/dist%d", devno);
+  return hcsr04_register(devpath, &g_hcsr04config.config);
 }
-
-#endif /* CONFIG_STM32_FREERUN and CONFIG_SENSORS_HCSR04 */
diff --git a/boards/arm/stm32/stm32f103-minimum/include/board.h b/boards/arm/stm32/stm32f103-minimum/include/board.h
index 14de351..ced5bcf 100644
--- a/boards/arm/stm32/stm32f103-minimum/include/board.h
+++ b/boards/arm/stm32/stm32f103-minimum/include/board.h
@@ -218,4 +218,16 @@
 #define BOARD_NRF24L01_GPIO_CE     GPIO_NRF24L01_CE
 #define BOARD_NRF24L01_GPIO_IRQ    GPIO_NRF24L01_IRQ
 
+/* HCSR04 driver */
+
+/* Pins config to use with HC-SR04 sensor */
+
+#define GPIO_HCSR04_INT   (GPIO_INPUT|GPIO_CNF_INFLOAT|GPIO_PORTA|GPIO_PIN0)
+#define GPIO_HCSR04_TRIG  (GPIO_OUTPUT|GPIO_CNF_OUTPP|GPIO_MODE_50MHz|\
+                           GPIO_OUTPUT_CLEAR|GPIO_PORTA|GPIO_PIN1)
+
+#define BOARD_HCSR04_GPIO_INT  GPIO_HCSR04_INT
+#define BOARD_HCSR04_GPIO_TRIG GPIO_HCSR04_TRIG
+#define BOARD_HCSR04_FRTIMER   1    /* TIM1 as free running timer */
+
 #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 b9bf930..595056c 100644
--- a/boards/arm/stm32/stm32f103-minimum/src/Make.defs
+++ b/boards/arm/stm32/stm32f103-minimum/src/Make.defs
@@ -99,10 +99,6 @@ ifeq ($(CONFIG_CL_MFRC522),y)
   CSRCS += stm32_mfrc522.c
 endif
 
-ifeq ($(CONFIG_SENSORS_HCSR04),y)
-  CSRCS += stm32_hcsr04.c
-endif
-
 ifeq ($(CONFIG_LCD_MAX7219),y)
   CSRCS += stm32_max7219.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 82c4725..fc6c0ac 100644
--- a/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c
+++ b/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c
@@ -117,6 +117,10 @@
 #include "stm32_nrf24l01.h"
 #endif
 
+#ifdef CONFIG_SENSORS_HCSR04
+#include "stm32_hcsr04.h"
+#endif
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
@@ -326,7 +330,7 @@ int stm32_bringup(void)
 #ifdef CONFIG_SENSORS_HCSR04
   /* Configure and initialize the HC-SR04 distance sensor */
 
-  ret = stm32_hcsr04_initialize("/dev/dist0");
+  ret = board_hcsr04_initialize(0);
   if (ret < 0)
     {
       syslog(LOG_ERR, "ERROR: stm32_hcsr04_initialize() failed: %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 f4dbf04..0a28f83 100644
--- a/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h
+++ b/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h
@@ -137,12 +137,6 @@
 
 #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)
-#define GPIO_HCSR04_TRIG  (GPIO_OUTPUT|GPIO_CNF_OUTPP|GPIO_MODE_50MHz|\
-                           GPIO_OUTPUT_CLEAR|GPIO_PORTA|GPIO_PIN1)
-
 /* Pin for APDS-9960 sensor */
 
 #define GPIO_APDS9960_INT (GPIO_INPUT|GPIO_CNF_INFLOAT|GPIO_PORTA|GPIO_PIN0)
@@ -309,40 +303,6 @@ int stm32_mmcsd_initialize(int minor);
 #endif
 
 /****************************************************************************
- * Name: nunchuck_initialize
- *
- * Description:
- *   Initialize and register the button joystick driver
- *
- ****************************************************************************/
-
-#ifdef CONFIG_INPUT_NUNCHUCK
-int nunchuck_initialize(FAR char *devname);
-#endif
-
-/****************************************************************************
- * Name: stm32_hcsr04_initialize
- *
- * Description:
- *   Called to initialize the HC-SR04 sensor
- *
- ****************************************************************************/
-
-int stm32_hcsr04_initialize(FAR const char *devname);
-
-/****************************************************************************
- * Name: stm32_lm75initialize
- *
- * Description:
- *   Called to initialize LM75 temperature sensor
- *
- ****************************************************************************/
-
-#ifdef CONFIG_LM75_I2C
-int stm32_lm75initialize(FAR const char *devpath);
-#endif
-
-/****************************************************************************
  * Name: stm32_max6675initialize
  *
  * Description: