PCF2127 and PCF2129 are high accuracy real-time-clock (RTC) module. This library provides simple interface to accessing clock information.

Dependents:   PCF2127_Demo PCF2127_Hello

PCF2127 and PCF2129

PCF2127T PCF2127T is in SO16 package

The PCF2127 and the PCF2129 are a CMOS Real Time Clock (RTC) and calendar with an integrated Temperature Compensated Crystal (Xtal) Oscillator (TCXO) and a 32.768 kHz quartz crystal optimized for very high accuracy and very low power consumption.
Both of PCF2127 and PCF2129 have a selectable I2C-bus or SPI-bus, a backup battery switch-over circuit, a programmable watchdog function, a timestamp function, and many other features.
On addition to this, the PCF2127 has 512 bytes of general-purpose static RAM.

These 4 types of RTC modules are software compatible. So this library "PCF2127" can be used all of those.
This library only supports I2C to communicate with the PCF2127/PCF2129.

Type variations

Main feature difference

type+/-3ppm accuracy range512 bytes RAMpackage
PCF2127T-30℃ to +80℃yesSO16
PCF2127AT-30℃ to +60℃yesSO20
PCF2129T-30℃ to +80℃not availableSO16
PCF2129AT-15℃ to +60℃not availableSO20

Pin assign

/media/uploads/nxp_ip/pcf2127_pcf2129_pin_assign.png

PCF2127T
PCF2127T

Connection between MCU and PCF2127/PCF2129

These examples show how the RTC module can be connected via I2C bus.

http://developer.mbed.org/media/components/pinouts/both_types_connection3.png

References

Files at this revision

API Documentation at this revision

Comitter:
nxp_ip
Date:
Tue Dec 09 07:21:13 2014 +0000
Parent:
0:1377bcf1455e
Child:
2:db76c68f998f
Commit message:
version 1.6:; changed: "battery switch-over function" as standard mode; changed: "CLKOUT" frequency is 1Hz; added: constructor by I2C object; added: set_time( time_t *tp ) function

Changed in this revision

PCF2127.cpp Show annotated file Show diff for this revision Revisions of this file
PCF2127.h Show annotated file Show diff for this revision Revisions of this file
--- a/PCF2127.cpp	Thu Dec 04 04:58:24 2014 +0000
+++ b/PCF2127.cpp	Tue Dec 09 07:21:13 2014 +0000
@@ -2,8 +2,8 @@
  *  PCF2127 library
  *
  *  @author     Akifumi (Tedd) OKANO, NXP Semiconductors
- *  @version    1.5
- *  @date       04-Dec-2014
+ *  @version    1.6
+ *  @date       09-Dec-2014
  *
  *  PCF2127 is a "real time clock (RTC)" module which is including a Xtal and TCXO
  *  http://www.nxp.com/products/interface_and_connectivity/real_time_clocks/rtcs_with_temp_compensation/series/PCF2127.html
@@ -20,16 +20,38 @@
 
 #define     PCF2127_I2C_SLAVE_ADDRESS   0xA2
 
-PCF2127::PCF2127( PinName sda, PinName sdl, char vControl_1, char vControl_2, char vControl_3 )
-    : i2c( sda, sdl ), device_address( PCF2127_I2C_SLAVE_ADDRESS )
+PCF2127::PCF2127( PinName sda, PinName sdl, char vControl_1, char vControl_2, char vControl_3, char vCLKOUT_ctl )
+    : i2c_p( new I2C( sda, sdl ) ), i2c( *i2c_p ), device_address( PCF2127_I2C_SLAVE_ADDRESS )
 {
-    set_register( Control_1, vControl_1 );
-    set_register( Control_2, vControl_2 );
-    set_register( Control_3, vControl_3 );
+    init( vControl_1, vControl_2, vControl_3, vCLKOUT_ctl );
+}
+
+PCF2127::PCF2127( I2C &i2c_, char vControl_1, char vControl_2, char vControl_3, char vCLKOUT_ctl )
+    : i2c_p( NULL ), i2c( i2c_ ), device_address( PCF2127_I2C_SLAVE_ADDRESS )
+{
+    init( vControl_1, vControl_2, vControl_3, vCLKOUT_ctl );
 }
 
 PCF2127::~PCF2127()
 {
+    if ( NULL != i2c_p )
+        delete  i2c_p;
+}
+
+int PCF2127::init( char vControl_1, char vControl_2, char vControl_3, char vCLKOUT_ctl )
+{
+    char    data[ 4 ];
+    int     err;
+    
+    data[ 0 ]   = Control_1; //  access start register address
+    data[ 1 ]   = vControl_1;
+    data[ 2 ]   = vControl_2;
+    data[ 3 ]   = vControl_3;
+    
+    err     = i2c.write( device_address, data, sizeof( data ) );
+    err    |= set_register( CLKOUT_ctl, vCLKOUT_ctl );
+
+    return ( err ? I2C_ACCESS_FAIL : NO_ERROR );
 }
 
 int PCF2127::is_init_required( void )
