void TwoWire::end() { DTWI::I2C_STATUS status = di2c.getStatus(); if(beginCount == 0) { return; } destroyTask(getTaskId(onI2C)); beginCount = 0; if(status.fMaster) { di2c.endMaster(); } else if(status.fSlave) { // forcefully end the slave di2c.endSlave(true); } // clean out the read / write buffer di2c.abort(); di2c.discard(); }
uint8_t TwoWire::endTransmission(uint8_t fStopBit) { if(fStopBit) { while(!di2c.stopMaster()); } return(true); }
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) { DTWI::I2C_STATUS status; // may have to wait for the last action to finish before // a repeated start can occur while(!di2c.startMasterRead(address, quantity)); do { status = di2c.getStatus(); } while(status.fMyBus && !status.fNacking); while(!di2c.stopMaster()); return(di2c.available()); }
void TwoWire::begin(void) { if(beginCount == 0) { beginCount++; // there are no callback in Master mode destroyTask(getTaskId(onI2C)); di2c.beginMaster(); } }
// must be called in: // slave rx event callback // or after requestFrom(address, numBytes) uint8_t TwoWire::read(void) { byte data; if(di2c.read(&data, 1) == 1) { return((uint8_t) data); } return('\0'); }
void TwoWire::begin(uint8_t address) { if(beginCount == 0) { beginCount++; // make sure we put the callback in the task manager beginSlave(onReceiveService, onRequestService); di2c.beginSlave(address); } }
static void onI2C(int id, void * tptr) { DTWI::I2C_STATUS status = di2c.getStatus(); uint8_t data; if(status.fSlave) { if(status.fRead) { while(di2c.available() > 0) { onReceiveServiceR(NULL, di2c.available()); } } // on writing out, we only call once when the // new session starts else if(status.fWrite && iSessionCur != status.iSession) { iSessionCur = status.iSession; onRequestServiceR(); } } }
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ /************************************************************************/ /* Revision History: */ /* 8/4/2014(KeithV): Created */ /************************************************************************/ // DTWI is not on the path and we don't want the user to have // to explicitly include the library. So just grab it and compile it #include "../DTWI/DTWI.cpp" #define ENABLE_END #include <Wire.h> DTWI0 di2c; TwoWire Wire; // Initialize Class Variables ////////////////////////////////////////////////// uint8_t TwoWire::rxBuffer[BUFFER_LENGTH]; uint8_t TwoWire::rxBufferIndex = 0; uint8_t TwoWire::rxBufferLength = 0; uint8_t TwoWire::txAddress = 0; uint8_t TwoWire::txBuffer[BUFFER_LENGTH]; uint8_t TwoWire::txBufferIndex = 0; uint8_t TwoWire::txBufferLength = 0; uint8_t TwoWire::transmitting = 0; void (*TwoWire::user_onRequest)(void);
// must be called in: // slave rx event callback // or after requestFrom(address, numBytes) uint8_t TwoWire::available(void) { return (di2c.available()); }
// must be called in: // slave tx event callback // or after beginTransmission(address) int TwoWire::write(uint8_t* data, uint8_t quantity) { return(di2c.write((const byte *) data, quantity)); }
// must be called in: // slave tx event callback // or after beginTransmission(address) int TwoWire::write(uint8_t data) { return(di2c.write((const byte *) &data, 1)); }
void TwoWire::beginTransmission(uint8_t address) { while(!di2c.startMasterWrite(address)); }