这是代码,它应该可以工作,但事实并非如此。我总是得到仲裁失败或不承认。我检查了其他所有内容,从属设备已连接并使用其他代码等等。
/* Set Master Disable bit and Slave Disable bit */
TWI0->TWI_CR = TWI_CR_SVDIS;
TWI0->TWI_CR = TWI_CR_MSDIS;
/* Set Master Enable bit */
TWI0->TWI_CR = TWI_CR_MSEN;
_twi_set_speed(TWI0, 100000, 84000000);
TWI0->TWI_IER = (TWI_IER_TXRDY | TWI_IER_OVRE | TWI_IER_NACK | TWI_IER_ARBLST);
TWI0->TWI_MMR = 0;
TWI0->TWI_MMR = TWI_MMR_DADR(addr) | ((0 << TWI_MMR_IADRSZ_Pos) & TWI_MMR_IADRSZ_Msk);
TWI0->TWI_IADR = 0;
uint8_t c = 0;
int cnt = 1;
uint32_t status;
while (cnt > 0) {
status = TWI0->TWI_SR;
if (status & TWI_SR_NACK) {
i2cp->errors = 12;
return 0;
}
if (status & TWI_SR_ARBLST) {
i2cp->errors = 13;
return 0;
}
if (!(status & TWI_SR_TXRDY)) {
continue;
}
TWI0->TWI_THR = c;
cnt--;
}
while (1) {
status = TWI0->TWI_SR;
if (status & TWI_SR_NACK) {
i2cp->errors = 10;
return 0;
}
if (status & TWI_SR_ARBLST) {
i2cp->errors = 11;
return 0;
}
if (status & TWI_SR_TXRDY) {
break;
}
}
TWI0->TWI_CR |= TWI_CR_STOP;
while (!(TWI0->TWI_SR & TWI_SR_TXCOMP)) {
}