This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP432P401R: Reading MSP432 core registers in C

Part Number: MSP432P401R

Hi all,

Is there any way to read the Core LR register in C?

(If yes, don't bother reading on!  If no, further details follow to give a background of why I need to read the LR register.  Perhaps if a direct read is not possible, then maybe I can restructure my approach so that I don't need to read the LR register)

I'm working with the TIRTOS/SYSBIOS exception handlers. 

I've decided to use the standard exception handlersbecause they provide some useful preprocessing of the exception status and provide output to the console that helps me determine that my custom exception handling is working properly.

My configuration is as follows

  • m3Hwi.excHandlerFunc is undefined, so one of the default handlers will be used,
  • m3Hwi.enableException = true while developing to get full exception decoding while developing, will switch to false once things settle
  • m3Hwi.excHookFunc = "&myExceptionHook" gives me a function that can perform some custom exception handling. 

My custom execption handler (myExceptionHook) dumps the exception context (plus some extra system state) to external flash (bitbashed) so that it can be read out later to try and diagnose the exception.

The problem here is the myExceptionHook gets passed an exception context that contains the state of the LR register prior to the exception, but not the state of the LR register after the exception.  The value after the exception provides some information about the context in which the exception occurred, and it is decoded by the standard exception handler.  I'd like to be able to do the same thing when I read the exception context from flash. 

One potential alternative is to stop using the in built exception handlers and have a full custom handler (i.e. define m3Hwi.excHandlerFunc = "&myExceptionHandler").  myExceptionHandler is passed two parameters -  an exception stack (which can be processed into an ExceptionContext structure) and what I assume is the current state of the LR.    I've tried this but I run into problems trying to replicate the Hwi_excFillContext() function.  The HWI module has access to data structures etc that I don't.  

Cheers

Julian

  • Julian

     You should be able to transfer the LR register to a variable using inline assembly.  It's not something we'd normally recommend, as this can have unintended consequences if you do something that the compiler isn't expecting. (i.e. stepping on / modifying a register that the compiler was using).    I'll look into getting you an example to show how this might be done,  but you might start by looking at this ARM SW development tools link which describes using assembly inline with C code.

    Regards,

      Bob

  • G'Day Bob,

    Thank you for the feedback. There is an interesting comment in that link you sent

    'No variables are declared for the sp (r13), lr (r14), and pc (r15) registers, and they cannot be read or directly modified in inline assembly code.'

    So it sounds like I might be out of luck.  I also did a search for some inline assembly examples and was a bit put off by the answer in this thread

    "....Today TI does not have or plan to provide assembly for the MSP432....."

    In any case, I have solved the problems I was having in creating a full custom exception handler and that removes my need to read the LR register directly.

    The solution was to copy the appropriate #includes from Hwi.c into my source file.  That gives me access the to parts of the RTOS I need to do a full conversion of the exception stack to an exception context.  The exception handler is passed the post-exception value of the LR register so I now have everything I need.

    Cheers

    Julian

  • Julian,
    I'm glad you were able to find a suitable workaround. As for the comment about TI not providing assembly for the MSP432, this simply means that we won't be providing our examples in this form. In the overwhelming majority of user cases, assembly-level code is not necessary to meet a project's goals (whether they're performance or power-related goals). To be the most portable and productive, it's best to be writing code at the Driverlib or (preferrably) TI Drivers level.

    Regards,
    Bob L.

**Attention** This is a public forum