SAMV71 Xplained Ultra Software Package 1.5

exceptions.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
00028 /* ---------------------------------------------------------------------------- */
00029 
00030 /**
00031  * \file
00032  * This file contains the default exception handlers.
00033  *
00034  * \note
00035  * The exception handler has weak aliases.
00036  * As they are weak aliases, any function with the same name will override
00037  * this definition.
00038  */
00039 
00040 /*----------------------------------------------------------------------------
00041  *        Headers
00042  *----------------------------------------------------------------------------*/
00043 
00044 #include "chip.h"
00045 
00046 /*----------------------------------------------------------------------------
00047  *        Exported functions
00048  *----------------------------------------------------------------------------*/
00049 
00050 /**
00051  * \brief Default NMI interrupt handler.
00052  */
00053 void NMI_Handler(void)
00054 {
00055     printf("\r\n-E- Enter NMI_Handler!");
00056 
00057     while (1);
00058 }
00059 
00060 /**
00061  * \brief This function back trace the stack to give exact address where fault
00062  happened
00063 **/
00064 __STATIC_INLINE uint32_t StackUnwind(void)
00065 {
00066     uint32_t Fault_Add;
00067 
00068 #if defined (__CC_ARM)
00069     uint32_t temp;
00070     __ASM("mrs temp, msp ");
00071     __ASM{ ldr Fault_Add, [temp, #28]}
00072 #else
00073     __ASM("mrs r0, msp ");
00074     __ASM("ldr %0, [r0,#28]" : "=r" (Fault_Add));
00075 #endif
00076     return Fault_Add;
00077 }
00078 
00079 /**
00080  * \brief If Other Faults are enabled then HardFault error will look for those
00081  *  errors to give more detail about fault
00082 **/
00083 static void HardFault_reason(void)
00084 {
00085     uint32_t CFSRValue;
00086     TRACE_DEBUG("In Hard Fault Handler\n\r");
00087     TRACE_DEBUG("SCB->HFSR = 0x%08x\n\r", SCB->HFSR);
00088 
00089     if ((SCB->HFSR & SCB_HFSR_DEBUGEVT_Msk)) {
00090         TRACE_DEBUG("Debug Event Hard Fault\n\r");
00091         TRACE_DEBUG("SCB->DFSR = 0x%08x\n", SCB->DFSR);
00092     }
00093 
00094     if ((SCB->HFSR & SCB_HFSR_VECTTBL_Msk)) {
00095         TRACE_DEBUG("Fault was due to vector table read on \
00096             exception processing\n\r");
00097     }
00098 
00099     // Forced HardFault
00100     if ((SCB->HFSR & SCB_HFSR_FORCED_Msk)) {
00101         TRACE_DEBUG("Forced Hard Fault\n\r");
00102         TRACE_DEBUG("SCB->CFSR = 0x%08x\n\r", SCB->CFSR);
00103 
00104         // Usage Fault
00105         if ((SCB->CFSR & SCB_CFSR_USGFAULTSR_Msk)) {
00106             CFSRValue = SCB->CFSR;
00107             TRACE_DEBUG("Usage fault: ");
00108             CFSRValue >>= SCB_CFSR_USGFAULTSR_Pos;
00109 
00110             if ((CFSRValue & (1 << 9)))
00111                 TRACE_DEBUG("Divide by zero\n\r");
00112 
00113             if ((CFSRValue & (1 << 8)))
00114                 TRACE_DEBUG("Unaligned access error\n\r");
00115 
00116             if ((CFSRValue & (1 << 3)))
00117                 TRACE_DEBUG("Coprocessor access error\n\r");
00118 
00119             if ((CFSRValue & (1 << 2)))
00120                 TRACE_DEBUG("Integrity check error on EXC_RETURN\n\r");
00121         }
00122 
00123         // Bus Fault
00124         if ((SCB->CFSR & SCB_CFSR_BUSFAULTSR_Msk)) {
00125             CFSRValue = SCB->CFSR;
00126             TRACE_DEBUG("Bus fault: ");
00127             CFSRValue >>= SCB_CFSR_BUSFAULTSR_Pos;
00128 
00129             if ((CFSRValue & (1 << 7)) && (CFSRValue & (1 << 1))) {
00130                 TRACE_DEBUG("Precise data access error. Bus Fault Address \
00131                     Register is: %x \n\r", SCB->BFAR);
00132             }
00133 
00134             if ((CFSRValue & (1 << 4)))
00135                 TRACE_DEBUG("Bus fault has occurred on exception entry\n\r");
00136 
00137             if ((CFSRValue & (1 << 3)))
00138                 TRACE_DEBUG("bus fault has occurred on exception return\n\r");
00139 
00140             if ((CFSRValue & (1 << 2)))
00141                 TRACE_DEBUG("Imprecise data access error\n\r");
00142 
00143             if ((CFSRValue & (1 << 0))) {
00144                 TRACE_DEBUG("This bit indicates a bus fault on an instruction \
00145                     pre-fetch. \n\r");
00146             }
00147         }
00148     }
00149 
00150     // MemoryFault
00151     if ((SCB->CFSR & SCB_CFSR_MEMFAULTSR_Msk)) {
00152         CFSRValue = SCB->CFSR;
00153         TRACE_DEBUG("Memory fault: ");
00154         CFSRValue >>= SCB_CFSR_MEMFAULTSR_Pos;
00155 
00156         if ((CFSRValue & (1 << 9)) != 0)
00157             TRACE_DEBUG("Divide by zero\n\r");
00158     }
00159 
00160     __ISB();
00161     __DMB();
00162     __ASM volatile("BKPT #01");
00163 }
00164 /**
00165  * \brief Default HardFault interrupt handler.
00166  */
00167 
00168 void HardFault_Handler(void)
00169 {
00170     printf("\n\rHardFault at address 0X%x\n\r", (int)StackUnwind());
00171     __ISB();
00172     __DMB();
00173     HardFault_reason();
00174 }
00175 
00176 #ifndef MPU_EXAMPLE_FEATURE
00177 /**
00178  * \brief Default MemManage interrupt handler.
00179  */
00180 void MemManage_Handler(void)
00181 {
00182     printf("\n\rMemoryMemFault (MPU fault) at address 0X%x\n\r",
00183            (int)StackUnwind());
00184     __ISB();
00185     __DMB();
00186     __ASM volatile("BKPT #01");
00187 }
00188 #endif
00189 
00190 /**
00191  * \brief Default BusFault interrupt handler.
00192  */
00193 void BusFault_Handler(void)
00194 {
00195     __ASM("nop");
00196     __ASM("nop");
00197     printf("\n\rBus Fault at address 0X%x\n\r", (int)StackUnwind());
00198 
00199     __ISB();
00200     __DMB();
00201     __ASM volatile("BKPT #01");
00202 }
00203 
00204 /**
00205  * \brief Default UsageFault interrupt handler.
00206  */
00207 void UsageFault_Handler(void)
00208 {
00209     printf("\r\nUsage fault at address 0X%x", (int)StackUnwind());
00210 
00211     __ISB();
00212     __DMB();
00213     __ASM volatile("BKPT #01");
00214 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines