ymodem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /**
  2. ******************************************************************************
  3. * @file IAP_Main/Src/ymodem.c
  4. * @author MCD Application Team
  5. * @version 1.0.0
  6. * @date 8-April-2015
  7. * @brief This file provides all the software functions related to the ymodem
  8. * protocol.
  9. ******************************************************************************
  10. *
  11. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  12. * You may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at:
  14. *
  15. * http://www.st.com/software_license_agreement_liberty_v2
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ******************************************************************************
  24. */
  25. /** @addtogroup STM32F1xx_IAP
  26. * @{
  27. */
  28. /* Includes ------------------------------------------------------------------*/
  29. #include "boot/boot.h"
  30. #include "ymodem.h"
  31. #include "string.h"
  32. #include "usart/usart.h"
  33. char boot[8]={"bootflag"}; //远程升级下发字符串
  34. /* Private typedef -----------------------------------------------------------*/
  35. /* Private define ------------------------------------------------------------*/
  36. #define CRC16_F /* activate the CRC16 integrity */
  37. /* Private macro -------------------------------------------------------------*/
  38. uint8_t aFileName[FILE_NAME_LENGTH];
  39. /* Private variables ---------------------------------------------------------*/
  40. /* @note ATTENTION - please keep this variable 32bit alligned */
  41. uint8_t aPacketData[PACKET_1K_SIZE + PACKET_DATA_INDEX + PACKET_TRAILER_SIZE];
  42. uint16_t package_size = 0;
  43. uint16_t leng = 0;
  44. /* Private function prototypes -----------------------------------------------*/
  45. //static void PrepareIntialPacket(uint8_t *p_data, const uint8_t *p_file_name, uint32_t length);
  46. //static void PreparePacket(uint8_t *p_source, uint8_t *p_packet, uint8_t pkt_nr, uint32_t size_blk);
  47. static HAL_StatusTypeDef ReceivePacket(uint8_t *p_data, uint32_t *p_length, uint32_t timeout);
  48. uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte);
  49. uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size);
  50. uint8_t CalcChecksum(const uint8_t *p_data, uint32_t size);
  51. /**
  52. * @brief Convert an Integer to a string
  53. * @param p_str: The string output pointer
  54. * @param intnum: The integer to be converted
  55. * @retval None
  56. */
  57. void Int2Str(uint8_t *p_str, uint32_t intnum)
  58. {
  59. uint32_t i, divider = 1000000000, pos = 0, status = 0;
  60. for (i = 0; i < 10; i++)
  61. {
  62. p_str[pos++] = (intnum / divider) + 48;
  63. intnum = intnum % divider;
  64. divider /= 10;
  65. if ((p_str[pos-1] == '0') & (status == 0))
  66. {
  67. pos = 0;
  68. }
  69. else
  70. {
  71. status++;
  72. }
  73. }
  74. }
  75. /**
  76. * @brief Convert a string to an integer
  77. * @param p_inputstr: The string to be converted
  78. * @param p_intnum: The integer value
  79. * @retval 1: Correct
  80. * 0: Error
  81. */
  82. uint32_t Str2Int(uint8_t *p_inputstr, uint32_t *p_intnum)
  83. {
  84. uint32_t i = 0, res = 0;
  85. uint32_t val = 0;
  86. if ((p_inputstr[0] == '0') && ((p_inputstr[1] == 'x') || (p_inputstr[1] == 'X')))
  87. {
  88. i = 2;
  89. while ( ( i < 11 ) && ( p_inputstr[i] != '\0' ) )
  90. {
  91. if (ISVALIDHEX(p_inputstr[i]))
  92. {
  93. val = (val << 4) + CONVERTHEX(p_inputstr[i]);
  94. }
  95. else
  96. {
  97. /* Return 0, Invalid input */
  98. res = 0;
  99. break;
  100. }
  101. i++;
  102. }
  103. /* valid result */
  104. if (p_inputstr[i] == '\0')
  105. {
  106. *p_intnum = val;
  107. res = 1;
  108. }
  109. }
  110. else /* max 10-digit decimal input */
  111. {
  112. while ( ( i < 11 ) && ( res != 1 ) )
  113. {
  114. if (p_inputstr[i] == '\0')
  115. {
  116. *p_intnum = val;
  117. /* return 1 */
  118. res = 1;
  119. }
  120. else if (((p_inputstr[i] == 'k') || (p_inputstr[i] == 'K')) && (i > 0))
  121. {
  122. val = val << 10;
  123. *p_intnum = val;
  124. res = 1;
  125. }
  126. else if (((p_inputstr[i] == 'm') || (p_inputstr[i] == 'M')) && (i > 0))
  127. {
  128. val = val << 20;
  129. *p_intnum = val;
  130. res = 1;
  131. }
  132. else if (ISVALIDDEC(p_inputstr[i]))
  133. {
  134. val = val * 10 + CONVERTDEC(p_inputstr[i]);
  135. }
  136. else
  137. {
  138. /* return 0, Invalid input */
  139. res = 0;
  140. break;
  141. }
  142. i++;
  143. }
  144. }
  145. return res;
  146. }
  147. /* Private functions ---------------------------------------------------------*/
  148. /**
  149. * @brief Receive a packet from sender
  150. * @param data
  151. * @param length
  152. * 0: end of transmission
  153. * 2: abort by sender
  154. * >0: packet length
  155. * @param timeout
  156. * @retval HAL_OK: normally return
  157. * HAL_BUSY: abort by user
  158. */
  159. static HAL_StatusTypeDef ReceivePacket(uint8_t *p_data, uint32_t *p_length, uint32_t timeout)
  160. {
  161. uint32_t crc;
  162. uint32_t packet_size = 0;
  163. HAL_StatusTypeDef status;
  164. uint8_t char1;
  165. uint8_t update_again = 0; //升级次数累加标志位
  166. *p_length = 0;
  167. status = HAL_UART_Receive(&UartHandle, &char1, 1, timeout);
  168. rt_kprintf("recv:%x-%d\n",char1,status);
  169. // if (status == 3) //判断是否通讯接收超时
  170. // {
  171. // update_again++;
  172. // rt_kprintf("Upgrade transfer timeout1!!!:\n");//程序下载超时退出本次下载
  173. // HAL_UART_Transmit(&UartHandle,(unsigned char*)boot,8,1000);//发送重新发送升级命令进行二次升级
  174. // rt_kprintf("Upgrade transfer timeout2!!!:\n");//程序下载超时退出本次下载
  175. // rt_thread_mdelay(500);
  176. // HAL_NVIC_SystemReset();//单片机重启
  177. // if (update_again == 5) //如果连续升级5次不成功退出当前升级
  178. // {
  179. // update_again = 0; //升级次数标志位清零
  180. // Start_app(); //从APP启动
  181. // }
  182. // }
  183. if (status == HAL_OK)
  184. {
  185. switch (char1)
  186. {
  187. case SOH:
  188. packet_size = PACKET_SIZE;
  189. package_size =PACKET_SIZE;
  190. leng = 133;
  191. rt_kprintf("SOH:data len is 128.\n");
  192. break;
  193. case STX:
  194. packet_size = PACKET_1K_SIZE;
  195. package_size = PACKET_1K_SIZE;
  196. leng = 1029;
  197. rt_kprintf("STX:data len is 1024.\n");
  198. break;
  199. case EOT: //数据传输完毕,发送方发送EOT
  200. rt_kprintf("EOT\n");
  201. break;
  202. case CA:
  203. rt_kprintf("CA\n");
  204. if ((HAL_UART_Receive(&UartHandle, &char1, 1, timeout) == HAL_OK) && (char1 == CA))
  205. {
  206. packet_size = 2;
  207. }
  208. else
  209. {
  210. status = HAL_ERROR;
  211. rt_kprintf("HAL_ERROR:%d\n",status);
  212. }
  213. break;
  214. case ABORT1:
  215. case ABORT2:
  216. status = HAL_BUSY;
  217. rt_kprintf("HAL_BUSY:%d\n",status);
  218. break;
  219. default:
  220. status = HAL_ERROR;
  221. rt_kprintf("default:HAL_ERROR:%d\n",status);
  222. break;
  223. }
  224. *p_data = char1;
  225. /*01 00 FF 72 74 74 68 72 65 61 64 2E 62 69 6E 00 32 34 34 34 38 20 31 34 31 36 35 35 35 36 33 34 33 20 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 34 B0*/
  226. if (packet_size >= package_size )
  227. {
  228. //接收第一包数据,长度是133
  229. status = HAL_UART_Receive(&UartHandle, &p_data[PACKET_NUMBER_INDEX], packet_size + PACKET_OVERHEAD_SIZE, timeout);
  230. rt_kprintf("ymoedm recv packet:%d\n");
  231. for(uint8_t ss = 0;ss < leng;ss++)
  232. {
  233. rt_kprintf("%x ",p_data[ss]);
  234. }
  235. /* Simple packet sanity check */
  236. if (status == HAL_OK )
  237. {
  238. if (p_data[PACKET_NUMBER_INDEX] != ((p_data[PACKET_CNUMBER_INDEX]) ^ NEGATIVE_BYTE))
  239. {
  240. packet_size = 0;
  241. status = HAL_ERROR;
  242. rt_kprintf("HAL_ERROR!!!:\n");
  243. }
  244. else
  245. {
  246. /* Check packet CRC */
  247. crc = p_data[ packet_size + PACKET_DATA_INDEX ] << 8;
  248. crc += p_data[ packet_size + PACKET_DATA_INDEX + 1 ];
  249. if (Cal_CRC16(&p_data[PACKET_DATA_INDEX], packet_size) != crc )
  250. {
  251. packet_size = 0;
  252. status = HAL_ERROR;
  253. rt_kprintf("CRC ERROR!!!:\n");
  254. }
  255. }
  256. }
  257. else
  258. {
  259. packet_size = 0;
  260. }
  261. }
  262. }
  263. *p_length = packet_size;
  264. return status;
  265. }
  266. /**
  267. * @brief 数据包发送初始化
  268. * @param p_data: 输出缓冲区
  269. * @param p_file_name: 要发送的文件的名称
  270. * @param length: 要发送的文件的长度(以字节为单位)
  271. * @retval None
  272. */
  273. //static void PrepareIntialPacket(uint8_t *p_data, const uint8_t *p_file_name, uint32_t length)
  274. //{
  275. // uint32_t i, j = 0;
  276. // uint8_t astring[10];
  277. //
  278. // /* first 3 bytes are constant */
  279. // p_data[PACKET_START_INDEX] = SOH;
  280. // p_data[PACKET_NUMBER_INDEX] = 0x00;
  281. // p_data[PACKET_CNUMBER_INDEX] = 0xff;
  282. //
  283. // /* Filename written */
  284. // for (i = 0; (p_file_name[i] != '\0') && (i < FILE_NAME_LENGTH); i++)
  285. // {
  286. // p_data[i + PACKET_DATA_INDEX] = p_file_name[i];
  287. // }
  288. //
  289. // p_data[i + PACKET_DATA_INDEX] = 0x00;
  290. //
  291. // /* file size written */
  292. // Int2Str (astring, length);
  293. // i = i + PACKET_DATA_INDEX + 1;
  294. // while (astring[j] != '\0')
  295. // {
  296. // p_data[i++] = astring[j++];
  297. // }
  298. //
  299. // /* padding with zeros */
  300. // for (j = i; j < PACKET_SIZE + PACKET_DATA_INDEX; j++)
  301. // {
  302. // p_data[j] = 0;
  303. // }
  304. //}
  305. /**
  306. * @brief 数据包初始化
  307. * @param p_source:指向要发送的数据的指针
  308. * @param p_packet:指向输出缓冲区的指针
  309. * @param pkt_nr:数据包编号
  310. * @param size_blk:要发送的块的长度(以字节为单位)
  311. * @retval None
  312. */
  313. //static void PreparePacket(uint8_t *p_source, uint8_t *p_packet, uint8_t pkt_nr, uint32_t size_blk)
  314. //{
  315. // uint8_t *p_record;
  316. // uint32_t i, size, packet_size;
  317. //
  318. // /* Make first three packet */
  319. // packet_size = size_blk >= PACKET_1K_SIZE ? PACKET_1K_SIZE : PACKET_SIZE;
  320. // size = size_blk < packet_size ? size_blk : packet_size;
  321. // if (packet_size == PACKET_1K_SIZE)
  322. // {
  323. // p_packet[PACKET_START_INDEX] = STX;
  324. // }
  325. // else
  326. // {
  327. // p_packet[PACKET_START_INDEX] = SOH;
  328. // }
  329. // p_packet[PACKET_NUMBER_INDEX] = pkt_nr;
  330. // p_packet[PACKET_CNUMBER_INDEX] = (~pkt_nr);
  331. // p_record = p_source;
  332. //
  333. // /* Filename packet has valid data */
  334. // for (i = PACKET_DATA_INDEX; i < size + PACKET_DATA_INDEX;i++)
  335. // {
  336. // p_packet[i] = *p_record++;
  337. // }
  338. // if ( size <= packet_size)
  339. // {
  340. // for (i = size + PACKET_DATA_INDEX; i < packet_size + PACKET_DATA_INDEX; i++)
  341. // {
  342. // p_packet[i] = 0x1A; /* EOF (0x1A) or 0x00 */
  343. // }
  344. // }
  345. //}
  346. /**
  347. * @brief 更新输入字节的 CRC16
  348. * @param crc_in输入值
  349. * @param 输入字节
  350. * @retval None
  351. */
  352. uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte)
  353. {
  354. uint32_t crc = crc_in;
  355. uint32_t in = byte | 0x100;
  356. do
  357. {
  358. crc <<= 1;
  359. in <<= 1;
  360. if(in & 0x100)
  361. ++crc;
  362. if(crc & 0x10000)
  363. crc ^= 0x1021;
  364. }
  365. while(!(in & 0x10000));
  366. return crc & 0xffffu;
  367. }
  368. /**
  369. * @brief 用于 ymodem 数据包的校准 CRC16
  370. * @param 数据
  371. * @param 长度
  372. * @retval None
  373. */
  374. uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size)
  375. {
  376. uint32_t crc = 0;
  377. const uint8_t* dataEnd = p_data+size;
  378. while(p_data < dataEnd)
  379. crc = UpdateCRC16(crc, *p_data++);
  380. crc = UpdateCRC16(crc, 0);
  381. crc = UpdateCRC16(crc, 0);
  382. return crc&0xffffu;
  383. }
  384. /**
  385. * @brief 计算 YModem 数据包的校验和
  386. * @param p_data 指向输入数据的指针
  387. * @param 输入数据的大小长度
  388. * @retval uint8_t校验和值
  389. */
  390. uint8_t CalcChecksum(const uint8_t *p_data, uint32_t size)
  391. {
  392. uint32_t sum = 0;
  393. const uint8_t *p_data_end = p_data + size;
  394. while (p_data < p_data_end )
  395. {
  396. sum += *p_data++;
  397. }
  398. return (sum & 0xffu);
  399. }
  400. /* Public functions ---------------------------------------------------------*/
  401. /**
  402. * @brief 使用 ymodem 协议和 CRC16 接收文件。
  403. * @param p_size 文件的大小..
  404. * @retval COM_StatusTypeDef接收/编程的结果
  405. */
  406. COM_StatusTypeDef Ymodem_Receive ( uint32_t *p_size )
  407. {
  408. uint32_t i, packet_length, session_done = 0, file_done, errors = 0, session_begin = 0,packets_received;
  409. uint32_t flashdestination, ramsource, filesize;
  410. uint8_t *file_ptr;
  411. uint8_t file_size[FILE_SIZE_LENGTH], tmp;
  412. COM_StatusTypeDef result = COM_OK;
  413. /* Initialize flashdestination variable */
  414. flashdestination = N_APP_ADDR_BASE;
  415. while ((session_done == 0) && (result == COM_OK))
  416. {
  417. packets_received = 0;
  418. file_done = 0;
  419. while ((file_done == 0) && (result == COM_OK))
  420. {
  421. switch (ReceivePacket(aPacketData, &packet_length, DOWNLOAD_TIMEOUT))
  422. {
  423. case HAL_OK:
  424. errors = 0;
  425. switch (packet_length)
  426. {
  427. case 2:
  428. /* Abort by sender */
  429. Serial_PutByte(ACK);
  430. result = COM_ABORT;
  431. break;
  432. case 0:
  433. /* End of transmission */
  434. Serial_PutByte(ACK);
  435. file_done = 1;
  436. break;
  437. default:
  438. /* Normal packet */
  439. if (aPacketData[PACKET_NUMBER_INDEX] != (packets_received & 0xff))
  440. {
  441. Serial_PutByte(NAK);
  442. }
  443. else
  444. {
  445. if (packets_received == 0)
  446. {
  447. /* File name packet */
  448. if (aPacketData[PACKET_DATA_INDEX] != 0)
  449. {
  450. /* File name extraction */
  451. i = 0;
  452. file_ptr = aPacketData + PACKET_DATA_INDEX;
  453. while ( (*file_ptr != 0) && (i < FILE_NAME_LENGTH))
  454. {
  455. aFileName[i++] = *file_ptr++;
  456. }
  457. /* File size extraction */
  458. aFileName[i++] = '\0';
  459. i = 0;
  460. file_ptr ++;
  461. while ( (*file_ptr != ' ') && (i < FILE_SIZE_LENGTH))
  462. {
  463. file_size[i++] = *file_ptr++;
  464. }
  465. file_size[i++] = '\0';
  466. Str2Int(file_size, &filesize);
  467. /* Test the size of the image to be sent */
  468. /* Image size is greater than Flash size */
  469. if (*p_size > (USER_FLASH_SIZE + 1))
  470. {
  471. /* End session */
  472. tmp = CA;
  473. HAL_UART_Transmit(&UartHandle, &tmp, 1, NAK_TIMEOUT);
  474. HAL_UART_Transmit(&UartHandle, &tmp, 1, NAK_TIMEOUT);
  475. result = COM_LIMIT;
  476. }
  477. /* erase user application area */
  478. //FLASH_If_Erase(APPLICATION_ADDRESS);
  479. rt_kprintf("erase user application area:%x\n",N_APP_ADDR_BASE);
  480. //MyErase_page(0x8005800,42);
  481. MyErase_page(gt_boot_var.app_addr_base,21);
  482. *p_size = filesize;
  483. Serial_PutByte(ACK);
  484. Serial_PutByte(CRC16);
  485. }
  486. /* File header packet is empty, end session */
  487. else
  488. {
  489. Serial_PutByte(ACK);
  490. file_done = 1;
  491. session_done = 1;
  492. break;
  493. }
  494. }
  495. else /* Data packet */
  496. {
  497. ramsource = (uint32_t) & aPacketData[PACKET_DATA_INDEX];
  498. /* Write received data in Flash */
  499. if (FLASH_If_Write(flashdestination, (uint32_t*) ramsource, packet_length/4) == 0)
  500. {
  501. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET);
  502. flashdestination += packet_length;
  503. Serial_PutByte(ACK);
  504. rt_kprintf("Write received data in Flash:%d\n",flashdestination);
  505. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
  506. }
  507. else /* An error occurred while writing to Flash memory */
  508. {
  509. /* End session */
  510. Serial_PutByte(CA);
  511. Serial_PutByte(CA);
  512. result = COM_DATA;
  513. rt_kprintf("Write received data in Flash ERROR!!!\n");
  514. }
  515. }
  516. packets_received ++;
  517. session_begin = 1;
  518. }
  519. break;
  520. }
  521. break;
  522. case HAL_BUSY: /* Abort actually */
  523. Serial_PutByte(CA);
  524. Serial_PutByte(CA);
  525. result = COM_ABORT;
  526. break;
  527. default:
  528. if (session_begin > 0)
  529. {
  530. errors ++;
  531. }
  532. if (errors > MAX_ERRORS)
  533. {
  534. /* Abort communication */
  535. Serial_PutByte(CA);
  536. Serial_PutByte(CA);
  537. }
  538. else
  539. {
  540. Serial_PutByte(CRC16); /* Ask for a packet */
  541. }
  542. break;
  543. }
  544. rt_thread_delay(1);
  545. }
  546. rt_thread_delay(1);
  547. }
  548. return result;
  549. }
  550. /**
  551. * @brief 使用 ymodem 协议传输文件
  552. * @param p_buf:第一个字节的地址
  553. * @param p_file_name:发送的文件的名称
  554. * @param file_size:传输的大小
  555. * @retval COM_tatusypeef通信的结果
  556. */
  557. COM_StatusTypeDef Ymodem_Transmit (uint8_t *p_buf, const uint8_t *p_file_name, uint32_t file_size)
  558. {
  559. uint32_t errors = 0, ack_recpt = 0, size = 0, pkt_size;
  560. uint8_t *p_buf_int;
  561. COM_StatusTypeDef result = COM_OK;
  562. uint32_t blk_number = 1;
  563. uint8_t a_rx_ctrl[2];
  564. uint8_t i;
  565. #ifdef CRC16_F
  566. uint32_t temp_crc;
  567. #else /* CRC16_F */
  568. uint8_t temp_chksum;
  569. #endif /* CRC16_F */
  570. /* Prepare first block - header */
  571. PrepareIntialPacket(aPacketData, p_file_name, file_size);
  572. while (( !ack_recpt ) && ( result == COM_OK ))
  573. {
  574. /* Send Packet */
  575. HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT);
  576. /* Send CRC or Check Sum based on CRC16_F */
  577. #ifdef CRC16_F
  578. temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
  579. Serial_PutByte(temp_crc >> 8);
  580. Serial_PutByte(temp_crc & 0xFF);
  581. #else /* CRC16_F */
  582. temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
  583. Serial_PutByte(temp_chksum);
  584. #endif /* CRC16_F */
  585. /* Wait for Ack and 'C' */
  586. if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
  587. {
  588. if (a_rx_ctrl[0] == ACK)
  589. {
  590. ack_recpt = 1;
  591. }
  592. else if (a_rx_ctrl[0] == CA)
  593. {
  594. if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA))
  595. {
  596. HAL_Delay( 2 );
  597. __HAL_UART_FLUSH_DRREGISTER(&UartHandle);
  598. result = COM_ABORT;
  599. }
  600. }
  601. }
  602. else
  603. {
  604. errors++;
  605. }
  606. if (errors >= MAX_ERRORS)
  607. {
  608. result = COM_ERROR;
  609. }
  610. }
  611. p_buf_int = p_buf;
  612. size = file_size;
  613. /* Here 1024 bytes length is used to send the packets */
  614. while ((size) && (result == COM_OK ))
  615. {
  616. /* Prepare next packet */
  617. PreparePacket(p_buf_int, aPacketData, blk_number, size);
  618. ack_recpt = 0;
  619. a_rx_ctrl[0] = 0;
  620. errors = 0;
  621. /* Resend packet if NAK for few times else end of communication */
  622. while (( !ack_recpt ) && ( result == COM_OK ))
  623. {
  624. /* Send next packet */
  625. if (size >= PACKET_1K_SIZE)
  626. {
  627. pkt_size = PACKET_1K_SIZE;
  628. }
  629. else
  630. {
  631. pkt_size = PACKET_SIZE;
  632. }
  633. HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], pkt_size + PACKET_HEADER_SIZE, NAK_TIMEOUT);
  634. /* Send CRC or Check Sum based on CRC16_F */
  635. #ifdef CRC16_F
  636. temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], pkt_size);
  637. Serial_PutByte(temp_crc >> 8);
  638. Serial_PutByte(temp_crc & 0xFF);
  639. #else /* CRC16_F */
  640. temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], pkt_size);
  641. Serial_PutByte(temp_chksum);
  642. #endif /* CRC16_F */
  643. /* Wait for Ack */
  644. if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == ACK))
  645. {
  646. ack_recpt = 1;
  647. if (size > pkt_size)
  648. {
  649. p_buf_int += pkt_size;
  650. size -= pkt_size;
  651. if (blk_number == (USER_FLASH_SIZE / PACKET_1K_SIZE))
  652. {
  653. result = COM_LIMIT; /* boundary error */
  654. }
  655. else
  656. {
  657. blk_number++;
  658. }
  659. }
  660. else
  661. {
  662. p_buf_int += pkt_size;
  663. size = 0;
  664. }
  665. }
  666. else
  667. {
  668. errors++;
  669. }
  670. /* Resend packet if NAK for a count of 10 else end of communication */
  671. if (errors >= MAX_ERRORS)
  672. {
  673. result = COM_ERROR;
  674. }
  675. }
  676. }
  677. /* Sending End Of Transmission char */
  678. ack_recpt = 0;
  679. a_rx_ctrl[0] = 0x00;
  680. errors = 0;
  681. while (( !ack_recpt ) && ( result == COM_OK ))
  682. {
  683. Serial_PutByte(EOT);
  684. /* Wait for Ack */
  685. if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
  686. {
  687. if (a_rx_ctrl[0] == ACK)
  688. {
  689. ack_recpt = 1;
  690. }
  691. else if (a_rx_ctrl[0] == CA)
  692. {
  693. if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA))
  694. {
  695. HAL_Delay( 2 );
  696. __HAL_UART_FLUSH_DRREGISTER(&UartHandle);
  697. result = COM_ABORT;
  698. }
  699. }
  700. }
  701. else
  702. {
  703. errors++;
  704. }
  705. if (errors >= MAX_ERRORS)
  706. {
  707. result = COM_ERROR;
  708. }
  709. }
  710. /* Empty packet sent - some terminal emulators need this to close session */
  711. if ( result == COM_OK )
  712. {
  713. /* Preparing an empty packet */
  714. aPacketData[PACKET_START_INDEX] = SOH;
  715. aPacketData[PACKET_NUMBER_INDEX] = 0;
  716. aPacketData[PACKET_CNUMBER_INDEX] = 0xFF;
  717. for (i = PACKET_DATA_INDEX; i < (PACKET_SIZE + PACKET_DATA_INDEX); i++)
  718. {
  719. aPacketData [i] = 0x00;
  720. }
  721. /* Send Packet */
  722. HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT);
  723. /* Send CRC or Check Sum based on CRC16_F */
  724. #ifdef CRC16_F
  725. temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
  726. Serial_PutByte(temp_crc >> 8);
  727. Serial_PutByte(temp_crc & 0xFF);
  728. #else /* CRC16_F */
  729. temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
  730. Serial_PutByte(temp_chksum);
  731. #endif /* CRC16_F */
  732. /* Wait for Ack and 'C' */
  733. if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
  734. {
  735. if (a_rx_ctrl[0] == CA)
  736. {
  737. HAL_Delay( 2 );
  738. __HAL_UART_FLUSH_DRREGISTER(&UartHandle);
  739. result = COM_ABORT;
  740. }
  741. }
  742. }
  743. return result; /* file transmitted successfully */
  744. }
  745. /**
  746. * @}
  747. */
  748. /*******************(C)COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/