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

NXP targets: Improved baudrate calculation function #116

Merged

Conversation

triffid
Copy link
Contributor

@triffid triffid commented Nov 28, 2013

Uses 100% integer math, delivers more accurate results with fewer iterations in less time.

Since it seems most of the NXP chips use identical math, I have also altered the other targets in this commit.

If there are non-NXP chips which use a similar serial block, consider porting to those as well

Comparison:

     BAUD   DL MUL DIV  CPU_CLOCK
  -------  --- --- --- ----------

new: { 9600, 625, 1, 0, 96000000}, // Actual baud: 9600, error =0.00%, 1 iterations
old: { 9600, 625, 1, 0, 96000000}, // Actual baud: 9600, error =0.00%, 0 iterations

new: { 38400, 125, 4, 1, 96000000}, // Actual baud: 38400, error =0.00%, 8 iterations
old: { 38400, 81, 14, 13, 96000000}, // Actual baud: 38409, error +0.02%, 420 iterations

new: { 57600, 81, 7, 2, 96000000}, // Actual baud: 57613, error +0.02%, 120 iterations
old: { 57600, 54, 14, 13, 96000000}, // Actual baud: 57613, error +0.02%, 315 iterations

new: { 115200, 27, 14, 13, 96000000}, // Actual baud: 115226, error +0.02%, 120 iterations
old: { 115200, 27, 14, 13, 96000000}, // Actual baud: 115226, error +0.02%, 210 iterations

new: { 230400, 23, 15, 2, 96000000}, // Actual baud: 230179, error -0.10%, 120 iterations
old: { 230400, 17, 15, 8, 96000000}, // Actual baud: 230179, error -0.10%, 525 iterations

new: { 250000, 24, 1, 0, 96000000}, // Actual baud: 250000, error =0.00%, 1 iterations
old: { 250000, 24, 1, 0, 96000000}, // Actual baud: 250000, error =0.00%, 0 iterations

new: {1000000, 6, 1, 0, 96000000}, // Actual baud: 1000000, error =0.00%, 1 iterations
old: {1000000, 6, 1, 0, 96000000}, // Actual baud: 1000000, error =0.00%, 0 iterations

new: {2000000, 3, 1, 0, 96000000}, // Actual baud: 2000000, error =0.00%, 1 iterations
old: {2000000, 3, 1, 0, 96000000}, // Actual baud: 2000000, error =0.00%, 0 iterations

new: { 9600, 514, 15, 4, 100000000}, // Actual baud: 9600, error =0.00%, 110 iterations
old: { 9600, 337, 15, 14, 100000000}, // Actual baud: 9593, error -0.07%, 1365 iterations

new: { 38400, 93, 4, 3, 100000000}, // Actual baud: 38402, error +0.01%, 120 iterations
old: { 38400, 85, 12, 11, 100000000}, // Actual baud: 38363, error -0.10%, 525 iterations

new: { 57600, 62, 4, 3, 100000000}, // Actual baud: 57604, error +0.01%, 120 iterations
old: { 57600, 61, 9, 7, 100000000}, // Actual baud: 57633, error +0.06%, 840 iterations

new: { 115200, 31, 4, 3, 100000000}, // Actual baud: 115207, error +0.01%, 120 iterations
old: { 115200, 31, 4, 3, 100000000}, // Actual baud: 115207, error +0.01%, 525 iterations

new: { 230400, 19, 7, 3, 100000000}, // Actual baud: 230263, error -0.06%, 120 iterations
old: { 230400, 19, 7, 3, 100000000}, // Actual baud: 230263, error -0.06%, 735 iterations

new: { 250000, 25, 1, 0, 100000000}, // Actual baud: 250000, error =0.00%, 1 iterations
old: { 250000, 25, 1, 0, 100000000}, // Actual baud: 250000, error =0.00%, 0 iterations

new: {1000000, 5, 4, 1, 100000000}, // Actual baud: 1000000, error =0.00%, 8 iterations
old: {1000000, 5, 4, 1, 100000000}, // Actual baud: 1000000, error =0.00%, 315 iterations

