Hi everyone,
I am trying to verify if the prescaler value calculate by sciSetBaudrate() is correct, but I could not get it right, so I am wondering if I calculate it wrongly.
Lets use RM46x as an example.
In RM46x Technical Reference Manual page 1547 it shows the formulas and result sample as following picture:-
The function sciSetBaudrate() generated by HalCoGen is as follow:-
void sciSetBaudrate(sciBASE_t *sci, uint32 baud)
{
float64 vclk = 50.000 * 1000000.0;
uint32 f = ((sci->GCR1 & 2U) == 2U) ? 16U : 1U;
uint32 temp;
float64 temp2;
/* USER CODE BEGIN (6) */
/* USER CODE END */
/*SAFETYMCUSW 96 S MR:6.1 <APPROVED> "Calculations including int and float cannot be avoided" */
temp = (f*(baud + 1U));
temp2 = ((vclk)/((float64)temp));
sci->BRS = (uint32)((uint32)temp2 & 0x00FFFFFFU);
/* USER CODE BEGIN (7) */
/* USER CODE END */
}
Lets try calculate the prescaler value using the same criteria in result sample with the formula in sciSetBaudrate():-
f = 16
baud = 115200
vclk = 50000000
temp = (f*(baud + 1U)) = 16*(115200 + 1) = 1843216
temp2 = ((vclk)/((float64)temp)) = 50000000/1843216 = 27.1265
sci->BRS = (uint32)((uint32)temp2 & 0x00FFFFFFU) = 27
Which the prescaler value calculated by sciSetBaudrate()(27) is different with the manual(26).
Which part did I calculate wrongly?
Best Regards,
Jacky