Charith Dassanayake / CPPToPigpio
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2C.cc Source File

I2C.cc

00001 //I2C.cc
00002 #include <stdio.h>
00003 namespace CPPToPigpio {
00004     I2C::I2C(PinName sdapin, PinName sclpin) : _pinsda(sdapin), _pinscl(sclpin)
00005     {
00006         //note: cannot mix and match buses
00007         CPPToPigpio::_handlePigpioInitialisationIfNeeded();
00008         if (_pinsda==p0 && _pinscl==p1) {
00009             _I2CBus = 0;
00010         }
00011         else if (_pinsda==p2 && _pinscl==p3){ 
00012             _I2CBus = 1;
00013         }
00014         else{
00015             perror("I2C0: sda-> p0, scl-> p1\nI2C1: sda-> p2, scl-> p3\n");
00016             _pinsda=_pinscl=NC;
00017             _I2CBus=-1;
00018         }
00019         gpioSetMode(static_cast<int>(_pinsda), PI_ALT0);
00020         gpioSetMode(static_cast<int>(_pinscl), PI_ALT0);
00021         _repeated = false;
00022         _bbstarted =false;
00023         _baud = 50000;
00024         
00025     }
00026     I2C::I2C(int sdapin, int sclpin) : I2C(static_cast<PinName>(sdapin), static_cast<PinName>(sclpin))
00027     {
00028         
00029     }
00030     I2C::~I2C(){
00031         CPPToPigpio::_handlePigpioTermination();
00032     }
00033     void I2C::frequency( int hz){
00034         _baud=hz;
00035     } 
00036     
00037     int I2C::read(unsigned address, char* buf, int length, bool repeated=false){
00038         _repeated = repeated;
00039         _handle = i2cOpen(_I2CBus, address, 0); //returns >=0
00040         int ret=i2cReadDevice(_handle, buf, length);
00041         i2cClose(_handle);
00042         if (_repeated) i2cSwitchCombined(1); 
00043         else i2cSwitchCombined(0);//turn off repeated start
00044         return !ret;
00045         
00046     }
00047     int I2C::read(__attribute__((unused)) int ack){
00048         int ret;
00049         if (_bbstarted){
00050             char cmdBuf[2] = { Read, 0x02}; //no stop
00051             char out[2];
00052             bbI2CZip(_pinsda, cmdBuf, 2, out, 2);
00053             ret=out[0];
00054             
00055         }
00056         else{
00057             _handle = i2cOpen(_I2CBus, 0, 0);
00058             ret= i2cReadByte(_handle); 
00059             i2cClose(_handle);
00060     }
00061         return ret;
00062     }
00063     int I2C::write(unsigned address, char* buf, int length, bool repeated = false){
00064         _repeated=repeated;
00065         int ret; 
00066         char tmp[length+1]={0};
00067         unsigned reg;
00068         if (_repeated) i2cSwitchCombined(1); //write->read to same addr => repeated start
00069         else i2cSwitchCombined(0);
00070         _handle = i2cOpen(_I2CBus, address, 0);
00071         if (length>1){
00072             reg = buf[0];
00073             for (int i=0; i<length; i++){
00074                 tmp[i+1]=buf[i];
00075             }
00076             ret = i2cWriteBlockData(_handle, reg, tmp, length);
00077         }
00078         else{
00079             ret = i2cWriteDevice(_handle, buf, length);
00080         }
00081         i2cClose(_handle);
00082         return !ret;
00083     }
00084     int I2C::write(int data){
00085         int ret;
00086         if (_bbstarted ){
00087             char tmp = (unsigned)data;
00088             char cmdBuf[3] = { Write,0x01, tmp}; //no stop
00089             char out[1];
00090             ret=bbI2CZip(_pinsda, cmdBuf, 3, out, 1);
00091         }
00092         
00093         else{
00094             _handle = i2cOpen(_I2CBus, 0, 0);
00095             ret = i2cWriteByte(_handle, data);
00096             i2cClose(_handle);
00097         }
00098         if (ret>=0) return 1;
00099         else return ret;
00100         
00101     }
00102     
00103     void I2C::start(void){
00104         if (!_bbstarted){
00105             bbI2COpen(_pinsda, _pinscl, _baud);
00106         }
00107         char cmdBuf[1] = {Start};
00108         char out[1];
00109         bbI2CZip(_pinsda, cmdBuf, 1, out, 1);
00110         _bbstarted = true;
00111         
00112     }
00113     void I2C::stop(void){
00114         if (_bbstarted) {
00115             char cmdBuf[1] = {Stop};
00116             char out[1];
00117             bbI2CZip(_pinsda, cmdBuf, 1, out, 1);
00118             bbI2CClose(_pinsda);
00119             _bbstarted = false;
00120             }
00121         
00122     }
00123     
00124     
00125     int I2C::bang(char* inBuf, unsigned inLen, char* outBuf, unsigned outLen, bool cont=false){
00126         if(!_bbstarted) {
00127             bbI2COpen(_pinsda, _pinscl, _baud);
00128             _bbstarted=true;}
00129         int ret=bbI2CZip(_pinsda, inBuf, inLen, outBuf, outLen);
00130         if (!cont) {
00131             bbI2CClose(_pinsda);
00132             _bbstarted=false;
00133         }
00134         if (ret>=0) return 1;
00135         else return ret;
00136     
00137         
00138     }
00139     
00140     
00141 }
00142