ad9912.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "ad9912.h"
  2. uint32_t ad9912regs[AD9912_COUNT] = {
  3. 0x000018,
  4. 0x000100,
  5. 0x000202,
  6. 0x000309,
  7. 0x000400,
  8. 0x000500,
  9. 0x001010,
  10. 0x001200,
  11. 0x001300,
  12. 0x002012,
  13. 0x002204,
  14. 0x010400,
  15. 0x010500,
  16. 0x010600,
  17. 0x01A6CD,
  18. 0x01A7CC,
  19. 0x01A8CC,
  20. 0x01A9CC,
  21. 0x01AACC,
  22. 0x01AB2C,
  23. 0x01AC00,
  24. 0x01AD00,
  25. 0x020005,
  26. 0x020100,
  27. 0x040BFF,
  28. 0x040C01,
  29. 0x040E10,
  30. 0x050000,
  31. 0x050100,
  32. 0x050200,
  33. 0x050300,
  34. 0x050400,
  35. 0x050500,
  36. 0x050600,
  37. 0x050700,
  38. 0x050800,
  39. 0x050900
  40. };
  41. /*-------------------------AD9912 INIT FUNCTION-------------------------*/
  42. void ad9912_init(void *bar1) {
  43. uint32_t *ptr_rst = bar1 + AD9912_BASE_ADDR;
  44. *ptr_rst = GPIO_INIT_HEADER;
  45. //Rst on
  46. *ptr_rst = AD9912_RST_ON;
  47. // Rst off
  48. *ptr_rst = GPIO_REG;
  49. //Init Header
  50. uint32_t *ptr = bar1 + AD9912_BASE_ADDR;
  51. *ptr = InitDDSHeader;
  52. //Init Data
  53. for (int k = 0; k < AD9912_COUNT; k++) {
  54. uint32_t *ptr = bar1 + AD9912_BASE_ADDR;
  55. *ptr = ad9912regs[k];
  56. }
  57. }
  58. /*----------------------------------------------------------------------*/
  59. double ad9912_set_main_band(double freq, int lmx_n, double f_pd) {
  60. // Divide the frequncy by the old value of the phase detector frequency and only left with the integer part
  61. uint32_t N = (uint32_t) (freq/f_pd);
  62. printf("N = %d\n", N);
  63. // Calculate the new phase detector frequency by dividing the frequency by the new value of N
  64. f_pd = freq/N; // Phase detector frequency
  65. printf("f_pd = %f\n", f_pd);
  66. return f_pd;
  67. }
  68. double ad9912_set_out_of_band(double freq,int lmx_n) {
  69. double f_pd;
  70. double f_vco; // VCO frequency
  71. double vco_div = 7.5e9/freq;
  72. int chan_div = 2;
  73. if (vco_div > 2 && vco_div <= 4) {
  74. chan_div = 4;
  75. f_vco = freq * chan_div;
  76. }
  77. else if (vco_div > 4 && vco_div <= 6) {
  78. chan_div = 6;
  79. f_vco = freq * chan_div;
  80. }
  81. else if (vco_div > 6 && vco_div <=8) {
  82. chan_div = 8;
  83. f_vco = freq * chan_div;
  84. }
  85. else if (vco_div > 8 && vco_div <= 12) {
  86. chan_div = 12;
  87. f_vco = freq * chan_div;
  88. }
  89. else if (vco_div > 12 && vco_div <= 16) {
  90. chan_div = 16;
  91. f_vco = freq * chan_div;
  92. }
  93. else if (vco_div > 16 && vco_div <= 24) {
  94. chan_div = 24;
  95. f_vco = freq * chan_div;
  96. }
  97. else if (vco_div > 24 && vco_div <= 32) {
  98. chan_div = 32;
  99. f_vco = freq * chan_div;
  100. }
  101. else if (vco_div > 32 && vco_div <= 48) {
  102. chan_div = 48;
  103. f_vco = freq * chan_div;
  104. }
  105. else if (vco_div > 48 && vco_div <= 64) {
  106. chan_div = 64;
  107. f_vco = freq * chan_div;
  108. }
  109. else if (vco_div > 64 && vco_div <= 72) {
  110. chan_div = 72;
  111. f_vco = freq * chan_div;
  112. }
  113. else if (vco_div > 72 && vco_div <= 96) {
  114. chan_div = 96;
  115. f_vco = freq * chan_div;
  116. }
  117. else if (vco_div > 96 && vco_div <= 128) {
  118. chan_div = 128;
  119. f_vco = freq * chan_div;
  120. }
  121. else if (vco_div > 128 && vco_div <= 192) {
  122. chan_div = 192;
  123. f_vco = freq * chan_div;
  124. }
  125. else if (vco_div > 192 && vco_div <= 256) {
  126. chan_div = 256;
  127. f_vco = freq * chan_div;
  128. }
  129. else if (vco_div > 256 && vco_div <= 384) {
  130. chan_div = 384;
  131. f_vco = freq * chan_div;
  132. }
  133. else if (vco_div > 384 && vco_div <= 512) {
  134. chan_div = 512;
  135. f_vco = freq * chan_div;
  136. }
  137. else if (vco_div > 512 && vco_div <= 768) {
  138. chan_div = 768;
  139. f_vco = freq * chan_div;
  140. }
  141. }
  142. // Divide the frequncy by the old value of the phase detector frequency and only left with the integer part
  143. uint32_t N = (uint32_t) (f_vco/f_pd);
  144. printf("N = %d\n", N);
  145. // Calculate the new phase detector frequency by dividing the frequency by the new value of N
  146. f_pd = f_vco/N; // Phase detector frequency
  147. printf("f_pd = %f\n", f_pd);
  148. return f_pd;
  149. double ad9912_set(void *bar1, double freq, double f_pd) {
  150. double fs = 1e9;
  151. int lmx_n = 50;
  152. if (freq >= 7500e6 && freq <= 15000e6) {
  153. f_pd = ad9912_set_main_band(freq, fs, lmx_n);
  154. }
  155. else if (freq >= 10e6 && freq < 7500e6) {
  156. f_pd = ad9912_set_out_of_band(freq, fs, lmx_n);
  157. }
  158. else {
  159. return -1;
  160. printf("Frequency is out of range\n");
  161. }
  162. printf("f_pd = %f\n", f_pd);
  163. // FTW word
  164. double ftw_word = (f_pd*pow(2, 48))/fs;
  165. // Split the FTW word between 6 registers
  166. // First one is ftw_word[7:0]
  167. uint32_t ftw0_7_0 = (uint32_t) ftw_word;
  168. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] & ~BITM_AD9912_FTW0_FREQ_WORD_7_0;
  169. 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);
  170. // Second one is ftw_word[15:8]
  171. uint32_t ftw0_15_8 = (uint32_t) (ftw_word >> 8);
  172. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] & ~BITM_AD9912_FTW0_FREQ_WORD_15_8;
  173. 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);
  174. // Third one is ftw_word[23:16]
  175. uint32_t ftw0_23_16 = (uint32_t) (ftw_word >> 16);
  176. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] & ~BITM_AD9912_FTW0_FREQ_WORD_23_16;
  177. 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);
  178. // Fourth one is ftw_word[31:24]
  179. uint32_t ftw0_31_24 = (uint32_t) (ftw_word >> 24);
  180. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] & ~BITM_AD9912_FTW0_FREQ_WORD_31_24;
  181. 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);
  182. // Fifth one is ftw_word[39:32]
  183. uint32_t ftw1_39_32 = (uint32_t) (ftw_word >> 32);
  184. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] & ~BITM_AD9912_FTW0_FREQ_WORD_39_24;
  185. 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);
  186. // Sixth one is ftw_word[47:40]
  187. uint32_t ftw1_47_40 = (uint32_t) (ftw_word >> 40);
  188. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] & ~BITM_AD9912_FTW0_FREQ_WORD_47_40;
  189. 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);
  190. // Set the frequency
  191. uint32_t ad9912_ftw_regs[] = {
  192. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0],
  193. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8],
  194. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16],
  195. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24],
  196. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24],
  197. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40]
  198. };
  199. // Create the appropriate header
  200. uint32_t *DDS_HEADER = bar1 + AD9912_BASE_ADDR;
  201. *DDS_HEADER = ((0 << 23) | (DeviceIdDDS << 18) | ((sizeof(ad9912_ftw_regs)/4) << 1) | 1);
  202. // Write the data
  203. for (int i = 0; i < sizeof(ad9912_ftw_regs)/4; i++) {
  204. uint32_t *DDS_DATA = bar1 + AD9912_BASE_ADDR;
  205. *DDS_DATA = ad9912_ftw_regs[i];
  206. }
  207. char filename[100];
  208. sprintf(filename, "%f.txt", f_pd);
  209. FILE * f = fopen(filename, "w");
  210. for (int i = 0; i < sizeof(ad9912regs) / 4; i++) {
  211. fprintf(f, "0x%08X\n", ad9912regs[i]);
  212. }
  213. fclose(f);
  214. return f_pd;
  215. }