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