Content originally posted in LPCWare by JoeM on Thu Apr 23 00:03:08 MST 2015
Using an NGX Xplorer with Lpc4330 and driving Uart0 at 1 MBaud for some debug output.
Currently using Lpcopen 2.12 and Lpcxpresso.
Configuring Uart0 to 1 MBaud won't work because of a rounding error within the baudrate calculation in Chip_UART_SetBaud().
Originally Chip_UART_SetBaud() code:
IMHO, the errornous part resists in the statement
div = clkin / (baudrate * 16);
and changing that to something like this:
tmp = baudrate * 16;
div = (clkin + tmp/2) / tmp;
make it work for e.g. 921,6 kBaud and 1 MBaud.
But now, another colleague says that it better has to be changed to:
tmp = baudrate * 16;
div = (clkin + (tmp  1)) / tmp;
What is the best / most correct way for calculating the Uart baudrate?
BTW: The errornous calculation can be found both in Lpcopen 2.12 and 2.16.
Using an NGX Xplorer with Lpc4330 and driving Uart0 at 1 MBaud for some debug output.
Currently using Lpcopen 2.12 and Lpcxpresso.
Configuring Uart0 to 1 MBaud won't work because of a rounding error within the baudrate calculation in Chip_UART_SetBaud().
Originally Chip_UART_SetBaud() code:

IMHO, the errornous part resists in the statement
div = clkin / (baudrate * 16);
and changing that to something like this:
tmp = baudrate * 16;
div = (clkin + tmp/2) / tmp;
make it work for e.g. 921,6 kBaud and 1 MBaud.
But now, another colleague says that it better has to be changed to:
tmp = baudrate * 16;
div = (clkin + (tmp  1)) / tmp;
What is the best / most correct way for calculating the Uart baudrate?
BTW: The errornous calculation can be found both in Lpcopen 2.12 and 2.16.
These are just different ways of rounding the result of the division:
Rounding up or down may be useful if you need upper or lower bounds instead of a close approximation.
But you should do the math how much deviation from the true value you get and check whether that is precise enough for your application.