* 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:37:18 2015 +0000
Revision:
0:2d777d5d7e6b
Child:
1:c388983364cc
1st publish.

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 0:2d777d5d7e6b 25 {
AkinoriHashimoto 0:2d777d5d7e6b 26 this->init();
AkinoriHashimoto 0:2d777d5d7e6b 27 }
AkinoriHashimoto 0:2d777d5d7e6b 28
AkinoriHashimoto 0:2d777d5d7e6b 29 PCA9547::PCA9547( I2C &i2c_, char i2c_address )
AkinoriHashimoto 0:2d777d5d7e6b 30 : i2c_p( NULL ), i2c( i2c_ ), i2c_addr( i2c_address )
AkinoriHashimoto 0:2d777d5d7e6b 31 {
AkinoriHashimoto 0:2d777d5d7e6b 32 this->init();
AkinoriHashimoto 0:2d777d5d7e6b 33 }
AkinoriHashimoto 0:2d777d5d7e6b 34
AkinoriHashimoto 0:2d777d5d7e6b 35 void PCA9547::init()
AkinoriHashimoto 0:2d777d5d7e6b 36 {
AkinoriHashimoto 0:2d777d5d7e6b 37 #ifdef PCA9547_RESET
AkinoriHashimoto 0:2d777d5d7e6b 38 resetPin= 1;
AkinoriHashimoto 0:2d777d5d7e6b 39 #endif
AkinoriHashimoto 0:2d777d5d7e6b 40 return;
AkinoriHashimoto 0:2d777d5d7e6b 41 }
AkinoriHashimoto 0:2d777d5d7e6b 42
AkinoriHashimoto 0:2d777d5d7e6b 43 PCA9547::~PCA9547()
AkinoriHashimoto 0:2d777d5d7e6b 44 {
AkinoriHashimoto 0:2d777d5d7e6b 45 if ( NULL != i2c_p )
AkinoriHashimoto 0:2d777d5d7e6b 46 delete i2c_p;
AkinoriHashimoto 0:2d777d5d7e6b 47 }
AkinoriHashimoto 0:2d777d5d7e6b 48
AkinoriHashimoto 0:2d777d5d7e6b 49 bool PCA9547::select(int ch)
AkinoriHashimoto 0:2d777d5d7e6b 50 {
AkinoriHashimoto 0:2d777d5d7e6b 51 if(ch<0 || 7<ch)
AkinoriHashimoto 0:2d777d5d7e6b 52 return false;
AkinoriHashimoto 0:2d777d5d7e6b 53 ch += 0x08;
AkinoriHashimoto 0:2d777d5d7e6b 54 char data= ch;
AkinoriHashimoto 0:2d777d5d7e6b 55 int status= i2c.write( i2c_addr, &data, 1 );
AkinoriHashimoto 0:2d777d5d7e6b 56 // if error occurred, status isn't 0 (0:success, other:err code).
AkinoriHashimoto 0:2d777d5d7e6b 57 // in C++, 0: false, other: true.
AkinoriHashimoto 0:2d777d5d7e6b 58 if(status == 0)
AkinoriHashimoto 0:2d777d5d7e6b 59 return true;
AkinoriHashimoto 0:2d777d5d7e6b 60
AkinoriHashimoto 0:2d777d5d7e6b 61 #ifdef PCA9547_RESET
AkinoriHashimoto 0:2d777d5d7e6b 62 resetPin= 0;
AkinoriHashimoto 0:2d777d5d7e6b 63 wait_ms(1);
AkinoriHashimoto 0:2d777d5d7e6b 64 resetPin= 1;
AkinoriHashimoto 0:2d777d5d7e6b 65 wait_ms(1);
AkinoriHashimoto 0:2d777d5d7e6b 66 status= i2c.write( i2c_addr, &data, 1 );
AkinoriHashimoto 0:2d777d5d7e6b 67 #endif
AkinoriHashimoto 0:2d777d5d7e6b 68 return false;
AkinoriHashimoto 0:2d777d5d7e6b 69 }
AkinoriHashimoto 0:2d777d5d7e6b 70
AkinoriHashimoto 0:2d777d5d7e6b 71 void PCA9547::disable( void )
AkinoriHashimoto 0:2d777d5d7e6b 72 {
AkinoriHashimoto 0:2d777d5d7e6b 73 char data= 0x00;
AkinoriHashimoto 0:2d777d5d7e6b 74 i2c.write( i2c_addr, &data, 1 );
AkinoriHashimoto 0:2d777d5d7e6b 75 }
AkinoriHashimoto 0:2d777d5d7e6b 76
AkinoriHashimoto 0:2d777d5d7e6b 77 // EOF