Pages: [1] 2   Go Down
Author Topic: Time and RTC libs - question on date and time integrity  (Read 5390 times)
0 Members and 1 Guest are viewing this topic.
Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, while playing with Time and RTC libraries (building a simple logger) I came accross following Q:
Is there any mechanism built into the libs which could avoid the situation the Date and Time may desintegrate while reading them around 00:00:00?
I mean the situation where I may read:

a) time_t t=now();
or
b) the sync provider (ie. the setSyncProvider(rtc.get_tm); ) reads the RTC and writes into time_t

while the DATE read is ie. 31/07/2012 and TIME jumps from 23:59:59 to 00:00:00 just before the moment of reading the time related structure (for both internal time_t and external RTC). Then the date&time set will be 31/7/2012 00:00:00, thus a day difference..
p.
« Last Edit: July 31, 2012, 09:52:36 am by pito » Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1673
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

All registers in the RTC must be read in the same I2C transaction.  I know RTClib https://github.com/adafruit/RTClib reads the DS1307 correctly.

I have written my own libraries for various RTC chips and all Maxim DS I2C chips have this feature:
Quote
When reading or writing the time and date registers, secondary (user) buffers are used to prevent errors when the internal registers update. When reading the time and date registers, the user buffers are synchronized to the internal registers on any I2C START. The time information is read from these secondary registers while the clock continues to run. This eliminates the need to re-read the registers in case the internal registers update during a read.
If your library has this bug, just read the time twice until the two values are equal.  Better yet find a new library.
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1673
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pito,

I also wrote my own replacement for Wire for I2C master access to devices so I have better control over the I2C protocol. 

I assume Wire does the read correctly as a single transaction but it is very complex with interrupts and both slave and master support.
Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Rtc_Pcf8563 library I am using reads Date and Time in two separate transactions. NXP recommends to read in one block to maintain integrity. So it needs to be modified..
http://arduino.cc/playground/Main/RTC-PCF8563
p.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4094
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Many RTC chips protect against this in the hardware. For example, from the DS1307 datasheet:

Quote
When reading or writing the time and date registers, secondary (user) buffers are used to prevent errors when the
internal registers update. When reading the time and date registers, the user buffers are synchronized to the
internal registers on any I2C START. The time information is read from these secondary registers while the clock
continues to run. This eliminates the need to re-read the registers in case the internal registers update during a
read. The divider chain is reset whenever the seconds register is written. Write transfers occur on the I2C
acknowledge from the DS1307. Once the divider chain is reset, to avoid rollover issues, the remaining time and
date registers must be written within one second.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Peoples Republic of Cantabrigia
Offline Offline
God Member
*****
Karma: 6
Posts: 722
Arduino happiness
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The Rtc_Pcf8563 library I am using reads Date and Time in two separate transactions.
For the DS3231, I read it all in one go and process from there. This is what I would always recommend. Perhaps there is a way to combine the reads into one transaction even if the results are later parsed in two different subroutines?
« Last Edit: July 31, 2012, 02:24:36 pm by Constantin » Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1673
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

RTClib here https://github.com/jcw/rtclib read all PCF8563 registers correctly:
Code:
DateTime RTC_PCF8563::now() {
    Wire.beginTransmission(PCF8563_ADDRESS);
    Wire.write(PCF8563_SEC_ADDR);
    Wire.endTransmission();

    Wire.requestFrom(PCF8563_ADDRESS, 7);
    uint8_t ss = bcd2bin(Wire.read() & 0x7F);
    uint8_t mm = bcd2bin(Wire.read() & 0x7F);
    uint8_t hh = bcd2bin(Wire.read() & 0x3F);
    uint8_t d = bcd2bin(Wire.read() & 0x3F);
    Wire.read();
    uint8_t m = bcd2bin(Wire.read()& 0x1F);
    uint16_t y = bcd2bin(Wire.read()) + 2000;
    
    return DateTime (y, m, d, hh, mm, ss);
}

