cw1310_comm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "cw1310_comm.h"
  2. #include "cw1310.h"
  3. #include "cw1310_sm.h"
  4. #include "plt.h"
  5. void cw1310_comm_dac(int idx)
  6. {
  7. struct cw1310_t *dev = &cw1310[idx];
  8. struct comm_t *comm = &dev->comm;
  9. int chanidx = comm->chanidx;
  10. int addr = comm->adr;
  11. int ret = 0;
  12. if (comm_get_state(comm) != COMMST_NORMAL)
  13. {
  14. return;
  15. }
  16. comm_start_cal_dac_timing(comm);
  17. chan_socketcan_ringbuffer_element_t e;
  18. static chan_socketcan_ringbuffer_element_t e_temp[128];
  19. int tempQueueCount = 0;
  20. // e.frame.can_id = 0x18A201F6 & CAN_EFF_MASK;
  21. e.frame.can_id = 0x18A200F6 | (addr << 8);
  22. // e.frame.can_id = 0x18A200F6 | (0xFF << 8); //发送广播消息
  23. e.frame.can_id |= CAN_EFF_FLAG;
  24. e.frame.can_dlc = 2;
  25. e.frame.data[0] = 0x5A;
  26. e.frame.data[1] = 0x4E;
  27. e.frame.data[2] = 0;
  28. e.frame.data[3] = 0;
  29. e.frame.data[4] = 0;
  30. e.frame.data[5] = 0;
  31. e.frame.data[6] = 0;
  32. e.frame.data[7] = 0;
  33. chan_lock(chanidx);
  34. chan_socketcan_txrb_queue(chanidx, e);
  35. usleep(100000); // 100ms
  36. while (chan_socketcan_rxrb_num_items(chanidx) > 0)
  37. {
  38. // printf("idx : %d items count : %d\n", addr, chan_socketcan_rxrb_num_items(chanidx));
  39. if (chan_socketcan_rxrb_dequeue(chanidx, &e) > 0)
  40. {
  41. int fun = (e.frame.can_id >> 16) & 0xff;
  42. int msgAddr = (e.frame.can_id) & 0xff;
  43. if ((fun) == 0xB0)
  44. {
  45. if (msgAddr != addr)
  46. {
  47. // log_dbg("can msg on addr%d is received when receive addr%d", msgAddr, addr);
  48. e_temp[tempQueueCount] = e;
  49. tempQueueCount++;
  50. }
  51. else
  52. {
  53. struct firedetect_t *fd = &(dev->fa_pack);
  54. fd->t1 = e.frame.data[0] - 40;
  55. fd->t2 = e.frame.data[1] - 40;
  56. fd->co = e.frame.data[2] * 256 + e.frame.data[3];
  57. fd->voc = e.frame.data[4] * 256 + e.frame.data[5];
  58. fd->smoke_flag = e.frame.data[6];
  59. if (fd->smoke_flag == 0xAA)
  60. {
  61. sprintf(fd->szSmokeFlags, "normal");
  62. }
  63. else if (fd->smoke_flag == 0x55)
  64. {
  65. sprintf(fd->szSmokeFlags, "warning");
  66. }
  67. else
  68. {
  69. sprintf(fd->szSmokeFlags, "unexpected");
  70. }
  71. fd->fireLevel = (e.frame.data[7] & 0xf0) >> 4;
  72. fd->status_code = (e.frame.data[7] & 0x0f);
  73. if (fd->status_code == 0)
  74. {
  75. sprintf(fd->szStatus, "normal");
  76. }
  77. else if (fd->status_code == 1)
  78. {
  79. sprintf(fd->szStatus, "fe on");
  80. }
  81. else if (fd->status_code == 2)
  82. {
  83. sprintf(fd->szStatus, "sensor failure");
  84. }
  85. else if (fd->status_code == 3)
  86. {
  87. sprintf(fd->szStatus, "hw failure");
  88. }
  89. cw1310_check_warning(idx);
  90. }
  91. }
  92. }
  93. }
  94. if(tempQueueCount>0 && tempQueueCount<128)
  95. {
  96. chan_socketcan_rxrb_queue_arr(chanidx, e_temp, tempQueueCount); // 重新入队
  97. }
  98. chan_unlock(chanidx);
  99. comm_stop_cal_dac_timing(comm);
  100. }
  101. int testCount = 0;
  102. int cw1310_check_warning(int idx)
  103. {
  104. struct cw1310_t *dev = &cw1310[idx];
  105. struct firedetect_t *fd = &(dev->fa_pack);
  106. int level = 0;
  107. if(( (fd->co>=dev->level4_co || fd->voc>=dev->level4_voc) &&(fd->t1>=dev->level3_tmp || fd->t2>=dev->level3_tmp)) || fd->t1>=dev->level4_tmp || fd->t2>=dev->level4_tmp )
  108. {
  109. level = 4;
  110. }
  111. else if(fd->co>=dev->level3_co || fd->voc>=dev->level3_voc || fd->t1>=dev->level3_tmp || fd->t2>=dev->level3_tmp)
  112. {
  113. level = 3;
  114. }
  115. else if(fd->co>=dev->level2_co || fd->voc>=dev->level2_voc || fd->fireLevel>=4 || fd->t1>=dev->level2_tmp || fd->t2>=dev->level2_tmp)
  116. {
  117. level = 2;
  118. }
  119. else if(fd->co>=dev->level1_co || fd->voc>=dev->level1_voc || fd->smoke_flag == 0x55 || (fd->fireLevel>1 && fd->fireLevel<4) )
  120. {
  121. level = 1;
  122. }
  123. if (level != fd->lastCheck)
  124. {
  125. fd->lastCheck = level;
  126. testCount = 0;
  127. }
  128. else if (testCount++ > 15 && (fd->warningLevel < fd->lastCheck || fd->warningLevel < 3)) // 连续探测15个周期,确认状态
  129. {
  130. fd->warningLevel = fd->lastCheck;
  131. }
  132. }
  133. int cw1310_comm_init(int idx)
  134. {
  135. struct cw1310_t *dev = &cw1310[idx];
  136. struct comm_t *comm = &dev->comm;
  137. comm_set_state(comm, COMMST_ERR);
  138. }
  139. int cw1310_comm_reset(int idx)
  140. {
  141. struct cw1310_t *dev = &cw1310[idx];
  142. struct comm_t *comm = &dev->comm;
  143. comm_set_state(comm, COMMST_NORMAL);
  144. }