Mini board PCU9669 evaluation kit library

Dependents:   mini_board_PCU9669

Files at this revision

API Documentation at this revision

Comitter:
nxp_ip
Date:
Wed Jan 14 04:30:37 2015 +0000
Parent:
3:930ef1a6bc99
Commit message:
PCA9665 error handling improved

Changed in this revision

PCA9665_access.c Show annotated file Show diff for this revision Revisions of this file
PCA9665_access.h Show annotated file Show diff for this revision Revisions of this file
diff -r 930ef1a6bc99 -r b69135913a8f PCA9665_access.c
--- a/PCA9665_access.c	Wed Jul 11 11:18:52 2012 +0000
+++ b/PCA9665_access.c	Wed Jan 14 04:30:37 2015 +0000
@@ -1,8 +1,8 @@
 /** A sample code for "mini board PCU9669/PCA9665"
  *
  *  @author  Akifumi (Tedd) OKANO, NXP Semiconductors
- *  @version 1.1
- *  @date    11-Jul-2012
+ *  @version 1.2
+ *  @date    13-Jan-2015
  *
  *  Released under the MIT License: http://mbed.org/license/mit
  *
@@ -21,10 +21,10 @@
  *    Debug and code adjustment has been done on 08-Sep-2011.
  *    Small sanitization for main.cpp. All mbed related codes are moved in to "hardware_abs.*". 13-Oct-2011
  *    hardware_abs are moved into parallel_bus library folder, 3 LED driver operation sample 13-Feb.-2012
- *    PCU9669 and PCA9665 codes are packed in a project 14-Feb-2012. 
- *    
+ *    PCU9669 and PCA9665 codes are packed in a project 14-Feb-2012.
+ *
  *    Before builidng the code, please edit the file mini_board_PCU9669/config.h
- *    Un-comment the target name what you want to target. 
+ *    Un-comment the target name what you want to target.
  */
 
 #include "PCA9665_access.h"
@@ -105,11 +105,13 @@
 char    int_happened        = 0;
 char    op_mode_flag        = OP_MODE_MASTER_ONLY;
 
