一、stm32H7 HAL库操作内部Flash BANK2读写不正常
超过1M写道bank2(0x08100000) 后面的内容不正常 HAL_FLASH_Program函数 返回错误 由于用的旧版本HAL库 看到安富莱贴子中关于旧版HAL BUG的问题参考新版HAL做了更改后仍然出现错误 不能正常写 决定采用寄存器方式写 不用HAL库 便将正点原子的寄存器版本中的STMFLASH_Write8Word 底层函数移植过来 问题解决
HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t FlashAddress, uint64_t DataAddress)
{
HAL_StatusTypeDef status = HAL_ERROR;
__IO uint64_t *dest_addr = (__IO uint64_t *)FlashAddress;
__IO uint64_t *src_addr = (__IO uint64_t*)((uint32_t)DataAddress);
uint32_t bank;
uint8_t row_index = 4;
__HAL_LOCK(&pFlash);
assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress));
if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
{
bank = FLASH_BANK_1;
}
else
{
bank = FLASH_BANK_2;
}
status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
if(status == HAL_OK)
{
if(bank == FLASH_BANK_1)
{
__HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1 | FLASH_FLAG_QW_BANK1 | FLASH_FLAG_WBNE_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK1);
SET_BIT(FLASH->CR1, FLASH_CR_PG);
}
else
{
__HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2 | FLASH_FLAG_QW_BANK2 | FLASH_FLAG_WBNE_BANK2 | FLASH_FLAG_ALL_ERRORS_BANK2);
SET_BIT(FLASH->CR2, FLASH_CR_PG);
}
__ISB();
__DSB();
#if 0
do
{
*dest_addr++ = *src_addr++;
} while (--row_index != 0);
#else
do
{
*dest_addr = *src_addr;
dest_addr++;
src_addr++;
row_index--;
} while (row_index != 0U);
#endif
__ISB();
__DSB();
status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
if(bank == FLASH_BANK_1)
{
if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_EOP_BANK1))
{
__HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
}
CLEAR_BIT(FLASH->CR1, FLASH_CR_PG);
}
else
{
if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_EOP_BANK2))
{
__HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
}
CLEAR_BIT(FLASH->CR2, FLASH_CR_PG);
}
}
__HAL_UNLOCK(&pFlash);
return status;
}
u8 STMFLASH_Write8Word(u32 faddr, u32* pdata)
{
u8 nword=8;
u8 res;
u8 bankx=0;
if(faddr<BANK2_FLASH_SECTOR_0)bankx=0;
else bankx=1;
res=STMFLASH_WaitDone(bankx,0XFF);
if(res==0)
{
if(bankx==0)
{
FLASH->CR1&=~(3<<4);
FLASH->CR1|=2<<4;
FLASH->CR1|=1<<1;
}else
{
FLASH->CR2&=~(3<<4);
FLASH->CR2|=2<<4;
FLASH->CR2|=1<<1;
}
while(nword)
{
*(vu32*)faddr=*pdata;
faddr+=4;
pdata++;
nword--;
}
__DSB();
res=STMFLASH_WaitDone(bankx,0XFF);
if(bankx==0)FLASH->CR1&=~(1<<1);
else FLASH->CR2&=~(1<<1);
}
return res;
}
|