| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354 |
- #include "lmx2594.h"
- #include <math.h>
- const uint32_t lmx2594_rst[] = {
- 0x002516,
- 0x002514
- };
- uint32_t lmx2594regs[LMX_COUNT] = {
- 0x700000,
- 0x6F0000,
- 0x6E0000,
- 0x6D0000,
- 0x6C0000,
- 0x6B0000,
- 0x6A0000,
- 0x690021,
- 0x680000,
- 0x670000,
- 0x660000,
- 0x650011,
- 0x640000,
- 0x630000,
- 0x620000,
- 0x610888,
- 0x600000,
- 0x5F0000,
- 0x5E0000,
- 0x5D0000,
- 0x5C0000,
- 0x5B0000,
- 0x5A0000,
- 0x590000,
- 0x580000,
- 0x570000,
- 0x560000,
- 0x550000,
- 0x540000,
- 0x530000,
- 0x520000,
- 0x510000,
- 0x500000,
- 0x4F0000,
- 0x4E016F,
- 0x4D0000,
- 0x4C000C,
- 0x4B0800,
- 0x4A0000,
- 0x49003F,
- 0x480001,
- 0x470081,
- 0x46C350,
- 0x450000,
- 0x4403E8,
- 0x430000,
- 0x4201F4,
- 0x410000,
- 0x401388,
- 0x3F0000,
- 0x3E0322,
- 0x3D00A8,
- 0x3C03E8,
- 0x3B0001,
- 0x3A9001,
- 0x390020,
- 0x380000,
- 0x370000,
- 0x360000,
- 0x350000,
- 0x340820,
- 0x330080,
- 0x320000,
- 0x314180,
- 0x300300,
- 0x2F0300,
- 0x2E07FC,
- 0x2DC8DF,
- 0x2C1FA0,
- 0x2B0000,
- 0x2A0000,
- 0x290000,
- 0x280000,
- 0x2703E8,
- 0x260000,
- 0x250104,
- 0x240032,
- 0x230004,
- 0x220000,
- 0x211E21,
- 0x200393,
- 0x1F43EC,
- 0x1E318C,
- 0x1D318C,
- 0x1C0488,
- 0x1B0002,
- 0x1A0DB0,
- 0x190C2B,
- 0x18071A,
- 0x17007C,
- 0x160001,
- 0x150401,
- 0x14F848,
- 0x1327B7,
- 0x120064,
- 0x11012C,
- 0x100080,
- 0x0F064F,
- 0x0E1E40,
- 0x0D4000,
- 0x0C5001,
- 0x0B0018,
- 0x0A10D8,
- 0x090604,
- 0x082000,
- 0x0740B2,
- 0x06C802,
- 0x0500C8,
- 0x041243,
- 0x030642,
- 0x020500,
- 0x010808,
- 0x00251C
- };
- double lmx_freq; // Frequency of the LMX2594
- void lmx2594_init(void *bar1) {
- // Header for LMX Reset
- uint32_t *ptr_rst = bar1 + LMX_BASE_ADDR;
- *ptr_rst = LMX2594_RST_HEADER;
- // Reset Data
- for (int m = 0; m < (sizeof(lmx2594_rst))/4; m++) {
- uint32_t *ptr = bar1 + LMX_BASE_ADDR;
- *ptr = lmx2594_rst[m];
- }
- // Header for init data
- uint32_t *ptr = bar1 + LMX_BASE_ADDR;
- *ptr = InitLMX2594Header;
- // Init data
- for (int i = 0; i < LMX_COUNT; i++) {
- uint32_t *ptr = bar1 + LMX_BASE_ADDR;
- *ptr = lmx2594regs[i];
- }
- FILE * f = fopen("init.txt", "w");
- for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
- fprintf(f, "0x%08X\n", lmx2594regs[i]);
- }
- fclose(f);
- }
- /*-------------------------LMX2594 Frequency Set-------------------------*/
- int lmx_freq_set_main_band(void *bar1, double freq, double f_pd) {
- double N_div;
- N_div = freq / f_pd;
- int vco_core;
- double f_coremin;
- double f_coremax;
- int c_core_min;
- int c_core_max;
- int a_core_min;
- int a_core_max;
- uint16_t vco_cap_ctrl_strt;
- uint16_t vco_daciset_strt;
- // divide whole part and fractional part
- uint32_t N = (uint32_t) N_div;
- // In frac part there is separate denominator and numerator
- // If frac part is 0 then the denominator is 1000 and numerator is 0
- uint32_t frac_n = (uint32_t) ((N_div - N) * (4294967295-1));
- uint32_t frac_d = 4294967295-1;
- // If frac part is 0 then the denominator is 1000 and numerator is 0
- if (frac_n == 0) {
- frac_n = 0;
- frac_d = 1000;
- }
- // Partial assist for the calibration
- //Determine a VCO core and other parameters
- if (freq >= 7500e6 && freq <= 8600e6) {
- vco_core = 1;
- f_coremin = 7500e6;
- f_coremax = 8600e6;
- c_core_min = 164;
- c_core_max = 12;
- a_core_min = 299;
- a_core_max = 240;
- }
- else if (freq > 8600e6 && freq < 9800e6) {
- vco_core = 2;
- f_coremin = 8600e6;
- f_coremax = 9800e6;
- c_core_min = 165;
- c_core_max = 16;
- a_core_min = 356;
- a_core_max = 247;
- }
- else if (freq >= 9800e6 && freq <= 10800e6) {
- vco_core = 3;
- f_coremin = 9800e6;
- f_coremax = 10800e6;
- c_core_min = 158;
- c_core_max = 19;
- a_core_min = 324;
- a_core_max = 224;
- }
- else if (freq > 10800e6 && freq <= 12000e6) {
- vco_core = 4;
- f_coremin = 10800e6;
- f_coremax = 12000e6;
- c_core_min = 140;
- c_core_max = 0;
- a_core_min = 383;
- a_core_max = 244;
- }
- else if (freq > 12000e6 && freq <= 12900e6) {
- vco_core = 5;
- f_coremin = 12000e6;
- f_coremax = 12900e6;
- c_core_min = 183;
- c_core_max = 36;
- a_core_min = 205;
- a_core_max = 146;
- }
- else if (freq > 12900e6 && freq <= 13900e6) {
- vco_core = 6;
- f_coremin = 12900e6;
- f_coremax = 13900e6;
- c_core_min = 155;
- c_core_max = 6;
- a_core_min = 242;
- a_core_max = 163;
- }
- else if (freq > 13900e6 && freq <= 15000e6) {
- vco_core = 7;
- f_coremin = 13900e6;
- f_coremax = 15000e6;
- c_core_min = 175;
- c_core_max = 19;
- a_core_min = 323;
- a_core_max = 244;
- };
- if (freq >=11900e6 && freq <=12100e6) {
- vco_daciset_strt = 300;
- vco_core = 4;
- vco_cap_ctrl_strt = 1;
- }
- vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
- vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
-
- printf("VCO_CORE = %d\n", vco_core);
- printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
- printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
- // Calibration assist
- //Set the VCO_CORE
- lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
- lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
- // Set the VCO_CAP_CTRL
- lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
- lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
- // Set the VCO_DACISET
- lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
- lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
- // Recommended sequnce for changin freq
- // 1. Change the N-div value
- // 2. Change the PLL numerator and denominator
- // 3. Program FCAL_EN bit
- // Clear the required parts of the register
- lmx2594regs[112-MASH_ORDER] = lmx2594regs[112-MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
- // Set the MASH_ORDER to 3
- lmx2594regs[112-MASH_ORDER] = lmx2594regs[112-MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
- // Set PF_DLY_SEL to 3
- if (freq <= 10e9) {
- 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] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
- printf("PFD_DLY_SEL = %d\n", 3);
- }
- else if (freq > 10e9) {
- 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] | (0x4 << BITP_LMX2594_R37_PFD_DLY_SEL);
- printf("PFD_DLY_SEL = %d\n", 4);
- }
- 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);
- // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
- lmx2594regs[112-PLL_NUM_S] = lmx2594regs[112-PLL_NUM_S] & (~0xFFFF);
- lmx2594regs[112-PLL_NUM_S] = lmx2594regs[112-PLL_NUM_S] | (frac_n >> 16);
- // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
- lmx2594regs[112-PLL_NUM_M] = lmx2594regs[112-PLL_NUM_M] & (~0xFFFF);
- // Next 16 bits of the numerator
- lmx2594regs[112-PLL_NUM_M] = lmx2594regs[112-PLL_NUM_M] | (frac_n & 0xFFFF);
- // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
- lmx2594regs[112-PLL_DEN_S] = lmx2594regs[112-PLL_DEN_S] & (~0xFFFF);
- // most significant 16 bits of the denominator
- lmx2594regs[112-PLL_DEN_S] = lmx2594regs[112-PLL_DEN_S] | (frac_d >> 16);
- // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
- lmx2594regs[112-PLL_DEN_M] = lmx2594regs[112-PLL_DEN_M] & (~0xFFFF);
- // Next 16 bits of the denominator
- lmx2594regs[112-PLL_DEN_M] = lmx2594regs[112-PLL_DEN_M] | (frac_d & 0xFFFF);
- lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
- lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
- // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
- lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
- lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_VCO;
- // Program the FCAL_EN bit
- lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
- // Show the all the upper 16 bits of the register lmx2594regs[PLL_N_S]
- // Determine which regs are changed and send only those
- uint32_t lmx_change_freq_regs[] = {
- lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
- lmx2594regs[112 - VCO_SEL],
- lmx2594regs[112 - CAP_CTRL_START],
- lmx2594regs[112 - VCO_DACISET],
- lmx2594regs[112-MASH_ORDER],
- lmx2594regs[112-PFD_DLY_SEL],
- lmx2594regs[112-PLL_N_S],
- lmx2594regs[112-PLL_N_M],
- lmx2594regs[112-PLL_DEN_S],
- lmx2594regs[112-PLL_DEN_M],
- lmx2594regs[112-PLL_NUM_S],
- lmx2594regs[112-PLL_NUM_M],
- lmx2594regs[112 - CHDIV],
- lmx2594regs[112 - CHDIV_DIV2],
- lmx2594regs[112-OUTA_MUX],
- lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
- lmx2594regs[112-FCAL_ADDR]
- };
- // Create a header for the LMX2594 with the appropriate number of words
- uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs)/4) << 1) | 1);
- uint32_t *ptr = bar1 + LMX_BASE_ADDR;
- *ptr = LMX_HEADER;
- for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
- uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
- *data_ptr = lmx_change_freq_regs[i];
- }
- char filename[100];
- sprintf(filename, "%f.txt", freq);
- FILE * f = fopen(filename, "w");
- for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
- fprintf(f, "0x%08X\n", lmx2594regs[i]);
- }
- fclose(f);
- printf("File has been written\n");
- printf("N_div = %f\n", N_div);
- printf("f_vco = %f\n", freq);
- printf("SEG1_EN %08X\n",lmx2594regs[112 - CHDIV_DIV2]);
- printf("N = %d\n", N);
- printf("frac_n = %u\n", frac_n);
- printf("frac_d = %u\n", frac_d);
- }
- int lmx_freq_set_main_band_int_mode(void *bar1, double freq, double f_pd) {
- 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;
- int c_core_min;
- int c_core_max;
- int a_core_min;
- int a_core_max;
- uint16_t vco_cap_ctrl_strt;
- uint16_t vco_daciset_strt;
- // Partial assist for the calibration
- //Determine a VCO core and other parameters
- if (freq >= 7500e6 && freq <= 8600e6) {
- vco_core = 1;
- f_coremin = 7500e6;
- f_coremax = 8600e6;
- c_core_min = 164;
- c_core_max = 12;
- a_core_min = 299;
- a_core_max = 240;
- }
- else if (freq > 8600e6 && freq < 9800e6) {
- vco_core = 2;
- f_coremin = 8600e6;
- f_coremax = 9800e6;
- c_core_min = 165;
- c_core_max = 16;
- a_core_min = 356;
- a_core_max = 247;
- }
- else if (freq >= 9800e6 && freq <= 10800e6) {
- vco_core = 3;
- f_coremin = 9800e6;
- f_coremax = 10800e6;
- c_core_min = 158;
- c_core_max = 19;
- a_core_min = 324;
- a_core_max = 224;
- }
- else if (freq > 10800e6 && freq <= 12000e6) {
- vco_core = 4;
- f_coremin = 10800e6;
- f_coremax = 12000e6;
- c_core_min = 140;
- c_core_max = 0;
- a_core_min = 383;
- a_core_max = 244;
- }
- else if (freq > 12000e6 && freq <= 12900e6) {
- vco_core = 5;
- f_coremin = 12000e6;
- f_coremax = 12900e6;
- c_core_min = 183;
- c_core_max = 36;
- a_core_min = 205;
- a_core_max = 146;
- }
- else if (freq > 12900e6 && freq <= 13900e6) {
- vco_core = 6;
- f_coremin = 12900e6;
- f_coremax = 13900e6;
- c_core_min = 155;
- c_core_max = 6;
- a_core_min = 242;
- a_core_max = 163;
- }
- else if (freq > 13900e6 && freq <= 15000e6) {
- vco_core = 7;
- f_coremin = 13900e6;
- f_coremax = 15000e6;
- c_core_min = 175;
- c_core_max = 19;
- a_core_min = 323;
- a_core_max = 244;
- };
- if (freq >=11900e6 && freq <=12100e6) {
- vco_daciset_strt = 300;
- vco_core = 4;
- vco_cap_ctrl_strt = 1;
- }
- vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
- vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
-
- printf("VCO_CORE = %d\n", vco_core);
- printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
- printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
- //Set the VCO_CORE
- lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
- lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
- // Set the VCO_CAP_CTRL
- lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
- lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
- // Set the VCO_DACISET
- lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
- lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
- // Set the PF_DLY_SEL
- if (freq <= 12500e6) {
- 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] | (0x1 << BITP_LMX2594_R37_PFD_DLY_SEL);
- printf("PFD_DLY_SEL = %d\n", 1);
- }
- else if (freq > 12500e6) {
- 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 >> 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);
- // 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;
- lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
- lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_VCO;
- // Program the FCAL_EN bit
- lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_R0_FCAL);
- lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
- uint32_t lmx_change_freq_regs[] = {
- lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
- lmx2594regs[112 - VCO_SEL],
- 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],
- lmx2594regs[112 - OUTA_MUX],
- lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
- lmx2594regs[112-FCAL_ADDR]
- };
- // Create a header for the LMX2594 with the appropriate number of words
- uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs)/4) << 1) | 1);
- uint32_t *ptr = bar1 + LMX_BASE_ADDR;
- *ptr = LMX_HEADER;
- for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
- uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
- *data_ptr = lmx_change_freq_regs[i];
- }
- char filename[100];
- sprintf(filename, "%f.txt", freq);
- FILE * f = fopen(filename, "w");
- for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
- fprintf(f, "0x%08X\n", lmx2594regs[i]);
- }
- 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;
- }
- int lmx_freq_set_out_of_band(void *bar1, double freq, double f_pd) {
- if (freq >= 10e6 && freq <= 1000e6) {
- lmx_freq = lmx_lower_bond_set(freq, f_pd);
- }
- else {
- lmx_freq = freq;
- }
- double f_vco = 2 * lmx_freq;
- int chan_div = 2;
- uint8_t ch_div_reg = 0; // 2
- double vco_div = 7.5e9 / lmx_freq;
- double N_div;
- int vco_core;
- double f_coremin;
- double f_coremax;
- int c_core_min;
- int c_core_max;
- int a_core_min;
- int a_core_max;
- uint16_t vco_cap_ctrl_strt;
- uint16_t vco_daciset_strt;
- // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
- 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;
- }
- switch (chan_div) {
- case 2:
- ch_div_reg = 0;
- break;
- case 4:
- ch_div_reg = 1;
- break;
- case 6:
- ch_div_reg = 2;
- break;
- case 8:
- ch_div_reg = 3;
- break;
- case 12:
- ch_div_reg = 4;
- break;
- case 16:
- ch_div_reg = 5;
- break;
- case 24:
- ch_div_reg = 6;
- break;
- case 32:
- ch_div_reg = 7;
- break;
- case 48:
- ch_div_reg = 8;
- break;
- case 64:
- ch_div_reg = 9;
- break;
- case 72:
- ch_div_reg = 10;
- break;
- case 96:
- ch_div_reg = 11;
- break;
- case 128:
- ch_div_reg = 12;
- break;
- case 192:
- ch_div_reg = 13;
- break;
- case 256:
- ch_div_reg = 14;
- break;
- case 384:
- ch_div_reg = 15;
- break;
- case 512:
- ch_div_reg = 16;
- break;
- case 768:
- ch_div_reg = 17;
- break;
- }
- } else {
- ch_div_reg = 0;
- f_vco = lmx_freq * 2;
- }
- N_div = f_vco / f_pd;
- // divide whole part and fractional part
- uint32_t N = (uint32_t) N_div;
- uint32_t frac_n = (uint32_t) ((N_div - N) * (4294967295-1));
- uint32_t frac_d = 4294967295-1;
- // If frac part is 0 then the denominator is 1000 and numerator is 0
- if (frac_n == 0) {
- frac_n = 0;
- frac_d = 1000;
- }
- // Partial assist for the calibration
- //Determine a VCO core and other parameters
- if (f_vco >= 7500e6 && f_vco <= 8600e6) {
- vco_core = 1;
- f_coremin = 7500e6;
- f_coremax = 8600e6;
- c_core_min = 164;
- c_core_max = 12;
- a_core_min = 299;
- a_core_max = 240;
- }
- else if (f_vco > 8600e6 && f_vco < 9800e6) {
- vco_core = 2;
- f_coremin = 8600e6;
- f_coremax = 9800e6;
- c_core_min = 165;
- c_core_max = 16;
- a_core_min = 356;
- a_core_max = 247;
- }
- else if (f_vco >= 9800e6 && f_vco <= 10800e6) {
- vco_core = 3;
- f_coremin = 9800e6;
- f_coremax = 10800e6;
- c_core_min = 158;
- c_core_max = 19;
- a_core_min = 324;
- a_core_max = 224;
- }
- else if (f_vco > 10800e6 && f_vco <= 12000e6) {
- vco_core = 4;
- f_coremin = 10800e6;
- f_coremax = 12000e6;
- c_core_min = 140;
- c_core_max = 0;
- a_core_min = 383;
- a_core_max = 244;
- }
- else if (f_vco > 12000e6 && f_vco <= 12900e6) {
- vco_core = 5;
- f_coremin = 12000e6;
- f_coremax = 12900e6;
- c_core_min = 183;
- c_core_max = 36;
- a_core_min = 205;
- a_core_max = 146;
- }
- else if (f_vco > 12900e6 && f_vco <= 13900e6) {
- vco_core = 6;
- f_coremin = 12900e6;
- f_coremax = 13900e6;
- c_core_min = 155;
- c_core_max = 6;
- a_core_min = 242;
- a_core_max = 163;
- }
- else if (f_vco > 13900e6 && f_vco <= 15000e6) {
- vco_core = 7;
- f_coremin = 13900e6;
- f_coremax = 15000e6;
- c_core_min = 175;
- c_core_max = 19;
- a_core_min = 323;
- a_core_max = 244;
- };
- vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
- vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
-
- if (f_vco >=11900e6 && f_vco <=12100e6) {
- vco_daciset_strt = 300;
- vco_core = 4;
- vco_cap_ctrl_strt = 1;
- }
- printf("VCO_CORE = %d\n", vco_core);
- printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
- printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
- // Calibration assist
- //Set the VCO_CORE
- lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
- lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
- // Set the VCO_CAP_CTRL_START
- lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
- lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
- // Set the VCO_DACISET
- lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
- lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
- lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
- // Set the MASH_ORDER to 3
- lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
- // Set PF_DLY_SEL to appropriate value
- if (f_vco <=10e9){
- 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] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
- printf("PFD_DLY_SEL = %d\n", 3);
- }
- else if (f_vco > 10e9) {
- 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] | (0x4 << BITP_LMX2594_R37_PFD_DLY_SEL);
- printf("PFD_DLY_SEL = %d\n", 4);
- }
- 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);
- // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
- lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] & (~0xFFFF);
- lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] | (frac_n >> 16);
- // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
- lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] & (~0xFFFF);
- // Next 16 bits of the numerator
- lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] | (frac_n & 0xFFFF);
- // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
- lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] & (~0xFFFF);
- // most significant 16 bits of the denominator
- lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] | (frac_d >> 16);
- // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
- lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] & (~0xFFFF);
- // Next 16 bits of the denominator
- lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] | (frac_d & 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
- lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
- // If the ch_div > 2 then set the SEG1_EN bit
- if (chan_div > 2) {
- lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
- lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
- }
- else {
- 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;
- lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
- lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
- // Program the FCAL_EN bit
- lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
- uint32_t lmx_change_freq_regs[] = {
- lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
- lmx2594regs[112 - VCO_SEL],
- lmx2594regs[112 - CAP_CTRL_START],
- lmx2594regs[112 - VCO_DACISET],
- lmx2594regs[112-MASH_ORDER],
- lmx2594regs[112-PFD_DLY_SEL],
- lmx2594regs[112 - PLL_N_S],
- lmx2594regs[112 - PLL_N_M],
- lmx2594regs[112 - PLL_DEN_S],
- lmx2594regs[112 - PLL_DEN_M],
- lmx2594regs[112 - PLL_NUM_S],
- lmx2594regs[112 - PLL_NUM_M],
- lmx2594regs[112 - CHDIV],
- lmx2594regs[112 - CHDIV_DIV2],
- lmx2594regs[112 - OUTA_MUX],
- lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
- lmx2594regs[112 - FCAL_ADDR]
- };
- // Create a header for the LMX2594 with the appropriate number of words
- uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs) / 4) << 1) | 1);
- uint32_t *ptr = bar1 + LMX_BASE_ADDR;
- *ptr = LMX_HEADER;
- // Send the data
- for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
- uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
- *data_ptr = lmx_change_freq_regs[i];
- }
- char filename[100];
- sprintf(filename, "%f.txt", freq);
- FILE * f = fopen(filename, "w");
- for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
- fprintf(f, "0x%08X\n", lmx2594regs[i]);
- }
- fclose(f);
- printf("N_div = %f\n", N_div);
- printf("f_vco = %f\n", f_vco);
- printf("SEG1_EN %08X\n",lmx2594regs[112 - CHDIV_DIV2]);
- printf("N = %d\n", N);
- printf("frac_n = %u\n", frac_n);
- printf("frac_d = %u\n", frac_d);
- printf("chan_div = %d\n", chan_div);
- printf("chan_div_reg = %d\n", ch_div_reg);
- printf("LMX2594 Registers\n");
- return 0;
- }
- int lmx_freq_set_out_of_band_int_mode(void *bar1, double freq, double f_pd) {
- if (freq >= 10e6 && freq <= 1000e6) {
- lmx_freq = lmx_lower_bond_set(freq, f_pd);
- }
- else {
- lmx_freq = freq;
- }
- double f_vco = 2 * lmx_freq;
- int chan_div = 2;
- uint8_t ch_div_reg = 0; // 2
- double vco_div = 7.5e9 / lmx_freq;
- int vco_core;
- double f_coremin;
- double f_coremax;
- int c_core_min;
- int c_core_max;
- int a_core_min;
- int a_core_max;
- uint16_t vco_cap_ctrl_strt;
- uint16_t vco_daciset_strt;
- // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
- 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;
- }
- switch (chan_div) {
- case 2:
- ch_div_reg = 0;
- break;
- case 4:
- ch_div_reg = 1;
- break;
- case 6:
- ch_div_reg = 2;
- break;
- case 8:
- ch_div_reg = 3;
- break;
- case 12:
- ch_div_reg = 4;
- break;
- case 16:
- ch_div_reg = 5;
- break;
- case 24:
- ch_div_reg = 6;
- break;
- case 32:
- ch_div_reg = 7;
- break;
- case 48:
- ch_div_reg = 8;
- break;
- case 64:
- ch_div_reg = 9;
- break;
- case 72:
- ch_div_reg = 10;
- break;
- case 96:
- ch_div_reg = 11;
- break;
- case 128:
- ch_div_reg = 12;
- break;
- case 192:
- ch_div_reg = 13;
- break;
- case 256:
- ch_div_reg = 14;
- break;
- case 384:
- ch_div_reg = 15;
- break;
- case 512:
- ch_div_reg = 16;
- break;
- case 768:
- ch_div_reg = 17;
- break;
- }
- } else {
- ch_div_reg = 0;
- f_vco = lmx_freq * 2;
- }
- 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
- //Determine a VCO core and other parameters
- if (f_vco >= 7500e6 && f_vco <= 8600e6) {
- vco_core = 1;
- f_coremin = 7500e6;
- f_coremax = 8600e6;
- c_core_min = 164;
- c_core_max = 12;
- a_core_min = 299;
- a_core_max = 240;
- }
- else if (f_vco > 8600e6 && f_vco < 9800e6) {
- vco_core = 2;
- f_coremin = 8600e6;
- f_coremax = 9800e6;
- c_core_min = 165;
- c_core_max = 16;
- a_core_min = 356;
- a_core_max = 247;
- }
- else if (f_vco >= 9800e6 && f_vco <= 10800e6) {
- vco_core = 3;
- f_coremin = 9800e6;
- f_coremax = 10800e6;
- c_core_min = 158;
- c_core_max = 19;
- a_core_min = 324;
- a_core_max = 224;
- }
- else if (f_vco > 10800e6 && f_vco <= 12000e6) {
- vco_core = 4;
- f_coremin = 10800e6;
- f_coremax = 12000e6;
- c_core_min = 140;
- c_core_max = 0;
- a_core_min = 383;
- a_core_max = 244;
- }
- else if (f_vco > 12000e6 && f_vco <= 12900e6) {
- vco_core = 5;
- f_coremin = 12000e6;
- f_coremax = 12900e6;
- c_core_min = 183;
- c_core_max = 36;
- a_core_min = 205;
- a_core_max = 146;
- }
- else if (f_vco > 12900e6 && f_vco <= 13900e6) {
- vco_core = 6;
- f_coremin = 12900e6;
- f_coremax = 13900e6;
- c_core_min = 155;
- c_core_max = 6;
- a_core_min = 242;
- a_core_max = 163;
- }
- else if (f_vco > 13900e6 && f_vco <= 15000e6) {
- vco_core = 7;
- f_coremin = 13900e6;
- f_coremax = 15000e6;
- c_core_min = 175;
- c_core_max = 19;
- a_core_min = 323;
- a_core_max = 244;
- };
- vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
- vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
-
- if (f_vco >=11900e6 && f_vco <=12100e6) {
- vco_daciset_strt = 300;
- vco_core = 4;
- vco_cap_ctrl_strt = 1;
- }
- printf("VCO_CORE = %d\n", vco_core);
- printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
- printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
- // Calibration assist
- //Set the VCO_CORE
- lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
- lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
- // Set the VCO_CAP_CTRL_START
- lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
- lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
- // Set the VCO_DACISET
- lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
- lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
- // Set the PFD_DLY_SEL to appropriate value
- if (freq <= 12500e6) {
- 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] | (0x1 << BITP_LMX2594_R37_PFD_DLY_SEL);
- printf("PFD_DLY_SEL = %d\n", 1);
- }
- else if (freq > 12500e6) {
- 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);
- }
- 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 >> 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
- lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
- // If the ch_div > 2 then set the SEG1_EN bit
- if (chan_div > 2) {
- lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
- lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
- }
- else {
- 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;
- lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
- lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
- // Program the FCAL_EN bit
- lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] & (~LMX2594_R0_FCAL_EN);
- lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
- uint32_t lmx_change_freq_regs []={
- lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
- lmx2594regs[112 - VCO_SEL],
- 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],
- lmx2594regs[112 - CHDIV_DIV2],
- lmx2594regs[112 - OUTA_MUX],
- lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
- lmx2594regs[112 - FCAL_ADDR]
- };
- // Create a header for the LMX2594 with the appropriate number of words
- uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs) / 4) << 1) | 1);
- uint32_t *ptr = bar1 + LMX_BASE_ADDR;
- *ptr = LMX_HEADER;
- // Send the data
- for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
- uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
- *data_ptr = lmx_change_freq_regs[i];
- }
- char filename[100];
- sprintf(filename, "%f.txt", freq);
- FILE * f = fopen(filename, "w");
- for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
- 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;
- }
- double lmx_lower_bond_set (double freq, double f_pd) {
- double f_max2870 = 4e9;
- double lmx_req_freq = f_max2870-freq; // 4 GHz - freq
- return lmx_req_freq;
- }
- int lmx_freq_set(void *bar1, double freq,double f_pd) {
- // double f_pd = 175e6;
- double N_div = 0;
- if (freq < 10e6 || freq > 15e9) {
- printf("Frequency range is 10 MHz to 15 GHz\n");
- return -1;
- }
- // if the frequency is in the main band - 7.5 GHz to 15 GHz
- if (freq >= 7.5e9 && freq <= 15e9) {
- // lmx_freq_set_main_band(bar1, freq, f_pd);
- lmx_freq_set_main_band_int_mode(bar1, freq, f_pd);
- }
- else if (freq < 7.5e9) {
- // lmx_freq_set_out_of_band(bar1, freq, f_pd);
- lmx_freq_set_out_of_band_int_mode(bar1, freq, f_pd);
- }
- // Switch the keys
- key_switch(bar1, freq,lmx_freq);
- return 0;
- }
- uint32_t lmx_ld_status(void *bar1) {
- uint32_t *read_ptr = (uint32_t *)(bar1 + LMX_LD_STATUS_ADDR);
- uint32_t read_value = *read_ptr;
- return read_value;
- }
|