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

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

commit 9009cd6f2dc23756d2d490bf395565d2cfe8ddcc
Author: Alan Carvalho de Assis <[email protected]>
AuthorDate: Tue Apr 16 19:41:38 2024 -0300

    boards: Move DS1307 initialization to stm32/common
    
    The DS1307 was used as board specific, but it is better move it to
    stm32/common to be easily used by other boards as well.
    
    Signed-off-by: Alan C. Assis <[email protected]>
---
 .../include/stm32_ds1307.h}                        | 95 +++++++++-------------
 boards/arm/stm32/common/src/Make.defs              |  4 +
 .../src/stm32_ds1307.c                             | 63 +++++++-------
 boards/arm/stm32/stm32f4discovery/src/Make.defs    |  4 -
 .../arm/stm32/stm32f4discovery/src/stm32_bringup.c |  6 +-
 .../stm32/stm32f4discovery/src/stm32f4discovery.h  | 12 ---
 6 files changed, 76 insertions(+), 108 deletions(-)

diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_ds1307.c 
b/boards/arm/stm32/common/include/stm32_ds1307.h
similarity index 53%
copy from boards/arm/stm32/stm32f4discovery/src/stm32_ds1307.c
copy to boards/arm/stm32/common/include/stm32_ds1307.h
index 06b91f0c8c..370e17ef40 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32_ds1307.c
+++ b/boards/arm/stm32/common/include/stm32_ds1307.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/arm/stm32/stm32f4discovery/src/stm32_ds1307.c
+ * boards/arm/stm32/common/include/stm32_ds1307.h
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -18,85 +18,66 @@
  *
  ****************************************************************************/
 
+#ifndef __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_DS1307_H
+#define __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_DS1307_H
+
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
 
-#include <errno.h>
-#include <debug.h>
-
-#include <nuttx/i2c/i2c_master.h>
-#include <nuttx/timers/rtc.h>
-#include <nuttx/timers/ds3231.h>
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
 
-#include "stm32.h"
-#include "stm32_i2c.h"
-#include "stm32f4discovery.h"
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
 
-#if defined(CONFIG_I2C) && defined(CONFIG_RTC_DS1307)
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
 
 /****************************************************************************
- * Pre-processor Definitions
+ * Public Data
  ****************************************************************************/
 
-#define DS1307_I2C_ADDR    0x6f /* DS1307 I2C Address */
-#define DS1307_I2C_BUS     1    /* DS1307 is on I2C1 */
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
 
 /****************************************************************************
- * Public Functions
+ * Public Function Prototypes
  ****************************************************************************/
 
 /****************************************************************************
- * Name: stm32_ds1307_init
+ * Name: board_ds1307_initialize
  *
  * Description:
  *   Initialize and configure the DS1307 RTC
  *
+ * Input Parameters:
+ *   busno - The I2C bus number where DS1307 is connected.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
  ****************************************************************************/
 
-int stm32_ds1307_init(void)
-{
-  struct i2c_master_s *i2c;
-  static bool initialized = false;
-  int ret;
-
-  /* Have we already initialized? */
-
-  if (!initialized)
-    {
-      /* No.. Get the I2C bus driver */
-
-      rtcinfo("Initialize I2C%d\n", DS1307_I2C_BUS);
-      i2c = stm32_i2cbus_initialize(DS1307_I2C_BUS);
-      if (!i2c)
-        {
-          rtcerr("ERROR: Failed to initialize I2C%d\n", DS1307_I2C_BUS);
-          return -ENODEV;
-        }
-
-      /* Now bind the I2C interface to the DS1307 RTC driver */
-
-      rtcinfo("Bind the DS1307 RTC driver to I2C%d\n", DS1307_I2C_BUS);
-      ret = dsxxxx_rtc_initialize(i2c);
-      if (ret < 0)
-        {
-          rtcerr("ERROR: Failed to bind I2C%d to the DS1307 RTC driver\n",
-                 DS1307_I2C_BUS);
-          return -ENODEV;
-        }
-
-      /* Synchronize the system time to the RTC time */
-
-      clock_synchronize(NULL);
-
-      /* Now we are initialized */
-
-      initialized = true;
-    }
+int board_ds1307_initialize(int busno);
 
-  return OK;
+#undef EXTERN
+#ifdef __cplusplus
 }
+#endif
 
-#endif /* CONFIG_I2C && CONFIG_RTC_DS1307 */
+#endif /* __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_DS1307_H */
diff --git a/boards/arm/stm32/common/src/Make.defs 
b/boards/arm/stm32/common/src/Make.defs
index 4cdb9f7ac2..d99d07f062 100644
--- a/boards/arm/stm32/common/src/Make.defs
+++ b/boards/arm/stm32/common/src/Make.defs
@@ -60,6 +60,10 @@ ifeq ($(CONFIG_LCD_SSD1306),y)
   CSRCS += stm32_ssd1306.c
 endif
 
+ifeq ($(CONFIG_RTC_DS1307),y)
+  CSRCS += stm32_ds1307.c
+endif
+
 ifeq ($(CONFIG_SENSORS_LM75),y)
   CSRCS += stm32_lm75.c
 endif
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_ds1307.c 
b/boards/arm/stm32/common/src/stm32_ds1307.c
similarity index 65%
rename from boards/arm/stm32/stm32f4discovery/src/stm32_ds1307.c
rename to boards/arm/stm32/common/src/stm32_ds1307.c
index 06b91f0c8c..328730da77 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32_ds1307.c
+++ b/boards/arm/stm32/common/src/stm32_ds1307.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/arm/stm32/stm32f4discovery/src/stm32_ds1307.c
+ * boards/arm/stm32/common/src/stm32_ds1307.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -33,7 +33,6 @@
 
 #include "stm32.h"
 #include "stm32_i2c.h"