-void interrupt_handler_PCA9665( void ) {
+void interrupt_handler_PCA9665( void )
+{
     int_happened    = 1;
 }
 
-void PCA9665_init( void ) {
+void PCA9665_init( void )
+{
     write_data( I2CCON, 0x40 );
     hw_wait_us( 1000 );
 
@@ -118,17 +120,26 @@
     //  initialize PCA9955 registers
 }
 
-void set_speed_mode( int mode ) {
+void parallel_software_reset( void )
+{
+    indirect_write( I2CPRESET, 0xA5 );
+    indirect_write( I2CPRESET, 0x5A );
+}
+
+void set_speed_mode( int mode )
+{
     indirect_write( I2CMODE, speed_mode[ mode ].i2cmode );
     indirect_write( I2CSCLL, speed_mode[ mode ].i2cscll );
     indirect_write( I2CSCLH, speed_mode[ mode ].i2csclh );
 }
 
-void set_buffer_mode( int mode ) {
+void set_buffer_mode( int mode )
+{
     buffer_mode_enable  = mode;
 }
 
-int i2c_write( char addr, char *dp, char length, char restart_flag ) {
+int i2c_write( char addr, char *dp, char length, char restart_flag )
+{
     return (
                buffer_mode_enable ?
                i2c_write_buffer_mode( addr, dp, length, restart_flag )
@@ -137,7 +148,8 @@
            );
 }
 
-int i2c_read( char addr, char *dp, char length, char restart_flag ) {
+int i2c_read( char addr, char *dp, char length, char restart_flag )
+{
     return (
                buffer_mode_enable ?
                i2c_read_buffer_mode( addr, dp, length, restart_flag )
@@ -147,7 +159,8 @@
 }
 
 
-int i2c_write_buffer_mode( char addr, char *dp, char length, char restart_flag ) {
+int i2c_write_buffer_mode( char addr, char *dp, char length, char restart_flag )
+{
     int     done            = BUS_CONTINUE;
     char    state;
     char    return_value    = 0xFF;
@@ -166,20 +179,19 @@
             int_happened    = 0;
 
             state   = read_data( I2CSTA );
+//printf( "STA = 0x%02X\r\n", state );
 
             switch ( state ) {
                 case MASTER_START_TXed   :
                 case MASTER_RESTART_TXed :
                     indirect_write( I2CCOUNT, length + 1 );
                     write_data( I2CDAT, addr & 0xFE );
-
 #ifdef  PCA9665_BURST_DATA_ACCESS
                     write_data_burst( I2CDAT, dp, length );
 #else
                     for ( i = 0; i < length; i++ )
                         write_data( I2CDAT, *dp++ );
 #endif
-
                     write_data( I2CCON, 0x41 );
                     break;
                 case MASTER_SLA_W_ACK  :  //  SLA+W TXed
@@ -192,6 +204,12 @@
                     return_value    = 0x01;
                     done            = BUS_STOP;
                     break;
+                case ILLEGAL_START_STOP :
+                case ILLEGAL_I2CCOUNT :
+                case SCL_STUCK :
+                    //  NOTE: In case of SCL_STUCK, the slave device may need to be reset
+                    parallel_software_reset();
+                    return ( 0xEE );
                 case MASTER_ARB_LOST :
                 case SLAVE_AL_ADDRESSED_R :
                 case SLAVE_AL_ADDRESSED_W :
@@ -205,8 +223,10 @@
         }
     }
 
+#if 0
     if ( OP_MODE_MASTER_ONLY == op_mode_flag )
         done    = BUS_STOP;
+#endif
 
     if ( (BUS_STOP == done) && !restart_flag )
         write_data( I2CCON, 0x50 );
@@ -214,7 +234,9 @@
     return ( return_value );
 }
 
-int i2c_read_buffer_mode( char addr, char *dp, char length, char restart_flag ) {
+
+int i2c_read_buffer_mode( char addr, char *dp, char length, char restart_flag )
+{
     int     done    = BUS_CONTINUE;
     char    state;
     char    return_value    = 0xFF;
@@ -259,6 +281,12 @@
                     done    = BUS_STOP;
 
                     break;
+                case ILLEGAL_START_STOP :
+                case ILLEGAL_I2CCOUNT :
+                case SCL_STUCK :
+                    //  NOTE: In case of SCL_STUCK, the slave device may need to be reset
+                    parallel_software_reset();
+                    return ( 0xEE );
                 case MASTER_ARB_LOST :
                 case SLAVE_AL_ADDRESSED_R :
                 case SLAVE_AL_ADDRESSED_W :
@@ -279,8 +307,10 @@
         *dp++   = read_data( I2CDAT );
 #endif
 
+#if 0
     if ( OP_MODE_MASTER_ONLY == op_mode_flag )
         done    = BUS_STOP;
+#endif
 
     if ( (BUS_STOP == done) && !restart_flag )
         write_data( I2CCON, 0x50 );
@@ -288,7 +318,8 @@
     return ( return_value );
 }
 
-int i2c_write_byte_mode( char addr, char *dp, char length, char restart_flag ) {
+int i2c_write_byte_mode( char addr, char *dp, char length, char restart_flag )
+{
     int     done            = BUS_CONTINUE;
     char    state;
 
@@ -343,7 +374,8 @@
     return ( length );
 }
 
-int i2c_read_byte_mode( char addr, char *dp, char length, char restart_flag ) {
+int i2c_read_byte_mode( char addr, char *dp, char length, char restart_flag )
+{
     int     done    = BUS_CONTINUE;
     char    state;
 
@@ -403,12 +435,14 @@
     return ( length );
 }
 
-void indirect_write( char idaddr, char data ) {
+void indirect_write( char idaddr, char data )
+{
     write_data( INDPTR, idaddr );
     write_data( INDIRECT, data );
 }
 
-char indirect_read( char idaddr ) {
+char indirect_read( char idaddr )
+{
     write_data( INDPTR, idaddr );
     return ( read_data( INDIRECT ) );
 }
diff -r 930ef1a6bc99 -r b69135913a8f PCA9665_access.h
--- a/PCA9665_access.h	Wed Jul 11 11:18:52 2012 +0000
+++ b/PCA9665_access.h	Wed Jan 14 04:30:37 2015 +0000
@@ -1,8 +1,8 @@
 /** A sample code for "mini board PCU9669/PCA9665"
  *
  *  @author  Akifumi (Tedd) OKANO, NXP Semiconductors
- *  @version 1.1
- *  @date    11-Jul-2012
+ *  @version 1.2
+ *  @date    13-Jan-2015
  *
  *  Released under the MIT License: http://mbed.org/license/mit
  *
@@ -21,10 +21,10 @@
  *    Debug and code adjustment has been done on 08-Sep-2011.
  *    Small sanitization for main.cpp. All mbed related codes are moved in to "hardware_abs.*". 13-Oct-2011
  *    hardware_abs are moved into parallel_bus library folder, 3 LED driver operation sample 13-Feb.-2012
- *    PCU9669 and PCA9665 codes are packed in a project 14-Feb-2012. 
- *    
+ *    PCU9669 and PCA9665 codes are packed in a project 14-Feb-2012.
+ *
  *    Before builidng the code, please edit the file mini_board_PCU9669/config.h
- *    Un-comment the target name what you want to target. 
+ *    Un-comment the target name what you want to target.
  */
 
 #ifndef MINIBOARD_PCA9665_ACCESS__