pll_4113.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. uint8_t halSynSetFreq(uint32_t rf_Freq)
  64. {
  65. uint32_t R, B;
  66. uint32_t A, P, p_mode;
  67. uint32_t N_val = 0;
  68. N_val = (rf_Freq / ADF4113_CH_STEP);
  69. if( N_val < ADF4113_PRE8_MIN_N) {
  70. return HAL_SYN_INVALID_PRESCALE;
  71. } else if(( N_val> ADF4113_PRE8_MIN_N) && (N_val < ADF4113_PRE16_MIN_N)) {
  72. P = 8;
  73. p_mode = ADF4113_PRESCALE8;
  74. } else if(( N_val > ADF4113_PRE16_MIN_N) && (N_val < ADF4113_PRE32_MIN_N)) {
  75. P = 16;
  76. p_mode = ADF4113_PRESCALE16;
  77. } else if((N_val > ADF4113_PRE32_MIN_N) && ( N_val < ADF4113_PRE64_MIN_N)) {
  78. P = 32;
  79. p_mode = ADF4113_PRESCALE32;
  80. } else if( N_val > ADF4113_PRE64_MIN_N) {
  81. P = 64;
  82. p_mode = ADF4113_PRESCALE64;
  83. }
  84. P = 32;
  85. B = N_val / P;
  86. A = N_val -(B * P);
  87. printf("FREQ:%f Mhz B : %d , A : %d N_VAL : %d \r\n",(float)(rf_Freq/1000000),B,A,N_val);
  88. printf("YJ 4113 : %x \r\n",N_Counter_Latch_Create(A,B,0));
  89. }
  90. uint32_t N_Counter_Latch_Create(uint16_t _ACOUNTER,uint16_t _BCOUNTER,uint8_t _CPGAIN){
  91. uint32_t ret = 0;
  92. uint32_t shift_bit = 0x01;
  93. uint8_t control_bit = 1;
  94. uint8_t i = 0;
  95. uint8_t reserve = 0;
  96. printf("_ACOUNTER : %d _BCOUNTER : %d \r\n",_ACOUNTER,_BCOUNTER);
  97. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  98. for(i = 0; i < 2; i++){
  99. if(control_bit & 0x01)
  100. ret += shift_bit << i;
  101. control_bit = control_bit >> 1;
  102. }
  103. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  104. for(i = 2; i < 8; i++){
  105. if(_ACOUNTER & 0x01)
  106. ret += shift_bit << i;
  107. _ACOUNTER = _ACOUNTER >> 1;
  108. }
  109. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  110. for(i = 8; i < 21; i++){
  111. if(_BCOUNTER & 0x01)
  112. ret += shift_bit << i;
  113. _BCOUNTER = _BCOUNTER >> 1;
  114. }
  115. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  116. if(_CPGAIN & 0x01)
  117. ret += shift_bit << i++;
  118. for(i = 22; i < 24; i++){
  119. if(reserve & 0x01)
  120. ret += shift_bit << i;
  121. reserve = reserve >> 1;
  122. }
  123. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  124. return ret;
  125. }
  126. void ADF4113_Module_Ctrl(PLL_Setting_st pll,uint32_t R0,uint32_t R1,uint32_t R2){
  127. R2 = R2 & 0xFFFFFF;
  128. R1 = R1 & 0xFFFFFF;
  129. R0 = R0 & 0xFFFFFF;
  130. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  131. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  132. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  133. /* R2 Ctrl */
  134. for(int i =0; i < 24; i++){
  135. if(R2 & 0x800000){
  136. printf("1");
  137. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  138. }
  139. else{
  140. printf("0");
  141. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  142. }
  143. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  144. Pol_Delay_us(10);
  145. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  146. R2 = ((R2 << 1) & 0xFFFFFF);
  147. }
  148. printf("\r\n");
  149. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  150. Pol_Delay_us(10);
  151. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  152. /* R0 Ctrl */
  153. for(int i =0; i < 24; i++){
  154. if(R0 & 0x800000){
  155. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  156. printf("1");
  157. }
  158. else{
  159. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  160. printf("0");
  161. }
  162. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  163. Pol_Delay_us(10);
  164. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  165. R0 = ((R0 << 1) & 0xFFFFFF);
  166. }
  167. printf("\r\n");
  168. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  169. Pol_Delay_us(10);
  170. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  171. /* R1 Ctrl */
  172. for(int i =0; i < 24; i++){
  173. if(R1 & 0x800000){
  174. printf("1");
  175. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  176. }
  177. else{
  178. printf("0");
  179. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  180. }
  181. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  182. Pol_Delay_us(10);
  183. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  184. R1 = ((R1 << 1) & 0xFFFFFF);
  185. }
  186. printf("\r\n");
  187. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  188. Pol_Delay_us(10);
  189. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  190. }