uint8_t TwoWire::endTransmission(uint8_t sendStop) { uint8_t error = I2C_MASTER_ERR_NONE; if(TX_BUFFER_EMPTY) return 0; //Wait for any previous transaction to complete while(ROM_I2CMasterBusBusy(MASTER_BASE)); while(ROM_I2CMasterBusy(MASTER_BASE)); //Select which slave we are requesting data from //false indicates we are writing to the slave ROM_I2CMasterSlaveAddrSet(MASTER_BASE, txAddress, false); while(ROM_I2CMasterBusy(MASTER_BASE)); unsigned long cmd = RUN_BIT | START_BIT; error = sendTxData(cmd,txBuffer[txReadIndex]); txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH; if(error) return error; while(!TX_BUFFER_EMPTY) { error = sendTxData(RUN_BIT,txBuffer[txReadIndex]); txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH; if(error) return getError(error); } if(txWriteRomQuantity!=0) // Hey we have Rom Block data to transmit! { while(txWriteRomIndex != txWriteRomQuantity) { uint8_t c = *PtrTxRomBuffer++; error = sendTxData(RUN_BIT,c); txWriteRomIndex++; if(error) return getError(error); } txWriteRomQuantity=0; // Clear to disable further tx } if(sendStop) { while(ROM_I2CMasterBusy(MASTER_BASE)); HWREG(MASTER_BASE + I2C_O_MCS) = STOP_BIT; while(ROM_I2CMasterBusy(MASTER_BASE)); currentState = IDLE; } else { currentState = MASTER_TX; } // indicate that we are done transmitting transmitting = 0; return error; }
uint8_t TwoWire::endTransmission(uint8_t sendStop) { uint8_t error = I2C_MASTER_ERR_NONE; digitalWrite(RED_LED, HIGH); if(TX_BUFFER_EMPTY) return 0; //Wait for any previous transaction to complete while(ROM_I2CMasterBusBusy(MASTER_BASE)); // Here it hangs sometimes! digitalWrite(RED_LED, LOW); while(ROM_I2CMasterBusy(MASTER_BASE)); //Select which slave we are requesting data from //false indicates we are writing to the slave ROM_I2CMasterSlaveAddrSet(MASTER_BASE, txAddress, false); while(ROM_I2CMasterBusy(MASTER_BASE)); unsigned long cmd = RUN_BIT | START_BIT; error = sendTxData(cmd,txBuffer[txReadIndex]); txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH; if(error) return error; while(!TX_BUFFER_EMPTY) { error = sendTxData(RUN_BIT,txBuffer[txReadIndex]); txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH; if(error) return getError(error); } if(sendStop) { while(ROM_I2CMasterBusy(MASTER_BASE)); HWREG(MASTER_BASE + I2C_O_MCS) = STOP_BIT; while(ROM_I2CMasterBusy(MASTER_BASE)); currentState = IDLE; } else { currentState = MASTER_TX; } // indicate that we are done transmitting transmitting = 0; return error; }