123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- /**
- ******************************************************************************
- * @file can_app.c
- * @author Vincent
- * @version V1.0.0
- * @date 23-12-2015
- * @brief
- ******************************************************************************
- *
- * COPYRIGHT(c) 2015 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "can_app.h"
- #include "uart_app.h"
- #include "RefreshMenu.h"
- #include "protocol.h"
- #include "string.h"
- #include "flash_if.h"
- #include "bin_check.h"
- /* Private variables ---------------------------------------------------------*/
- /*******************************************************************************
- * Function: void CAN_RxData_Process(CAN_Buf_TypeDef*ptCANRx)
- * Discrible:
- * Input:
- * Output:
- * Return:
- * Others:
- * date version author record
- * -----------------------------------------------
- * 20151107 V1.0 VINCENT Create
- *********************************************************************************/
- void CAN_RxData_Process(CAN_Buf_TypeDef* ptCANRx)
- {
- CD_UINT8 ucHeadHighByte,ucHeadLowByte,ucFrameMode;
- //CD_UINT8 file_headTail[PACKET_128B_SIZE]={0},file_body[PACKET_1K_SIZE]={0};
- CD_UINT16 i ;
-
- CD_UINT8 ucFrameLength,ucTrail;
- CD_UINT16 ucKeyCmd;
- CD_UINT32 ucCRCSum,CrcTemp;
-
- CD_UINT8 frameCrcBuff[FRAME_DATA_SIZE+2];
- if((*ptCANRx).ucBufCnt > 10)
- {
- /*清除超时标志*/
- // update.TimeCounter_Update = 0;
-
- ucHeadHighByte = cd_ReadChar(ptCANRx,0); //帧头1
- ucHeadLowByte = cd_ReadChar(ptCANRx,1); //帧头2
-
-
- if((PRO_HEAD1 == ucHeadHighByte)&&(PRO_HEAD2 == ucHeadLowByte))
- {
-
- ucFrameMode = cd_ReadChar(ptCANRx,2);
- ucFrameLength = cd_ReadChar(ptCANRx,3);
- ucKeyCmd = ((CD_UINT16)cd_ReadChar(ptCANRx,4) << 8 ) + ((CD_UINT16)cd_ReadChar(ptCANRx,5));
-
- if( FRAME_MODE_WRITE != ucFrameMode)
- {
- cd_DelChar(ptCANRx,1);
- return;
- }
-
- if((*ptCANRx).ucBufCnt <(ucFrameLength + 4 + 5 )) return;
-
- frameCrcBuff[0] = FRAME_HEAD1;
- frameCrcBuff[1] = FRAME_HEAD2;
- frameCrcBuff[2] = 0x07;
- frameCrcBuff[3] = 0x51;
-
- ucTrail = cd_ReadChar(ptCANRx,ucFrameLength + 4 + 4 );
-
- if(PRO_TAIL != ucTrail)
- {
- /*帧尾错误*/
- cd_DelChar(ptCANRx, 1);
- update.AckEnable = ENABLE;
- downLoad.userCmd = CMD_ACK_ERR;
- downLoad.step = 0xf0;
- return ;
- }
-
- ucCRCSum = (cd_ReadChar(ptCANRx,ucFrameLength + 4 ) << 24) + \
- (cd_ReadChar(ptCANRx,ucFrameLength + 4 + 1 ) << 16) + \
- (cd_ReadChar(ptCANRx,ucFrameLength + 4 + 2 ) << 8) + \
- cd_ReadChar(ptCANRx,ucFrameLength + 4 + 3 ) ;
- for(i=2; i< ( ucFrameLength + 4 ); i++)
- {
- frameCrcBuff[i+2] = cd_ReadChar(ptCANRx,i);
- }
-
- /*CRC 校验*/
- CrcTemp = CRC32_Calculate( (CD_UINT8 *)frameCrcBuff, (ucFrameLength + 4 + 2 ) );
- if( ucCRCSum != CrcTemp)
- {
- update.AckEnable = ENABLE;
- downLoad.userCmd = CMD_ACK_ERR;
- downLoad.step = 0xf1;
-
- cd_DelChar(ptCANRx, (ucFrameLength + 4 + 5 ));
- return ;
- }
-
- for(i=0; i< ( ucFrameLength-5); i++)
- {
- receice_data[i] = frameCrcBuff[i+13];
- }
- cd_DelChar(ptCANRx, (ucFrameLength + 4 + 5 ));
- update.TimeCounter_Update = 0;
- ptCANRx->ucCANEnable = 1;
- Data_Updata(ucKeyCmd, receice_data, &frameCrcBuff[9]);
- }
- else
- {
- cd_DelChar(ptCANRx, 1);
- }
-
- }
- }
- void CAN_sendBootInfo(void)
- {
- 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
- CD_UINT32 crc32Ret;
- //static CD_UINT8 ucInit_flag = 0;
-
- crc32Ret = CRC32_Calculate( (CD_UINT8 *)bootInfoBuff,17 );
-
- bootInfoBuff[17] = (CD_UINT8)( crc32Ret >> 24 );
- bootInfoBuff[18] = (CD_UINT8)( crc32Ret >> 16 );
- bootInfoBuff[19] = (CD_UINT8)( crc32Ret >> 8 );
- bootInfoBuff[20] = (CD_UINT8)( crc32Ret );
-
- //ready to send
- pTxMsg->rtr = 0;//0-data frame,1-remote frame
- pTxMsg->id = ((CAN_RxBuf_Struct1.ID & 0x7FF) << 18);//standard identifier
- //pTxMsg->id = ID & 0x1FFFFFFF;//extended identifier
- pTxMsg->xtd = 0;//0-11-bit standard identifier,1-29-bit extended identifier
- pTxMsg->esi = 0;
- pTxMsg->brs = 0;
- pTxMsg->fdf = 0;//0-Classic CAN format,1-CAN FD format
- pTxMsg->efc = 1;//1-Store Tx events
- //pTxMsg->mm = 0xAA;
-
- /*发送第1包数据*/
- pTxMsg->dlc = 8;
-
- pTxMsg->data[0] = bootInfoBuff[0];
- pTxMsg->data[1] = bootInfoBuff[1];
- for(int i=0;i<6;i++)
- {
- pTxMsg->data[i+2] = bootInfoBuff[i+4];
- }
- if(!cd_SentMsg(pTxMsg, 0)) return;
- /*发送第2包数据*/
- pTxMsg->dlc = 8;
- for(int i=0;i<8;i++)
- {
- pTxMsg->data[i] = bootInfoBuff[i+10];
- }
-
- if(!cd_SentMsg(pTxMsg, 1)) return;
-
- /*发送第3包数据*/
- pTxMsg->dlc = 4;
- for(int i=0;i<4;i++)
- {
- pTxMsg->data[i] = bootInfoBuff[i+18];
- }
-
- if(!cd_SentMsg(pTxMsg, 2)) return;
-
- }
- void CAN_sendAck(void)
- {
- static CD_UINT8 ackBuff_2[16]={ FRAME_HEAD1,FRAME_HEAD2,0x07,0x15,0x0C,0x05,0xA9,0x03,'A','C','K',0x00,0x00,0x00,0x00,0xF0 };
- CD_UINT32 crc32Ret;
- //uint8_t i;
-
- crc32Ret = CRC32_Calculate( (CD_UINT8 *)ackBuff_2,11 );
-
- ackBuff_2[11] = (CD_UINT8)( crc32Ret >> 24 );
- ackBuff_2[12] = (CD_UINT8)( crc32Ret >> 16 );
- ackBuff_2[13] = (CD_UINT8)( crc32Ret >> 8 );
- ackBuff_2[14] = (CD_UINT8)( crc32Ret );
- //ready to send
- pTxMsg->rtr = 0;//0-data frame,1-remote frame
- pTxMsg->id = ((CAN_RxBuf_Struct1.ID & 0x7FF) << 18);//standard identifier
- //pTxMsg->id = ID & 0x1FFFFFFF;//extended identifier
- pTxMsg->xtd = 0;//0-11-bit standard identifier,1-29-bit extended identifier
- pTxMsg->esi = 0;
- pTxMsg->brs = 0;
- pTxMsg->fdf = 0;//0-Classic CAN format,1-CAN FD format
- pTxMsg->efc = 1;//1-Store Tx events
- //pTxMsg->mm = 0xAA;
-
- /*发送第1包数据*/
- pTxMsg->dlc = 8;
-
- pTxMsg->data[0] = ackBuff_2[0];
- pTxMsg->data[1] = ackBuff_2[1];
-
- for(int i=0;i<6;i++)
- {
- pTxMsg->data[i+2] = ackBuff_2[i+4];
- }
- if(!cd_SentMsg(pTxMsg, 0)) return;
- /*发送第2包数据*/
- pTxMsg->dlc = 6;
- for(int i=0;i<6;i++)
- {
- pTxMsg->data[i] = ackBuff_2[i+10];
- }
-
- if(!cd_SentMsg(pTxMsg, 1)) return;
- }
- void CAN_sendUpdateAck( CD_UINT16 cmd ,CD_UINT16 packageNum )
- {
- static CD_UINT8 ackBuff[15]={ FRAME_HEAD1,FRAME_HEAD2,0x07,0x15,0x0C,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0 };
- CD_UINT32 crc32Ret;
- //static CD_UINT8 ucInit_flag = 0;
- //uint8_t i;
-
- ackBuff[6] = (CD_UINT8)(cmd >>8);
- ackBuff[7] = (CD_UINT8)cmd;
- ackBuff[8] = (CD_UINT8)packageNum;
- ackBuff[9] = (CD_UINT8)(packageNum>>8);
-
- crc32Ret = CRC32_Calculate( (CD_UINT8 *)ackBuff, 10);
-
- ackBuff[10] = (CD_UINT8)( crc32Ret >> 24 );
- ackBuff[11] = (CD_UINT8)( crc32Ret >> 16 );
- ackBuff[12] = (CD_UINT8)( crc32Ret >> 8 );
- ackBuff[13] = (CD_UINT8)( crc32Ret );
- //ready to send
- pTxMsg->rtr = 0;//0-data frame,1-remote frame
- pTxMsg->id = ((CAN_RxBuf_Struct1.ID & 0x7FF) << 18);//standard identifier
- //pTxMsg->id = ID & 0x1FFFFFFF;//extended identifier
- pTxMsg->xtd = 0;//0-11-bit standard identifier,1-29-bit extended identifier
- pTxMsg->esi = 0;
- pTxMsg->brs = 0;
- pTxMsg->fdf = 0;//0-Classic CAN format,1-CAN FD format
- pTxMsg->efc = 1;//1-Store Tx events
- //pTxMsg->mm = 0xAA;
-
- /*发送第1包数据*/
- pTxMsg->dlc = 8;
-
- pTxMsg->data[0] = ackBuff[0];
- pTxMsg->data[1] = ackBuff[1];
-
- for(int i=0;i<6;i++)
- {
- pTxMsg->data[i+2] = ackBuff[i+4];
- }
- if(!cd_SentMsg(pTxMsg, 0))
- return;
-
- /*发送第2包数据*/
- pTxMsg->dlc = 5;
-
- for(int i=0;i<5;i++)
- {
- pTxMsg->data[i] = ackBuff[i+10];
- }
-
- if(!cd_SentMsg(pTxMsg, 1))
- return;
- }
- /************************ (C) END OF FILE *********************************/
|