stm32_ota/SYSTEM/FLASH/flash.c

278 lines
10 KiB
C
Raw Normal View History

2023-12-15 06:28:06 +00:00
#include "flash.h"
#include "stm32f10x_flash.h"
#include "stm32f10x_rcc.h"
#include "usart.h"
#include "sysport.h"
/********************************************************************************
* @file flash.c
* @author <EFBFBD>̳ϿƼ<EFBFBD> Mr.Wang
* @version V1.0.0
* @date 11-Dec-2018
* @brief <EFBFBD>STM32<EFBFBD>ڲ<EFBFBD>flash<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
******************************************************************************
*******************************************************************************/
/****************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>void Read_Flash_Byte(uint32_t readAddr, uint8_t *p, uint16_t length)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD>readAddr<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡreadLen<EFBFBD>ֽڳ<EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>p<EFBFBD><EFBFBD>ַ<EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 readAddr <EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
* @param2 *readBuf <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
* @param3 readLen <EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
****************************************************************************/
void Read_Flash_Byte(uint32_t readAddr, uint8_t *readBuf, uint16_t readLen)
{
while(readLen--)
{
*(readBuf++) = *((uint8_t*)readAddr++) ;
}
}
/****************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>void Read_Flash_Byte(uint32_t readAddr, uint8_t *p, uint16_t length)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD>readAddr<EFBFBD><EFBFBD><EFBFBD>ʴ<EFBFBD><EFBFBD><EFBFBD>ȡreadLen<EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>p<EFBFBD><EFBFBD>ַ<EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 readAddr <EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
* @param2 *readBuf <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
* @param3 readLen <EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
****************************************************************************/
void Read_Flash_HalfWord(u32 readAddr, u16 *readBuf, u16 readLen)
{
u16 i ;
for(i=0;i<readLen;i++)
{
readBuf[i] = FLASH_ReadHalfWord(readAddr) ;//<2F><>ȡ2<C8A1><32><EFBFBD>ֽ<EFBFBD>.
readAddr += 2 ;//ƫ<><C6AB>2<EFBFBD><32><EFBFBD>ֽ<EFBFBD>.
}
}
/****************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>FLASH_Status Write_Flash_NoCheck(u32 WriteAddr, u16 *writeBuf, uint16_t writeLen)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD>û<EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>writeAddr<EFBFBD><EFBFBD>ַд<EFBFBD><EFBFBD><EFBFBD>writeLen<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 writeAddr д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
* @param2 *writeBuf <EFBFBD><EFBFBD>Ҫд<EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
* @param3 writeLen <EFBFBD><EFBFBD><EFBFBD><EFBFBD>(16λ)<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 FLASH_Status <EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASHд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @arg : FLASH_BUSY = 1, //æ<><C3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD>
FLASH_ERROR_PG, //<2F><><EFBFBD>̴<EFBFBD><CCB4><EFBFBD><EFBFBD><EFBFBD> <20><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD>0xFFFF<46>ĵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>
FLASH_ERROR_WRP, <EFBFBD><EFBFBD><EFBFBD>̴<EFBFBD><EFBFBD><EFBFBD>
FLASH_ERROR_TIMEOUT, <EFBFBD><EFBFBD><EFBFBD>̳<EFBFBD>ʱ
* @arg FLASH_COMPLETE: FLASH<EFBFBD><EFBFBD><EFBFBD>̳ɹ<EFBFBD>
* ˵ <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʹ<EFBFBD><EFBFBD>HSI<EFBFBD><EFBFBD>
****************************************************************************/
FLASH_Status Write_Flash_NoCheck(u32 WriteAddr, u16 *writeBuf, uint16_t writeLen)
{
u16 i;
FLASH_Status writeResult ;
for(i=0;i<writeLen;i++)
{
writeResult = FLASH_ProgramHalfWord(WriteAddr,writeBuf[i]);
if( writeResult != FLASH_COMPLETE )
break ;
WriteAddr+=2;//<2F><>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>2.
}
return writeResult ;
}
/****************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>void Write_Flash_MultiPage(u32 WriteAddr,u16 *writeBuf, uint16_t writeLen)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>writeAddr<EFBFBD><EFBFBD>ַд<EFBFBD><EFBFBD><EFBFBD>writeLen<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 writeAddr д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
* @param2 *writeBuf <EFBFBD><EFBFBD>Ҫд<EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
* @param3 writeLen <EFBFBD><EFBFBD><EFBFBD><EFBFBD>(16λ)<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 RunResult <EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASHд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @arg RUNERR: FLASH<EFBFBD><EFBFBD><EFBFBD>̴<EFBFBD><EFBFBD><EFBFBD>
* @arg RUNOK: FLASH<EFBFBD><EFBFBD><EFBFBD>̳ɹ<EFBFBD>
* ˵ <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʹ<EFBFBD><EFBFBD>HSI<EFBFBD><EFBFBD>
****************************************************************************/
RunResult Write_Flash_MultiPage(u32 WriteAddr, u16 *writeBuf, uint16_t writeLen)
{
u32 secpos; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
u16 secoff; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƫ<EFBFBD>Ƶ<EFBFBD>ַ(16λ<36>ּ<EFBFBD><D6BC><EFBFBD>)
u16 secremain; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD>ַ(16λ<36>ּ<EFBFBD><D6BC><EFBFBD>)
u16 i;
u32 offaddr; //ȥ<><C8A5>0X08000000<30><30><EFBFBD>ĵ<EFBFBD>ַ
u16 STMFLASH_BUF[FLASH_PAGE_SIZE/2];//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2K<32>ֽ<EFBFBD>
if(WriteAddr<FLASH_BASE||(WriteAddr>=(FLASH_BASE+1024*FLASH_SIZE))) return (InParamErr) ;//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>ַ
if( SET!= RCC_GetFlagStatus(RCC_FLAG_HSIRDY))
{
SysErr("") ; //HSI<53><49><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3>޷<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASH<53><48>
return (RUNERR );
}
FLASH_Unlock(); //<2F><><EFBFBD><EFBFBD>
offaddr=WriteAddr-FLASH_BASE; //ʵ<><CAB5>ƫ<EFBFBD>Ƶ<EFBFBD>ַ.
secpos=offaddr/FLASH_PAGE_SIZE; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ 0~127 for STM32F103RBT6
secoff=(offaddr%FLASH_PAGE_SIZE)/2; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ƫ<EFBFBD><C6AB>(2<><32><EFBFBD>ֽ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ.)
secremain=FLASH_PAGE_SIZE/2-secoff; //<2F><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD>С
if(writeLen<=secremain)secremain = writeLen;//<2F><><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ
while(1)
{
Read_Flash_HalfWord(secpos*FLASH_PAGE_SIZE+FLASH_BASE,STMFLASH_BUF,FLASH_PAGE_SIZE/2);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i=0;i<secremain;i++)//У<><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if(STMFLASH_BUF[secoff+i]!=0XFFFF)break;//<2F><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
}
if(i<secremain)//<2F><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
{
FLASH_ErasePage(secpos*FLASH_PAGE_SIZE+FLASH_BASE);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i=0;i<secremain;i++)//<2F><><EFBFBD><EFBFBD>
{
STMFLASH_BUF[i+secoff]=writeBuf[i];
}
Write_Flash_NoCheck(secpos*FLASH_PAGE_SIZE+FLASH_BASE,STMFLASH_BUF,FLASH_PAGE_SIZE/2);//д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}else Write_Flash_NoCheck(WriteAddr,writeBuf,secremain);//д<>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><><D6B1>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
if(writeLen==secremain)break;//д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
else//д<><D0B4>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>
{
secpos++; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>1
secoff=0; //ƫ<><C6AB>λ<EFBFBD><CEBB>Ϊ0
writeBuf+=secremain; //ָ<><D6B8>ƫ<EFBFBD><C6AB>
WriteAddr+=secremain; //д<><D0B4>ַƫ<D6B7><C6AB>
writeLen-=secremain; //<2F>ֽ<EFBFBD>(16λ)<29><><EFBFBD>ݼ<EFBFBD>
if(writeLen>(FLASH_PAGE_SIZE/2))secremain=FLASH_PAGE_SIZE/2;//<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
else secremain=writeLen;//<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
}
};
FLASH_Lock();//<2F><><EFBFBD><EFBFBD>
return (RUNOK );
}
/****************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>RunResult Write_Flash_OnePage(uint32_t writePageAddr, uint8_t *writeBuf, uint16_t writeLen)
* <EFBFBD>ⲿ<EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD>ErrorLogPrintf()
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 writePageAddr д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
* @param2 *writeBuf <EFBFBD><EFBFBD>Ҫд<EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
* @param3 writeLen <EFBFBD><EFBFBD>Ҫд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>FLASH_PAGE_SIZE<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 RunResult <EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASHд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @arg RUNERR: FLASH<EFBFBD><EFBFBD><EFBFBD>̴<EFBFBD><EFBFBD><EFBFBD>
* @arg RUNOK: FLASH<EFBFBD><EFBFBD><EFBFBD>̳ɹ<EFBFBD>
* ˵ <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʹ<EFBFBD><EFBFBD>HSI<EFBFBD><EFBFBD>
****************************************************************************/
RunResult Write_Flash_OnePage(uint32_t writePageAddr, uint8_t *writeBuf, uint16_t writeLen)
{
uint16_t halfWord, timeOut = 0 ;
writeLen = writeLen/2+(writeLen%2) ; //<2F><>lengthǿ<68>Ƹı<C6B8>Ϊ2<CEAA>ı<EFBFBD><C4B1><EFBFBD>
if( SET!= RCC_GetFlagStatus(RCC_FLAG_HSIRDY))
{
SysErr("") ; //HSI<53><49><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3>޷<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASH<53><48>
return (RUNERR );
}
FLASH_Unlock(); //FLASH<53><48><EFBFBD><EFBFBD>
if( (writePageAddr<FLASH_BASE) || (writePageAddr>=(FLASH_BASE+FLASH_SIZE)) || (writePageAddr%FLASH_PAGE_SIZE != 0) ) //<2F><>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ϊҳ<CEAA>׵<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>FLASH<53><48>ַ<EFBFBD><D6B7>Χ<EFBFBD><CEA7> <20>Ƿ<EFBFBD><C7B7><EFBFBD>ַ
{
SysErr("") ; //д<><D0B4>FLASH<53><48>ַ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>
FLASH_Lock(); //<2F><><EFBFBD><EFBFBD>
return (RUNERR );
}
if( writeLen > FLASH_PAGE_SIZE)//д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3>Ȳ<EFBFBD><C8B2>ܳ<EFBFBD><DCB3><EFBFBD>һҳ
{
SysErr("") ; //д<><D0B4>FLASH<53><48><EFBFBD>ȳ<EFBFBD><C8B3>ޣ<EFBFBD>
FLASH_Lock(); //<2F><><EFBFBD><EFBFBD>
return (RUNERR );
}
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);//<2F><><EFBFBD><EFBFBD>־λ
if( FLASH_COMPLETE != FLASH_ErasePage(writePageAddr)) //ִ<><D6B4>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
{
SysErr("") ; //FLASH ҳ<><D2B3><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>
}
for( timeOut=0; (SET == FLASH_GetFlagStatus(FLASH_FLAG_BSY)); timeOut++)
{
Wait_For_Nms(10) ;
if( timeOut>10 )
{
SysErr("") ; //дFlashæ<68>ȴ<EFBFBD><C8B4><EFBFBD>ʱ<EFBFBD><CAB1>
FLASH_Unlock(); //FLASH<53><48><EFBFBD><EFBFBD>
return (RUNERR);
}
}
while(writeLen--)
{
FLASH_ClearFlag( FLASH_FLAG_EOP ) ;
halfWord = *(writeBuf++);
halfWord |= *(writeBuf++) << 8;
FLASH_ProgramHalfWord(writePageAddr, halfWord);
for( timeOut=0; ( SET != FLASH_GetFlagStatus(FLASH_FLAG_EOP) ); timeOut++)
{
Wait_For_Nms(10) ;
if(timeOut>30)
{
SysErr("") ; //дFlash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return (RUNERR);
}
}
writePageAddr += 2;
}
FLASH_Lock(); //<2F><><EFBFBD><EFBFBD>
return (RUNOK) ;
}
/****************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>RunResult Write_Flash(uint32_t writeAddr, uint8_t *writeBuf, uint16_t writeLen)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>writeAddr<EFBFBD><EFBFBD>ַд<EFBFBD><EFBFBD><EFBFBD>writeLen<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 writeAddr д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
* @param2 *writeBuf <EFBFBD><EFBFBD>Ҫд<EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
* @param3 writeLen <EFBFBD><EFBFBD>Ҫд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 RunResult <EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASHд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @arg RUNERR: FLASH<EFBFBD><EFBFBD><EFBFBD>̴<EFBFBD><EFBFBD><EFBFBD>
* @arg RUNOK: FLASH<EFBFBD><EFBFBD><EFBFBD>̳ɹ<EFBFBD>
* ˵ <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʹ<EFBFBD><EFBFBD>HSI<EFBFBD><EFBFBD>
****************************************************************************/
RunResult Write_Flash(uint32_t writeAddr, uint8_t *writeBuf, uint16_t writeLen)
{
uint16_t index = 0, timeOut = 0, halfWord = 0 ;
u32 writeOffset = 0 ;
writeLen = writeLen/2+(writeLen%2) ; //<2F><>logLenǿ<6E>Ƹı<C6B8>Ϊ2<CEAA>ı<EFBFBD><C4B1><EFBFBD> <20><>logLen<65>ֽڳ<D6BD><DAB3><EFBFBD>תΪ<D7AA><CEAA><EFBFBD>ֳ<EFBFBD><D6B3><EFBFBD>
if( SET!= RCC_GetFlagStatus(RCC_FLAG_HSIRDY))
{
SysErr("") ; //HSI<53><49><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3>޷<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLASH<53><48>
return (RUNERR );
}
FLASH_Unlock(); //FLASH<53><48><EFBFBD><EFBFBD>
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);//<2F><><EFBFBD><EFBFBD>־λ
for( timeOut=0; (SET == FLASH_GetFlagStatus(FLASH_FLAG_BSY)); timeOut++)
{
2023-12-20 10:22:52 +00:00
Wait_For_Nms(50) ;
2023-12-15 06:28:06 +00:00
if( timeOut>10 )
{
SysErr("") ; //дFlash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
FLASH_Unlock(); //FLASH<53><48><EFBFBD><EFBFBD>
return(RUNERR) ;
}
}
while(writeLen--)
{
FLASH_ClearFlag( FLASH_FLAG_EOP ) ;
halfWord = *(writeBuf+(index++)) ; //ȡuint16_t<5F><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>halfWord LSB
halfWord |= *(writeBuf+(index++)) << 8 ; //ȡuint16_t<5F><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>halfWord HSB
if( ( (writeAddr + writeOffset) % FLASH_PAGE_SIZE ) == 0 ) //д<><D0B4><EFBFBD><EFBFBD>ַΪҳ<CEAA>׵<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ
{
FLASH_ErasePage(writeAddr+writeOffset) ;
}
FLASH_ProgramHalfWord(writeAddr+writeOffset, halfWord) ; //<2F><>ַƫ<D6B7><C6AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƫ<EFBFBD>ƺ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>ֹFLASH_ERROR_PG<50><47><EFBFBD><EFBFBD>
writeOffset += 2;
}
FLASH_Lock(); //<2F><><EFBFBD><EFBFBD>
return(RUNOK) ;
}