STM32 HAL 内存溢出中断服务函数
时间: 2023-11-17 16:04:52 浏览: 134
stm32的HAL库串口中断资料.docx
在 STM32 HAL 中,如果发生了内存溢出,会触发硬件中断,此时应该执行内存溢出中断服务函数来处理这个问题。
内存溢出中断服务函数的代码示例如下:
```c
void HardFault_Handler(void)
{
__ASM volatile("TST LR, #4");
__ASM volatile("ITE EQ");
__ASM volatile("MRSEQ R0, MSP");
__ASM volatile("MRSNE R0, PSP");
__ASM volatile("B hard_fault_handler_c");
}
void hard_fault_handler_c(unsigned int * hardfault_args)
{
volatile unsigned int stacked_r0;
volatile unsigned int stacked_r1;
volatile unsigned int stacked_r2;
volatile unsigned int stacked_r3;
volatile unsigned int stacked_r12;
volatile unsigned int stacked_lr;
volatile unsigned int stacked_pc;
volatile unsigned int stacked_psr;
stacked_r0 = ((unsigned long) hardfault_args[0]);
stacked_r1 = ((unsigned long) hardfault_args[1]);
stacked_r2 = ((unsigned long) hardfault_args[2]);
stacked_r3 = ((unsigned long) hardfault_args[3]);
stacked_r12 = ((unsigned long) hardfault_args[4]);
stacked_lr = ((unsigned long) hardfault_args[5]);
stacked_pc = ((unsigned long) hardfault_args[6]);
stacked_psr = ((unsigned long) hardfault_args[7]);
printf("[HardFault]\n");
printf("R0 = %x\n", stacked_r0);
printf("R1 = %x\n", stacked_r1);
printf("R2 = %x\n", stacked_r2);
printf("R3 = %x\n", stacked_r3);
printf("R12 = %x\n", stacked_r12);
printf("LR = %x\n", stacked_lr);
printf("PC = %x\n", stacked_pc);
printf("PSR = %x\n", stacked_psr);
while (1);
}
```
在该代码中,`HardFault_Handler` 函数是内存溢出中断服务函数的入口函数。在该函数中,首先使用汇编代码判断当前使用的堆栈是主堆栈还是进程堆栈,然后再跳转到 `hard_fault_handler_c` 函数中执行实际的处理。
在 `hard_fault_handler_c` 函数中,首先将异常发生时的相关寄存器的值保存到变量中,然后输出相关信息,并在最后进入死循环,以防止程序继续执行。
阅读全文