// ------------------------------------------------------------------------------------------------------ // Setup Master Transmit - initialize Tx buffer for transmit to slave at address // return: none // parameters: // address = target 7bit slave address // void i2c_t3::beginTransmission(uint8_t address) { i2c->txBuffer[0] = (address << 1); // store target addr i2c->txBufferLength = 1; clearWriteError(); // clear any previous write error i2c->currentStatus = I2C_WAITING; // reset status }
// // 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 TwoWire::beginTransmission(uint8_t address) { // indicate that we are transmitting transmitting_master = true; // set address of targeted slave txAddress = address; // reset tx buffer iterator vars txBufferIndex = 0; txBufferLength = 0; clearWriteError(); }