rtems-CVS-050303 i.MX/MX1/csb336 BUG and BUGFIX

Pavel Pisa ppisa4lists at pikron.com
Sun Mar 13 13:28:03 UTC 2005


Hello All,

I have found problems with clock and tick computation
on ARM CSB336. 

There is integer overflow in get_perclk1_freq() for our
PLL setup. I have solved it by using "long long" type.
I hope that it is allowed/normal to use "long long" in RTEMS.

I have added rounding to timer setup to achieve more
exact timing. 

I would be pleased, if changes are acceptable and could be
committed into official RTEMS tree. If you prefer and agree 
on GNAT entry, I fill that.

Patch is copied as attachment for the case, that it is
malformed in the body.

Thanks for help

                Pavel Pisa
        e-mail: pisa at cmp.felk.cvut.cz
        www:    http://cmp.felk.cvut.cz/~pisa
        work:   http://www.pikron.com



diff --minimal -r -u -P -p rtems/c/src/lib/libcpu/arm/mc9328mxl/clock/clockdrv.c.orig rtems/c/src/lib/libcpu/arm/mc9328mxl/clock/clockdrv.c
--- rtems/c/src/lib/libcpu/arm/mc9328mxl/clock/clockdrv.c.orig 2005-01-30 17:24:50.000000000 +0100
+++ rtems/c/src/lib/libcpu/arm/mc9328mxl/clock/clockdrv.c 2005-03-05 01:31:47.000000000 +0100
@@ -82,7 +82,7 @@ rtems_irq_connect_data clock_isr_data = 
         int cnt; \
         freq = get_perclk1_freq(); \
         printk("perclk1 freq is %d\n", freq); \
-        cnt = ((freq / 1000) * BSP_Configuration.microseconds_per_tick) / 1000;\
+        cnt = ((long long)freq * BSP_Configuration.microseconds_per_tick + 500000) / 1000000;\
         printk("cnt freq is %d\n", cnt); \
         MC9328MXL_TMR1_TCMP = cnt; \
         /* use PERCLK1 as input, enable timer */ \
diff --minimal -r -u -P -p rtems/c/src/lib/libbsp/arm/csb336/startup/bspstart.c.orig rtems/c/src/lib/libbsp/arm/csb336/startup/bspstart.c
--- rtems/c/src/lib/libbsp/arm/csb336/startup/bspstart.c.orig 2005-01-30 17:24:11.000000000 +0100
+++ rtems/c/src/lib/libbsp/arm/csb336/startup/bspstart.c 2005-03-05 01:53:34.000000000 +0100
@@ -126,7 +126,7 @@ void bsp_start_default( void )
     /*
      *  The following information is very useful when debugging.
      */
-#if 0
+#if 1
     printk( "work_space_size = 0x%x\n", 
             BSP_Configuration.work_space_size );
     printk( "maximum_extensions = 0x%x\n", 
@@ -153,12 +153,12 @@ void bsp_start_default( void )
 /* Calcuate the frequency for perclk1 */
 int get_perclk1_freq(void)
 {
-    int fin;
-    int fpll;
-    int pd;
-    int mfd;
-    int mfi;
-    int mfn;
+    unsigned int fin;
+    unsigned int fpll;
+    unsigned int pd;
+    unsigned int mfd;
+    unsigned int mfi;
+    unsigned int mfn;
     uint32_t reg;
     int perclk1;
 
@@ -187,13 +187,14 @@ int get_perclk1_freq(void)
     printk("mfd = %d\n", mfd);
     printk("mfi = %d\n", mfi);
     printk("mfn = %d\n", mfn);
-    printk("(fin * mfi) / (pd + 1) = %d\n", (fin * mfi) / (pd + 1));
-    printk("(fin * mfn) / ((pd + 1) * (mfd + 1)) = %d\n",
-           (fin * mfn) / ((pd + 1) * (mfd + 1)));
+    printk("rounded (fin * mfi) / (pd + 1) = %d\n", (fin * mfi) / (pd + 1));
+    printk("rounded (fin * mfn) / ((pd + 1) * (mfd + 1)) = %d\n",
+           ((long long)fin * mfn) / ((pd + 1) * (mfd + 1)));
 #endif
 
-    fpll = 2 * ( ((fin * mfi) / (pd + 1)) +
-                 ((fin * mfn) / ((pd + 1) * (mfd + 1))) );
+    fpll = 2 * ( ((fin * mfi  + (pd + 1) / 2) / (pd + 1)) +
+                 (((long long)fin * mfn + ((pd + 1) * (mfd + 1)) / 2) / 
+   ((pd + 1) * (mfd + 1))) );
 
     /* calculate the output of the PERCLK1 divider */
     reg = MC9328MXL_PLL_PCDR;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: rtems-csb336-clock-fix.diff
Type: text/x-diff
Size: 2713 bytes
Desc: not available
URL: <http://lists.rtems.org/pipermail/users/attachments/20050313/61b70efc/attachment-0001.bin>


More information about the users mailing list