new: {2000000, 2, 9, 5, 100000000}, // Actual baud: 2008929, error +0.45%, 120 iterations
old: {2000000, 2, 9, 5, 100000000}, // Actual baud: 2008929, error +0.45%, 315 iterations

new: { 9600, 625, 4, 1, 120000000}, // Actual baud: 9600, error =0.00%, 8 iterations
old: { 9600, 404, 15, 14, 120000000}, // Actual baud: 9602, error +0.02%, 1575 iterations

new: { 38400, 179, 11, 1, 120000000}, // Actual baud: 38408, error +0.02%, 120 iterations
old: { 38400, 101, 15, 14, 120000000}, // Actual baud: 38409, error +0.02%, 525 iterations

new: { 57600, 93, 5, 2, 120000000}, // Actual baud: 57604, error +0.01%, 120 iterations
old: { 57600, 68, 12, 11, 120000000}, // Actual baud: 57545, error -0.10%, 420 iterations

new: { 115200, 47, 13, 5, 120000000}, // Actual baud: 115248, error +0.04%, 120 iterations
old: { 115200, 34, 12, 11, 120000000}, // Actual baud: 115090, error -0.10%, 315 iterations

new: { 230400, 19, 7, 5, 120000000}, // Actual baud: 230263, error -0.06%, 120 iterations
old: { 230400, 17, 12, 11, 120000000}, // Actual baud: 230179, error -0.10%, 210 iterations

new: { 250000, 30, 1, 0, 120000000}, // Actual baud: 250000, error =0.00%, 1 iterations
old: { 250000, 30, 1, 0, 120000000}, // Actual baud: 250000, error =0.00%, 0 iterations

new: {1000000, 5, 2, 1, 120000000}, // Actual baud: 1000000, error =0.00%, 3 iterations
old: {1000000, 4, 8, 7, 120000000}, // Actual baud: 1000000, error =0.00%, 210 iterations

new: {2000000, 3, 4, 1, 120000000}, // Actual baud: 2000000, error =0.00%, 8 iterations
old: {2000000, 2, 8, 7, 120000000}, // Actual baud: 2000000, error =0.00%, 210 iterations

Uses 100% integer math, delivers more accurate results with fewer iterations in less time.

Since it seems most of the NXP chips use identical math, I have also altered the other targets in this commit.

If there are non-NXP chips which use a similar serial block, consider porting to those as well

Comparison:

         BAUD   DL MUL DIV  CPU_CLOCK
      -------  --- --- --- ----------
new: {   9600, 625,  1,  0,  96000000},	// Actual baud:    9600, error =0.00%,    1 iterations
old: {   9600, 625,  1,  0,  96000000},	// Actual baud:    9600, error =0.00%,    0 iterations

new: {  38400, 125,  4,  1,  96000000},	// Actual baud:   38400, error =0.00%,    8 iterations
old: {  38400,  81, 14, 13,  96000000},	// Actual baud:   38409, error +0.02%,  420 iterations

new: {  57600,  81,  7,  2,  96000000},	// Actual baud:   57613, error +0.02%,  120 iterations
old: {  57600,  54, 14, 13,  96000000},	// Actual baud:   57613, error +0.02%,  315 iterations

new: { 115200,  27, 14, 13,  96000000},	// Actual baud:  115226, error +0.02%,  120 iterations
old: { 115200,  27, 14, 13,  96000000},	// Actual baud:  115226, error +0.02%,  210 iterations

new: { 230400,  23, 15,  2,  96000000},	// Actual baud:  230179, error -0.10%,  120 iterations
old: { 230400,  17, 15,  8,  96000000},	// Actual baud:  230179, error -0.10%,  525 iterations

new: { 250000,  24,  1,  0,  96000000},	// Actual baud:  250000, error =0.00%,    1 iterations
old: { 250000,  24,  1,  0,  96000000},	// Actual baud:  250000, error =0.00%,    0 iterations