-#include "stm32f4discovery.h"
 
 #if defined(CONFIG_I2C) && defined(CONFIG_RTC_DS1307)
 
@@ -41,60 +40,56 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define DS1307_I2C_ADDR    0x6f /* DS1307 I2C Address */
-#define DS1307_I2C_BUS     1    /* DS1307 is on I2C1 */
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Name: stm32_ds1307_init
+ * Name: board_ds1307_initialize
  *
  * Description:
  *   Initialize and configure the DS1307 RTC
  *
+ * Input Parameters:
+ *   busno - The I2C bus number where DS1307 is connected.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
  ****************************************************************************/
 
-int stm32_ds1307_init(void)
+int board_ds1307_initialize(int busno)
 {
   struct i2c_master_s *i2c;
-  static bool initialized = false;
   int ret;
 
-  /* Have we already initialized? */
-
-  if (!initialized)
-    {
-      /* No.. Get the I2C bus driver */
+  rtcinfo("Initialize I2C%d\n", DS1307_I2C_BUS);
 
-      rtcinfo("Initialize I2C%d\n", DS1307_I2C_BUS);
-      i2c = stm32_i2cbus_initialize(DS1307_I2C_BUS);
-      if (!i2c)
-        {
-          rtcerr("ERROR: Failed to initialize I2C%d\n", DS1307_I2C_BUS);
-          return -ENODEV;
-        }
+  /* Initialize I2C */
 
-      /* Now bind the I2C interface to the DS1307 RTC driver */
+  i2c = stm32_i2cbus_initialize(busno);
+  if (!i2c)
+    {
+      rtcerr("ERROR: Failed to initialize I2C%d\n", busno);
+      return -ENODEV;
+    }
 
-      rtcinfo("Bind the DS1307 RTC driver to I2C%d\n", DS1307_I2C_BUS);
-      ret = dsxxxx_rtc_initialize(i2c);
-      if (ret < 0)
-        {
-          rtcerr("ERROR: Failed to bind I2C%d to the DS1307 RTC driver\n",
-                 DS1307_I2C_BUS);
-          return -ENODEV;
-        }
+  /* Now bind the I2C interface to the DS1307 RTC driver */
 
-      /* Synchronize the system time to the RTC time */
+  rtcinfo("Bind the DS1307 RTC driver to I2C%d\n", busno);
+  ret = dsxxxx_rtc_initialize(i2c);
+  if (ret < 0)
+    {
+      rtcerr("ERROR: Failed to bind I2C%d to the DS1307 RTC driver\n",
+             DS1307_I2C_BUS);
+      return -ENODEV;
+    }
 
-      clock_synchronize(NULL);
+  /* Synchronize the system time to the RTC time */
 
-      /* Now we are initialized */
+  clock_synchronize(NULL);
 
-      initialized = true;
-    }
+  /* Now we are initialized */
 
   return OK;
 }
diff --git a/boards/arm/stm32/stm32f4discovery/src/Make.defs 
b/boards/arm/stm32/stm32f4discovery/src/Make.defs
index 9bfa037602..4db5b7fc3f 100644
--- a/boards/arm/stm32/stm32f4discovery/src/Make.defs
+++ b/boards/arm/stm32/stm32f4discovery/src/Make.defs
@@ -88,10 +88,6 @@ ifeq ($(CONFIG_RGBLED),y)
 CSRCS += stm32_rgbled.c
 endif
 
-ifeq ($(CONFIG_RTC_DS1307),y)
-CSRCS += stm32_ds1307.c
-endif
-
 ifeq ($(CONFIG_PWM),y)
 CSRCS += stm32_pwm.c
 endif
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c 
b/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
index 4a62e0b64a..125fecee12 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
@@ -76,6 +76,10 @@
 #include "stm32_bmp180.h"
 #endif
 
+#ifdef CONFIG_RTC_DS1307
+#include "stm32_ds1307.h"
+#endif
+
 #ifdef CONFIG_SENSORS_MS56XX
 #include "stm32_ms5611.h"
 #endif
@@ -454,7 +458,7 @@ int stm32_bringup(void)
 #endif
 
 #ifdef CONFIG_RTC_DS1307
-  ret = stm32_ds1307_init();
+  ret = board_ds1307_initialize(1);
   if (ret < 0)
     {
       syslog(LOG_ERR, "Failed to initialize DS1307 RTC driver: %d\n", ret);
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h 
b/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
index ab08656198..89048910bb 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
@@ -522,18 +522,6 @@ int nunchuck_initialize(char *devname);
 int stm32_max7219init(const char *devpath);
 #endif
 
-/****************************************************************************
- * Name: stm32_ds1307_init
- *
- * Description:
- *   Initialize and register the DS1307 RTC
- *
- ****************************************************************************/
-
-#ifdef CONFIG_RTC_DS1307
-int stm32_ds1307_init(void);
-#endif
-
 /****************************************************************************
  * Name: stm32_st7032init
  *

Reply via email to