Objective
This page contains a basic general exception code example for the PIC32MX MCU.
The project implements a custom _general_exception_handler() function that is triggered by either a trap exception, or an illegal address exception, depending on which line of "bad" code is un-commented in main.c.
Simply uncomment one of the "bad" lines of code, then build/run in debug mode. The project contains a while(1); loop inside the general exception handler. When this is reached, the code can be halted, and the "Watches" window may be examined to view the Cause, Status and EPC registers to determine the cause of the exception. The disassembly listing output file should also be examined to verify the EPC value.
The hardware for this project uses the ArduinoTM compatible chipKIT WF-32 board from Digilent Inc.
Materials
Hardware Tools (Optional)
Tool | About | Purchase |
---|---|---|
| | |
| | |
| | |
| |
Software Tools
This project has been verified to work with the following versions of software tools:
MPLAB® X IDE v3.15, MPLAB® XC32 Compiler v1.40. The current versions of these tools will probably work as well, so try using them first.
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® X
Integrated Development Environment |
| | | | |
MPLAB® XC32
C/C++ Compiler |
| | | | |
Exercise Files
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project and Source Files
|
| | | |
We recommend extracting the .zip to the following "root" folders:
Windows:
- C:
Linux:
- /home/<username>/MPLABXProjects
Mac OS:
- Users/<username>/MPLABXProjects
You should see an appended sub-folder "/MTT/32bit-mx/code-examples/general-exception-usage" containing the project "general-exception-usage.X"
Procedure
Attach the debugger to the WF-32 board. Power up the board. Start MPLAB X.
Results
Error 1 (Illegal Address Exception - main.c - line 145 executed)
LED LD6 will light up, to indicate the occurrence of this exception:
In MPLAB® X, open the Watches Window window and add the Status, Cause and EPC CP0 registers to the view:
The Status register reads 0x00100002. Ignore the leading 0x0010 for now and focus on the last nibble 0x2 (StatusEXL = 1). Per the PIC32 Datasheet, this indicates that we've encountered an error.
StatusEXL:
Exception Level: Set by the processor when any exception other than Reset, Soft Reset, NMI or Cache Error exception are taken
The Cause register reads 0x10800014. Ignore the leading 0x1080 for now. The LSB value of 0x14 is tricky since it isn't properly aligned within the LSB to directly represent the EXCCODE value. We need to convert this number to the true value of the structure within CauseEXCCODE.
Right shift 0x14 twice, and we get the value 0x05. Looking at the exception cause table in the PIC32MX CPU Family Reference Manual, we see that this equates to ADDRESS ERROR EXCEPTION (STORE):
CauseEXCCODE<4:0>:
Exception Code Bits:
Indicates what kind of exception happened. Used by the _general_exception_handler() to determine the cause of the exception and branch to the appropriate handler code.
Note: On PIC32MX, Interrupt Exceptions all have their own dedicated entry point and therefore, their handlers do not need to need to consult CauseEXCCODE.
The EPC register reads 0x9D000028. This is the address of the instruction which the CPU will resume processing after returning from exception. We can confirm this by displaying the disassembly listing file (in MPLAB X, select Window»Output»Disassembly Listing File):
This is an example of a precise exception, whereby the EPC value identifies the instruction that caused the exception.
Error 2 (Trap Exception - main.c - line 146 executed)
LED LD5 will light up, to indicate the occurrence of this exception:
In MPLAB® X, open the Watches Window window and add the Status, Cause and EPC CP0 registers to the view:
The Status register reads 0x00100002. Ignore the leading 0x0010 for now and focus on the last nibble 0x2 (StatusEXL = 1). Per the PIC32 Datasheet, this indicates that we've encountered an error.
The Cause register reads 0x10800034. Right shift the LSB 0x34 twice, and we get the value 0x0D. Looking at the exception cause table in the PIC32MX CPU Family Reference Manual, we see that this equates to a TRAP EXCEPTION.
The EPC register reads 0x9D00002C. This is the address of the instruction which the CPU will resume processing after returning from exception. We can confirm this by displaying the disassembly listing file (in MPLAB X, select Window»Output»Disassembly Listing File):
This is an example of an imprecise exception, whereby the EPC value does not identify the instruction that caused the exception.
In this example, the divide operation is carried out by the Multiply-Divide Unit which runs in parallel to the main CPU. The EPC value points to the CPU instruction that was interrupted by the MDU exception.
Conclusions
This project has provided an example of how to implement a custom general exception handler on the PIC32MX MCU. For more information on the steps required to setup and configure interrupts and exceptions on the PIC32MX, please visit the PIC32MX Interrupt and Exception Usage page.
Table of Contents
|