wm_efuse.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /**************************************************************************
  2. * File Name : tls_efuse.c
  3. * Author :
  4. * Version :
  5. * Date :
  6. * Description : Use Flash Addr as virtual efuse
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
  9. * All rights reserved.
  10. *
  11. ***************************************************************************/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "wm_debug.h"
  16. #include "wm_regs.h"
  17. #include "wm_efuse.h"
  18. #include "wm_config.h"
  19. #include "list.h"
  20. #include "wm_internal_flash.h"
  21. #include "wm_crypto_hard.h"
  22. #include "wm_mem.h"
  23. #define USE_OTA_FT_PARAM 0
  24. #include "wm_flash_map.h"
  25. #define FT_MAGICNUM_ADDR (FLASH_BASE_ADDR)
  26. #define FT_PARAM_RUNTIME_ADDR (FT_MAGICNUM_ADDR + 0x1000)
  27. #define MAGICNUM_LEN (4)
  28. #define MAC_ADDR_LEN (6)
  29. #define FT_GAIN_LEN (84)
  30. #define FT_PARAM_EXT_REVERSED_LEN 32
  31. typedef struct FT_PARAM
  32. {
  33. unsigned int magic_no;
  34. unsigned int checksum;
  35. unsigned char wifi_mac_addr[MAC_ADDR_LEN];
  36. unsigned char wifi_macap_addr[MAC_ADDR_LEN];
  37. unsigned short version_no;
  38. unsigned char bt_mac_addr[MAC_ADDR_LEN];
  39. unsigned short ext_param_len;
  40. unsigned int tx_dcoffset;
  41. unsigned int rx_dcoffset;
  42. unsigned int tx_iq_gain;
  43. unsigned int rx_iq_gain;
  44. unsigned int tx_iq_phase;
  45. unsigned int rx_iq_phase;
  46. unsigned char tx_gain[FT_GAIN_LEN];
  47. }FT_PARAM_ST;
  48. typedef struct FT_PARAM_EXT_1
  49. {
  50. unsigned int rf_freq_err;
  51. unsigned int rf_cal_flag;
  52. FT_ADC_CAL_ST adc_cal_param;
  53. FT_TEMP_CAL_ST temp_cal_param;
  54. }FT_PARAM_ST_EXT_1;
  55. typedef struct FT_PARAM_VER1
  56. {
  57. FT_PARAM_ST ft_param;
  58. unsigned int ext_checksum;
  59. FT_PARAM_ST_EXT_1 ft_ext1;
  60. unsigned char _reversed[FT_PARAM_EXT_REVERSED_LEN];
  61. }FT_PARAM_ST_VER1;
  62. const u8 default_mac[6] = {0x00,0x25,0x08,0x09,0x01,0x0F};
  63. FT_PARAM_ST_VER1 gftParamVer1;
  64. FT_PARAM_ST *gftParam = (FT_PARAM_ST *)&gftParamVer1;
  65. static int _ft_ext1_valid(FT_PARAM_ST *pft)
  66. {
  67. //printf("version_no %d, ext_param_len %x\n", pft->version_no, pft->ext_param_len);
  68. if (pft->version_no > 0 && pft->version_no < 0xFFFF && pft->ext_param_len >= sizeof(FT_PARAM_ST_EXT_1) &&
  69. pft->ext_param_len <= (sizeof(FT_PARAM_ST_EXT_1) + FT_PARAM_EXT_REVERSED_LEN))
  70. {
  71. return TRUE;
  72. }
  73. return FALSE;
  74. }
  75. static int _ft_param_init(u32 ft_addr, FT_PARAM_ST *pft)
  76. {
  77. u32 crcvalue = 0;
  78. psCrcContext_t ctx;
  79. FT_PARAM_ST_VER1 *pft_ver1 = NULL;
  80. tls_fls_read(ft_addr, (unsigned char *)pft, sizeof(FT_PARAM_ST_VER1));
  81. if (pft->magic_no == SIGNATURE_WORD)
  82. {
  83. tls_crypto_init();
  84. tls_crypto_crc_init(&ctx, 0xFFFFFFFF, CRYPTO_CRC_TYPE_32, INPUT_REFLECT | OUTPUT_REFLECT);
  85. tls_crypto_crc_update(&ctx, (unsigned char *)pft + 8, sizeof(FT_PARAM_ST) - 8);
  86. tls_crypto_crc_final(&ctx, &crcvalue);
  87. if (pft->checksum != crcvalue)
  88. {
  89. //tls_mem_free(pft);
  90. return FALSE;
  91. }
  92. do
  93. {
  94. if (_ft_ext1_valid(pft))
  95. {
  96. pft_ver1 = (FT_PARAM_ST_VER1 *)pft;
  97. tls_crypto_crc_init(&ctx, 0xFFFFFFFF, CRYPTO_CRC_TYPE_32, INPUT_REFLECT | OUTPUT_REFLECT);
  98. tls_crypto_crc_update(&ctx, (unsigned char *)&pft_ver1->ft_ext1, pft->ext_param_len);
  99. tls_crypto_crc_final(&ctx, &crcvalue);
  100. if(pft_ver1->ext_checksum == crcvalue)
  101. {
  102. return 1;
  103. }
  104. else
  105. {
  106. return FALSE;
  107. }
  108. }
  109. pft->version_no = 0xFFFF;
  110. pft->ext_param_len = 0xFFFF;
  111. }while(0);
  112. return 2;
  113. }
  114. return FALSE;
  115. }
  116. int tls_ft_param_init(void)
  117. {
  118. int ret = 0;
  119. FT_PARAM_ST *pft = NULL;
  120. if (gftParam->magic_no == SIGNATURE_WORD)
  121. {
  122. return TRUE;
  123. }
  124. memset(gftParam, 0xFF, sizeof(FT_PARAM_ST_VER1));
  125. pft = tls_mem_alloc(sizeof(FT_PARAM_ST_VER1));
  126. if (pft == NULL)
  127. {
  128. return FALSE;
  129. }
  130. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  131. ret = _ft_param_init(FT_PARAM_RUNTIME_ADDR, pft);
  132. if(FALSE == ret)
  133. {
  134. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  135. ret = _ft_param_init(FT_MAGICNUM_ADDR, pft);
  136. }
  137. if(1 == ret)
  138. {
  139. memcpy((unsigned char *)gftParam, (unsigned char *)pft, sizeof(FT_PARAM_ST_VER1));
  140. }
  141. else if(2 == ret)
  142. {
  143. memcpy((unsigned char *)gftParam, (unsigned char *)pft, sizeof(FT_PARAM_ST));
  144. }
  145. tls_mem_free(pft);
  146. /*lock parameter*/
  147. tls_flash_unlock();
  148. return ret;
  149. }
  150. int tls_ft_param_get(unsigned int opnum, void *data, unsigned int rdlen)
  151. {
  152. int ret = 0;
  153. switch (opnum)
  154. {
  155. case CMD_FREQ_ERR:
  156. if(_ft_ext1_valid(gftParam))
  157. {
  158. memcpy(data, (char *)&gftParamVer1.ft_ext1.rf_freq_err, FREQERR_LEN);
  159. }
  160. else
  161. {
  162. ret = tls_fls_read(FREQERR_ADDR, data, FREQERR_LEN);
  163. if(ret)
  164. {
  165. return -1;
  166. }
  167. }
  168. break;
  169. case CMD_RF_CAL_FLAG:
  170. if(_ft_ext1_valid(gftParam))
  171. {
  172. memcpy(data, (char *)&gftParamVer1.ft_ext1.rf_cal_flag, CAL_FLAG_LEN);
  173. }
  174. else
  175. {
  176. ret = tls_fls_read(CAL_FLAG_ADDR, data, CAL_FLAG_LEN);
  177. if(ret)
  178. {
  179. return -1;
  180. }
  181. }
  182. break;
  183. case CMD_TX_ADC_CAL:
  184. if(_ft_ext1_valid(gftParam))
  185. {
  186. memcpy(data, (unsigned char *)&gftParamVer1.ft_ext1.adc_cal_param, rdlen);
  187. }
  188. else
  189. {
  190. return -1;
  191. }
  192. break;
  193. case CMD_WIFI_MAC: /*MAC*/
  194. if ((gftParam->wifi_mac_addr[0]&0x1)
  195. ||(0 == (gftParam->wifi_mac_addr[0]|gftParam->wifi_mac_addr[1]|gftParam->wifi_mac_addr[2]|gftParam->wifi_mac_addr[3]|gftParam->wifi_mac_addr[4]|gftParam->wifi_mac_addr[5])))
  196. {
  197. memcpy(data, default_mac, rdlen);
  198. }
  199. else
  200. {
  201. memcpy(data, gftParam->wifi_mac_addr, rdlen);
  202. }
  203. break;
  204. case CMD_WIFI_MACAP: /*MAC*/
  205. if ((gftParam->wifi_macap_addr[0]&0x1)
  206. ||(0 == (gftParam->wifi_macap_addr[0]|gftParam->wifi_macap_addr[1]|gftParam->wifi_macap_addr[2]|gftParam->wifi_macap_addr[3]|gftParam->wifi_macap_addr[4]|gftParam->wifi_macap_addr[5])))
  207. {
  208. memcpy(data, default_mac, rdlen);
  209. }
  210. else
  211. {
  212. memcpy(data, gftParam->wifi_macap_addr, rdlen);
  213. }
  214. break;
  215. case CMD_BT_MAC: /*MAC*/
  216. {
  217. u8 invalid_bt_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  218. u8 invalid_bt_mac1[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  219. if ((memcmp(gftParam->bt_mac_addr, invalid_bt_mac, 6) == 0)||(memcmp(gftParam->bt_mac_addr, invalid_bt_mac1, 6) == 0))
  220. {
  221. memcpy(data, default_mac, rdlen);
  222. *((u8*)data+5) +=1; /*defalut plus 1*/
  223. *((u8*)data) |= 0xC0; /*defalut public static type*/
  224. }
  225. else
  226. {
  227. memcpy(data, gftParam->bt_mac_addr, rdlen);
  228. }
  229. }
  230. break;
  231. case CMD_TX_DC: /*tx_dcoffset*/
  232. *(unsigned int *)data = gftParam->tx_dcoffset;
  233. break;
  234. case CMD_RX_DC: /*rx_dcoffset*/
  235. *(unsigned int *)data = gftParam->rx_dcoffset;
  236. break;
  237. case CMD_TX_IQ_GAIN:
  238. *(unsigned int *)data = gftParam->tx_iq_gain;
  239. break;
  240. case CMD_RX_IQ_GAIN:
  241. *(unsigned int *)data = gftParam->rx_iq_gain;
  242. break;
  243. case CMD_TX_IQ_PHASE:
  244. *(unsigned int *)data = gftParam->tx_iq_phase;
  245. break;
  246. case CMD_RX_IQ_PHASE:
  247. *(unsigned int *)data = gftParam->rx_iq_phase;
  248. break;
  249. case CMD_TX_GAIN: /*gain*/
  250. if (rdlen < FT_GAIN_LEN)
  251. {
  252. memcpy(data, gftParam->tx_gain, rdlen);
  253. }
  254. else
  255. {
  256. memcpy(data, gftParam->tx_gain, FT_GAIN_LEN);
  257. }
  258. break;
  259. default:
  260. return -1;
  261. }
  262. //printf("tls_ft_param_get: opnum=%d, val=%x\n", opnum, *(unsigned int *)data);
  263. return 0;
  264. }
  265. int tls_ft_param_set(unsigned int opnum, void *data, unsigned int len)
  266. {
  267. psCrcContext_t ctx;
  268. unsigned int writelen = 0;
  269. FT_PARAM_ST *pft = NULL;
  270. int ret = 0;
  271. tls_flash_unlock();
  272. if (!data || !len)
  273. {
  274. printf("%s:%d return -1\n", __FILE__, __LINE__);
  275. return -1;
  276. }
  277. pft = tls_mem_alloc(sizeof(FT_PARAM_ST_VER1));
  278. if (pft == NULL)
  279. {
  280. printf("%s:%d return -1\n", __FILE__, __LINE__);
  281. return -1;
  282. }
  283. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  284. ret = _ft_param_init(FT_PARAM_RUNTIME_ADDR, pft);
  285. if(ret)
  286. {
  287. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  288. ret = _ft_param_init(FT_MAGICNUM_ADDR, pft);
  289. if(!ret || memcmp(pft, gftParam, sizeof(FT_PARAM_ST_VER1)))
  290. {
  291. tls_flash_unlock();
  292. tls_fls_write(FT_MAGICNUM_ADDR, (unsigned char *)gftParam, sizeof(FT_PARAM_ST_VER1));
  293. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  294. ret = _ft_param_init(FT_MAGICNUM_ADDR, pft);
  295. if(!ret || memcmp(pft, gftParam, sizeof(FT_PARAM_ST_VER1)))
  296. {
  297. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  298. tls_mem_free(pft);
  299. printf("%s:%d return -1\n", __FILE__, __LINE__);
  300. return -1;
  301. }
  302. }
  303. }
  304. if(!_ft_ext1_valid(gftParam))
  305. {
  306. gftParam->version_no = 1;
  307. gftParam->ext_param_len = sizeof(FT_PARAM_ST_EXT_1);
  308. memset((char *)&gftParamVer1.ft_ext1, 0xFF, gftParam->ext_param_len);
  309. }
  310. switch (opnum)
  311. {
  312. case CMD_FREQ_ERR:
  313. memcpy((char *)&gftParamVer1.ft_ext1.rf_freq_err, (char *)data, FREQERR_LEN);
  314. break;
  315. case CMD_RF_CAL_FLAG:
  316. memcpy((char *)&gftParamVer1.ft_ext1.rf_cal_flag, (char *)data, CAL_FLAG_LEN);
  317. break;
  318. case CMD_TX_ADC_CAL:
  319. memcpy((unsigned char *)&gftParamVer1.ft_ext1.adc_cal_param, data, len);
  320. break;
  321. case CMD_WIFI_MAC: /*MAC*/
  322. memcpy(gftParam->wifi_mac_addr, (unsigned char *)data, len);
  323. break;
  324. case CMD_BT_MAC: /*BT MAC*/
  325. memcpy(gftParam->bt_mac_addr, (unsigned char *)data, len);
  326. break;
  327. case CMD_TX_DC: /*tx_dcoffset*/
  328. gftParam->tx_dcoffset = *(unsigned int *)data;
  329. break;
  330. case CMD_RX_DC: /*rx_dcoffset*/
  331. gftParam->rx_dcoffset = *(unsigned int *)data;
  332. break;
  333. case CMD_TX_IQ_GAIN:
  334. gftParam->tx_iq_gain = *(unsigned int *)data;
  335. break;
  336. case CMD_RX_IQ_GAIN:
  337. gftParam->rx_iq_gain = *(unsigned int *) data;
  338. break;
  339. case CMD_TX_IQ_PHASE:
  340. gftParam->tx_iq_phase = *(unsigned int *)data;
  341. break;
  342. case CMD_RX_IQ_PHASE:
  343. gftParam->rx_iq_phase = *(unsigned int *) data;
  344. break;
  345. case CMD_TX_GAIN: /*gain*/
  346. if (len >= FT_GAIN_LEN)
  347. {
  348. writelen = FT_GAIN_LEN;
  349. }
  350. else
  351. {
  352. writelen = len;
  353. }
  354. memcpy(gftParam->tx_gain, data, writelen);
  355. break;
  356. default:
  357. tls_mem_free(pft);
  358. printf("%s:%d return -1\n", __FILE__, __LINE__);
  359. return -1;
  360. }
  361. tls_crypto_init();
  362. /*len to write to flash*/
  363. writelen = sizeof(FT_PARAM_ST_VER1);
  364. tls_crypto_crc_init(&ctx, 0xFFFFFFFF, CRYPTO_CRC_TYPE_32, INPUT_REFLECT | OUTPUT_REFLECT);
  365. tls_crypto_crc_update(&ctx, (unsigned char *)&gftParamVer1.ft_ext1, gftParam->ext_param_len);
  366. tls_crypto_crc_final(&ctx, &gftParamVer1.ext_checksum);
  367. gftParam->magic_no = SIGNATURE_WORD;
  368. tls_crypto_crc_init(&ctx, 0xFFFFFFFF, CRYPTO_CRC_TYPE_32, INPUT_REFLECT | OUTPUT_REFLECT);
  369. tls_crypto_crc_update(&ctx, (unsigned char *)gftParam + 8, sizeof(FT_PARAM_ST) -8);
  370. tls_crypto_crc_final(&ctx, &gftParam->checksum);
  371. tls_flash_unlock();
  372. tls_fls_write(FT_PARAM_RUNTIME_ADDR, (unsigned char *)gftParam, writelen);
  373. // printf("tls_fls_write %08X %p %d %d\n", FT_PARAM_RUNTIME_ADDR, gftParam, writelen, ret);
  374. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  375. ret = _ft_param_init(FT_PARAM_RUNTIME_ADDR, pft);
  376. // printf("_ft_param_init %d\n", ret);
  377. if(!ret || memcmp(pft, gftParam, sizeof(FT_PARAM_ST_VER1)))
  378. {
  379. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  380. tls_mem_free(pft);
  381. printf("%s:%d return -1\n", __FILE__, __LINE__);
  382. return -1;
  383. }
  384. ret = tls_fls_write(FT_MAGICNUM_ADDR, (unsigned char *)gftParam, writelen);
  385. // printf("tls_fls_write2 %08X %p %d %d\n", FT_MAGICNUM_ADDR, gftParam, writelen, ret);
  386. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  387. ret = _ft_param_init(FT_MAGICNUM_ADDR, pft);
  388. // printf("_ft_param_init2 %d\n", ret);
  389. if(!ret || memcmp(pft, gftParam, sizeof(FT_PARAM_ST_VER1)))
  390. {
  391. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  392. tls_mem_free(pft);
  393. printf("%s:%d return -1\n", __FILE__, __LINE__);
  394. return -1;
  395. }
  396. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  397. tls_mem_free(pft);
  398. tls_flash_lock();
  399. return 0;
  400. }
  401. /**********************************************************************************************************
  402. * Description: This function is used to get mac addr.
  403. *
  404. * Arguments : mac mac addr,6 byte
  405. *
  406. * Returns : TLS_EFUSE_STATUS_OK get success
  407. * TLS_EFUSE_STATUS_EIO get failed
  408. **********************************************************************************************************/
  409. int tls_get_mac_addr(u8 *mac)
  410. {
  411. return tls_ft_param_get(CMD_WIFI_MAC, mac, 6);
  412. }
  413. /**********************************************************************************************************
  414. * Description: This function is used to set mac addr.
  415. *
  416. * Arguments : mac mac addr,6 byte
  417. *
  418. * Returns : TLS_EFUSE_STATUS_OK get success
  419. * TLS_EFUSE_STATUS_EIO get failed
  420. **********************************************************************************************************/
  421. int tls_set_mac_addr(u8 *mac)
  422. {
  423. return tls_ft_param_set(CMD_WIFI_MAC, mac, 6);
  424. }
  425. /**********************************************************************************************************
  426. * Description: This function is used to get bluetooth mac addr.
  427. *
  428. * Arguments : mac mac addr,6 byte
  429. *
  430. * Returns : TLS_EFUSE_STATUS_OK get success
  431. * TLS_EFUSE_STATUS_EIO get failed
  432. **********************************************************************************************************/
  433. int tls_get_bt_mac_addr(u8 *mac)
  434. {
  435. return tls_ft_param_get(CMD_BT_MAC, mac, 6);
  436. }
  437. /**********************************************************************************************************
  438. * Description: This function is used to set bluetooth mac addr.
  439. *
  440. * Arguments : mac mac addr,6 byte
  441. *
  442. * Returns : TLS_EFUSE_STATUS_OK get success
  443. * TLS_EFUSE_STATUS_EIO get failed
  444. **********************************************************************************************************/
  445. int tls_set_bt_mac_addr(u8 *mac)
  446. {
  447. return tls_ft_param_set(CMD_BT_MAC, mac, 6);
  448. }
  449. /**********************************************************************************************************
  450. * Description: This function is used to get tx lod.
  451. *
  452. * Arguments : *txlo
  453. *
  454. * Returns : 0 get success
  455. * -1 get failed
  456. **********************************************************************************************************/
  457. int tls_get_tx_lo(u8 *txlo)
  458. {
  459. return tls_ft_param_get(CMD_TX_DC, txlo, 4);
  460. }
  461. /**********************************************************************************************************
  462. * Description: This function is used to set tx lo.
  463. *
  464. * Arguments : txlo
  465. *
  466. * Returns : 0 set success
  467. * -1 set failed
  468. **********************************************************************************************************/
  469. int tls_set_tx_lo(u8 *txlo)
  470. {
  471. return tls_ft_param_set(CMD_TX_DC, txlo, 4);
  472. }
  473. /**********************************************************************************************************
  474. * Description: This function is used to get tx iq gain.
  475. *
  476. * Arguments : txGain
  477. *
  478. * Returns : 0 set success
  479. * -1 set failed
  480. **********************************************************************************************************/
  481. int tls_get_tx_iq_gain(u8 *txGain)
  482. {
  483. return tls_ft_param_get(CMD_TX_IQ_GAIN, txGain, 4);
  484. }
  485. /**********************************************************************************************************
  486. * Description: This function is used to set tx iq gain.
  487. *
  488. * Arguments : txGain
  489. *
  490. * Returns : 0 set success
  491. * -1 set failed
  492. **********************************************************************************************************/
  493. int tls_set_tx_iq_gain(u8 *txGain)
  494. {
  495. return tls_ft_param_set(CMD_TX_IQ_GAIN, txGain, 4);
  496. }
  497. /**********************************************************************************************************
  498. * Description: This function is used to get rx iq gain.
  499. *
  500. * Arguments : rxGain
  501. *
  502. * Returns : 0 set success
  503. * -1 set failed
  504. **********************************************************************************************************/
  505. int tls_get_rx_iq_gain(u8 *rxGain)
  506. {
  507. return tls_ft_param_get(CMD_RX_IQ_GAIN, rxGain, 4);
  508. }
  509. /**********************************************************************************************************
  510. * Description: This function is used to set rx iq gain.
  511. *
  512. * Arguments : rxGain
  513. *
  514. * Returns : 0 set success
  515. * -1 set failed
  516. **********************************************************************************************************/
  517. int tls_set_rx_iq_gain(u8 *rxGain)
  518. {
  519. return tls_ft_param_set(CMD_RX_IQ_GAIN, rxGain, 4);
  520. }
  521. /**********************************************************************************************************
  522. * Description: This function is used to get tx iq phase.
  523. *
  524. * Arguments : txPhase
  525. *
  526. * Returns : 0 set success
  527. * -1 set failed
  528. **********************************************************************************************************/
  529. int tls_get_tx_iq_phase(u8 *txPhase)
  530. {
  531. return tls_ft_param_get(CMD_TX_IQ_PHASE, txPhase, 4);
  532. }
  533. /**********************************************************************************************************
  534. * Description: This function is used to set tx iq phase.
  535. *
  536. * Arguments : txPhase
  537. *
  538. * Returns : 0 set success
  539. * -1 set failed
  540. **********************************************************************************************************/
  541. int tls_set_tx_iq_phase(u8 *txPhase)
  542. {
  543. return tls_ft_param_set(CMD_TX_IQ_PHASE, txPhase, 4);
  544. }
  545. /**********************************************************************************************************
  546. * Description: This function is used to get rx iq phase.
  547. *
  548. * Arguments : rxPhase
  549. *
  550. * Returns : 0 set success
  551. * -1 set failed
  552. **********************************************************************************************************/
  553. int tls_get_rx_iq_phase(u8 *rxPhase)
  554. {
  555. return tls_ft_param_get(CMD_RX_IQ_PHASE, rxPhase, 4);
  556. }
  557. /**********************************************************************************************************
  558. * Description: This function is used to set tx iq phase.
  559. *
  560. * Arguments : rxPhase
  561. *
  562. * Returns : 0 set success
  563. * -1 set failed
  564. **********************************************************************************************************/
  565. int tls_set_rx_iq_phase(u8 *rxPhase)
  566. {
  567. return tls_ft_param_set(CMD_RX_IQ_PHASE, rxPhase, 4);
  568. }
  569. int tls_freq_err_op(u8 *freqerr, u8 flag)
  570. {
  571. int ret = 0;
  572. int value = 0;
  573. if (flag){
  574. ret = tls_ft_param_set(CMD_FREQ_ERR, freqerr, FREQERR_LEN);
  575. }
  576. else
  577. {
  578. ret = tls_ft_param_get(CMD_FREQ_ERR, freqerr, FREQERR_LEN);
  579. if(!ret)
  580. {
  581. memcpy(&value, freqerr, FREQERR_LEN);
  582. if (value > 200000) /*when freq offset is out of range (-200KHz, 200KHz),do not use it*/
  583. {
  584. value = 200000;
  585. memcpy((char *)freqerr, (char *)&value, FREQERR_LEN);
  586. }
  587. else if (value < -200000)
  588. {
  589. value = -200000;
  590. memcpy((char *)freqerr, (char *)&value, FREQERR_LEN);
  591. }
  592. }
  593. }
  594. if (ret == 0)
  595. {
  596. return TLS_EFUSE_STATUS_OK;
  597. }
  598. else
  599. {
  600. return TLS_EFUSE_STATUS_EINVALID;
  601. }
  602. }
  603. int tls_rf_cal_finish_op(u8 *calflag, u8 flag)
  604. {
  605. int ret = 0;
  606. if (flag){
  607. ret = tls_ft_param_set(CMD_RF_CAL_FLAG, calflag, CAL_FLAG_LEN);
  608. }
  609. else
  610. {
  611. ret = tls_ft_param_get(CMD_RF_CAL_FLAG, calflag, CAL_FLAG_LEN);
  612. }
  613. if (ret == 0)
  614. {
  615. return TLS_EFUSE_STATUS_OK;
  616. }
  617. else
  618. {
  619. return TLS_EFUSE_STATUS_EINVALID;
  620. }
  621. }
  622. /**********************************************************************************************************
  623. * Description: This function is used to get tx gain.
  624. *
  625. * Arguments : txgain tx gain
  626. *
  627. * Returns : 0 get success
  628. * -1 get failed
  629. **********************************************************************************************************/
  630. int tls_get_tx_gain(u8 *txgain)
  631. {
  632. return tls_ft_param_get(CMD_TX_GAIN, txgain, TX_GAIN_LEN);
  633. }
  634. /**********************************************************************************************************
  635. * Description: This function is used to set tx gain.
  636. *
  637. * Arguments : txgain tx gain
  638. *
  639. * Returns : 0 set success
  640. * -1 set failed
  641. **********************************************************************************************************/
  642. int tls_set_tx_gain(u8 *txgain)
  643. {
  644. return tls_ft_param_set(CMD_TX_GAIN, txgain, TX_GAIN_LEN);
  645. }
  646. /**
  647. * @brief This function is used to get adc cal param
  648. *
  649. * @param[out] adc_cal adc cal param
  650. *
  651. * @retval TLS_EFUSE_STATUS_OK get success
  652. * @retval TLS_EFUSE_STATUS_EIO get failed
  653. */
  654. int tls_get_adc_cal_param(FT_ADC_CAL_ST *adc_cal)
  655. {
  656. return tls_ft_param_get(CMD_TX_ADC_CAL, adc_cal, sizeof(FT_ADC_CAL_ST));
  657. }
  658. /**
  659. * @brief This function is used to set adc cal param
  660. *
  661. * @param[out] adc_cal adc cal param
  662. *
  663. * @retval TLS_EFUSE_STATUS_OK get success
  664. * @retval TLS_EFUSE_STATUS_EIO get failed
  665. */
  666. int tls_set_adc_cal_param(FT_ADC_CAL_ST *adc_cal)
  667. {
  668. return tls_ft_param_set(CMD_TX_ADC_CAL, adc_cal, sizeof(FT_ADC_CAL_ST));
  669. }