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.

TMS320F280039: How to read PC in C code?

Part Number: TMS320F280039

Dear champs,

I am asking this for our customer.

How does the user read register Program Counter (PC) in C code?

If it's not possible to read it in C code, how does the user read it in asm code?

Wayne Huang

  • Hello Wayne,

    You can use an inline assembly function in C to read the PC: "__asm(" MOV XAR5, PC");". You can write the value of this register to a known location and then read that as a variable.

    Best regards,

    Omer Amir

  • Dear Omer,

    Would you please make it clearer "You can write the value of this register to a known location and then read that as a variable"?

  • Hello Wayne,

    You can refer to the "C28x Assembly Language Instructions" chapter in the C28x Instruction Set Reference Guide, but I was essentially trying to guide you to the MOV instructions, which allow the XARn registers to be written to a 32-bit location (MOVL loc32, XARn). You can refer to this document on how to use this assembly instruction.

    Best regards,

    Omer Amir

  • Dear Omer,

    The user is asking what if there is an interrupt in between these instructions?

    For example, in pseudo code below,

    asm(" MOV XAR5, PC");

    An ISR in higher priority just happens here....

    asm(" MOVL loc32, XAR5")

    Read loc32

    The user is concerned when XAR5 is used in the ISR.

    Is XARn always context-saved (pushed/popped) by CPU/C compiler when entering the ISR and leaving the ISR?

    Or should the user use EINT; DINT;

    Like 

    DINT;

    asm(" MOV XAR5, PC");

    asm(" MOVL loc32, XAR5")

    Read loc32

    EINT;

  • Hello Wayne,

    From what I have seen in projects I have tested, if an ISR is used it always pushes any registers that are directly used within it (i.e. if XAR0 is used in the assembly for the ISR, the ISR will make sure to push XAR0 to the stack upon entering the routine); however, I don't know if this is for certain that the compiler always does this for any function call, such as in the case where a function is called from within the ISR. I will confirm this with another expert.

    Best regards,

    Omer Amir

  • if an ISR is used it always pushes any registers that are directly used within it

    That is correct.  For further details, please search the C28x compiler manual for the sub-chapter titled The __interrupt Keyword.

    Thanks and regards,

    -George