/** ****************************************************************************** * @file uart_app.c * @author * @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 "uart_app.h" #include "uart_driver.h" #include "RefreshMenu.h" #include "protocol.h" #include "string.h" #include "flash_if.h" #include "bin_check.h" #include "can_driver.h" /* Private variables ---------------------------------------------------------*/ uint8_t receice_data[256]; /******************************************************************************* * Function: void UART_RxData_Process(USART_Buf_TypeDef*ptUartRx) * Discrible: * Input: * Output: * Return: * Others: * date version author record * ----------------------------------------------- * 20151107 V1.0 VINCENT Create *********************************************************************************/ uint8_t ERRdata=0; void UART_RxData_Process(USART_Buf_TypeDef* ptUartRx,USART_Buf_TypeDef* ptUartTx) { uint8_t ucHeadByte,ucFrameMode,ucAddr; //CD_UINT8 file_headTail[PACKET_128B_SIZE]={0},file_body[PACKET_1K_SIZE]={0}; uint16_t i ; uint8_t ucTrailHigh,ucTrailLow; uint8_t ucFrameLength; uint16_t ucKeyCmd,ucID; uint16_t ucAddSum=0,ucAddCal=0; uint32_t *ptUartRxData; if((*ptUartRx).ucBufCnt >= 8) { /*清除超时标志*/ // update.TimeCounter_Update = 0; ucHeadByte = UART_ReadChar(ptUartRx,0); //帧头 if(UARTFRAME_BEGIN == ucHeadByte) { ucAddr = UART_ReadChar(ptUartRx, 1);//地址 if(ucAddr != UARTADDR_CDL) { UART_DelChar(ptUartRx,1); return; } ucFrameMode = UART_ReadChar(ptUartRx,2); //模式 if(UARTMODE_WRITE != ucFrameMode) { UART_DelChar(ptUartRx,1); return; } ucFrameLength = UART_ReadChar(ptUartRx,3); //长度 if((*ptUartRx).ucBufCnt <(ucFrameLength + 8)) { return; } ucID = ((uint16_t)UART_ReadChar(ptUartRx,4) << 8 ) + ((uint16_t)UART_ReadChar(ptUartRx,5)); if(ucID != 0x0751) { UART_DelChar(ptUartRx,1); return; } ucKeyCmd = ((uint16_t)UART_ReadChar(ptUartRx,6) << 8 ) + ((uint16_t)UART_ReadChar(ptUartRx,7)); ucTrailLow = UART_ReadChar(ptUartRx,ucFrameLength + 6); ucTrailHigh = UART_ReadChar(ptUartRx,ucFrameLength + 7); if((ucTrailLow != UARTFRAME_END1) || (ucTrailHigh != UARTFRAME_END2)) { /*帧尾错误*/ UART_DelChar(ptUartRx, 1); update.AckEnable = ENABLE; downLoad.userCmd = CMD_ACK_ERR; downLoad.step = 0xf0; return ; } ucAddSum = (UART_ReadChar(ptUartRx,ucFrameLength + 4)) + \ (UART_ReadChar(ptUartRx,ucFrameLength + 5) << 8); for(i=8; i< (ucFrameLength + 4); i++) { receice_data[i-5] = UART_ReadChar(ptUartRx,i); } for(i=1; i< (ucFrameLength + 4); i++) { ucAddCal = UART_ReadChar(ptUartRx,i) + ucAddCal; } /*AddSum 校验*/ if( ucAddSum != ucAddCal) { update.AckEnable = ENABLE; downLoad.userCmd = CMD_ACK_ERR; downLoad.step = 0xf1; UART_DelChar(ptUartRx, (ucFrameLength + 8)); return ; } UART_DelChar(ptUartRx, (ucFrameLength + 8)); update.TimeCounter_Update = 0; ptUartRx->ucCOMEnable = 1; Data_Updata(ucKeyCmd, &receice_data[8], &receice_data[4]); } else { UART_DelChar(ptUartRx, 1); } } } void Data_Updata(uint16_t cmd, uint8_t* data, uint8_t* packagecount) { switch(cmd) { case CMD_UPDATA: //升级指令 //准备升级 if(downLoad.step != 1) { downLoad.step = 1; update.AckEnable = ENABLE; downLoad.userCmd = CMD_ACK_REQ; downLoad.packageSize=(uint32_t)(data[2] + (data[3]<<8)+ (data[4]<<16) +(data[5]<<24)); /*清除超时标志*/ update.TimeCounter_Startup = 0; } break; case CMD_PACKAGE_DATA: //数据包 if(CheckFlag == 0) { CheckFlag = 1; CheckInfo = (uint8_t *)data; //比较文件头 if(Check_CompareHead(CheckInfo, Check_UUID) == 0) { //解密获取bin文件crc32 Check_CalCrc32(CheckInfo, Crc32_Cal); //比较加密信息 if(Check_CompareEncryptInfo(CheckInfo) == 0) { //比较自定义信息 if(Check_OptionInfo(CheckInfo, OPTIONAL_ADDRESS) == 0) { //比较校验 if(Check_CompareXor(CheckInfo) == 0) { //文件头检验正确 CheckFlag = 2; /*擦除FLASH,剩余最后8k没有擦除,保留其他功能使用**/ //FLASH_If_Erase(APPLICATION_ADDRESS); //擦除时间约120ms ERRdata= CMD_USER_API_Flash_Range_Erase(APPLICATION_ADDRESS,USER_FLASH_BANK1_END_ADDRESS); /*清空flash写标志*/ for(int i=0;i= APPPACKGECOUNT_MAX_ADDRESS) { downLoad.packagecount = 0; } downLoad.userCmd = CMD_ACK_OK; downLoad.wrenable = ENABLE; } break; case CMD_UPDATA_END: //结束包 downLoad.step = 3; update.AckEnable = ENABLE; downLoad.userCmd = CMD_ACK_EOT; break; case 0x2505: downLoad.step = 5; update.AckEnable = ENABLE; break; default: if(CAN_RxBuf_Struct1.ucCANEnable == 1) { cd_DelChar(&CAN_RxBuf_Struct1, 1); } if(UART_RxBuff_Struct2.ucCOMEnable == 1) { UART_DelChar(&UART_RxBuff_Struct2, 1); } break; } } void uart_sendBootInfo(USART_Buf_TypeDef* ptUartTx) { static uint8_t bootInfoBuff[21]={ UARTFRAME_BEGIN,UARTADDR_CDL,0x0C,0x0D,0x07,0x15,0xC1,0x09,'M','C',' ','V','2','.','6','.','0',0x00,0x00,UARTFRAME_END1,UARTFRAME_END2 };//版本号: MC V2.0.3 uint16_t AddSum=0; //static CD_UINT8 ucInit_flag = 0; uint8_t i; for(i=1;i<17;i++) { AddSum = AddSum + bootInfoBuff[i]; } bootInfoBuff[17] = (uint8_t)( AddSum); bootInfoBuff[18] = (uint8_t)( AddSum >> 8); UART_SendData(ptUartTx,bootInfoBuff,21); } void uart_sendAck(USART_Buf_TypeDef* ptUartTx) { static uint8_t ackBuff_2[15]={ UARTFRAME_BEGIN,UARTADDR_CDL,0x0C,0x07,0x07,0x15,0xA9,0x03,'A','C','K',0x00,0x00,UARTFRAME_END1,UARTFRAME_END2 }; uint16_t AddSum=0; uint8_t i; for(i=1;i<11;i++) { AddSum = AddSum + ackBuff_2[i]; } ackBuff_2[11] = (uint8_t)( AddSum); ackBuff_2[12] = (uint8_t)( AddSum >> 8); UART_SendData(ptUartTx,ackBuff_2,15); } void uart_sendUpdateAck(USART_Buf_TypeDef* ptUartTx,uint16_t cmd, uint16_t packageNum) { static uint8_t ackBuff[14]={ UARTFRAME_BEGIN,UARTADDR_CDL,0x0C,0x06,0x07,0x15,0x00,0x00,0x00,0x00,0x00,0x00,UARTFRAME_END1,UARTFRAME_END2 }; uint16_t AddSum=0; uint8_t i; ackBuff[6] = (uint8_t)(cmd >>8); ackBuff[7] = (uint8_t)cmd; ackBuff[8] = (uint8_t)packageNum; ackBuff[9] = (uint8_t)(packageNum>>8); for(i=1;i<10;i++) { AddSum = AddSum + ackBuff[i]; } ackBuff[10] = (uint8_t)( AddSum); ackBuff[11] = (uint8_t)( AddSum >> 8); UART_SendData(ptUartTx,ackBuff,14); } /************************ (C) END OF FILE *********************************/