Sure... It's more than you asked for, but here are some checking routines:
(Embiic is the name of the project this code was used in)
uint32_t I2C_Get_Status_Register(){
return pTwi->TWI_SR;
}
boolean Embiic_Check_NACK(uint32_t sr) {
return ((sr & TWI_SR_NACK) & TWI_SR_NACK);
}
boolean Embiic_Check_TXCOMP(uint32_t sr) {
return ((sr & TWI_SR_TXCOMP) == TWI_SR_TXCOMP);
}
boolean Embiic_Wait_ACK_TXCOMP() {
unsigned long start, now;
uint32_t sr, tw_nack, tw_txcomp;
tw_txcomp = 0;
start = micros();
while (tw_txcomp == 0) {
sr = I2C_Get_Status_Register();
now = micros();
if (now < start) { // handle overflow, at at the expense of a longer timeout
start = now;
now++;
} else {
now = now - start;
}
if (now > I2C_ACK_TIMEOUTuS){
DBG_PRINTLN("Embiic_CheckTXCOMP: Timeout.");
I2C_Reset_Request = 1;
return false;
}
tw_nack = Embiic_Check_NACK(sr);
if (tw_nack != 0) {
DBG_PRINTLN("Embiic_CheckTXCOMP: NACK. Natch.");
return false;
}
tw_txcomp = Embiic_Check_TXCOMP(sr);
}
return true;
}
boolean Embiic_Wait_TXCOMP() {
unsigned long start, now;
uint32_t sr, tw_txcomp;
tw_txcomp = 0;
start = micros();
while (tw_txcomp == 0) {
sr = I2C_Get_Status_Register();
now = micros();
if (now < start) { // handle overflow, at at the expense of a longer timeout
start = now;
now++;
} else {
now = now - start;
}
if (now > I2C_ACK_TIMEOUTuS){
DBG_PRINTLN("Embiic_CheckTXCOMP: Timeout.");
I2C_Reset_Request = 1;
return false;
}
tw_txcomp = Embiic_Check_TXCOMP(sr);
}
return true;
}
boolean Embiic_WaitByteSent() {
uint32_t sr;
//bad: TXCOMP is 1, NACK is 1
unsigned long start, now;
start = micros();
while (1) {
now = micros();
if (now < start) { // handle overflow, at at the expense of a longer timeout
start = now;
now++;
} else {
now = now - start;
}
if (now > I2C_ACK_TIMEOUTuS){
DBG_PRINTLN("Embiic_WaitByteSent: Timeout.");
I2C_Reset_Request = 1;
return false;
}
sr = I2C_Get_Status_Register();
if ((sr & TWI_SR_NACK) & TWI_SR_NACK) {
DBG_PRINTLN("Embiic_WaitByteSent: NACK. Bad Address?");
return false;
}
if ((sr & TWI_SR_TXRDY) == TWI_SR_TXRDY) {
return true;
}
}
return true;
}
boolean Embiic_WaitByteReceived() {
unsigned long start, now;
start = millis();
while (!TWI_ByteReceived(WIRE_INTERFACE)) {
now = millis();
if (now < start) { // handle overflow, at at the expense of a longer timeout
start = now;
now++;
} else {
now = now - start;
}
if (now > I2C_READ_TIMEOUTmS){
DBG_PRINTLN("Embiic_WaitByteReceived: Timeout.");
I2C_Reset_Request = 1;
return false;
}
}
return true;
}
-Chris
I'm not a coder. I just play one on the internets.