2025-12-29 4:19 AM - last edited on 2025-12-29 5:00 AM by Andrew Neil
I was doing reset through SPI command and expected results are not happening.
this is the line i was expecting to reset the MCU
SCB->AIRCR = (NVIC_AIRCR_VECTKEY | (SCB->AIRCR & (0x700)) | (1 << NVIC_SYSRESETREQ));
where
#define NVIC_AIRCR_VECTKEY (0x05FA << 16)
#define NVIC_SYSRESETREQ 2also my watchdog is running and expected to reset the system.
code snippet
FLASH_Unlock();
FLASH_ErasePage(0x08013000);
FLASH_ProgramWord(0x08013000, (uint64_t)0x0000000000000000ULL);
FLASH_Lock();
FLASH_Unlock();
FLASH_ErasePage(0x08013008);
FLASH_Lock();
SCB->AIRCR = (NVIC_AIRCR_VECTKEY | (SCB->AIRCR & (0x700)) | (1 << NVIC_SYSRESETREQ));
__DSB();
while (1);
Edited to apply proper source code formatting - please see How to insert source code for future reference.
2025-12-29 6:05 AM - edited 2025-12-29 6:06 AM
Hi i have tried toggle a gpio as you mentioned and it didnt reset. As I mentioned some hung state or something which i couldnt trace. And i will let you know about the NRST pin since i am lacking the setup as of now
2025-12-30 6:23 AM
@TDK @mƎALLEm @Andrew Neil
Hi I found that, it's not issue with reset it's purely related to the flash operations I am doing. Once I commented those operations I am able to proper reset.
these are the operations I am perform
FLASH_Unlock();
FLASH_ErasePage(0x08013000); // newly added
FLASH_ProgramWord(0x08013000, (uint64_t)0x0000000000000000ULL);
FLASH_Lock();
FLASH_Unlock();
FLASH_ErasePage(0x08013800);
FLASH_Lock();were the related functions are
void FLASH_Unlock(void)
{
/* (1) Wait till no operation is on going */
/* (2) Check that the Flash is unlocked */
/* (3) Perform unlock sequence */
/* (4) Set EOPIE otherwise EOP bit will never be set */
Flash_WaitIfBusy();
if ((FLASH->CR & FLASH_CR_LOCK) != 0) /* (2) */
{
FLASH->KEYR = FLASH_KEY1; /* (3) */
FLASH->KEYR = FLASH_KEY2;
}
FLASH->CR |= FLASH_CR_EOPIE; /* (4) */
FLASH->SR = FLASH_SR_PGSERR|FLASH_SR_SIZERR|FLASH_SR_PGAERR|FLASH_SR_WRPERR|FLASH_SR_PROGERR|FLASH_SR_EOP|FLASH_SR_OPERR; // Clean all registers
}
void FLASH_ErasePage(uint32_t Page_Address)
{
Page_Address = Page_Address & 0xFFFF;
Page_Address = Page_Address/C_PAGE_SIZE; /* (0) */
Flash_WaitIfBusy();
/* If the previous operation is completed, proceed to erase the page */
FLASH->CR |= FLASH_CR_PER; /* (1) */
FLASH->CR &= ~(FLASH_CR_PNB); /* (2) */
FLASH->CR |= (Page_Address << FLASH_CR_PNB_Pos); /* (2) */
FLASH->CR |= FLASH_CR_STRT; /* (3) */
__NOP();
__NOP();
Flash_WaitIfBusy(); /* (4) */
FLASH->CR &= ~FLASH_CR_PER; /* (5)*/
}
void FLASH_ProgramWord(uint32_t Address, uint64_t Data)
{
__IO uint32_t tmp = 0;
Flash_WaitIfBusy();
/* If the previous operation is completed, proceed to program the new first
half word */
FLASH->CR |= FLASH_CR_PG;
*(__IO uint32_t*)Address = (uint32_t)Data;
/* If the previous operation is completed, proceed to program the new second
half word */
tmp = Address + 4;
*(__IO uint32_t*) tmp = (uint32_t)(Data >> 32);
/* Wait for last operation to be completed */
Flash_WaitIfBusy();
if ((FLASH->SR & FLASH_SR_EOP) != 0)
{
FLASH->SR = FLASH_SR_EOP;
}
else
{
// If for some reason EOP is not set, error occured while writing
dbglog(0xFE);
error_type = ERR_FLASH_PRG_EOP;
}
/* Disable the PG Bit */
FLASH->CR &= ~FLASH_CR_PG;
}
void FLASH_Lock(void)
{
FLASH->CR |= FLASH_CR_LOCK;
}
void Flash_WaitIfBusy(void)
{
/* (1) Wait till no operation is on going */
while ((FLASH->SR & FLASH_SR_BSY1) != 0) /* (1) */
{
__NOP();
}
}Please Help me with this scenario
2025-12-30 7:34 AM
This looks like AI generated code with the comments. Maybe stick to the working HAL implementations to modify flash.