public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot-Users] [PATCH] ColdFire: Fix UART baudrate formula
@ 2008-05-29 17:29 Tsi-Chung.Liew
  2008-05-29 17:37 ` Jerry Van Baren
  0 siblings, 1 reply; 5+ messages in thread
From: Tsi-Chung.Liew @ 2008-05-29 17:29 UTC (permalink / raw)
  To: u-boot

From: TsiChung Liew <Tsi-Chung.Liew@freescale.com>

Replace counter = bus_clk / (baudrate * 32) with
counter = ((bus_clk / 32) + (baudrate /2 )) / baudrate
formula

Signed-off-by: TsiChung Liew <Tsi-Chung.Liew@freescale.com>
---
 drivers/serial/mcfuart.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/serial/mcfuart.c b/drivers/serial/mcfuart.c
index 88f3eb1..75e85b7 100644
--- a/drivers/serial/mcfuart.c
+++ b/drivers/serial/mcfuart.c
@@ -63,8 +63,8 @@ int serial_init(void)
 	uart->umr = UART_UMR_SB_STOP_BITS_1;
 
 	/* Setting up BaudRate */
-	counter = (u32) (gd->bus_clk / (gd->baudrate));
-	counter >>= 5;
+	counter = (u32) (gd->bus_clk / 32) + (gd->baudrate / 2));
+	counter = counter / gd->baudrate;
 
 	/* write to CTUR: divide counter upper byte */
 	uart->ubg1 = (u8) ((counter & 0xff00) >> 8);
-- 
1.5.4.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [U-Boot-Users] [PATCH] ColdFire: Fix UART baudrate formula
  2008-05-29 17:29 Tsi-Chung.Liew
@ 2008-05-29 17:37 ` Jerry Van Baren
  0 siblings, 0 replies; 5+ messages in thread
From: Jerry Van Baren @ 2008-05-29 17:37 UTC (permalink / raw)
  To: u-boot

Tsi-Chung.Liew wrote:
> From: TsiChung Liew <Tsi-Chung.Liew@freescale.com>
> 
> Replace counter = bus_clk / (baudrate * 32) with
> counter = ((bus_clk / 32) + (baudrate /2 )) / baudrate
> formula

I assume it works. :-)

Your comment is a duplicate of the code.  A better comment would tell 
the reader *why* you made the change.  Suggestion...

The formula "counter = (u32) (gd->bus_clk / (gd->baudrate)) / 32" can 
generate the wrong divisor due to integer division truncation.  Round 
the calculated divisor value by adding 1/2 the baudrate before dividing 
by the baudrate.

Acked-by: Gerald Van Baren <vanbaren@cideas.com>

> Signed-off-by: TsiChung Liew <Tsi-Chung.Liew@freescale.com>
> ---
>  drivers/serial/mcfuart.c |    4 ++--
>  1 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/serial/mcfuart.c b/drivers/serial/mcfuart.c
> index 88f3eb1..75e85b7 100644
> --- a/drivers/serial/mcfuart.c
> +++ b/drivers/serial/mcfuart.c
> @@ -63,8 +63,8 @@ int serial_init(void)
>  	uart->umr = UART_UMR_SB_STOP_BITS_1;
>  
>  	/* Setting up BaudRate */
> -	counter = (u32) (gd->bus_clk / (gd->baudrate));
> -	counter >>= 5;
> +	counter = (u32) (gd->bus_clk / 32) + (gd->baudrate / 2));
> +	counter = counter / gd->baudrate;
>  
>  	/* write to CTUR: divide counter upper byte */
>  	uart->ubg1 = (u8) ((counter & 0xff00) >> 8);

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot-Users] [PATCH] ColdFire: Fix UART baudrate formula
@ 2008-05-29 20:26 Tsi-Chung.Liew
  2008-05-29 20:59 ` Kenneth Johansson
  0 siblings, 1 reply; 5+ messages in thread
From: Tsi-Chung.Liew @ 2008-05-29 20:26 UTC (permalink / raw)
  To: u-boot

From: TsiChung Liew <Tsi-Chung.Liew@freescale.com>

The formula "counter = (u32) (gd->bus_clk / gd->baudrate) / 32"
can generate the wrong divisor due to integer division truncation.
Round the calculated divisor value by adding 1/2 the baudrate
before dividing by the baudrate.

Signed-off-by: TsiChung Liew <Tsi-Chung.Liew@freescale.com>
Acked-by: Gerald Van Baren <vanbaren@cideas.com>
---
 drivers/serial/mcfuart.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/serial/mcfuart.c b/drivers/serial/mcfuart.c
index 88f3eb1..75e85b7 100644
--- a/drivers/serial/mcfuart.c
+++ b/drivers/serial/mcfuart.c
@@ -63,8 +63,8 @@ int serial_init(void)
 	uart->umr = UART_UMR_SB_STOP_BITS_1;
 
 	/* Setting up BaudRate */
-	counter = (u32) (gd->bus_clk / (gd->baudrate));
-	counter >>= 5;
+	counter = (u32) (gd->bus_clk / 32) + (gd->baudrate / 2));
+	counter = counter / gd->baudrate;
 
 	/* write to CTUR: divide counter upper byte */
 	uart->ubg1 = (u8) ((counter & 0xff00) >> 8);
-- 
1.5.4.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [U-Boot-Users] [PATCH] ColdFire: Fix UART baudrate formula
  2008-05-29 20:26 [U-Boot-Users] [PATCH] ColdFire: Fix UART baudrate formula Tsi-Chung.Liew
@ 2008-05-29 20:59 ` Kenneth Johansson
  0 siblings, 0 replies; 5+ messages in thread
