关于前面的问题:
https://e2e.ti.com/support/microcontrollers/c2000-microcontrollers-group/c2000/f/c2000-microcontrollers-forum/907412/tms320f28388s-i2c-read-the-wrong-data/3362401#3362401
https://e2e.ti.com/support/microcontrollers/c2000-microcontrollers-group/c2000/f/c2000-microcontrollers-forum/909853/compiler-tms320f28388s-unable-to-disable-the-optimization-level-for-some-function?tisearch=e2e-sitesearch&keymatch=TMS320F28388S
在我的系统中、当我运行以下代码时、在重复模式主发送器中:
//Flush FIFOs
I2C_disableFIFO(I2CA_BASE);
I2C_enableFIFO(I2CA_BASE);
//Setup how many bytes to send
//I2C_setDataCount(I2CA_BASE, 0x01);
//Put data to be transmitted in TX FIFO
I2C_putData(I2CA_BASE, byte);
//wait for the data to be sent
//ARDY is low when data is not yet sent. ARDY gets set when all data in FIFO has been sent or NACK is received
attemptCount = 1U;
while(attemptCount < 100U)
{
if((I2C_getStatus(I2CA_BASE) & I2C_STR_ARDY) != 0U)
{
//all data in FIFO has been sent along with START, Device Address or NACK has been received
//so break out of the loop
break;
}
else
{
//all data in FIFO is yet to be sent
//stay inside loop
attemptCount++;
DEVICE_DELAY_US(10);
}
}
//Outside the loop, if ARDY is still low, that means data hasn't been sent yet and timeout has occurred
//Exit function and return timeout error
if((I2C_getStatus(I2CA_BASE) & I2C_STR_ARDY) == 0U)
{
//enable write protect
GPIO_writePin(39, 1);
return ERROR_TIMEOUT;
}
此代码在禁用优化的情况下工作正常。 但是、当我启用优化时、该函数返回 ERROR_TIMEOUT。
如上面的链接中所述、我可以通过在首次检查 ARDY 之前添加延迟来解决此问题。 我可以使用2个代码:
1.添加 了__asm (" RPT #12 || NOP");
//Flush FIFOs
I2C_disableFIFO(I2CA_BASE);
I2C_enableFIFO(I2CA_BASE);
//Setup how many bytes to send
//I2C_setDataCount(I2CA_BASE, 0x01);
//Put data to be transmitted in TX FIFO
I2C_putData(I2CA_BASE, byte);
//wait for the data to be sent
//ARDY is low when data is not yet sent. ARDY gets set when all data in FIFO has been sent or NACK is received
attemptCount = 1U;
while(attemptCount < 100U)
{
__asm(" RPT #12 || NOP");
if((I2C_getStatus(I2CA_BASE) & I2C_STR_ARDY) != 0U)
{
//all data in FIFO has been sent along with START, Device Address or NACK has been received
//so break out of the loop
break;
}
else
{
//all data in FIFO is yet to be sent
//stay inside loop
attemptCount++;
DEVICE_DELAY_US(10);
}
}
//Outside the loop, if ARDY is still low, that means data hasn't been sent yet and timeout has occurred
//Exit function and return timeout error
if((I2C_getStatus(I2CA_BASE) & I2C_STR_ARDY) == 0U)
{
//enable write protect
GPIO_writePin(39, 1);
return ERROR_TIMEOUT;
}
2.移动 DEVICE_DELAY_US (10);
//Flush FIFOs
I2C_disableFIFO(I2CA_BASE);
I2C_enableFIFO(I2CA_BASE);
//Setup how many bytes to send
//I2C_setDataCount(I2CA_BASE, 0x01);
//Put data to be transmitted in TX FIFO
I2C_putData(I2CA_BASE, byte);
//wait for the data to be sent
//ARDY is low when data is not yet sent. ARDY gets set when all data in FIFO has been sent or NACK is received
attemptCount = 1U;
while(attemptCount < 100U)
{
DEVICE_DELAY_US(10);
if((I2C_getStatus(I2CA_BASE) & I2C_STR_ARDY) != 0U)
{
//all data in FIFO has been sent along with START, Device Address or NACK has been received
//so break out of the loop
break;
}
else
{
//all data in FIFO is yet to be sent
//stay inside loop
attemptCount++;
}
}
//Outside the loop, if ARDY is still low, that means data hasn't been sent yet and timeout has occurred
//Exit function and return timeout error
if((I2C_getStatus(I2CA_BASE) & I2C_STR_ARDY) == 0U)
{
//enable write protect
GPIO_writePin(39, 1);
return ERROR_TIMEOUT;
}
为什么会出现此问题? 是否存在流水线问题? 或者 I2C 需要一些周期来响应?