This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Internal flash write using HAL function

I am about to rewrite CPU internal flash ROM with HAL function.
There is something I do not understand so please let me ask a question.

The CPU used is STM32F4.

I'm writing by the uint16_t unit.
HAL_ERASEx...function are being used.

1. Before writing, is it necessary to "read two bytes anywhere on the flash and write the same data to the same place"?

2. Is delay necessary behind ERASE? If necessary, how much is its delay time?

3. Uint16_t Is delay required after writing unit? If so, what is the delay time?

4. Writing in of a flash, interrupt disabled status, ( __disable_irq()), I think it should be done and carried out. 1) Is interrupt disabled processing necessary? 2)Does the HAL_flash-function normally move by interrupt disabled status?

dummyFlash(); by carrying out.
I wrote and it became OK.

When I don't act, all everything's writing in related functions of after HAL-Function are an error. (I write and don't stick to the address.)
Is there any previous setting?

dummyFlash();
→ In 0x08000000* (0x08000000) is written in before flash rewriting of internal organs after a microcomputer reset now.

--Source code--
void test()
{ dummyFlash(); uint16_t dt[] = { 0x0123, 0x4567 }; internalFlashWrite(1, 0x08004000, dt, 2);
}

//--------------------------------------------------------------------------
// internal Flash sector erase
// in
// uint32_t eraseSector : erase sector no.(0~11)
//--------------------------------------------------------------------------
HAL_StatusTypeDef internalFlashErase(uint32_t eraseSector, uint32_t *SectorError)
{ FLASH_EraseInitTypeDef FEITdef;

FEITdef.TypeErase = FLASH_TYPEERASE_SECTORS; FEITdef.Banks = FLASH_BANK_1; FEITdef.Sector = eraseSector; FEITdef.NbSectors = 1; FEITdef.VoltageRange = FLASH_VOLTAGE_RANGE_3;

HAL_StatusTypeDef r = HAL_FLASHEx_Erase(&FEITdef, SectorError);

return r;
}

//--------------------------------------------------------------------------
// dummy flash
//--------------------------------------------------------------------------
void dummyFlash()
{ uint16_t dt = *(uint16_t *)0x08000000; HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, 0x08000000, dt);
}

//--------------------------------------------------------------------------
// internal Flash sector write
// in
// uint32_t eraseSector : 0~11
// uint32_t addr : write flash $
// uint16_t *dt : write data
// uint32_t halfWordSize : half word size
//--------------------------------------------------------------------------
void internalFlashWrite(uint32_t eraseSector, uint32_t addr, uint16_t *dt,uint32_t halfWordSize)
{ uint32_t SectorError = 0; HAL_StatusTypeDef r;

r = HAL_FLASH_Unlock(); if( r != HAL_OK ) return;

r = internalFlashErase(eraseSector, &SectorError); if (r != HAL_OK) return;

for (uint32_t i = 0; i < halfWordSize; i++) { r = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, addr + (i * 2), *(dt + i)); if(r != HAL_OK) return; } r = HAL_FLASH_Lock(); if( r != HAL_OK ) return;
}