Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow any baud rate; not just a restricted set #41

Closed
wants to merge 1 commit into from

Conversation

Timmmm
Copy link

@Timmmm Timmmm commented Jan 22, 2016

Fixes #40.

I'm pretty sure the rounding is unnecessary (i.e. the hardware rounds / truncates anyway), but I did it anyway so the values match the Nordic header.

Fixes ARMmbed#40.

I'm pretty sure the rounding is unnecessary (i.e. the hardware rounds / truncates anyway), but I did it anyway so the values match the Nordic header.
@ciarmcom
Copy link
Member

Automatic CI verification build not done, please verify manually.

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 1, 2016

@emidttun @pan-

@ciarmcom
Copy link
Member

ciarmcom commented Mar 1, 2016

Automatic CI verification build not done, please verify manually.

// Round it to 20 bits (see link above).
br = (br + 0x800) & 0xFFFFF000;

// Limit it to the valid range.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the valid range defined? From the nRF51 ref manual any 32-bit value seems to be accepted.

@Timmmm
Copy link
Author

Timmmm commented Mar 4, 2016

Here is the code I used to test it. It's Matlab/Octave rather than C, but it is the same maths.

function code = ToBaudCode(br)

code = bitshift(br, 8, 32) ...
+ bitshift(br, 4, 32)...
 - bitshift(br, 1, 32)...
 - br...
 - bitshift(br, -1, 32)...
 - bitshift(br, -4, 32)...
 - bitshift(br, -9, 32)...
 - bitshift(br, -14, 32)...
 - bitshift(br, -16, 32)...
 - bitshift(br, -17, 32)...
 - bitshift(br, -18, 32)...
 - bitshift(br, -19, 32);

 % Round it
 code = bitand(code + 0x800, 0xFFFFF000);

 end

Test:

bauds = [1200 2400 4800 9600 14400 19200 28800 38400 57600 76800 115200 230400 250000 460800 921600 1000000];
for ff = bauds
    dec2hex(ToBaudCode(ff))
end

Output:

ans = 4F000
ans = 9D000
ans = 13B000
ans = 275000
ans = 3B0000
ans = 4EA000
ans = 75F000
ans = 9D5000
ans = EBF000
ans = 13A9000
ans = 1D7E000
ans = 3AFB000
ans = 4000000
ans = 75F7000
ans = EBEE000
ans = 10000000

Correct values:

#define UART_BAUDRATE_BAUDRATE_Baud1200 (0x0004F000UL) /*!< 1200 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud2400 (0x0009D000UL) /*!< 2400 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud4800 (0x0013B000UL) /*!< 4800 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud9600 (0x00275000UL) /*!< 9600 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud14400 (0x003B0000UL) /*!< 14400 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud19200 (0x004EA000UL) /*!< 19200 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud28800 (0x0075F000UL) /*!< 28800 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud38400 (0x009D5000UL) /*!< 38400 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud57600 (0x00EBF000UL) /*!< 57600 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud76800 (0x013A9000UL) /*!< 76800 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud115200 (0x01D7E000UL) /*!< 115200 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud230400 (0x03AFB000UL) /*!< 230400 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud250000 (0x04000000UL) /*!< 250000 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud460800 (0x075F7000UL) /*!< 460800 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud921600 (0x0EBED000UL) /*!< 921600 baud. */
#define UART_BAUDRATE_BAUDRATE_Baud1M (0x10000000UL) /*!< 1M baud. */

Looks like the 921600 value has been rounded in the latest code. My value is very slightly different, but it shouldn't matter in practice (it's a 0.002% difference).

It seems pretty clear from the datasheet to me that the maximum rate is 1 Mbaud, especially as 1 Mbaud gets a special value (0x10000000). The minimum rate is due to the rounding (I don't think anyone will want to go as low as 15 baud anyway!).

@Timmmm Timmmm closed this Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Only specific baud rates allowed, but chip supports any.
4 participants