CANNet.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /********************************************************************************
  2. * 常州易控汽车电子股份有限公司
  3. * (c) Copyright 2009-2015 ECTEK
  4. * 保留所有权利
  5. ********************************************************************************
  6. * 工程名称:整车管理
  7. * 文件名: CANNet.c
  8. * 功能描述 : CAN总线管理
  9. * 引用记录:
  10. * 备注:
  11. * 修订记录:
  12. * 日期 版本 作者 描述
  13. * 2011.07.14 00.01.00 李中 第一版
  14. *******************************************************************************/
  15. #include "HardwareLib.h"
  16. #include "CANNet.h"
  17. #include "AccPedCD.h"
  18. #include "BleCanProtocolFunc.h"
  19. #include "BcuCanProtocolFunc.h"
  20. #include "VcuCanProtocolFunc.h"
  21. MCANDrv_infoMsgConfS_TYPE CANNet_infoMsgTxConfSA[3];
  22. MCAN_stRetE_TYPE CAN1Result;
  23. uint16 CANNet_idxSchedUB;
  24. MCANDrv_infoMsgConfS_TYPE CANNet_InfoRxMsgConfSA[128];
  25. uint8 CANNet_datRxMsgBufM[128][9];
  26. CanMsgInfo infoVcuSendMsg;
  27. CanMsgInfo infoBmsSendMsg;
  28. volatile BOOL VcuSendFlg = FALSE;
  29. volatile BOOL BmsSendFlg = FALSE;
  30. void CANNet_Init(void)
  31. {
  32. /*示例代码*/
  33. /*各个CAN节点初始化*/
  34. MCANDrv_SetBaudrate(_CAN_INDEX_CAN0, 250000);
  35. MCANDrv_SetCANMode(_CAN_INDEX_CAN0, MCAN_NODEMODE_BUF);
  36. MCANDrv_SetCallbackFunc(_CAN_INDEX_CAN0, (pFunc_RxMsgCallback)CANNet_RecvMsgCAN0, NULL_PTR);
  37. MCANDrv_SetBaudrate(_CAN_INDEX_CAN1, 250000);
  38. MCANDrv_SetCANMode(_CAN_INDEX_CAN1, MCAN_NODEMODE_BUF);
  39. MCANDrv_SetCallbackFunc(_CAN_INDEX_CAN1, (pFunc_RxMsgCallback)CANNet_RecvMsgCAN1, NULL_PTR);
  40. /*第2路CAN用于CCP、UDS协议,由底层配置*/
  41. // MCANDrv_SetCallbackFunc(_CAN_INDEX_CAN2, (pFunc_RxMsgCallback)CANNet_RecvMsgCAN2, NULL_PTR);
  42. /*接收报文初始化,主要配置CAN2*/
  43. CANNet_InfoRxMsgConfSA[0].infoMsgIDUI = 0x1CEF0000UL;
  44. CANNet_InfoRxMsgConfSA[0].infoMsgParU.B.CAN = 2;
  45. CANNet_InfoRxMsgConfSA[0].infoMsgParU.B.MO = 3;
  46. CANNet_InfoRxMsgConfSA[0].infoMsgParU.B.DIR = _MCAN_DIR_RX;
  47. CANNet_InfoRxMsgConfSA[0].infoMsgParU.B.DLC = 8;
  48. CANNet_InfoRxMsgConfSA[0].infoMsgParU.B.IDE = _MCAN_IDE_EXT;
  49. MCANDrv_SetMsgObj(CANNet_InfoRxMsgConfSA[0], CANNet_RecvMsgCAN2);
  50. CANNet_InfoRxMsgConfSA[1].infoMsgIDUI = 0x1C00ECEAUL;
  51. CANNet_InfoRxMsgConfSA[1].infoMsgParU.B.CAN = 2;
  52. CANNet_InfoRxMsgConfSA[1].infoMsgParU.B.MO = 4;
  53. CANNet_InfoRxMsgConfSA[1].infoMsgParU.B.DIR = _MCAN_DIR_RX;
  54. CANNet_InfoRxMsgConfSA[1].infoMsgParU.B.DLC = 8;
  55. CANNet_InfoRxMsgConfSA[1].infoMsgParU.B.IDE = _MCAN_IDE_EXT;
  56. MCANDrv_SetMsgObj(CANNet_InfoRxMsgConfSA[1], CANNet_RecvMsgCAN2);
  57. /*发送报文初始化*/
  58. CANNet_infoMsgTxConfSA[0].infoMsgIDUI = 0x1F000024UL;
  59. CANNet_infoMsgTxConfSA[0].infoMsgParU.B.CAN = 0;
  60. CANNet_infoMsgTxConfSA[0].infoMsgParU.B.MO = 0;
  61. CANNet_infoMsgTxConfSA[0].infoMsgParU.B.DIR = _MCAN_DIR_TX;
  62. CANNet_infoMsgTxConfSA[0].infoMsgParU.B.DLC = 8;
  63. CANNet_infoMsgTxConfSA[0].infoMsgParU.B.IDE = _MCAN_IDE_EXT;
  64. // MCANDrv_SetMsgObj(CANNet_infoMsgTxConfSA[0], NULL_PTR);
  65. CANNet_infoMsgTxConfSA[1].infoMsgIDUI = 0x1F000025UL;
  66. CANNet_infoMsgTxConfSA[1].infoMsgParU.B.CAN = 1;
  67. CANNet_infoMsgTxConfSA[1].infoMsgParU.B.MO = 1;
  68. CANNet_infoMsgTxConfSA[1].infoMsgParU.B.DIR = _MCAN_DIR_TX;
  69. CANNet_infoMsgTxConfSA[1].infoMsgParU.B.DLC = 8;
  70. CANNet_infoMsgTxConfSA[1].infoMsgParU.B.IDE = _MCAN_IDE_EXT;
  71. // MCANDrv_SetMsgObj(CANNet_infoMsgTxConfSA[1], NULL_PTR);
  72. CANNet_infoMsgTxConfSA[2].infoMsgIDUI = 0x1F000026UL;
  73. CANNet_infoMsgTxConfSA[2].infoMsgParU.B.CAN = 2;
  74. CANNet_infoMsgTxConfSA[2].infoMsgParU.B.MO = 2;
  75. CANNet_infoMsgTxConfSA[2].infoMsgParU.B.DIR = _MCAN_DIR_TX;
  76. CANNet_infoMsgTxConfSA[2].infoMsgParU.B.DLC = 8;
  77. CANNet_infoMsgTxConfSA[2].infoMsgParU.B.IDE = _MCAN_IDE_EXT;
  78. }
  79. // CAN0接收消息处理函数 VCU
  80. void CANNet_RecvMsgCAN0(MCANDrv_infoMsgS_TYPE infoMsgS)
  81. {
  82. //直连状态下,CAN0空置,连接CAN1
  83. //非直连状态下,CAN0接收VCU消息
  84. VcuDecodeFunction(infoMsgS.infoConfS.infoMsgIDUI, infoMsgS.datMsgUBA);
  85. infoVcuSendMsg.CanID = infoMsgS.infoConfS.infoMsgIDUI;
  86. memcpy(infoVcuSendMsg.CanData,infoMsgS.datMsgUBA,8);
  87. VcuSendFlg = TRUE;
  88. if((infoVcuSendMsg.CanID&0x0000FFFF)==0x0000F3D0||(infoVcuSendMsg.CanID&0x0000FFFF)==0x000015F6||(infoVcuSendMsg.CanID&0x0000FFFF)==0x000051ED||(infoVcuSendMsg.CanID&0x0000FFFF)==0x1C00EDEA)
  89. {
  90. CANNet_infoMsgTxConfSA[1].infoMsgIDUI = infoVcuSendMsg.CanID;
  91. infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[1];
  92. memset(infoMsgS.datMsgUBA, 0x00, 8);
  93. if((infoVcuSendMsg.CanID==0x1802F3D0)&& 0)
  94. {
  95. static uint8 VcuLife = 0;
  96. uint8 chk = 0;
  97. VcuLife = (VcuLife+1)>14?0:(VcuLife+1);
  98. infoVcuSendMsg.CanData[1] = (BattHvCtl<<4)|(VcuLife&0x0F);
  99. for(uint8 i=1;i<8;i++)
  100. {
  101. chk = chk^infoVcuSendMsg.CanData[i];
  102. }
  103. infoVcuSendMsg.CanData[0] = chk;
  104. }
  105. memcpy(infoMsgS.datMsgUBA,infoVcuSendMsg.CanData,8);
  106. MCANDrv_SendMsg(infoMsgS);
  107. }
  108. }
  109. // CAN1接收消息处理函数
  110. void CANNet_RecvMsgCAN1(MCANDrv_infoMsgS_TYPE infoMsgS)
  111. {
  112. BcuDecodeFunction(infoMsgS.infoConfS.infoMsgIDUI, infoMsgS.datMsgUBA);
  113. infoBmsSendMsg.CanID = infoMsgS.infoConfS.infoMsgIDUI;
  114. memcpy(infoBmsSendMsg.CanData,infoMsgS.datMsgUBA,8);
  115. BmsSendFlg = TRUE;
  116. CANNet_infoMsgTxConfSA[0].infoMsgIDUI = infoBmsSendMsg.CanID;
  117. static uint8 TmsErrorFltTemp = 0;
  118. if((infoBmsSendMsg.CanID==0x1880D0F3)&& 0)//主负继电器状态和主正一起发出 加TMS故障屏蔽
  119. {
  120. uint8 DataTemp = 0x00;
  121. if(((infoBmsSendMsg.CanData[1]>>4)&0x03)==0x02)//主负继电器状态和主正一起发出
  122. {
  123. DataTemp = infoBmsSendMsg.CanData[1]&0x3F;
  124. DataTemp = DataTemp|((0x02)<<6);
  125. infoBmsSendMsg.CanData[1] = DataTemp;
  126. }
  127. else
  128. {
  129. DataTemp = infoBmsSendMsg.CanData[1]&0x3F;
  130. DataTemp = DataTemp|((0x01)<<6);
  131. infoBmsSendMsg.CanData[1] = DataTemp;
  132. }
  133. if(infoBmsSendMsg.CanData[7]==93)//TMS故障屏蔽
  134. {
  135. TmsErrorFltTemp = 1;
  136. clrbit(infoBmsSendMsg.CanData[4],6);
  137. infoBmsSendMsg.CanData[7] = 0;
  138. }
  139. }
  140. if((infoBmsSendMsg.CanID==0x1881D0F3)&& TmsErrorFltTemp)//屏蔽Tms出现的故障等级
  141. {
  142. uint8 chk = 0;
  143. uint8 ErrNum = 0;
  144. clrbit(infoBmsSendMsg.CanData[2],5);
  145. clrbit(infoBmsSendMsg.CanData[2],6);
  146. ErrNum = (infoBmsSendMsg.CanData[7]>>3);
  147. ErrNum = (ErrNum-1)<=0?0:(ErrNum-1);
  148. infoBmsSendMsg.CanData[7] = (infoBmsSendMsg.CanData[7]&0x03)|(ErrNum<<3);
  149. for(uint8 i=1;i<8;i++)
  150. {
  151. chk = chk^infoBmsSendMsg.CanData[i];
  152. }
  153. infoVcuSendMsg.CanData[0] = chk;
  154. }
  155. infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[0];
  156. memset(infoMsgS.datMsgUBA, 0x00, 8);
  157. memcpy(infoMsgS.datMsgUBA,infoBmsSendMsg.CanData,8);
  158. MCANDrv_SendMsg(infoMsgS);
  159. }
  160. /*备注,CAN2接收报文时,需要配置接收报文的MO号,如果不配置,将进入不了CAN中断*/
  161. void CANNet_RecvMsgCAN2(MCANDrv_infoMsgS_TYPE infoMsgS)
  162. {
  163. Can2ProtocolDecode(infoMsgS.infoConfS.infoMsgIDUI, infoMsgS.datMsgUBA);
  164. }
  165. //发送消息管理
  166. void CAN0Net_Manage1ms(void)
  167. {
  168. MCANDrv_infoMsgS_TYPE infoMsgS;
  169. if(BmsSendFlg){
  170. //BmsSendFlg = FALSE;
  171. }
  172. }
  173. void CAN1Net_Manage1ms(void)
  174. {
  175. static UINT32 Can1TimerCounter = 0;
  176. static UINT8 BcuIDIdx = 0;
  177. static BOOL BcuChkFlg = FALSE;
  178. MCANDrv_infoMsgS_TYPE infoMsgS;
  179. if (Can1TimerCounter % 100 == 0)
  180. {
  181. if (Can1TimerCounter % (EbcOutTable[BcuIDIdx][1]) == 0)
  182. {
  183. CANNet_infoMsgTxConfSA[1].infoMsgIDUI = EbcOutTable[BcuIDIdx][0];
  184. infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[1];
  185. memset(infoMsgS.datMsgUBA, 0x00, 8);
  186. BcuEncodeFunction(EbcOutTable[BcuIDIdx][0], &(infoMsgS.datMsgUBA[0]));
  187. MCANDrv_SendMsg(infoMsgS);
  188. }
  189. BcuIDIdx++;
  190. BcuChkFlg = FALSE;
  191. }
  192. else if(VcuSendFlg){
  193. //VcuSendFlg = FALSE;
  194. }
  195. if (BcuIDIdx >= (sizeof(EbcOutTable) / 8))
  196. {
  197. BcuIDIdx = 0;
  198. BcuChkFlg = TRUE;
  199. }
  200. if (BcuChkFlg)
  201. {
  202. Can1TimerCounter = Can1TimerCounter + 1;
  203. }
  204. }
  205. void CAN2Net_Manage1ms(void)
  206. {
  207. MCANDrv_infoMsgS_TYPE infoMsgS;
  208. static UINT32 Can2TimerCounter = 0;
  209. static uint8 Can2SendIdx = 0;
  210. BOOL Can2SendChkFlg = 0;
  211. if (Can2TimerCounter % 100 == 0)
  212. {
  213. if (Can2TimerCounter % (Can2OutTable[Can2SendIdx][1]) == 0)
  214. {
  215. CANNet_infoMsgTxConfSA[2].infoMsgIDUI = Can2OutTable[Can2SendIdx][0];
  216. infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[2];
  217. memset(infoMsgS.datMsgUBA, 0x00, 8);
  218. Can2EncodeFunction(Can2OutTable[Can2SendIdx][0], &(infoMsgS.datMsgUBA[0]));
  219. MCANDrv_SendMsg(infoMsgS);
  220. }
  221. Can2SendIdx++;
  222. Can2SendChkFlg = FALSE;
  223. }
  224. if (Can2SendIdx >= (sizeof(Can2OutTable) / 8))
  225. {
  226. Can2SendIdx = 0;
  227. Can2SendChkFlg = TRUE;
  228. }
  229. if (Can2SendChkFlg)
  230. {
  231. Can2TimerCounter = Can2TimerCounter + 1;
  232. }
  233. }