@@ -37,10 +59,10 @@
     return ( read_register( Seconds ) & 0x80 ? true : false );
 }
 
-void PCF2127::set_time( struct tm *dtp )
+int PCF2127::set_time( struct tm *dtp )
 {
     char        buf[ 8 ];
-    char        c;
+    char        err;
 
     buf[ 0 ]    = Seconds;
     buf[ 1 ]    = i2bcd( dtp->tm_sec  );
@@ -51,14 +73,17 @@
     buf[ 6 ]    = i2bcd( dtp->tm_mon  + 1   );
     buf[ 7 ]    = i2bcd( dtp->tm_year - 100 );
 
-    c    = read_register( Seconds );
-    while ( c == read_register( Seconds ) )
-        ;
-
-    i2c.write( device_address, buf, 8 );
+    err = i2c.write( device_address, buf, 8 );
+    
+    return ( err ? I2C_ACCESS_FAIL : NO_ERROR );
 }
 
-void PCF2127::set_time( char *s )
+int PCF2127::set_time( time_t *tp )
+{
+    return ( set_time( localtime( tp ) ) );
+}
+
+int PCF2127::set_time( char *s )
 {
     //  The time information should be given in format of "YYYY MM DD HH MM SS"
 
@@ -67,25 +92,30 @@
     dtp = &dt;
 
     sscanf( s, "%d %d %d %d %d %d", &(dtp->tm_year), &(dtp->tm_mon), &(dtp->tm_mday), &(dtp->tm_hour), &(dtp->tm_min), &(dtp->tm_sec) );
-    printf( "%d/%d/%d - %d:%d:%d\r\n", (dtp->tm_year), (dtp->tm_mon), (dtp->tm_mday), (dtp->tm_hour), (dtp->tm_min), (dtp->tm_sec) );
+    printf( "%02d/%02d/%02d - %02d:%02d:%02d\r\n", (dtp->tm_year), (dtp->tm_mon), (dtp->tm_mday), (dtp->tm_hour), (dtp->tm_min), (dtp->tm_sec) );
 
     // adjust for tm structure required values
     dtp->tm_year = dtp->tm_year - 1900;
     dtp->tm_mon  = dtp->tm_mon - 1;
 
-    set_time( dtp );
+    return ( set_time( dtp ) );
 }
 
 time_t PCF2127::time( time_t *tp )
 {
     struct tm   dt, *dtp;
     time_t      t;
-    char        buf[ 8 ]    = { Seconds };
+    char        buf[ 8 ];
 
     dtp = &dt;
 
-    i2c.write( device_address, buf, 1 );
-    i2c.read( device_address, buf, 7 );
+    buf[ 0 ]    = Seconds;  //  read start register address
+
+    if ( i2c.write( device_address, buf, 1 ) )
+        return ( TIME_FUNC_ERROR );
+
+    if ( i2c.read( device_address, buf, 7 ) )
+        return ( TIME_FUNC_ERROR );
 
     dtp->tm_sec     = bcd2i( buf[ 0 ] );
     dtp->tm_min     = bcd2i( buf[ 1 ] );
@@ -103,30 +133,31 @@
     return( t );
 }
 
-void PCF2127::set_alarm( char addr, char s )
+int PCF2127::set_alarm( char addr, char s )
 {
     char    v;
 
     v   = i2bcd( s );
-    set_register( addr, v );
+    
+    return ( set_register( addr, v ) );
 }
 
-void PCF2127::clear_intr( void )
+int PCF2127::clear_intr( void )
 {
-    set_register( Control_2, 0x00 );
+    return ( set_register( Control_2, 0x00 ) );
 }
 
-void PCF2127::set_register( char addr, char data )
+int PCF2127::set_register( char addr, char data )
 {
     char    b[ 2 ];
 
     b[ 0 ]    = addr;
     b[ 1 ]    = data;
 
-    i2c.write( device_address, b, 2 );
+    return ( i2c.write( device_address, b, 2 ) );
 }
 
