#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
|
*/
|
unsigned short int STMFLASH_ReadHalfWord(unsigned int faddr)
|
{
|
return *(volatile unsigned short int*)faddr;
|
}
|
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;
|
return 1;
|
}
|
|
|
/**
|
* @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 PageError = 0;
|
FLASH_EraseInitTypeDef EraseInitStruct;
|
HAL_FLASH_Unlock();
|
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
|
EraseInitStruct.PageAddress = Address;
|
EraseInitStruct.NbPages = Len >>7;
|
// HAL_FLASH_Lock();
|
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
|
{
|
HAL_FLASH_Lock();
|
return 0;
|
}else{
|
HAL_FLASH_Lock();
|
return 1;
|
}
|
|
// uint32_t NbrOFPage = 0;
|
// uint32_t EraseCount = 0;
|
// HAL_StatusTypeDef status = HAL_OK;
|
//
|
// HAL_FLASH_Unlock();
|
// NbrOFPage = FLASH_Pages_Calculate( Len );
|
//
|
// for(; EraseCount < NbrOFPage; EraseCount++)
|
// {
|
// FLASH_PageErase( Address + ( PAGE_SIZE * EraseCount) );
|
// status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
|
//
|
// if( status != HAL_OK)
|
// break;
|
// }
|
//
|
// HAL_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
|
*/
|
#define DATA_32 ((uint32_t)0x12345678)
|
uint32_t FLASH_Write( uint32_t Address, const uint8_t* pData, uint32_t Len)
|
{
|
uint32_t WriteCount = 0,temp;
|
uint32_t FlashDestination = Address;
|
uint32_t FlashSource = (uint32_t)pData;
|
HAL_StatusTypeDef FLASHStatus = HAL_OK;
|
|
HAL_FLASH_Unlock();
|
|
for( ; WriteCount < Len; WriteCount+=4)
|
{temp=*(uint32_t*)FlashSource;
|
FLASHStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAMDATA_WORD, FlashDestination, temp);
|
|
if( FLASHStatus != HAL_OK)
|
{
|
break;
|
}
|
if (*(uint16_t*)FlashDestination != *(uint16_t*)FlashSource)
|
{
|
//break;
|
}
|
FlashDestination += 4;
|
FlashSource += 4;
|
}
|
|
HAL_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);
|
// }
|
//
|
// }
|
|
//}
|