STM32F1 enters HardFault_Handler()

Publisher:创新脑细胞Latest update time:2018-05-29 Source: eefocusKeywords:STM32F1  HardFault_Handler() Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When accessing SRAM, a problem occurs. The problem code is as follows:

The main function program is as follows:

  1. /*  

  2.  * Function name: main 

  3.  * Description: Main function 

  4.  * Input: None 

  5.  * Output: None 

  6.  */  

  7. int main(void)  

  8. {  

  9.     USART1_Config();  

  10.     printf("USART IS OK!\r\n");  

  11.     SysTick_Init();  

  12.     LED_GPIO_Init();  

  13.             

  14.     LED_Turn(LEDALL,LED_OFF);  

  15.     printf("Enter SRAM memory test! \r\n");  

  16.     FSMC_SRAM_Test();  

  17.     printf("Enter SRAM memory test ended! \r\n");  

  18.             

  19.     while(1)  

  20.     {  

  21.         LED_Turn(LED4,LED_ON);  

  22.         Delay_ms(500);  

  23.         LED_Turn(LED4,LED_OFF);  

  24.         Delay_ms(500);  

  25.     }    

  26. }  

  27. The content of the FSMC_SRAM_Test() function on line 16 is as follows:  

  28.   

  29. /* 

  30. ************************************************************************ 

  31.   Function name: void FMSC_SRAM_Test(void) 

  32.                 FSMC_SRAM test example 

  33.   Entry parameters: None   

  34.                  

  35.   Return data: None 

  36.                  

  37.   other instructions: 

  38.                  

  39.   Calling form:      

  40. ************************************************************************ 

  41. */  

  42. void FSMC_SRAM_Test(void)  

  43. {  

  44.     u32 WriteReadStatus = 0, Index = 0;  

  45.     u16 TxBuffer[SRAM_BUFFER_SIZE];  

  46.     u16 RxBuffer[SRAM_BUFFER_SIZE];  

  47.                       

  48.   printf("\n\rFSMC initialization \r\n");  

  49.     //FSMC initialization  

  50.   FSMC_SRAM_Init();  

  51.   printf("\n\r Static memory initialization completed!\r");  

  52.                       

  53.   /*Fill the data BUFFER of SRAM with a series of data starting from 0x1234 and increasing continuously */  

  54.   SRAM_Fill_Buffer(TxBuffer, SRAM_BUFFER_SIZE, 0x1234);  

  55.                       

  56.   printf("\n\rStart writing data to static memory \r");    

  57.   FSMC_SRAM_WriteBuffer(TxBuffer, SRAM_WRITE_READ_ADDR, SRAM_BUFFER_SIZE);  

  58.   printf("\n\r Static memory writing data completed!\r");   

  59.                       

  60.   printf("\n\rStart reading data from static memory\r");   

  61.     FSMC_SRAM_ReadBuffer(RxBuffer, SRAM_WRITE_READ_ADDR, SRAM_BUFFER_SIZE);    

  62.   printf("\n\r Static memory data reading completed!\r");  

  63.                       

  64.   printf("\n\r Check whether the written data is consistent with the read data, if yes: DS1 is on, if no: DS2 is on!\n\r");          

  65.     for (Index = 0x00; (Index < SRAM_BUFFER_SIZE) && (WriteReadStatus == 0); Index++)  

  66.     {  

  67.         if (RxBuffer[Index] != TxBuffer[Index])  

  68.         {  

  69.             WriteReadStatus = Index + 1;  

  70.         }  

  71.         printf("%02x ",RxBuffer[Index]);  

  72.     }  

  73.                   

  74.     if (WriteReadStatus == 0)  

  75.     {   

  76.         LED_Turn(LED1,LED_ON);  

  77.     }  

  78.     else  

  79.     {   

  80.         LED_Turn(LED2,LED_ON);  

  81.     }  

  82. }  

When this function is blocked, the program after this function can be executed.
But when it is not blocked, the function can be entered normally. The pintf() function on line 21 cannot be printed, but the FSMC_SRAM_Init() on line 22 can be entered;
but this function enters HardFault_Handler() after executing only two sentences;
after blocking the occurrence statement of entering HardFault_Handler(), it still enters HardFault_Handler() after executing only two sentences.
It is very frustrating. From the search results on the Internet, it is roughly as follows:


  • Memory leak

  • Stack overflow, increase the stack size

  • Array out of bounds

  • Wrong operation of hardware

  • The location of variable definition affects the compiler

  • The pointer points to a place that the system does not allow, and the access is out of bounds.

  • JLINK driver version problem

There are too many explanations, it's very confusing, it seems that there are many reasons for entering the HardFault_Handler() function.
I also made corresponding adjustments and processing according to the opinions of the majority of netizens... The results are all the same tragic.
Later, an unintentional move solved my problem. I defined the two arrays declared by the FSMC_SRAM_Test() function as global variables, as follows:
u16 TxBuffer[SRAM_BUFFER_SIZE];
u16 RxBuffer[SRAM_BUFFER_SIZE];
and the problem was solved. I was lucky and fortunate.
Visually, the analysis of the problem is as follows:

  • The array definition is incorrect, and the compiler has problems with its handling.

  • Another possibility is that the array I defined is too large. SRAM_BUFFER_SIZE is 0XFF. There is probably a memory problem, but the compiler cannot detect it.


Keywords:STM32F1  HardFault_Handler() Reference address:STM32F1 enters HardFault_Handler()

Previous article:STM32F1 drives AM2302 temperature and humidity sensor
Next article:KeilMDK4.22 compiles STM32 project and reports error: Error: L6218

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号