cassemblyintegrationpic24

MpLab, ASM, C, Building To accommodate both


I have a large and substantial ASM project for a PIC24 chip. (The specific chip is the PIC24FJ256GB210)

I now have some other routines in C.

I want to incorporate these into my project.

The C routines are in a project of 5 or so files, one of which contains the int main(void) statement as the starting point. This was for the purpose of testing them and giving us the confidence that they work. We are now ready to move that code and incorporate it into the larger existing system.

The assembly language stuff starts with the __reset: instruction.

How do I arrange the project and build options so that I can do these next three things ?

Interestingly enough, Microchip's User forums and sample code sections seem to miss this idea (or, more likely, I haven't figured out how to find them).

I would think this question has been asked a lot, and I hope I'm not duplicating a previous question, but I don't see it here nor on MicroChip's site. Links to helpful websites on this topic are welcome.

If I just need to learn how to search this and other sites better, that will be a useful and workable answer in and of itself. Again, hope I'm not asking a duplicate question.


Solution

  • I recommend you to read DS51284H ("MPLAB® C COMPILER FOR PIC24 MCUs AND dsPIC® DSCs USER’S GUIDE") (PDF).

    In particular see section 4.4 STARTUP AND INITIALIZATION

    "Two C run-time startup modules are included in the libpic30.a archive/library. The entry point for both startup modules is __reset. The linker scripts construct a GOTO __reset instruction at location 0 in program memory, which transfers control upon device reset.
    ....
    5. The function main is called with no parameters."

    Your __reset label and the one in the CRT (C run-time) would appear to conflict. If you have the source for the CRT you could change that by renaming the __reset label in the CRT to something else so that your own __reset always is called first.
    Another point is that it sounds like you want to take a stand-alone program and use it as a library from within your own program. Since stand-alone programs often are designed to perform one or more specific tasks and exit once that task is finished you might want to refactor your C code a bit to make it more library-ish (like getting rid of the main() function and perhaps replace it with some sort of init() function).


    And section 4.11 FUNCTION CALL CONVENTIONS.

    "The first eight working registers (W0-W7) are used for function parameters. Parameters are allocated to registers in left-to-right order, and a parameter is assigned to the first available register that is suitably aligned.
    ....
    Function return values are returned in W0 for 8- or 16-bit scalars, W1:W0 for 32-bit scalars, and W3:W2:W1:W0 for 64-bit scalars."