#include "plt.h" #define REG_SET_ON_OFF (1) #define REG_SET_DC_MODE (10) #define REG_SET_DC_V (11) #define REG_SET_DC_V_RISE (12) #define REG_SET_DC_V_FAIL (13) #define REG_SET_DC_C (14) #define REG_SET_DC_C_RISE (15) #define REG_SET_DC_C_FALL (16) #define REG_SET_DC_V_HIGH (17) #define REG_SET_DC_V_LOW (18) #define REG_SET_DC_I_HIGH (19) #define REG_SET_DC_I_LOW (20) #define REG_SET_DC_P_HIGH (23) #define REG_SET_DC_P_LOW (24) #define REG_SET_OVP_ENABLE (85) #define REG_SET_OVP (86) #define REG_SET_OVP_DELAY (87) #define REG_SET_OCP_ENABLE (88) #define REG_SET_OCP (89) #define REG_SET_OCP_DELAY (90) #define REG_SET_OPP_ENABLE (91) #define REG_SET_OPP (92) #define REG_SET_OPP_DELAY (93) #define REG_SET_UCP_ENABLE (94) #define REG_SET_UCP (95) #define REG_SET_UCP_DELAY (96) #define REG_SET_UCP_WAUMUP (97) #define REG_SET_UVP_ENABLE (98) #define REG_SET_UVP (99) #define REG_SET_UVP_DELAY (100) #define REG_SET_UVP_WAUMUP (101) #define REG_SET_CLEAR_PROT (112) inline static float it6000_reg_bigend2float(unsigned short regH,unsigned short regL) { float* ret; int val = 0; val += ((int)regH) << 16; val += regL; ret = (float *)&val; return *ret; } inline static int it6000_reg_bigend2int(unsigned short regH,unsigned short regL) { int val = 0; val += ((int)regH) << 16; val += regL; return val; } int it6000_comm_init(int idx) { struct it6000_t* dev = &it6000[idx]; struct comm_t* comm = &dev->comm; comm_set_state( comm, COMMST_ERR ); } int it6000_comm_reset(int idx) { struct it6000_t* dev = &it6000[idx]; struct comm_t* comm = &dev->comm; comm_set_state( comm, COMMST_NORMAL ); comm_set_dac_param_en(comm, 1); } int it6000_set_dev_current(int idx, float cur) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_DC_C; unsigned short val_buf[2]; val_buf[0] = *((int*)(&cur)) >> 16; val_buf[1] = *((int*)(&cur)); // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, cur:%f, fail", __func__, idx, cur); } return ret; } int it6000_set_dev_startcmd( int idx ) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_ON_OFF; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = 0; val_buf[1] = 1; // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } int it6000_set_dev_stopcmd( int idx ) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_ON_OFF; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = 0; val_buf[1] = 0; // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } // send fault reset cmd to pcs // 0:invalid 1:reset int it6000_set_dev_resetcmd( int idx ) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_CLEAR_PROT; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = 0; val_buf[1] = 0; // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } //val,0:cv,1:cc int it6000_set_dev_dc_mode( int idx, int val ) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_DC_MODE; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = 0; val_buf[1] = val; // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } static void it6000_comm_dac_0x0001(int idx) { unsigned short tab_us[128]={0}; struct it6000_t* dev = &it6000[idx]; struct comm_t* comm = &dev->comm; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int start, nb, rc; float* temp_f; int temp_i; if( comm_get_state(comm) != COMMST_NORMAL ){ return; } nb = 118; start = 0x0001; chan_lock(chanidx); rc = chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us); if( rc < 0){ comm_set_state(comm, COMMST_ERR); } chan_unlock(chanidx); if( rc == 0){ /* read ok */ dev->sysStatus = it6000_reg_bigend2int(tab_us[1 - start],tab_us[2 - start]); if(dev->sysStatus == 0){ sprintf(dev->szSysStatus,"%s","OFF"); }else if(dev->sysStatus == 1){ sprintf(dev->szSysStatus,"%s","ON"); }else{ sprintf(dev->szSysStatus,"%s","unkown"); } dev->dc_mode = it6000_reg_bigend2int(tab_us[19 - start],tab_us[20 - start]); if(dev->dc_mode == 0){ sprintf(dev->szDc_mode,"%s","CV"); }else if(dev->dc_mode == 1){ sprintf(dev->szDc_mode,"%s","CC"); }else{ sprintf(dev->szDc_mode,"%s","unkown"); } //vol dev->dc_volt = it6000_reg_bigend2float(tab_us[21 - start],tab_us[22 - start]); dev->dc_volt_rise = it6000_reg_bigend2float(tab_us[23 - start],tab_us[24 - start]); dev->dc_volt_fall = it6000_reg_bigend2float(tab_us[25 - start],tab_us[26 - start]); dev->dc_current = it6000_reg_bigend2float(tab_us[27 - start],tab_us[28 - start]); dev->dc_current_rise = it6000_reg_bigend2float(tab_us[29 - start],tab_us[30 - start]); dev->dc_current_fall= it6000_reg_bigend2float(tab_us[31 - start],tab_us[32 - start]); // dev->dc_v_high = it6000_reg_bigend2float(tab_us[33 - start],tab_us[34 - start]); dev->dc_v_low = it6000_reg_bigend2float(tab_us[35 - start],tab_us[36 - start]); dev->dc_i_high = it6000_reg_bigend2float(tab_us[37 - start],tab_us[38 - start]); dev->dc_i_low = it6000_reg_bigend2float(tab_us[39 - start],tab_us[40 - start]); dev->cc_speed_state = it6000_reg_bigend2int(tab_us[41 - start],tab_us[42 - start]); dev->cv_speed_state = it6000_reg_bigend2int(tab_us[43 - start],tab_us[44 - start]); dev->power_high = it6000_reg_bigend2float(tab_us[45 - start],tab_us[46 - start]); dev->power_low = it6000_reg_bigend2float(tab_us[47 - start],tab_us[48 - start]); dev->meter_v_rms = it6000_reg_bigend2float(tab_us[107 - start],tab_us[108 - start]); dev->meter_i_rms = it6000_reg_bigend2float(tab_us[109 - start],tab_us[110 - start]); dev->meter_p_rms = it6000_reg_bigend2float(tab_us[111 - start],tab_us[112 - start]); dev->meter_op_reg = it6000_reg_bigend2int(tab_us[113 - start],tab_us[114 - start]); dev->meter_state_reg = it6000_reg_bigend2int(tab_us[115 - start],tab_us[116 - start]); dev->meter_ques_reg = it6000_reg_bigend2int(tab_us[117 - start],tab_us[118 - start]); } } // 0xF100 PCS internal config static void it6000_comm_dac_0x0059( int idx ) { unsigned short tab_us[128]={0}; struct it6000_t* dev = &it6000[idx]; struct comm_t* comm = &dev->comm; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int start, nb, rc; if( comm_get_state(comm) != COMMST_NORMAL ){ return; } /* system info */ nb = 112; start = 59; usleep(1000); chan_lock(chanidx); rc = chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us); if( rc < 0){ comm_set_state(comm, COMMST_ERR); } chan_unlock(chanidx); if( rc == 0){ /* read ok */ dev->dc_ovp_enable = it6000_reg_bigend2int(tab_us[111 - start],tab_us[112 - start]); dev->dc_ovp = it6000_reg_bigend2float(tab_us[113 - start],tab_us[114 - start]); dev->dc_ovp_delay = it6000_reg_bigend2int(tab_us[115 - start],tab_us[116 - start]); dev->dc_ocp_enable = it6000_reg_bigend2int(tab_us[117 - start],tab_us[118 - start]); dev->dc_ocp = it6000_reg_bigend2float(tab_us[119 - start],tab_us[120 - start]); dev->dc_ocp_delay = it6000_reg_bigend2int(tab_us[121 - start],tab_us[122 - start]); dev->dc_opp_enable = it6000_reg_bigend2int(tab_us[123 - start],tab_us[124 - start]); dev->dc_opp = it6000_reg_bigend2float(tab_us[125 - start],tab_us[126 - start]); dev->dc_opp_delay = it6000_reg_bigend2int(tab_us[127 - start],tab_us[128 - start]); dev->dc_ucp_enable = it6000_reg_bigend2int(tab_us[129 - start],tab_us[130 - start]); dev->dc_ucp = it6000_reg_bigend2float(tab_us[131 - start],tab_us[132 - start]); dev->dc_ucp_delay = it6000_reg_bigend2int(tab_us[133 - start],tab_us[134 - start]); dev->dc_ucp_warmup = it6000_reg_bigend2int(tab_us[135 - start],tab_us[136 - start]); dev->dc_uvp_enable = it6000_reg_bigend2int(tab_us[137 - start],tab_us[138 - start]); dev->dc_uvp = it6000_reg_bigend2float(tab_us[139 - start],tab_us[140 - start]); dev->dc_uvp_delay = it6000_reg_bigend2int(tab_us[141 - start],tab_us[142 - start]); dev->dc_uvp_warmup = it6000_reg_bigend2int(tab_us[143 - start],tab_us[144 - start]); dev->sw_prot = it6000_reg_bigend2int(tab_us[167 - start],tab_us[168 - start]); dev->hw_prot = it6000_reg_bigend2int(tab_us[169 - start],tab_us[170 - start]); } } int it6000_set_ovp_enable(int idx,int enable) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_OVP_ENABLE; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = enable >> 16; val_buf[1] = enable; // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } int it6000_set_ovp(int idx,float ovp) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_OVP; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = *((int *)(&ovp)) >> 16; val_buf[1] = *((int *)(&ovp)); // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } int it6000_set_uvp_enable(int idx,int enable) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_UVP_ENABLE; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = enable >> 16; val_buf[1] = enable; // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } int it6000_set_uvp(int idx,float uvp) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_UVP; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = *((int *)(&uvp)) >> 16; val_buf[1] = *((int *)(&uvp)); // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } int it6000_set_ocp_enable(int idx,int enable) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_OCP_ENABLE; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = enable >> 16; val_buf[1] = enable; // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } int it6000_set_ocp(int idx,float ocp) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_OCP; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = *((int *)(&ocp)) >> 16; val_buf[1] = *((int *)(&ocp)); // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } int it6000_clear_protect(int idx) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_CLEAR_PROT; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = 0; val_buf[1] = 0; // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } int it6000_set_vh(int idx,float vh) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_DC_V_HIGH; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = *((int *)(&vh)) >> 16; val_buf[1] = *((int *)(&vh)); // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } int it6000_set_vl(int idx,float vl) { int ret = 0; struct it6000_t* dev = &it6000[idx]; int chanidx = dev->comm.chanidx; int addr = dev->comm.adr; int regaddr = REG_SET_DC_V_LOW; int nb = 1; int trycnt = 0; unsigned short val_buf[2]; val_buf[0] = *((int *)(&vl)) >> 16; val_buf[1] = *((int *)(&vl)); // modbus tcp connection, no delay chan_lock(chanidx); ret = chan_write_multi_registers(chanidx,addr,regaddr,2,val_buf); chan_unlock(chanidx); if( ret < 0 ){ log_dbg("%s, idx:%d, fail", __func__, idx); } return ret; } void it6000_comm_dac( int idx ) { struct it6000_t* dev = &it6000[idx]; struct comm_t* comm = &dev->comm; unsigned short tab_us[128]={0}; int start, nb; int chan_idx = dev->comm.chanidx; int addr = dev->comm.adr; int ret = 0; if( comm_get_state(comm) != COMMST_NORMAL ){ return; } comm_start_cal_dac_timing(comm); it6000_comm_dac_0x0001(idx); it6000_comm_dac_0x0059(idx); }