it6000_comm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. #include "plt.h"
  2. #define REG_SET_ON_OFF (1)
  3. #define REG_SET_DC_MODE (10)
  4. #define REG_SET_DC_V (11)
  5. #define REG_SET_DC_V_RISE (12)
  6. #define REG_SET_DC_V_FAIL (13)
  7. #define REG_SET_DC_C (14)
  8. #define REG_SET_DC_C_RISE (15)
  9. #define REG_SET_DC_C_FALL (16)
  10. #define REG_SET_DC_V_HIGH (17)
  11. #define REG_SET_DC_V_LOW (18)
  12. #define REG_SET_DC_I_HIGH (19)
  13. #define REG_SET_DC_I_LOW (20)
  14. #define REG_SET_DC_P_HIGH (23)
  15. #define REG_SET_DC_P_LOW (24)
  16. #define REG_SET_OVP_ENABLE (85)
  17. #define REG_SET_OVP (86)
  18. #define REG_SET_OVP_DELAY (87)
  19. #define REG_SET_OCP_ENABLE (88)
  20. #define REG_SET_OCP (89)
  21. #define REG_SET_OCP_DELAY (90)
  22. #define REG_SET_OPP_ENABLE (91)
  23. #define REG_SET_OPP (92)
  24. #define REG_SET_OPP_DELAY (93)
  25. #define REG_SET_UCP_ENABLE (94)
  26. #define REG_SET_UCP (95)
  27. #define REG_SET_UCP_DELAY (96)
  28. #define REG_SET_UCP_WAUMUP (97)
  29. #define REG_SET_UVP_ENABLE (98)
  30. #define REG_SET_UVP (99)
  31. #define REG_SET_UVP_DELAY (100)
  32. #define REG_SET_UVP_WAUMUP (101)
  33. #define REG_SET_CLEAR_PROT (112)
  34. inline static float it6000_reg_bigend2float(unsigned short regH,unsigned short regL)
  35. {
  36. float* ret;
  37. int val = 0;
  38. val += ((int)regH) << 16;
  39. val += regL;
  40. ret = (float *)&val;
  41. return *ret;
  42. }
  43. inline static int it6000_reg_bigend2int(unsigned short regH,unsigned short regL)
  44. {
  45. int val = 0;
  46. val += ((int)regH) << 16;
  47. val += regL;
  48. return val;
  49. }
  50. int it6000_comm_init(int idx)
  51. {
  52. struct it6000_t* dev = &it6000[idx];
  53. struct comm_t* comm = &dev->comm;
  54. comm_set_state( comm, COMMST_ERR );
  55. }
  56. int it6000_comm_reset(int idx)
  57. {
  58. struct it6000_t* dev = &it6000[idx];
  59. struct comm_t* comm = &dev->comm;
  60. comm_set_state( comm, COMMST_NORMAL );
  61. comm_set_dac_param_en(comm, 1);
  62. }
  63. int it6000_set_dev_current(int idx, float cur)
  64. {
  65. int ret = 0;
  66. struct it6000_t* dev = &it6000[idx];
  67. int chanidx = dev->comm.chanidx;
  68. int addr = dev->comm.adr;
  69. int regaddr = REG_SET_DC_C;
  70. unsigned short val_buf[2];
  71. val_buf[0] = *((int*)(&cur)) >> 16;
  72. val_buf[1] = *((int*)(&cur));
  73. // modbus tcp connection, no delay
  74. chan_lock(chanidx);
  75. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  76. chan_unlock(chanidx);
  77. if( ret < 0 ){
  78. log_dbg("%s, idx:%d, cur:%f, fail", __func__, idx, cur);
  79. }
  80. return ret;
  81. }
  82. int it6000_set_dev_startcmd( int idx )
  83. {
  84. int ret = 0;
  85. struct it6000_t* dev = &it6000[idx];
  86. int chanidx = dev->comm.chanidx;
  87. int addr = dev->comm.adr;
  88. int regaddr = REG_SET_ON_OFF;
  89. int nb = 1;
  90. int trycnt = 0;
  91. unsigned short val_buf[2];
  92. val_buf[0] = 0;
  93. val_buf[1] = 1;
  94. // modbus tcp connection, no delay
  95. chan_lock(chanidx);
  96. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  97. chan_unlock(chanidx);
  98. if( ret < 0 ){
  99. log_dbg("%s, idx:%d, fail", __func__, idx);
  100. }
  101. return ret;
  102. }
  103. int it6000_set_dev_stopcmd( int idx )
  104. {
  105. int ret = 0;
  106. struct it6000_t* dev = &it6000[idx];
  107. int chanidx = dev->comm.chanidx;
  108. int addr = dev->comm.adr;
  109. int regaddr = REG_SET_ON_OFF;
  110. int nb = 1;
  111. int trycnt = 0;
  112. unsigned short val_buf[2];
  113. val_buf[0] = 0;
  114. val_buf[1] = 0;
  115. // modbus tcp connection, no delay
  116. chan_lock(chanidx);
  117. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  118. chan_unlock(chanidx);
  119. if( ret < 0 ){
  120. log_dbg("%s, idx:%d, fail", __func__, idx);
  121. }
  122. return ret;
  123. }
  124. // send fault reset cmd to pcs
  125. // 0:invalid 1:reset
  126. int it6000_set_dev_resetcmd( int idx )
  127. {
  128. int ret = 0;
  129. struct it6000_t* dev = &it6000[idx];
  130. int chanidx = dev->comm.chanidx;
  131. int addr = dev->comm.adr;
  132. int regaddr = REG_SET_CLEAR_PROT;
  133. int nb = 1;
  134. int trycnt = 0;
  135. unsigned short val_buf[2];
  136. val_buf[0] = 0;
  137. val_buf[1] = 0;
  138. // modbus tcp connection, no delay
  139. chan_lock(chanidx);
  140. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  141. chan_unlock(chanidx);
  142. if( ret < 0 ){
  143. log_dbg("%s, idx:%d, fail", __func__, idx);
  144. }
  145. return ret;
  146. }
  147. //val,0:cv,1:cc
  148. int it6000_set_dev_dc_mode( int idx, int val )
  149. {
  150. int ret = 0;
  151. struct it6000_t* dev = &it6000[idx];
  152. int chanidx = dev->comm.chanidx;
  153. int addr = dev->comm.adr;
  154. int regaddr = REG_SET_DC_MODE;
  155. int nb = 1;
  156. int trycnt = 0;
  157. unsigned short val_buf[2];
  158. val_buf[0] = 0;
  159. val_buf[1] = val;
  160. // modbus tcp connection, no delay
  161. chan_lock(chanidx);
  162. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  163. chan_unlock(chanidx);
  164. if( ret < 0 ){
  165. log_dbg("%s, idx:%d, fail", __func__, idx);
  166. }
  167. return ret;
  168. }
  169. static void it6000_comm_dac_0x0001(int idx)
  170. {
  171. unsigned short tab_us[128]={0};
  172. struct it6000_t* dev = &it6000[idx];
  173. struct comm_t* comm = &dev->comm;
  174. int chanidx = dev->comm.chanidx;
  175. int addr = dev->comm.adr;
  176. int start, nb, rc;
  177. float* temp_f;
  178. int temp_i;
  179. if( comm_get_state(comm) != COMMST_NORMAL ){
  180. return;
  181. }
  182. nb = 118;
  183. start = 0x0001;
  184. chan_lock(chanidx);
  185. rc = chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us);
  186. if( rc < 0){
  187. comm_set_state(comm, COMMST_ERR);
  188. }
  189. chan_unlock(chanidx);
  190. if( rc == 0){ /* read ok */
  191. dev->sysStatus = it6000_reg_bigend2int(tab_us[1 - start],tab_us[2 - start]);
  192. if(dev->sysStatus == 0){
  193. sprintf(dev->szSysStatus,"%s","OFF");
  194. }else if(dev->sysStatus == 1){
  195. sprintf(dev->szSysStatus,"%s","ON");
  196. }else{
  197. sprintf(dev->szSysStatus,"%s","unkown");
  198. }
  199. dev->dc_mode = it6000_reg_bigend2int(tab_us[19 - start],tab_us[20 - start]);
  200. if(dev->dc_mode == 0){
  201. sprintf(dev->szDc_mode,"%s","CV");
  202. }else if(dev->dc_mode == 1){
  203. sprintf(dev->szDc_mode,"%s","CC");
  204. }else{
  205. sprintf(dev->szDc_mode,"%s","unkown");
  206. }
  207. //vol
  208. dev->dc_volt = it6000_reg_bigend2float(tab_us[21 - start],tab_us[22 - start]);
  209. dev->dc_volt_rise = it6000_reg_bigend2float(tab_us[23 - start],tab_us[24 - start]);
  210. dev->dc_volt_fall = it6000_reg_bigend2float(tab_us[25 - start],tab_us[26 - start]);
  211. dev->dc_current = it6000_reg_bigend2float(tab_us[27 - start],tab_us[28 - start]);
  212. dev->dc_current_rise = it6000_reg_bigend2float(tab_us[29 - start],tab_us[30 - start]);
  213. dev->dc_current_fall= it6000_reg_bigend2float(tab_us[31 - start],tab_us[32 - start]);
  214. //
  215. dev->dc_v_high = it6000_reg_bigend2float(tab_us[33 - start],tab_us[34 - start]);
  216. dev->dc_v_low = it6000_reg_bigend2float(tab_us[35 - start],tab_us[36 - start]);
  217. dev->dc_i_high = it6000_reg_bigend2float(tab_us[37 - start],tab_us[38 - start]);
  218. dev->dc_i_low = it6000_reg_bigend2float(tab_us[39 - start],tab_us[40 - start]);
  219. dev->cc_speed_state = it6000_reg_bigend2int(tab_us[41 - start],tab_us[42 - start]);
  220. dev->cv_speed_state = it6000_reg_bigend2int(tab_us[43 - start],tab_us[44 - start]);
  221. dev->power_high = it6000_reg_bigend2float(tab_us[45 - start],tab_us[46 - start]);
  222. dev->power_low = it6000_reg_bigend2float(tab_us[47 - start],tab_us[48 - start]);
  223. dev->meter_v_rms = it6000_reg_bigend2float(tab_us[107 - start],tab_us[108 - start]);
  224. dev->meter_i_rms = it6000_reg_bigend2float(tab_us[109 - start],tab_us[110 - start]);
  225. dev->meter_p_rms = it6000_reg_bigend2float(tab_us[111 - start],tab_us[112 - start]);
  226. dev->meter_op_reg = it6000_reg_bigend2int(tab_us[113 - start],tab_us[114 - start]);
  227. dev->meter_state_reg = it6000_reg_bigend2int(tab_us[115 - start],tab_us[116 - start]);
  228. dev->meter_ques_reg = it6000_reg_bigend2int(tab_us[117 - start],tab_us[118 - start]);
  229. }
  230. }
  231. // 0xF100 PCS internal config
  232. static void it6000_comm_dac_0x0059( int idx )
  233. {
  234. unsigned short tab_us[128]={0};
  235. struct it6000_t* dev = &it6000[idx];
  236. struct comm_t* comm = &dev->comm;
  237. int chanidx = dev->comm.chanidx;
  238. int addr = dev->comm.adr;
  239. int start, nb, rc;
  240. if( comm_get_state(comm) != COMMST_NORMAL ){
  241. return;
  242. }
  243. /* system info */
  244. nb = 112;
  245. start = 59;
  246. usleep(1000);
  247. chan_lock(chanidx);
  248. rc = chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us);
  249. if( rc < 0){
  250. comm_set_state(comm, COMMST_ERR);
  251. }
  252. chan_unlock(chanidx);
  253. if( rc == 0){ /* read ok */
  254. dev->dc_ovp_enable = it6000_reg_bigend2int(tab_us[111 - start],tab_us[112 - start]);
  255. dev->dc_ovp = it6000_reg_bigend2float(tab_us[113 - start],tab_us[114 - start]);
  256. dev->dc_ovp_delay = it6000_reg_bigend2int(tab_us[115 - start],tab_us[116 - start]);
  257. dev->dc_ocp_enable = it6000_reg_bigend2int(tab_us[117 - start],tab_us[118 - start]);
  258. dev->dc_ocp = it6000_reg_bigend2float(tab_us[119 - start],tab_us[120 - start]);
  259. dev->dc_ocp_delay = it6000_reg_bigend2int(tab_us[121 - start],tab_us[122 - start]);
  260. dev->dc_opp_enable = it6000_reg_bigend2int(tab_us[123 - start],tab_us[124 - start]);
  261. dev->dc_opp = it6000_reg_bigend2float(tab_us[125 - start],tab_us[126 - start]);
  262. dev->dc_opp_delay = it6000_reg_bigend2int(tab_us[127 - start],tab_us[128 - start]);
  263. dev->dc_ucp_enable = it6000_reg_bigend2int(tab_us[129 - start],tab_us[130 - start]);
  264. dev->dc_ucp = it6000_reg_bigend2float(tab_us[131 - start],tab_us[132 - start]);
  265. dev->dc_ucp_delay = it6000_reg_bigend2int(tab_us[133 - start],tab_us[134 - start]);
  266. dev->dc_ucp_warmup = it6000_reg_bigend2int(tab_us[135 - start],tab_us[136 - start]);
  267. dev->dc_uvp_enable = it6000_reg_bigend2int(tab_us[137 - start],tab_us[138 - start]);
  268. dev->dc_uvp = it6000_reg_bigend2float(tab_us[139 - start],tab_us[140 - start]);
  269. dev->dc_uvp_delay = it6000_reg_bigend2int(tab_us[141 - start],tab_us[142 - start]);
  270. dev->dc_uvp_warmup = it6000_reg_bigend2int(tab_us[143 - start],tab_us[144 - start]);
  271. dev->sw_prot = it6000_reg_bigend2int(tab_us[167 - start],tab_us[168 - start]);
  272. dev->hw_prot = it6000_reg_bigend2int(tab_us[169 - start],tab_us[170 - start]);
  273. }
  274. }
  275. int it6000_set_ovp_enable(int idx,int enable)
  276. {
  277. int ret = 0;
  278. struct it6000_t* dev = &it6000[idx];
  279. int chanidx = dev->comm.chanidx;
  280. int addr = dev->comm.adr;
  281. int regaddr = REG_SET_OVP_ENABLE;
  282. int nb = 1;
  283. int trycnt = 0;
  284. unsigned short val_buf[2];
  285. val_buf[0] = enable >> 16;
  286. val_buf[1] = enable;
  287. // modbus tcp connection, no delay
  288. chan_lock(chanidx);
  289. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  290. chan_unlock(chanidx);
  291. if( ret < 0 ){
  292. log_dbg("%s, idx:%d, fail", __func__, idx);
  293. }
  294. return ret;
  295. }
  296. int it6000_set_ovp(int idx,float ovp)
  297. {
  298. int ret = 0;
  299. struct it6000_t* dev = &it6000[idx];
  300. int chanidx = dev->comm.chanidx;
  301. int addr = dev->comm.adr;
  302. int regaddr = REG_SET_OVP;
  303. int nb = 1;
  304. int trycnt = 0;
  305. unsigned short val_buf[2];
  306. val_buf[0] = *((int *)(&ovp)) >> 16;
  307. val_buf[1] = *((int *)(&ovp));
  308. // modbus tcp connection, no delay
  309. chan_lock(chanidx);
  310. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  311. chan_unlock(chanidx);
  312. if( ret < 0 ){
  313. log_dbg("%s, idx:%d, fail", __func__, idx);
  314. }
  315. return ret;
  316. }
  317. int it6000_set_uvp_enable(int idx,int enable)
  318. {
  319. int ret = 0;
  320. struct it6000_t* dev = &it6000[idx];
  321. int chanidx = dev->comm.chanidx;
  322. int addr = dev->comm.adr;
  323. int regaddr = REG_SET_UVP_ENABLE;
  324. int nb = 1;
  325. int trycnt = 0;
  326. unsigned short val_buf[2];
  327. val_buf[0] = enable >> 16;
  328. val_buf[1] = enable;
  329. // modbus tcp connection, no delay
  330. chan_lock(chanidx);
  331. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  332. chan_unlock(chanidx);
  333. if( ret < 0 ){
  334. log_dbg("%s, idx:%d, fail", __func__, idx);
  335. }
  336. return ret;
  337. }
  338. int it6000_set_uvp(int idx,float uvp)
  339. {
  340. int ret = 0;
  341. struct it6000_t* dev = &it6000[idx];
  342. int chanidx = dev->comm.chanidx;
  343. int addr = dev->comm.adr;
  344. int regaddr = REG_SET_UVP;
  345. int nb = 1;
  346. int trycnt = 0;
  347. unsigned short val_buf[2];
  348. val_buf[0] = *((int *)(&uvp)) >> 16;
  349. val_buf[1] = *((int *)(&uvp));
  350. // modbus tcp connection, no delay
  351. chan_lock(chanidx);
  352. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  353. chan_unlock(chanidx);
  354. if( ret < 0 ){
  355. log_dbg("%s, idx:%d, fail", __func__, idx);
  356. }
  357. return ret;
  358. }
  359. int it6000_set_ocp_enable(int idx,int enable)
  360. {
  361. int ret = 0;
  362. struct it6000_t* dev = &it6000[idx];
  363. int chanidx = dev->comm.chanidx;
  364. int addr = dev->comm.adr;
  365. int regaddr = REG_SET_OCP_ENABLE;
  366. int nb = 1;
  367. int trycnt = 0;
  368. unsigned short val_buf[2];
  369. val_buf[0] = enable >> 16;
  370. val_buf[1] = enable;
  371. // modbus tcp connection, no delay
  372. chan_lock(chanidx);
  373. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  374. chan_unlock(chanidx);
  375. if( ret < 0 ){
  376. log_dbg("%s, idx:%d, fail", __func__, idx);
  377. }
  378. return ret;
  379. }
  380. int it6000_set_ocp(int idx,float ocp)
  381. {
  382. int ret = 0;
  383. struct it6000_t* dev = &it6000[idx];
  384. int chanidx = dev->comm.chanidx;
  385. int addr = dev->comm.adr;
  386. int regaddr = REG_SET_OCP;
  387. int nb = 1;
  388. int trycnt = 0;
  389. unsigned short val_buf[2];
  390. val_buf[0] = *((int *)(&ocp)) >> 16;
  391. val_buf[1] = *((int *)(&ocp));
  392. // modbus tcp connection, no delay
  393. chan_lock(chanidx);
  394. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  395. chan_unlock(chanidx);
  396. if( ret < 0 ){
  397. log_dbg("%s, idx:%d, fail", __func__, idx);
  398. }
  399. return ret;
  400. }
  401. int it6000_clear_protect(int idx)
  402. {
  403. int ret = 0;
  404. struct it6000_t* dev = &it6000[idx];
  405. int chanidx = dev->comm.chanidx;
  406. int addr = dev->comm.adr;
  407. int regaddr = REG_SET_CLEAR_PROT;
  408. int nb = 1;
  409. int trycnt = 0;
  410. unsigned short val_buf[2];
  411. val_buf[0] = 0;
  412. val_buf[1] = 0;
  413. // modbus tcp connection, no delay
  414. chan_lock(chanidx);
  415. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  416. chan_unlock(chanidx);
  417. if( ret < 0 ){
  418. log_dbg("%s, idx:%d, fail", __func__, idx);
  419. }
  420. return ret;
  421. }
  422. int it6000_set_vh(int idx,float vh)
  423. {
  424. int ret = 0;
  425. struct it6000_t* dev = &it6000[idx];
  426. int chanidx = dev->comm.chanidx;
  427. int addr = dev->comm.adr;
  428. int regaddr = REG_SET_DC_V_HIGH;
  429. int nb = 1;
  430. int trycnt = 0;
  431. unsigned short val_buf[2];
  432. val_buf[0] = *((int *)(&vh)) >> 16;
  433. val_buf[1] = *((int *)(&vh));
  434. // modbus tcp connection, no delay
  435. chan_lock(chanidx);
  436. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  437. chan_unlock(chanidx);
  438. if( ret < 0 ){
  439. log_dbg("%s, idx:%d, fail", __func__, idx);
  440. }
  441. return ret;
  442. }
  443. int it6000_set_vl(int idx,float vl)
  444. {
  445. int ret = 0;
  446. struct it6000_t* dev = &it6000[idx];
  447. int chanidx = dev->comm.chanidx;
  448. int addr = dev->comm.adr;
  449. int regaddr = REG_SET_DC_V_LOW;
  450. int nb = 1;
  451. int trycnt = 0;
  452. unsigned short val_buf[2];
  453. val_buf[0] = *((int *)(&vl)) >> 16;
  454. val_buf[1] = *((int *)(&vl));
  455. // modbus tcp connection, no delay
  456. chan_lock(chanidx);
  457. ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf);
  458. chan_unlock(chanidx);
  459. if( ret < 0 ){
  460. log_dbg("%s, idx:%d, fail", __func__, idx);
  461. }
  462. return ret;
  463. }
  464. void it6000_comm_dac( int idx )
  465. {
  466. struct it6000_t* dev = &it6000[idx];
  467. struct comm_t* comm = &dev->comm;
  468. unsigned short tab_us[128]={0};
  469. int start, nb;
  470. int chan_idx = dev->comm.chanidx;
  471. int addr = dev->comm.adr;
  472. int ret = 0;
  473. if( comm_get_state(comm) != COMMST_NORMAL ){
  474. return;
  475. }
  476. comm_start_cal_dac_timing(comm);
  477. it6000_comm_dac_0x0001(idx);
  478. it6000_comm_dac_0x0059(idx);
  479. }