Browse Source

finish uart ucdc IAP test

JY 5 months ago
parent
commit
35a40ae185
2 changed files with 262 additions and 247 deletions
  1. 230 224
      IAP_YMODEM_ATY.c
  2. 32 23
      IAP_YMODEM_ATY.h

+ 230 - 224
IAP_YMODEM_ATY.c

@@ -21,6 +21,10 @@
 * @version
 *       - 1_01_240911 > ATY
 *           -# Preliminary version, first Release
+*       - 1_01_250701 > ATY
+*           -# real fix to lib type
+*       - 1_01_250704 > ATY
+*           -# finish uart and ucdc IAP whole test
 ********************************************************************************
 */
 
@@ -33,20 +37,17 @@
 uint8_t EmptyBuf[1] = {0};
 
 USBD_CDC_HandleTypeDef* hcdc;
-uint32_t PUT_IN_CDC_Receive_FS = 0;
+uint32_t ucdcRcvLength = 0;
 uint8_t ucdcRcvOverFlag = 1;
 uint32_t ucdcRcvLastTime = 0;
 uint8_t ucdcRcvBuffer[APP_RX_DATA_SIZE] = {0};
 
-uint32_t rcvDbg = 0;
-uint8_t temp_str[12] = {0};
-
 void All_Reset(void)
 {
     // Disable global interrupts
     __disable_irq();
 
-
+    // Deinnit peripherals
     HAL_UART_DeInit(&Interface_UART);
     __HAL_RCC_USB_CLK_DISABLE();
     HAL_RCC_DeInit();
@@ -83,20 +84,47 @@ void Interface_Clean(void)
 #ifndef IAP_YMODEM_ATY_USB
     __HAL_UART_FLUSH_DRREGISTER(&Interface_UART);
 #else
-    PUT_IN_CDC_Receive_FS = 0;
+    ucdcRcvLength = 0;
     ucdcRcvOverFlag = 1;
 
 #endif
 }
 
+void PUT_IN_CDC_Receive_FS(uint8_t* Buf, uint32_t* Len)
+{
+    if(ucdcRcvOverFlag == 1){ // new start
+        ucdcRcvOverFlag = 0;
+        ucdcRcvLength = *Len;
+        memcpy(ucdcRcvBuffer, Buf, *Len);
+        if(*Len < 64){
+            ucdcRcvOverFlag = 1;
+        }
+        else{
+            ucdcRcvLastTime = HAL_GetTick();
+        }
+    }
+    else{ // pending to before
+        memcpy(ucdcRcvBuffer + ucdcRcvLength, Buf, *Len);
+        ucdcRcvLength += *Len;
+        if(*Len < 64){
+            ucdcRcvOverFlag = 1;
+        }
+        else{
+            ucdcRcvLastTime = HAL_GetTick();
+        }
+    }
+    // USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
+    // USBD_CDC_ReceivePacket(&hUsbDeviceFS);
+    // return (USBD_OK);
+}
+
+
 uint32_t timeoutCount = 0;
 uint32_t timeoutFinal = 100;
 HAL_StatusTypeDef Interface_Receive(uint8_t* bytes, uint16_t size, uint32_t timeout)
 {
 #ifndef IAP_YMODEM_ATY_USB
     HAL_StatusTypeDef state = HAL_UART_Receive(&Interface_UART, bytes, size, timeout);
-    uint8_t length = (size > 12 ? 12 : size);
-    memcpy(temp_str, bytes, length);
     return state;
     // return HAL_UART_Receive(&Interface_UART, bytes, size, timeout);
 #else
@@ -106,7 +134,7 @@ HAL_StatusTypeDef Interface_Receive(uint8_t* bytes, uint16_t size, uint32_t time
 
     timeoutCount = 0;
     timeoutFinal = timeout;
-    while(PUT_IN_CDC_Receive_FS == 0){
+    while(ucdcRcvLength == 0){
         timeoutCount++;
         if(timeoutCount > timeoutFinal){
             timeoutCount = 0;
@@ -123,12 +151,11 @@ HAL_StatusTypeDef Interface_Receive(uint8_t* bytes, uint16_t size, uint32_t time
         ucdcRcvOverFlag = 1;
     }
 
-    if(PUT_IN_CDC_Receive_FS > 0 && PUT_IN_CDC_Receive_FS <= APP_RX_DATA_SIZE){
-        uint16_t copySize = (PUT_IN_CDC_Receive_FS > size) ? size : PUT_IN_CDC_Receive_FS;
+    if(ucdcRcvLength > 0 && ucdcRcvLength <= APP_RX_DATA_SIZE){
+        uint16_t copySize = (ucdcRcvLength > size) ? size : ucdcRcvLength;
         memcpy(bytes, ucdcRcvBuffer, copySize);
-        PUT_IN_CDC_Receive_FS -= copySize;
-        memcpy(ucdcRcvBuffer, ucdcRcvBuffer + copySize, PUT_IN_CDC_Receive_FS);
-        // Interface_Clean();
+        ucdcRcvLength -= copySize;
+        memcpy(ucdcRcvBuffer, ucdcRcvBuffer + copySize, ucdcRcvLength);
     }
     return HAL_OK;
 #endif
@@ -159,24 +186,169 @@ HAL_StatusTypeDef Interface_Transmit(uint8_t* bytes, uint16_t size, uint32_t tim
 }
 
 
+/**
+  * @brief  Display the Main Menu on HyperTerminal
+  */
+void Main_Menu(void)
+{
+    uint8_t key = 0;
+    uint16_t errTimes = 0;
+    uint32_t FlashProtection = 0;
+
+    /* Initialise Flash */
+    FLASH_If_Init();
+
+    /* Test if any sector of Flash memory where user application will be loaded is write protected */
+    FlashProtection = FLASH_If_GetWriteProtectionStatus();
+
+    Interface_PutString("\r\n\r\n=================== Main Menu ============================\r\n\r\n");
+    Interface_PutString("  Download image to the internal Flash ----------------- 1\r\n\r\n");
+    Interface_PutString("  Upload image from the internal Flash ----------------- 2\r\n\r\n");
+    Interface_PutString("  Execute the loaded application ----------------------- 3\r\n\r\n");
+
+    if(FlashProtection != FLASHIF_PROTECTION_NONE)
+    {
+        Interface_PutString("  Disable the write protection ------------------------- 4\r\n\r\n");
+    }
+    else
+    {
+        Interface_PutString("  Enable the write protection -------------------------- 4\r\n\r\n");
+    }
+    Interface_PutString("==========================================================\r\n\r\n");
+
+    while(1)
+    {
+        /* Clean the input path */
+        Interface_Clean();
+
+        /* Receive key */
+        uint16_t autoBootTime = 5000;
+        Interface_Receive(&key, 1, autoBootTime);
+        /* Auto jump to app */
+        if(key == 0)
+            key = 51;
+
+        switch(key)
+        {
+            case '*':
+                /* Upload user application from the Flash */
+                Interface_Upload();
+                break;
+            case '2':
+                /* Upload user application from the Flash */
+                Interface_PutString("\r\nAuthorize wrong!\r\n\r\n");
+                break;
+            case '1':
+                /* Download user application in the Flash */
+                if(Interface_Download() != COM_OK)
+                    break;
+            case '3':
+                Interface_PutString("\r\nStart program execution...\r\n");
+                JumpToAppWithReset();
+                break;
+            case '4':
+                if(FlashProtection != FLASHIF_PROTECTION_NONE)
+                {
+                    /* Disable the write protection */
+                    if(FLASH_If_WriteProtectionConfig(FLASHIF_WRP_DISABLE) == FLASHIF_OK)
+                    {
+                        Interface_PutString("Write protection disabled...\r\n");
+                        Interface_PutString("System will now restart...\r\n");
+                        /* Launch the option byte loading */
+                        HAL_FLASH_OB_Launch();
+                    }
+                    else
+                    {
+                        Interface_PutString("Error: Flash write un-protection failed...\r\n");
+                    }
+                }
+                else
+                {
+                    if(FLASH_If_WriteProtectionConfig(FLASHIF_WRP_ENABLE) == FLASHIF_OK)
+                    {
+                        Interface_PutString("Write protection enabled...\r\n");
+                        Interface_PutString("System will now restart...\r\n");
+                        /* Launch the option byte loading */
+                        HAL_FLASH_OB_Launch();
+                    }
+                    else
+                    {
+                        Interface_PutString("Error: Flash write protection failed...\r\n");
+                    }
+                }
+                break;
+            default:
+                errTimes++;
+                if(errTimes > 500){
+                    Interface_PutString("Start program execution for max err...\r\n\r\n");
+                    JumpToAppWithReset();
+                }
+                // Interface_PutString("Invalid Number ! ==> The number should be either 1, 2, 3 or 4\r\n");
+                break;
+        }
+        key = 0;
+    }
+}
+
+void Main_Cycle(void)
+{
+    uint8_t sKey = 0;
+    Interface_PutString("\r\n\r\nS to start\r\n\r\n");
+    while(1)
+    {
+        if(1)
+        {
+            if(Interface_Receive(&sKey, 1, RX_TIMEOUT) == HAL_OK)
+            {
+                if(sKey == 'S')
+                {
+                    Interface_Clean();
+                    uint8_t verStr[] = "1_01_250705\r\n";
+                    Interface_Transmit(verStr, sizeof(verStr), 100);
+                    Main_Menu();
+                }
+                else{
+                    Interface_Clean();
+                }
+            }
+            else{
+                Interface_Clean();
+            }
+        }
+        /* Keep the user application running */
+        else
+        {
+            JumpToAppWithReset();
+        }
+    }
+}
+
+/* ****************************************************************************/
+
+
+/* interface realize **********************************************************/
 /**
   * @brief  Print a string on the HyperTerminal
   * @param  p_string: The string to be printed
-  * @retval None
+  * @retval send state
   */
 HAL_StatusTypeDef Interface_PutString(uint8_t* p_string)
 {
+#ifdef IAP_YMODEM_ATY_DBG
     uint16_t length = 0;
     while(p_string[length] != '\0')
     {
         length++;
     }
     return Interface_Transmit(p_string, length, TX_TIMEOUT);
+#else
+    return Interface_Transmit("", 0, TX_TIMEOUT);;
+#endif
 }
 
 /**
   * @brief  Transmit a byte to the HyperTerminal
-  * @param  param The byte to be sent
+  * @param  bytes The byte to be sent
   * @retval send state
   */
 
@@ -185,12 +357,9 @@ HAL_StatusTypeDef Interface_PutByte(uint8_t bytes)
     return Interface_Transmit(&bytes, 1, TX_TIMEOUT);
 }
 
-uint32_t FlashProtection = 0;
-
 /**
   * @brief  Download a file
-  * @param  None
-  * @retval None
+  * @retval download state
   */
 COM_StatusTypeDef Interface_Download(void)
 {
@@ -198,7 +367,7 @@ COM_StatusTypeDef Interface_Download(void)
     uint32_t size = 0;
     COM_StatusTypeDef result;
 
-    Interface_PutString("\r\nWaiting for the file to be sent ... (press 'a' to abort)\r\n");
+    Interface_PutString("\r\nWaiting for the file to be sent... (press 'a' to abort)\r\n");
     result = Ymodem_Receive(&size);
     if(result == COM_OK)
     {
@@ -231,14 +400,12 @@ COM_StatusTypeDef Interface_Download(void)
 
 /**
   * @brief  Upload a file
-  * @param  None
-  * @retval None
   */
 void Interface_Upload(void)
 {
     uint8_t status = 0;
 
-    Interface_PutString("\r\n\r\nSelect receive file ... (press 'a' to abort)\r\n");
+    Interface_PutString("\r\n\r\nSelect receive file... (press 'a' to abort)\r\n");
     while(1)
     {
         Interface_Receive(&status, 1, RX_TIMEOUT);
@@ -268,157 +435,12 @@ void Interface_Upload(void)
     }
 }
 
-uint16_t errTimes = 0;
-uint8_t key = 0;
-/**
-  * @brief  Display the Main Menu on HyperTerminal
-  * @param  None
-  * @retval None
-  */
-void Main_Menu(void)
-{
-    /* Initialise Flash */
-    FLASH_If_Init();
-
-    /* Test if any sector of Flash memory where user application will be loaded is write protected */
-    FlashProtection = FLASH_If_GetWriteProtectionStatus();
 
-#ifdef IAP_YMODEM_ATY_DBG
-    Interface_PutString("\r\n\r\n=================== Main Menu ============================\r\n\r\n");
-    Interface_PutString("  Download image to the internal Flash ----------------- 1\r\n\r\n");
-    Interface_PutString("  Upload image from the internal Flash ----------------- 2\r\n\r\n");
-    Interface_PutString("  Execute the loaded application ----------------------- 3\r\n\r\n");
-
-    if(FlashProtection != FLASHIF_PROTECTION_NONE)
-    {
-        Interface_PutString("  Disable the write protection ------------------------- 4\r\n\r\n");
-    }
-    else
-    {
-        Interface_PutString("  Enable the write protection -------------------------- 4\r\n\r\n");
-    }
-    Interface_PutString("==========================================================\r\n\r\n");
-#endif
-
-    while(1)
-    {
-        /* Clean the input path */
-        Interface_Clean();
-
-        /* Receive key */
-        uint16_t autoBootTime = 50000;
-        Interface_Receive(&key, 1, autoBootTime);
-//        if(key == 0)
-//            key = 51;
-
-        if(1){
-            switch(key)
-            {
-                case '*':
-                    /* Upload user application from the Flash */
-                    Interface_Upload();
-                    break;
-                case '2':
-                    /* Upload user application from the Flash */
-                    Interface_PutString("\r\nAuthorize wrong!\r\n\r\n");
-                    break;
-                case '1':
-                    /* Download user application in the Flash */
-                    if(Interface_Download() != COM_OK)
-                        break;
-                case '3':
-                    Interface_PutString("\r\nStart program execution......\r\n");
-                    JumpToAppWithReset();
-                    break;
-                case '4':
-                    if(FlashProtection != FLASHIF_PROTECTION_NONE)
-                    {
-                      /* Disable the write protection */
-                        if(FLASH_If_WriteProtectionConfig(FLASHIF_WRP_DISABLE) == FLASHIF_OK)
-                        {
-                            Interface_PutString("Write protection disabled...\r\n");
-                            Interface_PutString("System will now restart...\r\n");
-                            /* Launch the option byte loading */
-                            HAL_FLASH_OB_Launch();
-                        }
-                        else
-                        {
-                            Interface_PutString("Error: Flash write un-protection failed...\r\n");
-                        }
-                    }
-                    else
-                    {
-                        if(FLASH_If_WriteProtectionConfig(FLASHIF_WRP_ENABLE) == FLASHIF_OK)
-                        {
-                            Interface_PutString("Write protection enabled...\r\n");
-                            Interface_PutString("System will now restart...\r\n");
-                            /* Launch the option byte loading */
-                            HAL_FLASH_OB_Launch();
-                        }
-                        else
-                        {
-                            Interface_PutString("Error: Flash write protection failed...\r\n");
-                        }
-                    }
-                    break;
-                default:
-                    errTimes++;
-                    if(errTimes > 500){
-                        Interface_PutString("Start program execution for max err......\r\n\r\n");
-                        JumpToAppWithReset();
-                    }
-                    // Interface_PutString("Invalid Number ! ==> The number should be either 1, 2, 3 or 4\r\n");
-                    break;
-            }
-        }
-    }
-}
-
-
-uint8_t sKey = 0;
-void Main_Cycle(void)
-{
-    Interface_PutString("\r\n\r\nS to start\r\n\r\n");
-    // Interface_Clean();
-    while(1)
-    {
-        if(1)
-        {
-            // if(Interface_Receive(&sKey, 1, DOWNLOAD_TIMEOUT) != HAL_OK)
-            // {
-            //     Interface_PutByte(CRC16);
-            // }
-            if(Interface_Receive(&sKey, 1, RX_TIMEOUT) == HAL_OK)
-            {
-                if(sKey == 'S')
-                {
-                    Interface_Clean();
-                    Main_Menu();
-                }
-                else{
-                    Interface_Clean();
-                }
-            }
-            else{
-                Interface_Clean();
-            }
-        }
-        /* Keep the user application running */
-        else
-        {
-            JumpToAppWithReset();
-        }
-    }
-}
-
-
-/* ****************************************************************************/
 /* common *********************************************************************/
 /**
   * @brief  Convert an Integer to a string
   * @param  p_str: The string output pointer
   * @param  intnum: The integer to be converted
-  * @retval None
   */
 void Int2Str(uint8_t* p_str, uint32_t intnum)
 {
@@ -464,7 +486,7 @@ uint32_t Str2Int(uint8_t* p_inputstr, uint32_t* p_intnum)
             }
             else
             {
-              /* Return 0, Invalid input */
+                /* Return 0, Invalid input */
                 res = 0;
                 break;
             }
@@ -485,7 +507,7 @@ uint32_t Str2Int(uint8_t* p_inputstr, uint32_t* p_intnum)
             if(p_inputstr[i] == '\0')
             {
                 *p_intnum = val;
-                /* return 1 */
+                /* return 1 correct */
                 res = 1;
             }
             else if(((p_inputstr[i] == 'k') || (p_inputstr[i] == 'K')) && (i > 0))
@@ -506,7 +528,7 @@ uint32_t Str2Int(uint8_t* p_inputstr, uint32_t* p_intnum)
             }
             else
             {
-              /* return 0, Invalid input */
+                /* return 0, Invalid input */
                 res = 0;
                 break;
             }
@@ -518,12 +540,9 @@ uint32_t Str2Int(uint8_t* p_inputstr, uint32_t* p_intnum)
 }
 
 
-/* ****************************************************************************/
 /* flash_if *******************************************************************/
 /**
   * @brief  Unlocks Flash for write access
-  * @param  None
-  * @retval None
   */
 void FLASH_If_Init(void)
 {
@@ -549,7 +568,7 @@ uint32_t FLASH_If_Erase(uint32_t start)
     FLASH_EraseInitTypeDef pEraseInit;
     HAL_StatusTypeDef status = HAL_OK;
 
-    /* Unlock the Flash to enable the flash control register access *************/
+    /* Unlock the Flash to enable the flash control register access */
     HAL_FLASH_Unlock();
 
     /* Get the sector where start the user flash area */
@@ -562,12 +581,12 @@ uint32_t FLASH_If_Erase(uint32_t start)
     status = HAL_FLASHEx_Erase(&pEraseInit, &PageError);
 
     /* Lock the Flash to disable the flash control register access (recommended
-       to protect the FLASH memory against possible unwanted operation) *********/
+       to protect the FLASH memory against possible unwanted operation) */
     HAL_FLASH_Lock();
 
     if(status != HAL_OK)
     {
-      /* Error occurred while page erase */
+        /* Error occurred while page erase */
         return FLASHIF_ERASEKO;
     }
 
@@ -588,19 +607,19 @@ uint32_t FLASH_If_Write(uint32_t destination, uint32_t* p_source, uint32_t lengt
 {
     uint32_t i = 0;
 
-    /* Unlock the Flash to enable the flash control register access *************/
+    /* Unlock the Flash to enable the flash control register access */
     HAL_FLASH_Unlock();
 
     for(i = 0; (i < length) && (destination <= (USER_FLASH_END_ADDRESS - 4)); i++)
     {
-      /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
-         be done by word */
+        /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
+           be done by word */
         if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, destination, *(uint32_t*)(p_source + i)) == HAL_OK)
         {
-         /* Check the written value */
+            /* Check the written value */
             if(*(uint32_t*)destination != *(uint32_t*)(p_source + i))
             {
-              /* Flash content doesn't match SRAM content */
+                /* Flash content doesn't match SRAM content */
                 return(FLASHIF_WRITINGCTRL_ERROR);
             }
             /* Increment FLASH destination address */
@@ -608,13 +627,13 @@ uint32_t FLASH_If_Write(uint32_t destination, uint32_t* p_source, uint32_t lengt
         }
         else
         {
-          /* Error occurred while writing data in Flash memory */
+            /* Error occurred while writing data in Flash memory */
             return (FLASHIF_WRITING_ERROR);
         }
     }
 
     /* Lock the Flash to disable the flash control register access (recommended
-       to protect the FLASH memory against possible unwanted operation) *********/
+       to protect the FLASH memory against possible unwanted operation) */
     HAL_FLASH_Lock();
 
     return (FLASHIF_OK);
@@ -622,7 +641,6 @@ uint32_t FLASH_If_Write(uint32_t destination, uint32_t* p_source, uint32_t lengt
 
 /**
   * @brief  Returns the write protection status of application flash area.
-  * @param  None
   * @retval If a sector in application area is write-protected returned value is a combinaison
             of the possible values : FLASHIF_PROTECTION_WRPENABLED, FLASHIF_PROTECTION_PCROPENABLED, ...
   *         If no sector is write-protected FLASHIF_PROTECTION_NONE is returned.
@@ -632,28 +650,28 @@ uint32_t FLASH_If_GetWriteProtectionStatus(void)
     uint32_t ProtectedPAGE = FLASHIF_PROTECTION_NONE;
     FLASH_OBProgramInitTypeDef OptionsBytesStruct;
 
-    /* Unlock the Flash to enable the flash control register access *************/
+    /* Unlock the Flash to enable the flash control register access */
     HAL_FLASH_Unlock();
 
-    /* Check if there are write protected sectors inside the user flash area ****/
+    /* Check if there are write protected sectors inside the user flash area */
     HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
 
     /* Lock the Flash to disable the flash control register access (recommended
-       to protect the FLASH memory against possible unwanted operation) *********/
+       to protect the FLASH memory against possible unwanted operation) */
     HAL_FLASH_Lock();
 
-    /* Get pages already write protected ****************************************/
+    /* Get pages already write protected */
     ProtectedPAGE = ~(OptionsBytesStruct.WRPPage) & FLASH_PAGE_TO_BE_PROTECTED;
 
-    /* Check if desired pages are already write protected ***********************/
+    /* Check if desired pages are already write protected */
     if(ProtectedPAGE != 0)
     {
-      /* Some sectors inside the user flash area are write protected */
+        /* Some sectors inside the user flash area are write protected */
         return FLASHIF_PROTECTION_WRPENABLED;
     }
     else
     {
-      /* No write protected sectors inside the user flash area */
+        /* No write protected sectors inside the user flash area */
         return FLASHIF_PROTECTION_NONE;
     }
 }
@@ -670,7 +688,7 @@ uint32_t FLASH_If_WriteProtectionConfig(uint32_t protectionstate)
     HAL_StatusTypeDef result = HAL_OK;
 
 
-    /* Get pages write protection status ****************************************/
+    /* Get pages write protection status */
     HAL_FLASHEx_OBGetConfig(&config_old);
 
     /* The parameter says whether we turn the protection on or off */
@@ -682,16 +700,16 @@ uint32_t FLASH_If_WriteProtectionConfig(uint32_t protectionstate)
     /* No read protection, keep BOR and reset settings */
     config_new.RDPLevel = OB_RDP_LEVEL_0;
     config_new.USERConfig = config_old.USERConfig;
-    /* Get pages already write protected ****************************************/
+    /* Get pages already write protected */
     ProtectedPAGE = config_old.WRPPage | FLASH_PAGE_TO_BE_PROTECTED;
 
-    /* Unlock the Flash to enable the flash control register access *************/
+    /* Unlock the Flash to enable the flash control register access */
     HAL_FLASH_Unlock();
 
-    /* Unlock the Options Bytes *************************************************/
+    /* Unlock the Options Bytes */
     HAL_FLASH_OB_Unlock();
 
-    /* Erase all the option Bytes ***********************************************/
+    /* Erase all the option Bytes */
     result = HAL_FLASHEx_OBErase();
 
     if(result == HAL_OK)
@@ -704,19 +722,13 @@ uint32_t FLASH_If_WriteProtectionConfig(uint32_t protectionstate)
 }
 
 
-/* ****************************************************************************/
 /* ymodem *********************************************************************/
-#define CRC16_F       /* activate the CRC16 integrity */
-/* @note ATTENTION - please keep this variable 32bit alligned */
+/* activate the CRC16 integrity */
+#define CRC16_F
+/* ATTENTION - please keep this variable 32bit alligned */
 uint8_t aPacketData[PACKET_1K_SIZE + PACKET_DATA_INDEX + PACKET_TRAILER_SIZE];
 uint8_t aFileName[FILE_NAME_LENGTH];
 
-static void PrepareIntialPacket(uint8_t* p_data, const uint8_t* p_file_name, uint32_t length);
-static void PreparePacket(uint8_t* p_source, uint8_t* p_packet, uint8_t pkt_nr, uint32_t size_blk);
-static HAL_StatusTypeDef ReceivePacket(uint8_t* p_data, uint32_t* p_length, uint32_t timeout);
-uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte);
-uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size);
-uint8_t CalcChecksum(const uint8_t* p_data, uint32_t size);
 
 /**
   * @brief  Receive a packet from sender
@@ -729,15 +741,14 @@ uint8_t CalcChecksum(const uint8_t* p_data, uint32_t size);
   * @retval HAL_OK: normally return
   *         HAL_BUSY: abort by user
   */
-uint8_t byteOne;
 static HAL_StatusTypeDef ReceivePacket(uint8_t* p_data, uint32_t* p_length, uint32_t timeout)
 {
     uint32_t crc;
     uint32_t packet_size = 0;
     HAL_StatusTypeDef status;
+    uint8_t byteOne;
 
     *p_length = 0;
-    // todo
     status = Interface_Receive(&byteOne, 1, timeout);
 
     if(status == HAL_OK)
@@ -753,7 +764,6 @@ static HAL_StatusTypeDef ReceivePacket(uint8_t* p_data, uint32_t* p_length, uint
             case EOT:
                 break;
             case CA:
-                // todo
                 if((Interface_Receive(&byteOne, 1, timeout) == HAL_OK) && (byteOne == CA))
                 {
                     packet_size = 2;
@@ -812,13 +822,11 @@ static HAL_StatusTypeDef ReceivePacket(uint8_t* p_data, uint32_t* p_length, uint
   * @param  p_data:  output buffer
   * @param  p_file_name: name of the file to be sent
   * @param  length: length of the file to be sent in bytes
-  * @retval None
   */
-uint8_t astring[10];
 static void PrepareIntialPacket(uint8_t* p_data, const uint8_t* p_file_name, uint32_t length)
 {
     uint32_t i, j = 0;
-    // uint8_t astring[10];
+    uint8_t astring[10];
 
     /* first 3 bytes are constant */
     p_data[PACKET_START_INDEX] = SOH;
@@ -854,7 +862,6 @@ static void PrepareIntialPacket(uint8_t* p_data, const uint8_t* p_file_name, uin
   * @param  p_packet: pointer to the output buffer
   * @param  pkt_nr: number of the packet
   * @param  size_blk: length of the block to be sent in bytes
-  * @retval None
   */
 static void PreparePacket(uint8_t* p_source, uint8_t* p_packet, uint8_t pkt_nr, uint32_t size_blk)
 {
@@ -894,7 +901,6 @@ static void PreparePacket(uint8_t* p_source, uint8_t* p_packet, uint8_t pkt_nr,
   * @brief  Update CRC16 for input byte
   * @param  crc_in input value
   * @param  input byte
-  * @retval None
   */
 uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte)
 {
@@ -920,7 +926,6 @@ uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte)
   * @brief  Cal CRC16 for YModem Packet
   * @param  data
   * @param  length
-  * @retval None
   */
 uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size)
 {
@@ -1008,7 +1013,7 @@ COM_StatusTypeDef Ymodem_Receive(uint32_t* p_size)
                                     /* File name packet */
                                     if(aPacketData[PACKET_DATA_INDEX] != 0)
                                     {
-                                      /* File name extraction */
+                                        /* File name extraction */
                                         i = 0;
                                         file_ptr = aPacketData + PACKET_DATA_INDEX;
                                         while((*file_ptr != 0) && (i < FILE_NAME_LENGTH))
@@ -1031,13 +1036,13 @@ COM_StatusTypeDef Ymodem_Receive(uint32_t* p_size)
                                         /* Image size is greater than Flash size */
                                         if(*p_size > (USER_FLASH_SIZE + 1))
                                         {
-                                          /* End session */
+                                            /* End session */
                                             tmp = CA;
                                             Interface_Transmit(&tmp, 1, NAK_TIMEOUT);
                                             Interface_Transmit(&tmp, 1, NAK_TIMEOUT);
                                             result = COM_LIMIT;
                                         }
-                                        /* erase user application area */
+                                        /* Erase user application area */
                                         FLASH_If_Erase(APPLICATION_ADDRESS);
                                         *p_size = filesize;
 
@@ -1065,7 +1070,7 @@ COM_StatusTypeDef Ymodem_Receive(uint32_t* p_size)
                                     }
                                     else /* An error occurred while writing to Flash memory */
                                     {
-                                      /* End session */
+                                        /* End session */
                                         Interface_PutByte(CA);
                                         Interface_PutByte(CA);
                                         result = COM_DATA;
@@ -1089,13 +1094,14 @@ COM_StatusTypeDef Ymodem_Receive(uint32_t* p_size)
                     }
                     if(errors > MAX_ERRORS)
                     {
-                      /* Abort communication */
+                        /* Abort communication */
                         Interface_PutByte(CA);
                         Interface_PutByte(CA);
                     }
                     else
                     {
-                        Interface_PutByte(CRC16); /* Ask for a packet */
+                        /* Ask for a packet */
+                        Interface_PutByte(CRC16);
                     }
                     break;
             }
@@ -1242,7 +1248,7 @@ COM_StatusTypeDef Ymodem_Transmit(uint8_t* p_buf, const uint8_t* p_file_name, ui
             /* Resend packet if NAK  for a count of 10 else end of communication */
             if(errors >= MAX_ERRORS)
             {
-                result = COM_ERROR;//
+                result = COM_ERROR;
             }
         }
     }
@@ -1320,7 +1326,7 @@ COM_StatusTypeDef Ymodem_Transmit(uint8_t* p_buf, const uint8_t* p_file_name, ui
         }
     }
 
-    return result; /* file transmitted successfully */
+    return result; /* File transmitted successfully */
 }
 
 

+ 32 - 23
IAP_YMODEM_ATY.h

@@ -23,6 +23,8 @@
 *           -# Preliminary version, first Release
 *       - 1_01_250701 > ATY
 *           -# real fix to lib type
+*       - 1_01_250704 > ATY
+*           -# finish uart and ucdc IAP whole test
 ********************************************************************************
 */
 
@@ -33,8 +35,9 @@
 #include "HW_RESET_ATY.h"
 
 /******************************* For user *************************************/
+// do not forget change USER_VECT_TAB_ADDRESS VECT_TAB_OFFSET at system_stm32f1xx.c
 #ifndef IAP_YMODEM_ATY_DBG
-#define IAP_YMODEM_ATY_DBG
+// #define IAP_YMODEM_ATY_DBG
 #endif
 
 #ifndef IAP_YMODEM_ATY_USB
@@ -48,19 +51,31 @@ extern UART_HandleTypeDef huart3;
 #include "usb_device.h"
 #include "usbd_cdc_if.h"
 extern USBD_HandleTypeDef hUsbDeviceFS;
-extern uint32_t PUT_IN_CDC_Receive_FS;
-extern uint8_t ucdcRcvOverFlag;
-extern uint32_t ucdcRcvLastTime;
-extern uint8_t ucdcRcvBuffer[APP_RX_DATA_SIZE];
-
-extern uint32_t rcvDbg;
-extern uint8_t temp_str[12];
+void PUT_IN_CDC_Receive_FS(uint8_t* Buf, uint32_t* Len);
 
 void Main_Menu(void);
 void Main_Cycle(void);
 /******************************************************************************/
 
 
+/* interface realize **********************************************************/
+/**
+  * @brief  Comm status structures definition
+  */
+typedef enum
+{
+    COM_OK = 0x00,
+    COM_ERROR = 0x01,
+    COM_ABORT = 0x02,
+    COM_TIMEOUT = 0x03,
+    COM_DATA = 0x04,
+    COM_LIMIT = 0x05
+} COM_StatusTypeDef;
+
+HAL_StatusTypeDef Interface_PutString(uint8_t* p_string);
+HAL_StatusTypeDef Interface_PutByte(uint8_t bytes);
+COM_StatusTypeDef Interface_Download(void);
+void Interface_Upload(void);
 
 
 /* common *********************************************************************/
@@ -93,7 +108,7 @@ enum
     FLASHIF_PROTECTION_ERRROR
 };
 
-/* protection type */
+/* Protection type */
 enum{
     FLASHIF_PROTECTION_NONE = 0,
     FLASHIF_PROTECTION_PCROPENABLED = 0x1,
@@ -101,7 +116,7 @@ enum{
     FLASHIF_PROTECTION_RDPENABLED = 0x4,
 };
 
-/* protection update */
+/* Protection update */
 enum {
     FLASHIF_WRP_ENABLE,
     FLASHIF_WRP_DISABLE
@@ -188,19 +203,6 @@ uint32_t FLASH_If_WriteProtectionConfig(uint32_t modifier);
 
 
 /* ymodem *********************************************************************/
-/**
-  * @brief  Comm status structures definition
-  */
-typedef enum
-{
-    COM_OK = 0x00,
-    COM_ERROR = 0x01,
-    COM_ABORT = 0x02,
-    COM_TIMEOUT = 0x03,
-    COM_DATA = 0x04,
-    COM_LIMIT = 0x05
-} COM_StatusTypeDef;
-
 /* Packet structure defines */
 #define PACKET_HEADER_SIZE          ((uint32_t)3)
 #define PACKET_DATA_INDEX           ((uint32_t)4)
@@ -242,6 +244,13 @@ typedef enum
 #define DOWNLOAD_TIMEOUT            ((uint32_t)1000) /* One second retry delay */
 #define MAX_ERRORS                  ((uint32_t)5)
 
+static void PrepareIntialPacket(uint8_t* p_data, const uint8_t* p_file_name, uint32_t length);
+static void PreparePacket(uint8_t* p_source, uint8_t* p_packet, uint8_t pkt_nr, uint32_t size_blk);
+static HAL_StatusTypeDef ReceivePacket(uint8_t* p_data, uint32_t* p_length, uint32_t timeout);
+uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte);
+uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size);
+uint8_t CalcChecksum(const uint8_t* p_data, uint32_t size);
+
 COM_StatusTypeDef Ymodem_Receive(uint32_t* p_size);
 COM_StatusTypeDef Ymodem_Transmit(uint8_t* p_buf, const uint8_t* p_file_name, uint32_t file_size);