#include "Flash.h" /** * @brief Calculate the number of pages * @param Size: The image size,the units of Size is Byte * @retval The number of pages or error */ uint32_t FLASH_Pages_Calculate(uint32_t Size) { uint32_t Flash_Page_Number = 0; if( Size % PAGE_SIZE != 0) { Flash_Page_Number = ( Size / PAGE_SIZE) + 1; } else Flash_Page_Number = Size / PAGE_SIZE; if( Flash_Page_Number > MAX_FPAGE_NUM) return 0; else return Flash_Page_Number; } /** * @brief erase the block before write Data * @param Address: the start address to erase Len: the length need to erase,uints is Byte * @retval 0:error; 1:success */ uint32_t FLASH_Prepare(uint32_t Address, uint32_t Len) //ÆðʼµØÖ·ºÍ×Ö³¤ { uint32_t NbrOFPage = 0; uint32_t EraseCount = 0; FLASH_Status FLASHStatus = FLASH_COMPLETE; FLASH_Unlock(); NbrOFPage = FLASH_Pages_Calculate( Len ); for(; EraseCount < NbrOFPage; EraseCount++) { FLASHStatus = FLASH_ErasePage( Address + ( PAGE_SIZE * EraseCount) ); if( FLASHStatus != FLASH_COMPLETE) break; } FLASH_Lock(); if( EraseCount != NbrOFPage) return 0; else return 1; } /** * @brief read the data from flash * @param Address: the start address to read Len: the length need to read,uints is Byte * @retval */ void FLASH_Read( uint32_t Address, uint8_t* Readbuff, uint32_t Len) { uint32_t ReadCount = 0; for( ; ReadCount < Len; ReadCount++) { Readbuff[ ReadCount ] = ((uint8_t *)Address)[ ReadCount ]; } } /** * @brief write data into flash * @param Address: the start address to write Len: the length need to write,uints is Byte * @retval 0:error ; 1:success */ uint32_t FLASH_Write( uint32_t Address, const uint8_t* pData, uint32_t Len) { uint32_t WriteCount = 0; uint32_t FlashDestination = Address; uint32_t FlashSource = (uint32_t)pData; FLASH_Status FLASHStatus = FLASH_COMPLETE; FLASH_Unlock(); for( ; WriteCount < Len; WriteCount+=2) { FLASHStatus = FLASH_ProgramHalfWord(FlashDestination, *(uint16_t*)FlashSource); if( FLASHStatus != FLASH_COMPLETE) { break; } if (*(uint16_t*)FlashDestination != *(uint16_t*)FlashSource) { break; } FlashDestination += 2; FlashSource += 2; } FLASH_Lock(); if( WriteCount < Len) return 0; else return 1; } ///** // * @brief Disable the write protection of desired pages // * @param None // * @retval None // */ //void FLASH_Dis_WriteProt_Pages(void) //{ // uint32_t UserOptionByte = 0, WRPR = 0; // uint16_t Var1 = OB_IWDG_SW, Var2 = OB_STOP_NoRST, Var3 = OB_STDBY_NoRST; // FLASH_Status Status = FLASH_BUSY; // static uint32_t UserMemoryMask = 0; // WRPR = FLASH_GetWriteProtectionOptionByte(); // /* Test if user memory is write protected */ // if ((WRPR & UserMemoryMask) != UserMemoryMask) // { // UserOptionByte = FLASH_GetUserOptionByte(); // UserMemoryMask |= WRPR; // Status = FLASH_EraseOptionBytes(); //½â³ý±£»¤ // if (UserMemoryMask != 0xFFFFFFFF) // { // Status = FLASH_EnableWriteProtection((uint32_t)~UserMemoryMask); // } // /* Test if user Option Bytes are programmed */ // if ((UserOptionByte & 0x07) != 0x07) // { // /* Restore user Option Bytes */ // if ((UserOptionByte & 0x01) == 0x0) // { // Var1 = OB_IWDG_HW; // } // if ((UserOptionByte & 0x02) == 0x0) // { // Var2 = OB_STOP_RST; // } // if ((UserOptionByte & 0x04) == 0x0) // { // Var3 = OB_STDBY_RST; // } // FLASH_UserOptionByteConfig(Var1, Var2, Var3); // } // // } //}