ARM cortex-M3 Exception Handling Analysis

Source: Internet
Author: User

1. The processor may be in the following states before an exception occurs:

1.
Handler

2.
Thread, MSP

3.
Thread, PSP

 

Ii. Exceptions:

1,
There is a stack pressure process. If PSP is used when an exception occurs, it is pushed to PSP. If an exception occurs, it is pushed to MSP.

2,
The LR value will be set based on the processor mode and the stack used (of course, the configured LR value will be re-applied to the stack)

 

Iii. When an exception is returned:

The stack is interpreted based on the LR value and then POP data from the corresponding stack to the Register.

Example:

When OSStartHighRdy-> ow.dsv-> ow.dsv_nosave is used to start the first thread, when an exception occurs, the stack is pressed to MSP (which does not affect PSP content ).
Before the LR command returns an exception, there is an orr lr, LR, #0x04 to set the LR value to ensure that the POP data in PSP is returned from the exception to the Register, in this case, the PSP value is OSTCBHighRdy-> OSTCBStkPtr, which is the Stack Array defined when the task is created. When initializing the task array in the OSTaskStkInit function, make sure that the structure of the stack is the same as that of the stack generated by the processor's automatic pressure stack when an exception occurs.

 

Iv. Stack content analysis:

1,
Variable

2,
When calling a function, if the sub-function is more complex, the compiler will automatically press into the r4-r11, LR (into the number of registers, determined by the complexity of the sub-function)

3,
Exception saving, pushed to stack is r0-r3, R12, LR, PC, xspr (hardware completed directly)

4,
Process switching, all registers are pressed to the stack

 

Specific operations after an exception or crash:

 

When an exception occurs, we need two values: PC and LR. We can find the stack through LR and then find the PC through stack.

1,
If LR = 0xfffffff9 indicates that MSP is used when an exception occurs, we only need to read the value of the current SP,

SP + = 0x1c; the read content is the value of the PC that is pushed into the stack when an exception is generated. This value is compared with the disassembly code to get the exception generated in specific words.

2,
If LR = 0xfffffffd indicates that PSP is used when an exception occurs, we need to read the PSP value instead of directly reading the sp value. In the integrated development environment of Keil, during debugging, the register window has a banked option, which gives the current PSP value. Of course, you can add two sentences in exception handling:

Mrs r0, PSP

Push {R0}

We can get the value of PSP in the current MSP, and then the operation is the same as above, PSP + = 0x1c;
The read content is the value of the PC that is pushed into the stack when an exception is generated. This value is compared with the disassembly code to obtain the exception generated by the specific statement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.