Collectin code of 11 bit multiplication.
authorMartin Prudek <prudemar@fel.cvut.cz>
Sun, 10 May 2015 14:48:50 +0000 (16:48 +0200)
committerMartin Prudek <prudemar@fel.cvut.cz>
Sun, 10 May 2015 14:48:50 +0000 (16:48 +0200)
pmsm-control/test_sw/main_pmsm.c

index 6e539b2..a85e65f 100644 (file)
@@ -233,7 +233,19 @@ void * pos_monitor(void* param){
        }
        return (void*)0;
 }
-
+/*
+ * \brief
+ * Multiplication of 11 bit
+ */
+inline int16_t mult_cap(int32_t s,int d){
+       int j;
+       int res=0;
+       for(j=0;j!=11;j++){
+               /* multiplicate as if maximum sinus value was unity */
+               res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
+       }
+       return res;
+}
 inline
 int sin_commutator(int duty){
        #define DEGREE_60        715827883
@@ -243,64 +255,33 @@ int sin_commutator(int duty){
        #define DEGREE_300      3579139413
        uint32_t j,pos;
        int32_t sin;
-       uint16_t pwm;
        pos=rps.index_dist*4294967;
        if (duty>=0){   /*clockwise rotation*/
                /* 1st phase */
                sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
-                pwm=0;
-                for(j=0;j!=11;j++){
-                       /* multiplicate as if maximum sinus value was unity */
-                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
-                }
-                rps.pwm1=pwm;
+                rps.pwm1=mult_cap(sin, duty);
 
                 /* 2nd phase */
                 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
-                pwm=0;
-                for(j=0;j!=11;j++){
-                       /* multiplicate as if maximum sinus value was unity */
-                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
-                }
-                rps.pwm2=pwm;
+                rps.pwm2=mult_cap(sin, duty);;
 
                 /* 3rd phase */
                 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
-                pwm=0;
-                for(j=0;j!=11;j++){
-                       /* multiplicate as if maximum sinus value was unity */
-                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
-                }
-                rps.pwm3=pwm;
+                rps.pwm3=mult_cap(sin, duty);;
         }else{
                duty=-duty;
 
                /* 1st phase */
                sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
-                pwm=0;
-                for(j=0;j!=11;j++){
-                       /* multiplicate as if maximum sinus value was unity */
-                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
-                }
-                rps.pwm1=pwm;
+                rps.pwm1=mult_cap(sin, duty);
 
                 /* 2nd phase */
                 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
-                pwm=0;
-                for(j=0;j!=11;j++){
-                       /* multiplicate as if maximum sinus value was unity */
-                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
-                }
-                rps.pwm2=pwm;
+                rps.pwm2=mult_cap(sin, duty);
 
                 /* 3rd phase */
                 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
-                pwm=0;
-                for(j=0;j!=11;j++){
-                       /* multiplicate as if maximum sinus value was unity */
-                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
-                }
-                rps.pwm3=pwm;
+                rps.pwm3=mult_cap(sin, duty);
         }
         return 0;
 }