123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- /* Copyright Statement:
- *
- * This software/firmware and related documentation ("AutoChips Software") are
- * protected under relevant copyright laws. The information contained herein is
- * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
- * the prior written permission of AutoChips inc. and/or its licensors, any
- * reproduction, modification, use or disclosure of AutoChips Software, and
- * information contained herein, in whole or in part, shall be strictly
- * prohibited.
- *
- * AutoChips Inc. (C) 2016. All rights reserved.
- *
- * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
- * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
- * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
- * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
- * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
- * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
- * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
- * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
- * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
- * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
- * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
- * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
- * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
- * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
- * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
- * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
- * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
- * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
- */
- #include <string.h>
- #include "uart.h"
- #include "crc16.h"
- #include "cobs.h"
- #include "IAP.h"
- #include "Comm.h"
- //#include "can.h"
- ///<cmd
- #define ControlCmd (0x02)
- #define DataTransferCmd (0x40)
- #define DataTransferCmdHost (0x41)
- ///<smd
- ///<cmd=0x40
- #define DataTransferRequest (0x00)
- #define TransferData (0x01)
- #define TransferExit (0x02)
- ///<cmd=0x02
- #define RequestReset (0x00)
- /* cmd=0x40 smd=0x00 */
- /* Transfer type */
- #define IAP_BIN_FILE (1)
- #define MaxBlockSize (256 + 6)
- /* Result */
- #define ResultSuccess (0)
- #define ResultBusy (1)
- #define ResultExceedLimit (2)
- #define ResultTotalLengthErr (3)
- #define ResultCRCErr (4)
- #define ResultSequenceErr (5)
- #define ResultParamErr (6)
- #define ResultExecErr (7)
- #define SendBufferLength (280)
- #define ReceiveBufferLength (280)
- static int SendFrame(uint8_t *data, uint16_t length);
- static int GetFrame(uint8_t *data, uint16_t *length);
- fifo_TypeDef g_commRxFifo;
- static uint8_t s_commRxDataBuff[RxBufferLength];
- ///<flash variable
- uint8_t s_flashType;
- uint32_t s_flashMemSize;
- uint32_t s_recvByte;
- uint8_t s_sequence;
- static uint8_t s_sendBuffer[SendBufferLength]; ///<发送buff
- static uint8_t s_receiveBuffer[ReceiveBufferLength];
- uint32_t s_writeBytes;
- uint8_t *s_startAddress;
- uint8_t s_writeSequence;
- uint32_t s_writeIndex;
- uint8_t s_writeData[SendBufferLength];
- void Start_IAPTest(void)
- {
- uint16_t crc;
- uint16_t index=0;
- s_writeBytes = 0x800 * 12; ///<写24K数据
- s_writeSequence = 0;
- s_writeIndex = 0;
- s_startAddress = (uint8_t *)APP_ADDRESS; ///<将自己的app程序发送给目标板
-
- printf("start IAP writebyte=%08x\r\n", s_writeBytes);
- s_writeData[index++] = DataTransferCmd;
- s_writeData[index++] = DataTransferRequest;
- s_writeData[index++] = IAP_BIN_FILE;
-
- s_writeData[index++] = (uint8_t)((s_writeBytes >> 24) & 0xff);
- s_writeData[index++] = (uint8_t)((s_writeBytes >> 16) & 0xff);
- s_writeData[index++] = (uint8_t)((s_writeBytes >> 8) & 0xff);
- s_writeData[index++] = (uint8_t)(s_writeBytes & 0xff);
-
-
- crc = crc16(s_writeData, index);
- s_writeData[index++] = (uint8_t)((crc>>8) & 0x00ff);
- s_writeData[index++] = (uint8_t)(crc & 0x00ff);
-
- SendFrame(s_writeData, index);
- }
- /**
- * DataTransferRequestHostHandle
- *
- * @param[in] pdata:data point
- * @param[in] length:data length
- *
- * @return none
- *
- * @brief 上位机处理 数据传输请求反馈
- */
- static void DataTransferRequestHostHandle(uint8_t *pdata, uint16_t length)
- {
- uint8_t result;
- uint16_t crc;
- uint16_t index=0;
- result = pdata[2];
- if (result == ResultSuccess)
- {
- printf("data transfer request success\r\n");
- s_writeData[index++] = DataTransferCmd;
- s_writeData[index++] = TransferData;
-
- s_writeData[index++] = s_writeSequence;
-
- memcpy(&s_writeData[index], &s_startAddress[s_writeIndex], 256);
- index += 256;
- s_writeIndex += 256;
- s_writeSequence++;
-
- crc = crc16(s_writeData, index);
- s_writeData[index++] = (uint8_t)((crc>>8) & 0x00ff);
- s_writeData[index++] = (uint8_t)(crc & 0x00ff);
-
- SendFrame(s_writeData, index);
- }
- else
- {
- printf("TransferRequestFail result=%02x\r\n", result);
- }
- }
- /**
- * TransferDataHostHandle
- *
- * @param[in] pdata:data point
- * @param[in] length:data length
- *
- * @return none
- *
- * @brief 上位机处理 传输数据处理反馈
- */
- static void TransferDataHostHandle(uint8_t *pdata, uint16_t length)
- {
- uint8_t result;
- uint16_t crc;
- uint16_t index=0;
- result = pdata[2];
- if (result == ResultSuccess)
- {
- printf("transfer data success\r\n");
- if (s_writeIndex < s_writeBytes)
- {
- s_writeData[index++] = DataTransferCmd;
- s_writeData[index++] = TransferData;
-
- s_writeData[index++] = s_writeSequence;
-
- memcpy(&s_writeData[index], &s_startAddress[s_writeIndex], 256);
- index += 256;
- s_writeIndex += 256;
- s_writeSequence++;
- }
- else
- {
- s_writeData[index++] = DataTransferCmd;
- s_writeData[index++] = TransferExit;
-
- crc = crc16(s_startAddress, s_writeBytes);
- s_writeData[index++] = (uint8_t)((crc>>8) & 0x00ff);
- s_writeData[index++] = (uint8_t)(crc & 0x00ff);
- }
- crc = crc16(s_writeData, index);
- s_writeData[index++] = (uint8_t)((crc>>8) & 0x00ff);
- s_writeData[index++] = (uint8_t)(crc & 0x00ff);
-
- SendFrame(s_writeData, index);
- }
- else
- {
- printf("TransferDataFail result=%02x\r\n", result);
- }
- }
- /**
- * TransferExitHostHandle
- *
- * @param[in] pdata:data point
- * @param[in] length:data length
- *
- * @return none
- *
- * @brief 上位机处理 传输数据结束反馈
- */
- static void TransferExitHostHandle(uint8_t *pdata, uint16_t length)
- {
- uint8_t result;
- uint16_t crc;
- uint16_t index=0;
- result = pdata[2];
- if (result == ResultSuccess)
- {
- printf("transfer exit success\r\n");
- s_writeData[index++] = ControlCmd;
- s_writeData[index++] = RequestReset;
-
- crc = crc16(s_writeData, index);
- s_writeData[index++] = (uint8_t)((crc>>8) & 0x00ff);
- s_writeData[index++] = (uint8_t)(crc & 0x00ff);
-
- SendFrame(s_writeData, index);
- }
- else
- {
- printf("TransferExitFail result=%02x\r\n", result);
- }
- }
- /**
- * DataTransferCmdHostHandle
- *
- * @param[in] pdata:data point
- * @param[in] length:data length
- *
- * @return none
- *
- * @brief 上位机处理 数据传输命令反馈处理
- */
- static void DataTransferCmdHostHandle(uint8_t *pdata, uint16_t length)
- {
- uint8_t smd = pdata[1];
- switch (smd)
- {
- case DataTransferRequest:
- DataTransferRequestHostHandle(pdata, length);
- break;
- case TransferData:
- TransferDataHostHandle(pdata, length);
- break;
- case TransferExit:
- TransferExitHostHandle(pdata, length);
- break;
- default :break;
- }
- }
- /**
- * DataTransferRequestHandle
- *
- * @param[in] pdata:data point
- * @param[in] length:data length
- *
- * @return none
- *
- * @brief 上位机通信 数据传输请求
- */
- static void DataTransferRequestHandle(uint8_t *pdata, uint16_t length)
- {
- uint8_t data[15];
- uint8_t index=0;
- uint16_t crc;
- s_flashType = pdata[2];
- s_flashMemSize = pdata[3];
- s_flashMemSize <<= 8;
- s_flashMemSize += pdata[4];
- s_flashMemSize <<= 8;
- s_flashMemSize += pdata[5];
- s_flashMemSize <<= 8;
- s_flashMemSize += pdata[6];
- s_sequence = 0;
- s_recvByte = 0;
-
- data[index++] = pdata[0] + 1;
- data[index++] = pdata[1];
- if (s_flashType != IAP_BIN_FILE) ///<目前只支持传输IAP bin 文件
- {
- data[index] = ResultParamErr;
- }
- else
- {
- if (s_flashMemSize > APP_SIZE)
- {
- data[index] = ResultExceedLimit; ///<exceed limit
- }
- else
- {
- IAP_Start();
- data[index] = ResultSuccess;
- }
- }
-
- index++;
- data[index++] = (uint8_t)((MaxBlockSize >> 24) & 0xff);
- data[index++] = (uint8_t)((MaxBlockSize >> 16) & 0xff);
- data[index++] = (uint8_t)((MaxBlockSize >> 8) & 0xff);
- data[index++] = (uint8_t)(MaxBlockSize & 0xff);
-
- crc = crc16(data, index);
- data[index++] = (uint8_t)((crc>>8) & 0x00ff);
- data[index++] = (uint8_t)(crc & 0x00ff);
-
- SendFrame(data, index);
- }
- /**
- * TransferDataHandle
- *
- * @param[in] pdata:data point
- * @param[in] length:data length
- *
- * @return none
- *
- * @brief 上位机通信 传输数据处理
- */
- static void TransferDataHandle(uint8_t *pdata, uint16_t length)
- {
- uint8_t data[10];
- uint8_t index=0;
- uint16_t crc;
-
- data[index++] = pdata[0] + 1;
- data[index++] = pdata[1];
- if (length > MaxBlockSize)
- {
- data[index] = ResultExceedLimit; ///<长度超出限制
- IAP_Stop();
- }
- else if (pdata[2] != s_sequence)
- {
- data[index] = ResultSequenceErr; ///<序列号不正确
- IAP_Stop();
- }
- else
- {
- data[index] = ResultSuccess; ///<succeed
- if (0 != IAP_Write(&pdata[3], length - 5))
- {
- data[index] = ResultExecErr; ///<执行错误
- IAP_Stop();
- }
- s_recvByte += length - 5;
- s_sequence++;
- }
- index++;
-
- crc = crc16(data, index);
- data[index++] = (uint8_t)((crc>>8) & 0x00ff);
- data[index++] = (uint8_t)(crc & 0x00ff);
-
- SendFrame(data, index);
- }
- /**
- * TransferExitHandle
- *
- * @param[in] pdata:data point
- * @param[in] length:data length
- *
- * @return none
- *
- * @brief 上位机通信 传输数据结束
- */
- static void TransferExitHandle(uint8_t *pdata, uint16_t length)
- {
- uint8_t data[10];
- uint8_t index=0;
- uint16_t crc;
- uint16_t crcCheck;
-
- data[index++] = pdata[0] + 1;
- data[index++] = pdata[1];
- crc = ((uint16_t)pdata[2] << 8) | pdata[3];
-
- if (0 != IAP_Stop())
- {
- data[index] = ResultExecErr; ///<执行错误
- }
- else
- {
- if (s_recvByte != s_flashMemSize)
- {
- data[index] = ResultTotalLengthErr; ///<接收数据长度不正确
- }
- else
- {
- data[index] = ResultSuccess;
- if (s_flashType == IAP_BIN_FILE)
- {
- crcCheck = crc16((const uint8_t *)APP_ADDRESS, s_recvByte);
- if (crcCheck == crc)
- {
- config->AppCrc = crc;
- config->AppSize = s_flashMemSize;
- //if (0 != RefreshConfig())
- //{
- // data[index] = ResultExecErr;
- //}
- }
- else
- {
- data[index] = ResultCRCErr; ///crc校验失败
- }
- }
- }
- }
-
- index++;
-
- crc = crc16(data, index);
- data[index++] = (uint8_t)((crc>>8) & 0x00ff);
- data[index++] = (uint8_t)(crc & 0x00ff);
-
- SendFrame(data, index);
- }
- /**
- * RequestResetHandle
- *
- * @param[in] pdata:data point
- * @param[in] length:data length
- *
- * @return none
- *
- * @brief 上位机通信 复位请求处理
- */
- static void RequestResetHandle(uint8_t *pdata, uint16_t length)
- {
- NVIC_SystemReset();
- }
- /**
- * ControlCmdHandle
- *
- * @param[in] pdata:data point
- * @param[in] length:data length
- *
- * @return none
- *
- * @brief 上位机通信 控制命令处理
- */
- static void ControlCmdHandle(uint8_t *pdata, uint16_t length)
- {
- uint8_t smd = pdata[1];
- switch (smd)
- {
- case RequestReset: //0x00
- printf("Receive Reset Request\r\n");
- RequestResetHandle(pdata, length);
- break;
- default :break;
- }
- }
- /**
- * DataTransferCmdHandle
- *
- * @param[in] pdata:data point
- * @param[in] length:data length
- *
- * @return none
- *
- * @brief 上位机通信 数据传输命令处理
- */
- static void DataTransferCmdHandle(uint8_t *pdata, uint16_t length)
- {
- uint8_t smd = pdata[1];
- switch (smd)
- {
- case DataTransferRequest: //0x00
- printf("Receive Data Transfer Request\r\n");
- DataTransferRequestHandle(pdata, length);
- break;
- case TransferData: //0x01
- printf("Receive Transfer Data\r\n");
- TransferDataHandle(pdata, length);
- break;
- case TransferExit: //0x02
- printf("Receive Transfer Exit\r\n");
- TransferExitHandle(pdata, length);
- break;
- default :break;
- }
- }
-
- /**
- * ComRxDataHandle
- *
- * @param[in] pdata:data point
- * @param[in] length:data length
- *
- * @return none
- *
- * @brief 上位机通信 数据接收处理函数
- */
- void ComRxDataHandle(void)
- {
- uint16_t length = ReceiveBufferLength;
- uint8_t cmd;
- uint16_t crc;
- if (0 == GetFrame(s_receiveBuffer, &length))
- {
- crc = ((uint16_t)s_receiveBuffer[length-2]<<8) | s_receiveBuffer[length-1];
- if (crc != crc16(s_receiveBuffer, length-2))
- {
- ///<crc error
- return;
- }
- cmd = s_receiveBuffer[0];
- switch (cmd)
- {
- case ControlCmd: //0x02
- ControlCmdHandle(s_receiveBuffer, length);
- break;
- case DataTransferCmd: //0x40
- DataTransferCmdHandle(s_receiveBuffer, length);
- break;
- case DataTransferCmdHost: //0x41
- DataTransferCmdHostHandle(s_receiveBuffer, length);
- break;
-
- default :break;
- }
- }
-
- }
- void Comm_Init(void)
- {
-
- fifo_Init(&g_commRxFifo, s_commRxDataBuff, RxBufferLength);
- uart_Initialize();
- //CAN_Initialize();
- }
- static int SendFrame(uint8_t *data, uint16_t length)
- {
- cobs_encode_result result;
- if (length != 0)
- {
- result = cobs_encode(s_sendBuffer, SendBufferLength, data, length);
- if (result.status == COBS_ENCODE_OK)
- {
- //Add delimitor
- *(s_sendBuffer+result.out_len) = 0;
- result.out_len++;
- uart0_TransmitData(s_sendBuffer, result.out_len);
- //CAN_Transmit(s_sendBuffer, result.out_len);
- }
- }
- return 0;
- }
- static int GetFrame(uint8_t *data, uint16_t *length)
- {
- uint16_t frameLength;
- cobs_decode_result result;
- frameLength = Fifo_strchr(&g_commRxFifo, 0);
-
- if (frameLength != 0)
- {
- if (*length < frameLength)
- { ///<帧长超过了buff的长度,复位fifo
- fifo_Reset(&g_commRxFifo);
- return -1;
- }
- fifo_retrieve(&g_commRxFifo, data, frameLength);
- result = cobs_decode(data, *length, data, frameLength-1);
- if (result.status == COBS_DECODE_OK)
- {
- *length = result.out_len;
- return 0;
- }
- }
- return -1;
- }
|