can_app.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. ******************************************************************************
  3. * @file can_app.c
  4. * @author Vincent
  5. * @version V1.0.0
  6. * @date 23-12-2015
  7. * @brief
  8. ******************************************************************************
  9. *
  10. * COPYRIGHT(c) 2015 STMicroelectronics
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "can_app.h"
  38. #include "uart_app.h"
  39. #include "RefreshMenu.h"
  40. #include "protocol.h"
  41. #include "string.h"
  42. #include "flash_if.h"
  43. #include "bin_check.h"
  44. /* Private variables ---------------------------------------------------------*/
  45. /*******************************************************************************
  46. * Function: void CAN_RxData_Process(CAN_Buf_TypeDef*ptCANRx)
  47. * Discrible:
  48. * Input:
  49. * Output:
  50. * Return:
  51. * Others:
  52. * date version author record
  53. * -----------------------------------------------
  54. * 20151107 V1.0 VINCENT Create
  55. *********************************************************************************/
  56. void CAN_RxData_Process(CAN_Buf_TypeDef* ptCANRx)
  57. {
  58. CD_UINT8 ucHeadHighByte,ucHeadLowByte,ucFrameMode;
  59. //CD_UINT8 file_headTail[PACKET_128B_SIZE]={0},file_body[PACKET_1K_SIZE]={0};
  60. CD_UINT16 i ;
  61. CD_UINT8 ucFrameLength,ucTrail;
  62. CD_UINT16 ucKeyCmd;
  63. CD_UINT32 ucCRCSum,CrcTemp;
  64. CD_UINT8 frameCrcBuff[FRAME_DATA_SIZE+2];
  65. if((*ptCANRx).ucBufCnt > 10)
  66. {
  67. /*清除超时标志*/
  68. // update.TimeCounter_Update = 0;
  69. ucHeadHighByte = cd_ReadChar(ptCANRx,0); //帧头1
  70. ucHeadLowByte = cd_ReadChar(ptCANRx,1); //帧头2
  71. if((PRO_HEAD1 == ucHeadHighByte)&&(PRO_HEAD2 == ucHeadLowByte))
  72. {
  73. ucFrameMode = cd_ReadChar(ptCANRx,2);
  74. ucFrameLength = cd_ReadChar(ptCANRx,3);
  75. ucKeyCmd = ((CD_UINT16)cd_ReadChar(ptCANRx,4) << 8 ) + ((CD_UINT16)cd_ReadChar(ptCANRx,5));
  76. if( FRAME_MODE_WRITE != ucFrameMode)
  77. {
  78. cd_DelChar(ptCANRx,1);
  79. return;
  80. }
  81. if((*ptCANRx).ucBufCnt <(ucFrameLength + 4 + 5 )) return;
  82. frameCrcBuff[0] = FRAME_HEAD1;
  83. frameCrcBuff[1] = FRAME_HEAD2;
  84. frameCrcBuff[2] = 0x07;
  85. frameCrcBuff[3] = 0x51;
  86. ucTrail = cd_ReadChar(ptCANRx,ucFrameLength + 4 + 4 );
  87. if(PRO_TAIL != ucTrail)
  88. {
  89. /*帧尾错误*/
  90. cd_DelChar(ptCANRx, 1);
  91. update.AckEnable = ENABLE;
  92. downLoad.userCmd = CMD_ACK_ERR;
  93. downLoad.step = 0xf0;
  94. return ;
  95. }
  96. ucCRCSum = (cd_ReadChar(ptCANRx,ucFrameLength + 4 ) << 24) + \
  97. (cd_ReadChar(ptCANRx,ucFrameLength + 4 + 1 ) << 16) + \
  98. (cd_ReadChar(ptCANRx,ucFrameLength + 4 + 2 ) << 8) + \
  99. cd_ReadChar(ptCANRx,ucFrameLength + 4 + 3 ) ;
  100. for(i=2; i< ( ucFrameLength + 4 ); i++)
  101. {
  102. frameCrcBuff[i+2] = cd_ReadChar(ptCANRx,i);
  103. }
  104. /*CRC 校验*/
  105. CrcTemp = CRC32_Calculate( (CD_UINT8 *)frameCrcBuff, (ucFrameLength + 4 + 2 ) );
  106. if( ucCRCSum != CrcTemp)
  107. {
  108. update.AckEnable = ENABLE;
  109. downLoad.userCmd = CMD_ACK_ERR;
  110. downLoad.step = 0xf1;
  111. cd_DelChar(ptCANRx, (ucFrameLength + 4 + 5 ));
  112. return ;
  113. }
  114. for(i=0; i< ( ucFrameLength-5); i++)
  115. {
  116. receice_data[i] = frameCrcBuff[i+13];
  117. }
  118. cd_DelChar(ptCANRx, (ucFrameLength + 4 + 5 ));
  119. update.TimeCounter_Update = 0;
  120. ptCANRx->ucCANEnable = 1;
  121. Data_Updata(ucKeyCmd, receice_data, &frameCrcBuff[9]);
  122. }
  123. else
  124. {
  125. cd_DelChar(ptCANRx, 1);
  126. }
  127. }
  128. }
  129. void CAN_sendBootInfo(void)
  130. {
  131. static CD_UINT8 bootInfoBuff[22]={ FRAME_HEAD1,FRAME_HEAD2,0x07,0x15,0x0C,0x0B,0xC1,0x09,'M','C',' ','V','2','.','6','.','0',0x00,0x00,0x00,0x00,0xF0 };//版本号: MC V1.3.0
  132. CD_UINT32 crc32Ret;
  133. //static CD_UINT8 ucInit_flag = 0;
  134. crc32Ret = CRC32_Calculate( (CD_UINT8 *)bootInfoBuff,17 );
  135. bootInfoBuff[17] = (CD_UINT8)( crc32Ret >> 24 );
  136. bootInfoBuff[18] = (CD_UINT8)( crc32Ret >> 16 );
  137. bootInfoBuff[19] = (CD_UINT8)( crc32Ret >> 8 );
  138. bootInfoBuff[20] = (CD_UINT8)( crc32Ret );
  139. //ready to send
  140. pTxMsg->rtr = 0;//0-data frame,1-remote frame
  141. pTxMsg->id = ((CAN_RxBuf_Struct1.ID & 0x7FF) << 18);//standard identifier
  142. //pTxMsg->id = ID & 0x1FFFFFFF;//extended identifier
  143. pTxMsg->xtd = 0;//0-11-bit standard identifier,1-29-bit extended identifier
  144. pTxMsg->esi = 0;
  145. pTxMsg->brs = 0;
  146. pTxMsg->fdf = 0;//0-Classic CAN format,1-CAN FD format
  147. pTxMsg->efc = 1;//1-Store Tx events
  148. //pTxMsg->mm = 0xAA;
  149. /*发送第1包数据*/
  150. pTxMsg->dlc = 8;
  151. pTxMsg->data[0] = bootInfoBuff[0];
  152. pTxMsg->data[1] = bootInfoBuff[1];
  153. for(int i=0;i<6;i++)
  154. {
  155. pTxMsg->data[i+2] = bootInfoBuff[i+4];
  156. }
  157. if(!cd_SentMsg(pTxMsg, 0)) return;
  158. /*发送第2包数据*/
  159. pTxMsg->dlc = 8;
  160. for(int i=0;i<8;i++)
  161. {
  162. pTxMsg->data[i] = bootInfoBuff[i+10];
  163. }
  164. if(!cd_SentMsg(pTxMsg, 1)) return;
  165. /*发送第3包数据*/
  166. pTxMsg->dlc = 4;
  167. for(int i=0;i<4;i++)
  168. {
  169. pTxMsg->data[i] = bootInfoBuff[i+18];
  170. }
  171. if(!cd_SentMsg(pTxMsg, 2)) return;
  172. }
  173. void CAN_sendAck(void)
  174. {
  175. static CD_UINT8 ackBuff_2[16]={ FRAME_HEAD1,FRAME_HEAD2,0x07,0x15,0x0C,0x05,0xA9,0x03,'A','C','K',0x00,0x00,0x00,0x00,0xF0 };
  176. CD_UINT32 crc32Ret;
  177. //uint8_t i;
  178. crc32Ret = CRC32_Calculate( (CD_UINT8 *)ackBuff_2,11 );
  179. ackBuff_2[11] = (CD_UINT8)( crc32Ret >> 24 );
  180. ackBuff_2[12] = (CD_UINT8)( crc32Ret >> 16 );
  181. ackBuff_2[13] = (CD_UINT8)( crc32Ret >> 8 );
  182. ackBuff_2[14] = (CD_UINT8)( crc32Ret );
  183. //ready to send
  184. pTxMsg->rtr = 0;//0-data frame,1-remote frame
  185. pTxMsg->id = ((CAN_RxBuf_Struct1.ID & 0x7FF) << 18);//standard identifier
  186. //pTxMsg->id = ID & 0x1FFFFFFF;//extended identifier
  187. pTxMsg->xtd = 0;//0-11-bit standard identifier,1-29-bit extended identifier
  188. pTxMsg->esi = 0;
  189. pTxMsg->brs = 0;
  190. pTxMsg->fdf = 0;//0-Classic CAN format,1-CAN FD format
  191. pTxMsg->efc = 1;//1-Store Tx events
  192. //pTxMsg->mm = 0xAA;
  193. /*发送第1包数据*/
  194. pTxMsg->dlc = 8;
  195. pTxMsg->data[0] = ackBuff_2[0];
  196. pTxMsg->data[1] = ackBuff_2[1];
  197. for(int i=0;i<6;i++)
  198. {
  199. pTxMsg->data[i+2] = ackBuff_2[i+4];
  200. }
  201. if(!cd_SentMsg(pTxMsg, 0)) return;
  202. /*发送第2包数据*/
  203. pTxMsg->dlc = 6;
  204. for(int i=0;i<6;i++)
  205. {
  206. pTxMsg->data[i] = ackBuff_2[i+10];
  207. }
  208. if(!cd_SentMsg(pTxMsg, 1)) return;
  209. }
  210. void CAN_sendUpdateAck( CD_UINT16 cmd ,CD_UINT16 packageNum )
  211. {
  212. static CD_UINT8 ackBuff[15]={ FRAME_HEAD1,FRAME_HEAD2,0x07,0x15,0x0C,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0 };
  213. CD_UINT32 crc32Ret;
  214. //static CD_UINT8 ucInit_flag = 0;
  215. //uint8_t i;
  216. ackBuff[6] = (CD_UINT8)(cmd >>8);
  217. ackBuff[7] = (CD_UINT8)cmd;
  218. ackBuff[8] = (CD_UINT8)packageNum;
  219. ackBuff[9] = (CD_UINT8)(packageNum>>8);
  220. crc32Ret = CRC32_Calculate( (CD_UINT8 *)ackBuff, 10);
  221. ackBuff[10] = (CD_UINT8)( crc32Ret >> 24 );
  222. ackBuff[11] = (CD_UINT8)( crc32Ret >> 16 );
  223. ackBuff[12] = (CD_UINT8)( crc32Ret >> 8 );
  224. ackBuff[13] = (CD_UINT8)( crc32Ret );
  225. //ready to send
  226. pTxMsg->rtr = 0;//0-data frame,1-remote frame
  227. pTxMsg->id = ((CAN_RxBuf_Struct1.ID & 0x7FF) << 18);//standard identifier
  228. //pTxMsg->id = ID & 0x1FFFFFFF;//extended identifier
  229. pTxMsg->xtd = 0;//0-11-bit standard identifier,1-29-bit extended identifier
  230. pTxMsg->esi = 0;
  231. pTxMsg->brs = 0;
  232. pTxMsg->fdf = 0;//0-Classic CAN format,1-CAN FD format
  233. pTxMsg->efc = 1;//1-Store Tx events
  234. //pTxMsg->mm = 0xAA;
  235. /*发送第1包数据*/
  236. pTxMsg->dlc = 8;
  237. pTxMsg->data[0] = ackBuff[0];
  238. pTxMsg->data[1] = ackBuff[1];
  239. for(int i=0;i<6;i++)
  240. {
  241. pTxMsg->data[i+2] = ackBuff[i+4];
  242. }
  243. if(!cd_SentMsg(pTxMsg, 0))
  244. return;
  245. /*发送第2包数据*/
  246. pTxMsg->dlc = 5;
  247. for(int i=0;i<5;i++)
  248. {
  249. pTxMsg->data[i] = ackBuff[i+10];
  250. }
  251. if(!cd_SentMsg(pTxMsg, 1))
  252. return;
  253. }
  254. /************************ (C) END OF FILE *********************************/