can_app.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "RefreshMenu.h"
  39. #include "protocol.h"
  40. #include "string.h"
  41. #include "flash_if.h"
  42. /* Private variables ---------------------------------------------------------*/
  43. /*******************************************************************************
  44. * Function: void CAN_RxData_Process(CAN_Buf_TypeDef*ptCANRx)
  45. * Discrible:
  46. * Input:
  47. * Output:
  48. * Return:
  49. * Others:
  50. * date version author record
  51. * -----------------------------------------------
  52. * 20151107 V1.0 VINCENT Create
  53. *********************************************************************************/
  54. void CAN_RxData_Process(CAN_Buf_TypeDef* ptCANRx)
  55. {
  56. CD_UINT8 ucHeadHighByte,ucHeadLowByte,ucFrameMode;
  57. //CD_UINT8 file_headTail[PACKET_128B_SIZE]={0},file_body[PACKET_1K_SIZE]={0};
  58. CD_UINT16 i ;
  59. CD_UINT8 ucFrameLength,ucTrail;
  60. CD_UINT16 ucKeyCmd;
  61. CD_UINT32 ucCRCSum,CrcTemp;
  62. static CD_UINT8 frameCrcBuff[FRAME_DATA_SIZE+2];
  63. if((*ptCANRx).ucBufCnt > 10)
  64. {
  65. /*清除超时标志*/
  66. // update.TimeCounter_Update = 0;
  67. ucHeadHighByte = cd_ReadChar(ptCANRx,0); //帧头1
  68. ucHeadLowByte = cd_ReadChar(ptCANRx,1); //帧头2
  69. if((PRO_HEAD1 == ucHeadHighByte)&&(PRO_HEAD2 == ucHeadLowByte))
  70. {
  71. ucFrameMode = cd_ReadChar(ptCANRx,2);
  72. ucFrameLength = cd_ReadChar(ptCANRx,3);
  73. ucKeyCmd = ((CD_UINT16)cd_ReadChar(ptCANRx,4) << 8 ) + ((CD_UINT16)cd_ReadChar(ptCANRx,5));
  74. if( FRAME_MODE_WRITE != ucFrameMode)
  75. {
  76. cd_DelChar(ptCANRx,1);
  77. return;
  78. }
  79. if((*ptCANRx).ucBufCnt <(ucFrameLength + 4 + 5 )) return;
  80. frameCrcBuff[0] = FRAME_HEAD1;
  81. frameCrcBuff[1] = FRAME_HEAD2;
  82. frameCrcBuff[2] = 0x07;
  83. frameCrcBuff[3] = 0x51;
  84. ucTrail = cd_ReadChar(ptCANRx,ucFrameLength + 4 + 4 );
  85. if(PRO_TAIL != ucTrail)
  86. {
  87. /*帧尾错误*/
  88. cd_DelChar(ptCANRx, 1);
  89. update.AckEnable = ENABLE;
  90. downLoad.userCmd = CMD_ACK_ERR;
  91. downLoad.step = 0xf0;
  92. return ;
  93. }
  94. ucCRCSum = (cd_ReadChar(ptCANRx,ucFrameLength + 4 ) << 24) + \
  95. (cd_ReadChar(ptCANRx,ucFrameLength + 4 + 1 ) << 16) + \
  96. (cd_ReadChar(ptCANRx,ucFrameLength + 4 + 2 ) << 8) + \
  97. cd_ReadChar(ptCANRx,ucFrameLength + 4 + 3 ) ;
  98. for(i=2; i< ( ucFrameLength + 4 ); i++)
  99. {
  100. frameCrcBuff[i+2] = cd_ReadChar(ptCANRx,i);
  101. }
  102. /*CRC 校验*/
  103. CrcTemp = CRC32_Calculate( (CD_UINT8 *)frameCrcBuff, (ucFrameLength + 4 + 2 ) );
  104. if( ucCRCSum != CrcTemp)
  105. {
  106. update.AckEnable = ENABLE;
  107. downLoad.userCmd = CMD_ACK_ERR;
  108. downLoad.step = 0xf1;
  109. cd_DelChar(ptCANRx, (ucFrameLength + 4 + 5 ));
  110. return ;
  111. }
  112. cd_DelChar(ptCANRx, (ucFrameLength + 4 + 5 ));
  113. update.TimeCounter_Update = 0;
  114. switch(ucKeyCmd)
  115. {
  116. case CMD_UPDATA: //升级指令
  117. //准备升级
  118. if(downLoad.step != 1)
  119. {
  120. downLoad.step = 1;
  121. update.AckEnable = ENABLE;
  122. downLoad.userCmd = CMD_ACK_REQ;
  123. downLoad.packageSize = *(uint32_t *)(frameCrcBuff + 15);
  124. /*擦除FLASH,剩余最后8k没有擦除,保留其他功能使用**/
  125. FLASH_If_Erase(APPLICATION_ADDRESS); //擦除时间约120ms
  126. /*清空flash写标志*/
  127. for(i=0;i<2048;i++)
  128. {
  129. flag_FlashWR[i] = 0;
  130. }
  131. /*清除超时标志*/
  132. update.TimeCounter_Startup = 0;
  133. }
  134. break;
  135. case CMD_PACKAGE_DATA: //数据包
  136. downLoad.step = 2;
  137. downLoad.dataBuff_128B = (CD_UINT8 *)frameCrcBuff + FRAME_HEAD + PACKAGE_DATA_HEAD + 2;
  138. downLoad.packagecount = (frameCrcBuff[10] << 8) + frameCrcBuff[9];
  139. if(downLoad.packagecount >= 2048)
  140. {
  141. downLoad.packagecount = 0;
  142. }
  143. downLoad.userCmd = CMD_ACK_OK;
  144. downLoad.wrenable = ENABLE;
  145. break;
  146. case CMD_UPDATA_END: //结束包
  147. downLoad.step = 3;
  148. update.AckEnable = ENABLE;
  149. downLoad.userCmd = CMD_ACK_EOT;
  150. break;
  151. case 0x2505:
  152. downLoad.step = 5;
  153. update.AckEnable = ENABLE;
  154. break;
  155. default:
  156. cd_DelChar(ptCANRx, 1);
  157. break;
  158. }
  159. }
  160. else
  161. {
  162. cd_DelChar(ptCANRx, 1);
  163. }
  164. }
  165. }
  166. void sendBootInfo(void)
  167. {
  168. static CD_UINT8 bootInfoBuff[22]={ FRAME_HEAD1,FRAME_HEAD2,0x07,0x15,0x0C,0x0B,0xC1,0x09,'M','C',' ','V','1','.','3','.','0',0x00,0x00,0x00,0x00,0xF0 };//版本号: MC V1.3.0
  169. CD_UINT32 crc32Ret;
  170. //static CD_UINT8 ucInit_flag = 0;
  171. uint8_t i;
  172. crc32Ret = CRC32_Calculate( (CD_UINT8 *)bootInfoBuff,17 );
  173. bootInfoBuff[17] = (CD_UINT8)( crc32Ret >> 24 );
  174. bootInfoBuff[18] = (CD_UINT8)( crc32Ret >> 16 );
  175. bootInfoBuff[19] = (CD_UINT8)( crc32Ret >> 8 );
  176. bootInfoBuff[20] = (CD_UINT8)( crc32Ret );
  177. //ready to send
  178. pTxMsg->tx_sfid = CAN_RxBuf_Struct1.ID;
  179. pTxMsg->tx_ft = CAN_FT_DATA;
  180. pTxMsg->tx_ff = CAN_FF_STANDARD;
  181. /*发送第1包数据*/
  182. pTxMsg->tx_dlen = 8;
  183. pTxMsg->tx_data[0] = bootInfoBuff[0];
  184. pTxMsg->tx_data[1] = bootInfoBuff[1];
  185. memcpy(pTxMsg->tx_data+2,bootInfoBuff+4,6);
  186. if(!cd_SentMsg(pTxMsg)) return;
  187. /*发送第2包数据*/
  188. pTxMsg->tx_dlen = 8;
  189. memcpy(pTxMsg->tx_data,bootInfoBuff+10,8);
  190. if(!cd_SentMsg(pTxMsg)) return;
  191. /*发送第3包数据*/
  192. pTxMsg->tx_dlen = 4;
  193. memcpy(pTxMsg->tx_data,bootInfoBuff+18,4);
  194. if(!cd_SentMsg(pTxMsg)) return;
  195. }
  196. void sendAck(void)
  197. {
  198. static CD_UINT8 ackBuff_2[16]={ FRAME_HEAD1,FRAME_HEAD2,0x07,0x15,0x0C,0x05,0xA9,0x03,'A','C','K',0x00,0x00,0x00,0x00,0xF0 };
  199. CD_UINT32 crc32Ret;
  200. //uint8_t i;
  201. crc32Ret = CRC32_Calculate( (CD_UINT8 *)ackBuff_2,11 );
  202. ackBuff_2[11] = (CD_UINT8)( crc32Ret >> 24 );
  203. ackBuff_2[12] = (CD_UINT8)( crc32Ret >> 16 );
  204. ackBuff_2[13] = (CD_UINT8)( crc32Ret >> 8 );
  205. ackBuff_2[14] = (CD_UINT8)( crc32Ret );
  206. //ready to send
  207. pTxMsg->tx_sfid = CAN_RxBuf_Struct1.ID;
  208. pTxMsg->tx_ft = CAN_FT_DATA;
  209. pTxMsg->tx_ff = CAN_FF_STANDARD;
  210. /*发送第1包数据*/
  211. pTxMsg->tx_dlen = 8;
  212. pTxMsg->tx_data[0] = ackBuff_2[0];
  213. pTxMsg->tx_data[1] = ackBuff_2[1];
  214. memcpy(pTxMsg->tx_data+2,ackBuff_2+4,6);
  215. if(!cd_SentMsg(pTxMsg)) return;
  216. /*发送第2包数据*/
  217. pTxMsg->tx_dlen = 6;
  218. memcpy(pTxMsg->tx_data,ackBuff_2+10,pTxMsg->tx_dlen);
  219. if(!cd_SentMsg(pTxMsg)) return;
  220. }
  221. void sendUpdateAck( CD_UINT16 cmd ,CD_UINT16 packageNum )
  222. {
  223. static CD_UINT8 ackBuff[15]={ FRAME_HEAD1,FRAME_HEAD2,0x07,0x15,0x0C,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0 };
  224. CD_UINT32 crc32Ret;
  225. //static CD_UINT8 ucInit_flag = 0;
  226. //uint8_t i;
  227. ackBuff[6] = (CD_UINT8)(cmd >>8);
  228. ackBuff[7] = (CD_UINT8)cmd;
  229. ackBuff[8] = (CD_UINT8)packageNum;
  230. ackBuff[9] = (CD_UINT8)(packageNum>>8);
  231. crc32Ret = CRC32_Calculate( (CD_UINT8 *)ackBuff, 10);
  232. ackBuff[10] = (CD_UINT8)( crc32Ret >> 24 );
  233. ackBuff[11] = (CD_UINT8)( crc32Ret >> 16 );
  234. ackBuff[12] = (CD_UINT8)( crc32Ret >> 8 );
  235. ackBuff[13] = (CD_UINT8)( crc32Ret );
  236. //ready to send
  237. pTxMsg->tx_sfid = CAN_RxBuf_Struct1.ID;
  238. pTxMsg->tx_ft = CAN_FT_DATA;
  239. pTxMsg->tx_ff = CAN_FF_STANDARD;
  240. /*发送第1包数据*/
  241. pTxMsg->tx_dlen = 8;
  242. pTxMsg->tx_data[0] = ackBuff[0];
  243. pTxMsg->tx_data[1] = ackBuff[1];
  244. memcpy(pTxMsg->tx_data+2,ackBuff+4,6);
  245. if(!cd_SentMsg(pTxMsg))
  246. return;
  247. /*发送第2包数据*/
  248. pTxMsg->tx_dlen = 5;
  249. memcpy(pTxMsg->tx_data,ackBuff+10,pTxMsg->tx_dlen);
  250. if(!cd_SentMsg(pTxMsg))
  251. return;
  252. }
  253. /************************ (C) END OF FILE *********************************/