lmx2594.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #include "lmx2594.h"
  2. const uint32_t lmx2594_rst[2] = {
  3. 0x00251e,
  4. 0x00251c
  5. };
  6. uint32_t lmx2594regs[LMX_COUNT] = {
  7. 0x700000,
  8. 0x6F0000,
  9. 0x6E0000,
  10. 0x6D0000,
  11. 0x6C0000,
  12. 0x6B0000,
  13. 0x6A0000,
  14. 0x690021,
  15. 0x680000,
  16. 0x670000,
  17. 0x660000,
  18. 0x650011,
  19. 0x640000,
  20. 0x630000,
  21. 0x620000,
  22. 0x610888,
  23. 0x600000,
  24. 0x5F0000,
  25. 0x5E0000,
  26. 0x5D0000,
  27. 0x5C0000,
  28. 0x5B0000,
  29. 0x5A0000,
  30. 0x590000,
  31. 0x580000,
  32. 0x570000,
  33. 0x560000,
  34. 0x550000,
  35. 0x540000,
  36. 0x530000,
  37. 0x520000,
  38. 0x510000,
  39. 0x500000,
  40. 0x4F0000,
  41. 0x4E0105,
  42. 0x4D0000,
  43. 0x4C000C,
  44. 0x4B0C40,
  45. 0x4A0000,
  46. 0x49003F,
  47. 0x480001,
  48. 0x470081,
  49. 0x46C350,
  50. 0x450000,
  51. 0x4403E8,
  52. 0x430000,
  53. 0x4201F4,
  54. 0x410000,
  55. 0x401388,
  56. 0x3F0000,
  57. 0x3E0322,
  58. 0x3D00A8,
  59. 0x3C03E8,
  60. 0x3B0001,
  61. 0x3A9001,
  62. 0x390020,
  63. 0x380000,
  64. 0x370000,
  65. 0x360000,
  66. 0x350000,
  67. 0x340820,
  68. 0x330080,
  69. 0x320000,
  70. 0x314180,
  71. 0x300300,
  72. 0x2F0300,
  73. 0x2E07FD,
  74. 0x2DC8DF,
  75. 0x2C1F20,
  76. 0x2B0000,
  77. 0x2A0000,
  78. 0x290000,
  79. 0x280000,
  80. 0x2703E8,
  81. 0x260000,
  82. 0x250104,
  83. 0x240032,
  84. 0x230004,
  85. 0x220000,
  86. 0x211E21,
  87. 0x200393,
  88. 0x1F43EC,
  89. 0x1E318C,
  90. 0x1D318C,
  91. 0x1C0488,
  92. 0x1B0002,
  93. 0x1A0DB0,
  94. 0x190C2B,
  95. 0x18071A,
  96. 0x17007C,
  97. 0x160001,
  98. 0x150401,
  99. 0x14D848,
  100. 0x1327B7,
  101. 0x120064,
  102. 0x110130,
  103. 0x100080,
  104. 0x0F064F,
  105. 0x0E1E40,
  106. 0x0D4000,
  107. 0x0C5001,
  108. 0x0B0018,
  109. 0x0A10D8,
  110. 0x090604,
  111. 0x082000,
  112. 0x0740B2,
  113. 0x06C802,
  114. 0x0500C8,
  115. 0x041443,
  116. 0x030642,
  117. 0x020500,
  118. 0x01080B,
  119. 0x00251C
  120. };
  121. void lmx2594_init(void *bar1) {
  122. // Header for LMX Reset
  123. uint32_t *ptr_rst = bar1 + LMX_BASE_ADDR;
  124. *ptr_rst = LMX2594_RST_HEADER;
  125. // Reset Data
  126. for (int m = 0; m < 2; m++) {
  127. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  128. *ptr = lmx2594_rst[m];
  129. }
  130. // Header for init data
  131. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  132. *ptr = InitLMX2594Header;
  133. // Init data
  134. for (int i = 0; i < LMX_COUNT; i++) {
  135. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  136. *ptr = lmx2594regs[i];
  137. }
  138. }
  139. /*-------------------------LMX2594 Frequency Set-------------------------*/
  140. int lmx_freq_set_main_band(void *bar1, double freq, double f_pd) {
  141. double N_div;
  142. N_div = freq / f_pd;
  143. // divide whole part and fractional part
  144. uint32_t N = (uint32_t) N_div;
  145. // In frac part there is separate denominator and numerator
  146. // If frac part is 0 then the denominator is 1000 and numerator is 0
  147. uint32_t frac_n = (uint32_t) ((N_div - N) * 524287);
  148. uint32_t frac_d = 524287;
  149. // If frac part is 0 then the denominator is 1000 and numerator is 0
  150. if (frac_n == 0) {
  151. frac_n = 0;
  152. frac_d = 524287;
  153. }
  154. // Recommended sequnce for changin freq
  155. // 1. Change the N-div value
  156. // 2. Change the PLL numerator and denominator
  157. // 3. Program FCAL_EN bit
  158. // Clear the required parts of the register
  159. lmx2594regs[112-MASH_ORDER] = lmx2594regs[112-MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
  160. // Set the MASH_ORDER to 3
  161. lmx2594regs[112-MASH_ORDER] = lmx2594regs[112-MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
  162. // Set PF_DLY_SEL to 3
  163. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  164. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
  165. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  166. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  167. //CLear the lower 16 bits of the register
  168. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  169. // Next 16 bits of the register
  170. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  171. // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
  172. lmx2594regs[112-PLL_NUM_S] = lmx2594regs[112-PLL_NUM_S] & (~0xFFFF);
  173. lmx2594regs[112-PLL_NUM_S] = lmx2594regs[112-PLL_NUM_S] | (frac_n >> 16);
  174. // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
  175. lmx2594regs[112-PLL_NUM_M] = lmx2594regs[112-PLL_NUM_M] & (~0xFFFF);
  176. // Next 16 bits of the numerator
  177. lmx2594regs[112-PLL_NUM_M] = lmx2594regs[112-PLL_NUM_M] | (frac_n & 0xFFFF);
  178. // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
  179. lmx2594regs[112-PLL_DEN_S] = lmx2594regs[112-PLL_DEN_S] & (~0xFFFF);
  180. // most significant 16 bits of the denominator
  181. lmx2594regs[112-PLL_DEN_S] = lmx2594regs[112-PLL_DEN_S] | (frac_d >> 16);
  182. // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
  183. lmx2594regs[112-PLL_DEN_M] = lmx2594regs[112-PLL_DEN_M] & (~0xFFFF);
  184. // Next 16 bits of the denominator
  185. lmx2594regs[112-PLL_DEN_M] = lmx2594regs[112-PLL_DEN_M] | (frac_d & 0xFFFF);
  186. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  187. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  188. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_VCO;
  189. // Program the FCAL_EN bit
  190. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  191. // Show the all the upper 16 bits of the register lmx2594regs[PLL_N_S]
  192. // Determine which regs are changed and send only those
  193. uint32_t lmx_change_freq_regs[] = {
  194. lmx2594regs[112-MASH_ORDER],
  195. lmx2594regs[112-PFD_DLY_SEL],
  196. lmx2594regs[112-PLL_N_S],
  197. lmx2594regs[112-PLL_N_M],
  198. lmx2594regs[112-PLL_DEN_S],
  199. lmx2594regs[112-PLL_DEN_M],
  200. lmx2594regs[112-PLL_NUM_S],
  201. lmx2594regs[112-PLL_NUM_M],
  202. lmx2594regs[112-OUTA_MUX],
  203. lmx2594regs[112-FCAL_ADDR]
  204. };
  205. // Create a header for the LMX2594 with the appropriate number of words
  206. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs)/4) << 1) | 1);
  207. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  208. *ptr = LMX_HEADER;
  209. for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
  210. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  211. *data_ptr = lmx_change_freq_regs[i];
  212. }
  213. usleep(1);
  214. return 0;
  215. }
  216. int lmx_freq_set_out_of_band(void *bar1, double freq, double f_pd) {
  217. double f_vco = 2 * freq;
  218. int chan_div = 2;
  219. uint8_t ch_div_reg = 0; // 2
  220. double vco_div = 7.5e9 / freq;
  221. double N_div;
  222. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  223. if (f_vco < 7.5e9) {
  224. if (vco_div > 2 && vco_div <= 4)
  225. chan_div = 4; // 4
  226. f_vco = freq * chan_div;
  227. if (vco_div > 4 && vco_div <= 6) {
  228. chan_div = 6; // 6
  229. f_vco = freq * chan_div;
  230. }
  231. if (vco_div > 6 && vco_div <= 8) {
  232. chan_div = 8; // 8
  233. f_vco = freq * chan_div;
  234. }
  235. if (vco_div > 8 && vco_div <= 12) {
  236. chan_div = 12; // 12
  237. f_vco = freq * chan_div;
  238. }
  239. if (vco_div > 12 && vco_div <= 16) {
  240. chan_div = 16; // 16
  241. f_vco = freq * chan_div;
  242. }
  243. if (vco_div > 16 && vco_div <= 24) {
  244. chan_div = 24; // 24
  245. f_vco = freq * chan_div;
  246. }
  247. if (vco_div > 24 && vco_div <= 32) {
  248. chan_div = 32; // 32
  249. f_vco = freq * chan_div;
  250. }
  251. if (vco_div > 32 && vco_div <= 48) {
  252. chan_div = 48; // 48
  253. f_vco = freq * chan_div;
  254. }
  255. if (vco_div > 48 && vco_div <= 64) {
  256. chan_div = 64; // 64
  257. f_vco = freq * chan_div;
  258. }
  259. if (vco_div > 64 && vco_div <= 72) {
  260. chan_div = 72; // 72
  261. f_vco = freq * chan_div;
  262. }
  263. if (vco_div > 72 && vco_div <= 96) {
  264. chan_div = 96; // 96
  265. f_vco = freq * chan_div;
  266. }
  267. if (vco_div > 96 && vco_div <= 128) {
  268. chan_div = 128; // 128
  269. f_vco = freq * chan_div;
  270. }
  271. if (vco_div > 128 && vco_div <= 192) {
  272. chan_div = 192; // 192
  273. f_vco = freq * chan_div;
  274. }
  275. if (vco_div > 192 && vco_div <= 256) {
  276. chan_div = 256; // 256
  277. f_vco = freq * chan_div;
  278. }
  279. if (vco_div > 256 && vco_div <= 384) {
  280. chan_div = 384; // 384
  281. f_vco = freq * chan_div;
  282. }
  283. if (vco_div > 384 && vco_div <= 512) {
  284. chan_div = 512; // 512
  285. f_vco = freq * chan_div;
  286. }
  287. if (vco_div > 512 && vco_div <= 768) {
  288. chan_div = 768; // 768
  289. f_vco = freq * chan_div;
  290. }
  291. switch (chan_div) {
  292. case 2:
  293. ch_div_reg = 0;
  294. break;
  295. case 4:
  296. ch_div_reg = 1;
  297. break;
  298. case 6:
  299. ch_div_reg = 2;
  300. break;
  301. case 8:
  302. ch_div_reg = 3;
  303. break;
  304. case 12:
  305. ch_div_reg = 4;
  306. break;
  307. case 16:
  308. ch_div_reg = 5;
  309. break;
  310. case 24:
  311. ch_div_reg = 6;
  312. break;
  313. case 32:
  314. ch_div_reg = 7;
  315. break;
  316. case 48:
  317. ch_div_reg = 8;
  318. break;
  319. case 64:
  320. ch_div_reg = 9;
  321. break;
  322. case 72:
  323. ch_div_reg = 10;
  324. break;
  325. case 96:
  326. ch_div_reg = 11;
  327. break;
  328. case 128:
  329. ch_div_reg = 12;
  330. break;
  331. case 192:
  332. ch_div_reg = 13;
  333. break;
  334. case 256:
  335. ch_div_reg = 14;
  336. break;
  337. case 384:
  338. ch_div_reg = 15;
  339. break;
  340. case 512:
  341. ch_div_reg = 16;
  342. break;
  343. case 768:
  344. ch_div_reg = 17;
  345. break;
  346. }
  347. } else {
  348. ch_div_reg = 0;
  349. f_vco = freq * 2;
  350. }
  351. N_div = f_vco / f_pd;
  352. // divide whole part and fractional part
  353. uint32_t N = (uint32_t) N_div;
  354. uint32_t frac_n = (uint32_t) ((N_div - N) * 524287);
  355. uint32_t frac_d = 524287;
  356. // If frac part is 0 then the denominator is 1000 and numerator is 0
  357. if (frac_n == 0) {
  358. frac_n = 0;
  359. frac_d = 524287;
  360. }
  361. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
  362. // Set the MASH_ORDER to 3
  363. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
  364. // Set PF_DLY_SEL to 3
  365. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  366. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
  367. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] & (~0xFFFF);
  368. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] | (N >> 16);
  369. //CLear the lower 16 bits of the register
  370. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] & (~0xFFFF);
  371. // Next 16 bits of the register
  372. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] | (N & 0xFFFF);
  373. // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
  374. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] & (~0xFFFF);
  375. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] | (frac_n >> 16);
  376. // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
  377. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] & (~0xFFFF);
  378. // Next 16 bits of the numerator
  379. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] | (frac_n & 0xFFFF);
  380. // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
  381. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] & (~0xFFFF);
  382. // most significant 16 bits of the denominator
  383. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] | (frac_d >> 16);
  384. // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
  385. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] & (~0xFFFF);
  386. // Next 16 bits of the denominator
  387. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] | (frac_d & 0xFFFF);
  388. // Program the CHDIV value
  389. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  390. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  391. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  392. // If the ch_div > 2 then set the SEG1_EN bit
  393. if (chan_div > 2) {
  394. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  395. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  396. }
  397. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  398. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  399. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  400. // Program the FCAL_EN bit
  401. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  402. uint32_t lmx_change_freq_regs[] = {
  403. lmx2594regs[112-MASH_ORDER],
  404. lmx2594regs[112-PFD_DLY_SEL],
  405. lmx2594regs[112 - PLL_N_S],
  406. lmx2594regs[112 - PLL_N_M],
  407. lmx2594regs[112 - PLL_DEN_S],
  408. lmx2594regs[112 - PLL_DEN_M],
  409. lmx2594regs[112 - PLL_NUM_S],
  410. lmx2594regs[112 - PLL_NUM_M],
  411. lmx2594regs[112 - FCAL_ADDR],
  412. lmx2594regs[112 - CHDIV_DIV2],
  413. lmx2594regs[112 - CHDIV],
  414. lmx2594regs[112 - OUTA_MUX]
  415. };
  416. // Create a header for the LMX2594 with the appropriate number of words
  417. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs) / 4) << 1) | 1);
  418. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  419. *ptr = LMX_HEADER;
  420. // Send the data
  421. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  422. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  423. *data_ptr = lmx_change_freq_regs[i];
  424. }
  425. usleep(1);
  426. return 0;
  427. }
  428. int lmx_freq_set(void *bar1, double freq) {
  429. double f_pd = 200e6;
  430. double N_div = 0;
  431. if (freq < 10e6 || freq > 15e9) {
  432. printf("Frequency range is 10 MHz to 15 GHz\n");
  433. return -1;
  434. }
  435. // if the frequency is in the main band - 7.5 GHz to 15 GHz
  436. if (freq >= 7.5e9 && freq <= 15e9) {
  437. lmx_freq_set_main_band(bar1, freq, f_pd);
  438. }
  439. else if (freq < 7.5e9) {
  440. lmx_freq_set_out_of_band(bar1, freq, f_pd);
  441. }
  442. return 0;
  443. }