ad9912.c 8.5 KB

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