Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

130 lignes
5.0 KiB

  1. /******************************************************************************/
  2. /* Files to Include */
  3. /******************************************************************************/
  4. #include <plib.h> /* Include to use PIC32 peripheral libraries */
  5. #include <stdint.h> /* For uint32_t definition */
  6. #include <stdbool.h> /* For true/false definition */
  7. /******************************************************************************/
  8. /* Exception Macro Definitions */
  9. /******************************************************************************/
  10. /*When WRITE_EXCEPTION_CAUSE_TO_FLASH is defined the PIC32 executes a self
  11. write routine to save the exception cause register.*/
  12. /* #define WRITE_EXCEPTION_CAUSE_TO_FLASH */
  13. #ifdef WRITE_EXCEPTION_CAUSE_TO_FLASH
  14. /* Physical Addresses which are at the end of KSEG 0 program memory. */
  15. /* User may want to adjust these values */
  16. #define EXCEPTION_CAUSE 0x1D007FFC
  17. #define EXCEPTION_ADDR 0x1D007FF8
  18. #endif
  19. /******************************************************************************/
  20. /* Exception Variable Declaration */
  21. /******************************************************************************/
  22. /* static in case exception condition would stop auto variable being created */
  23. static enum {
  24. EXCEP_IRQ = 0, /* interrupt */
  25. EXCEP_AdEL = 4, /* address error exception (load or ifetch) */
  26. EXCEP_AdES, /* address error exception (store) */
  27. EXCEP_IBE, /* bus error (ifetch) */
  28. EXCEP_DBE, /* bus error (load/store) */
  29. EXCEP_Sys, /* syscall */
  30. EXCEP_Bp, /* breakpoint */
  31. EXCEP_RI, /* reserved instruction */
  32. EXCEP_CpU, /* coprocessor unusable */
  33. EXCEP_Overflow, /* arithmetic overflow */
  34. EXCEP_Trap, /* trap (possible divide by zero) */
  35. EXCEP_IS1 = 16, /* implementation specfic 1 */
  36. EXCEP_CEU, /* CorExtend Unuseable */
  37. EXCEP_C2E /* coprocessor 2 */
  38. } _excep_code;
  39. /* static in case exception condition would stop auto variable being created */
  40. static unsigned int _epc_code;
  41. static unsigned int _excep_addr;
  42. /******************************************************************************/
  43. /* Exception Handling */
  44. /******************************************************************************/
  45. /* This function overrides the normal _weak_ _generic_exception_handler which
  46. is defined in the C32 User's Guide. The _weak_ _generic_exception_handler
  47. just does an infinite loop. */
  48. void _general_exception_handler(void)
  49. {
  50. unsigned long t0 = _CP0_GET_COUNT(); /* Used for NVMOP 6 us Delay */
  51. /* Mask off Mask of the ExcCode Field from the Cause Register
  52. Refer to the MIPs M4K Software User's manual */
  53. _excep_code=_CP0_GET_CAUSE() & 0x0000007C >> 2;
  54. _excep_addr=_CP0_GET_EPC();
  55. _CP0_SET_STATUS(_CP0_GET_STATUS()&0xFFFFFFE); /* Disable Interrupts */
  56. #ifdef WRITE_EXCEPTION_CAUSE_TO_FLASH
  57. /* Store the exception causes in program memory in case the part exhibited
  58. the problem in release mode. Gives user a place to start debugging
  59. the problem. */
  60. NVMCON = 0x4001; /* set WREN and Word Programing mode */
  61. NVMADDR = EXCEPTION_CAUSE; /* PM Address at which we'll store the */
  62. /* cause register */
  63. NVMDATA = _excep_code;
  64. /* wait at least 6 us for LVD start-up
  65. assume we're running at max frequency
  66. (80 MHz) so we're always safe */
  67. {
  68. while (_CP0_GET_COUNT() - t0 < (80/2)*6);
  69. }
  70. NVMKEY = 0xAA996655;
  71. NVMKEY = 0x556699AA; /* unlock sequence */
  72. NVMCONSET = NVMCON_WR;
  73. while(NVMCON & NVMCON_WR); /* wait on write to finish */
  74. NVMCON = 0x4001; /* set WREN and Word Programing mode */
  75. NVMADDR = EXCEPTION_ADDR; /* PM Address at which we'll store the */
  76. /* exception address register */
  77. NVMDATA = _excep_addr;
  78. /* wait at least 6 us for LVD start-up
  79. assume we're running at max frequency
  80. (80 MHz) so we're always safe */
  81. {
  82. while (_CP0_GET_COUNT() - t0 < (80/2)*6);
  83. }
  84. NVMKEY = 0xAA996655;
  85. NVMKEY = 0x556699AA; /* unlock sequence */
  86. NVMCONSET = NVMCON_WR;
  87. while(NVMCON & NVMCON_WR);
  88. /* Write the exception cause and address to the part can be read and
  89. the cause determined. */
  90. NVMWriteWord((void*)EXCEPTION_CAUSE, _excep_code);
  91. NVMWriteWord((void*)EXCEPTION_ADDR, _excep_addr);
  92. #endif
  93. while (1)
  94. {
  95. /* Examine _excep_code to identify the type of exception */
  96. /* Examine _excep_addr to find the address that caused the exception */
  97. Nop();
  98. Nop();
  99. Nop();
  100. SoftReset();
  101. Nop();
  102. }
  103. }