2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 21:07:55 +01:00
|
|
|
* drivers/timers/ds3231.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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
|
2015-11-17 21:07:55 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-17 21:07:55 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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.
|
2015-11-17 21:07:55 +01:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 21:07:55 +01:00
|
|
|
* Included Files
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2016-01-30 15:00:16 +01:00
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2015-11-17 21:07:55 +01:00
|
|
|
#include <nuttx/timers/ds3231.h>
|
|
|
|
|
|
|
|
#include "ds3231.h"
|
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
#ifdef CONFIG_RTC_DSXXXX
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 21:07:55 +01:00
|
|
|
* Pre-processor Definitions
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2019-12-07 06:36:33 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Configuration ************************************************************/
|
2019-12-07 06:36:33 +01:00
|
|
|
|
2015-11-17 21:07:55 +01:00
|
|
|
/* This RTC implementation supports only date/time RTC hardware */
|
|
|
|
|
|
|
|
#ifndef CONFIG_RTC_DATETIME
|
|
|
|
# error CONFIG_RTC_DATETIME must be set to use this driver
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_RTC_HIRES
|
|
|
|
# error CONFIG_RTC_HIRES must NOT be set with this driver
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_DS3231_I2C_FREQUENCY
|
|
|
|
# error CONFIG_DS3231_I2C_FREQUENCY is not configured
|
|
|
|
# define CONFIG_DS3231_I2C_FREQUENCY 400000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_DS3231_I2C_FREQUENCY > 400000
|
|
|
|
# error CONFIG_DS3231_I2C_FREQUENCY is out of range
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DS3231_I2C_ADDRESS 0x68
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2020-02-23 09:50:23 +01:00
|
|
|
* Private Types
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2019-12-07 06:36:33 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* This structure describes the state of the DS3231 chip.
|
|
|
|
* Only a single RTC is supported.
|
2015-11-17 21:07:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct ds3231_dev_s
|
|
|
|
{
|
2016-01-30 15:36:47 +01:00
|
|
|
FAR struct i2c_master_s *i2c; /* Contained reference to the I2C bus driver */
|
2015-11-17 21:07:55 +01:00
|
|
|
};
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 21:07:55 +01:00
|
|
|
* Public Data
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* g_rtc_enabled is set true after the RTC has successfully initialized */
|
|
|
|
|
|
|
|
volatile bool g_rtc_enabled = false;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 21:07:55 +01:00
|
|
|
* Private Data
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2019-12-07 06:36:33 +01:00
|
|
|
|
2015-11-17 21:07:55 +01:00
|
|
|
/* The state of the DS3231 chip. Only a single RTC is supported */
|
|
|
|
|
|
|
|
static struct ds3231_dev_s g_ds3231;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 21:07:55 +01:00
|
|
|
* Private Functions
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 21:07:55 +01:00
|
|
|
* Name: rtc_dumptime
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Show the broken out time.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2016-06-16 15:39:23 +02:00
|
|
|
#ifdef CONFIG_DEBUG_RTC_INFO
|
2015-11-17 21:07:55 +01:00
|
|
|
static void rtc_dumptime(FAR struct tm *tp, FAR const char *msg)
|
|
|
|
{
|
2016-06-16 15:39:23 +02:00
|
|
|
rtcinfo("%s:\n", msg);
|
|
|
|
rtcinfo(" tm_sec: %08x\n", tp->tm_sec);
|
|
|
|
rtcinfo(" tm_min: %08x\n", tp->tm_min);
|
|
|
|
rtcinfo(" tm_hour: %08x\n", tp->tm_hour);
|
|
|
|
rtcinfo(" tm_mday: %08x\n", tp->tm_mday);
|
|
|
|
rtcinfo(" tm_mon: %08x\n", tp->tm_mon);
|
|
|
|
rtcinfo(" tm_year: %08x\n", tp->tm_year);
|
|
|
|
rtcinfo(" tm_wday: %08x\n", tp->tm_wday);
|
|
|
|
rtcinfo(" tm_yday: %08x\n", tp->tm_yday);
|
|
|
|
rtcinfo(" tm_isdst: %08x\n", tp->tm_isdst);
|
2015-11-17 21:07:55 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define rtc_dumptime(tp, msg)
|
|
|
|
#endif
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 21:07:55 +01:00
|
|
|
* Name: rtc_bin2bcd
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Converts a 2 digit binary to BCD format
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* value - The byte to be converted.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The value in BCD representation
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
static uint8_t rtc_bin2bcd(int value)
|
|
|
|
{
|
|
|
|
uint8_t msbcd = 0;
|
|
|
|
|
|
|
|
while (value >= 10)
|
|
|
|
{
|
|
|
|
msbcd++;
|
|
|
|
value -= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (msbcd << 4) | value;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-19 21:54:30 +01:00
|
|
|
* Name: rtc_bcd2bin
|
2015-11-17 21:07:55 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert from 2 digit BCD to binary.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* value - The BCD value to be converted.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The value in binary representation
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
static int rtc_bcd2bin(uint8_t value)
|
|
|
|
{
|
|
|
|
int tens = ((int)value >> 4) * 10;
|
|
|
|
return tens + (value & 0x0f);
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 21:07:55 +01:00
|
|
|
* Public Functions
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-19 21:54:30 +01:00
|
|
|
* Name: dsxxxx_rtc_initialize
|
2015-11-17 21:07:55 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2021-03-04 08:02:21 +01:00
|
|
|
* Initialize the hardware RTC per the selected configuration.
|
|
|
|
* This function is called once during the OS initialization sequence by
|
|
|
|
* board-specific logic.
|
2015-11-17 23:24:09 +01:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
* After dsxxxx_rtc_initialize() is called, the OS function
|
|
|
|
* clock_synchronize() should also be called to synchronize the system
|
|
|
|
* timer to a hardware RTC. That operation is normally performed
|
|
|
|
* automatically by the system during clock initialization. However, when
|
|
|
|
* an external RTC is used, the board logic will need to explicitly
|
|
|
|
* re-synchronize the system timer to the RTC when the RTC becomes
|
|
|
|
* available.
|
2015-11-17 21:07:55 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno on failure
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2016-01-30 15:36:47 +01:00
|
|
|
int dsxxxx_rtc_initialize(FAR struct i2c_master_s *i2c)
|
2015-11-17 21:07:55 +01:00
|
|
|
{
|
|
|
|
/* Remember the i2c device and claim that the RTC is enabled */
|
|
|
|
|
|
|
|
g_ds3231.i2c = i2c;
|
|
|
|
g_rtc_enabled = true;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 21:07:55 +01:00
|
|
|
* Name: up_rtc_getdatetime
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the current date and time from the date/time RTC. This interface
|
|
|
|
* is only supported by the date/time RTC hardware implementation.
|
2021-03-04 08:02:21 +01:00
|
|
|
* It is used to replace the system timer. It is only used by the RTOS
|
|
|
|
* during initialization to set up the system time when CONFIG_RTC and
|
|
|
|
* CONFIG_RTC_DATETIME are selected (and CONFIG_RTC_HIRES is not).
|
2015-11-17 21:07:55 +01:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
* NOTE:
|
|
|
|
* Some date/time RTC hardware is capability of sub-second accuracy.
|
|
|
|
* That sub-second accuracy is lost in this interface. However, since the
|
|
|
|
* system time is reinitialized on each power-up/reset, there will be no
|
|
|
|
* timing inaccuracy in the long run.
|
2015-11-17 21:07:55 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tp - The location to return the high resolution time value.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno on failure
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
int up_rtc_getdatetime(FAR struct tm *tp)
|
|
|
|
{
|
2015-11-17 21:39:47 +01:00
|
|
|
struct i2c_msg_s msg[4];
|
|
|
|
uint8_t secaddr;
|
|
|
|
uint8_t buffer[7];
|
|
|
|
uint8_t seconds;
|
2015-11-17 21:07:55 +01:00
|
|
|
int tmp;
|
|
|
|
int ret;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* If this function is called before the RTC has been initialized
|
|
|
|
* (and it will be), then just return the data/time of the epoch,
|
|
|
|
* 12:00 am, Jan 1, 1970.
|
2015-11-19 21:54:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!g_rtc_enabled)
|
|
|
|
{
|
|
|
|
tp->tm_sec = 0;
|
|
|
|
tp->tm_min = 0;
|
|
|
|
tp->tm_hour = 0;
|
|
|
|
|
|
|
|
/* Jan 1, 1970 was a Thursday */
|
|
|
|
|
|
|
|
tp->tm_wday = 4;
|
|
|
|
tp->tm_mday = 1;
|
|
|
|
tp->tm_mon = 0;
|
|
|
|
tp->tm_year = 70;
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
2015-11-17 21:07:55 +01:00
|
|
|
/* Select to begin reading at the seconds register */
|
|
|
|
|
2016-02-01 21:17:20 +01:00
|
|
|
secaddr = DSXXXX_TIME_SECR;
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2016-02-01 21:17:20 +01:00
|
|
|
msg[0].frequency = CONFIG_DS3231_I2C_FREQUENCY;
|
|
|
|
msg[0].addr = DS3231_I2C_ADDRESS;
|
|
|
|
msg[0].flags = 0;
|
|
|
|
msg[0].buffer = &secaddr;
|
|
|
|
msg[0].length = 1;
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2015-11-17 21:39:47 +01:00
|
|
|
/* Set up to read 7 registers: secondss, minutes, hour, day-of-week, date,
|
|
|
|
* month, year
|
|
|
|
*/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2016-02-01 21:17:20 +01:00
|
|
|
msg[1].frequency = CONFIG_DS3231_I2C_FREQUENCY;
|
|
|
|
msg[1].addr = DS3231_I2C_ADDRESS;
|
|
|
|
msg[1].flags = I2C_M_READ;
|
|
|
|
msg[1].buffer = buffer;
|
|
|
|
msg[1].length = 7;
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2015-11-17 21:39:47 +01:00
|
|
|
/* Read the seconds register again */
|
|
|
|
|
2016-02-01 21:17:20 +01:00
|
|
|
msg[2].frequency = CONFIG_DS3231_I2C_FREQUENCY;
|
|
|
|
msg[2].addr = DS3231_I2C_ADDRESS;
|
|
|
|
msg[2].flags = 0;
|
|
|
|
msg[2].buffer = &secaddr;
|
|
|
|
msg[2].length = 1;
|
2015-11-17 21:39:47 +01:00
|
|
|
|
2016-02-01 21:17:20 +01:00
|
|
|
msg[3].frequency = CONFIG_DS3231_I2C_FREQUENCY;
|
|
|
|
msg[3].addr = DS3231_I2C_ADDRESS;
|
|
|
|
msg[3].flags = I2C_M_READ;
|
|
|
|
msg[3].buffer = &seconds;
|
|
|
|
msg[3].length = 1;
|
2015-11-17 21:39:47 +01:00
|
|
|
|
2016-02-01 19:14:31 +01:00
|
|
|
/* Perform the transfer. The transfer may be performed repeatedly of the
|
2021-03-04 08:02:21 +01:00
|
|
|
* seconds values decreases, meaning that that was a rollover in the
|
|
|
|
* seconds.
|
2015-11-17 21:39:47 +01:00
|
|
|
*/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2015-11-17 21:39:47 +01:00
|
|
|
do
|
2015-11-17 21:07:55 +01:00
|
|
|
{
|
2015-11-17 21:39:47 +01:00
|
|
|
ret = I2C_TRANSFER(g_ds3231.i2c, msg, 4);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-01-24 13:55:14 +01:00
|
|
|
rtcerr("ERROR: I2C_TRANSFER failed: %d\n", ret);
|
2015-11-17 21:39:47 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2015-11-17 21:07:55 +01:00
|
|
|
}
|
2015-11-19 21:54:30 +01:00
|
|
|
while ((buffer[0] & DSXXXX_TIME_SEC_BCDMASK) >
|
|
|
|
(seconds & DSXXXX_TIME_SEC_BCDMASK));
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* Format the return time */
|
2019-12-07 06:36:33 +01:00
|
|
|
|
2015-11-17 21:07:55 +01:00
|
|
|
/* Return seconds (0-61) */
|
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
tp->tm_sec = rtc_bcd2bin(buffer[0] & DSXXXX_TIME_SEC_BCDMASK);
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* Return minutes (0-59) */
|
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
tp->tm_min = rtc_bcd2bin(buffer[1] & DSXXXX_TIME_MIN_BCDMASK);
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* Return hour (0-23). This assumes 24-hour time was set. */
|
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
tp->tm_hour = rtc_bcd2bin(buffer[2] & DSXXXX_TIME_HOUR24_BCDMASK);
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* Return the day of the week (0-6) */
|
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
tp->tm_wday = (rtc_bcd2bin(buffer[3]) & DSXXXX_TIME_DAY_MASK) - 1;
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* Return the day of the month (1-31) */
|
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
tp->tm_mday = rtc_bcd2bin(buffer[4] & DSXXXX_TIME_DATE_BCDMASK);
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* Return the month (0-11) */
|
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
tp->tm_mon = rtc_bcd2bin(buffer[5] & DSXXXX_TIME_MONTH_BCDMASK) - 1;
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
/* Return the years since 1900 */
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
tmp = rtc_bcd2bin(buffer[6] & DSXXXX_TIME_YEAR_BCDMASK);
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2015-11-20 02:31:52 +01:00
|
|
|
#if defined(CONFIG_RTC_DS3231) || defined(CONFIG_RTC_DS3232)
|
2015-11-19 22:57:36 +01:00
|
|
|
if ((buffer[5] & DS323X_TIME_CENTURY_MASK) == DS323X_TIME_1900)
|
2015-11-17 21:07:55 +01:00
|
|
|
{
|
|
|
|
tp->tm_year = tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tp->tm_year = tmp + 100;
|
|
|
|
}
|
2015-11-19 21:54:30 +01:00
|
|
|
#else
|
2021-03-04 08:02:21 +01:00
|
|
|
/* No century indicator.
|
|
|
|
* The RTC will hold years since 1968 (a leap year like 2000)
|
2015-11-19 22:12:40 +01:00
|
|
|
*/
|
2015-11-19 21:54:30 +01:00
|
|
|
|
2015-11-19 22:12:40 +01:00
|
|
|
tp->tm_year = tmp + 68;
|
2015-11-19 21:54:30 +01:00
|
|
|
#endif
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
rtc_dumptime(tp, "Returning");
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 21:07:55 +01:00
|
|
|
* Name: up_rtc_settime
|
|
|
|
*
|
|
|
|
* Description:
|
2021-03-04 08:02:21 +01:00
|
|
|
* Set the RTC to the provided time. All RTC implementations must be able
|
|
|
|
* to set their time based on a standard timespec.
|
2015-11-17 21:07:55 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tp - the time to use
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno on failure
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
int up_rtc_settime(FAR const struct timespec *tp)
|
|
|
|
{
|
2015-11-17 21:39:47 +01:00
|
|
|
struct i2c_msg_s msg[3];
|
2015-11-17 21:07:55 +01:00
|
|
|
struct tm newtm;
|
|
|
|
time_t newtime;
|
|
|
|
uint8_t buffer[8];
|
2015-11-17 21:39:47 +01:00
|
|
|
uint8_t seconds;
|
2015-11-17 21:07:55 +01:00
|
|
|
uint8_t century;
|
|
|
|
uint8_t year;
|
|
|
|
int ret;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* If this function is called before the RTC has been initialized then just
|
|
|
|
* return an error.
|
2015-11-19 21:54:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!g_rtc_enabled)
|
|
|
|
{
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:55:14 +01:00
|
|
|
rtcinfo("Setting time tp=(%d,%d)\n", (int)tp->tv_sec, (int)tp->tv_nsec);
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* Get the broken out time */
|
|
|
|
|
|
|
|
newtime = (time_t)tp->tv_sec;
|
|
|
|
if (tp->tv_nsec >= 500000000)
|
|
|
|
{
|
|
|
|
/* Round up */
|
|
|
|
|
|
|
|
newtime++;
|
|
|
|
}
|
|
|
|
|
2019-12-07 06:36:33 +01:00
|
|
|
if (localtime_r(&newtime, &newtm) == NULL)
|
|
|
|
{
|
|
|
|
rtcerr("ERROR: localtime_r failed\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:55:14 +01:00
|
|
|
rtc_dumptime(&newtm, "New time");
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* Construct the message */
|
2019-12-07 06:36:33 +01:00
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Write starting with the seconds register */
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
buffer[0] = DSXXXX_TIME_SECR;
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* Save seconds (0-59) converted to BCD */
|
|
|
|
|
|
|
|
buffer[1] = rtc_bin2bcd(newtm.tm_sec);
|
|
|
|
|
|
|
|
/* Save minutes (0-59) converted to BCD */
|
|
|
|
|
|
|
|
buffer[2] = rtc_bin2bcd(newtm.tm_min);
|
|
|
|
|
2015-11-21 00:36:10 +01:00
|
|
|
/* Save hour (0-23) with 24-hour time indication */
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
buffer[3] = rtc_bin2bcd(newtm.tm_hour) | DSXXXX_TIME_24;
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* Save the day of the week (1-7) */
|
|
|
|
|
|
|
|
buffer[4] = rtc_bin2bcd(newtm.tm_wday + 1);
|
|
|
|
|
2015-11-21 00:36:10 +01:00
|
|
|
/* Save the day of the month (1-31) */
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
buffer[5] = rtc_bin2bcd(newtm.tm_mday);
|
|
|
|
|
2015-11-20 02:31:52 +01:00
|
|
|
#if defined(CONFIG_RTC_DS3231) || defined(CONFIG_RTC_DS3232)
|
2015-11-17 21:07:55 +01:00
|
|
|
/* Handle years in the 20th vs the 21st century */
|
|
|
|
|
|
|
|
if (newtm.tm_year < 100)
|
|
|
|
{
|
|
|
|
/* Convert years in the range 1900-1999 */
|
|
|
|
|
2015-11-19 22:57:36 +01:00
|
|
|
century = DS323X_TIME_1900;
|
2015-11-17 21:07:55 +01:00
|
|
|
year = newtm.tm_year;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Convert years in the range 2000-2099 */
|
|
|
|
|
2015-11-19 22:57:36 +01:00
|
|
|
century = DS323X_TIME_2000;
|
2015-11-17 21:07:55 +01:00
|
|
|
year = newtm.tm_year - 100;
|
|
|
|
}
|
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
#else
|
2015-11-19 22:12:40 +01:00
|
|
|
/* Use years since 1968 (a leap year like 2000) */
|
2015-11-19 21:54:30 +01:00
|
|
|
|
|
|
|
century = 0;
|
2015-11-19 22:12:40 +01:00
|
|
|
year = newtm.tm_year - 68;
|
2015-11-19 21:54:30 +01:00
|
|
|
#endif
|
|
|
|
|
2015-11-17 21:07:55 +01:00
|
|
|
/* Save the month (1-12) with century */
|
|
|
|
|
|
|
|
buffer[6] = rtc_bin2bcd(newtm.tm_mon + 1) | century;
|
|
|
|
|
|
|
|
/* Save the year */
|
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
buffer[7] = rtc_bin2bcd(year);
|
2015-11-17 21:07:55 +01:00
|
|
|
|
|
|
|
/* Setup the I2C message */
|
|
|
|
|
2016-02-01 21:17:20 +01:00
|
|
|
msg[0].frequency = CONFIG_DS3231_I2C_FREQUENCY;
|
|
|
|
msg[0].addr = DS3231_I2C_ADDRESS;
|
|
|
|
msg[0].flags = 0;
|
|
|
|
msg[0].buffer = buffer;
|
|
|
|
msg[0].length = 8;
|
2015-11-17 21:39:47 +01:00
|
|
|
|
|
|
|
/* Read back the seconds register */
|
|
|
|
|
2016-02-01 21:17:20 +01:00
|
|
|
msg[1].frequency = CONFIG_DS3231_I2C_FREQUENCY;
|
|
|
|
msg[1].addr = DS3231_I2C_ADDRESS;
|
|
|
|
msg[1].flags = 0;
|
|
|
|
msg[1].buffer = buffer;
|
|
|
|
msg[1].length = 1;
|
|
|
|
|
|
|
|
msg[2].frequency = CONFIG_DS3231_I2C_FREQUENCY;
|
|
|
|
msg[2].addr = DS3231_I2C_ADDRESS;
|
|
|
|
msg[2].flags = I2C_M_READ;
|
|
|
|
msg[2].buffer = &seconds;
|
|
|
|
msg[2].length = 1;
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
/* Perform the transfer. This transfer will be repeated if the seconds
|
|
|
|
* count rolls over to a smaller value while writing.
|
2015-11-18 00:35:17 +01:00
|
|
|
*/
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2015-11-17 21:39:47 +01:00
|
|
|
do
|
2015-11-17 21:07:55 +01:00
|
|
|
{
|
2015-11-17 21:39:47 +01:00
|
|
|
ret = I2C_TRANSFER(g_ds3231.i2c, msg, 3);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-01-24 13:55:14 +01:00
|
|
|
rtcerr("ERROR: I2C_TRANSFER failed: %d\n", ret);
|
2015-11-17 21:39:47 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2015-11-17 21:07:55 +01:00
|
|
|
}
|
2015-11-19 21:54:30 +01:00
|
|
|
while ((buffer[1] & DSXXXX_TIME_SEC_BCDMASK) >
|
|
|
|
(seconds & DSXXXX_TIME_SEC_BCDMASK));
|
2015-11-17 21:07:55 +01:00
|
|
|
|
2015-11-17 21:39:47 +01:00
|
|
|
return OK;
|
2015-11-17 21:07:55 +01:00
|
|
|
}
|
|
|
|
|
2015-11-19 21:54:30 +01:00
|
|
|
#endif /* CONFIG_RTC_DSXXXX */
|