| | |
| | | * @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) |
| | | unsigned short int STMFLASH_ReadHalfWord(unsigned int faddr) |
| | | { |
| | | return *(volatile unsigned short 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; |
| | | 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; |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | |
| | | 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 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_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) |
| | |
| | | * @brief read the data from flash |
| | | * @param Address: the start address to read |
| | | Len: the length need to read,uints is Byte |
| | | * @retval |
| | | * @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 ]; |
| | | } |
| | | uint32_t ReadCount = 0; |
| | | |
| | | for( ; ReadCount < Len; ReadCount++) |
| | | { |
| | | Readbuff[ ReadCount ] = ((uint8_t *)Address)[ ReadCount ]; |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | #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; |
| | | |
| | | 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; |
| | | |
| | | } |
| | | |
| | | ///** |
| | |
| | | |
| | | // /* Test if user Option Bytes are programmed */ |
| | | // if ((UserOptionByte & 0x07) != 0x07) |
| | | // { |
| | | // { |
| | | // /* Restore user Option Bytes */ |
| | | // if ((UserOptionByte & 0x01) == 0x0) |
| | | // { |
| | |
| | | |
| | | // FLASH_UserOptionByteConfig(Var1, Var2, Var3); |
| | | // } |
| | | // |
| | | // |
| | | // } |
| | | |
| | | //} |