123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616 |
- #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);
- }
|