mbedmicro/mbed — pull request #116 by triffid (2013-11-28 15:54:43+00:00)
Title: NXP targets: Improved baudrate calculation function

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