new: {1000000,   6,  1,  0,  96000000},	// Actual baud: 1000000, error =0.00%,    1 iterations
old: {1000000,   6,  1,  0,  96000000},	// Actual baud: 1000000, error =0.00%,    0 iterations

new: {2000000,   3,  1,  0,  96000000},	// Actual baud: 2000000, error =0.00%,    1 iterations
old: {2000000,   3,  1,  0,  96000000},	// Actual baud: 2000000, error =0.00%,    0 iterations

new: {   9600, 514, 15,  4, 100000000},	// Actual baud:    9600, error =0.00%,  110 iterations
old: {   9600, 337, 15, 14, 100000000},	// Actual baud:    9593, error -0.07%, 1365 iterations

new: {  38400,  93,  4,  3, 100000000},	// Actual baud:   38402, error +0.01%,  120 iterations
old: {  38400,  85, 12, 11, 100000000},	// Actual baud:   38363, error -0.10%,  525 iterations

new: {  57600,  62,  4,  3, 100000000},	// Actual baud:   57604, error +0.01%,  120 iterations
old: {  57600,  61,  9,  7, 100000000},	// Actual baud:   57633, error +0.06%,  840 iterations

new: { 115200,  31,  4,  3, 100000000},	// Actual baud:  115207, error +0.01%,  120 iterations
old: { 115200,  31,  4,  3, 100000000},	// Actual baud:  115207, error +0.01%,  525 iterations

new: { 230400,  19,  7,  3, 100000000},	// Actual baud:  230263, error -0.06%,  120 iterations
old: { 230400,  19,  7,  3, 100000000},	// Actual baud:  230263, error -0.06%,  735 iterations

new: { 250000,  25,  1,  0, 100000000},	// Actual baud:  250000, error =0.00%,    1 iterations
old: { 250000,  25,  1,  0, 100000000},	// Actual baud:  250000, error =0.00%,    0 iterations

new: {1000000,   5,  4,  1, 100000000},	// Actual baud: 1000000, error =0.00%,    8 iterations
old: {1000000,   5,  4,  1, 100000000},	// Actual baud: 1000000, error =0.00%,  315 iterations

new: {2000000,   2,  9,  5, 100000000},	// Actual baud: 2008929, error +0.45%,  120 iterations
old: {2000000,   2,  9,  5, 100000000},	// Actual baud: 2008929, error +0.45%,  315 iterations

new: {   9600, 625,  4,  1, 120000000},	// Actual baud:    9600, error =0.00%,    8 iterations
old: {   9600, 404, 15, 14, 120000000},	// Actual baud:    9602, error +0.02%, 1575 iterations

new: {  38400, 179, 11,  1, 120000000},	// Actual baud:   38408, error +0.02%,  120 iterations
old: {  38400, 101, 15, 14, 120000000},	// Actual baud:   38409, error +0.02%,  525 iterations

new: {  57600,  93,  5,  2, 120000000},	// Actual baud:   57604, error +0.01%,  120 iterations
old: {  57600,  68, 12, 11, 120000000},	// Actual baud:   57545, error -0.10%,  420 iterations

new: { 115200,  47, 13,  5, 120000000},	// Actual baud:  115248, error +0.04%,  120 iterations
old: { 115200,  34, 12, 11, 120000000},	// Actual baud:  115090, error -0.10%,  315 iterations

new: { 230400,  19,  7,  5, 120000000},	// Actual baud:  230263, error -0.06%,  120 iterations
old: { 230400,  17, 12, 11, 120000000},	// Actual baud:  230179, error -0.10%,  210 iterations

new: { 250000,  30,  1,  0, 120000000},	// Actual baud:  250000, error =0.00%,    1 iterations
old: { 250000,  30,  1,  0, 120000000},	// Actual baud:  250000, error =0.00%,    0 iterations

new: {1000000,   5,  2,  1, 120000000},	// Actual baud: 1000000, error =0.00%,    3 iterations
old: {1000000,   4,  8,  7, 120000000},	// Actual baud: 1000000, error =0.00%,  210 iterations

