What is the formula for calculation of values for the UART BAUDRATE register from bit rates?
What is the formula for calculation of values for the UART BAUDRATE register from bit rates?
Hi Bengt,
note: this formula is simplified and may differ from the actual baudrate measurement
The formula is: Baudrate = desired baudrate * 2^32 / 16000000
Example: Baudrate of 31250 should then be 8388608 decimal = 0x800000.
Note that you will have to round the number afterwards: rounded_value = (value + 0x800) & 0xFFFFF000
Since the baudrate generator will be sourced by the 16 M (Either RC or XOSC, depending on your configuration), then your error rate will be ~equal to the overall drift of the system clock and the accuracy of the baudrate generator towards your target baudrate.
Other way around: Check out this define in nrf51_bitfields.h line 5702:
UART_BAUDRATE_BAUDRATE_Baud115200 (0x01D7E000UL) /*!< 115200 baud. */
The actual baudrate set with the above define is:
baudrate_reg_val * 16M / 2^32 = 115203.86 baud.
Best regards Håkon
Tnx Håkon. I assume you put this in the next Reference Manual as well.
BTW, why the heavy rounding? 'nrf51_bitfields.h' have a value for 921600bps that is 0x0EBEDFA4UL. By rounding this gets 0x0EBEE000. Does this imply the the baud rate counter is not 32-bit but mere 20-bit?
This is the tested code to get the BAUDRATE value from an arbitrary bit rate. It gives the same values as 'nrf51_bitfields.h' except for 921600bps and for that we wait for an explanation from Håkon.
static uint32_t uartBaudrate(uint32_t bps) { return ((bps * 0x100000000ULL) / SYSCLK + 0x800) & 0xfffff000; }
Hi Bengt,
That specific value got me a bit puzzled. I had a quick chat with a HW-designer, and he assured me that the baudrate generator for the UART is actually 20 bits, so rounding procedure is required.
As far as I see, the 921K600 baud definition in nrf51_bitfields.h is incorrect. I will input a bug regarding this item. I see that this define is removed from our reference manual, so it's likely a glitch that it's still defined in the header file.
I will also push for the calculation to be inserted into our documentation.
BR Håkon