Hi Felipe Garcia,
the intention of the assert is clear, but in the original code the assertion will not work with the diff variable:
In case the MCU works with 15 MHz clock provides an input for the Uart with a tolerance less then 0.2 %, but 'diff' contains not the best value from the loop. 'diff' can be an other value from the baud rate loop which fails in the assertion:
Orginal code:
else
{
for (osrval = best_osrval; osrval >= 8; osrval--)
{
brgval = (srcClock_Hz / ((osrval + 1) * baudrate_Bps)) - 1;
if (brgval > 0xFFFF)
{
continue;
}
baudrate = srcClock_Hz / ((osrval + 1) * (brgval + 1));
diff = baudrate_Bps < baudrate ? baudrate - baudrate_Bps : baudrate_Bps - baudrate;
if (diff < best_diff)
{
/// 15.000.000 Hz / 130 --> 115.384 130 is possible with ((osrval + 1) * (brgval + 1)) tolerance < 0.2%
best_diff = diff; <-------- remember later on best difference
best_osrval = osrval; <-------- best osrval
best_brgval = brgval; <-------- best brgval
}
}
/* value over range */
if (best_brgval > 0xFFFF)
{
return kStatus_USART_BaudrateNotSupport;
}
/* If the baud rate caculated is not very precise, please select the FRG clock as
* the USART's source clock, and set the FRG frequency to a more suitable value.
*/
assert(diff < ((baudrate_Bps / 100) * 3)); <-------- wrong comparison
base->OSR = best_osrval;
base->BRG = best_brgval;
}
So the following is the correct code for the assertion:
assert(best_diff < ((baudrate_Bps / 100) * 3));