Bug in fsl_uart.c for LPC802 (assertion)?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Bug in fsl_uart.c for LPC802 (assertion)?

738 Views
wkh
Contributor I

Original code in fsl_uart.c

------------------------------------------------------------------------------------------------

status_t USART_SetBaudRate(USART_Type *base, uint32_t baudrate_Bps, uint32_t srcClock_Hz):

Line 354:

        /* 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?

        base->OSR = best_osrval;
        base->BRG = best_brgval;
    }

--------------------------------------------------------------------------------------------

In case of setting 115200 baud the assert happens.

I have changed the following  >>> assert(best_diff < ((baudrate_Bps / 100) * 3)); <<<< to avoid the

assert, because 115.200 is a valid value in case of a 15MHz system or main clock is used.

Correct change?

Labels (1)
0 Kudos
3 Replies

571 Views
wkh
Contributor I

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));

0 Kudos

571 Views
FelipeGarcia
NXP Employee
NXP Employee

Hi Karlheinz Wuersch,

 

Thanks for your reply.

 

I have checked the modification of the assert on my side and you are correct. The assert should work with a 115200 baudrate. I tested the changes I didn’t have any issues using UART at 115200, you shouldn't have either.

 

Thanks again for your feedback.

 

Best regards,

Felipe

0 Kudos

571 Views
FelipeGarcia
NXP Employee
NXP Employee

Hi Karlheinz Wuersch,

 

By doing the change you suggested you will be ignoring the difference between the baudrate you require and the baudrate calculated.

That assert is located there to prevent using inaccurate baudrates.

 

Your code may work using 115200 but it wouldn’t guaranty a reliable operation.

 

I hopes this clarifies.

 

Have a great day,
Felipe

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos