Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

Wondering if anyone has tried running an entire program on a PIC32 out of RAM? How would one go about doing that. I understand that you can use the ramfunc keyword to move certain functions to RAM however they still require the copy to be on Flash.

What I would like to do is have a loader program on the PIC32 that can accept a hex file over the serial port etc and rather than writing it to flash have it run out of ram directly.

Ideally I was hoping that by modifying the default linker script you could target a program to run from RAM alternatively if anyone knows how we would patch up a program that was linked to run from flash to run from RAM that could work too

Thanks Mike

share|improve this question
I don't know about PIC tools, but some other IDEs (like CrossStudio for ARM) allow you to choose very easily where to place your code, either in flash or RAM, for debugging purposes. – Telaclavo May 2 '12 at 18:59
This is a great idea! – Joel B May 3 '12 at 5:33

3 Answers

Both the longramfunc and ramfunc attributes place the function in a special area of flash that will be copied to RAM by the startup code. You don't want to use them.

For functions which will be called from flash, you want to use the longcall attribute which specifies that a 32-bit call (JALR) is needed instead of a 28-bit call (JAL), since flash and RAM are located in separate 512MB segments.

You will also need to set up a custom linker file with an executable RAM section, and include that section name on every function that is placed there.

The longramfunc macro also includes the longcall attribute, whereas the ramfunc macro does not, and is intended for calls from one RAM function to another. In your case, for functions called by other RAM functions, you do not want to use the ramfunc attribute because it sets up the copy from flash.

Since you will be copying the functions yourself (e.g. off of an SD card), you will need to include your own loader that interprets the hex code and copies the data to flash as needed.

share|improve this answer
Would it be possible to load a program into a fixed area in ram and just do a blind jump into the address zero of that range, assuming the hex file will have valid code at that point? – CMP May 2 '12 at 22:52
@CMP, yes you could do that by writing the jump in PIC32 assembly. I think it would be easier though to create a dummy function which would establish the linkage, and then it would be overwritten by the code actually loaded into RAM. – tcrosley May 2 '12 at 22:59

I don't believe any of the pic32 support executing from external memory. I have brainstormed this question before (see this question) and it is generally a pain. There are some pics that support this, but most that I know of are 8-bit (PIC18F85J5 is one).

Generally if you need more program space, get a bigger microcontroller.

If you want to be able to update the code more frequently than you want to program the device, you can write a sort of bootloader that can copy bytes from an external source into the flash memory.

If you want to load and run a hex file it sounds like you want a bootloader. I could not find a good enough case to go in any other direction. The only real alternative I can think of is writing some kind of interpreter to run a program from ram, but that would probably introduce more overhead than you want.

share|improve this answer
1  
I thought he was talking about internal RAM, rather than external memory. I guess he wants to frequently d-load functions into the MCU and execute them, but doesn't want to wear out the FLASH. – Rocketmagnet May 2 '12 at 22:11
I think I stand corrected. Check out this sample application: ww1.microchip.com/downloads/en/DeviceDoc/… – CMP May 2 '12 at 22:46
@CMP, however that example uses the longramfunc and ramfunc macros which place the code in flash and copy it to RAM at startup -- which the OP is trying to get around. See my answer. However it is a valid example of the PIC32 executing from RAM. – tcrosley May 2 '12 at 22:49

You have three basic solutions to your problem of wanting to download programs to run after flashing the part.

  1. Use an interpreted language like Lua, Basic, or Forth. Write (or, more likely, just port) an existing virtual machine that interprets text instructions rather than machine code, and download the program as text. You have to reimplement your program in the interpreted language, but that's quicker and easier (in Lua especially) than writing it in C.
  2. Just get some extra storage and reflash the whole part. You haven't described what you're doing, so I can't know if this is applicable, but it's easy enough to add a serial Flash IC and/or microSD card with plenty of storage to your board and have the bootloader select a profile to run. At a Flash lifetime of 10,000 rewrites, this gives you 27 years of once-per-day reflashes.
  3. Do what you originally intended. This will require some careful programming (see tcrosley's answer for some methods required) and some fancy linker scripts. Good luck.

Largely adapted from my answer to 'Compiling code to run from external RAM'; a similar problem.

share|improve this answer
I've looked at using Lua on a micro. It's possible, but you do need a lot of RAM. I think they recommend something like at least 256k. – Rocketmagnet May 3 '12 at 7:30
@Rocketmagnet - The recommendation is 64k, which is pretty common on the Cortex-M3, ARM7TDMI, and AVR32 platforms that eLua targets. It's also available on the PIC32 (but there's no port for PIC32 yet). It can be fit into 32k, but that doesn't leave you a lot of room for programs - remember that each character in a program is a byte. The actual RAM requirements for the standard libraries that load at startup are about 17k, but that can be reduced to less than 6k with the Lua Tiny RAM patch. – Kevin Vermeer May 3 '12 at 14:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.