From 9009cd6f2dc23756d2d490bf395565d2cfe8ddcc Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Tue, 16 Apr 2024 19:41:38 -0300 Subject: [PATCH] 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 --- .../arm/stm32/common/include/stm32_ds1307.h | 83 +++++++++++++++++++ boards/arm/stm32/common/src/Make.defs | 4 + .../src/stm32_ds1307.c | 71 ++++++++-------- .../arm/stm32/stm32f4discovery/src/Make.defs | 4 - .../stm32f4discovery/src/stm32_bringup.c | 6 +- .../stm32f4discovery/src/stm32f4discovery.h | 12 --- 6 files changed, 125 insertions(+), 55 deletions(-) create mode 100644 boards/arm/stm32/common/include/stm32_ds1307.h rename boards/arm/stm32/{stm32f4discovery => common}/src/stm32_ds1307.c (65%) diff --git a/boards/arm/stm32/common/include/stm32_ds1307.h b/boards/arm/stm32/common/include/stm32_ds1307.h new file mode 100644 index 0000000000..370e17ef40 --- /dev/null +++ b/boards/arm/stm32/common/include/stm32_ds1307.h @@ -0,0 +1,83 @@ +/**************************************************************************** + * 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 + * 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 __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_DS1307_H +#define __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_DS1307_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Type Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * 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 board_ds1307_initialize(int busno); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#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,61 +40,57 @@ * 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? */ + rtcinfo("Initialize I2C%d\n", DS1307_I2C_BUS); - if (!initialized) + /* Initialize I2C */ + + i2c = stm32_i2cbus_initialize(busno); + if (!i2c) { - /* 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; + rtcerr("ERROR: Failed to initialize I2C%d\n", busno); + return -ENODEV; } + /* Now bind the I2C interface to the DS1307 RTC driver */ + + 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; + } + + /* Synchronize the system time to the RTC time */ + + clock_synchronize(NULL); + + /* 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 *