Ted Grosch
/
Nucleo_TOF_I2C
Export to MBED Studio
i2cHandler.cpp@0:62b846b3988a, 2020-10-25 (annotated)
- Committer:
- tgrosch
- Date:
- Sun Oct 25 00:58:24 2020 +0000
- Revision:
- 0:62b846b3988a
- Child:
- 1:8567a29d4699
First successful compile.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tgrosch | 0:62b846b3988a | 1 | /* |
tgrosch | 0:62b846b3988a | 2 | * i2cHandler.cpp |
tgrosch | 0:62b846b3988a | 3 | * |
tgrosch | 0:62b846b3988a | 4 | * Created on: Nov 19, 2017 |
tgrosch | 0:62b846b3988a | 5 | * Author: a0875461 |
tgrosch | 0:62b846b3988a | 6 | */ |
tgrosch | 0:62b846b3988a | 7 | |
tgrosch | 0:62b846b3988a | 8 | #include "i2cHandler.h" |
tgrosch | 0:62b846b3988a | 9 | //#include "../definitions.h" |
tgrosch | 0:62b846b3988a | 10 | |
tgrosch | 0:62b846b3988a | 11 | #include "hostController.h" |
tgrosch | 0:62b846b3988a | 12 | #define OPT3101_I2C_SLAVEADDRESS 0x50 // TODO fix this slave address |
tgrosch | 0:62b846b3988a | 13 | I2C::i2cHandler::i2cHandler(void){ |
tgrosch | 0:62b846b3988a | 14 | //#ifdef TIMSP430F5529_LAUNCHPAD_CALIBRATION_TOOL |
tgrosch | 0:62b846b3988a | 15 | this->slaveAddress=OPT3101_I2C_SLAVEADDRESS; |
tgrosch | 0:62b846b3988a | 16 | //#endif |
tgrosch | 0:62b846b3988a | 17 | this->regBankSize=3; |
tgrosch | 0:62b846b3988a | 18 | this->timeOut=0xFF; |
tgrosch | 0:62b846b3988a | 19 | this->dataRx= new uint8_t[this->regBankSize+1]; |
tgrosch | 0:62b846b3988a | 20 | this->continousReadByteCount=3*3-1; |
tgrosch | 0:62b846b3988a | 21 | this->continousReadRegisterAddress=0x08; |
tgrosch | 0:62b846b3988a | 22 | } |
tgrosch | 0:62b846b3988a | 23 | |
tgrosch | 0:62b846b3988a | 24 | I2C::i2cHandler::~i2cHandler(void){ |
tgrosch | 0:62b846b3988a | 25 | delete [] this->dataRx; |
tgrosch | 0:62b846b3988a | 26 | this->dataRx=NULL; |
tgrosch | 0:62b846b3988a | 27 | } |
tgrosch | 0:62b846b3988a | 28 | |
tgrosch | 0:62b846b3988a | 29 | void I2C::i2cHandler::init(void){ |
tgrosch | 0:62b846b3988a | 30 | // no reason to initalize mbed I2C |
tgrosch | 0:62b846b3988a | 31 | // this->i2cParam.selectClockSource = USCI_B_I2C_CLOCKSOURCE_SMCLK; // Clecting the SMCLK For the I2C Clocks. |
tgrosch | 0:62b846b3988a | 32 | // this->i2cParam.i2cClk = UCS_getSMCLK(); |
tgrosch | 0:62b846b3988a | 33 | // this->i2cParam.dataRate = USCI_B_I2C_SET_DATA_RATE_400KBPS; // Setting to 400KBPS |
tgrosch | 0:62b846b3988a | 34 | // USCI_B_I2C_initMaster(USCI_B1_BASE, &this->i2cParam); |
tgrosch | 0:62b846b3988a | 35 | |
tgrosch | 0:62b846b3988a | 36 | //Specify slave address |
tgrosch | 0:62b846b3988a | 37 | // USCI_B_I2C_setSlaveAddress(USCI_B1_BASE,this->slaveAddress); |
tgrosch | 0:62b846b3988a | 38 | // this->slaveAddress_=this->slaveAddress; |
tgrosch | 0:62b846b3988a | 39 | |
tgrosch | 0:62b846b3988a | 40 | //Set Transmit mode |
tgrosch | 0:62b846b3988a | 41 | //USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_TRANSMIT_MODE); // Setting device as I2C Master device. |
tgrosch | 0:62b846b3988a | 42 | |
tgrosch | 0:62b846b3988a | 43 | //Enable I2C Module to start operations |
tgrosch | 0:62b846b3988a | 44 | // USCI_B_I2C_enable(USCI_B1_BASE); // Enabling the I2C Module |
tgrosch | 0:62b846b3988a | 45 | |
tgrosch | 0:62b846b3988a | 46 | } |
tgrosch | 0:62b846b3988a | 47 | |
tgrosch | 0:62b846b3988a | 48 | void I2C::i2cHandler::updateSlaveAddress(){ |
tgrosch | 0:62b846b3988a | 49 | if(this->slaveAddress_!=this->slaveAddress){ |
tgrosch | 0:62b846b3988a | 50 | this->slaveAddress_=this->slaveAddress; |
tgrosch | 0:62b846b3988a | 51 | // USCI_B_I2C_setSlaveAddress(USCI_B1_BASE,this->slaveAddress); |
tgrosch | 0:62b846b3988a | 52 | } |
tgrosch | 0:62b846b3988a | 53 | } |
tgrosch | 0:62b846b3988a | 54 | |
tgrosch | 0:62b846b3988a | 55 | bool I2C::i2cHandler::write(){ |
tgrosch | 0:62b846b3988a | 56 | static uint32_t cnt; |
tgrosch | 0:62b846b3988a | 57 | static uint8_t c; |
tgrosch | 0:62b846b3988a | 58 | static bool state; |
tgrosch | 0:62b846b3988a | 59 | |
tgrosch | 0:62b846b3988a | 60 | this->updateSlaveAddress(); |
tgrosch | 0:62b846b3988a | 61 | state=true; |
tgrosch | 0:62b846b3988a | 62 | // USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_TRANSMIT_MODE); |
tgrosch | 0:62b846b3988a | 63 | |
tgrosch | 0:62b846b3988a | 64 | // cnt=this->timeOut; |
tgrosch | 0:62b846b3988a | 65 | // while((USCI_B_I2C_isBusBusy(USCI_B1_BASE)) & cnt--); |
tgrosch | 0:62b846b3988a | 66 | // if(!cnt) |
tgrosch | 0:62b846b3988a | 67 | // return false; |
tgrosch | 0:62b846b3988a | 68 | // state&=USCI_B_I2C_masterSendMultiByteStartWithTimeout(USCI_B1_BASE,this->address,this->timeOut); // Function returns 1 for success |
tgrosch | 0:62b846b3988a | 69 | if(!state) |
tgrosch | 0:62b846b3988a | 70 | return false; |
tgrosch | 0:62b846b3988a | 71 | |
tgrosch | 0:62b846b3988a | 72 | // while (USCI_B_I2C_masterIsStartSent(USCI_B1_BASE)); |
tgrosch | 0:62b846b3988a | 73 | // for(c=0;c<this->regBankSize;c++) |
tgrosch | 0:62b846b3988a | 74 | // state&=USCI_B_I2C_masterSendMultiByteNextWithTimeout(USCI_B1_BASE,this->dataTx[c],this->timeOut); |
tgrosch | 0:62b846b3988a | 75 | // state&=USCI_B_I2C_masterSendMultiByteStopWithTimeout(USCI_B1_BASE,this->timeOut); // Function returns 1 for success |
tgrosch | 0:62b846b3988a | 76 | // while (USCI_B_I2C_masterIsStopSent(USCI_B1_BASE)); |
tgrosch | 0:62b846b3988a | 77 | // USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_TRANSMIT_MODE); |
tgrosch | 0:62b846b3988a | 78 | // return state; |
tgrosch | 0:62b846b3988a | 79 | } |
tgrosch | 0:62b846b3988a | 80 | |
tgrosch | 0:62b846b3988a | 81 | |
tgrosch | 0:62b846b3988a | 82 | bool I2C::i2cHandler::write(uint8_t address,uint8_t *data){ |
tgrosch | 0:62b846b3988a | 83 | this->address=address; |
tgrosch | 0:62b846b3988a | 84 | this->dataTx=data; |
tgrosch | 0:62b846b3988a | 85 | return this->write(); |
tgrosch | 0:62b846b3988a | 86 | } |
tgrosch | 0:62b846b3988a | 87 | /* This is overloaded function of i2C Write. This makes the register interface simple */ |
tgrosch | 0:62b846b3988a | 88 | bool I2C::i2cHandler::write(uint8_t address,uint32_t data){ |
tgrosch | 0:62b846b3988a | 89 | this->address=address; |
tgrosch | 0:62b846b3988a | 90 | this->dataTx=(uint8_t*) &data; |
tgrosch | 0:62b846b3988a | 91 | return this->write(); |
tgrosch | 0:62b846b3988a | 92 | } |
tgrosch | 0:62b846b3988a | 93 | /* This is overloaded function of i2C Write. This makes the register interface simple */ |
tgrosch | 0:62b846b3988a | 94 | bool I2C::i2cHandler::write(uint8_t *data){ |
tgrosch | 0:62b846b3988a | 95 | this->address=data[0]; |
tgrosch | 0:62b846b3988a | 96 | this->dataTx=&data[1]; |
tgrosch | 0:62b846b3988a | 97 | return this->write(); |
tgrosch | 0:62b846b3988a | 98 | } |
tgrosch | 0:62b846b3988a | 99 | |
tgrosch | 0:62b846b3988a | 100 | bool I2C::i2cHandler::read(){ |
tgrosch | 0:62b846b3988a | 101 | static bool state; |
tgrosch | 0:62b846b3988a | 102 | static uint8_t c; |
tgrosch | 0:62b846b3988a | 103 | static uint32_t cnt; |
tgrosch | 0:62b846b3988a | 104 | this->updateSlaveAddress(); |
tgrosch | 0:62b846b3988a | 105 | state=true; |
tgrosch | 0:62b846b3988a | 106 | // cnt=this->timeOut; |
tgrosch | 0:62b846b3988a | 107 | // while((USCI_B_I2C_isBusBusy(USCI_B1_BASE)) & cnt--); |
tgrosch | 0:62b846b3988a | 108 | // if(!cnt) |
tgrosch | 0:62b846b3988a | 109 | // return false; |
tgrosch | 0:62b846b3988a | 110 | // state&=USCI_B_I2C_masterSendSingleByteWithTimeout(USCI_B1_BASE,this->dataRx[0],this->timeOut); // Function returns 1 for success |
tgrosch | 0:62b846b3988a | 111 | // if(!state) |
tgrosch | 0:62b846b3988a | 112 | // return false; |
tgrosch | 0:62b846b3988a | 113 | // cnt=this->timeOut; |
tgrosch | 0:62b846b3988a | 114 | // while (USCI_B_I2C_masterIsStartSent(USCI_B1_BASE) & cnt--); |
tgrosch | 0:62b846b3988a | 115 | // if(!cnt) |
tgrosch | 0:62b846b3988a | 116 | // return false; |
tgrosch | 0:62b846b3988a | 117 | // USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_RECEIVE_MODE); |
tgrosch | 0:62b846b3988a | 118 | // USCI_B_I2C_masterReceiveMultiByteStart(USCI_B1_BASE); |
tgrosch | 0:62b846b3988a | 119 | // for(c=1;c<this->regBankSize;c++) |
tgrosch | 0:62b846b3988a | 120 | // this->dataRx[c]=USCI_B_I2C_masterReceiveSingle(USCI_B1_BASE); |
tgrosch | 0:62b846b3988a | 121 | // USCI_B_I2C_masterReceiveMultiByteStop(USCI_B1_BASE); |
tgrosch | 0:62b846b3988a | 122 | // this->dataRx[c]=USCI_B_I2C_masterReceiveMultiByteEndPoll(USCI_B1_BASE); |
tgrosch | 0:62b846b3988a | 123 | // USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_TRANSMIT_MODE); |
tgrosch | 0:62b846b3988a | 124 | return state; |
tgrosch | 0:62b846b3988a | 125 | } |
tgrosch | 0:62b846b3988a | 126 | |
tgrosch | 0:62b846b3988a | 127 | bool I2C::i2cHandler::continuousRead(uint8_t *data){ |
tgrosch | 0:62b846b3988a | 128 | static bool state; |
tgrosch | 0:62b846b3988a | 129 | static uint8_t c; |
tgrosch | 0:62b846b3988a | 130 | |
tgrosch | 0:62b846b3988a | 131 | state=true; |
tgrosch | 0:62b846b3988a | 132 | // if(USCI_B_I2C_isBusBusy(USCI_B1_BASE)) |
tgrosch | 0:62b846b3988a | 133 | // return false; |
tgrosch | 0:62b846b3988a | 134 | // state&=USCI_B_I2C_masterSendSingleByteWithTimeout(USCI_B1_BASE,this->continousReadRegisterAddress,this->timeOut); // Function returns 1 for success |
tgrosch | 0:62b846b3988a | 135 | // while (USCI_B_I2C_masterIsStartSent(USCI_B1_BASE)); |
tgrosch | 0:62b846b3988a | 136 | // USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_RECEIVE_MODE); |
tgrosch | 0:62b846b3988a | 137 | // USCI_B_I2C_masterReceiveMultiByteStart(USCI_B1_BASE); |
tgrosch | 0:62b846b3988a | 138 | // for(c=0;c<this->continousReadByteCount;c++) |
tgrosch | 0:62b846b3988a | 139 | // data[c]=USCI_B_I2C_masterReceiveSingle(USCI_B1_BASE); |
tgrosch | 0:62b846b3988a | 140 | // USCI_B_I2C_masterReceiveMultiByteStop(USCI_B1_BASE); |
tgrosch | 0:62b846b3988a | 141 | // data[c]=USCI_B_I2C_masterReceiveMultiByteEndPoll(USCI_B1_BASE); |
tgrosch | 0:62b846b3988a | 142 | // USCI_B_I2C_setMode(USCI_B1_BASE,USCI_B_I2C_TRANSMIT_MODE); |
tgrosch | 0:62b846b3988a | 143 | return state; |
tgrosch | 0:62b846b3988a | 144 | } |
tgrosch | 0:62b846b3988a | 145 | |
tgrosch | 0:62b846b3988a | 146 | |
tgrosch | 0:62b846b3988a | 147 | |
tgrosch | 0:62b846b3988a | 148 | |
tgrosch | 0:62b846b3988a | 149 | uint8_t* I2C::i2cHandler::read(uint8_t address){ |
tgrosch | 0:62b846b3988a | 150 | this->dataRx[0]=address; |
tgrosch | 0:62b846b3988a | 151 | this->read(); |
tgrosch | 0:62b846b3988a | 152 | return &this->dataRx[1]; |
tgrosch | 0:62b846b3988a | 153 | } |
tgrosch | 0:62b846b3988a | 154 | |
tgrosch | 0:62b846b3988a | 155 | bool I2C::i2cHandler::read(uint8_t address,uint8_t *data){ |
tgrosch | 0:62b846b3988a | 156 | static bool state; |
tgrosch | 0:62b846b3988a | 157 | static uint8_t c; |
tgrosch | 0:62b846b3988a | 158 | |
tgrosch | 0:62b846b3988a | 159 | this->dataRx[0]=address; |
tgrosch | 0:62b846b3988a | 160 | state=this->read(); |
tgrosch | 0:62b846b3988a | 161 | for(c=1;c<this->regBankSize+1;c++) |
tgrosch | 0:62b846b3988a | 162 | data[c-1]=this->dataRx[c]; |
tgrosch | 0:62b846b3988a | 163 | return state; |
tgrosch | 0:62b846b3988a | 164 | } |
tgrosch | 0:62b846b3988a | 165 | |
tgrosch | 0:62b846b3988a | 166 | bool I2C::i2cHandler::read(uint8_t *data){ |
tgrosch | 0:62b846b3988a | 167 | static bool state; |
tgrosch | 0:62b846b3988a | 168 | static uint8_t c; |
tgrosch | 0:62b846b3988a | 169 | state=true; |
tgrosch | 0:62b846b3988a | 170 | this->dataRx[0]=data[0]; |
tgrosch | 0:62b846b3988a | 171 | state=this->read(); |
tgrosch | 0:62b846b3988a | 172 | for(c=1;c<this->regBankSize+1;c++) |
tgrosch | 0:62b846b3988a | 173 | data[c]=this->dataRx[c]; |
tgrosch | 0:62b846b3988a | 174 | return state; |
tgrosch | 0:62b846b3988a | 175 | } |
tgrosch | 0:62b846b3988a | 176 | |
tgrosch | 0:62b846b3988a | 177 | bool I2C::i2cHandler::read(uint8_t address,uint32_t *data){ |
tgrosch | 0:62b846b3988a | 178 | static bool state; |
tgrosch | 0:62b846b3988a | 179 | static uint8_t c; |
tgrosch | 0:62b846b3988a | 180 | static uint32_t addon; |
tgrosch | 0:62b846b3988a | 181 | state=true; |
tgrosch | 0:62b846b3988a | 182 | |
tgrosch | 0:62b846b3988a | 183 | this->dataRx[0]=address; |
tgrosch | 0:62b846b3988a | 184 | state=this->read(); |
tgrosch | 0:62b846b3988a | 185 | *data=0; |
tgrosch | 0:62b846b3988a | 186 | for(c=0;c<this->regBankSize;c++){ |
tgrosch | 0:62b846b3988a | 187 | addon=this->dataRx[1+c]; |
tgrosch | 0:62b846b3988a | 188 | addon=addon<<(c<<3); |
tgrosch | 0:62b846b3988a | 189 | *data+=addon; |
tgrosch | 0:62b846b3988a | 190 | } |
tgrosch | 0:62b846b3988a | 191 | |
tgrosch | 0:62b846b3988a | 192 | return state; |
tgrosch | 0:62b846b3988a | 193 | } |
tgrosch | 0:62b846b3988a | 194 | |
tgrosch | 0:62b846b3988a | 195 |