Hi,
i have a problem in understanding the i2c communication.
I have setup eusci b for the i2c and I am trying to read / write an VL53L0X sensor with it. The problem is, that whenevery I want to write something, my logic analyzer says, I'm writing 0xFF. When I do a step-by-step debugging, I can see that I am writing the correct data, so I guess there is a loop or delay missing, but i cannot find where...
First of all, my initilization code:
void i2cInit() {
UCB0CTLW0 |= UCSWRST; // put into SW Reset
UCB0CTLW0 |= UCSSEL_3; // choose SMCLK
UCB0BRW = 40; // set prescaler = 80 => 100 kHz
UCB0CTLW0 |= UCMODE_3; // put into I2C Mode
UCB0CTLW0 |= UCMST; // put into MASTER mode
UCB0I2CSA = 0x29; // slave addr = 0x29
UCB0TBCNT = 1; // default byte count = 1
UCB0CTLW1 |= UCASTP_2; // auto STOP mode
P1SEL1 &= ~BIT3; // P1.3 = SCL
P1SEL0 |= BIT3;
P1SEL1 &= ~BIT2; // P1.2 = SDA
P1SEL0 |= BIT2;
UCB0CTLW0 &= ~UCSWRST; // get out of SW Reset
__delay_cycles(10000);
}
then my read register code
int i2cReadReg(uint8_t reg, uint8_t *dest, uint16_t timeout) {
int toCounter = 0;
UCB0TBCNT = 1;
UCB0CTLW0 |= UCTR; // put into Tx mode
UCB0CTLW0 |= UCTXSTT; // gen START
while(UCB0CTLW0 & UCTXSTT) // wait for START to be sent
{
if(toCounter++ > timeout)
return -1;
}
UCB0TXBUF = reg; // write register
while (UCB0IFG & !UCTXIFG) // wait for Tx buffer to be empty
{
if(toCounter++ > timeout)
return -2;
}
while (UCB0CTL1 & UCTXSTP) // wait for STOP
{
if(toCounter++ > timeout)
return -3;
}
UCB0CTLW0 &= ~UCTR; // put into Rx mode
UCB0CTLW0 |= UCTXSTT; // gen START
while(UCB0CTLW0 & UCTXSTT) // wait for START to be sent
{
if(toCounter++ > timeout)
return -4;
}
while (UCB0IFG & !UCRXIFG) // wait for Rx Data to arrive
{
if(toCounter++ > timeout)
return -5;
}
*dest = UCB0RXBUF;
while (UCB0CTL1 & UCTXSTP) // wait for STOP
{
if(toCounter++ > timeout)
return -6;
}
return 1;
}
below you can see images of the logic analyzer with the debug active and without. I tried adding some __delay_cycles between start and data set, before start... did not help.
What am I doing wrong?
PS: sorry somewhow the "code" tag threw an error for me...
Without Debug:
With Debug: