This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

SVC_Handler for Cortex M0

Note: This was originally posted on 25th October 2012 at http://forums.arm.com

Hello

we are trying to find  a short implementation for the SVCHandler interrupt.
I want to do exactly like described here, but then a working version for the Cortex-m0:

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0203j/BABFGEFG.html

and here:

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0179b/ar01s02s07.html

we have no experts at assembly and the only thing we need is to get the svc number and arguments.
But if someone can explain me how the svc handler works I would be greatly helped.
And maybe someone can point me in the right direction on how to return values : "[font=Verdana, Tahoma, Arial, Helvetica, sans-serif][size=2]Any return value must also be passed back to the caller by modifying the stacked register values[/size][/font]".

Thank you in advance!

Martijn
  • Note: This was originally posted on 26th October 2012 at http://forums.arm.com

    Thank you for your response!!

    yes I do use KEIL MDK-ARM.
    when it try to compile it gives me an error:
    error: A1163E: Unknown opcode stacking_used_MSP , expecting opcode or Macro


    But just a moment ago it found this:


    SVC_Handler  PROC
            ;EXPORT  SVC_Handler         [WEAK]
    IMPORT SVC_Handler_main
    MOV R0,SP
    B SVC_Handler_main
            ENDP


    It works exactly how I want it to,but is this the right way to do this?

    The rest works great And i think I understand what is happening.
    Th only thing i do not really get is :
    svc_number = ((char *)svc_args[6])[-2];
    Does It takes  few bits from the link register?

    Thnx!
  • Note: This was originally posted on 26th October 2012 at http://forums.arm.com

    Thank you so much!

    This works great:


    ;--------- SVC HANDLER --------------
    SVC_Handler  PROC
            ;  EXPORT  SVC_Handler            [WEAK]
            IMPORT SVC_Handler_main
            MOVS  R0, #4
            MOV   R1, LR
            TST   R0, R1
            BEQ   stacking_used_MSP
            MRS   R0, PSP ; first parameter - stacking was using PSP
            LDR   R1, =SVC_Handler_main
            BX    R1
    stacking_used_MSP
            MRS   R0, MSP ; first parameter - stacking was using MSP
            LDR   R1, =SVC_Handler_main
            BX    R1
            ENDP
    ;--------- END SVC HANDLER --------------



    the compiler gave an error on __cpp

    And thank you for the explanation on the svc number!
  • Note: This was originally posted on 26th October 2012 at http://forums.arm.com

    Hi

    Look like a text formatting issue confused the MDK tool.
    Please try adding indentations as follows:

    // SVC handler - Assembly wrapper to extract
    //         stack frame starting address
    __asm void SVC_Handler(void)
    {
      MOVS   r0, #4
      MOV r1, LR
      TST r0, r1
      BEQ stacking_used_MSP
      MRS R0, PSP ; first parameter - stacking was using PSP
      LDR R1,=__cpp(SVC_Handler_main)
      BX  R1
    stacking_used_MSP
      MRS R0, MSP ; first parameter - stacking was using MSP
      LDR R1,=__cpp(SVC_Handler_main)
      BX  R1
    }


    The other solution you found works too, but only if your application always use MSP (Main Stack Pointer).

    The code (svc_number = ((char *)svc_args[6])[-2])

    svc_args[6] is the stacked PC. It is the address of the instruction after the SVC.

    The SVC instruction is located in the memory address [stacked_PC - 2], because SVC is a 2 byte instruction.

    The SVC number is the lower byte of the instruction.

    So if without the "[-2]", (char *)svc_args[6] read the lower byte of the instruction after the SVC.
    Adding the "[-2]", it handle that as array of bytes and therefore read the SVC number.

    Hope this is clearer.

    regards,
    Joseph
  • Note: This was originally posted on 26th October 2012 at http://forums.arm.com

    I see.
    My code is embedded inside C using embedded assembler feature inside C environment. That's why I use __cpp().
    In your case you are putting the code in a separated assembly file, in that case __cpp() should be removed.

    regards,
    Joseph