// // Originally, 'endTransmission' was an f(void) function. // It has been modified to take one parameter indicating // whether or not a STOP should be performed on the bus. // Calling endTransmission(false) allows a sketch to // perform a repeated start. // // WARNING: Nothing in the library keeps track of whether // the bus tenure has been properly ended with a STOP. It // is very possible to leave the bus in a hung state if // no call to endTransmission(true) is made. Some I2C // devices will behave oddly if they do not see a STOP. // uint8_t TwoWire::endTransmission(uint8_t sendStop) { /* Set direction to send for sending of address and data. */ uint8_t result = (uint8_t)getWriteError(); if(result != 0) { // reset tx buffer iterator vars txBufferIndex = 0; txBufferLength = 0; // indicate that we are done transmitting transmitting_master = false; return 1; } uint32_t t0 = millis(); while(I2C_HAL_GetStatusFlag(instance, kI2CBusBusy) && !I2C_HAL_IsMaster(instance)) { if(millis() - t0 >= 25) return 4; //timeout } uint16_t slaveAddress; slaveAddress = (txAddress << 1U) & 0x00FFU; bool sent = sendAddress(slaveAddress); //tx buffer also sent by interrupt // reset tx buffer iterator vars txBufferIndex = 0; txBufferLength = 0; // indicate that we are done transmitting transmitting_master = false; clearWriteError(); result = 4; if(sent) //sent without timeout { if(master_state == MASTER_STATE_COMPLETE) { result = 0; } else if(master_state == MASTER_STATE_TX_NAK) //failed { result = (txBufferIndex == 0) ? 2 : 3; //address or data fail sendStop = true; } if(sendStop) { I2C_HAL_SendStop(instance); } } I2C_HAL_SetDirMode(instance, kI2CReceive); return result; }
//------------------------------------------------------------------------------ void FatStreamBase::putch(char c) { if (c == '\n' && !(getmode() & ios::binary)) { write('\r'); } write(c); if (getWriteError()) { setstate(badbit); } }
// ------------------------------------------------------------------------------------------------------ // Get Wire Error - returns "Wire" error code from a failed Tx/Rx command // return: 0=success, 1=data too long, 2=recv addr NACK, 3=recv data NACK, 4=other error // (Note: error code 1 (data too long) is only valid after Tx, if it appears after Rx then it was set by a previous Tx) // uint8_t i2c_t3::getError(void) { // convert status to Arduino return values (give these a higher priority than buf overflow error) switch(i2c->currentStatus) { case I2C_ADDR_NAK: return 2; case I2C_DATA_NAK: return 3; case I2C_ARB_LOST: return 4; case I2C_TIMEOUT: return 4; default: break; } if(getWriteError()) return 1; // if write_error was set then flag as buffer overflow return 0; // no errors }
//------------------------------------------------------------------------------ void FatStreamBase::putstr(const char* str) { size_t n = 0; while (1) { char c = str[n]; if (c == '\0' || (c == '\n' && !(getmode() & ios::binary))) { if (n > 0) { write(str, n); } if (c == '\0') { break; } write('\r'); str += n; n = 0; } n++; } if (getWriteError()) { setstate(badbit); } }