pll_4113.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. uint8_t PLL_1_8_DL_Error_Cnt = 0;
  25. uint8_t PLL_1_8_UL_Error_Cnt = 0;
  26. uint8_t PLL_2_1_DL_Error_Cnt = 0;
  27. uint8_t PLL_2_1_UL_Error_Cnt = 0;
  28. PLL_Setting_st ADF4113_1_8G_DL = {
  29. PLL_CLK_GPIO_Port,
  30. PLL_CLK_Pin,
  31. PLL_DATA_GPIO_Port,
  32. PLL_DATA_Pin,
  33. PLL_EN_1_8G_DL_GPIO_Port,
  34. PLL_EN_1_8G_DL_Pin,
  35. };
  36. PLL_Setting_st ADF4113_1_8G_UL = {
  37. PLL_CLK_GPIO_Port,
  38. PLL_CLK_Pin,
  39. PLL_DATA_GPIO_Port,
  40. PLL_DATA_Pin,
  41. PLL_EN_1_8G_UL_GPIO_Port,
  42. PLL_EN_1_8G_UL_Pin,
  43. };
  44. PLL_Setting_st ADF4113_2_1G_DL = {
  45. PLL_CLK_GPIO_Port,
  46. PLL_CLK_Pin,
  47. PLL_DATA_GPIO_Port,
  48. PLL_DATA_Pin,
  49. PLL_EN_2_1G_DL_GPIO_Port,
  50. PLL_EN_2_1G_DL_Pin,
  51. };
  52. PLL_Setting_st ADF4113_2_1G_UL = {
  53. PLL_CLK_GPIO_Port,
  54. PLL_CLK_Pin,
  55. PLL_DATA_GPIO_Port,
  56. PLL_DATA_Pin,
  57. PLL_EN_2_1G_UL_GPIO_Port,
  58. PLL_EN_2_1G_UL_Pin,
  59. };
  60. // Error Code
  61. typedef struct{
  62. uint16_t B;
  63. uint16_t P;
  64. uint16_t A;
  65. uint16_t N;
  66. }Adf4113_st;
  67. void ADF4113_Initialize(void){
  68. if(Flash_Save_data[INDEX_PLL_1_8G_DL_H] == 0 && Flash_Save_data[INDEX_PLL_1_8G_DL_L] == 0){
  69. Flash_Save_data[INDEX_PLL_1_8G_DL_H] = ((18425 & 0xFF00) >> 8);//0x47;
  70. Flash_Save_data[INDEX_PLL_1_8G_DL_L] = (18425 & 0x00FF);
  71. }
  72. if(Flash_Save_data[INDEX_PLL_1_8G_UL_H] == 0 && Flash_Save_data[INDEX_PLL_1_8G_UL_L] == 0){
  73. Flash_Save_data[INDEX_PLL_1_8G_UL_H] = ((17475 & 0xFF00) >> 8);
  74. Flash_Save_data[INDEX_PLL_1_8G_UL_L] = (17475 & 0x00FF);
  75. }
  76. if(Flash_Save_data[INDEX_PLL_2_1G_DL_H] == 0 && Flash_Save_data[INDEX_PLL_2_1G_DL_L] == 0){
  77. Flash_Save_data[INDEX_PLL_2_1G_DL_H] = ((21400 & 0xFF00) >> 8);
  78. Flash_Save_data[INDEX_PLL_2_1G_DL_L] = (21400 & 0x00FF);
  79. }
  80. if(Flash_Save_data[INDEX_PLL_2_1G_UL_H] == 0 && Flash_Save_data[INDEX_PLL_2_1G_UL_L] == 0){
  81. Flash_Save_data[INDEX_PLL_2_1G_UL_H] = ((19500 & 0xFF00) >> 8);
  82. Flash_Save_data[INDEX_PLL_2_1G_UL_L] = (19500 & 0x00FF);
  83. }
  84. // ADF4113_Module_Ctrl(ADF4113_1_8G_DL,0x000410,0x03E801,0x9F8092);
  85. // HAL_Delay(1);
  86. // ADF4113_Module_Ctrl(ADF4113_1_8G_UL,0x000410,0x038D31,0x9f8092);
  87. // HAL_Delay(1);
  88. // ADF4113_Module_Ctrl(ADF4113_2_1G_DL,0x410,0x4DE71,0x9F8092);
  89. // HAL_Delay(1);
  90. // ADF4113_Module_Ctrl(ADF4113_2_1G_UL,0x000410,0x59A31,0x9f8092);
  91. }
  92. void ADF4113_Check(void){
  93. uint16_t temp_val = 0;
  94. if(HAL_GPIO_ReadPin(PLL_LD_1_8G_DL_GPIO_Port, PLL_LD_1_8G_DL_Pin) == GPIO_PIN_RESET){
  95. temp_val = (Prev_data[INDEX_PLL_1_8G_DL_H] << 8) | (Prev_data[INDEX_PLL_1_8G_DL_L]);
  96. // ADF4113_Module_Ctrl(ADF4113_1_8G_DL,0x000410,halSynSetFreq((temp_val * 1000000) / 10 ),0x9F8092);
  97. ADF4113_Module_Ctrl(ADF4113_1_8G_DL,0x000410,0x03E801,0x9F8092);
  98. if(PLL_1_8_DL_Error_Cnt == 3){
  99. Error_Message_Occur(DL_1_8);
  100. }
  101. if(PLL_1_8_DL_Error_Cnt < 4)
  102. PLL_1_8_DL_Error_Cnt++;
  103. HAL_Delay(1);
  104. }else{
  105. PLL_1_8_DL_Error_Cnt = 0;
  106. }
  107. if(HAL_GPIO_ReadPin(PLL_LD_1_8G_UL_GPIO_Port, PLL_LD_1_8G_UL_Pin) == GPIO_PIN_RESET){
  108. temp_val = (Prev_data[INDEX_PLL_1_8G_UL_H] << 8) | (Prev_data[INDEX_PLL_1_8G_UL_L]);
  109. ADF4113_Module_Ctrl(ADF4113_1_8G_UL,0x000410,halSynSetFreq((temp_val * 1000000) / 10 ),0x9F8092);
  110. // ADF4113_Module_Ctrl(ADF4113_1_8G_UL,0x000410,0x038D31,0x9f8092);
  111. if(PLL_1_8_UL_Error_Cnt == 3){
  112. Error_Message_Occur(UL_1_8);
  113. }
  114. if(PLL_1_8_UL_Error_Cnt < 4)
  115. PLL_1_8_UL_Error_Cnt++;
  116. HAL_Delay(1);
  117. }else{
  118. PLL_1_8_UL_Error_Cnt = 0;
  119. }
  120. if(HAL_GPIO_ReadPin(PLL_LD_2_1G_DL_GPIO_Port, PLL_LD_2_1G_DL_Pin) == GPIO_PIN_RESET){
  121. temp_val = (Prev_data[INDEX_PLL_2_1G_DL_H] << 8) | (Prev_data[INDEX_PLL_2_1G_DL_L]);
  122. // ADF4113_Module_Ctrl(ADF4113_2_1G_DL,0x000410,halSynSetFreq((temp_val * 1000000) / 10 ),0x9F8092);
  123. ADF4113_Module_Ctrl(ADF4113_2_1G_DL,0x410,0x4DE71,0x9F8092);
  124. if(PLL_2_1_DL_Error_Cnt == 3){
  125. Error_Message_Occur(DL_2_1);
  126. }
  127. if(PLL_2_1_DL_Error_Cnt < 4)
  128. PLL_2_1_DL_Error_Cnt++;
  129. HAL_Delay(1);
  130. }else{
  131. PLL_2_1_DL_Error_Cnt = 0;
  132. }
  133. if(HAL_GPIO_ReadPin(PLL_LD_2_1G_UL_GPIO_Port, PLL_LD_2_1G_UL_Pin) == GPIO_PIN_RESET){
  134. temp_val = (Prev_data[INDEX_PLL_2_1G_UL_H] << 8) | (Prev_data[INDEX_PLL_2_1G_UL_L]);
  135. // ADF4113_Module_Ctrl(ADF4113_2_1G_UL,0x000410,halSynSetFreq((temp_val * 1000000) / 10 ),0x9F8092);
  136. ADF4113_Module_Ctrl(ADF4113_2_1G_UL,0x000410,0x59A31,0x9f8092);
  137. if(PLL_2_1_UL_Error_Cnt == 3){
  138. Error_Message_Occur(UL_2_1);
  139. }
  140. if(PLL_2_1_UL_Error_Cnt < 4)
  141. PLL_2_1_UL_Error_Cnt++;
  142. HAL_Delay(1);
  143. }else{
  144. PLL_2_1_UL_Error_Cnt = 0;
  145. }
  146. }
  147. uint32_t halSynSetFreq(uint32_t rf_Freq)
  148. {
  149. uint32_t R, B;
  150. uint32_t A, P, p_mode;
  151. uint32_t N_val = 0;
  152. N_val = (rf_Freq / ADF4113_CH_STEP);
  153. if( N_val < ADF4113_PRE8_MIN_N) {
  154. return HAL_SYN_INVALID_PRESCALE;
  155. } else if(( N_val> ADF4113_PRE8_MIN_N) && (N_val < ADF4113_PRE16_MIN_N)) {
  156. P = 8;
  157. p_mode = ADF4113_PRESCALE8;
  158. } else if(( N_val > ADF4113_PRE16_MIN_N) && (N_val < ADF4113_PRE32_MIN_N)) {
  159. P = 16;
  160. p_mode = ADF4113_PRESCALE16;
  161. } else if((N_val > ADF4113_PRE32_MIN_N) && ( N_val < ADF4113_PRE64_MIN_N)) {
  162. P = 32;
  163. p_mode = ADF4113_PRESCALE32;
  164. } else if( N_val > ADF4113_PRE64_MIN_N) {
  165. P = 64;
  166. p_mode = ADF4113_PRESCALE64;
  167. }
  168. P = 32;
  169. B = N_val / P;
  170. A = N_val -(B * P);
  171. #if 1 // PYJ.2019.08.10_BEGIN --
  172. printf("FREQ:%f Mhz B : %d , A : %d N_VAL : %d \r\n",(float)(rf_Freq/1000000),B,A,N_val);
  173. printf("YJ 4113 : %x \r\n",N_Counter_Latch_Create(A,B,0));
  174. #endif // PYJ.2019.08.10_END --
  175. return N_Counter_Latch_Create(A,B,0);
  176. }
  177. uint32_t N_Counter_Latch_Create(uint16_t _ACOUNTER,uint16_t _BCOUNTER,uint8_t _CPGAIN){
  178. uint32_t ret = 0;
  179. uint32_t shift_bit = 0x01;
  180. uint8_t control_bit = 1;
  181. uint8_t i = 0;
  182. uint8_t reserve = 0;
  183. #ifdef DEBUG_PRINT
  184. printf("_ACOUNTER : %d _BCOUNTER : %d \r\n",_ACOUNTER,_BCOUNTER);
  185. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  186. #endif /* DEBUG_PRINT */
  187. for(i = 0; i < 2; i++){
  188. if(control_bit & 0x01)
  189. ret += shift_bit << i;
  190. control_bit = control_bit >> 1;
  191. }
  192. #ifdef DEBUG_PRINT
  193. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  194. #endif /* DEBUG_PRINT */
  195. for(i = 2; i < 8; i++){
  196. if(_ACOUNTER & 0x01)
  197. ret += shift_bit << i;
  198. _ACOUNTER = _ACOUNTER >> 1;
  199. }
  200. #ifdef DEBUG_PRINT
  201. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  202. #endif /* DEBUG_PRINT */
  203. for(i = 8; i < 21; i++){
  204. if(_BCOUNTER & 0x01)
  205. ret += shift_bit << i;
  206. _BCOUNTER = _BCOUNTER >> 1;
  207. }
  208. #ifdef DEBUG_PRINT
  209. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  210. #endif /* DEBUG_PRINT */
  211. if(_CPGAIN & 0x01)
  212. ret += shift_bit << i++;
  213. for(i = 22; i < 24; i++){
  214. if(reserve & 0x01)
  215. ret += shift_bit << i;
  216. reserve = reserve >> 1;
  217. }
  218. #ifdef DEBUG_PRINT
  219. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  220. #endif /* DEBUG_PRINT */
  221. return ret;
  222. }
  223. void ADF4113_Module_Ctrl(PLL_Setting_st pll,uint32_t R0,uint32_t R1,uint32_t R2){
  224. R2 = R2 & 0xFFFFFF;
  225. R1 = R1 & 0xFFFFFF;
  226. R0 = R0 & 0xFFFFFF;
  227. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  228. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  229. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  230. /* R2 Ctrl */
  231. for(int i =0; i < 24; i++){
  232. if(R2 & 0x800000){
  233. #if 0 // PYJ.2019.08.11_BEGIN --
  234. printf("1");
  235. #endif // PYJ.2019.08.11_END --
  236. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  237. }
  238. else{
  239. #if 0 // PYJ.2019.08.11_BEGIN --
  240. printf("0");
  241. #endif // PYJ.2019.08.11_END --
  242. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  243. }
  244. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  245. Pol_Delay_us(10);
  246. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  247. R2 = ((R2 << 1) & 0xFFFFFF);
  248. }
  249. #if 0 // PYJ.2019.08.11_BEGIN --
  250. printf("\r\n");
  251. #endif // PYJ.2019.08.11_END --
  252. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  253. Pol_Delay_us(10);
  254. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  255. /* R0 Ctrl */
  256. for(int i =0; i < 24; i++){
  257. if(R0 & 0x800000){
  258. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  259. #if 0 // PYJ.2019.08.11_BEGIN --
  260. printf("1");
  261. #endif // PYJ.2019.08.11_END --
  262. }
  263. else{
  264. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  265. #if 0 // PYJ.2019.08.11_BEGIN --
  266. printf("0");
  267. #endif // PYJ.2019.08.11_END --
  268. }
  269. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  270. Pol_Delay_us(10);
  271. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  272. R0 = ((R0 << 1) & 0xFFFFFF);
  273. }
  274. #if 0 // PYJ.2019.08.11_BEGIN --
  275. printf("\r\n");
  276. #endif // PYJ.2019.08.11_END --
  277. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  278. Pol_Delay_us(10);
  279. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  280. /* R1 Ctrl */
  281. for(int i =0; i < 24; i++){
  282. if(R1 & 0x800000){
  283. #if 0 // PYJ.2019.08.11_BEGIN --
  284. printf("1");
  285. #endif // PYJ.2019.08.11_END --
  286. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  287. }
  288. else{
  289. #if 0 // PYJ.2019.08.11_BEGIN --
  290. printf("0");
  291. #endif // PYJ.2019.08.11_END --
  292. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  293. }
  294. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  295. Pol_Delay_us(10);
  296. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  297. R1 = ((R1 << 1) & 0xFFFFFF);
  298. }
  299. #if 0 // PYJ.2019.08.11_BEGIN --
  300. printf("\r\n");
  301. #endif // PYJ.2019.08.11_END --
  302. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  303. Pol_Delay_us(10);
  304. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  305. }