내부 플래쉬를 삭제를 하려는데 삭제가 계속 안되는 현상이 발생했습니다.
NVIC_EnableIRQ(SysTick_IRQn);(NVIC_DisableIRQ 도 동일)로 인터럽트를 설정하면서 발생하는 부분은 파악하였는데 정확한 이유는 조금 더 확인해 봐야겠습니다.
#define FLASH_USER_START_ADDR ADDR_FLASH_PAGE_96 /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR ADDR_FLASH_PAGE_97 + FLASH_PAGE_SIZE - 1 /* End @ of user Flash area */
void Test_FLASH(void)
{
uint8_t achp_Data[32] = {0x12, 0x34, 0x56, 0x78, 0x89, 0x34, 0x56, 0x78\
,0x34, 0x56, 0x78, 0x89, 0x34, 0x56, 0x78, 0x12\
,0x56, 0x78, 0x89, 0x34, 0x56, 0x78, 0x12, 0x12\
,0x78, 0x89, 0x34, 0x56, 0x78, 0x12, 0x12, 0x12\
};
uint32_t aa_nDataSize = 32;
Program_FLASH(FLASH_USER_START_ADDR, FLASH_USER_END_ADDR, achp_Data, aa_nDataSize);
}
/*******************************************************************************
* Function Name :
* Parameters :
* Return : None
* Description :
432 = 1 Bank
496 = 1, 2 Bank
*******************************************************************************/
static uint32_t GetBank_FLASH(uint32_t nAddr)
{
return FLASH_BANK_1;
}
/*******************************************************************************
* Function Name :
* Parameters :
* Return :
* Description :
*******************************************************************************/
static uint32_t GetPage_FLASH(uint32_t Addr)
{
uint32_t page = 0;
if (Addr < (FLASH_BASE + FLASH_BANK_SIZE)){
/* Bank 1 */
page = (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;
}
else{
/* Bank 2 */
page = (Addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
}
return page;
}
/*******************************************************************************
* Function Name :
* Parameters :
* Return :
* Description :
*******************************************************************************/
uint32_t Program_FLASH(uint32_t a_nStartAddr, uint32_t a_nEndAddr, uint8_t *chp_Data, uint32_t a_nDataSize)
{
uint64_t MergeData64;
uint32_t ProgramL, TempL;
if(a_nDataSize%8 != 0){
DEBUGPRINT("Program_FLASH Size Error\r\n");
return FALSE;
}
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Clear OPTVERR bit set on virgin samples */
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
/* Get the 1st page to erase */
FirstPage = GetPage_FLASH(a_nStartAddr);
/* Get the number of pages to erase from 1st page */
NbOfPages = GetPage_FLASH(a_nEndAddr) - FirstPage + 1;
/* Get the bank */
BankNumber = GetBank_FLASH(a_nStartAddr);
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks = BankNumber;
EraseInitStruct.Page = FirstPage;
EraseInitStruct.NbPages = NbOfPages;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK){
while (1){
DEBUGPRINT("Page Erase Error\r\n");
HAL_FLASH_Lock();
return FALSE;
}
}
for(ProgramL = 0; ProgramL < a_nDataSize; ProgramL+=8){
MergeData64 = (uint64_t)chp_Data[ProgramL+3] + ((uint64_t)chp_Data[ProgramL+2] << 8)\
+ ((uint64_t)chp_Data[ProgramL+1] << 16) + ((uint64_t)chp_Data[ProgramL] << 24)\
+ ((uint64_t)chp_Data[ProgramL+7] << 32) + ((uint64_t)chp_Data[ProgramL+6] << 40)\
+ ((uint64_t)chp_Data[ProgramL+5] << 48) + ((uint64_t)chp_Data[ProgramL+4] << 56);
/* Write */
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, a_nStartAddr, MergeData64) != HAL_OK){
DEBUGPRINT("HAL_FLASH_Program Error\r\n");
HAL_FLASH_Lock();
return FALSE;
}
a_nStartAddr = a_nStartAddr + 8;
}
return TRUE;
}