Using the example code from this site (and all other examples appear to be similar):
The DWT counter on the STM32F411 appears to increment correctly when running a debug session but not in a release session. Here’s an example function that uses DWT to execute a timeout on the I2C bus:
tKernelReturnValue
OSp_I2C_Master_Send_Data(
I2C_TypeDef * I2C,
char data)
{
uint32_t timeout = KIN1_DWT_CYCCNT;
tKernelReturnValue result;
I2C->DR = data;
result.error = O$_I2C_TRANSMIT_ERROR;
while( (!(I2C->SR1 & I2C_SR1_TXE)) &&
((KIN1_DWT_CYCCNT - timeout) < I2C_BUS_TIMEOUT) ) {
OS_ClrWDT();
} // end while
if (I2C->SR1 & I2C_SR1_TXE) {
result.error = O$_SUCCESS;
} // end if
return result;
} // end OSp_I2C_Master_Send_Data
In a live debug session I can verify the DWT is running, but the code won’t break out of the while
loop during a release build session. Normally I would have assumed that a clock was incorrectly configured but the F411 reference manual doesn’t indicate that the DWT connects to any clock source that has to be independently enabled. I have used this same example code on the LPC55S69 and not had this issue. I have also set the compiler to not optimize anything just in case. Not sure what else to look at for this issue.