RTClib also works with DS1307.  DS1307 libraries often work with some ot the other Maxim I2C RTC chips.
« Last Edit: July 31, 2012, 12:32:52 pm by fat16lib » Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe we need an "RTC" library (or it shall be a part of Time.cpp) with all that functionality you may find with various rtc_chip's libs, and the libs related to the chips shall include only low level routines for reading/writing datetime and alarm..
For example ds1307 and pcf libs differ in number of various datetime conversion functions, even the chips are basically the same except the registers order..
p.
« Last Edit: July 31, 2012, 01:41:01 pm by pito » Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Enclosed pls find the modified library for PCF8563 ( see http://arduino.cc/playground/Main/RTC-PCF8563 ).
Added getDateAndTime() for oneshot regs read, and get_tm() for usage with ie. setSyncProvider(rtc.get_tm)..
p.

* Rtc_Pcf8563.h (4.31 KB - downloaded 36 times.)
* Rtc_Pcf8563.cpp (14.54 KB - downloaded 37 times.)
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1673
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote several RTC GPS date time libraries. 

The most complete library uses UTC for RTC chips and GPS.  It handles time zones and daylight/summer time. It can format date time in ISO formats or other custom formats.  It has all popular POSIX conversions between epoch time and local/UTC.  It has a database and algorithms for common DST rules.

This library is overkill for most users so I never posted it.

Some uses need a very lean library for data logging that just gives yyyy-mm-dd hh:mm:ss from the RTC registers.  Other users need more features.

My guess that existing libraries satisfy most user's needs if you get rid of bugs.

Date time libraries can be a bottomless pit.  Just look a libraries like Boost Date Time.

This is more of a problem of knowing what users need than writing code.  Once you know the requirements code is easy.
Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One positive thing about the Time.cpp library is it uses "time_t" what is the unix_time. So the rtc chip library needs to convert the rtc date and time to time_t, all the other functions are various formatting exercises around time_t only..
So the rtc_chip_lib shall include ~4 functions only: getDateAndTime(), setDateAndTime() and the same with Alarm..
« Last Edit: July 31, 2012, 02:56:40 pm by pito » Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1673
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Time library does not produce Unix/POSIX time.  The standard for the POSIX epoch is 1970-01-01 00:00:00 UTC.

Not the number of seconds from some random local time.  POSIX time is the same in every time zone at a given instant.

http://www.epochconverter.com/
Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is what I get (UTC):
07/31/2012  22:29:58  1343773798
p.
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1673
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So what?  Time can not convert between POSIX time and local time since it doesn't know about time zones.  It is not a POSIX time library.

You must be two hours east of Zulu time. Well one hour plus summer time.
« Last Edit: July 31, 2012, 03:42:19 pm by fat16lib » Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 197
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote several RTC GPS date time libraries. 

The most complete library uses UTC for RTC chips and GPS.  It handles time zones and daylight/summer time. It can format date time in ISO formats or other custom formats.  It has all popular POSIX conversions between epoch time and local/UTC.  It has a database and algorithms for common DST rules.

This library is overkill for most users so I never posted it.

....

This is more of a problem of knowing what users need than writing code.  Once you know the requirements code is easy.

Several months ago, I bought a do-it-yourself alarm clock called the Alpha Clock Five. I bought it with the intention of modifying it to show the date as well as the time, and also to show seconds. I found it immensely difficult to modify to my liking. Maybe I'm not much of a coder.

What I would have found extremely useful during the process would have been a library like the one you describe. What I would like to know, though, is two things:
  • How fast does your library run? This clock uses a multiplexed LED display: would I see constant flickering while the time is being recalculated?
  • I made a display mode which shows the time in increments of 1/8 minute. This is 7.5 seconds per increment. Can your library handle the half second?

Please post your library so I can have a look at it.

Some functions I have written are here: http://arduino.cc/forum/index.php/topic,101401.0.html
Logged

Pages: [1] 2   Go Up
Jump to: