Quellcode durchsuchen

Протестирована одновременная перестройка ad9912 и lmx2594

zaytsev.mikhail.olegovich@gmail.com vor 1 Jahr
Ursprung
Commit
ca7630f413
5 geänderte Dateien mit 277 neuen und 117 gelöschten Zeilen
  1. 123 99
      Devices/ad9912.c
  2. 5 5
      Devices/ad9912.h
  3. 129 10
      Devices/lmx2594.c
  4. 17 2
      Devices/lmx2594regs.h
  5. 3 1
      command.c

+ 123 - 99
Devices/ad9912.c

@@ -1,4 +1,5 @@
 #include "ad9912.h"
+#include <math.h>
 
  uint32_t ad9912regs[AD9912_COUNT] = {
         0x000018,
@@ -15,12 +16,12 @@
         0x010400,
         0x010500,
         0x010600,
-        0x01A6CD,
-        0x01A7CC,
-        0x01A8CC,
-        0x01A9CC,
-        0x01AACC,
-        0x01AB2C,
+        0x01A633,
+        0x01A733,
+        0x01A833,
+        0x01A933,
+        0x01AA33,
+        0x01AB33,
         0x01AC00,
         0x01AD00,
         0x020005,
@@ -59,101 +60,131 @@ void ad9912_init(void *bar1) {
 }
 /*----------------------------------------------------------------------*/
 
-double ad9912_set_main_band(double freq, int lmx_n, double f_pd) {
+double ad9912_set_main_band(double freq,double f_pd) {
     // Divide the frequncy by the old value of the phase detector frequency and only left with the integer part
     uint32_t N = (uint32_t) (freq/f_pd);
+    if (freq <= 12500e6) {
+        if (N < 28){
+            N= 28;
+        };
+    }
+    else if (freq > 12500e6) {
+        if (N <32) {
+            N = 32;
+        }
+    };
     printf("N = %d\n", N);
     // Calculate the new phase detector frequency by dividing the frequency by the new value of N
     f_pd = freq/N; // Phase detector frequency
-    printf("f_pd = %f\n", f_pd);
     return f_pd;
 }
 
-double ad9912_set_out_of_band(double freq,int lmx_n) {
-    double f_pd;
-
-    double f_vco; // VCO frequency
-    double vco_div = 7.5e9/freq;
-    int chan_div = 2;
-
-    if (vco_div > 2 && vco_div <= 4) {
-        chan_div = 4;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 4 && vco_div <= 6) {
-        chan_div = 6;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 6 && vco_div <=8) {
-        chan_div = 8;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 8 && vco_div <= 12) {
-        chan_div = 12;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 12 && vco_div <= 16) {
-        chan_div = 16;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 16 && vco_div <= 24) {
-        chan_div = 24;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 24 && vco_div <= 32) {
-        chan_div = 32;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 32 && vco_div <= 48) {
-        chan_div = 48;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 48 && vco_div <= 64) {
-        chan_div = 64;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 64 && vco_div <= 72) {
-        chan_div = 72;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 72 && vco_div <= 96) {
-        chan_div = 96;
-        f_vco = freq * chan_div;
+double ad9912_set_out_of_band(double freq,double f_pd) {
+    
+    printf("f_pd in out_of_band func = %f\n",f_pd);
+    printf("freq in func = %f\n",freq);
+    double lmx_freq;
+    if (freq >100e3 && freq <=1000e6) {
+        lmx_freq = 4000e6-freq;
     }
-    else if (vco_div > 96 && vco_div <= 128) {
-        chan_div = 128;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 128 && vco_div <= 192) {
-        chan_div = 192;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 192 && vco_div <= 256) {
-        chan_div = 256;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 256 && vco_div <= 384) {
-        chan_div = 384;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 384 && vco_div <= 512) {
-        chan_div = 512;
-        f_vco = freq * chan_div;
-    }
-    else if (vco_div > 512 && vco_div <= 768) {
-        chan_div = 768;
-        f_vco = freq * chan_div;
+    else {
+        lmx_freq = freq;
     }
-}
+    double f_vco = 2*lmx_freq; // VCO frequency
+    double vco_div = 7.5e9/lmx_freq;
+    int chan_div = 2;
 
-       // Divide the frequncy by the old value of the phase detector frequency and only left with the integer part
+    if (f_vco < 7.5e9) {
+        if (vco_div > 2 && vco_div <= 4)
+            chan_div = 4;  // 4
+        f_vco = lmx_freq * chan_div;
+        if (vco_div > 4 && vco_div <= 6) {
+            chan_div = 6;  // 6
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 6 && vco_div <= 8) {
+            chan_div = 8;  // 8
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 8 && vco_div <= 12) {
+            chan_div = 12;  // 12
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 12 && vco_div <= 16) {
+            chan_div = 16;  // 16
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 16 && vco_div <= 24) {
+            chan_div = 24;  // 24
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 24 && vco_div <= 32) {
+            chan_div = 32;  // 32
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 32 && vco_div <= 48) {
+            chan_div = 48;  // 48
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 48 && vco_div <= 64) {
+            chan_div = 64;  // 64
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 64 && vco_div <= 72) {
+            chan_div = 72;  // 72
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 72 && vco_div <= 96) {
+            chan_div = 96;  // 96
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 96 && vco_div <= 128) {
+            chan_div = 128;  // 128
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 128 && vco_div <= 192) {
+            chan_div = 192;  // 192
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 192 && vco_div <= 256) {
+            chan_div = 256;  // 256
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 256 && vco_div <= 384) {
+            chan_div = 384;  // 384
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 384 && vco_div <= 512) {
+            chan_div = 512;  // 512
+            f_vco = lmx_freq * chan_div;
+        }
+        if (vco_div > 512 && vco_div <= 768) {
+            chan_div = 768;  // 768
+            f_vco = lmx_freq * chan_div;
+        }
+    }
+
+    printf ("Chan_div in func = %d\n",chan_div);
+    printf("F_vco in func = %f\n",f_vco);
+    // Divide the frequncy by the old value of the phase detector frequency and only left with the integer part
     uint32_t N = (uint32_t) (f_vco/f_pd);
+    if (f_vco <= 12500e6) {
+        if (N < 28){
+            N= 28;
+        };
+    }
+    else if (f_vco > 12500e6) {
+        if (N <32) {
+            N = 32;
+        }
+    };
     printf("N = %d\n", N);
     // Calculate the new phase detector frequency by dividing the frequency by the new value of N
     f_pd = f_vco/N; // Phase detector frequency
-    printf("f_pd = %f\n", f_pd);
 
     return f_pd;
+}
+
 
 
 double ad9912_set(void *bar1, double freq, double f_pd) {
@@ -161,10 +192,10 @@ double ad9912_set(void *bar1, double freq, double f_pd) {
     int lmx_n = 50;
 
     if (freq >= 7500e6 && freq <= 15000e6) {
-        f_pd = ad9912_set_main_band(freq, fs, lmx_n);
+        f_pd = ad9912_set_main_band(freq,f_pd);
     }
     else if (freq >= 10e6 && freq < 7500e6) {
-        f_pd = ad9912_set_out_of_band(freq, fs, lmx_n);
+        f_pd = ad9912_set_out_of_band(freq,f_pd);
     }
     else {
         return -1;
@@ -175,27 +206,27 @@ double ad9912_set(void *bar1, double freq, double f_pd) {
     double ftw_word = (f_pd*pow(2, 48))/fs;
     // Split the FTW word between 6 registers
     // First one is ftw_word[7:0]
-    uint32_t ftw0_7_0 = (uint32_t) ftw_word;
+    uint8_t ftw0_7_0 = (uint8_t) ftw_word;
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] & ~BITM_AD9912_FTW0_FREQ_WORD_7_0;
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] | (ftw0_7_0 << BITP_AD9912_FTW0_FREQ_WORD_7_0);
     // Second one is ftw_word[15:8]
-    uint32_t ftw0_15_8 = (uint32_t) (ftw_word >> 8);
+    uint8_t ftw0_15_8 = (uint8_t) ((uint64_t)ftw_word >> 8);
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] & ~BITM_AD9912_FTW0_FREQ_WORD_15_8;
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] | (ftw0_15_8 << BITP_AD9912_FTW0_FREQ_WORD_15_8);
     // Third one is ftw_word[23:16]
-    uint32_t ftw0_23_16 = (uint32_t) (ftw_word >> 16);
+    uint8_t ftw0_23_16 = (uint32_t) ((uint64_t)ftw_word >> 16);
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] & ~BITM_AD9912_FTW0_FREQ_WORD_23_16;
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] | (ftw0_23_16 << BITP_AD9912_FTW0_FREQ_WORD_23_16);
     // Fourth one is ftw_word[31:24]
-    uint32_t ftw0_31_24 = (uint32_t) (ftw_word >> 24);
+    uint8_t ftw0_31_24 = (uint64_t) ((uint64_t)ftw_word >> 24);
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] & ~BITM_AD9912_FTW0_FREQ_WORD_31_24;
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] | (ftw0_31_24 << BITP_AD9912_FTW0_FREQ_WORD_31_24);
     // Fifth one is ftw_word[39:32]
-    uint32_t ftw1_39_32 = (uint32_t) (ftw_word >> 32);
+    uint8_t ftw1_39_32 = (uint32_t) ((uint64_t)ftw_word >> 32);
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] & ~BITM_AD9912_FTW0_FREQ_WORD_39_24;
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] | (ftw1_39_32<< BITP_AD9912_FTW0_FREQ_WORD_39_24);
     // Sixth one is ftw_word[47:40]
-    uint32_t ftw1_47_40 = (uint32_t) (ftw_word >> 40);
+    uint8_t ftw1_47_40 = (uint32_t) ((uint64_t)ftw_word >> 40);
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] & ~BITM_AD9912_FTW0_FREQ_WORD_47_40;
     ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] | (ftw1_47_40 << BITP_AD9912_FTW0_FREQ_WORD_47_40);
     // Set the frequency
@@ -218,13 +249,6 @@ double ad9912_set(void *bar1, double freq, double f_pd) {
         uint32_t *DDS_DATA = bar1 + AD9912_BASE_ADDR;
         *DDS_DATA = ad9912_ftw_regs[i];
     }
-    char filename[100];
-    sprintf(filename, "%f.txt", f_pd);
-    FILE * f = fopen(filename, "w");
-    for (int i = 0; i < sizeof(ad9912regs) / 4; i++) {
-        fprintf(f, "0x%08X\n", ad9912regs[i]);
-    }
-    fclose(f);
 
     return f_pd;
 

+ 5 - 5
Devices/ad9912.h

@@ -57,19 +57,19 @@
  * 										FTW0[39:32]
  *********************************************************************************/
 #define BITP_AD9912_FTW0_FREQ_WORD_39_24                        0
-#define BITM_AD9912_FTW0_FREQ_WORD_39_24                        (0xFF << BITP_AD9912_FTW1_FREQ_WORD_7_0)
+#define BITM_AD9912_FTW0_FREQ_WORD_39_24                        (0xFF << BITP_AD9912_FTW0_FREQ_WORD_7_0)
 #define REGP_AD9912_FTW0_FREQ_WORD_39_24                         0x12
 /**********************************************************************************
  * 										FTW0[47:40]
 *********************************************************************************/
 #define BITP_AD9912_FTW0_FREQ_WORD_47_40                        0
-#define BITM_AD9912_FTW0_FREQ_WORD_47_40                        (0xFF << BITP_AD9912_FTW1_FREQ_WORD_47_40)
+#define BITM_AD9912_FTW0_FREQ_WORD_47_40                        (0xFF << BITP_AD9912_FTW0_FREQ_WORD_47_40)
 #define REGP_AD9912_FTW0_FREQ_WORD_47_40                         0x13
 
 
 /*----------------------------------------------------------------------*/
 void ad9912_init(void *bar1);
-double ad9912_set(void *bar1, double freq);
-double ad9912_set_out_of_band(double freq, int lmx_n);
-double ad9912_set_main_band(double freq, int lmx_n);
+double ad9912_set(void *bar1, double freq, double f_pd);
+double ad9912_set_out_of_band(double freq,double f_pd); 
+double ad9912_set_main_band(double freq,double f_pd);
 #endif //DMADRIVER_AD9912_H

+ 129 - 10
Devices/lmx2594.c

@@ -359,9 +359,22 @@ int lmx_freq_set_main_band(void *bar1, double freq, double f_pd) {
 
 int lmx_freq_set_main_band_int_mode(void *bar1, double freq, double f_pd) {
 
-    uint32_t N_div;
+    double N_div;
+    printf("f_pd before = %f\n",f_pd);
     N_div = freq / f_pd;
 
+    uint32_t N = (uint32_t) N_div;
+     if (freq <= 12500e6) {
+        if (N < 28){
+            N= 28;
+        };
+    }
+    else if (freq > 12500e6) {
+        if (N <32) {
+            N = 32;
+        }
+    };
+
     int vco_core;
     double f_coremin;
     double f_coremax;
@@ -471,14 +484,54 @@ int lmx_freq_set_main_band_int_mode(void *bar1, double freq, double f_pd) {
         lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
         lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x2 << BITP_LMX2594_R37_PFD_DLY_SEL);
         printf("PFD_DLY_SEL = %d\n", 2);
+    };
+    int cal_clk_div;
+    //SET the FCAL_HPFD_ADJ
+    if (f_pd <= 100e6) {
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_LESS100MHZ;
+    }
+    else if (f_pd > 100e6 && f_pd <= 150e6) {
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_100_150MHZ;
+    }
+    else if (f_pd > 150e6 && f_pd <= 200e6) {
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_150_200MHZ;
+    }
+    else if (f_pd > 200e6) {
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_MORE200MHZ;
+    };
+    // SET the CAL_CLK_DIV value 
+    if (f_pd <= 200e6 ) {
+        lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
+        lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV1;
+        cal_clk_div = 0;
     }
+    else if (f_pd > 200e6 && f_pd <= 400e6) {
+        lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
+        lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV2;
+        cal_clk_div = 1;
+    }
+    else if (f_pd > 400e6 && f_pd < 800e6) {
+         lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
+        lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV4;
+        cal_clk_div = 2;
+    };
+    //Calculate the ACAL_CMP_DLY
+    double Fsmclk = f_pd/(pow(2,cal_clk_div));
+    uint8_t acal_cmp_dly = (uint8_t) ((uint64_t)round((Fsmclk)/10e6));
+    //Set the ACAL_CMP_DLY value
+    lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] &(~BITM_LMX2594_R4_ACAL_CMP_DLY);
+    lmx2594regs[112-R4_ADDR]  = lmx2594regs[112-R4_ADDR] | (acal_cmp_dly << BITP_LMX2594_R4_ACAL_CMP_DLY);
     // SET the N_DIV
-      lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
-    lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N_div >> 16);
+    lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
+    lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
     //CLear the lower 16 bits of the register
     lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
     // Next 16 bits of the register
-    lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N_div & 0xFFFF);
+    lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
     // Clear the SEG1_EN bit
     lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
     // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
@@ -494,6 +547,8 @@ int lmx_freq_set_main_band_int_mode(void *bar1, double freq, double f_pd) {
             lmx2594regs[112 - CAP_CTRL_START],
             lmx2594regs[112 - VCO_DACISET],
             lmx2594regs[112-PFD_DLY_SEL],
+            lmx2594regs[112-R4_ADDR],
+            lmx2594regs[112-R1_ADDR],
             lmx2594regs[112-CHDIV_DIV2],
             lmx2594regs[112-PLL_N_S],
             lmx2594regs[112-PLL_N_M],
@@ -517,6 +572,11 @@ int lmx_freq_set_main_band_int_mode(void *bar1, double freq, double f_pd) {
     }
     fclose(f);
 
+    printf("N_div = %f\n", N_div);
+    printf("N = %d\n", N);
+    printf("f_vco = %f\n", freq);
+    printf("SEG1_EN %08X\n",lmx2594regs[112 - CHDIV_DIV2]);
+
     return 0;
 
 }
@@ -1032,7 +1092,19 @@ int lmx_freq_set_out_of_band_int_mode(void *bar1, double freq, double f_pd) {
         ch_div_reg = 0;
         f_vco = lmx_freq * 2;
     }
-    uint32_t N_div = f_vco / f_pd;
+    double N_div = f_vco / f_pd;
+    uint32_t N = (uint32_t) N_div;
+     if (f_vco <= 12500e6) {
+        if (N < 28){
+            N= 28;
+        };
+    }
+    else if (f_vco > 12500e6) {
+        if (N <32) {
+            N = 32;
+        }
+    };
+
 
     // Partial assist for the calibration
 
@@ -1136,12 +1208,52 @@ int lmx_freq_set_out_of_band_int_mode(void *bar1, double freq, double f_pd) {
         lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x2 << BITP_LMX2594_R37_PFD_DLY_SEL);
         printf("PFD_DLY_SEL = %d\n", 2);
     }
+    if (f_pd <= 100e6) {
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_LESS100MHZ;
+    }
+    else if (f_pd > 100e6 && f_pd <= 150e6) {
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_100_150MHZ;
+    }
+    else if (f_pd > 150e6 && f_pd <= 200e6) {
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_150_200MHZ;
+    }
+    else if (f_pd > 200e6) {
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
+        lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_MORE200MHZ;
+    };
+    // SET the CAL_CLK_DIV value 
+    int cal_clk_div;
+    if (f_pd <= 200e6 ) {
+        lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
+        lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV1;
+        cal_clk_div =0;
+    }
+    else if (f_pd > 200e6 && f_pd <= 400e6) {
+        lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
+        lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV2;
+        cal_clk_div =1;
+    }
+    else if (f_pd > 400e6 && f_pd < 800e6) {
+         lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
+        lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV4;
+        cal_clk_div = 2;
+    };
+    //Calculate the ACAL_CMP_DLY
+    double Fsmclk = f_pd/(pow(2,cal_clk_div));
+    uint8_t acal_cmp_dly = (uint8_t) ((uint64_t)round((Fsmclk)/10e6));;
+    //Set the ACAL_CMP_DLY value
+    lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] &(~BITM_LMX2594_R4_ACAL_CMP_DLY);
+    lmx2594regs[112-R4_ADDR]  = lmx2594regs[112-R4_ADDR] | (acal_cmp_dly << BITP_LMX2594_R4_ACAL_CMP_DLY);
     // Set the N value
-    lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] & (~0xFFFF);
-    lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] | (N_div >> 16);
-    // Clear the lower 16 bits of the register
-    lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] & (~0xFFFF);
-    lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] | (N_div & 0xFFFF);
+    lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
+    lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
+    //CLear the lower 16 bits of the register
+    lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
+    // Next 16 bits of the register
+    lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
      // Program the CHDIV value
     lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
     // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
@@ -1168,6 +1280,8 @@ int lmx_freq_set_out_of_band_int_mode(void *bar1, double freq, double f_pd) {
         lmx2594regs[112 - CAP_CTRL_START],
         lmx2594regs[112 - VCO_DACISET],
         lmx2594regs[112-PFD_DLY_SEL],
+        lmx2594regs[112-R4_ADDR],
+        lmx2594regs[112-R1_ADDR],
         lmx2594regs[112 - PLL_N_S],
         lmx2594regs[112 - PLL_N_M],
         lmx2594regs[112 - CHDIV],
@@ -1193,6 +1307,11 @@ int lmx_freq_set_out_of_band_int_mode(void *bar1, double freq, double f_pd) {
         fprintf(f, "0x%08X\n", lmx2594regs[i]);
     }
     fclose(f);
+    printf("N_div = %f\n", N_div);
+    printf("f_vco = %f\n", f_vco);
+    printf("N = %d\n", N);
+    printf("chan_div = %d\n", chan_div);
+    printf("chan_div_reg = %d\n", ch_div_reg);
     return 0;
 }
 

+ 17 - 2
Devices/lmx2594regs.h

@@ -26,7 +26,11 @@
 //R17
 #define     VCO_DACISET                                 0x11
 //R14
-#define     CPG_REG                                     0xD
+#define     CPG_REG                                     0xE
+//R4
+#define     R4_ADDR                                     0x04
+//R1
+#define     R1_ADDR                                     0x01
 //R0
 #define     FCAL_ADDR                                   0x00
 
@@ -107,15 +111,26 @@
 #define BITM_LMX2594_R14_CPG                            (0x7 <<BITP_LMX2594_R14_CPG)
 #define ENUM_LMX2594_R14_CPG_TRISTATE                   (0x0<<BITP_LMX2594_R14_CPG)
 #define ENUM_LMX2594_R14_CPG_15ma                       (0x7<<BITP_LMX2594_R14_CPG)
+/**********************************************************************************
+ * 										R4
+ *********************************************************************************/
+#define BITP_LMX2594_R4_ACAL_CMP_DLY                       8
+#define BITM_LMX2594_R4_ACAL_CMP_DLY                       (0xFF<<BITP_LMX2594_R4_ACAL_CMP_DLY)
 /**********************************************************************************
  * 										R1
  *********************************************************************************/
-
+#define BITP_LMX2594_R1_CAL_CLK_DIV                     0
+#define BITM_LMX2594_R1_CAL_CLK_DIV                     (0x7 << BITP_LMX2594_R1_CAL_CLK_DIV)
+#define ENUM_LMX2594_R1_CAL_CLK_DIV1                    (0x0 << BITP_LMX2594_R1_CAL_CLK_DIV)
+#define ENUM_LMX2594_R1_CAL_CLK_DIV2                    (0x1 << BITP_LMX2594_R1_CAL_CLK_DIV)
+#define ENUM_LMX2594_R1_CAL_CLK_DIV4                    (0x2 << BITP_LMX2594_R1_CAL_CLK_DIV)
+#define ENUM_LMX2594_R1_CAL_CLK_DIV8                    (0x3 << BITP_LMX2594_R1_CAL_CLK_DIV)
 /**********************************************************************************
  * 										R0
  *********************************************************************************/
 
 #define BITP_LMX2594_R0_FCAL_HPFD_ADJ                   7
+#define BITM_LMX2594_RO_FCAL_HPFD_ADJ                   (0x3 << BITP_LMX2594_R0_FCAL_HPFD_ADJ)
 #define LMX2594_R0_FCAL_HPFD_ADJ                        (0x03 << BITP_LMX2594_R0_FCAL_HPFD_ADJ)
 #define ENUM_LMX2594_R0_FCAL_HPFD_ADJ_LESS100MHZ        (0x00 << BITP_LMX2594_R0_FCAL_HPFD_ADJ)
 #define ENUM_LMX2594_R0_FCAL_HPFD_ADJ_100_150MHZ        (0x01 << BITP_LMX2594_R0_FCAL_HPFD_ADJ)

+ 3 - 1
command.c

@@ -12,8 +12,9 @@
 
 #include "Devices/lmx2594.h"
 #include "Devices/dac8811.h"
+#include "Devices/ad9912.h"
 
-double f_pd = 175e6;
+double f_pd = 200e6;
 
 //Массив структур Command, который связывает строки команд с соответствующими функциями.
 Command commands[] = {
@@ -36,6 +37,7 @@ void handleFreqCmd(const char* recvBuff)
 
 	printf("Frequency: %.2f Hz\n", freq[0]);
 	f_pd = ad9912_set(bar1, freq[0], f_pd);
+	printf("f_pd frequency is set to %.6f MHz\n", f_pd/1e6);
     lmx_freq_set(bar1, freq[0], f_pd);
     printf("The frequency is set to %.2f MHz\n", freq[0]/1e6);
 }