Trackdown the exceptions in MQX

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Trackdown the exceptions in MQX

Trackdown the exceptions in MQX

When working on Cortex-M4 platform, an exception which has not been expected (an interrupt without ISR, an access to an illegal address, etc).

The code below shows how to implement it.

static void expt_frm_dump(pointer ext_frm_ptr)

{

  static char *expt_name[] = {

    "None",

    "Reset",

    "NMI",

    "HardFault",

    "MemManage",

    "BusFault",

    "UsageFault",

    "Rsvd",

    "Rsvd",

    "Rsvd",

    "SVCall",

    "Debug Monitor",

    "Rsvd",

    "PendSV",

    "SysTick"

  };

  uint_32 excpt_num = __get_PSR() & 0x1FF;

  printf("Opps, bad thing happened.\n");

  if(excpt_num < 16){

    printf("The exception [%s] invoked in TASK 0x%x\n",

         expt_name[excpt_num] , _task_get_id());

    printf("Dump the exception frame as :\n");

    printf("R0:\t0x%08x\n", *((uint_32 *)ext_frm_ptr));

    printf("R1:\t0x%08x\n", *((uint_32 *)ext_frm_ptr + 1));

    printf("R2:\t0x%08x\n", *((uint_32 *)ext_frm_ptr + 2));

    printf("R3:\t0x%08x\n", *((uint_32 *)ext_frm_ptr + 3));

    printf("R12:\t0x%08x\n", *((uint_32 *)ext_frm_ptr + 4));

    printf("LR:\t0x%08x\n", *((uint_32 *)ext_frm_ptr + 5));

    printf("PC:\t0x%08x\n", *((uint_32 *)ext_frm_ptr + 6));

    printf("PSR:\t0x%08x\n", *((uint_32 *)ext_frm_ptr + 7));

  }else{

    printf("The external interrupt %d occured while no handler to serve it.\n");

  }

}

void  task_exception_handler(_mqx_uint para, pointer stack_ptr)

{

    pointer expt_frm_ptr = (pointer)__get_PSP();

    expt_frm_dump(expt_frm_ptr);

}

void test_task(unsigned long para)

{

  unsigned int *p_bad = (unsigned int *)0;

  printf("Install the _int_exception_isr to replace the _int_default_isr\n");

  _int_install_exception_isr();

  /* Set the exception handler of the task */

  _task_set_exception_handler(_task_get_id(), task_exception_handler);

  *p_bad = 0;     // an access to address 0

   _task_block();

}

%3CLINGO-SUB%20id%3D%22lingo-sub-1122087%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ETrackdown%20the%20exceptions%20in%20MQX%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1122087%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EWhen%20working%20on%20Cortex-M4%20platform%2C%20an%20exception%20which%20has%20not%20been%20expected%20(an%20interrupt%20without%20ISR%2C%20an%20access%20to%20an%20illegal%20address%2C%20etc).%3C%2FP%3E%3CP%3EThe%20code%20below%20shows%20how%20to%20implement%20it.%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CPRE%20__default_attr%3D%22c%2B%2B%22%20__jive_macro_name%3D%22code%22%20class%3D%22jive_text_macro%20_jivemacro_uid_13685271450596202%20jive_macro_code%22%20jivemacro_uid%3D%22_13685271450596202%22%20modifiedtitle%3D%22true%22%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3Estatic%20void%20expt_frm_dump(pointer%20ext_frm_ptr)%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%7B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20static%20char%20*expt_name%5B%5D%20%3D%20%7B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22None%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22Reset%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22NMI%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22HardFault%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22MemManage%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22BusFault%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22UsageFault%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22Rsvd%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22Rsvd%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22Rsvd%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22SVCall%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22Debug%20Monitor%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22Rsvd%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22PendSV%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22SysTick%22%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20%7D%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20uint_32%20excpt_num%20%3D%20__get_PSR()%20%26amp%3B%200x1FF%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20printf(%22Opps%2C%20bad%20thing%20happened.%5Cn%22)%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20if(excpt_num%20%26lt%3B%2016)%7B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22The%20exception%20%5B%25s%5D%20invoked%20in%20TASK%200x%25x%5Cn%22%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20expt_name%5Bexcpt_num%5D%20%2C%20_task_get_id())%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22Dump%20the%20exception%20frame%20as%20%3A%5Cn%22)%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22R0%3A%5Ct0x%2508x%5Cn%22%2C%20*((uint_32%20*)ext_frm_ptr))%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22R1%3A%5Ct0x%2508x%5Cn%22%2C%20*((uint_32%20*)ext_frm_ptr%20%2B%201))%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22R2%3A%5Ct0x%2508x%5Cn%22%2C%20*((uint_32%20*)ext_frm_ptr%20%2B%202))%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22R3%3A%5Ct0x%2508x%5Cn%22%2C%20*((uint_32%20*)ext_frm_ptr%20%2B%203))%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22R12%3A%5Ct0x%2508x%5Cn%22%2C%20*((uint_32%20*)ext_frm_ptr%20%2B%204))%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22LR%3A%5Ct0x%2508x%5Cn%22%2C%20*((uint_32%20*)ext_frm_ptr%20%2B%205))%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22PC%3A%5Ct0x%2508x%5Cn%22%2C%20*((uint_32%20*)ext_frm_ptr%20%2B%206))%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22PSR%3A%5Ct0x%2508x%5Cn%22%2C%20*((uint_32%20*)ext_frm_ptr%20%2B%207))%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20%7Delse%7B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22The%20external%20interrupt%20%25d%20occured%20while%20no%20handler%20to%20serve%20it.%5Cn%22)%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20%7D%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%7D%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3Evoid%26nbsp%3B%20task_exception_handler(_mqx_uint%20para%2C%20pointer%20stack_ptr)%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%7B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20pointer%20expt_frm_ptr%20%3D%20(pointer)__get_PSP()%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20expt_frm_dump(expt_frm_ptr)%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%7D%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3Evoid%20test_task(unsigned%20long%20para)%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%7B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20unsigned%20int%20*p_bad%20%3D%20(unsigned%20int%20*)0%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20printf(%22Install%20the%20_int_exception_isr%20to%20replace%20the%20_int_default_isr%5Cn%22)%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20_int_install_exception_isr()%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20%2F*%20Set%20the%20exception%20handler%20of%20the%20task%20*%2F%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20_task_set_exception_handler(_task_get_id()%2C%20task_exception_handler)%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%20*p_bad%20%3D%200%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20an%20access%20to%20address%200%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%26nbsp%3B%26nbsp%3B%20_task_block()%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%208pt%3B%22%3E%7D%3C%2FSPAN%3E%3C%2FP%3E%0A%0A%0A%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
No ratings
Version history
Last update:
‎09-10-2020 02:53 AM
Updated by: