| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334 |
- /**
- * @file IAP_YMODEM_ATY.c
- *
- * @param Project DEVICE_GENERAL_ATY_LIB
- *
- * @author ATY
- *
- * @copyright
- * - Copyright 2017 - 2023 MZ-ATY
- * - This code follows:
- * - MZ-ATY Various Contents Joint Statement -
- * <a href="https://mengze.top/MZ-ATY_VCJS">
- * https://mengze.top/MZ-ATY_VCJS</a>
- * - CC 4.0 BY-NC-SA -
- * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
- * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
- * - Your use will be deemed to have accepted the terms of this statement.
- *
- * @brief Familiar functions of IAP with YMODEM for all embedded device
- *
- * @version
- * - 1_01_240911 > ATY
- * -# Preliminary version, first Release
- ********************************************************************************
- */
- #ifndef __IAP_YMODEM_ATY_C
- #define __IAP_YMODEM_ATY_C
- #include "IAP_YMODEM_ATY.h"
- /******************************* For user *************************************/
- uint8_t EmptyBuf[1] = {0};
- USBD_CDC_HandleTypeDef* hcdc;
- uint32_t PUT_IN_CDC_Receive_FS = 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();
- HAL_UART_DeInit(&Interface_UART);
- __HAL_RCC_USB_CLK_DISABLE();
- HAL_RCC_DeInit();
- HAL_DeInit();
- // Disable and reset SysTick timer
- SysTick->CTRL = 0;
- SysTick->LOAD = 0;
- SysTick->VAL = 0;
- // Optional: Disable and clear all NVIC interrupts
- for(int i = 0; i < 8; i++) {
- NVIC->ICER[i] = 0xFFFFFFFF; // Disable all interrupts
- NVIC->ICPR[i] = 0xFFFFFFFF; // Clear pending status
- }
- // Optional: Reset interrupt priority registers
- // for(int i = 0; i < 48; i++) {
- // NVIC->IP[i] = 0; // Set all priorities to default
- // }
- // Optional: Set vector table offset to App's address
- SCB->VTOR = APPLICATION_ADDRESS & 0x1FFFFF80;
- }
- void JumpToAppWithReset(void)
- {
- All_Reset();
- JumpToApp(APPLICATION_ADDRESS);
- }
- void Interface_Clean(void)
- {
- #ifndef IAP_YMODEM_ATY_USB
- __HAL_UART_FLUSH_DRREGISTER(&Interface_UART);
- #else
- PUT_IN_CDC_Receive_FS = 0;
- ucdcRcvOverFlag = 1;
- #endif
- }
- 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
- hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
- if(hcdc->RxLength > APP_RX_DATA_SIZE)
- return HAL_ERROR;
- timeoutCount = 0;
- timeoutFinal = timeout;
- while(PUT_IN_CDC_Receive_FS == 0){
- timeoutCount++;
- if(timeoutCount > timeoutFinal){
- timeoutCount = 0;
- USBD_CDC_ReceivePacket(&hUsbDeviceFS);
- return HAL_TIMEOUT;
- }
- HAL_Delay(1);
- }
- if(ucdcRcvOverFlag == 0)
- {
- while(HAL_GetTick() - ucdcRcvLastTime < 200)
- {
- }
- 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;
- memcpy(bytes, ucdcRcvBuffer, copySize);
- PUT_IN_CDC_Receive_FS -= copySize;
- memcpy(ucdcRcvBuffer, ucdcRcvBuffer + copySize, PUT_IN_CDC_Receive_FS);
- // Interface_Clean();
- }
- return HAL_OK;
- #endif
- }
- HAL_StatusTypeDef Interface_Transmit(uint8_t* bytes, uint16_t size, uint32_t timeout)
- {
- #ifndef IAP_YMODEM_ATY_USB
- /* May be timeouted... */
- if(Interface_UART.gState == HAL_UART_STATE_TIMEOUT)
- {
- Interface_UART.gState = HAL_UART_STATE_READY;
- }
- return HAL_UART_Transmit(&Interface_UART, bytes, size, timeout);
- #else
- Interface_Clean();
- ucdcRcvOverFlag = 1;
- timeoutCount = 0;
- timeoutFinal = timeout;
- while(CDC_Transmit_FS(bytes, size) != HAL_OK){
- timeoutCount++;
- if(timeoutCount > timeoutFinal)
- return HAL_TIMEOUT;
- HAL_Delay(1);
- }
- return HAL_OK;
- #endif
- }
- /**
- * @brief Print a string on the HyperTerminal
- * @param p_string: The string to be printed
- * @retval None
- */
- HAL_StatusTypeDef Interface_PutString(uint8_t* p_string)
- {
- uint16_t length = 0;
- while(p_string[length] != '\0')
- {
- length++;
- }
- return Interface_Transmit(p_string, length, TX_TIMEOUT);
- }
- /**
- * @brief Transmit a byte to the HyperTerminal
- * @param param The byte to be sent
- * @retval send state
- */
- 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
- */
- COM_StatusTypeDef Interface_Download(void)
- {
- uint8_t number[11] = {0};
- uint32_t size = 0;
- COM_StatusTypeDef result;
- Interface_PutString("\r\nWaiting for the file to be sent ... (press 'a' to abort)\r\n");
- result = Ymodem_Receive(&size);
- if(result == COM_OK)
- {
- Interface_PutString("\r\n\r\nProgramming Completed Successfully!\n\r--------------------------------\r\n Name: ");
- Interface_PutString(aFileName);
- Int2Str(number, size);
- Interface_PutString("\r\nSize: ");
- Interface_PutString(number);
- Interface_PutString(" Bytes\r\n");
- Interface_PutString("-------------------\r\n");
- }
- else if(result == COM_LIMIT)
- {
- Interface_PutString("\r\nThe image size is higher than the allowed space memory!\n\r");
- }
- else if(result == COM_DATA)
- {
- Interface_PutString("\r\nVerification failed!\n\r");
- }
- else if(result == COM_ABORT)
- {
- Interface_PutString("\r\nAborted by user.\n\r");
- }
- else
- {
- Interface_PutString("\r\nFailed to receive the file!\r\n");
- }
- return result;
- }
- /**
- * @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");
- while(1)
- {
- Interface_Receive(&status, 1, RX_TIMEOUT);
- if(status == CRC16)
- {
- /* Transmit the flash image through ymodem protocol */
- status = Ymodem_Transmit((uint8_t*)APPLICATION_ADDRESS, (const uint8_t*)"UploadedFlashImage.bin", USER_FLASH_SIZE);
- if(status != 0)
- {
- if(status == COM_ABORT)
- Interface_PutString("\r\nAborted by user.\n\r");
- else
- Interface_PutString("\r\nError occurred while transmitting file\r\n");
- }
- else
- {
- Interface_PutString("\r\nFile uploaded successfully\r\n");
- }
- break;
- }
- else if(status == ABORT1 || status == ABORT2)
- {
- Interface_PutString("\r\nAborted by user.\n\r");
- break;
- }
- }
- }
- 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)
- {
- uint32_t i, divider = 1000000000, pos = 0, status = 0;
- for(i = 0; i < 10; i++)
- {
- p_str[pos++] = (intnum / divider) + 48;
- intnum = intnum % divider;
- divider /= 10;
- if((p_str[pos - 1] == '0') & (status == 0))
- {
- pos = 0;
- }
- else
- {
- status++;
- }
- }
- }
- /**
- * @brief Convert a string to an integer
- * @param p_inputstr: The string to be converted
- * @param p_intnum: The integer value
- * @retval 1: Correct
- * 0: Error
- */
- uint32_t Str2Int(uint8_t* p_inputstr, uint32_t* p_intnum)
- {
- uint32_t i = 0, res = 0;
- uint32_t val = 0;
- if((p_inputstr[0] == '0') && ((p_inputstr[1] == 'x') || (p_inputstr[1] == 'X')))
- {
- i = 2;
- while((i < 11) && (p_inputstr[i] != '\0'))
- {
- if(ISVALIDHEX(p_inputstr[i]))
- {
- val = (val << 4) + CONVERTHEX(p_inputstr[i]);
- }
- else
- {
- /* Return 0, Invalid input */
- res = 0;
- break;
- }
- i++;
- }
- /* valid result */
- if(p_inputstr[i] == '\0')
- {
- *p_intnum = val;
- res = 1;
- }
- }
- else /* max 10-digit decimal input */
- {
- while((i < 11) && (res != 1))
- {
- if(p_inputstr[i] == '\0')
- {
- *p_intnum = val;
- /* return 1 */
- res = 1;
- }
- else if(((p_inputstr[i] == 'k') || (p_inputstr[i] == 'K')) && (i > 0))
- {
- val = val << 10;
- *p_intnum = val;
- res = 1;
- }
- else if(((p_inputstr[i] == 'm') || (p_inputstr[i] == 'M')) && (i > 0))
- {
- val = val << 20;
- *p_intnum = val;
- res = 1;
- }
- else if(ISVALIDDEC(p_inputstr[i]))
- {
- val = val * 10 + CONVERTDEC(p_inputstr[i]);
- }
- else
- {
- /* return 0, Invalid input */
- res = 0;
- break;
- }
- i++;
- }
- }
- return res;
- }
- /* ****************************************************************************/
- /* flash_if *******************************************************************/
- /**
- * @brief Unlocks Flash for write access
- * @param None
- * @retval None
- */
- void FLASH_If_Init(void)
- {
- /* Unlock the Program memory */
- HAL_FLASH_Unlock();
- /* Clear all FLASH flags */
- __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
- /* Unlock the Program memory */
- HAL_FLASH_Lock();
- }
- /**
- * @brief This function does an erase of all user flash area
- * @param start: start of user flash area
- * @retval FLASHIF_OK : user flash area successfully erased
- * FLASHIF_ERASEKO : error occurred
- */
- uint32_t FLASH_If_Erase(uint32_t start)
- {
- uint32_t NbrOfPages = 0;
- uint32_t PageError = 0;
- FLASH_EraseInitTypeDef pEraseInit;
- HAL_StatusTypeDef status = HAL_OK;
- /* Unlock the Flash to enable the flash control register access *************/
- HAL_FLASH_Unlock();
- /* Get the sector where start the user flash area */
- NbrOfPages = (USER_FLASH_END_ADDRESS - start) / FLASH_PAGE_SIZE;
- pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
- pEraseInit.PageAddress = start;
- pEraseInit.Banks = FLASH_BANK_1;
- pEraseInit.NbPages = NbrOfPages;
- 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) *********/
- HAL_FLASH_Lock();
- if(status != HAL_OK)
- {
- /* Error occurred while page erase */
- return FLASHIF_ERASEKO;
- }
- return FLASHIF_OK;
- }
- /**
- * @brief This function writes a data buffer in flash (data are 32-bit aligned).
- * @note After writing data buffer, the flash content is checked.
- * @param destination: start address for target location
- * @param p_source: pointer on buffer with data to write
- * @param length: length of data buffer (unit is 32-bit word)
- * @retval uint32_t 0: Data successfully written to Flash memory
- * 1: Error occurred while writing data in Flash memory
- * 2: Written Data in flash memory is different from expected one
- */
- uint32_t FLASH_If_Write(uint32_t destination, uint32_t* p_source, uint32_t length)
- {
- uint32_t i = 0;
- /* 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 */
- if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, destination, *(uint32_t*)(p_source + i)) == HAL_OK)
- {
- /* Check the written value */
- if(*(uint32_t*)destination != *(uint32_t*)(p_source + i))
- {
- /* Flash content doesn't match SRAM content */
- return(FLASHIF_WRITINGCTRL_ERROR);
- }
- /* Increment FLASH destination address */
- destination += 4;
- }
- else
- {
- /* 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) *********/
- HAL_FLASH_Lock();
- return (FLASHIF_OK);
- }
- /**
- * @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.
- */
- 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 *************/
- HAL_FLASH_Unlock();
- /* 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) *********/
- HAL_FLASH_Lock();
- /* Get pages already write protected ****************************************/
- ProtectedPAGE = ~(OptionsBytesStruct.WRPPage) & FLASH_PAGE_TO_BE_PROTECTED;
- /* Check if desired pages are already write protected ***********************/
- if(ProtectedPAGE != 0)
- {
- /* Some sectors inside the user flash area are write protected */
- return FLASHIF_PROTECTION_WRPENABLED;
- }
- else
- {
- /* No write protected sectors inside the user flash area */
- return FLASHIF_PROTECTION_NONE;
- }
- }
- /**
- * @brief Configure the write protection status of user flash area.
- * @param protectionstate : FLASHIF_WRP_DISABLE or FLASHIF_WRP_ENABLE the protection
- * @retval uint32_t FLASHIF_OK if change is applied.
- */
- uint32_t FLASH_If_WriteProtectionConfig(uint32_t protectionstate)
- {
- uint32_t ProtectedPAGE = 0x0;
- FLASH_OBProgramInitTypeDef config_new, config_old;
- HAL_StatusTypeDef result = HAL_OK;
- /* Get pages write protection status ****************************************/
- HAL_FLASHEx_OBGetConfig(&config_old);
- /* The parameter says whether we turn the protection on or off */
- config_new.WRPState = (protectionstate == FLASHIF_WRP_ENABLE ? OB_WRPSTATE_ENABLE : OB_WRPSTATE_DISABLE);
- /* We want to modify only the Write protection */
- config_new.OptionType = OPTIONBYTE_WRP;
- /* 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 ****************************************/
- ProtectedPAGE = config_old.WRPPage | FLASH_PAGE_TO_BE_PROTECTED;
- /* Unlock the Flash to enable the flash control register access *************/
- HAL_FLASH_Unlock();
- /* Unlock the Options Bytes *************************************************/
- HAL_FLASH_OB_Unlock();
- /* Erase all the option Bytes ***********************************************/
- result = HAL_FLASHEx_OBErase();
- if(result == HAL_OK)
- {
- config_new.WRPPage = ProtectedPAGE;
- result = HAL_FLASHEx_OBProgram(&config_new);
- }
- return (result == HAL_OK ? FLASHIF_OK : FLASHIF_PROTECTION_ERRROR);
- }
- /* ****************************************************************************/
- /* ymodem *********************************************************************/
- #define CRC16_F /* activate the CRC16 integrity */
- /* @note 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
- * @param data
- * @param length
- * 0: end of transmission
- * 2: abort by sender
- * >0: packet length
- * @param timeout
- * @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;
- *p_length = 0;
- // todo
- status = Interface_Receive(&byteOne, 1, timeout);
- if(status == HAL_OK)
- {
- switch(byteOne)
- {
- case SOH:
- packet_size = PACKET_SIZE;
- break;
- case STX:
- packet_size = PACKET_1K_SIZE;
- break;
- case EOT:
- break;
- case CA:
- // todo
- if((Interface_Receive(&byteOne, 1, timeout) == HAL_OK) && (byteOne == CA))
- {
- packet_size = 2;
- }
- else
- {
- status = HAL_ERROR;
- }
- break;
- case ABORT1:
- case ABORT2:
- status = HAL_BUSY;
- break;
- default:
- status = HAL_ERROR;
- break;
- }
- *p_data = byteOne;
- if(packet_size >= PACKET_SIZE)
- {
- status = Interface_Receive(&p_data[PACKET_NUMBER_INDEX], packet_size + PACKET_OVERHEAD_SIZE, timeout);
- /* Simple packet sanity check */
- if(status == HAL_OK)
- {
- if(p_data[PACKET_NUMBER_INDEX] != ((p_data[PACKET_CNUMBER_INDEX]) ^ NEGATIVE_BYTE))
- {
- packet_size = 0;
- status = HAL_ERROR;
- }
- else
- {
- /* Check packet CRC */
- crc = p_data[packet_size + PACKET_DATA_INDEX] << 8;
- crc += p_data[packet_size + PACKET_DATA_INDEX + 1];
- if(Cal_CRC16(&p_data[PACKET_DATA_INDEX], packet_size) != crc)
- {
- packet_size = 0;
- status = HAL_ERROR;
- }
- }
- }
- else
- {
- packet_size = 0;
- }
- }
- }
- *p_length = packet_size;
- return status;
- }
- /**
- * @brief Prepare the first block
- * @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];
- /* first 3 bytes are constant */
- p_data[PACKET_START_INDEX] = SOH;
- p_data[PACKET_NUMBER_INDEX] = 0x00;
- p_data[PACKET_CNUMBER_INDEX] = 0xff;
- /* Filename written */
- for(i = 0; (p_file_name[i] != '\0') && (i < FILE_NAME_LENGTH); i++)
- {
- p_data[i + PACKET_DATA_INDEX] = p_file_name[i];
- }
- p_data[i + PACKET_DATA_INDEX] = 0x00;
- /* file size written */
- Int2Str(astring, length);
- i = i + PACKET_DATA_INDEX + 1;
- while(astring[j] != '\0')
- {
- p_data[i++] = astring[j++];
- }
- /* padding with zeros */
- for(j = i; j < PACKET_SIZE + PACKET_DATA_INDEX; j++)
- {
- p_data[j] = 0;
- }
- }
- /**
- * @brief Prepare the data packet
- * @param p_source: pointer to the data to be sent
- * @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)
- {
- uint8_t* p_record;
- uint32_t i, size, packet_size;
- /* Make first three packet */
- packet_size = size_blk >= PACKET_1K_SIZE ? PACKET_1K_SIZE : PACKET_SIZE;
- size = size_blk < packet_size ? size_blk : packet_size;
- if(packet_size == PACKET_1K_SIZE)
- {
- p_packet[PACKET_START_INDEX] = STX;
- }
- else
- {
- p_packet[PACKET_START_INDEX] = SOH;
- }
- p_packet[PACKET_NUMBER_INDEX] = pkt_nr;
- p_packet[PACKET_CNUMBER_INDEX] = (~pkt_nr);
- p_record = p_source;
- /* Filename packet has valid data */
- for(i = PACKET_DATA_INDEX; i < size + PACKET_DATA_INDEX; i++)
- {
- p_packet[i] = *p_record++;
- }
- if(size <= packet_size)
- {
- for(i = size + PACKET_DATA_INDEX; i < packet_size + PACKET_DATA_INDEX; i++)
- {
- p_packet[i] = 0x1A; /* EOF (0x1A) or 0x00 */
- }
- }
- }
- /**
- * @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)
- {
- uint32_t crc = crc_in;
- uint32_t in = byte | 0x100;
- do
- {
- crc <<= 1;
- in <<= 1;
- if(in & 0x100)
- ++crc;
- if(crc & 0x10000)
- crc ^= 0x1021;
- }
- while(!(in & 0x10000));
- return crc & 0xffffu;
- }
- /**
- * @brief Cal CRC16 for YModem Packet
- * @param data
- * @param length
- * @retval None
- */
- uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size)
- {
- uint32_t crc = 0;
- const uint8_t* dataEnd = p_data + size;
- while(p_data < dataEnd)
- crc = UpdateCRC16(crc, *p_data++);
- crc = UpdateCRC16(crc, 0);
- crc = UpdateCRC16(crc, 0);
- return crc & 0xffffu;
- }
- /**
- * @brief Calculate Check sum for YModem Packet
- * @param p_data Pointer to input data
- * @param size length of input data
- * @retval uint8_t checksum value
- */
- uint8_t CalcChecksum(const uint8_t* p_data, uint32_t size)
- {
- uint32_t sum = 0;
- const uint8_t* p_data_end = p_data + size;
- while(p_data < p_data_end)
- {
- sum += *p_data++;
- }
- return (sum & 0xffu);
- }
- /* Public functions ---------------------------------------------------------*/
- /**
- * @brief Receive a file using the ymodem protocol with CRC16.
- * @param p_size The size of the file.
- * @retval COM_StatusTypeDef result of reception/programming
- */
- COM_StatusTypeDef Ymodem_Receive(uint32_t* p_size)
- {
- uint32_t i, packet_length, session_done = 0, file_done, errors = 0, session_begin = 0;
- uint32_t flashdestination, ramsource, filesize;
- uint8_t* file_ptr;
- uint8_t file_size[FILE_SIZE_LENGTH], tmp, packets_received;
- COM_StatusTypeDef result = COM_OK;
- /* Initialize flashdestination variable */
- flashdestination = APPLICATION_ADDRESS;
- while((session_done == 0) && (result == COM_OK))
- {
- packets_received = 0;
- file_done = 0;
- while((file_done == 0) && (result == COM_OK))
- {
- switch(ReceivePacket(aPacketData, &packet_length, DOWNLOAD_TIMEOUT))
- {
- case HAL_OK:
- errors = 0;
- switch(packet_length)
- {
- case 2:
- /* Abort by sender */
- Interface_PutByte(ACK);
- result = COM_ABORT;
- break;
- case 0:
- /* End of transmission */
- Interface_PutByte(ACK);
- file_done = 1;
- break;
- default:
- /* Normal packet */
- if(aPacketData[PACKET_NUMBER_INDEX] != packets_received)
- {
- Interface_PutByte(NAK);
- }
- else
- {
- if(packets_received == 0)
- {
- /* File name packet */
- if(aPacketData[PACKET_DATA_INDEX] != 0)
- {
- /* File name extraction */
- i = 0;
- file_ptr = aPacketData + PACKET_DATA_INDEX;
- while((*file_ptr != 0) && (i < FILE_NAME_LENGTH))
- {
- aFileName[i++] = *file_ptr++;
- }
- /* File size extraction */
- aFileName[i++] = '\0';
- i = 0;
- file_ptr++;
- while((*file_ptr != ' ') && (i < FILE_SIZE_LENGTH))
- {
- file_size[i++] = *file_ptr++;
- }
- file_size[i++] = '\0';
- Str2Int(file_size, &filesize);
- /* Test the size of the image to be sent */
- /* Image size is greater than Flash size */
- if(*p_size > (USER_FLASH_SIZE + 1))
- {
- /* End session */
- tmp = CA;
- Interface_Transmit(&tmp, 1, NAK_TIMEOUT);
- Interface_Transmit(&tmp, 1, NAK_TIMEOUT);
- result = COM_LIMIT;
- }
- /* erase user application area */
- FLASH_If_Erase(APPLICATION_ADDRESS);
- *p_size = filesize;
- Interface_PutByte(ACK);
- Interface_PutByte(CRC16);
- }
- /* File header packet is empty, end session */
- else
- {
- Interface_PutByte(ACK);
- file_done = 1;
- session_done = 1;
- break;
- }
- }
- else /* Data packet */
- {
- ramsource = (uint32_t)&aPacketData[PACKET_DATA_INDEX];
- /* Write received data in Flash */
- if(FLASH_If_Write(flashdestination, (uint32_t*)ramsource, packet_length / 4) == FLASHIF_OK)
- {
- flashdestination += packet_length;
- Interface_PutByte(ACK);
- }
- else /* An error occurred while writing to Flash memory */
- {
- /* End session */
- Interface_PutByte(CA);
- Interface_PutByte(CA);
- result = COM_DATA;
- }
- }
- packets_received++;
- session_begin = 1;
- }
- break;
- }
- break;
- case HAL_BUSY: /* Abort actually */
- Interface_PutByte(CA);
- Interface_PutByte(CA);
- result = COM_ABORT;
- break;
- default:
- if(session_begin > 0)
- {
- errors++;
- }
- if(errors > MAX_ERRORS)
- {
- /* Abort communication */
- Interface_PutByte(CA);
- Interface_PutByte(CA);
- }
- else
- {
- Interface_PutByte(CRC16); /* Ask for a packet */
- }
- break;
- }
- }
- }
- return result;
- }
- /**
- * @brief Transmit a file using the ymodem protocol
- * @param p_buf: Address of the first byte
- * @param p_file_name: Name of the file sent
- * @param file_size: Size of the transmission
- * @retval COM_StatusTypeDef result of the communication
- */
- COM_StatusTypeDef Ymodem_Transmit(uint8_t* p_buf, const uint8_t* p_file_name, uint32_t file_size)
- {
- uint32_t errors = 0, ack_recpt = 0, size = 0, pkt_size;
- uint8_t* p_buf_int;
- COM_StatusTypeDef result = COM_OK;
- uint32_t blk_number = 1;
- uint8_t a_rx_ctrl[2];
- uint8_t i;
- #ifdef CRC16_F
- uint32_t temp_crc;
- #else /* CRC16_F */
- uint8_t temp_chksum;
- #endif /* CRC16_F */
- /* Prepare first block - header */
- PrepareIntialPacket(aPacketData, p_file_name, file_size);
- while((!ack_recpt) && (result == COM_OK))
- {
- /* Send Packet */
- Interface_Transmit(&aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT);
- /* Send CRC or Check Sum based on CRC16_F */
- #ifdef CRC16_F
- temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
- Interface_PutByte(temp_crc >> 8);
- Interface_PutByte(temp_crc & 0xFF);
- #else /* CRC16_F */
- temp_chksum = CalcChecksum(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
- Interface_PutByte(temp_chksum);
- #endif /* CRC16_F */
- /* Wait for Ack and 'C' */
- if(Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
- {
- if(a_rx_ctrl[0] == ACK)
- {
- ack_recpt = 1;
- }
- else if(a_rx_ctrl[0] == CA)
- {
- if((Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA))
- {
- HAL_Delay(2);
- Interface_Clean();
- result = COM_ABORT;
- }
- }
- else if(a_rx_ctrl[0] == ABORT1 || a_rx_ctrl[0] == ABORT2)
- {
- result = COM_ABORT;
- }
- }
- else
- {
- errors++;
- }
- if(errors >= MAX_ERRORS)
- {
- result = COM_ERROR;
- }
- }
- p_buf_int = p_buf;
- size = file_size;
- /* Here 1024 bytes length is used to send the packets */
- while((size) && (result == COM_OK))
- {
- /* Prepare next packet */
- PreparePacket(p_buf_int, aPacketData, blk_number, size);
- ack_recpt = 0;
- a_rx_ctrl[0] = 0;
- errors = 0;
- /* Resend packet if NAK for few times else end of communication */
- while((!ack_recpt) && (result == COM_OK))
- {
- /* Send next packet */
- if(size >= PACKET_1K_SIZE)
- {
- pkt_size = PACKET_1K_SIZE;
- }
- else
- {
- pkt_size = PACKET_SIZE;
- }
- Interface_Transmit(&aPacketData[PACKET_START_INDEX], pkt_size + PACKET_HEADER_SIZE, NAK_TIMEOUT);
- /* Send CRC or Check Sum based on CRC16_F */
- #ifdef CRC16_F
- temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], pkt_size);
- Interface_PutByte(temp_crc >> 8);
- Interface_PutByte(temp_crc & 0xFF);
- #else /* CRC16_F */
- temp_chksum = CalcChecksum(&aPacketData[PACKET_DATA_INDEX], pkt_size);
- Interface_PutByte(temp_chksum);
- #endif /* CRC16_F */
- /* Wait for Ack */
- if((Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == ACK))
- {
- ack_recpt = 1;
- if(size > pkt_size)
- {
- p_buf_int += pkt_size;
- size -= pkt_size;
- if(blk_number == (USER_FLASH_SIZE / PACKET_1K_SIZE))
- {
- result = COM_LIMIT; /* boundary error */
- }
- else
- {
- blk_number++;
- }
- }
- else
- {
- p_buf_int += pkt_size;
- size = 0;
- }
- }
- else
- {
- errors++;
- }
- /* Resend packet if NAK for a count of 10 else end of communication */
- if(errors >= MAX_ERRORS)
- {
- result = COM_ERROR;//
- }
- }
- }
- /* Sending End Of Transmission char */
- ack_recpt = 0;
- a_rx_ctrl[0] = 0x00;
- errors = 0;
- while((!ack_recpt) && (result == COM_OK))
- {
- Interface_PutByte(EOT);
- /* Wait for Ack */
- if(Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
- {
- if(a_rx_ctrl[0] == ACK)
- {
- ack_recpt = 1;
- }
- else if(a_rx_ctrl[0] == CA)
- {
- if((Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA))
- {
- HAL_Delay(2);
- Interface_Clean();
- result = COM_ABORT;
- }
- }
- }
- else
- {
- errors++;
- }
- if(errors >= MAX_ERRORS)
- {
- result = COM_ERROR;
- }
- }
- /* Empty packet sent - some terminal emulators need this to close session */
- if(result == COM_OK)
- {
- /* Preparing an empty packet */
- aPacketData[PACKET_START_INDEX] = SOH;
- aPacketData[PACKET_NUMBER_INDEX] = 0;
- aPacketData[PACKET_CNUMBER_INDEX] = 0xFF;
- for(i = PACKET_DATA_INDEX; i < (PACKET_SIZE + PACKET_DATA_INDEX); i++)
- {
- aPacketData[i] = 0x00;
- }
- /* Send Packet */
- Interface_Transmit(&aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT);
- /* Send CRC or Check Sum based on CRC16_F */
- #ifdef CRC16_F
- temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
- Interface_PutByte(temp_crc >> 8);
- Interface_PutByte(temp_crc & 0xFF);
- #else /* CRC16_F */
- temp_chksum = CalcChecksum(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
- Interface_PutByte(temp_chksum);
- #endif /* CRC16_F */
- /* Wait for Ack and 'C' */
- if(Interface_Receive(&a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
- {
- if(a_rx_ctrl[0] == CA)
- {
- HAL_Delay(2);
- Interface_Clean();
- result = COM_ABORT;
- }
- }
- }
- return result; /* file transmitted successfully */
- }
- #endif /* __IAP_YMODEM_ATY_C */
- /******************************** End Of File *********************************/
|