pll_4113.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**************************************************************************************************
  2. Filename: hal_adf4113.c
  3. Revised: $Date: 2013-11-17 $
  4. Revision: $Revision: $
  5. Description: This file contains the interface to the ADF4113 frequency synthesizer.
  6. **************************************************************************************************/
  7. #include "pll_4113.h"
  8. void ADF4113_Module_Ctrl(PLL_Setting_st pll,uint32_t R0,uint32_t R1,uint32_t R2);
  9. uint32_t N_Counter_Latch_Create(uint16_t _ACOUNTER,uint16_t _BCOUNTER,uint8_t _CPGAIN);
  10. #define ADF4113_PRESCALE8 0
  11. #define ADF4113_PRESCALE16 1
  12. #define ADF4113_PRESCALE32 2
  13. #define ADF4113_PRESCALE64 3
  14. // ADF4113 Prescale value for minimum required division ratio
  15. #define ADF4113_PRE8_MIN_N 56
  16. #define ADF4113_PRE16_MIN_N 240
  17. #define ADF4113_PRE32_MIN_N 992
  18. #define ADF4113_PRE64_MIN_N 4032
  19. // Frequency Settings
  20. // Initally, the synthesizer will operate at 2450 MHz
  21. #define ADF4113_CH_STEP 50000
  22. #define HAL_SYN_INVALID_PRESCALE 0x04
  23. #define ADF4113_REF_FREQ_MHZ 13000000
  24. PLL_Setting_st ADF4113_1_8G_DL = {
  25. PLL_CLK_GPIO_Port,
  26. PLL_CLK_Pin,
  27. PLL_DATA_GPIO_Port,
  28. PLL_DATA_Pin,
  29. PLL_EN_1_8G_DL_GPIO_Port,
  30. PLL_EN_1_8G_DL_Pin,
  31. };
  32. PLL_Setting_st ADF4113_1_8G_UL = {
  33. PLL_CLK_GPIO_Port,
  34. PLL_CLK_Pin,
  35. PLL_DATA_GPIO_Port,
  36. PLL_DATA_Pin,
  37. PLL_EN_1_8G_UL_GPIO_Port,
  38. PLL_EN_1_8G_UL_Pin,
  39. };
  40. PLL_Setting_st ADF4113_2_1G_DL = {
  41. PLL_CLK_GPIO_Port,
  42. PLL_CLK_Pin,
  43. PLL_DATA_GPIO_Port,
  44. PLL_DATA_Pin,
  45. PLL_EN_2_1G_DL_GPIO_Port,
  46. PLL_EN_2_1G_DL_Pin,
  47. };
  48. PLL_Setting_st ADF4113_2_1G_UL = {
  49. PLL_CLK_GPIO_Port,
  50. PLL_CLK_Pin,
  51. PLL_DATA_GPIO_Port,
  52. PLL_DATA_Pin,
  53. PLL_EN_2_1G_UL_GPIO_Port,
  54. PLL_EN_2_1G_UL_Pin,
  55. };
  56. // Error Code
  57. typedef struct{
  58. uint16_t B;
  59. uint16_t P;
  60. uint16_t A;
  61. uint16_t N;
  62. }Adf4113_st;
  63. void ADF4113_Initialize(void){
  64. ADF4113_Module_Ctrl(ADF4113_1_8G_DL,0x000410,0x03E801,0x9F8092);
  65. HAL_Delay(1);
  66. ADF4113_Module_Ctrl(ADF4113_1_8G_UL,0x000410,0x038D31,0x9f8092);
  67. HAL_Delay(1);
  68. ADF4113_Module_Ctrl(ADF4113_2_1G_DL,0x410,0x4DE71,0x9F8092);
  69. HAL_Delay(1);
  70. ADF4113_Module_Ctrl(ADF4113_2_1G_UL,0x000410,0x59A31,0x9f8092);
  71. }
  72. uint8_t halSynSetFreq(uint32_t rf_Freq)
  73. {
  74. uint32_t R, B;
  75. uint32_t A, P, p_mode;
  76. uint32_t N_val = 0;
  77. N_val = (rf_Freq / ADF4113_CH_STEP);
  78. if( N_val < ADF4113_PRE8_MIN_N) {
  79. return HAL_SYN_INVALID_PRESCALE;
  80. } else if(( N_val> ADF4113_PRE8_MIN_N) && (N_val < ADF4113_PRE16_MIN_N)) {
  81. P = 8;
  82. p_mode = ADF4113_PRESCALE8;
  83. } else if(( N_val > ADF4113_PRE16_MIN_N) && (N_val < ADF4113_PRE32_MIN_N)) {
  84. P = 16;
  85. p_mode = ADF4113_PRESCALE16;
  86. } else if((N_val > ADF4113_PRE32_MIN_N) && ( N_val < ADF4113_PRE64_MIN_N)) {
  87. P = 32;
  88. p_mode = ADF4113_PRESCALE32;
  89. } else if( N_val > ADF4113_PRE64_MIN_N) {
  90. P = 64;
  91. p_mode = ADF4113_PRESCALE64;
  92. }
  93. P = 32;
  94. B = N_val / P;
  95. A = N_val -(B * P);
  96. #ifdef DEBUG_PRINT
  97. printf("FREQ:%f Mhz B : %d , A : %d N_VAL : %d \r\n",(float)(rf_Freq/1000000),B,A,N_val);
  98. printf("YJ 4113 : %x \r\n",N_Counter_Latch_Create(A,B,0));
  99. #endif /* DEBUG_PRINT */
  100. }
  101. uint32_t N_Counter_Latch_Create(uint16_t _ACOUNTER,uint16_t _BCOUNTER,uint8_t _CPGAIN){
  102. uint32_t ret = 0;
  103. uint32_t shift_bit = 0x01;
  104. uint8_t control_bit = 1;
  105. uint8_t i = 0;
  106. uint8_t reserve = 0;
  107. #ifdef DEBUG_PRINT
  108. printf("_ACOUNTER : %d _BCOUNTER : %d \r\n",_ACOUNTER,_BCOUNTER);
  109. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  110. #endif /* DEBUG_PRINT */
  111. for(i = 0; i < 2; i++){
  112. if(control_bit & 0x01)
  113. ret += shift_bit << i;
  114. control_bit = control_bit >> 1;
  115. }
  116. #ifdef DEBUG_PRINT
  117. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  118. #endif /* DEBUG_PRINT */
  119. for(i = 2; i < 8; i++){
  120. if(_ACOUNTER & 0x01)
  121. ret += shift_bit << i;
  122. _ACOUNTER = _ACOUNTER >> 1;
  123. }
  124. #ifdef DEBUG_PRINT
  125. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  126. #endif /* DEBUG_PRINT */
  127. for(i = 8; i < 21; i++){
  128. if(_BCOUNTER & 0x01)
  129. ret += shift_bit << i;
  130. _BCOUNTER = _BCOUNTER >> 1;
  131. }
  132. #ifdef DEBUG_PRINT
  133. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  134. #endif /* DEBUG_PRINT */
  135. if(_CPGAIN & 0x01)
  136. ret += shift_bit << i++;
  137. for(i = 22; i < 24; i++){
  138. if(reserve & 0x01)
  139. ret += shift_bit << i;
  140. reserve = reserve >> 1;
  141. }
  142. #ifdef DEBUG_PRINT
  143. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  144. #endif /* DEBUG_PRINT */
  145. return ret;
  146. }
  147. void ADF4113_Module_Ctrl(PLL_Setting_st pll,uint32_t R0,uint32_t R1,uint32_t R2){
  148. R2 = R2 & 0xFFFFFF;
  149. R1 = R1 & 0xFFFFFF;
  150. R0 = R0 & 0xFFFFFF;
  151. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  152. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  153. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  154. /* R2 Ctrl */
  155. for(int i =0; i < 24; i++){
  156. if(R2 & 0x800000){
  157. #ifdef DEBUG_PRINT
  158. printf("1");
  159. #endif /* DEBUG_PRINT */
  160. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  161. }
  162. else{
  163. #ifdef DEBUG_PRINT
  164. printf("0");
  165. #endif /* DEBUG_PRINT */
  166. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  167. }
  168. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  169. Pol_Delay_us(10);
  170. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  171. R2 = ((R2 << 1) & 0xFFFFFF);
  172. }
  173. #ifdef DEBUG_PRINT
  174. printf("\r\n");
  175. #endif /* DEBUG_PRINT */
  176. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  177. Pol_Delay_us(10);
  178. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  179. /* R0 Ctrl */
  180. for(int i =0; i < 24; i++){
  181. if(R0 & 0x800000){
  182. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  183. #ifdef DEBUG_PRINT
  184. printf("1");
  185. #endif /* DEBUG_PRINT */
  186. }
  187. else{
  188. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  189. #ifdef DEBUG_PRINT
  190. printf("0");
  191. #endif /* DEBUG_PRINT */
  192. }
  193. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  194. Pol_Delay_us(10);
  195. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  196. R0 = ((R0 << 1) & 0xFFFFFF);
  197. }
  198. #ifdef DEBUG_PRINT
  199. printf("\r\n");
  200. #endif /* DEBUG_PRINT */
  201. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  202. Pol_Delay_us(10);
  203. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  204. /* R1 Ctrl */
  205. for(int i =0; i < 24; i++){
  206. if(R1 & 0x800000){
  207. #ifdef DEBUG_PRINT
  208. printf("1");
  209. #endif /* DEBUG_PRINT */
  210. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  211. }
  212. else{
  213. #ifdef DEBUG_PRINT
  214. printf("0");
  215. #endif /* DEBUG_PRINT */
  216. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  217. }
  218. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  219. Pol_Delay_us(10);
  220. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  221. R1 = ((R1 << 1) & 0xFFFFFF);
  222. }
  223. #ifdef DEBUG_PRINT
  224. printf("\r\n");
  225. #endif /* DEBUG_PRINT */
  226. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  227. Pol_Delay_us(10);
  228. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  229. }