-char PCF2127::read_register( char addr )
+int PCF2127::read_register( char addr )
 {
     char    data;
 
--- a/PCF2127.h	Thu Dec 04 04:58:24 2014 +0000
+++ b/PCF2127.h	Tue Dec 09 07:21:13 2014 +0000
@@ -2,8 +2,8 @@
  *  PCF2127 library
  *
  *  @author     Akifumi (Tedd) OKANO, NXP Semiconductors
- *  @version    1.5
- *  @date       04-Dec-2014
+ *  @version    1.6
+ *  @date       09-Dec-2014
  *
  *  PCF2127 is a "real time clock (RTC)" module which is including a Xtal and TCXO
  *  http://www.nxp.com/products/interface_and_connectivity/real_time_clocks/rtcs_with_temp_compensation/series/PCF2127.html
@@ -54,17 +54,38 @@
         Aging_offset
     }
     RegisterName;
-
+    
+    /** Error code */
+    typedef enum {
+        NO_ERROR                = 0,
+        CLOCK_INTEGRITY_FAIL    = 1,
+        I2C_ACCESS_FAIL         = 2,
+        TIME_FUNC_ERROR         = ((time_t)-1)
+    }
+    ErrorNum;
+    
     /** Create a PCF2127 instance connected to specified I2C pins with specified address
      *
      *  @param I2C_sda I2C-bus SDA pin
      *  @param I2C_scl I2C-bus SCL pin
-     *  @param vControl_1 (option) data for Control_1 register
+     *  @param vControl_1 (option) data for Control_1 register (default setting generates interrupts by second and minute)
      *  @param vControl_2 (option) data for Control_2 register
-     *  @param vControl_3 (option) data for Control_3 register
+     *  @param vControl_3 (option) data for Control_3 register (default setting of battery switch-over function as standard mode)
+     *  @param vCLKOUT_ctl (option) data for CLKOUT_ctl register (default setting 1Hz output on CLKOUT pin)
+     CLKOUT_ctl
      */
-    PCF2127( PinName sda, PinName sdl, char vControl_1 = 0x03, char vControl_2 = 0x00, char vControl_3 = 0x60 );
+    PCF2127( PinName sda, PinName sdl, char vControl_1 = Cntl1, char vControl_2 = Cntl2, char vControl_3 = Cntl1, char vCLKOUT_ctl = ClkOut );
 
+    /** Create a PCF2127 instance connected to specified I2C pins with specified address
+     *
+     *  @param i2c I2C object (instance)
+     *  @param vControl_1 (option) data for Control_1 register (default setting generates interrupts by second and minute)
+     *  @param vControl_2 (option) data for Control_2 register
+     *  @param vControl_3 (option) data for Control_3 register (default setting of battery switch-over function as standard mode)
+     *  @param vCLKOUT_ctl (option) data for CLKOUT_ctl register (default setting 1Hz output on CLKOUT pin)
+     */
+    PCF2127( I2C &i2c, char vControl_1 = Cntl1, char vControl_2 = Cntl2, char vControl_3 = Cntl1, char vCLKOUT_ctl = ClkOut );
+    
     /** Destractor
      */
     ~PCF2127();
@@ -79,12 +100,18 @@
      *
      *  @param dtp Pointer to struct tm
      */
-    void    set_time( struct tm *dtp );
+    int     set_time( struct tm *dtp );
+    
+    /** Set the time
+     *
+     *  @param tp pointer to time_t
+     */
+    int     set_time( time_t *tp );
 
     /** Set the time
      *  @param s String data: The time information should be given in format of "YYYY MM DD HH MM SS"
      */
-    void    set_time( char *s );
+    int     set_time( char *s );
 
     /** Get time of day
      *
@@ -99,22 +126,33 @@
      *  @param addr Register address
      *  @param Integer data. Converted to BCD before writing inot the register.
      */
-    void    set_alarm( char addr, char s );
+    int     set_alarm( char addr, char s );
 
     /** Clear interrupt flag
      *
      *  Clears interrupt flags by writing 0x00 into Control_2 register
      *
      */
-    void    clear_intr( void );
+    int     clear_intr( void );
 
 private:
-    void set_register( char addr, char data );
-    char read_register( char addr );
-    char i2bcd( char n );
-    char bcd2i( char bcd );
 
-    I2C     i2c;
+    typedef enum {
+        Cntl1   = 0x03,
+        Cntl2   = 0x00,
+        Cntl3   = 0x00,
+        ClkOut  = 0x46
+    }
+    DefaultRegParam;
+    
+    int     init( char vControl_1, char vControl_2, char vControl_3, char vCLKOUT_ctl );
+    int     set_register( char addr, char data );
+    int     read_register( char addr );
+    char    i2bcd( char n );
+    char    bcd2i( char bcd );
+
+    I2C     *i2c_p;
+    I2C     &i2c;
     char    device_address;
 }
 ;