dtsd1352_comm.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "plt.h"
  2. void dtsd1352_comm_dac( int idx )
  3. {
  4. unsigned short tab_us[128]={0};
  5. struct dtsd1352_t* dev = &dtsd1352[idx];
  6. struct comm_t* comm = &dev->comm;
  7. int chanidx = comm->chanidx;
  8. int addr = comm->adr;
  9. int start, nb, rc, i;
  10. int ret = 0;
  11. unsigned int tmp;
  12. if(comm_get_state(comm) != COMMST_NORMAL){
  13. return;
  14. }
  15. comm_start_cal_dac_timing(comm);
  16. if( comm_get_dac_param_en(comm) == 1 ){
  17. comm_set_dac_param_en(comm, 0);
  18. nb = 32;
  19. start = 0x0078;
  20. chan_lock(chanidx);
  21. usleep(100000); // 100ms
  22. rc = chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us);
  23. if( rc < 0){
  24. comm_set_state(comm, COMMST_ERR);
  25. }
  26. chan_unlock(chanidx);
  27. if( rc == 0 ){ // read ok
  28. dev->PT = tab_us[0x008D - start];
  29. dev->CT = tab_us[0x008E - start];
  30. }else{
  31. return;
  32. }
  33. }
  34. nb = 32;
  35. start = 0x0000;
  36. chan_lock(chanidx);
  37. usleep(100000); // 100ms
  38. rc = chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us);
  39. if( rc < 0){
  40. comm_set_state(comm, COMMST_ERR);
  41. }
  42. chan_unlock(chanidx);
  43. if( rc == 0 ){ // read ok
  44. dev->com_active_e = (tab_us[0x0000 - start]<<16 | tab_us[0x0001 - start])*dev->PT*dev->CT*0.01;
  45. dev->pos_active_e = (tab_us[0x000A - start]<<16 | tab_us[0x000B - start])*dev->PT*dev->CT*0.01;
  46. dev->neg_active_e = (tab_us[0x0014 - start]<<16 | tab_us[0x0015 - start])*dev->PT*dev->CT*0.01;
  47. }else{
  48. return;
  49. }
  50. nb = 23;
  51. start = 0x0061;
  52. chan_lock(chanidx);
  53. usleep(100000); // 100ms
  54. rc = chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us);
  55. if( rc < 0){
  56. comm_set_state(comm, COMMST_ERR);
  57. }
  58. chan_unlock(chanidx);
  59. if( rc == 0 ){ // read ok
  60. dev->ua = tab_us[0x0061 - start]*dev->PT*0.1;
  61. dev->ub = tab_us[0x0062 - start]*dev->PT*0.1;
  62. dev->uc = tab_us[0x0063 - start]*dev->PT*0.1;
  63. dev->ia = tab_us[0x0064 - start]*dev->CT*0.01;
  64. dev->ib = tab_us[0x0065 - start]*dev->CT*0.01;
  65. dev->ic = tab_us[0x0066 - start]*dev->CT*0.01;
  66. dev->freq = tab_us[0x0077 - start]*0.01;
  67. }else{
  68. return ;
  69. }
  70. nb = 24;
  71. start = 0x016A;
  72. chan_lock(chanidx);
  73. usleep(100000); // 100ms
  74. rc = chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us);
  75. if( rc < 0){
  76. comm_set_state(comm, COMMST_ERR);
  77. }
  78. chan_unlock(chanidx);
  79. if( rc == 0 ){ // read ok
  80. //dev->com_active_p = (int)((tab_us[0x016A - start]<<16 | tab_us[0x016B - start]))*dev->PT*dev->CT*0.001;
  81. tmp = tab_us[0x016A - start]<<16 | tab_us[0x016B - start];
  82. dev->com_active_p = (*(int*)(&tmp))*dev->PT*dev->CT*0.001;
  83. dev->pwr_factor = tab_us[0x017F - start]*0.001;
  84. }else{
  85. return;
  86. }
  87. #if 1
  88. nb = 8;
  89. start = 0x0198;
  90. chan_lock(chanidx);
  91. usleep(100000); // 100ms
  92. rc = chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us);
  93. if( rc < 0){
  94. comm_set_state(comm, COMMST_ERR);
  95. }
  96. chan_unlock(chanidx);
  97. if( rc == 0 ){ // read ok
  98. dev->pos_active_dem = tab_us[0x0198 - start];
  99. //comm_stop_cal_dac_timing(comm);
  100. }else{
  101. return;
  102. }
  103. #endif
  104. nb = 6;
  105. start = 0x05DD;
  106. chan_lock(chanidx);
  107. usleep(100000); // 100ms
  108. rc = chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us);
  109. if( rc < 0){
  110. comm_set_state(comm, COMMST_ERR);
  111. }
  112. chan_unlock(chanidx);
  113. if( rc == 0 ){ // read ok
  114. dev->THDUa = tab_us[0x05DD - start]*0.01;
  115. dev->THDUb = tab_us[0x05DE - start]*0.01;
  116. dev->THDUc = tab_us[0x05DF - start]*0.01;
  117. dev->THDIa = tab_us[0x05E0 - start]*0.01;
  118. dev->THDIb = tab_us[0x05E1 - start]*0.01;
  119. dev->THDIc = tab_us[0x05E2 - start]*0.01;
  120. }else{
  121. return;
  122. }
  123. nb = 28;
  124. start = 0x0697;
  125. chan_lock(chanidx);
  126. usleep(100000); // 100ms
  127. rc = chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us);
  128. if( rc < 0){
  129. comm_set_state(comm, COMMST_ERR);
  130. }
  131. chan_unlock(chanidx);
  132. if( rc == 0 ){ // read ok
  133. dev->total_fund_ap = tab_us[0x06A6 - start]*0.001;
  134. dev->total_fund_rp = tab_us[0x06AA - start]*0.001;
  135. dev->total_harm_ap = tab_us[0x06AE - start]*0.001;
  136. dev->total_harm_rp = tab_us[0x06B2 - start]*0.001;
  137. comm_stop_cal_dac_timing(comm);
  138. }else{
  139. return;
  140. }
  141. }
  142. int dtsd1352_set_dev_pt( int idx, int val)
  143. {
  144. int ret = 0;
  145. struct dtsd1352_t* dev = &dtsd1352[idx];
  146. struct comm_t* comm = &dev->comm;
  147. int chanidx = comm->chanidx;
  148. int addr = comm->adr;
  149. int regaddr = 0x008D;
  150. int nb = 1;
  151. chan_lock(chanidx);
  152. usleep(100000); // 100ms
  153. ret = chan_write_single_register_with_retry( chanidx, addr, regaddr, val);
  154. chan_unlock(chanidx);
  155. log_dbg("%s, idx:%d, val:%d, ret:%d", __func__, idx, val, ret);
  156. return ret;
  157. }
  158. int dtsd1352_set_dev_ct( int idx, int val)
  159. {
  160. int ret = 0;
  161. struct dtsd1352_t* dev = &dtsd1352[idx];
  162. struct comm_t* comm = &dev->comm;
  163. int chanidx = comm->chanidx;
  164. int addr = comm->adr;
  165. int regaddr = 0x008E;
  166. int nb = 1;
  167. chan_lock(chanidx);
  168. usleep(100000); // 100ms
  169. ret = chan_write_single_register_with_retry( chanidx, addr, regaddr, val);
  170. chan_unlock(chanidx);
  171. log_dbg("%s, idx:%d, val:%d, ret:%d", __func__, idx, val, ret);
  172. return ret;
  173. }
  174. int dtsd1352_comm_init(int idx)
  175. {
  176. struct dtsd1352_t* dev = &dtsd1352[idx];
  177. struct comm_t* comm = &dev->comm;
  178. comm_set_state( comm, COMMST_ERR );
  179. }
  180. int dtsd1352_comm_reset(int idx)
  181. {
  182. struct dtsd1352_t* dev = &dtsd1352[idx];
  183. struct comm_t* comm = &dev->comm;
  184. comm_set_state( comm, COMMST_NORMAL );
  185. comm_set_dac_param_en(comm, 1);
  186. }