boards/arm/stm32/stm32f4discovery/src: Add support for external DS1307 module.

This commit is contained in:
Alan Carvalho de Assis 2019-09-22 07:08:00 -06:00 committed by Gregory Nutt
parent c123967fe9
commit 8eafe880dc
5 changed files with 201 additions and 0 deletions

View File

@ -38,6 +38,7 @@ Contents
- Quadrature Encoder
- FPU
- STM32F4DIS-BB
- RTC DS1307
- SSD1289
- UG-2864AMBAG01 / UG-2864HSWEG01
- HCI UART
@ -417,6 +418,52 @@ On-board PIO usage:
PC10 DAT2
---------- ------------- ------------------------------
RTC DS1307
==========
It is possible to use a low cost extern DS1307 RTC to keep date and time
always updated. These DS1307 RTC modules come with a 3V button battery, then
even when the board is turned OFF the Date/Time registers keep running.
You can connect the module this way (STM32F4Discovery to DS1307 board): GND
to GND; 5V to VCC; PB9 to SDA; PB6 to SCL. In the NuttX menuconfig you need
to enable these options:
System Type --->
STM32 Peripheral Support --->
[*] I2C1
Device Drivers --->
Timer Driver Support --->
[*] RTC Driver Support --->
-*- Date/Time RTC Support
[*] External RTC Support
[*] DS130x/DS323x RTC Driver
Maxim Integrated RTC (DS1307) --->
(100000) DS1307/DS323x I2C frequency
Application Configuration --->
NSH Library --->
Disable Individual commands --->
[ ] Disable date ( <-- Deselect )
It is also a good idea to enable the DEBUG to RTC initially, you will see:
ABCDF
stm32_ds1307_init: Initialize I2C1
stm32_ds1307_init: Bind the DS1307 RTC driver to I2C1
rtc_dumptime: Returning:
rtc_dumptime: tm_sec: 00000039
rtc_dumptime: tm_min: 00000001
rtc_dumptime: tm_hour: 00000009
rtc_dumptime: tm_mday: 00000016
rtc_dumptime: tm_mon: 00000008
rtc_dumptime: tm_year: 00000077
NuttShell (NSH)
nsh> date
Sep 22 09:01:58 2019
SSD1289
=======

View File

@ -125,6 +125,10 @@ 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

View File

@ -329,6 +329,15 @@ int stm32_bringup(void)
}
#endif
#ifdef CONFIG_RTC_DS1307
ret = stm32_ds1307_init();
if (ret < 0)
{
syslog(LOG_ERR, "Failed to initialize DS1307 RTC driver: %d\n", ret);
return ret;
}
#endif
#ifdef HAVE_RTC_DRIVER
/* Instantiate the STM32 lower-half RTC driver */

View File

@ -0,0 +1,128 @@
/****************************************************************************
* boards/arm/stm32/stm32f4discovery/src/stm32_ds1307.c
*
* Copyright (C) 2019 Alan Carvalho de Assis. All rights reserved.
* Author: Alan Carvalho de Assis <acassis@gmail.com>
*
* 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 <errno.h>
#include <debug.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/timers/rtc.h>
#include <nuttx/timers/ds3231.h>
#include "stm32.h"
#include "stm32_i2c.h"
#include "stm32f4discovery.h"
#if defined(CONFIG_I2C) && defined(CONFIG_RTC_DS1307)
/****************************************************************************
* 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
*
* Description:
* Initialize and configure the DS1307 RTC
*
****************************************************************************/
int stm32_ds1307_init(void)
{
FAR 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;
}
#ifdef CONFIG_I2C_DRIVER
/* Register the I2C to get the "nsh> i2c bus" command working */
ret = i2c_register(i2c, DS1307_I2C_BUS);
if (ret < 0)
{
rtcerr("ERROR: Failed to register I2C%d driver: %d\n", bus, ret);
return -ENODEV;
}
#endif
/* Synchronize the system time to the RTC time */
clock_synchronize();
/* Now we are initialized */
initialized = true;
}
return OK;
}
#endif /* CONFIG_I2C && CONFIG_RTC_DS1307 */

View File

@ -216,6 +216,7 @@
#endif
/* STM32F4 Discovery GPIOs **************************************************/
/* LEDs */
#define GPIO_LED1 (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
@ -497,6 +498,18 @@ int nunchuck_initialize(FAR char *devname);
int stm32_max7219init(FAR 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
*