Question

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 ?

  • Keep starting with my __reset instruction
  • (Or at least make sure that my existing __reset and the int main(void) at least cooperate with each other)
  • Call his routines from the ASM code
  • Use the same data buffers that the C code sets up

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.

Était-ce utile?

La 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."

Autres conseils

Michael gave you a good answer. The only thing I would like to add is that you should make the project in C and cut the assembly functions within it.

This way you keep the speedy and functional asm code and can mantain the project in C, which is much easier.

It is not in your interest to convert the C code into assembly and have a large assembly code to mantain, its the other way around.

Once you read the docs you will see it is not so hard to use an assembly function in C, but to get you started, you can take a look at this:

C:\ ...bla bla... \Microchip\MPLAB C30\src\dsp\include\dsp.h

contains function declaration in C for the actual assembly functions located in this folder:

C:\ ...bla bla... \Microchip\MPLAB C30\src\dsp\asm

You can begin with the function _VectorAdd: Vector Addition, file "vadd.s"

Note that the assembly function _VectorAdd is defined as VectorAdd in the header file.

This example files are for the dsp engine in the DSPIC, something the PIC24 does not feature. But it is still ilustrative enough to extract the principle.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top