new: {2000000,   3,  4,  1, 120000000},	// Actual baud: 2000000, error =0.00%,    8 iterations
old: {2000000,   2,  8,  7, 120000000},	// Actual baud: 2000000, error =0.00%,  210 iterations
@bogdanm
Copy link
Contributor

bogdanm commented Nov 29, 2013

Hi,

Thanks for your contribution. Before we accept it, could you please sign the mbed contributor's agreement here:

http://mbed.org/contributor_agreement/

It's a required step for everybody who contributes code to mbed.

Thanks,
Bogdan

@triffid
Copy link
Contributor Author

triffid commented Nov 29, 2013

done :)

bogdanm added a commit that referenced this pull request Dec 2, 2013
…lculation

NXP targets: Improved baudrate calculation function
@bogdanm bogdanm merged commit 43e49e5 into ARMmbed:master Dec 2, 2013
@triffid triffid deleted the feature/NXP_improved-baudrate-calculation branch March 3, 2014 00:02
bridadan pushed a commit that referenced this pull request Jun 21, 2016
Resubmission of #92 but with SHA's in .lib files
yossi2le pushed a commit to yossi2le/mbed-os that referenced this pull request Jan 2, 2019
- "(uint32_t)APPLICATION_ADDRESS + 4" simply suppresses an warning from the
  compiler (related to doing arithmetic on void* pointers).
- "applicationResetHandler" is now static, to avoid placing it on the stack, since
  it is used after the stack pointer is set to the application's stack pointer.
- while(1) is needed for armcc. Without it, armcc insists to jump to the
  application's entry point only *after* restoring the context of
  "forwardControlToApplication":

    806c:       f380 8808       msr     MSP, r0
    8070:       481d            ldr     r0, [pc, ARMmbed#116]  ; (80e8 <forwardControlToApplication+0xe8>)
    8072:       6004            str     r4, [r0, #0]
    8074:       6828            ldr     r0, [r5, #0]
    8076:       e8bd 4070       ldmia.w sp!, {r4, r5, r6, lr}    ==> context restore
    807a:       4700            bx      r0                       ==> jump to application

This changes the application's initial stack pointer. Because of this, the
cloud client example doesn't start anymore when concatenated with the bootloader.

With "while(1);" in place, we get the right behaviour:

    806a:       f380 8808       msr     MSP, r0
    806e:       481d            ldr     r0, [pc, ARMmbed#116]  ; (80e4 <forwardControlToApplication+0xe4>)
    8070:       6004            str     r4, [r0, #0]
    8072:       6828            ldr     r0, [r5, #0]
    8074:       4780            blx     r0                      ==> jump to application

Tested with armcc 5.03 update 3. Also tested that the GCC compilation still works
with these changes in place.
artokin pushed a commit to artokin/mbed-os that referenced this pull request Feb 25, 2019
…cc3d

227cc3d Merge pull request ARMmbed#120 from ARMmbed/sync_with_mbedos_2
88894d1 Follow Mbed OS coding style
f3db9a1 (via Mbed OS) Add mbedtls platform setup and teardown to modules
5feb8dd Merge pull request ARMmbed#119 from ARMmbed/sync_with_MbedOS
10d5054 (via Mbed OS) Add missing mbed_lib.json for frameworks and nanostack
a344676 Remove references to yotta (ARMmbed#118)
610afda Remove excess tracing (ARMmbed#117)
53382d6 Merge pull request ARMmbed#116 from ARMmbed/IOTTHD-1608
15889cb Use Mbed OS coding style
d6eff5c Clarify function signature
587e8de Update message prevalidation API
46f86d4 Add API to set callback for CoAP msg prevalidation (ARMmbed#115)
0eb6630 Merge pull request ARMmbed#114 from ARMmbed/sync_with_mbed_os
f75732b (split) Add unit tests for TLSSocket and TLSSocketWrapper

git-subtree-dir: features/nanostack/coap-service
git-subtree-split: 227cc3d
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.

2 participants