2017-07-25 20 views
0
void _general_exception_handler (unsigned caused, unsigned status) 
    { 
     RCON = RCON_EXCEPTION; 

     // Point of No return. 
     Kernel_Reset(); 
    } 

我的代碼似乎陷入了這個陷阱,我有幾個問題想弄清楚爲什麼它從調用我的代碼中的有效函數或更好的問題,繼續困擾着我是處理器如何知道已有違規。MPLAB -X中PIC32的一般異常處理程序,軟件如何知道何時拋出該錯誤?

1)當我看到在調試器監視窗口中,cause顯示我Address 0xA0007EC0value 0x10800C1Cstatus表明我address 0xA0007EC4value is 0x00100003。我怎麼能從這些地址和價值信息中找出異常的原因和狀態? 2)處理器如何知道發生異常故障?

Snapshots of Memory

+3

軟件不知道。硬件知道。 –

+0

可以將初始化爲NULL的指針'static Log_t * pLog = NULL;'引起異常錯誤,因爲它的地址爲NULL。例如,如果正在使用'pLog'來從之前讀取數據,那麼它將被分配一個VALID ADDRESS。由於NULL意味着內存的開始,如果你試圖訪問禁止部分,它會拋出異常錯誤? – newb7777

+1

是的。基本上它是硬件本身檢測無效條件,比如訪問保留的內存位置。您可以查看MCU的存儲器映射圖,以瞭解這些保留段的位置。處理器知道調用該異常處理程序,因爲當硬件檢測到異常時,異常會「向量化」到特定地址。實際上,它意味着如果出現異常,請在此指定地址執行代碼,編譯器/鏈接器負責其餘部分。更多信息:http://microchipdeveloper.com/faq:82 – mindcruzer

回答

0

這裏是處理我通常在PIC32項目中使用:

// Exception handler: 
static enum { 
EXCEP_IRQ = 0,   // interrupt 
EXCEP_AdEL = 4,   // address error exception (load or ifetch) 
EXCEP_AdES,    // address error exception (store) 
EXCEP_IBE,    // bus error (ifetch) 
EXCEP_DBE,    // bus error (load/store) 
EXCEP_Sys,    // syscall 
EXCEP_Bp,    // breakpoint 
EXCEP_RI,    // reserved instruction 
EXCEP_CpU,    // coprocessor unusable 
EXCEP_Overflow,   // arithmetic overflow 
EXCEP_Trap,    // trap (possible divide by zero) 
EXCEP_IS1 = 16,   // implementation specfic 1 
EXCEP_CEU,    // CorExtend Unuseable 
EXCEP_C2E    // coprocessor 2 
    } _excep_code; 

    static unsigned int _epc_code; 
    static unsigned int _excep_addr; 

    // this function overrides the normal _weak_ generic handler 
    void _general_exception_handler(void) 
    { 
    asm volatile("mfc0 %0,$13" : "=r" (_excep_code)); 
    asm volatile("mfc0 %0,$14" : "=r" (_excep_addr)); 

    _excep_code = (_excep_code & 0x0000007C) >> 2; 
     while (1) { 
     // Examine _excep_code to identify the type of exception 
     // Examine _excep_addr to find the address that caused the 
    exception 
    Nop(); 
    Nop(); 
    Nop(); 
    } 
}// End of exception handler 
0

我有這樣的竟然是在微芯片的代碼中的錯誤問題:

sprintf(foo_str, "%f", NAN) 

會(並將)拋出一個總線錯誤異常......(錯誤在他們的「_fconvert」函數中)調試/識別我修改了我的異常處理程序來打印出來(產品已經有一個ASCII UART連接到它的主機)什麼是例外,違規地址是什麼,然後它會自行重置。復位部分很難找到,它位於PIC32MX的許多Microchip文檔之一中。這取代了和諧(1.10)生成的「_general_exception_handler()」,不知道這是否與和諧2.x的兼容

void _general_exception_handler (void) 
{ 
    char fail_str[96]; 
    /* Mask off Mask of the ExcCode Field from the Cause Register 
    Refer to the MIPs Software User's manual */ 
    _excep_code = (_CP0_GET_CAUSE() & 0x0000007C) >> 2; 
    _excep_addr = _CP0_GET_EPC(); 
    _cause_str = cause[_excep_code]; 
    sprintf(fail_str, "ERROR,%s,cause=%d,addr=%x,", 
        _cause_str, _excep_code, _excep_addr); 
    //"send_my_string" copies the string to the ring buffer 
    send_my_string(fail_str); 
    //"tx_chars_from_ring_buffer" returns # of chars left in the ring buffer 
    while(tx_chars_from_ring_buffer()); 
    //queue up another set of chars equal to the length of the UART FIFO 
    sprintf(fail_str, "\r\r\r\r\r\r\r\r\r\r"); 
    send_my_string(fail_str); 
    while(tx_chars_from_ring_buffer()); 
#if (__DEBUG) 
    while (1) 
    { 
     SYS_DEBUG_BreakPoint(); 
    } 
#endif 
/* The following code illustrates a software Reset */ 
// assume interrupts are disabled 
// assume the DMA controller is suspended 
// assume the device is locked 
/* perform a system unlock sequence */ 
// starting critical sequence 
SYSKEY = 0x00000000; //write invalid key to force lock 
SYSKEY = 0xAA996655; //write key1 to SYSKEY 
SYSKEY = 0x556699AA; //write key2 to SYSKEY 
// OSCCON is now unlocked 
/* set SWRST bit to arm reset */ 
RSWRSTSET = 1; 
/* read RSWRST register to trigger reset */ 
_excep_code = RSWRST; 
/* prevent any unwanted code execution until reset occurs*/ 
while(1); 
} 

我沒有與它玩了一下,以確保該消息得到了直通輸出FIFO在復位被調用之前...

相關問題