DS1307 RTC I2C library

This forum is about you. Feel free to discuss anything is related to embedded and electronics, your awesome projects, your ideas, your announcements, not necessarily related to ChibiOS but to embedded in general. This forum is NOT for support.
User avatar
Fede
Posts: 37
Joined: Thu Nov 08, 2012 8:30 pm

DS1307 RTC I2C library

Post by Fede »

Hi all!

this is a first release of a small library that allow the communication with DS1307 RTC module...
https://sourceforge.net/projects/chibiostinyrtcds1307/
[url]hg clone ssh://[email protected]/p/chibio ... s1307/code chibiostinyrtcds1307-code[/url]

I hope that it will helpful to someone, besides me :)
bye
fede
pito
Posts: 199
Joined: Sun Nov 06, 2011 3:54 pm

Re: DS1307 RTC I2C library

Post by pito »

Fyi: a faster routine for unixtime conversion (from arduino forum by fat16lib):

Code: Select all

// Time starts 2000-1-1  For Linux 1970-1-1 is used.
//const uint16_t EPOCH_YEAR = 1970;  // Linux
const uint16_t EPOCH_YEAR = 2000;
//------------------------------------------------------------------------------
/** Count of days since Epoch.
 * 1900 < EPOCH_YEAR, MAX_YEAR < 2100, (MAX_YEAR - EPOCH_YEAR) < 178.
 * \param[in] y year (EPOCH_YEAR <= y <= MAX_YEAR)
 * \param[in] m month 1 <= m <= 12
 * \param[in] d day 1 <= d <= 31
 * \return Count of days since epoch
 *
 * Derived from Zeller's congruence
 */
uint16_t daysSinceEpoch(uint16_t y, uint8_t m, uint8_t d) {
  if (m < 3) {
    m += 12;
    y--;
  }
  return 365 * (y + 1 - EPOCH_YEAR)  + y / 4 - (EPOCH_YEAR - 1) / 4
    + (153 * m - 2) / 5 + d - 398;
}
//------------------------------------------------------------------------------
/** Seconds since 1 Jan EPOCH_YEAR */
uint32_t secondsSinceEpoch(uint16_t year, uint8_t month, uint8_t day,
                           uint8_t hour, uint8_t minute, uint8_t second) {
  uint16_t days = daysSinceEpoch(year, month, day);
  return second + 60L * (minute + 60L * (hour + 24L * days));
}
User avatar
Fede
Posts: 37
Joined: Thu Nov 08, 2012 8:30 pm

Re: DS1307 RTC I2C library

Post by Fede »

Thank you pito!
I'll update the library...
bye
pito
Posts: 199
Joined: Sun Nov 06, 2011 3:54 pm

Re: DS1307 RTC I2C library

Post by pito »

Hi Fede,
trying to run your example with PCF8563 (similar to DS1307) on stm32VL but I get panic in

Code: Select all

RTC_PCF8563_status = i2cMasterTransmitTimeout(&I2CD1, PCF8563_ADDRESS, 0x00, 1, rxbuf, 9, TMO);

at

Code: Select all

  chDbgCheck((i2cp != NULL) && (addr != 0) &&
             (txbytes > 0) && (txbuf != NULL) &&
             ((rxbytes == 0) || ((rxbytes > 0) && (rxbuf != NULL))) &&
             (timeout != TIME_IMMEDIATE),
             "i2cMasterTransmitTimeout");
pito
Posts: 199
Joined: Sun Nov 06, 2011 3:54 pm

Re: DS1307 RTC I2C library

Post by pito »

This works ( "txbuf" instead of "0x00" in your i2cMasterTransmitTimeout() calls ) :

Code: Select all

..
uint8_t rxbuf[RTC_PCF8563_RX_DEPTH];
   uint8_t txbuf[1];
   txbuf[0]= 0x00;
   i2cAcquireBus(&I2CD1);
   RTC_PCF8563_status = i2cMasterTransmitTimeout(&I2CD1, PCF8563_ADDRESS, txbuf, 1, rxbuf, 9, TMO);
   i2cReleaseBus(&I2CD1);
..
User avatar
Fede
Posts: 37
Joined: Thu Nov 08, 2012 8:30 pm

Re: DS1307 RTC I2C library

Post by Fede »

Hi pito!
thanks again, i'll update the library with this trick...

If you are interested, i would like to add you as author of ChibiOS TinyRTC DS1307 repo. (with R/W privileges)

So you could upload your version (PCF8563) in the same repo...

tell me about this idea...

PS: only if you like to share your code, you can post here the files and then i push it in the repo... (it's only an idea :D )
pito
Posts: 199
Joined: Sun Nov 06, 2011 3:54 pm

Re: DS1307 RTC I2C library

Post by pito »

.. files are already posted - feel free to use it.. :)
PS: mind the DS1307 is 5V only..
User avatar
Fede
Posts: 37
Joined: Thu Nov 08, 2012 8:30 pm

Re: DS1307 RTC I2C library

Post by Fede »

Upgrading code ... ... ... Done!
pito
Posts: 199
Joined: Sun Nov 06, 2011 3:54 pm

Re: DS1307 RTC I2C library

Post by pito »

Hi Fede,
enclosed your DS1307.c file with fixing some issues (do compare the files). I get none warnings with it (w/ my rtc).
One Q:
there is an rtc support in chibios already (os/various/chrtclib.h.c), using <time.h> library and the standard tm structure for dealing with time stuff. I like the uint64_t unix time with useconds in it :).

Thinking about to integrate both drivers with the chibios rtc lib somehow..
http://chibios.sourceforge.net/docs/ker ... r_t_c.html

The simplest version would be to have an rtcDS1307GetTime(..) and rtcPCF8563GetTime(..) which update the tm structure with the rtc time.
Attachments
chTinyRTC.rar
(2.76 KiB) Downloaded 818 times
User avatar
Fede
Posts: 37
Joined: Thu Nov 08, 2012 8:30 pm

Re: DS1307 RTC I2C library

Post by Fede »

Hi pito, thank you for your trouble-shooting!
I think that it is possible to use the standard tm structure... but sincerely this integration is too much... "time consuming for my stuff"

But this is downright a good idea...

Ps: Repo updated!
Post Reply