Ted Grosch / Mbed 2 deprecated Nucleo_TOF_I2C

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers i2cHandler.cpp Source File

i2cHandler.cpp

00001 /*
00002  * i2cHandler.cpp
00003  *
00004  *  Created on: Nov 19, 2017
00005  *    Author: a0875461
00006  */
00007 
00008 #include "i2cHandler.h"
00009 //#include "../definitions.h"
00010 
00011 #include "hostController.h "
00012 #define OPT3101_I2C_SLAVEADDRESS 0x50 // TODO fix this slave address
00013 I2C::i2cHandler::i2cHandler(void){
00014 //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL
00015     this->slaveAddress=OPT3101_I2C_SLAVEADDRESS;
00016 //#endif
00017     this->regBankSize=3;
00018     this->timeOut=0xFF;
00019     this->dataRx= new uint8_t[this->regBankSize+1];
00020     this->continousReadByteCount=3*3-1;
00021     this->continousReadRegisterAddress=0x08;
00022 }
00023 
00024 I2C::i2cHandler::~i2cHandler(void){
00025     delete [] this->dataRx;
00026     this->dataRx=NULL;
00027 }
00028 
00029 void I2C::i2cHandler::init(void){
00030     //TODO do platform initialization here
00031     // no reason to initalize mbed I2C
00032 //  this->i2cParam.selectClockSource = USCI_B_I2C_CLOCKSOURCE_SMCLK; // Clecting the SMCLK For the I2C Clocks.
00033 //  this->i2cParam.i2cClk = UCS_getSMCLK();
00034 //  this->i2cParam.dataRate = USCI_B_I2C_SET_DATA_RATE_400KBPS; // Setting to 400KBPS
00035 //  USCI_B_I2C_initMaster(USCI_B1_BASE, &this->i2cParam);
00036 
00037     //Specify slave address
00038 //  USCI_B_I2C_setSlaveAddress(USCI_B1_BASE,this->slaveAddress);
00039 //  this->slaveAddress_=this->slaveAddress;
00040 
00041     //Set Transmit mode
00042     //USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_TRANSMIT_MODE); // Setting device as I2C Master device.
00043 
00044     //Enable I2C Module to start operations
00045 //  USCI_B_I2C_enable(USCI_B1_BASE); // Enabling the I2C Module
00046 
00047 }
00048 
00049 void I2C::i2cHandler::updateSlaveAddress(){
00050     if(this->slaveAddress_!=this->slaveAddress){
00051         this->slaveAddress_=this->slaveAddress;
00052 //      USCI_B_I2C_setSlaveAddress(USCI_B1_BASE,this->slaveAddress);
00053     }
00054 }
00055 
00056 bool I2C::i2cHandler::write(){
00057     static uint32_t cnt;
00058     static uint8_t c;
00059     static bool state;
00060 
00061     this->updateSlaveAddress();
00062     state=true;
00063     //TODO this is the I2C write
00064 //    USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_TRANSMIT_MODE);
00065 
00066 //   cnt=this->timeOut;
00067 //  while((USCI_B_I2C_isBusBusy(USCI_B1_BASE)) & cnt--);
00068 //  if(!cnt)
00069 //      return false;
00070 //  state&=USCI_B_I2C_masterSendMultiByteStartWithTimeout(USCI_B1_BASE,this->address,this->timeOut); // Function returns 1 for success
00071     if(!state)
00072         return false;
00073 
00074 //  while (USCI_B_I2C_masterIsStartSent(USCI_B1_BASE));
00075 //  for(c=0;c<this->regBankSize;c++)
00076 //      state&=USCI_B_I2C_masterSendMultiByteNextWithTimeout(USCI_B1_BASE,this->dataTx[c],this->timeOut);
00077 //  state&=USCI_B_I2C_masterSendMultiByteStopWithTimeout(USCI_B1_BASE,this->timeOut); // Function returns 1 for success
00078 //  while (USCI_B_I2C_masterIsStopSent(USCI_B1_BASE));
00079 //    USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_TRANSMIT_MODE);
00080     return state;
00081 }
00082 
00083 
00084 bool I2C::i2cHandler::write(uint8_t address,uint8_t *data){
00085     this->address=address;
00086     this->dataTx=data;
00087     return this->write();
00088 }
00089 /* This is overloaded function of i2C Write. This makes the register interface simple */
00090 bool I2C::i2cHandler::write(uint8_t address,uint32_t data){
00091     this->address=address;
00092     this->dataTx=(uint8_t*) &data;
00093     return this->write();
00094 }
00095 /* This is overloaded function of i2C Write. This makes the register interface simple */
00096 bool I2C::i2cHandler::write(uint8_t *data){
00097     this->address=data[0];
00098     this->dataTx=&data[1];
00099     return this->write();
00100 }
00101 
00102 bool I2C::i2cHandler::read(){
00103     static bool state;
00104     static uint8_t c;
00105     static uint32_t cnt;
00106     this->updateSlaveAddress();
00107     state=true;
00108     //TODO put in the I2C read
00109 //  cnt=this->timeOut;
00110 //  while((USCI_B_I2C_isBusBusy(USCI_B1_BASE)) & cnt--);
00111 //  if(!cnt)
00112 //      return false;
00113 //  state&=USCI_B_I2C_masterSendSingleByteWithTimeout(USCI_B1_BASE,this->dataRx[0],this->timeOut); // Function returns 1 for success
00114 //  if(!state)
00115 //      return false;
00116 //  cnt=this->timeOut;
00117 //  while (USCI_B_I2C_masterIsStartSent(USCI_B1_BASE) & cnt--);
00118 //  if(!cnt)
00119 //      return false;
00120 //  USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_RECEIVE_MODE);
00121 //  USCI_B_I2C_masterReceiveMultiByteStart(USCI_B1_BASE);
00122 //  for(c=1;c<this->regBankSize;c++)
00123 //      this->dataRx[c]=USCI_B_I2C_masterReceiveSingle(USCI_B1_BASE);
00124 //  USCI_B_I2C_masterReceiveMultiByteStop(USCI_B1_BASE);
00125 //  this->dataRx[c]=USCI_B_I2C_masterReceiveMultiByteEndPoll(USCI_B1_BASE);
00126 //   USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_TRANSMIT_MODE);
00127     return state;
00128 }
00129 
00130 bool I2C::i2cHandler::continuousRead(uint8_t *data){
00131     static bool state;
00132     static uint8_t c;
00133 
00134     state=true;
00135     //TODO Put is the continiuos read of continouseReadByteCount bytes here
00136 //  if(USCI_B_I2C_isBusBusy(USCI_B1_BASE))
00137 //      return false;
00138 //  state&=USCI_B_I2C_masterSendSingleByteWithTimeout(USCI_B1_BASE,this->continousReadRegisterAddress,this->timeOut); // Function returns 1 for success
00139 //  while (USCI_B_I2C_masterIsStartSent(USCI_B1_BASE));
00140 //    USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_RECEIVE_MODE);
00141 //  USCI_B_I2C_masterReceiveMultiByteStart(USCI_B1_BASE);
00142 //  for(c=0;c<this->continousReadByteCount;c++)
00143 //      data[c]=USCI_B_I2C_masterReceiveSingle(USCI_B1_BASE);
00144 //    USCI_B_I2C_masterReceiveMultiByteStop(USCI_B1_BASE);
00145 //  data[c]=USCI_B_I2C_masterReceiveMultiByteEndPoll(USCI_B1_BASE);
00146 //    USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_TRANSMIT_MODE);
00147     return state;
00148 }
00149 
00150 
00151 
00152 
00153 uint8_t* I2C::i2cHandler::read(uint8_t address){
00154     this->dataRx[0]=address;
00155     this->read();
00156     return &this->dataRx[1];
00157 }
00158 
00159 bool I2C::i2cHandler::read(uint8_t address,uint8_t *data){
00160     static bool state;
00161     static uint8_t c;
00162 
00163     this->dataRx[0]=address;
00164     state=this->read();
00165     for(c=1;c<this->regBankSize+1;c++)
00166         data[c-1]=this->dataRx[c];
00167     return state;
00168 }
00169 
00170 bool I2C::i2cHandler::read(uint8_t *data){
00171     static bool state;
00172     static uint8_t c;
00173     state=true;
00174     this->dataRx[0]=data[0];
00175     state=this->read();
00176     for(c=1;c<this->regBankSize+1;c++)
00177         data[c]=this->dataRx[c];
00178     return state;
00179 }
00180 
00181 bool I2C::i2cHandler::read(uint8_t address,uint32_t *data){
00182     static bool state;
00183     static uint8_t c;
00184     static uint32_t addon;
00185     state=true;
00186 
00187     this->dataRx[0]=address;
00188     state=this->read();
00189     *data=0;
00190     for(c=0;c<this->regBankSize;c++){
00191         addon=this->dataRx[1+c];
00192         addon=addon<<(c<<3);
00193         *data+=addon;
00194     }
00195 
00196     return state;
00197 }
00198 
00199