求助: STM32L073 的Flash Half-Page 编程
最近使用NUCLEO-L073RZ 开发板试验一下Flash的烧写,Copy Flash区域的数据到另一个Flash区域。按Word烧写没有问题,但是按Half-Page烧写总是不成功。按Word烧写的效率太低。在L0的库函数中未见Half-Page的例子,
试验代码在RAM中运行,关闭了Flash 预取;仍然不成功。
试验中发现:
如果 pBu 指向RAM空间则可以写成功,指向Flash 则不会成功。
代码如下:
u32 xBu[16]={0x11111111,0x22222222,0x33333333,0x44444444};
__ramfunc void IAP_Flash_CopyPage(u32 DstAdr, u32 *pBu, s32 Count) {
u16 i,k;
if((FLASH->PECR & FLASH_PECR_PRGLOCK) != RESET) {
if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET) {
FLASH->PEKEYR = FLASH_PEKEY1;
FLASH->PEKEYR = FLASH_PEKEY2;
}
FLASH->PRGKEYR = FLASH_PRGKEY1;
FLASH->PRGKEYR = FLASH_PRGKEY2;
}
while (Count>0) {
// Erase One Page;
while (FLASH->SR & FLASH_FLAG_BSY);
FLASH->PECR |= (FLASH_PECR_ERASE | FLASH_PECR_PROG);
*(vu32 *)DstAdr = 0x00000000;
while (FLASH->SR & FLASH_FLAG_BSY);
FLASH->PECR &= ~(FLASH_PECR_ERASE | FLASH_PECR_PROG);
FLASH->SR |= FLASH_SR_EOP;
// Prog 2 Half Pages
for (k=0; k<2; k++) {
if (Count>0) {
FLASH->PECR |= (FLASH_PECR_FPRG | FLASH_PECR_PROG);
for (i=0; i<16; i++) {
*(vu32 *)DstAdr =(u32)*pBu;
DstAdr+=4; pBu++;
}
Count-=64;
while (FLASH->SR & FLASH_FLAG_BSY);
FLASH->SR |= FLASH_SR_EOP;
FLASH->PECR &= ~(FLASH_PECR_FPRG | FLASH_PECR_PROG);
}
}
}
FLASH->SR |= FLASH_SR_EOP;
FLASH->PECR &=~ (FLASH_PECR_FPRG | FLASH_PECR_PROG);
FLASH->PECR |= FLASH_PECR_PELOCK;
} |