Page 1 of 2

DS1307 RTC I2C library

Posted: Mon Sep 23, 2013 2:38 pm
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://federossi@hg.code.sf.net/p/chibio ... s1307/code chibiostinyrtcds1307-code[/url]

I hope that it will helpful to someone, besides me :)
bye
fede

Re: DS1307 RTC I2C library

Posted: Mon Sep 23, 2013 10:35 pm
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));
}

Re: DS1307 RTC I2C library

Posted: Tue Sep 24, 2013 8:40 am
by Fede
Thank you pito!
I'll update the library...
bye

Re: DS1307 RTC I2C library

Posted: Tue Sep 24, 2013 11:11 am
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");

Re: DS1307 RTC I2C library

Posted: Tue Sep 24, 2013 11:48 am
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);
..

Re: DS1307 RTC I2C library

Posted: Tue Sep 24, 2013 1:56 pm
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 )

Re: DS1307 RTC I2C library

Posted: Tue Sep 24, 2013 2:12 pm
by pito
.. files are already posted - feel free to use it.. :)
PS: mind the DS1307 is 5V only..

Re: DS1307 RTC I2C library

Posted: Tue Sep 24, 2013 2:40 pm
by Fede
Upgrading code ... ... ... Done!

Re: DS1307 RTC I2C library

Posted: Wed Sep 25, 2013 8:15 pm
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.

Re: DS1307 RTC I2C library

Posted: Fri Sep 27, 2013 6:07 pm
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!