InetrfaceProducts NXP / PCF2127

Dependents:   PCF2127_Demo PCF2127_Hello

Files at this revision

API Documentation at this revision

Comitter:
nxp_ip
Date:
Thu Dec 04 04:58:24 2014 +0000
Child:
1:700e0285cfd8
Commit message:
initial version

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF2127.cpp	Thu Dec 04 04:58:24 2014 +0000
@@ -0,0 +1,148 @@
+/*
+ *  PCF2127 library
+ *
+ *  @author     Akifumi (Tedd) OKANO, NXP Semiconductors
+ *  @version    1.5
+ *  @date       04-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
+ *
+ *  RTC initializing part is ported from..
+ *    http://mbed.org/users/roen/notebook/real-time/
+ *
+ *  This code is refined version of..
+ *    http://developer.mbed.org/users/okano/code/NXP_PCF2127A/
+ */
+
+#include    "mbed.h"
+#include    "PCF2127.h"
+
+#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 )
+{
+    set_register( Control_1, vControl_1 );
+    set_register( Control_2, vControl_2 );
+    set_register( Control_3, vControl_3 );
+}
+
+PCF2127::~PCF2127()
+{
+}
+
+int PCF2127::is_init_required( void )
+{
+    return ( read_register( Seconds ) & 0x80 ? true : false );
+}
+
+void PCF2127::set_time( struct tm *dtp )
+{
+    char        buf[ 8 ];
+    char        c;
+
+    buf[ 0 ]    = Seconds;
+    buf[ 1 ]    = i2bcd( dtp->tm_sec  );
+    buf[ 2 ]    = i2bcd( dtp->tm_min  );
+    buf[ 3 ]    = i2bcd( dtp->tm_hour );
+    buf[ 4 ]    = i2bcd( dtp->tm_mday );
+    buf[ 5 ]    = i2bcd( dtp->tm_wday );
+    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 );
+}
+
+void PCF2127::set_time( char *s )
+{
+    //  The time information should be given in format of "YYYY MM DD HH MM SS"
+
+    struct tm   dt, *dtp;
+
+    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) );
+
+    // adjust for tm structure required values
+    dtp->tm_year = dtp->tm_year - 1900;
+    dtp->tm_mon  = dtp->tm_mon - 1;
+
+    set_time( dtp );
+}
+
+time_t PCF2127::time( time_t *tp )
+{
+    struct tm   dt, *dtp;
+    time_t      t;
+    char        buf[ 8 ]    = { Seconds };
+
+    dtp = &dt;
+
+    i2c.write( device_address, buf, 1 );
+    i2c.read( device_address, buf, 7 );
+
+    dtp->tm_sec     = bcd2i( buf[ 0 ] );
+    dtp->tm_min     = bcd2i( buf[ 1 ] );
+    dtp->tm_hour    = bcd2i( buf[ 2 ] );
+    dtp->tm_mday    = bcd2i( buf[ 3 ] );
+    dtp->tm_wday    = bcd2i( buf[ 4 ] );
+    dtp->tm_mon     = bcd2i( buf[ 5 ] ) - 1;
+    dtp->tm_year    = bcd2i( buf[ 6 ] ) + 100;
+
+    t   = mktime( dtp );
+
+    if ( tp )
+        *tp  = t;
+
+    return( t );
+}
+
+void PCF2127::set_alarm( char addr, char s )
+{
+    char    v;
+
+    v   = i2bcd( s );
+    set_register( addr, v );
+}
+
+void PCF2127::clear_intr( void )
+{
+    set_register( Control_2, 0x00 );
+}
+
+void PCF2127::set_register( char addr, char data )
+{
+    char    b[ 2 ];
+
+    b[ 0 ]    = addr;
+    b[ 1 ]    = data;
+
+    i2c.write( device_address, b, 2 );
+}
+
+char PCF2127::read_register( char addr )
+{
+    char    data;
+
+    data    = addr;
+    i2c.write( device_address, &data, 1 );
+    i2c.read( device_address, &data, 1 );
+
+    return ( data );
+}
+
+char PCF2127::i2bcd( char n )
+{
+    return ( ((n / 10) << 4) | (n % 10) );
+}
+
+char PCF2127::bcd2i( char bcd )
+{
+    return ( ((bcd >> 4) * 10) + (bcd & 0x0F) );
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF2127.h	Thu Dec 04 04:58:24 2014 +0000
@@ -0,0 +1,121 @@
+/*
+ *  PCF2127 library
+ *
+ *  @author     Akifumi (Tedd) OKANO, NXP Semiconductors
+ *  @version    1.5
+ *  @date       04-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
+ *
+ *  RTC initializing part is ported from..
+ *    http://mbed.org/users/roen/notebook/real-time/
+ *
+ *  This code is refined version of..
+ *    http://developer.mbed.org/users/okano/code/NXP_PCF2127A/
+ */
+
+#ifndef        MBED_PCF2127
+#define        MBED_PCF2127
+
+#include    "mbed.h"
+
+class PCF2127
+{
+public:
+
+    /** name of the PCF2127 registers */
+    typedef enum {
+        Control_1,
+        Control_2,
+        Control_3,
+        Seconds,
+        Minutes,
+        Hours,
+        Days,
+        Weekdays,
+        Months,
+        Years,
+        Second_alarm,
+        Minute_alarm,
+        Hour_alarm,
+        Day_alarm,
+        Weekday_alarm,
+        CLKOUT_ctl,
+        Watchdg_tim_ctl,
+        Watchdg_tim_val,
+        Timestp_ctl,
+        Sec_timestp,
+        Min_timestp,
+        Hour_timestp,
+        Day_timestp,
+        Mon_timestp,
+        Year_timestp,
+        Aging_offset
+    }
+    RegisterName;
+
+    /** 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_2 (option) data for Control_2 register
+     *  @param vControl_3 (option) data for Control_3 register
+     */
+    PCF2127( PinName sda, PinName sdl, char vControl_1 = 0x03, char vControl_2 = 0x00, char vControl_3 = 0x60 );
+
+    /** Destractor
+     */
+    ~PCF2127();
+
+    /** Clock integrity check
+     *
+     *  @return non-zero value if the clock was stopped (means need to set the time)
+     */
+    int     is_init_required( void );
+
+    /** Set the time
+     *
+     *  @param dtp Pointer to struct tm
+     */
+    void    set_time( struct tm *dtp );
+
+    /** 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 );
+
+    /** Get time of day
+     *
+     *  This function works similar to "time()" in standard-C-library
+     *
+     *  @param tp Pointer to time_t
+     */
+    time_t  time( time_t *tp );
+
+    /** Register access interface with integer to BCD conversion
+     *
+     *  @param addr Register address
+     *  @param Integer data. Converted to BCD before writing inot the register.
+     */
+    void    set_alarm( char addr, char s );
+
+    /** Clear interrupt flag
+     *
+     *  Clears interrupt flags by writing 0x00 into Control_2 register
+     *
+     */
+    void    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;
+    char    device_address;
+}
+;
+#endif  // end of "#ifndef MBED_PCF2127"