IAP_YMODEM_ATY.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /**
  2. * @file IAP_YMODEM_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of IAP with YMODEM for all embedded device
  20. *
  21. * @version
  22. * - 1_01_240911 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_01_250701 > ATY
  25. * -# real fix to lib type
  26. * - 1_01_250704 > ATY
  27. * -# finish uart and ucdc IAP whole test
  28. ********************************************************************************
  29. */
  30. #ifndef __IAP_YMODEM_ATY_C
  31. #define __IAP_YMODEM_ATY_C
  32. #include "IAP_YMODEM_ATY.h"
  33. /******************************* For user *************************************/
  34. uint8_t EmptyBuf[1] = {0};
  35. #ifdef IAP_YMODEM_ATY_USB
  36. USBD_CDC_HandleTypeDef* hcdc;
  37. uint32_t ucdcRcvLength = 0;
  38. uint8_t ucdcRcvOverFlag = 1;
  39. uint32_t ucdcRcvLastTime = 0;
  40. uint8_t ucdcRcvBuffer[PACKET_1K_SIZE + PACKET_DATA_INDEX + PACKET_TRAILER_SIZE] = {0};
  41. #endif
  42. void All_Reset(void)
  43. {
  44. // Disable global interrupts
  45. __disable_irq();
  46. // Deinnit peripherals
  47. HAL_UART_DeInit(&Interface_UART);
  48. __HAL_RCC_USB_CLK_DISABLE();
  49. HAL_RCC_DeInit();
  50. HAL_DeInit();
  51. // Disable and reset SysTick timer
  52. SysTick->CTRL = 0;
  53. SysTick->LOAD = 0;
  54. SysTick->VAL = 0;
  55. // Optional: Disable and clear all NVIC interrupts
  56. for(int i = 0; i < 8; i++) {
  57. NVIC->ICER[i] = 0xFFFFFFFF; // Disable all interrupts
  58. NVIC->ICPR[i] = 0xFFFFFFFF; // Clear pending status
  59. }
  60. // Optional: Reset interrupt priority registers
  61. // for(int i = 0; i < 48; i++) {
  62. // NVIC->IP[i] = 0; // Set all priorities to default
  63. // }
  64. // Optional: Set vector table offset to App's address
  65. SCB->VTOR = APPLICATION_ADDRESS & 0x1FFFFF80;
  66. }
  67. void JumpToAppWithReset(void)
  68. {
  69. All_Reset();
  70. JumpToApp(APPLICATION_ADDRESS);
  71. }
  72. void Interface_Clean(void)
  73. {
  74. #ifndef IAP_YMODEM_ATY_USB
  75. #warning "NOT IAP_YMODEM_ATY_USB"
  76. __HAL_UART_FLUSH_DRREGISTER(&Interface_UART);
  77. #else
  78. #warning "IAP_YMODEM_ATY_USB"
  79. ucdcRcvLength = 0;
  80. ucdcRcvOverFlag = 1;
  81. #endif
  82. }
  83. #ifdef IAP_YMODEM_ATY_USB
  84. void PUT_IN_CDC_Receive_FS(uint8_t* Buf, uint32_t* Len)
  85. {
  86. if(ucdcRcvOverFlag == 1){ // new start
  87. ucdcRcvOverFlag = 0;
  88. ucdcRcvLength = *Len;
  89. memcpy(ucdcRcvBuffer, Buf, *Len);
  90. if(*Len < 64){
  91. ucdcRcvOverFlag = 1;
  92. }
  93. else{
  94. ucdcRcvLastTime = HAL_GetTick();
  95. }
  96. }
  97. else{ // pending to before
  98. memcpy(ucdcRcvBuffer + ucdcRcvLength, Buf, *Len);
  99. ucdcRcvLength += *Len;
  100. if(*Len < 64){
  101. ucdcRcvOverFlag = 1;
  102. }
  103. else{
  104. ucdcRcvLastTime = HAL_GetTick();
  105. }
  106. }
  107. // USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  108. // USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  109. // return (USBD_OK);
  110. }
  111. #endif
  112. uint32_t timeoutCount = 0;
  113. uint32_t timeoutFinal = 100;
  114. HAL_StatusTypeDef Interface_Receive(uint8_t* bytes, uint16_t size, uint32_t timeout)
  115. {
  116. #ifndef IAP_YMODEM_ATY_USB
  117. HAL_StatusTypeDef state = HAL_UART_Receive(&Interface_UART, bytes, size, timeout);
  118. return state;
  119. // return HAL_UART_Receive(&Interface_UART, bytes, size, timeout);
  120. #else
  121. hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
  122. if(hcdc->RxLength > APP_RX_DATA_SIZE)
  123. return HAL_ERROR;
  124. timeoutCount = 0;
  125. timeoutFinal = timeout;
  126. while(ucdcRcvLength == 0){
  127. timeoutCount++;
  128. if(timeoutCount > timeoutFinal){
  129. timeoutCount = 0;
  130. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  131. bytes[0] = 0;
  132. return HAL_TIMEOUT;
  133. }
  134. HAL_Delay(1);
  135. }
  136. if(ucdcRcvOverFlag == 0)
  137. {
  138. while(HAL_GetTick() - ucdcRcvLastTime < 200)
  139. {
  140. }
  141. ucdcRcvOverFlag = 1;
  142. }
  143. if(ucdcRcvLength > 0 && ucdcRcvLength <= PACKET_1K_SIZE + PACKET_DATA_INDEX + PACKET_TRAILER_SIZE){
  144. uint16_t copySize = (ucdcRcvLength > size) ? size : ucdcRcvLength;
  145. memcpy(bytes, ucdcRcvBuffer, copySize);
  146. ucdcRcvLength -= copySize;
  147. memcpy(ucdcRcvBuffer, ucdcRcvBuffer + copySize, ucdcRcvLength);
  148. }
  149. return HAL_OK;
  150. #endif
  151. }
  152. HAL_StatusTypeDef Interface_Transmit(uint8_t* bytes, uint16_t size, uint32_t timeout)
  153. {
  154. #ifndef IAP_YMODEM_ATY_USB
  155. /* May be timeouted... */
  156. if(Interface_UART.gState == HAL_UART_STATE_TIMEOUT)
  157. {
  158. Interface_UART.gState = HAL_UART_STATE_READY;
  159. }
  160. return HAL_UART_Transmit(&Interface_UART, bytes, size, timeout);
  161. #else
  162. Interface_Clean();
  163. ucdcRcvOverFlag = 1;
  164. timeoutCount = 0;
  165. timeoutFinal = timeout;
  166. while(CDC_Transmit_FS(bytes, size) != HAL_OK){
  167. timeoutCount++;
  168. if(timeoutCount > timeoutFinal)
  169. return HAL_TIMEOUT;
  170. HAL_Delay(1);
  171. }
  172. return HAL_OK;
  173. #endif
  174. }
  175. /**
  176. * @brief Display the Main Menu on HyperTerminal
  177. */
  178. void Main_Menu(void)
  179. {
  180. uint8_t key = 0;
  181. uint16_t errTimes = 0;
  182. uint32_t FlashProtection = 0;
  183. /* Initialise Flash */
  184. FLASH_If_Init();
  185. /* Test if any sector of Flash memory where user application will be loaded is write protected */
  186. FlashProtection = FLASH_If_GetWriteProtectionStatus();
  187. Interface_PutString("\r\n\r\n=================== Main Menu ============================\r\n\r\n");
  188. Interface_PutString(" Download image to the internal Flash ----------------- 1\r\n\r\n");
  189. Interface_PutString(" Upload image from the internal Flash ----------------- 2\r\n\r\n");
  190. Interface_PutString(" Execute the loaded application ----------------------- 3\r\n\r\n");
  191. if(FlashProtection != FLASHIF_PROTECTION_NONE)
  192. {
  193. Interface_PutString(" Disable the write protection ------------------------- 4\r\n\r\n");
  194. }
  195. else
  196. {
  197. Interface_PutString(" Enable the write protection -------------------------- 4\r\n\r\n");
  198. }
  199. Interface_PutString("==========================================================\r\n\r\n");
  200. while(1)
  201. {
  202. /* Clean the input path */
  203. Interface_Clean();
  204. /* Receive key */
  205. Interface_Receive(&key, 1, IAP_3_TIME);
  206. /* Auto jump to app */
  207. if(key == 0)
  208. key = 51;
  209. switch(key)
  210. {
  211. case '*':
  212. /* Upload user application from the Flash */
  213. Interface_Upload();
  214. break;
  215. case '2':
  216. /* Upload user application from the Flash */
  217. Interface_PutString("\r\nAuthorize wrong!\r\n\r\n");
  218. break;
  219. case '1':
  220. /* Download user application in the Flash */
  221. if(Interface_Download() != COM_OK)
  222. break;
  223. case '3':
  224. Interface_PutString("\r\nStart program execution...\r\n");
  225. JumpToAppWithReset();
  226. break;
  227. case '4':
  228. if(FlashProtection != FLASHIF_PROTECTION_NONE)
  229. {
  230. /* Disable the write protection */
  231. if(FLASH_If_WriteProtectionConfig(FLASHIF_WRP_DISABLE) == FLASHIF_OK)
  232. {
  233. Interface_PutString("Write protection disabled...\r\n");
  234. Interface_PutString("System will now restart...\r\n");
  235. /* Launch the option byte loading */
  236. HAL_FLASH_OB_Launch();
  237. }
  238. else
  239. {
  240. Interface_PutString("Error: Flash write un-protection failed...\r\n");
  241. }
  242. }
  243. else
  244. {
  245. if(FLASH_If_WriteProtectionConfig(FLASHIF_WRP_ENABLE) == FLASHIF_OK)
  246. {
  247. Interface_PutString("Write protection enabled...\r\n");
  248. Interface_PutString("System will now restart...\r\n");
  249. /* Launch the option byte loading */
  250. HAL_FLASH_OB_Launch();
  251. }
  252. else
  253. {
  254. Interface_PutString("Error: Flash write protection failed...\r\n");
  255. }
  256. }
  257. break;
  258. default:
  259. errTimes++;
  260. if(errTimes > 500){
  261. Interface_PutString("Start program execution for max err...\r\n\r\n");
  262. JumpToAppWithReset();
  263. }
  264. // Interface_PutString("Invalid Number ! ==> The number should be either 1, 2, 3 or 4\r\n");
  265. break;
  266. }
  267. key = 0;
  268. }
  269. }
  270. #include "rtc.h"
  271. void Main_Cycle(void)
  272. {
  273. uint8_t sKey[2] = {0};
  274. HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 101);
  275. HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, 250);
  276. HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR3, 705);
  277. Interface_PutString("\r\n\r\nS to start\r\n\r\n");
  278. while(1)
  279. {
  280. #ifdef IAP_YMODEM_ATY_USB
  281. if(hcdc->RxLength > 1024){
  282. uint8_t t[1] = {0};
  283. Interface_Receive(t, 1, 10);
  284. continue;
  285. }
  286. #endif
  287. if(1)
  288. {
  289. if(Interface_Receive(sKey, 2, IAP_S_TIME) == HAL_OK)
  290. {
  291. if(sKey[0] == 'S')
  292. {
  293. if(sKey[1] == '3'){
  294. Interface_Clean();
  295. Interface_PutString("\r\nStart program execution...\r\n");
  296. JumpToAppWithReset();
  297. }
  298. else if(sKey[1] == '1'){
  299. Interface_Clean();
  300. if(Interface_Download() != COM_OK){
  301. Interface_PutString("\r\nStart program execution...\r\n");
  302. JumpToAppWithReset();
  303. }
  304. }
  305. else{
  306. Interface_Clean();
  307. Main_Menu();
  308. }
  309. }
  310. else{
  311. Interface_Clean();
  312. }
  313. }
  314. else{
  315. Interface_Clean();
  316. Main_Menu();
  317. }
  318. }
  319. /* Keep the user application running */
  320. else
  321. {
  322. JumpToAppWithReset();
  323. }
  324. }
  325. }
  326. /* ****************************************************************************/
  327. /* interface realize **********************************************************/
  328. /**
  329. * @brief Print a string on the HyperTerminal
  330. * @param p_string: The string to be printed
  331. * @retval send state
  332. */
  333. HAL_StatusTypeDef Interface_PutString(uint8_t* p_string)
  334. {
  335. #ifdef IAP_YMODEM_ATY_DBG
  336. uint16_t length = 0;
  337. while(p_string[length] != '\0')
  338. {
  339. length++;
  340. }
  341. return Interface_Transmit(p_string, length, TX_TIMEOUT);
  342. #else
  343. return Interface_Transmit("", 0, TX_TIMEOUT);;
  344. #endif
  345. }
  346. /**
  347. * @brief Transmit a byte to the HyperTerminal
  348. * @param bytes The byte to be sent
  349. * @retval send state
  350. */
  351. HAL_StatusTypeDef Interface_PutByte(uint8_t bytes)
  352. {
  353. return Interface_Transmit(&bytes, 1, TX_TIMEOUT);
  354. }
  355. /**
  356. * @brief Download a file
  357. * @retval download state
  358. */
  359. COM_StatusTypeDef Interface_Download(void)
  360. {
  361. uint8_t number[11] = {0};
  362. uint32_t size = 0;
  363. COM_StatusTypeDef result;
  364. Interface_PutString("\r\nWaiting for the file to be sent... (press 'a' to abort)\r\n");
  365. Interface_PutByte(CRC16);
  366. result = Ymodem_Receive(&size);
  367. if(result == COM_OK)
  368. {
  369. Interface_PutString("\r\n\r\nProgramming Completed Successfully!\n\r--------------------------------\r\n Name: ");
  370. Interface_PutString(aFileName);
  371. Int2Str(number, size);
  372. Interface_PutString("\r\nSize: ");
  373. Interface_PutString(number);
  374. Interface_PutString(" Bytes\r\n");
  375. Interface_PutString("-------------------\r\n");
  376. }
  377. else if(result == COM_LIMIT)
  378. {
  379. Interface_PutString("\r\nThe image size is higher than the allowed space memory!\n\r");
  380. }
  381. else if(result == COM_DATA)
  382. {
  383. Interface_PutString("\r\nVerification failed!\n\r");
  384. }
  385. else if(result == COM_ABORT)
  386. {
  387. Interface_PutString("\r\nAborted by user.\n\r");
  388. }
  389. else
  390. {
  391. Interface_PutString("\r\nFailed to receive the file!\r\n");
  392. }
  393. return result;
  394. }
  395. /**
  396. * @brief Upload a file
  397. */
  398. void Interface_Upload(void)
  399. {
  400. uint8_t status = 0;
  401. Interface_PutString("\r\n\r\nSelect receive file... (press 'a' to abort)\r\n");
  402. while(1)
  403. {
  404. Interface_Receive(&status, 1, RX_TIMEOUT);
  405. if(status == CRC16)
  406. {
  407. /* Transmit the flash image through ymodem protocol */
  408. status = Ymodem_Transmit((uint8_t*)APPLICATION_ADDRESS, (const uint8_t*)"UploadedFlashImage.bin", USER_FLASH_SIZE);
  409. if(status != 0)
  410. {
  411. if(status == COM_ABORT)
  412. Interface_PutString("\r\nAborted by user.\n\r");
  413. else
  414. Interface_PutString("\r\nError occurred while transmitting file\r\n");
  415. }
  416. else
  417. {
  418. Interface_PutString("\r\nFile uploaded successfully\r\n");
  419. }
  420. break;
  421. }
  422. else if(status == ABORT1 || status == ABORT2)
  423. {
  424. Interface_PutString("\r\nAborted by user.\n\r");
  425. break;
  426. }
  427. }
  428. }
  429. /* common *********************************************************************/
  430. /**
  431. * @brief Convert an Integer to a string
  432. * @param p_str: The string output pointer
  433. * @param intnum: The integer to be converted
  434. */
  435. void Int2Str(uint8_t* p_str, uint32_t intnum)
  436. {
  437. uint32_t i, divider = 1000000000, pos = 0, status = 0;
  438. for(i = 0; i < 10; i++)
  439. {
  440. p_str[pos++] = (intnum / divider) + 48;
  441. intnum = intnum % divider;
  442. divider /= 10;
  443. if((p_str[pos - 1] == '0') & (status == 0))
  444. {
  445. pos = 0;
  446. }
  447. else
  448. {
  449. status++;
  450. }
  451. }
  452. }
  453. /**
  454. * @brief Convert a string to an integer
  455. * @param p_inputstr: The string to be converted
  456. * @param p_intnum: The integer value
  457. * @retval 1: Correct
  458. * 0: Error
  459. */
  460. uint32_t Str2Int(uint8_t* p_inputstr, uint32_t* p_intnum)
  461. {
  462. uint32_t i = 0, res = 0;
  463. uint32_t val = 0;
  464. if((p_inputstr[0] == '0') && ((p_inputstr[1] == 'x') || (p_inputstr[1] == 'X')))
  465. {
  466. i = 2;
  467. while((i < 11) && (p_inputstr[i] != '\0'))
  468. {
  469. if(ISVALIDHEX(p_inputstr[i]))
  470. {
  471. val = (val << 4) + CONVERTHEX(p_inputstr[i]);
  472. }
  473. else
  474. {
  475. /* Return 0, Invalid input */
  476. res = 0;
  477. break;
  478. }
  479. i++;
  480. }
  481. /* valid result */
  482. if(p_inputstr[i] == '\0')
  483. {
  484. *p_intnum = val;
  485. res = 1;
  486. }
  487. }
  488. else /* max 10-digit decimal input */
  489. {
  490. while((i < 11) && (res != 1))
  491. {
  492. if(p_inputstr[i] == '\0')
  493. {
  494. *p_intnum = val;
  495. /* return 1 correct */
  496. res = 1;
  497. }
  498. else if(((p_inputstr[i] == 'k') || (p_inputstr[i] == 'K')) && (i > 0))
  499. {
  500. val = val << 10;
  501. *p_intnum = val;
  502. res = 1;
  503. }
  504. else if(((p_inputstr[i] == 'm') || (p_inputstr[i] == 'M')) && (i > 0))
  505. {
  506. val = val << 20;
  507. *p_intnum = val;
  508. res = 1;
  509. }
  510. else if(ISVALIDDEC(p_inputstr[i]))
  511. {
  512. val = val * 10 + CONVERTDEC(p_inputstr[i]);
  513. }
  514. else
  515. {
  516. /* return 0, Invalid input */
  517. res = 0;
  518. break;
  519. }
  520. i++;
  521. }
  522. }
  523. return res;
  524. }
  525. /* flash_if *******************************************************************/
  526. /**
  527. * @brief Unlocks Flash for write access
  528. */
  529. void FLASH_If_Init(void)
  530. {
  531. /* Unlock the Program memory */
  532. HAL_FLASH_Unlock();
  533. /* Clear all FLASH flags */
  534. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
  535. /* Unlock the Program memory */
  536. HAL_FLASH_Lock();
  537. }
  538. /**
  539. * @brief This function does an erase of all user flash area
  540. * @param start: start of user flash area
  541. * @retval FLASHIF_OK : user flash area successfully erased
  542. * FLASHIF_ERASEKO : error occurred
  543. */
  544. uint32_t FLASH_If_Erase(uint32_t start)
  545. {
  546. uint32_t NbrOfPages = 0;
  547. uint32_t PageError = 0;
  548. FLASH_EraseInitTypeDef pEraseInit;
  549. HAL_StatusTypeDef status = HAL_OK;
  550. /* Unlock the Flash to enable the flash control register access */
  551. HAL_FLASH_Unlock();
  552. /* Get the sector where start the user flash area */
  553. NbrOfPages = (USER_FLASH_END_ADDRESS - start) / FLASH_PAGE_SIZE;
  554. pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
  555. pEraseInit.PageAddress = start;
  556. pEraseInit.Banks = FLASH_BANK_1;
  557. pEraseInit.NbPages = NbrOfPages;
  558. status = HAL_FLASHEx_Erase(&pEraseInit, &PageError);
  559. /* Lock the Flash to disable the flash control register access (recommended
  560. to protect the FLASH memory against possible unwanted operation) */
  561. HAL_FLASH_Lock();
  562. if(status != HAL_OK)
  563. {
  564. /* Error occurred while page erase */
  565. return FLASHIF_ERASEKO;
  566. }
  567. return FLASHIF_OK;
  568. }
  569. /**
  570. * @brief This function writes a data buffer in flash (data are 32-bit aligned).
  571. * @note After writing data buffer, the flash content is checked.
  572. * @param destination: start address for target location
  573. * @param p_source: pointer on buffer with data to write
  574. * @param length: length of data buffer (unit is 32-bit word)
  575. * @retval uint32_t 0: Data successfully written to Flash memory
  576. * 1: Error occurred while writing data in Flash memory
  577. * 2: Written Data in flash memory is different from expected one
  578. */
  579. uint32_t FLASH_If_Write(uint32_t destination, uint32_t* p_source, uint32_t length)
  580. {
  581. uint32_t i = 0;
  582. /* Unlock the Flash to enable the flash control register access */
  583. HAL_FLASH_Unlock();
  584. for(i = 0; (i < length) && (destination <= (USER_FLASH_END_ADDRESS - 4)); i++)
  585. {
  586. /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  587. be done by word */
  588. if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, destination, *(uint32_t*)(p_source + i)) == HAL_OK)
  589. {
  590. /* Check the written value */
  591. if(*(uint32_t*)destination != *(uint32_t*)(p_source + i))
  592. {
  593. /* Flash content doesn't match SRAM content */
  594. return(FLASHIF_WRITINGCTRL_ERROR);
  595. }
  596. /* Increment FLASH destination address */
  597. destination += 4;
  598. }
  599. else
  600. {
  601. /* Error occurred while writing data in Flash memory */
  602. return (FLASHIF_WRITING_ERROR);
  603. }
  604. }
  605. /* Lock the Flash to disable the flash control register access (recommended
  606. to protect the FLASH memory against possible unwanted operation) */
  607. HAL_FLASH_Lock();
  608. return (FLASHIF_OK);
  609. }
  610. /**
  611. * @brief Returns the write protection status of application flash area.
  612. * @retval If a sector in application area is write-protected returned value is a combinaison
  613. of the possible values : FLASHIF_PROTECTION_WRPENABLED, FLASHIF_PROTECTION_PCROPENABLED, ...
  614. * If no sector is write-protected FLASHIF_PROTECTION_NONE is returned.
  615. */
  616. uint32_t FLASH_If_GetWriteProtectionStatus(void)
  617. {
  618. uint32_t ProtectedPAGE = FLASHIF_PROTECTION_NONE;
  619. FLASH_OBProgramInitTypeDef OptionsBytesStruct;
  620. /* Unlock the Flash to enable the flash control register access */
  621. HAL_FLASH_Unlock();
  622. /* Check if there are write protected sectors inside the user flash area */
  623. HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
  624. /* Lock the Flash to disable the flash control register access (recommended
  625. to protect the FLASH memory against possible unwanted operation) */
  626. HAL_FLASH_Lock();
  627. /* Get pages already write protected */
  628. ProtectedPAGE = ~(OptionsBytesStruct.WRPPage) & FLASH_PAGE_TO_BE_PROTECTED;
  629. /* Check if desired pages are already write protected */
  630. if(ProtectedPAGE != 0)
  631. {
  632. /* Some sectors inside the user flash area are write protected */
  633. return FLASHIF_PROTECTION_WRPENABLED;
  634. }
  635. else
  636. {
  637. /* No write protected sectors inside the user flash area */
  638. return FLASHIF_PROTECTION_NONE;
  639. }
  640. }
  641. /**
  642. * @brief Configure the write protection status of user flash area.
  643. * @param protectionstate : FLASHIF_WRP_DISABLE or FLASHIF_WRP_ENABLE the protection
  644. * @retval uint32_t FLASHIF_OK if change is applied.
  645. */
  646. uint32_t FLASH_If_WriteProtectionConfig(uint32_t protectionstate)
  647. {
  648. uint32_t ProtectedPAGE = 0x0;
  649. FLASH_OBProgramInitTypeDef config_new, config_old;
  650. HAL_StatusTypeDef result = HAL_OK;
  651. /* Get pages write protection status */
  652. HAL_FLASHEx_OBGetConfig(&config_old);
  653. /* The parameter says whether we turn the protection on or off */
  654. config_new.WRPState = (protectionstate == FLASHIF_WRP_ENABLE ? OB_WRPSTATE_ENABLE : OB_WRPSTATE_DISABLE);
  655. /* We want to modify only the Write protection */
  656. config_new.OptionType = OPTIONBYTE_WRP;
  657. /* No read protection, keep BOR and reset settings */
  658. config_new.RDPLevel = OB_RDP_LEVEL_0;
  659. config_new.USERConfig = config_old.USERConfig;
  660. /* Get pages already write protected */
  661. ProtectedPAGE = config_old.WRPPage | FLASH_PAGE_TO_BE_PROTECTED;
  662. /* Unlock the Flash to enable the flash control register access */
  663. HAL_FLASH_Unlock();
  664. /* Unlock the Options Bytes */
  665. HAL_FLASH_OB_Unlock();
  666. /* Erase all the option Bytes */
  667. result = HAL_FLASHEx_OBErase();
  668. if(result == HAL_OK)
  669. {
  670. config_new.WRPPage = ProtectedPAGE;
  671. result = HAL_FLASHEx_OBProgram(&config_new);
  672. }
  673. return (result == HAL_OK ? FLASHIF_OK : FLASHIF_PROTECTION_ERRROR);
  674. }
  675. /* ymodem *********************************************************************/
  676. /* activate the CRC16 integrity */
  677. #define CRC16_F
  678. /* ATTENTION - please keep this variable 32bit alligned */
  679. uint8_t aPacketData[PACKET_1K_SIZE + PACKET_DATA_INDEX + PACKET_TRAILER_SIZE];
  680. uint8_t aFileName[FILE_NAME_LENGTH];
  681. /**
  682. * @brief Receive a packet from sender
  683. * @param data
  684. * @param length
  685. * 0: end of transmission
  686. * 2: abort by sender
  687. * >0: packet length
  688. * @param timeout
  689. * @retval HAL_OK: normally return
  690. * HAL_BUSY: abort by user
  691. */
  692. uint8_t byteOneTmp = 0;
  693. HAL_StatusTypeDef statusA;
  694. static HAL_StatusTypeDef ReceivePacket(uint8_t* p_data, uint32_t* p_length, uint32_t timeout)
  695. {
  696. uint32_t crc;
  697. uint32_t packet_size = 0;
  698. HAL_StatusTypeDef status;
  699. uint8_t byteOne;
  700. *p_length = 0;
  701. status = Interface_Receive(&byteOne, 1, timeout);
  702. statusA = status;
  703. byteOneTmp = byteOne;
  704. if(status == HAL_OK)
  705. {
  706. switch(byteOne)
  707. {
  708. case SOH:
  709. packet_size = PACKET_SIZE;
  710. break;
  711. case STX:
  712. packet_size = PACKET_1K_SIZE;
  713. break;
  714. case EOT:
  715. break;
  716. case CA:
  717. if((Interface_Receive(&byteOne, 1, timeout) == HAL_OK) && (byteOne == CA))
  718. {
  719. packet_size = 2;
  720. }
  721. else
  722. {
  723. status = HAL_ERROR;
  724. }
  725. break;
  726. case ABORT1:
  727. case ABORT2:
  728. status = HAL_BUSY;
  729. break;
  730. default:
  731. status = HAL_ERROR;
  732. break;
  733. }
  734. *p_data = byteOne;
  735. if(packet_size >= PACKET_SIZE)
  736. {
  737. status = Interface_Receive(&p_data[PACKET_NUMBER_INDEX], packet_size + PACKET_OVERHEAD_SIZE, timeout);
  738. /* Simple packet sanity check */
  739. if(status == HAL_OK)
  740. {
  741. if(p_data[PACKET_NUMBER_INDEX] != ((p_data[PACKET_CNUMBER_INDEX]) ^ NEGATIVE_BYTE))
  742. {
  743. packet_size = 0;
  744. status = HAL_ERROR;
  745. }
  746. else
  747. {
  748. /* Check packet CRC */
  749. crc = p_data[packet_size + PACKET_DATA_INDEX] << 8;
  750. crc += p_data[packet_size + PACKET_DATA_INDEX + 1];
  751. if(Cal_CRC16(&p_data[PACKET_DATA_INDEX], packet_size) != crc)
  752. {
  753. packet_size = 0;
  754. status = HAL_ERROR;
  755. }
  756. }
  757. }
  758. else
  759. {
  760. packet_size = 0;
  761. }
  762. }
  763. }
  764. *p_length = packet_size;
  765. return status;
  766. }
  767. /**
  768. * @brief Prepare the first block
  769. * @param p_data: output buffer
  770. * @param p_file_name: name of the file to be sent
  771. * @param length: length of the file to be sent in bytes
  772. */
  773. static void PrepareIntialPacket(uint8_t* p_data, const uint8_t* p_file_name, uint32_t length)
  774. {
  775. uint32_t i, j = 0;
  776. uint8_t astring[10];
  777. /* first 3 bytes are constant */
  778. p_data[PACKET_START_INDEX] = SOH;
  779. p_data[PACKET_NUMBER_INDEX] = 0x00;
  780. p_data[PACKET_CNUMBER_INDEX] = 0xff;
  781. /* Filename written */
  782. for(i = 0; (p_file_name[i] != '\0') && (i < FILE_NAME_LENGTH); i++)
  783. {
  784. p_data[i + PACKET_DATA_INDEX] = p_file_name[i];
  785. }
  786. p_data[i + PACKET_DATA_INDEX] = 0x00;
  787. /* file size written */
  788. Int2Str(astring, length);
  789. i = i + PACKET_DATA_INDEX + 1;
  790. while(astring[j] != '\0')
  791. {
  792. p_data[i++] = astring[j++];
  793. }
  794. /* padding with zeros */
  795. for(j = i; j < PACKET_SIZE + PACKET_DATA_INDEX; j++)
  796. {
  797. p_data[j] = 0;
  798. }
  799. }
  800. /**
  801. * @brief Prepare the data packet
  802. * @param p_source: pointer to the data to be sent
  803. * @param p_packet: pointer to the output buffer
  804. * @param pkt_nr: number of the packet
  805. * @param size_blk: length of the block to be sent in bytes
  806. */
  807. static void PreparePacket(uint8_t* p_source, uint8_t* p_packet, uint8_t pkt_nr, uint32_t size_blk)
  808. {
  809. uint8_t* p_record;
  810. uint32_t i, size, packet_size;
  811. /* Make first three packet */
  812. packet_size = size_blk >= PACKET_1K_SIZE ? PACKET_1K_SIZE : PACKET_SIZE;
  813. size = size_blk < packet_size ? size_blk : packet_size;
  814. if(packet_size == PACKET_1K_SIZE)
  815. {
  816. p_packet[PACKET_START_INDEX] = STX;
  817. }
  818. else
  819. {
  820. p_packet[PACKET_START_INDEX] = SOH;
  821. }
  822. p_packet[PACKET_NUMBER_INDEX] = pkt_nr;
  823. p_packet[PACKET_CNUMBER_INDEX] = (~pkt_nr);
  824. p_record = p_source;
  825. /* Filename packet has valid data */
  826. for(i = PACKET_DATA_INDEX; i < size + PACKET_DATA_INDEX; i++)
  827. {
  828. p_packet[i] = *p_record++;
  829. }
  830. if(size <= packet_size)
  831. {
  832. for(i = size + PACKET_DATA_INDEX; i < packet_size + PACKET_DATA_INDEX; i++)
  833. {
  834. p_packet[i] = 0x1A; /* EOF (0x1A) or 0x00 */
  835. }
  836. }
  837. }
  838. /**
  839. * @brief Update CRC16 for input byte
  840. * @param crc_in input value
  841. * @param input byte
  842. */
  843. uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte)
  844. {
  845. uint32_t crc = crc_in;
  846. uint32_t in = byte | 0x100;
  847. do
  848. {
  849. crc <<= 1;
  850. in <<= 1;
  851. if(in & 0x100)
  852. ++crc;
  853. if(crc & 0x10000)
  854. crc ^= 0x1021;
  855. }
  856. while(!(in & 0x10000));
  857. return crc & 0xffffu;
  858. }
  859. /**
  860. * @brief Cal CRC16 for YModem Packet
  861. * @param data
  862. * @param length
  863. */
  864. uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size)
  865. {
  866. uint32_t crc = 0;
  867. const uint8_t* dataEnd = p_data + size;
  868. while(p_data < dataEnd)
  869. crc = UpdateCRC16(crc, *p_data++);
  870. crc = UpdateCRC16(crc, 0);
  871. crc = UpdateCRC16(crc, 0);
  872. return crc & 0xffffu;
  873. }
  874. /**
  875. * @brief Calculate Check sum for YModem Packet
  876. * @param p_data Pointer to input data
  877. * @param size length of input data
  878. * @retval uint8_t checksum value
  879. */
  880. uint8_t CalcChecksum(const uint8_t* p_data, uint32_t size)
  881. {
  882. uint32_t sum = 0;
  883. const uint8_t* p_data_end = p_data + size;
  884. while(p_data < p_data_end)
  885. {
  886. sum += *p_data++;
  887. }
  888. return (sum & 0xffu);
  889. }
  890. /* Public functions ---------------------------------------------------------*/
  891. /**
  892. * @brief Receive a file using the ymodem protocol with CRC16.
  893. * @param p_size The size of the file.
  894. * @retval COM_StatusTypeDef result of reception/programming
  895. */
  896. HAL_StatusTypeDef statA = HAL_OK;
  897. uint16_t packets_received;
  898. COM_StatusTypeDef Ymodem_Receive(uint32_t* p_size)
  899. {
  900. uint32_t i, packet_length, session_done = 0, file_done, errors = 0, session_begin = 0;
  901. uint32_t flashdestination, ramsource, filesize;
  902. uint8_t* file_ptr;
  903. uint8_t file_size[FILE_SIZE_LENGTH], tmp;
  904. COM_StatusTypeDef result = COM_OK;
  905. /* Initialize flashdestination variable */
  906. flashdestination = APPLICATION_ADDRESS;
  907. while((session_done == 0) && (result == COM_OK))
  908. {
  909. packets_received = 0;
  910. file_done = 0;
  911. while((file_done == 0) && (result == COM_OK))
  912. {
  913. statA = ReceivePacket(aPacketData, &packet_length, DOWNLOAD_TIMEOUT);
  914. switch(statA)
  915. {
  916. case HAL_OK:
  917. errors = 0;
  918. switch(packet_length)
  919. {
  920. case 2:
  921. /* Abort by sender */
  922. Interface_PutByte(ACK);
  923. result = COM_ABORT;
  924. break;
  925. case 0:
  926. /* End of transmission */
  927. Interface_PutByte(ACK);
  928. file_done = 1;
  929. break;
  930. default:
  931. /* Normal packet */
  932. if(aPacketData[PACKET_NUMBER_INDEX] != (packets_received % 256))
  933. {
  934. Interface_PutByte(NAK);
  935. }
  936. else
  937. {
  938. if(packets_received == 0)
  939. {
  940. /* File name packet */
  941. if(aPacketData[PACKET_DATA_INDEX] != 0)
  942. {
  943. /* File name extraction */
  944. i = 0;
  945. file_ptr = aPacketData + PACKET_DATA_INDEX;
  946. while((*file_ptr != 0) && (i < FILE_NAME_LENGTH))
  947. {
  948. aFileName[i++] = *file_ptr++;
  949. }
  950. /* File size extraction */
  951. aFileName[i++] = '\0';
  952. i = 0;
  953. file_ptr++;
  954. while((*file_ptr != ' ') && (i < FILE_SIZE_LENGTH))
  955. {
  956. file_size[i++] = *file_ptr++;
  957. }
  958. file_size[i++] = '\0';
  959. Str2Int(file_size, &filesize);
  960. /* Test the size of the image to be sent */
  961. /* Image size is greater than Flash size */
  962. if(*p_size > (USER_FLASH_SIZE + 1))
  963. {
  964. /* End session */
  965. tmp = CA;
  966. Interface_Transmit(&tmp, 1, NAK_TIMEOUT);
  967. Interface_Transmit(&tmp, 1, NAK_TIMEOUT);
  968. result = COM_LIMIT;
  969. }
  970. /* Erase user application area */
  971. FLASH_If_Erase(APPLICATION_ADDRESS);
  972. *p_size = filesize;
  973. Interface_PutByte(ACK);
  974. Interface_PutByte(CRC16);
  975. }
  976. /* File header packet is empty, end session */
  977. else
  978. {
  979. Interface_PutByte(ACK);
  980. file_done = 1;
  981. session_done = 1;
  982. break;
  983. }
  984. }
  985. else /* Data packet */
  986. {
  987. ramsource = (uint32_t)&aPacketData[PACKET_DATA_INDEX];
  988. /* Write received data in Flash */
  989. if(FLASH_If_Write(flashdestination, (uint32_t*)ramsource, packet_length / 4) == FLASHIF_OK)
  990. {
  991. flashdestination += packet_length;
  992. Interface_PutByte(ACK);
  993. }
  994. else /* An error occurred while writing to Flash memory */
  995. {
  996. /* End session */
  997. Interface_PutByte(CA);
  998. Interface_PutByte(CA);
  999. result = COM_DATA;
  1000. }
  1001. }
  1002. packets_received++;
  1003. session_begin = 1;
  1004. }
  1005. break;
  1006. }
  1007. break;
  1008. case HAL_BUSY: /* Abort actually */
  1009. Interface_PutByte(CA);
  1010. Interface_PutByte(CA);
  1011. result = COM_ABORT;
  1012. break;
  1013. default:
  1014. if(session_begin > 0)
  1015. {
  1016. errors++;
  1017. }
  1018. if(errors > MAX_ERRORS)
  1019. {
  1020. /* Abort communication */
  1021. Interface_PutByte(CA);
  1022. Interface_PutByte(CA);
  1023. }
  1024. else
  1025. {
  1026. /* Ask for a packet */
  1027. Interface_PutByte(CRC16);
  1028. }
  1029. break;
  1030. }
  1031. }
  1032. }
  1033. return result;
  1034. }
  1035. /**
  1036. * @brief Transmit a file using the ymodem protocol
  1037. * @param p_buf: Address of the first byte
  1038. * @param p_file_name: Name of the file sent
  1039. * @param file_size: Size of the transmission
  1040. * @retval COM_StatusTypeDef result of the communication
  1041. */
  1042. COM_StatusTypeDef Ymodem_Transmit(uint8_t* p_buf, const uint8_t* p_file_name, uint32_t file_size)
  1043. {
  1044. uint32_t errors = 0, ack_recpt = 0, size = 0, pkt_size;
  1045. uint8_t* p_buf_int;
  1046. COM_StatusTypeDef result = COM_OK;
  1047. uint32_t blk_number = 1;
  1048. uint8_t a_rx_ctrl[2];
  1049. uint8_t i;
  1050. #ifdef CRC16_F
  1051. uint32_t temp_crc;
  1052. #else /* CRC16_F */
  1053. uint8_t temp_chksum;
  1054. #endif /* CRC16_F */
  1055. /* Prepare first block - header */
  1056. PrepareIntialPacket(aPacketData, p_file_name, file_size);
  1057. while((!ack_recpt) && (result == COM_OK))
  1058. {
  1059. /* Send Packet */
  1060. Interface_Transmit(&aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT);
  1061. /* Send CRC or Check Sum based on CRC16_F */
  1062. #ifdef CRC16_F
  1063. temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
  1064. Interface_PutByte(temp_crc >> 8);
  1065. Interface_PutByte(temp_crc & 0xFF);
  1066. #else /* CRC16_F */
  1067. temp_chksum = CalcChecksum(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
  1068. Interface_PutByte(temp_chksum);
  1069. #endif /* CRC16_F */
  1070. /* Wait for Ack and 'C' */
  1071. if(Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
  1072. {
  1073. if(a_rx_ctrl[0] == ACK)
  1074. {
  1075. ack_recpt = 1;
  1076. }
  1077. else if(a_rx_ctrl[0] == CA)
  1078. {
  1079. if((Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA))
  1080. {
  1081. HAL_Delay(2);
  1082. Interface_Clean();
  1083. result = COM_ABORT;
  1084. }
  1085. }
  1086. else if(a_rx_ctrl[0] == ABORT1 || a_rx_ctrl[0] == ABORT2)
  1087. {
  1088. result = COM_ABORT;
  1089. }
  1090. }
  1091. else
  1092. {
  1093. errors++;
  1094. }
  1095. if(errors >= MAX_ERRORS)
  1096. {
  1097. result = COM_ERROR;
  1098. }
  1099. }
  1100. p_buf_int = p_buf;
  1101. size = file_size;
  1102. /* Here 1024 bytes length is used to send the packets */
  1103. while((size) && (result == COM_OK))
  1104. {
  1105. /* Prepare next packet */
  1106. PreparePacket(p_buf_int, aPacketData, blk_number, size);
  1107. ack_recpt = 0;
  1108. a_rx_ctrl[0] = 0;
  1109. errors = 0;
  1110. /* Resend packet if NAK for few times else end of communication */
  1111. while((!ack_recpt) && (result == COM_OK))
  1112. {
  1113. /* Send next packet */
  1114. if(size >= PACKET_1K_SIZE)
  1115. {
  1116. pkt_size = PACKET_1K_SIZE;
  1117. }
  1118. else
  1119. {
  1120. pkt_size = PACKET_SIZE;
  1121. }
  1122. Interface_Transmit(&aPacketData[PACKET_START_INDEX], pkt_size + PACKET_HEADER_SIZE, NAK_TIMEOUT);
  1123. /* Send CRC or Check Sum based on CRC16_F */
  1124. #ifdef CRC16_F
  1125. temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], pkt_size);
  1126. Interface_PutByte(temp_crc >> 8);
  1127. Interface_PutByte(temp_crc & 0xFF);
  1128. #else /* CRC16_F */
  1129. temp_chksum = CalcChecksum(&aPacketData[PACKET_DATA_INDEX], pkt_size);
  1130. Interface_PutByte(temp_chksum);
  1131. #endif /* CRC16_F */
  1132. /* Wait for Ack */
  1133. if((Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == ACK))
  1134. {
  1135. ack_recpt = 1;
  1136. if(size > pkt_size)
  1137. {
  1138. p_buf_int += pkt_size;
  1139. size -= pkt_size;
  1140. if(blk_number == (USER_FLASH_SIZE / PACKET_1K_SIZE))
  1141. {
  1142. result = COM_LIMIT; /* boundary error */
  1143. }
  1144. else
  1145. {
  1146. blk_number++;
  1147. }
  1148. }
  1149. else
  1150. {
  1151. p_buf_int += pkt_size;
  1152. size = 0;
  1153. }
  1154. }
  1155. else
  1156. {
  1157. errors++;
  1158. }
  1159. /* Resend packet if NAK for a count of 10 else end of communication */
  1160. if(errors >= MAX_ERRORS)
  1161. {
  1162. result = COM_ERROR;
  1163. }
  1164. }
  1165. }
  1166. /* Sending End Of Transmission char */
  1167. ack_recpt = 0;
  1168. a_rx_ctrl[0] = 0x00;
  1169. errors = 0;
  1170. while((!ack_recpt) && (result == COM_OK))
  1171. {
  1172. Interface_PutByte(EOT);
  1173. /* Wait for Ack */
  1174. if(Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
  1175. {
  1176. if(a_rx_ctrl[0] == ACK)
  1177. {
  1178. ack_recpt = 1;
  1179. }
  1180. else if(a_rx_ctrl[0] == CA)
  1181. {
  1182. if((Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA))
  1183. {
  1184. HAL_Delay(2);
  1185. Interface_Clean();
  1186. result = COM_ABORT;
  1187. }
  1188. }
  1189. }
  1190. else
  1191. {
  1192. errors++;
  1193. }
  1194. if(errors >= MAX_ERRORS)
  1195. {
  1196. result = COM_ERROR;
  1197. }
  1198. }
  1199. /* Empty packet sent - some terminal emulators need this to close session */
  1200. if(result == COM_OK)
  1201. {
  1202. /* Preparing an empty packet */
  1203. aPacketData[PACKET_START_INDEX] = SOH;
  1204. aPacketData[PACKET_NUMBER_INDEX] = 0;
  1205. aPacketData[PACKET_CNUMBER_INDEX] = 0xFF;
  1206. for(i = PACKET_DATA_INDEX; i < (PACKET_SIZE + PACKET_DATA_INDEX); i++)
  1207. {
  1208. aPacketData[i] = 0x00;
  1209. }
  1210. /* Send Packet */
  1211. Interface_Transmit(&aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT);
  1212. /* Send CRC or Check Sum based on CRC16_F */
  1213. #ifdef CRC16_F
  1214. temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
  1215. Interface_PutByte(temp_crc >> 8);
  1216. Interface_PutByte(temp_crc & 0xFF);
  1217. #else /* CRC16_F */
  1218. temp_chksum = CalcChecksum(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
  1219. Interface_PutByte(temp_chksum);
  1220. #endif /* CRC16_F */
  1221. /* Wait for Ack and 'C' */
  1222. if(Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
  1223. {
  1224. if(a_rx_ctrl[0] == CA)
  1225. {
  1226. HAL_Delay(2);
  1227. Interface_Clean();
  1228. result = COM_ABORT;
  1229. }
  1230. }
  1231. }
  1232. return result; /* File transmitted successfully */
  1233. }
  1234. #endif /* __IAP_YMODEM_ATY_C */
  1235. /******************************** End Of File *********************************/