Comm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("AutoChips Software") are
  4. * protected under relevant copyright laws. The information contained herein is
  5. * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
  6. * the prior written permission of AutoChips inc. and/or its licensors, any
  7. * reproduction, modification, use or disclosure of AutoChips Software, and
  8. * information contained herein, in whole or in part, shall be strictly
  9. * prohibited.
  10. *
  11. * AutoChips Inc. (C) 2016. All rights reserved.
  12. *
  13. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  14. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
  15. * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
  16. * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
  17. * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  19. * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
  20. * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
  21. * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
  22. * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
  23. * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
  24. * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
  25. * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
  26. * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  27. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
  28. * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
  29. * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
  30. * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
  31. * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
  32. */
  33. #include <string.h>
  34. #include "uart.h"
  35. #include "crc16.h"
  36. #include "cobs.h"
  37. #include "IAP.h"
  38. #include "Comm.h"
  39. //#include "can.h"
  40. ///<cmd
  41. #define ControlCmd (0x02)
  42. #define DataTransferCmd (0x40)
  43. #define DataTransferCmdHost (0x41)
  44. ///<smd
  45. ///<cmd=0x40
  46. #define DataTransferRequest (0x00)
  47. #define TransferData (0x01)
  48. #define TransferExit (0x02)
  49. ///<cmd=0x02
  50. #define RequestReset (0x00)
  51. /* cmd=0x40 smd=0x00 */
  52. /* Transfer type */
  53. #define IAP_BIN_FILE (1)
  54. #define MaxBlockSize (256 + 6)
  55. /* Result */
  56. #define ResultSuccess (0)
  57. #define ResultBusy (1)
  58. #define ResultExceedLimit (2)
  59. #define ResultTotalLengthErr (3)
  60. #define ResultCRCErr (4)
  61. #define ResultSequenceErr (5)
  62. #define ResultParamErr (6)
  63. #define ResultExecErr (7)
  64. #define SendBufferLength (280)
  65. #define ReceiveBufferLength (280)
  66. static int SendFrame(uint8_t *data, uint16_t length);
  67. static int GetFrame(uint8_t *data, uint16_t *length);
  68. fifo_TypeDef g_commRxFifo;
  69. static uint8_t s_commRxDataBuff[RxBufferLength];
  70. ///<flash variable
  71. uint8_t s_flashType;
  72. uint32_t s_flashMemSize;
  73. uint32_t s_recvByte;
  74. uint8_t s_sequence;
  75. static uint8_t s_sendBuffer[SendBufferLength]; ///<发送buff
  76. static uint8_t s_receiveBuffer[ReceiveBufferLength];
  77. uint32_t s_writeBytes;
  78. uint8_t *s_startAddress;
  79. uint8_t s_writeSequence;
  80. uint32_t s_writeIndex;
  81. uint8_t s_writeData[SendBufferLength];
  82. void Start_IAPTest(void)
  83. {
  84. uint16_t crc;
  85. uint16_t index=0;
  86. s_writeBytes = 0x800 * 12; ///<写24K数据
  87. s_writeSequence = 0;
  88. s_writeIndex = 0;
  89. s_startAddress = (uint8_t *)APP_ADDRESS; ///<将自己的app程序发送给目标板
  90. printf("start IAP writebyte=%08x\r\n", s_writeBytes);
  91. s_writeData[index++] = DataTransferCmd;
  92. s_writeData[index++] = DataTransferRequest;
  93. s_writeData[index++] = IAP_BIN_FILE;
  94. s_writeData[index++] = (uint8_t)((s_writeBytes >> 24) & 0xff);
  95. s_writeData[index++] = (uint8_t)((s_writeBytes >> 16) & 0xff);
  96. s_writeData[index++] = (uint8_t)((s_writeBytes >> 8) & 0xff);
  97. s_writeData[index++] = (uint8_t)(s_writeBytes & 0xff);
  98. crc = crc16(s_writeData, index);
  99. s_writeData[index++] = (uint8_t)((crc>>8) & 0x00ff);
  100. s_writeData[index++] = (uint8_t)(crc & 0x00ff);
  101. SendFrame(s_writeData, index);
  102. }
  103. /**
  104. * DataTransferRequestHostHandle
  105. *
  106. * @param[in] pdata:data point
  107. * @param[in] length:data length
  108. *
  109. * @return none
  110. *
  111. * @brief 上位机处理 数据传输请求反馈
  112. */
  113. static void DataTransferRequestHostHandle(uint8_t *pdata, uint16_t length)
  114. {
  115. uint8_t result;
  116. uint16_t crc;
  117. uint16_t index=0;
  118. result = pdata[2];
  119. if (result == ResultSuccess)
  120. {
  121. printf("data transfer request success\r\n");
  122. s_writeData[index++] = DataTransferCmd;
  123. s_writeData[index++] = TransferData;
  124. s_writeData[index++] = s_writeSequence;
  125. memcpy(&s_writeData[index], &s_startAddress[s_writeIndex], 256);
  126. index += 256;
  127. s_writeIndex += 256;
  128. s_writeSequence++;
  129. crc = crc16(s_writeData, index);
  130. s_writeData[index++] = (uint8_t)((crc>>8) & 0x00ff);
  131. s_writeData[index++] = (uint8_t)(crc & 0x00ff);
  132. SendFrame(s_writeData, index);
  133. }
  134. else
  135. {
  136. printf("TransferRequestFail result=%02x\r\n", result);
  137. }
  138. }
  139. /**
  140. * TransferDataHostHandle
  141. *
  142. * @param[in] pdata:data point
  143. * @param[in] length:data length
  144. *
  145. * @return none
  146. *
  147. * @brief 上位机处理 传输数据处理反馈
  148. */
  149. static void TransferDataHostHandle(uint8_t *pdata, uint16_t length)
  150. {
  151. uint8_t result;
  152. uint16_t crc;
  153. uint16_t index=0;
  154. result = pdata[2];
  155. if (result == ResultSuccess)
  156. {
  157. printf("transfer data success\r\n");
  158. if (s_writeIndex < s_writeBytes)
  159. {
  160. s_writeData[index++] = DataTransferCmd;
  161. s_writeData[index++] = TransferData;
  162. s_writeData[index++] = s_writeSequence;
  163. memcpy(&s_writeData[index], &s_startAddress[s_writeIndex], 256);
  164. index += 256;
  165. s_writeIndex += 256;
  166. s_writeSequence++;
  167. }
  168. else
  169. {
  170. s_writeData[index++] = DataTransferCmd;
  171. s_writeData[index++] = TransferExit;
  172. crc = crc16(s_startAddress, s_writeBytes);
  173. s_writeData[index++] = (uint8_t)((crc>>8) & 0x00ff);
  174. s_writeData[index++] = (uint8_t)(crc & 0x00ff);
  175. }
  176. crc = crc16(s_writeData, index);
  177. s_writeData[index++] = (uint8_t)((crc>>8) & 0x00ff);
  178. s_writeData[index++] = (uint8_t)(crc & 0x00ff);
  179. SendFrame(s_writeData, index);
  180. }
  181. else
  182. {
  183. printf("TransferDataFail result=%02x\r\n", result);
  184. }
  185. }
  186. /**
  187. * TransferExitHostHandle
  188. *
  189. * @param[in] pdata:data point
  190. * @param[in] length:data length
  191. *
  192. * @return none
  193. *
  194. * @brief 上位机处理 传输数据结束反馈
  195. */
  196. static void TransferExitHostHandle(uint8_t *pdata, uint16_t length)
  197. {
  198. uint8_t result;
  199. uint16_t crc;
  200. uint16_t index=0;
  201. result = pdata[2];
  202. if (result == ResultSuccess)
  203. {
  204. printf("transfer exit success\r\n");
  205. s_writeData[index++] = ControlCmd;
  206. s_writeData[index++] = RequestReset;
  207. crc = crc16(s_writeData, index);
  208. s_writeData[index++] = (uint8_t)((crc>>8) & 0x00ff);
  209. s_writeData[index++] = (uint8_t)(crc & 0x00ff);
  210. SendFrame(s_writeData, index);
  211. }
  212. else
  213. {
  214. printf("TransferExitFail result=%02x\r\n", result);
  215. }
  216. }
  217. /**
  218. * DataTransferCmdHostHandle
  219. *
  220. * @param[in] pdata:data point
  221. * @param[in] length:data length
  222. *
  223. * @return none
  224. *
  225. * @brief 上位机处理 数据传输命令反馈处理
  226. */
  227. static void DataTransferCmdHostHandle(uint8_t *pdata, uint16_t length)
  228. {
  229. uint8_t smd = pdata[1];
  230. switch (smd)
  231. {
  232. case DataTransferRequest:
  233. DataTransferRequestHostHandle(pdata, length);
  234. break;
  235. case TransferData:
  236. TransferDataHostHandle(pdata, length);
  237. break;
  238. case TransferExit:
  239. TransferExitHostHandle(pdata, length);
  240. break;
  241. default :break;
  242. }
  243. }
  244. /**
  245. * DataTransferRequestHandle
  246. *
  247. * @param[in] pdata:data point
  248. * @param[in] length:data length
  249. *
  250. * @return none
  251. *
  252. * @brief 上位机通信 数据传输请求
  253. */
  254. static void DataTransferRequestHandle(uint8_t *pdata, uint16_t length)
  255. {
  256. uint8_t data[15];
  257. uint8_t index=0;
  258. uint16_t crc;
  259. s_flashType = pdata[2];
  260. s_flashMemSize = pdata[3];
  261. s_flashMemSize <<= 8;
  262. s_flashMemSize += pdata[4];
  263. s_flashMemSize <<= 8;
  264. s_flashMemSize += pdata[5];
  265. s_flashMemSize <<= 8;
  266. s_flashMemSize += pdata[6];
  267. s_sequence = 0;
  268. s_recvByte = 0;
  269. data[index++] = pdata[0] + 1;
  270. data[index++] = pdata[1];
  271. if (s_flashType != IAP_BIN_FILE) ///<目前只支持传输IAP bin 文件
  272. {
  273. data[index] = ResultParamErr;
  274. }
  275. else
  276. {
  277. if (s_flashMemSize > APP_SIZE)
  278. {
  279. data[index] = ResultExceedLimit; ///<exceed limit
  280. }
  281. else
  282. {
  283. IAP_Start();
  284. data[index] = ResultSuccess;
  285. }
  286. }
  287. index++;
  288. data[index++] = (uint8_t)((MaxBlockSize >> 24) & 0xff);
  289. data[index++] = (uint8_t)((MaxBlockSize >> 16) & 0xff);
  290. data[index++] = (uint8_t)((MaxBlockSize >> 8) & 0xff);
  291. data[index++] = (uint8_t)(MaxBlockSize & 0xff);
  292. crc = crc16(data, index);
  293. data[index++] = (uint8_t)((crc>>8) & 0x00ff);
  294. data[index++] = (uint8_t)(crc & 0x00ff);
  295. SendFrame(data, index);
  296. }
  297. /**
  298. * TransferDataHandle
  299. *
  300. * @param[in] pdata:data point
  301. * @param[in] length:data length
  302. *
  303. * @return none
  304. *
  305. * @brief 上位机通信 传输数据处理
  306. */
  307. static void TransferDataHandle(uint8_t *pdata, uint16_t length)
  308. {
  309. uint8_t data[10];
  310. uint8_t index=0;
  311. uint16_t crc;
  312. data[index++] = pdata[0] + 1;
  313. data[index++] = pdata[1];
  314. if (length > MaxBlockSize)
  315. {
  316. data[index] = ResultExceedLimit; ///<长度超出限制
  317. IAP_Stop();
  318. }
  319. else if (pdata[2] != s_sequence)
  320. {
  321. data[index] = ResultSequenceErr; ///<序列号不正确
  322. IAP_Stop();
  323. }
  324. else
  325. {
  326. data[index] = ResultSuccess; ///<succeed
  327. if (0 != IAP_Write(&pdata[3], length - 5))
  328. {
  329. data[index] = ResultExecErr; ///<执行错误
  330. IAP_Stop();
  331. }
  332. s_recvByte += length - 5;
  333. s_sequence++;
  334. }
  335. index++;
  336. crc = crc16(data, index);
  337. data[index++] = (uint8_t)((crc>>8) & 0x00ff);
  338. data[index++] = (uint8_t)(crc & 0x00ff);
  339. SendFrame(data, index);
  340. }
  341. /**
  342. * TransferExitHandle
  343. *
  344. * @param[in] pdata:data point
  345. * @param[in] length:data length
  346. *
  347. * @return none
  348. *
  349. * @brief 上位机通信 传输数据结束
  350. */
  351. static void TransferExitHandle(uint8_t *pdata, uint16_t length)
  352. {
  353. uint8_t data[10];
  354. uint8_t index=0;
  355. uint16_t crc;
  356. uint16_t crcCheck;
  357. data[index++] = pdata[0] + 1;
  358. data[index++] = pdata[1];
  359. crc = ((uint16_t)pdata[2] << 8) | pdata[3];
  360. if (0 != IAP_Stop())
  361. {
  362. data[index] = ResultExecErr; ///<执行错误
  363. }
  364. else
  365. {
  366. if (s_recvByte != s_flashMemSize)
  367. {
  368. data[index] = ResultTotalLengthErr; ///<接收数据长度不正确
  369. }
  370. else
  371. {
  372. data[index] = ResultSuccess;
  373. if (s_flashType == IAP_BIN_FILE)
  374. {
  375. crcCheck = crc16((const uint8_t *)APP_ADDRESS, s_recvByte);
  376. if (crcCheck == crc)
  377. {
  378. config->AppCrc = crc;
  379. config->AppSize = s_flashMemSize;
  380. //if (0 != RefreshConfig())
  381. //{
  382. // data[index] = ResultExecErr;
  383. //}
  384. }
  385. else
  386. {
  387. data[index] = ResultCRCErr; ///crc校验失败
  388. }
  389. }
  390. }
  391. }
  392. index++;
  393. crc = crc16(data, index);
  394. data[index++] = (uint8_t)((crc>>8) & 0x00ff);
  395. data[index++] = (uint8_t)(crc & 0x00ff);
  396. SendFrame(data, index);
  397. }
  398. /**
  399. * RequestResetHandle
  400. *
  401. * @param[in] pdata:data point
  402. * @param[in] length:data length
  403. *
  404. * @return none
  405. *
  406. * @brief 上位机通信 复位请求处理
  407. */
  408. static void RequestResetHandle(uint8_t *pdata, uint16_t length)
  409. {
  410. NVIC_SystemReset();
  411. }
  412. /**
  413. * ControlCmdHandle
  414. *
  415. * @param[in] pdata:data point
  416. * @param[in] length:data length
  417. *
  418. * @return none
  419. *
  420. * @brief 上位机通信 控制命令处理
  421. */
  422. static void ControlCmdHandle(uint8_t *pdata, uint16_t length)
  423. {
  424. uint8_t smd = pdata[1];
  425. switch (smd)
  426. {
  427. case RequestReset: //0x00
  428. printf("Receive Reset Request\r\n");
  429. RequestResetHandle(pdata, length);
  430. break;
  431. default :break;
  432. }
  433. }
  434. /**
  435. * DataTransferCmdHandle
  436. *
  437. * @param[in] pdata:data point
  438. * @param[in] length:data length
  439. *
  440. * @return none
  441. *
  442. * @brief 上位机通信 数据传输命令处理
  443. */
  444. static void DataTransferCmdHandle(uint8_t *pdata, uint16_t length)
  445. {
  446. uint8_t smd = pdata[1];
  447. switch (smd)
  448. {
  449. case DataTransferRequest: //0x00
  450. printf("Receive Data Transfer Request\r\n");
  451. DataTransferRequestHandle(pdata, length);
  452. break;
  453. case TransferData: //0x01
  454. printf("Receive Transfer Data\r\n");
  455. TransferDataHandle(pdata, length);
  456. break;
  457. case TransferExit: //0x02
  458. printf("Receive Transfer Exit\r\n");
  459. TransferExitHandle(pdata, length);
  460. break;
  461. default :break;
  462. }
  463. }
  464. /**
  465. * ComRxDataHandle
  466. *
  467. * @param[in] pdata:data point
  468. * @param[in] length:data length
  469. *
  470. * @return none
  471. *
  472. * @brief 上位机通信 数据接收处理函数
  473. */
  474. void ComRxDataHandle(void)
  475. {
  476. uint16_t length = ReceiveBufferLength;
  477. uint8_t cmd;
  478. uint16_t crc;
  479. if (0 == GetFrame(s_receiveBuffer, &length))
  480. {
  481. crc = ((uint16_t)s_receiveBuffer[length-2]<<8) | s_receiveBuffer[length-1];
  482. if (crc != crc16(s_receiveBuffer, length-2))
  483. {
  484. ///<crc error
  485. return;
  486. }
  487. cmd = s_receiveBuffer[0];
  488. switch (cmd)
  489. {
  490. case ControlCmd: //0x02
  491. ControlCmdHandle(s_receiveBuffer, length);
  492. break;
  493. case DataTransferCmd: //0x40
  494. DataTransferCmdHandle(s_receiveBuffer, length);
  495. break;
  496. case DataTransferCmdHost: //0x41
  497. DataTransferCmdHostHandle(s_receiveBuffer, length);
  498. break;
  499. default :break;
  500. }
  501. }
  502. }
  503. void Comm_Init(void)
  504. {
  505. fifo_Init(&g_commRxFifo, s_commRxDataBuff, RxBufferLength);
  506. uart_Initialize();
  507. //CAN_Initialize();
  508. }
  509. static int SendFrame(uint8_t *data, uint16_t length)
  510. {
  511. cobs_encode_result result;
  512. if (length != 0)
  513. {
  514. result = cobs_encode(s_sendBuffer, SendBufferLength, data, length);
  515. if (result.status == COBS_ENCODE_OK)
  516. {
  517. //Add delimitor
  518. *(s_sendBuffer+result.out_len) = 0;
  519. result.out_len++;
  520. uart0_TransmitData(s_sendBuffer, result.out_len);
  521. //CAN_Transmit(s_sendBuffer, result.out_len);
  522. }
  523. }
  524. return 0;
  525. }
  526. static int GetFrame(uint8_t *data, uint16_t *length)
  527. {
  528. uint16_t frameLength;
  529. cobs_decode_result result;
  530. frameLength = Fifo_strchr(&g_commRxFifo, 0);
  531. if (frameLength != 0)
  532. {
  533. if (*length < frameLength)
  534. { ///<帧长超过了buff的长度,复位fifo
  535. fifo_Reset(&g_commRxFifo);
  536. return -1;
  537. }
  538. fifo_retrieve(&g_commRxFifo, data, frameLength);
  539. result = cobs_decode(data, *length, data, frameLength-1);
  540. if (result.status == COBS_DECODE_OK)
  541. {
  542. *length = result.out_len;
  543. return 0;
  544. }
  545. }
  546. return -1;
  547. }