* PCA9547 library * +++++ Based on; https://developer.mbed.org/users/okano/code/PCA9547/ +++++ * * PCA9547: an I2C bus multiplexer control library * * PCA9547 is an I2C multiplexer which enables to select 1:8 multiplexed I2C bus. * The multiplexer is useful for deviding I2C bus to avoiding slave address conflict and separating capacitive loads. * * For more information about PCA9547: * http://www.nxp.com/documents/data_sheet/PCA9547.pdf

Committer:
AkinoriHashimoto
Date:
Fri Oct 02 05:46:31 2015 +0000
Revision:
1:c388983364cc
Parent:
0:2d777d5d7e6b
resetpin adj.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 0:2d777d5d7e6b 1 /**
AkinoriHashimoto 0:2d777d5d7e6b 2 * PCA9547 library
AkinoriHashimoto 0:2d777d5d7e6b 3 * +++++ Based on; https://developer.mbed.org/users/okano/code/PCA9547/ +++++
AkinoriHashimoto 0:2d777d5d7e6b 4 *
AkinoriHashimoto 0:2d777d5d7e6b 5 * @author Tedd OKANO
AkinoriHashimoto 0:2d777d5d7e6b 6 * @version 0.2
AkinoriHashimoto 0:2d777d5d7e6b 7 * @date Feb-2015
AkinoriHashimoto 0:2d777d5d7e6b 8 *
AkinoriHashimoto 0:2d777d5d7e6b 9 * PCA9547: an I2C bus multiplexer control library
AkinoriHashimoto 0:2d777d5d7e6b 10 *
AkinoriHashimoto 0:2d777d5d7e6b 11 * PCA9547 is an I2C multiplexer which enables to select 1:8 multiplexed I2C bus.
AkinoriHashimoto 0:2d777d5d7e6b 12 * The multiplexer is useful for deviding I2C bus to avoiding slave address conflict and separating capacitive loads.
AkinoriHashimoto 0:2d777d5d7e6b 13 *
AkinoriHashimoto 0:2d777d5d7e6b 14 * For more information about PCA9547:
AkinoriHashimoto 0:2d777d5d7e6b 15 * http://www.nxp.com/documents/data_sheet/PCA9547.pdf
AkinoriHashimoto 0:2d777d5d7e6b 16 *
AkinoriHashimoto 0:2d777d5d7e6b 17 */
AkinoriHashimoto 0:2d777d5d7e6b 18
AkinoriHashimoto 0:2d777d5d7e6b 19 #include "PCA9547.h"
AkinoriHashimoto 0:2d777d5d7e6b 20
AkinoriHashimoto 0:2d777d5d7e6b 21
AkinoriHashimoto 0:2d777d5d7e6b 22
AkinoriHashimoto 0:2d777d5d7e6b 23 PCA9547::PCA9547( PinName sda, PinName scl, char i2c_address )
AkinoriHashimoto 0:2d777d5d7e6b 24 : i2c_p( new I2C( sda, scl ) ), i2c( *i2c_p ), i2c_addr( i2c_address )
AkinoriHashimoto 1:c388983364cc 25 #ifdef PCA9547_RESET
AkinoriHashimoto 1:c388983364cc 26 resetPin(PCA9547_RESET)
AkinoriHashimoto 1:c388983364cc 27 #endif
AkinoriHashimoto 0:2d777d5d7e6b 28 {
AkinoriHashimoto 0:2d777d5d7e6b 29 this->init();
AkinoriHashimoto 0:2d777d5d7e6b 30 }
AkinoriHashimoto 0:2d777d5d7e6b 31
AkinoriHashimoto 0:2d777d5d7e6b 32 PCA9547::PCA9547( I2C &i2c_, char i2c_address )
AkinoriHashimoto 0:2d777d5d7e6b 33 : i2c_p( NULL ), i2c( i2c_ ), i2c_addr( i2c_address )
AkinoriHashimoto 1:c388983364cc 34 #ifdef PCA9547_RESET
AkinoriHashimoto 1:c388983364cc 35 resetPin(PCA9547_RESET)
AkinoriHashimoto 1:c388983364cc 36 #endif
AkinoriHashimoto 0:2d777d5d7e6b 37 {
AkinoriHashimoto 0:2d777d5d7e6b 38 this->init();
AkinoriHashimoto 0:2d777d5d7e6b 39 }
AkinoriHashimoto 0:2d777d5d7e6b 40
AkinoriHashimoto 0:2d777d5d7e6b 41 void PCA9547::init()
AkinoriHashimoto 0:2d777d5d7e6b 42 {
AkinoriHashimoto 0:2d777d5d7e6b 43 #ifdef PCA9547_RESET
AkinoriHashimoto 0:2d777d5d7e6b 44 resetPin= 1;
AkinoriHashimoto 0:2d777d5d7e6b 45 #endif
AkinoriHashimoto 0:2d777d5d7e6b 46 return;
AkinoriHashimoto 0:2d777d5d7e6b 47 }
AkinoriHashimoto 0:2d777d5d7e6b 48
AkinoriHashimoto 0:2d777d5d7e6b 49 PCA9547::~PCA9547()
AkinoriHashimoto 0:2d777d5d7e6b 50 {
AkinoriHashimoto 0:2d777d5d7e6b 51 if ( NULL != i2c_p )
AkinoriHashimoto 0:2d777d5d7e6b 52 delete i2c_p;
AkinoriHashimoto 0:2d777d5d7e6b 53 }
AkinoriHashimoto 0:2d777d5d7e6b 54
AkinoriHashimoto 0:2d777d5d7e6b 55 bool PCA9547::select(int ch)
AkinoriHashimoto 0:2d777d5d7e6b 56 {
AkinoriHashimoto 0:2d777d5d7e6b 57 if(ch<0 || 7<ch)
AkinoriHashimoto 0:2d777d5d7e6b 58 return false;
AkinoriHashimoto 0:2d777d5d7e6b 59 ch += 0x08;
AkinoriHashimoto 0:2d777d5d7e6b 60 char data= ch;
AkinoriHashimoto 0:2d777d5d7e6b 61 int status= i2c.write( i2c_addr, &data, 1 );
AkinoriHashimoto 0:2d777d5d7e6b 62 // if error occurred, status isn't 0 (0:success, other:err code).
AkinoriHashimoto 0:2d777d5d7e6b 63 // in C++, 0: false, other: true.
AkinoriHashimoto 0:2d777d5d7e6b 64 if(status == 0)
AkinoriHashimoto 0:2d777d5d7e6b 65 return true;
AkinoriHashimoto 0:2d777d5d7e6b 66
AkinoriHashimoto 0:2d777d5d7e6b 67 #ifdef PCA9547_RESET
AkinoriHashimoto 0:2d777d5d7e6b 68 resetPin= 0;
AkinoriHashimoto 0:2d777d5d7e6b 69 wait_ms(1);
AkinoriHashimoto 0:2d777d5d7e6b 70 resetPin= 1;
AkinoriHashimoto 0:2d777d5d7e6b 71 wait_ms(1);
AkinoriHashimoto 0:2d777d5d7e6b 72 status= i2c.write( i2c_addr, &data, 1 );
AkinoriHashimoto 0:2d777d5d7e6b 73 #endif
AkinoriHashimoto 0:2d777d5d7e6b 74 return false;
AkinoriHashimoto 0:2d777d5d7e6b 75 }
AkinoriHashimoto 0:2d777d5d7e6b 76
AkinoriHashimoto 0:2d777d5d7e6b 77 void PCA9547::disable( void )
AkinoriHashimoto 0:2d777d5d7e6b 78 {
AkinoriHashimoto 0:2d777d5d7e6b 79 char data= 0x00;
AkinoriHashimoto 0:2d777d5d7e6b 80 i2c.write( i2c_addr, &data, 1 );
AkinoriHashimoto 0:2d777d5d7e6b 81 }
AkinoriHashimoto 0:2d777d5d7e6b 82
AkinoriHashimoto 0:2d777d5d7e6b 83 // EOF