About STM32 software reset code writing

Publisher:beup001Latest update time:2017-09-11 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

For STM32, there are two ways to reset the software:

 

1) Use the official software library

 

  The system reset function is directly provided in the stm32f10x_nvic.c file of the official software library

 

/****************************************************** ******************************
* Function Name: NVIC_GenerateSystemReset
* Description: Generates a system reset.
* Input: None
* Output: None
* Return : None
********************************************* **********************************/
void NVIC_GenerateSystemReset(void)
{
  SCB->AIRCR = AIRCR_VECTKEY_MASK | (u32 )0x04;
}

 

But isn't it OK to just call this function?

 

There is such a sentence in the Cortex-M3 authoritative guide

 

There is a point to note here: there is often a delay from the time SYSRESETREQ is asserted to the time the reset generator executes the reset command
. During this delay, the processor can still respond to interrupt requests. But our original intention is often
to stop the execution here and not do anything else. Therefore, it is best to
set FAULTMASK before issuing a reset request.

 

So it is best to set FAULTMASK to be on the safe side.

 

The official stm32f10x_nvic.c file also provides this function directly

 

/****************************************************** ******************************
* Function Name: NVIC_SETFAULTMASK
* Description: Enables the FAULTMASK priority: Raises the execution priority to -1 .
* Input : None
* Output : None
* Return : None
************************************* ******************************************/
void NVIC_SETFAULTMASK(void)
{
  __SETFAULTMASK ();
}

 

So to reset the system, just call these two functions.

NVIC_SETFAULTMASK();

GenerateSystemReset();

 

2) Write the corresponding assembly code yourself

 

In fact, the principle is the same. I didn't see the official function at first, so I wrote it myself. In fact, if you look at the official function, it actually calls the same code as cortexm3_macro.s. Haha.

 

My code:

 

/*******************************************************************************
* Function Name : SystemReset
* Description : Configures the port pin connected to the push button. GPIO_D_4
* Input : None
* Output : None
* Return : None
*******************************************************************************/
__asm ​​void SystemReset(void)
{
 MOV R0, #1 //; 
 MSR FAULTMASK, R0 //; Clear FAULTMASK Disable all interrupts
 LDR R0, =0xE000ED0C //;
 LDR R1, =0x05FA0004 //; 
 STR R1, [R0] //; System software reset   

 
deadloop
    B deadloop //; Dead loop prevents the program from running to the following code
}

 

Then when you need it, just call the function directly in the C program.

 

SystemReset();

In the MDK environment, when the assembly code is embedded in the C code, writing and calling it is the same as C function.

The above function is defined in misc.c;

The function is declared in misc.h: void SystemReset(void);

Call function in main.c: SystemReset();

Replenish:

Software reset code common to MDK and IAR:

#if defined ( __CC_ARM ) /*------------------RealView Compiler ------------------*/ 
__asm ​​void GenerateSystemReset (void) 

 MOV R0, #1 //;  
 MSR FAULTMASK, R0 //; FAULTMASK disables all interrupts 
 LDR R0, =0xE000ED0C //; 
 LDR R1, =0x05FA0004 //;  
 STR R1, [R0] //;    
  
deadloop 
    B deadloop //;  

#elif (defined (__ICCARM__)) /*------------------ ICC Compiler -------------- -----*/ 
//#pragma diag_suppress=Pe940 
void GenerateSystemReset(void) 

  __ASM("MOV R0, #1"); 
  __ASM("MSR FAULTMASK, R0"); 
  SCB->AIRCR = 0x05FA0004; 
  for( ;;); 
}

In the above code, the program can be reset when running in FLASH, but it fails to be debugged in RAM. The reason may be that the boot on the board is loose, resulting in problems with the RAM boot selection.

 

From the above, we can see that the embedded assembly under IAR is not a simple asm("...");, such as asm("LDR R0, =0xE000ED0C "); will report an error


Keywords:STM32 Reference address:About STM32 software reset code writing

Previous article:STm32 uses stm32cube GPIO to light up the LED
Next article:STM32F0xx IAP implementation of interrupt vector table redefinition

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号