wm_efuse.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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: /*STA MAC*/
  322. memcpy(gftParam->wifi_mac_addr, (unsigned char *)data, len);
  323. break;
  324. case CMD_WIFI_MACAP: /*AP MAC*/
  325. memcpy(gftParam->wifi_macap_addr, (unsigned char *)data, len);
  326. break;
  327. case CMD_BT_MAC: /*BT MAC*/
  328. memcpy(gftParam->bt_mac_addr, (unsigned char *)data, len);
  329. break;
  330. case CMD_TX_DC: /*tx_dcoffset*/
  331. gftParam->tx_dcoffset = *(unsigned int *)data;
  332. break;
  333. case CMD_RX_DC: /*rx_dcoffset*/
  334. gftParam->rx_dcoffset = *(unsigned int *)data;
  335. break;
  336. case CMD_TX_IQ_GAIN:
  337. gftParam->tx_iq_gain = *(unsigned int *)data;
  338. break;
  339. case CMD_RX_IQ_GAIN:
  340. gftParam->rx_iq_gain = *(unsigned int *) data;
  341. break;
  342. case CMD_TX_IQ_PHASE:
  343. gftParam->tx_iq_phase = *(unsigned int *)data;
  344. break;
  345. case CMD_RX_IQ_PHASE:
  346. gftParam->rx_iq_phase = *(unsigned int *) data;
  347. break;
  348. case CMD_TX_GAIN: /*gain*/
  349. if (len >= FT_GAIN_LEN)
  350. {
  351. writelen = FT_GAIN_LEN;
  352. }
  353. else
  354. {
  355. writelen = len;
  356. }
  357. memcpy(gftParam->tx_gain, data, writelen);
  358. break;
  359. default:
  360. tls_mem_free(pft);
  361. printf("%s:%d return -1\n", __FILE__, __LINE__);
  362. return -1;
  363. }
  364. tls_crypto_init();
  365. /*len to write to flash*/
  366. writelen = sizeof(FT_PARAM_ST_VER1);
  367. tls_crypto_crc_init(&ctx, 0xFFFFFFFF, CRYPTO_CRC_TYPE_32, INPUT_REFLECT | OUTPUT_REFLECT);
  368. tls_crypto_crc_update(&ctx, (unsigned char *)&gftParamVer1.ft_ext1, gftParam->ext_param_len);
  369. tls_crypto_crc_final(&ctx, &gftParamVer1.ext_checksum);
  370. gftParam->magic_no = SIGNATURE_WORD;
  371. tls_crypto_crc_init(&ctx, 0xFFFFFFFF, CRYPTO_CRC_TYPE_32, INPUT_REFLECT | OUTPUT_REFLECT);
  372. tls_crypto_crc_update(&ctx, (unsigned char *)gftParam + 8, sizeof(FT_PARAM_ST) -8);
  373. tls_crypto_crc_final(&ctx, &gftParam->checksum);
  374. tls_flash_unlock();
  375. tls_fls_write(FT_PARAM_RUNTIME_ADDR, (unsigned char *)gftParam, writelen);
  376. // printf("tls_fls_write %08X %p %d %d\n", FT_PARAM_RUNTIME_ADDR, gftParam, writelen, ret);
  377. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  378. ret = _ft_param_init(FT_PARAM_RUNTIME_ADDR, pft);
  379. // printf("_ft_param_init %d\n", ret);
  380. if(!ret || memcmp(pft, gftParam, sizeof(FT_PARAM_ST_VER1)))
  381. {
  382. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  383. tls_mem_free(pft);
  384. printf("%s:%d return -1\n", __FILE__, __LINE__);
  385. return -1;
  386. }
  387. ret = tls_fls_write(FT_MAGICNUM_ADDR, (unsigned char *)gftParam, writelen);
  388. // printf("tls_fls_write2 %08X %p %d %d\n", FT_MAGICNUM_ADDR, gftParam, writelen, ret);
  389. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  390. ret = _ft_param_init(FT_MAGICNUM_ADDR, pft);
  391. // printf("_ft_param_init2 %d\n", ret);
  392. if(!ret || memcmp(pft, gftParam, sizeof(FT_PARAM_ST_VER1)))
  393. {
  394. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  395. tls_mem_free(pft);
  396. printf("%s:%d return -1\n", __FILE__, __LINE__);
  397. return -1;
  398. }
  399. memset(pft, 0xFF, sizeof(FT_PARAM_ST_VER1));
  400. tls_mem_free(pft);
  401. tls_flash_lock();
  402. return 0;
  403. }
  404. /**********************************************************************************************************
  405. * Description: This function is used to get mac addr.
  406. *
  407. * Arguments : mac mac addr,6 byte
  408. *
  409. * Returns : TLS_EFUSE_STATUS_OK get success
  410. * TLS_EFUSE_STATUS_EIO get failed
  411. **********************************************************************************************************/
  412. int tls_get_mac_addr(u8 *mac)
  413. {
  414. return tls_ft_param_get(CMD_WIFI_MAC, mac, 6);
  415. }
  416. /**********************************************************************************************************
  417. * Description: This function is used to set mac addr.
  418. *
  419. * Arguments : mac mac addr,6 byte
  420. *
  421. * Returns : TLS_EFUSE_STATUS_OK get success
  422. * TLS_EFUSE_STATUS_EIO get failed
  423. **********************************************************************************************************/
  424. int tls_set_mac_addr(u8 *mac)
  425. {
  426. return tls_ft_param_set(CMD_WIFI_MAC, mac, 6);
  427. }
  428. /**********************************************************************************************************
  429. * Description: This function is used to get bluetooth mac addr.
  430. *
  431. * Arguments : mac mac addr,6 byte
  432. *
  433. * Returns : TLS_EFUSE_STATUS_OK get success
  434. * TLS_EFUSE_STATUS_EIO get failed
  435. **********************************************************************************************************/
  436. int tls_get_bt_mac_addr(u8 *mac)
  437. {
  438. return tls_ft_param_get(CMD_BT_MAC, mac, 6);
  439. }
  440. /**********************************************************************************************************
  441. * Description: This function is used to set bluetooth mac addr.
  442. *
  443. * Arguments : mac mac addr,6 byte
  444. *
  445. * Returns : TLS_EFUSE_STATUS_OK get success
  446. * TLS_EFUSE_STATUS_EIO get failed
  447. **********************************************************************************************************/
  448. int tls_set_bt_mac_addr(u8 *mac)
  449. {
  450. return tls_ft_param_set(CMD_BT_MAC, mac, 6);
  451. }
  452. /**********************************************************************************************************
  453. * Description: This function is used to get tx lod.
  454. *
  455. * Arguments : *txlo
  456. *
  457. * Returns : 0 get success
  458. * -1 get failed
  459. **********************************************************************************************************/
  460. int tls_get_tx_lo(u8 *txlo)
  461. {
  462. return tls_ft_param_get(CMD_TX_DC, txlo, 4);
  463. }
  464. /**********************************************************************************************************
  465. * Description: This function is used to set tx lo.
  466. *
  467. * Arguments : txlo
  468. *
  469. * Returns : 0 set success
  470. * -1 set failed
  471. **********************************************************************************************************/
  472. int tls_set_tx_lo(u8 *txlo)
  473. {
  474. return tls_ft_param_set(CMD_TX_DC, txlo, 4);
  475. }
  476. /**********************************************************************************************************
  477. * Description: This function is used to get tx iq gain.
  478. *
  479. * Arguments : txGain
  480. *
  481. * Returns : 0 set success
  482. * -1 set failed
  483. **********************************************************************************************************/
  484. int tls_get_tx_iq_gain(u8 *txGain)
  485. {
  486. return tls_ft_param_get(CMD_TX_IQ_GAIN, txGain, 4);
  487. }
  488. /**********************************************************************************************************
  489. * Description: This function is used to set tx iq gain.
  490. *
  491. * Arguments : txGain
  492. *
  493. * Returns : 0 set success
  494. * -1 set failed
  495. **********************************************************************************************************/
  496. int tls_set_tx_iq_gain(u8 *txGain)
  497. {
  498. return tls_ft_param_set(CMD_TX_IQ_GAIN, txGain, 4);
  499. }
  500. /**********************************************************************************************************
  501. * Description: This function is used to get rx iq gain.
  502. *
  503. * Arguments : rxGain
  504. *
  505. * Returns : 0 set success
  506. * -1 set failed
  507. **********************************************************************************************************/
  508. int tls_get_rx_iq_gain(u8 *rxGain)
  509. {
  510. return tls_ft_param_get(CMD_RX_IQ_GAIN, rxGain, 4);
  511. }
  512. /**********************************************************************************************************
  513. * Description: This function is used to set rx iq gain.
  514. *
  515. * Arguments : rxGain
  516. *
  517. * Returns : 0 set success
  518. * -1 set failed
  519. **********************************************************************************************************/
  520. int tls_set_rx_iq_gain(u8 *rxGain)
  521. {
  522. return tls_ft_param_set(CMD_RX_IQ_GAIN, rxGain, 4);
  523. }
  524. /**********************************************************************************************************
  525. * Description: This function is used to get tx iq phase.
  526. *
  527. * Arguments : txPhase
  528. *
  529. * Returns : 0 set success
  530. * -1 set failed
  531. **********************************************************************************************************/
  532. int tls_get_tx_iq_phase(u8 *txPhase)
  533. {
  534. return tls_ft_param_get(CMD_TX_IQ_PHASE, txPhase, 4);
  535. }
  536. /**********************************************************************************************************
  537. * Description: This function is used to set tx iq phase.
  538. *
  539. * Arguments : txPhase
  540. *
  541. * Returns : 0 set success
  542. * -1 set failed
  543. **********************************************************************************************************/
  544. int tls_set_tx_iq_phase(u8 *txPhase)
  545. {
  546. return tls_ft_param_set(CMD_TX_IQ_PHASE, txPhase, 4);
  547. }
  548. /**********************************************************************************************************
  549. * Description: This function is used to get rx iq phase.
  550. *
  551. * Arguments : rxPhase
  552. *
  553. * Returns : 0 set success
  554. * -1 set failed
  555. **********************************************************************************************************/
  556. int tls_get_rx_iq_phase(u8 *rxPhase)
  557. {
  558. return tls_ft_param_get(CMD_RX_IQ_PHASE, rxPhase, 4);
  559. }
  560. /**********************************************************************************************************
  561. * Description: This function is used to set tx iq phase.
  562. *
  563. * Arguments : rxPhase
  564. *
  565. * Returns : 0 set success
  566. * -1 set failed
  567. **********************************************************************************************************/
  568. int tls_set_rx_iq_phase(u8 *rxPhase)
  569. {
  570. return tls_ft_param_set(CMD_RX_IQ_PHASE, rxPhase, 4);
  571. }
  572. int tls_freq_err_op(u8 *freqerr, u8 flag)
  573. {
  574. int ret = 0;
  575. int value = 0;
  576. if (flag){
  577. ret = tls_ft_param_set(CMD_FREQ_ERR, freqerr, FREQERR_LEN);
  578. }
  579. else
  580. {
  581. ret = tls_ft_param_get(CMD_FREQ_ERR, freqerr, FREQERR_LEN);
  582. if(!ret)
  583. {
  584. memcpy(&value, freqerr, FREQERR_LEN);
  585. if (value > 200000) /*when freq offset is out of range (-200KHz, 200KHz),do not use it*/
  586. {
  587. value = 200000;
  588. memcpy((char *)freqerr, (char *)&value, FREQERR_LEN);
  589. }
  590. else if (value < -200000)
  591. {
  592. value = -200000;
  593. memcpy((char *)freqerr, (char *)&value, FREQERR_LEN);
  594. }
  595. }
  596. }
  597. if (ret == 0)
  598. {
  599. return TLS_EFUSE_STATUS_OK;
  600. }
  601. else
  602. {
  603. return TLS_EFUSE_STATUS_EINVALID;
  604. }
  605. }
  606. int tls_rf_cal_finish_op(u8 *calflag, u8 flag)
  607. {
  608. int ret = 0;
  609. if (flag){
  610. ret = tls_ft_param_set(CMD_RF_CAL_FLAG, calflag, CAL_FLAG_LEN);
  611. }
  612. else
  613. {
  614. ret = tls_ft_param_get(CMD_RF_CAL_FLAG, calflag, CAL_FLAG_LEN);
  615. }
  616. if (ret == 0)
  617. {
  618. return TLS_EFUSE_STATUS_OK;
  619. }
  620. else
  621. {
  622. return TLS_EFUSE_STATUS_EINVALID;
  623. }
  624. }
  625. /**********************************************************************************************************
  626. * Description: This function is used to get tx gain.
  627. *
  628. * Arguments : txgain tx gain
  629. *
  630. * Returns : 0 get success
  631. * -1 get failed
  632. **********************************************************************************************************/
  633. int tls_get_tx_gain(u8 *txgain)
  634. {
  635. return tls_ft_param_get(CMD_TX_GAIN, txgain, TX_GAIN_LEN);
  636. }
  637. /**********************************************************************************************************
  638. * Description: This function is used to set tx gain.
  639. *
  640. * Arguments : txgain tx gain
  641. *
  642. * Returns : 0 set success
  643. * -1 set failed
  644. **********************************************************************************************************/
  645. int tls_set_tx_gain(u8 *txgain)
  646. {
  647. return tls_ft_param_set(CMD_TX_GAIN, txgain, TX_GAIN_LEN);
  648. }
  649. /**
  650. * @brief This function is used to get adc cal param
  651. *
  652. * @param[out] adc_cal adc cal param
  653. *
  654. * @retval TLS_EFUSE_STATUS_OK get success
  655. * @retval TLS_EFUSE_STATUS_EIO get failed
  656. */
  657. int tls_get_adc_cal_param(FT_ADC_CAL_ST *adc_cal)
  658. {
  659. return tls_ft_param_get(CMD_TX_ADC_CAL, adc_cal, sizeof(FT_ADC_CAL_ST));
  660. }
  661. /**
  662. * @brief This function is used to set adc cal param
  663. *
  664. * @param[out] adc_cal adc cal param
  665. *
  666. * @retval TLS_EFUSE_STATUS_OK get success
  667. * @retval TLS_EFUSE_STATUS_EIO get failed
  668. */
  669. int tls_set_adc_cal_param(FT_ADC_CAL_ST *adc_cal)
  670. {
  671. return tls_ft_param_set(CMD_TX_ADC_CAL, adc_cal, sizeof(FT_ADC_CAL_ST));
  672. }