Hi, Franco,
Regarding your question, frankly speaking, I am not sure if the following code is correct or not. How about setting the osr=15? from my opinion, the transmitter baudrate=(LPUART ASYNCH Module Clock)/(SBR[12:0]*16), so the OSR bits should be equal to 15.
Pls have a try.
BR
XiangJun Rong
/*FUNCTION**********************************************************************
*
* Function Name : LPUART_HAL_SetBaudRate
* Description : Configures the LPUART baud rate.
* In some LPUART instances the user must disable the transmitter/receiver
* before calling this function.
* Generally, this may be applied to all LPUARTs to ensure safe operation.
*
*END**************************************************************************/
lpuart_status_t LPUART_HAL_SetBaudRate(LPUART_Type * base,
uint32_t sourceClockInHz,
uint32_t desiredBaudRate)
{
uint16_t sbr, sbrTemp, i;
uint32_t osr, tempDiff, calculatedBaud, baudDiff;
/* This lpuart instantiation uses a slightly different baud rate calculation
* The idea is to use the best OSR (over-sampling rate) possible
* Note, osr is typically hard-set to 16 in other lpuart instantiations
* First calculate the baud rate using the minimum OSR possible (4) */
osr = 4;
sbr = (sourceClockInHz/(desiredBaudRate * osr));
/*set sbr to 1 if the sourceClockInHz can not satisfy the desired baud rate*/
if(sbr == 0)
{
sbr = 1;
}
calculatedBaud = (sourceClockInHz / (osr * sbr));
if (calculatedBaud > desiredBaudRate)
{
baudDiff = calculatedBaud - desiredBaudRate;
}
else
{
baudDiff = desiredBaudRate - calculatedBaud;
}
/* loop to find the best osr value possible, one that generates minimum baudDiff
* iterate through the rest of the supported values of osr */
for (i = 5; i <= 32; i++)
{
/* calculate the temporary sbr value */
sbrTemp = (sourceClockInHz/(desiredBaudRate * i));
/*set sbrTemp to 1 if the sourceClockInHz can not satisfy the desired baud rate*/
if(sbrTemp == 0)
{
sbrTemp = 1;
}
/* calculate the baud rate based on the temporary osr and sbr values */
calculatedBaud = (sourceClockInHz / (i * sbrTemp));
if (calculatedBaud > desiredBaudRate)
{
tempDiff = calculatedBaud - desiredBaudRate;
}
else
{
tempDiff = desiredBaudRate - calculatedBaud;
}
if (tempDiff <= baudDiff)
{
baudDiff = tempDiff;
osr = i; /* update and store the best osr value calculated */
sbr = sbrTemp; /* update store the best sbr value calculated */
}
}
/* Check to see if actual baud rate is within 3% of desired baud rate
* based on the best calculate osr value */
if (baudDiff < ((desiredBaudRate / 100) * 3))
{
/* Acceptable baud rate, check if osr is between 4x and 7x oversampling.
* If so, then "BOTHEDGE" sampling must be turned on */
if ((osr > 3) && (osr < 8))
{
LPUART_BWR_BAUD_BOTHEDGE(base, 1);
}
/* program the osr value (bit value is one less than actual value) */
LPUART_BWR_BAUD_OSR(base, (osr-1));
/* write the sbr value to the BAUD registers */
LPUART_BWR_BAUD_SBR(base, sbr);
}
else
{
/* Unacceptable baud rate difference of more than 3% */
return kStatus_LPUART_BaudRateCalculationError;
}
return kStatus_LPUART_Success;
}