From: Kenneth Johansson @ 2008-05-29 20:59 UTC (permalink / raw)
  To: u-boot


On Thu, 2008-05-29 at 15:26 -0500, Tsi-Chung.Liew wrote:
> From: TsiChung Liew <Tsi-Chung.Liew@freescale.com>
> 
> The formula "counter = (u32) (gd->bus_clk / gd->baudrate) / 32"
> can generate the wrong divisor due to integer division truncation.
> Round the calculated divisor value by adding 1/2 the baudrate
> before dividing by the baudrate.
> 
> Signed-off-by: TsiChung Liew <Tsi-Chung.Liew@freescale.com>
> Acked-by: Gerald Van Baren <vanbaren@cideas.com>
> ---
>  drivers/serial/mcfuart.c |    4 ++--
>  1 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/serial/mcfuart.c b/drivers/serial/mcfuart.c
> index 88f3eb1..75e85b7 100644
> --- a/drivers/serial/mcfuart.c
> +++ b/drivers/serial/mcfuart.c
> @@ -63,8 +63,8 @@ int serial_init(void)
>  	uart->umr = UART_UMR_SB_STOP_BITS_1;
>  
>  	/* Setting up BaudRate */
> -	counter = (u32) (gd->bus_clk / (gd->baudrate));
> -	counter >>= 5;
> +	counter = (u32) (gd->bus_clk / 32) + (gd->baudrate / 2));

And you compiled this ?? 

> +	counter = counter / gd->baudrate;
>  
>  	/* write to CTUR: divide counter upper byte */
>  	uart->ubg1 = (u8) ((counter & 0xff00) >> 8);

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot-Users] [PATCH] ColdFire: Fix UART baudrate formula
@ 2008-05-29 22:20 Tsi-Chung.Liew
  0 siblings, 0 replies; 5+ messages in thread
From: Tsi-Chung.Liew @ 2008-05-29 22:20 UTC (permalink / raw)
  To: u-boot

From: TsiChung Liew <Tsi-Chung.Liew@freescale.com>

The formula "counter = (u32) (gd->bus_clk / gd->baudrate) / 32"
can generate the wrong divisor due to integer division truncation.
Round the calculated divisor value by adding 1/2 the baudrate
before dividing by the baudrate.

Signed-off-by: TsiChung Liew <Tsi-Chung.Liew@freescale.com>
Acked-by: Gerald Van Baren <vanbaren@cideas.com>
---
 drivers/serial/mcfuart.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/serial/mcfuart.c b/drivers/serial/mcfuart.c
index 88f3eb1..5eb4f45 100644
--- a/drivers/serial/mcfuart.c
+++ b/drivers/serial/mcfuart.c
@@ -63,8 +63,8 @@ int serial_init(void)
 	uart->umr = UART_UMR_SB_STOP_BITS_1;
 
 	/* Setting up BaudRate */
-	counter = (u32) (gd->bus_clk / (gd->baudrate));
-	counter >>= 5;
+	counter = (u32) ((gd->bus_clk / 32) + (gd->baudrate / 2));
+	counter = counter / gd->baudrate;
 
 	/* write to CTUR: divide counter upper byte */
 	uart->ubg1 = (u8) ((counter & 0xff00) >> 8);
-- 
1.5.4.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-05-29 22:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-05-29 20:26 [U-Boot-Users] [PATCH] ColdFire: Fix UART baudrate formula Tsi-Chung.Liew
2008-05-29 20:59 ` Kenneth Johansson
  -- strict thread matches above, loose matches on Subject: below --
2008-05-29 22:20 Tsi-Chung.Liew
2008-05-29 17:29 Tsi-Chung.Liew
2008-05-29 17:37 ` Jerry Van Baren

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox