InetrfaceProducts NXP / PCF2127

Dependents:   PCF2127_Demo PCF2127_Hello

Files at this revision

API Documentation at this revision

Comitter:
nxp_ip
Date:
Wed Dec 10 01:43:30 2014 +0000
Parent:
1:700e0285cfd8
Child:
3:e2a6ac61fcbd
Commit message:
internal RAM access API has been added (for PCF2127 only)

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	Tue Dec 09 07:21:13 2014 +0000
+++ b/PCF2127.cpp	Wed Dec 10 01:43:30 2014 +0000
@@ -2,8 +2,8 @@
  *  PCF2127 library
  *
  *  @author     Akifumi (Tedd) OKANO, NXP Semiconductors
- *  @version    1.6
- *  @date       09-Dec-2014
+ *  @version    1.7
+ *  @date       10-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
@@ -42,12 +42,12 @@
 {
     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 );
 
@@ -74,7 +74,7 @@
     buf[ 7 ]    = i2bcd( dtp->tm_year - 100 );
 
     err = i2c.write( device_address, buf, 8 );
-    
+
     return ( err ? I2C_ACCESS_FAIL : NO_ERROR );
 }
 
@@ -138,7 +138,7 @@
     char    v;
 
     v   = i2bcd( s );
-    
+
     return ( set_register( addr, v ) );
 }
 
@@ -147,6 +147,46 @@
     return ( set_register( Control_2, 0x00 ) );
 }
 
+int PCF2127::RAM_write( int address, char *p, int size )
+{
+    char    b[ size + 1 ];
+
+    b[ 0 ]  = RAM_wrt_cmd;
+
+    for ( int i = 1; i <= size; i++ )
+        b[ i ]  = *p++;
+
+    if ( set_RAM_address( address ) )
+        return ( I2C_ACCESS_FAIL );
+
+    return ( i2c.write( device_address, b, sizeof( b ) ) ? I2C_ACCESS_FAIL : NO_ERROR );
+}
+
+int PCF2127::RAM_read( int address, char *p, int size )
+{
+    char    b   = RAM_rd_cmd;
+
+    if ( set_RAM_address( address ) )
+        return ( I2C_ACCESS_FAIL );
+
+    if ( i2c.write( device_address, &b, 1 ) )
+        return ( I2C_ACCESS_FAIL );
+
+    return ( i2c.read( device_address, p, size ) ? I2C_ACCESS_FAIL : NO_ERROR );
+}
+
+
+int PCF2127::set_RAM_address( char address )
+{
+    char    b[ 3 ];
+
+    b[ 0 ]  = RAM_addr_MSB;
+    b[ 1 ]  = (address >> 8) & 0x1;
+    b[ 2 ]  = address & 0xFF;
+
+    return ( i2c.write( device_address, b, sizeof( b ) ) );
+}
+
 int PCF2127::set_register( char addr, char data )
 {
     char    b[ 2 ];
@@ -154,7 +194,7 @@
     b[ 0 ]    = addr;
     b[ 1 ]    = data;
 
-    return ( i2c.write( device_address, b, 2 ) );
+    return ( i2c.write( device_address, b, sizeof( b ) ) );
 }
 
 int PCF2127::read_register( char addr )
--- a/PCF2127.h	Tue Dec 09 07:21:13 2014 +0000
+++ b/PCF2127.h	Wed Dec 10 01:43:30 2014 +0000
@@ -2,8 +2,8 @@
  *  PCF2127 library
  *
  *  @author     Akifumi (Tedd) OKANO, NXP Semiconductors
- *  @version    1.6
- *  @date       09-Dec-2014
+ *  @version    1.7
+ *  @date       10-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
@@ -51,10 +51,14 @@
         Day_timestp,
         Mon_timestp,
         Year_timestp,
-        Aging_offset
+        Aging_offset,
+        RAM_addr_MSB,
+        RAM_addr_LSB,
+        RAM_wrt_cmd,
+        RAM_rd_cmd
     }
     RegisterName;
-    
+
     /** Error code */
     typedef enum {
         NO_ERROR                = 0,
@@ -63,53 +67,57 @@
         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 (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)
+     *  @param I2C_sda      I2C-bus SDA pin
+     *  @param I2C_scl      I2C-bus SCL pin
+     *  @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)
      CLKOUT_ctl
      */
     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)
+     *  @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();
 
     /** Clock integrity check
      *
-     *  @return non-zero value if the clock was stopped (means need to set the time)
+     *  @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
+     *  @param dtp  Pointer to struct tm
+     *  @return     Error code (NO_ERROR==0)
      */
     int     set_time( struct tm *dtp );
-    
+
     /** Set the time
      *
-     *  @param tp pointer to time_t
+     *  @param tp   pointer to time_t
+     *  @return     Error code (NO_ERROR==0)
      */
     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"
+     *
+     *  @param s    String data: The time information should be given in format of "YYYY MM DD HH MM SS"
+     *  @return     Error code (NO_ERROR==0)
      */
     int     set_time( char *s );
 
@@ -117,14 +125,16 @@
      *
      *  This function works similar to "time()" in standard-C-library
      *
-     *  @param tp Pointer to time_t
+     *  @param tp   Pointer to time_t
+     *  @return     Error code (NO_ERROR==0)
      */
     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.
+     *  @param s    Integer data. Converted to BCD before writing inot the register.
+     *  @return     Error code (NO_ERROR==0)
      */
     int     set_alarm( char addr, char s );
 
@@ -132,9 +142,32 @@
      *
      *  Clears interrupt flags by writing 0x00 into Control_2 register
      *
+     *  @return Error code (NO_ERROR==0)
      */
     int     clear_intr( void );
 
+    /** Writing data into RAM (for PCF2127 only)
+     *
+     *  Write data into PCF2127 internal RAM
+     *
+     *  @param address  target address of internal RAM
+     *  @param *p       pointer to write data buffer
+     *  @param size     size of writing data
+     *  @return         Error code (NO_ERROR==0)
+     */
+    int     RAM_write( int address, char *p, int size );
+
+    /** Reading data from RAM (for PCF2127 only)
+     *
+     *  Read data from PCF2127 internal RAM
+     *
+     *  @param address  target address of internal RAM
+     *  @param *p       pointer to read data buffer
+     *  @param size     size of writing data
+     *  @return         Error code (NO_ERROR==0)
+     */
+    int     RAM_read( int address, char *p, int size );
+
 private:
 
     typedef enum {
@@ -144,13 +177,14 @@
         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 );
-
+    int     set_RAM_address( char address );
+    
     I2C     *i2c_p;
     I2C     &i2c;
     char    device_address;