zoukankan      html  css  js  c++  java
  • 【STM32F407开发板用户手册】第11章 STM32F407移植SEGGER的硬件异常分析

    最新教程下载:http://www.armbbs.cn/forum.php?mod=viewthread&tid=93255

    第11章       STM32F407移植SEGGER的硬件异常分析

    本章节为大家介绍SEGGER的硬件异常分析代码在MDK和IAR中的使用方法,在实际项目中比较有实用价值,因为我们经常会遇到进入硬件异常的情况。

    11.1 初学者重要提示

    11.2 移植方法

    11.3 MDK锁定硬件异常位置方法

    11.4 IAR锁定硬件异常位置方法

    11.5 硬件异常原因分析

    11.6 IAR注释自带的硬件异常

    11.7 实验例程

    11.8 总结

    11.1 初学者重要提示

    1.   MDK本身也是支持硬件异常分析的,就是不够直观,此贴是MDK的硬件异常分析文档:http://www.armbbs.cn/forum.php?mod=viewthread&tid=21940
    2.   IAR8带的硬件异常分析比较好用,在本章11.6小节有说明。

    11.2 移植方法

    直接移植SEGGER的硬件异常代码会有错误警告,这里针对IAR和MDK版本做了些简单修改,方便大家移植到自己的工程里面。

    •   MDK版本移植

    源文件位于本章配套例子的UserseggerHardFaultHandlerMDK文件夹,添加如下两个文件到工程里面即可。

     

    •   IAR版本移植

    源文件位于本章配套例子的UserseggerHardFaultHandlerIAR文件夹,添加如下两个文件到工程里面即可。

     

    在文件SEGGER_HardFaultHandler.c里面都添加了串口打印功能,方便不用编译器的调试功能时,通过串口打印提示是否进入硬件异常。

    #define ERR_INFO "
    Enter HardFault_Handler, System Halt.
    "
    
    #if 1
    {
        const char *pError = ERR_INFO;
        uint8_t i;
    
        for (i = 0; i < strlen(ERR_INFO); i++)
        {
            USART1->TDR = pError[i];
            /* 等待发送结束 */
            while((USART1->ISR & USART_ISR_TC) == 0);
        }    
    }
    #endif 

    11.3 MDK锁定硬件异常位置方法

    以本章配套的例子为大家做个说明。

    1、第1步:测试方法比较简单,进入调试状态,全速运行,然后按下K1按键,就会进入硬件异常中断,此时停止调试,程序就会自动定位到如下位置:

     

    2、在Watch1窗口添加变量_Continue

     

    3、修改为任何非0数值,就可以继续单步调试。这个代码后面还有一个第1步中的while循环,也可以继续采用第2步的方法修改。退出硬件异常后就是大家进入硬件异常前下一条要执行的指令(可能还是这个函数本身,因为一个函数由多个指令完成)。定位到出问题的位置:

     

    11.4 IAR锁定硬件异常位置方法

    以本章配套的例子为大家做个说明。

    1、第1步:测试方法比较简单,进入调试状态,全速运行,然后按下K1按键,就会进入硬件异常中断,此时停止调试,程序就会自动定位到如下位置:

     

    2、在Watch1窗口添加变量_Continue

     

    3、修改为任何非0数值,就可以继续单步调试。这个代码后面还有一个第1步中的while循环,也可以继续采用第2步的方法修改。退出硬件异常后就是大家进入硬件异常前下一条要执行的指令(可能还是这个函数本身,因为一个函数由多个指令完成)。定位到出问题的位置:

     

    11.5 硬件异常原因分析

    SEGGER提供的这个机制查找出问题的位置比较方便,具体原因需要继续在调试界面里面添加HardFaultRegs结构变量,这个结构体变量添加了所有大家想看的东西。下面是MDK调试状态查看部分结构体数值:

     

    具体上面的变量代表什么含义呢,代码里面有注释,查阅起来没有IAR自带的硬件异常提示方便(注意,下面的代码用到了位域)。

    #if DEBUG
    static volatile unsigned int _Continue;  // Set this variable to 1 to run further
    
    static struct {
      struct {
        volatile unsigned int r0;            // Register R0
        volatile unsigned int r1;            // Register R1
        volatile unsigned int r2;            // Register R2
        volatile unsigned int r3;            // Register R3
        volatile unsigned int r12;           // Register R12
        volatile unsigned int lr;            // Link register
        volatile unsigned int pc;            // Program counter
        union {
          volatile unsigned int byte;
          struct {
            unsigned int IPSR : 8;           // Interrupt Program Status register (IPSR)
            unsigned int EPSR : 19;          // Execution Program Status register (EPSR)
            unsigned int APSR : 5;           // Application Program Status register (APSR)
          } bits;
        } psr;                               // Program status register.
      } SavedRegs;
    
      union {
        volatile unsigned int byte;
        struct {
          unsigned int MEMFAULTACT    : 1;   // Read as 1 if memory management fault is active
          unsigned int BUSFAULTACT    : 1;   // Read as 1 if bus fault exception is active
          unsigned int UnusedBits1    : 1;
          unsigned int USGFAULTACT    : 1;   // Read as 1 if usage fault exception is active
          unsigned int UnusedBits2    : 3;
          unsigned int SVCALLACT      : 1;   // Read as 1 if SVC exception is active
          unsigned int MONITORACT     : 1;   // Read as 1 if debug monitor exception is active
          unsigned int UnusedBits3    : 1;
          unsigned int PENDSVACT      : 1;   // Read as 1 if PendSV exception is active
          unsigned int SYSTICKACT     : 1;   // Read as 1 if SYSTICK exception is active
          unsigned int USGFAULTPENDED : 1;   // Usage fault pended; usage fault started but was replaced by a
     higher-priority exception
          unsigned int MEMFAULTPENDED : 1;   //  Memory management fault pended; memory management fault started
     but was replaced by a higher-priority exception
          unsigned int BUSFAULTPENDED : 1;   // Bus fault pended; bus fault handler was started but was replaced
     by a higher-priority exception
          unsigned int SVCALLPENDED   : 1;   // SVC pended; SVC was started but was replaced by a higher-priority
     exception
          unsigned int MEMFAULTENA    : 1;   // Memory management fault handler enable
          unsigned int BUSFAULTENA    : 1;   // Bus fault handler enable
          unsigned int USGFAULTENA    : 1;   // Usage fault handler enable
        } bits;
      } syshndctrl;                          // System Handler Control and State Register (0xE000ED24)
    
      /* 省略未写 */
    
      volatile unsigned int afsr;            // Auxiliary Fault Status Register (0xE000ED3C), Vendor controlled (optional)
    } HardFaultRegs;
    #endif

    11.6 IAR自带的硬件异常分析

    还以本章配套的例子为例,进入调试状态,全速运行,然后按下K1按键,就会进入硬件异常中断,此时停止调试,IAR还会弹出一个硬件异常错误分析,刚进来的时候也许是个空白

     

    单步调试刷新下就出来了:

     

    指出了问题的原因是操作的数据地址有问题。

    11.7 实验例程

    专门为本章节配套了一个例子:V5-001_移植SEGGER的硬件异常分析机制。大家可以按照本章教程提供的方法进行测试。

    11.8 总结

    除了SEGGER的硬件异常分析方案,建议也测试下MDK和IAR的,以后遇到硬件异常问题,解决起来可以得心应手。

  • 相关阅读:
    构造函数的特点
    HashMap源码分析
    DVWA-7.1 SQL Injection(SQL注入)-Low
    DVWA-6.4 Insecure CAPTCHA(不安全的验证码)-Impossible
    DVWA-6.3 Insecure CAPTCHA(不安全的验证码)-High
    DVWA-6.2 Insecure CAPTCHA(不安全的验证码)-Medium
    DVWA-6.1 Insecure CAPTCHA(不安全的验证码)-Low
    DVWA-5.4 File Upload(文件上传)-Impossible
    DVWA-5.3 File Upload(文件上传)-High-绕过文件类型限制
    DVWA-5.2 File Upload(文件上传)-Medium-绕过文件类型限制
  • 原文地址:https://www.cnblogs.com/armfly/p/13252993.html
Copyright © 2011-2022 走看看