Code from SD card

Hey,
is there a way to run code from my sd card? not as a bootloader, just run the code.
I want to make something that you can select a file and run it so the bootloader will run the main code (as always) and later on i will load another code to run.
How can i do it?
Thank you for your help

Not without huge modification to the bootloader.

Mark

MyNick:
Hey,
is there a way to run code from my sd card? not as a bootloader, just run the code.
I want to make something that you can select a file and run it so the bootloader will run the main code (as always) and later on i will load another code to run.
How can i do it?
Thank you for your help

Unfortunately, Arduino isn't the correct platform for this. Raspberry Pi might work as it actually uses a full-fledged operating system.

In order for a processor to execute code it needs to have the code within it's address space. On regular computers, this is done by loading the program code from the storage medium (floppy or hard disk, SD card, Thumb drive, etc) into memory. While you could load a file from a SD card into memory of the Arduino, unfortunately you couldn't actually execute it because of the way the processor architecture used by the Atmel processors on Arduinos. The Modified Harvard architecture separates RAM (where you could load a file from the SD card) from program-space memory. You can't even aim the program pointer (what the processor uses to know where the next command to execute is) into RAM.

Theoretically it could be possible to modify the bootloader (or create a new bootloader) to copy a file from a SD card to RAM and then copy from RAM to program space, but that is way above my coding skills. I couldn't even tell you if there is enough memory on the Atmel processors to do this and have enough space in the program space (yes, the bootloader takes up program space) to be able to do much.

holmes4:
Not without huge modification to the bootloader.

Mark

I'm not sure that you can make the needed modifications to the bootloader to include the compiler and linker.

I'm going to show my age here. I used this technique "back in the old days" using a IBM PC XT. Memory was scarce (640K), so I used compiled code loaded into a memory buffer and executed there, but it had to be compiled and linked to use in that memory section before loading into the XT memory. And that was loading into RAM, not program memory (EEPROM?) like an Uno or Mega.

It would be a real trick to load a new section of code into program memory.

AVR won't run code from RAM.

The way around this problem is that Program = Code + Data.

If you write the Code in parts that the Data then feeds and controls the order they run, the Data can mean more to the execution than the Code itself. To that end, things like pointers to functions/data and tables as well as use of state machine(s) can be some of your best tools.

An easy to understand example would be to code an interpreter. Your data will be interpreter code that you will write and debug as a program. Just don't expect extreme speed except for specially coded functions which is perfectly acceptable in most cases.

It depends on what you want to do, I read the question as is it possible to put the hex file that avrdude uploads on an Sd card and then loaded (to progmem) from there. So "all" that's required is to add the Sd lib to the bootloader and rewrite the rest of the bottloader to that it reads from the SD card and not the Serial link. As this would change the size to the bootloader the compiler would need to given a different start address. But no change to the compiler/link code.

If of course your thinking about some kind of run time relocation/multi tasking thing then you should add a loog list of hardware changes eg support for context switching, room for a minimum "os" of some kind to deal with serial so that it can be shared .....

Mark

SurferTim:
I'm going to show my age here. I used this technique "back in the old days" using a IBM PC XT. Memory was scarce (640K), so I used compiled code loaded into a memory buffer and executed there, but it had to be compiled and linked to use in that memory section before loading into the XT memory. And that was loading into RAM, not program memory (EEPROM?) like an Uno or Mega.

It would be a real trick to load a new section of code into program memory.

In the DOS and CP/M environments you could load and run resident programs. IIRC, (it's been a while) those systems ran on that. I remember breaking into the CP/M docs and seeing that kind of thing in 1981. After being stuck with an interpreter BASIC (MICROPOLIS, came with the floppy drives for the company machines) for months it was like the whole world opening up!

I have a thin Intel 8047 book that lays that approach out as SOP. IIRC the 8080 book did too. A lot of how I structure state machines is based on what I learned from those.

@goforsmoke - do TSR (terminate and stay resident) in which case most of windows (it dos 3.0 really ) still uses them.

Mark

holmes4:
It depends on what you want to do, I read the question as is it possible to put the hex file that avrdude uploads on an Sd card and then loaded (to progmem) from there. So "all" that's required is to add the Sd lib to the bootloader and rewrite the rest of the bottloader to that it reads from the SD card and not the Serial link. As this would change the size to the bootloader the compiler would need to given a different start address. But no change to the compiler/link code.

If of course your thinking about some kind of run time relocation/multi tasking thing then you should add a loog list of hardware changes eg support for context switching, room for a minimum "os" of some kind to deal with serial so that it can be shared .....

Mark

I don't think that you can get there from here. The Program Counter points to flash and bootloaders available do not provide runtime write to flash.

There are different FORTH's for AVR's that do write to flash, allowing dynamic code and data extension but they don't use a bootloader. It's likely that at least one other system does the same.

SD data has to be read into RAM to use. AVR can't run RAM directly.

@goforsmoke - do TSR (terminate and stay resident) in which case most of windows (it dos 3.0 really ) still uses them.

Mark

Did they ever get around all the CP/M sections and drivers that Gate's "dirty man" (aka pirate hacker) stole?
I thought they did by going over to what they lifted from Unix.

It begs the old question, did Micro$loth ever invent (as opposed to steal and modify) anything that wasn't a flop?
Like the bumper sticker said:
Some day Microsoft will make a product that doesn't suck.
That will be the day they make vacuum cleaners.

This alone made getting up this morning worthwhile!

Like the bumper sticker said:
Some day Microsoft will make a product that doesn't suck.
That will be the day they make vacuum cleaners.

Of course, M$ will patent sucky things.

Ray

MyNick:
Hey,
is there a way to run code from my sd card? not as a bootloader, just run the code.
I want to make something that you can select a file and run it so the bootloader will run the main code (as always) and later on i will load another code to run.
How can i do it?
Thank you for your help

You may wish to investigate http://bitlash.net which is a BASIC interpreter callable of running a program from SD. If your need is simple, maybe inspecting a few pins and setting others, such a setup may be satisfactory.

Ray

mrburnette:

MyNick:
Hey,
is there a way to run code from my sd card? not as a bootloader, just run the code.
I want to make something that you can select a file and run it so the bootloader will run the main code (as always) and later on i will load another code to run.
How can i do it?
Thank you for your help

You may wish to investigate http://bitlash.net which is a BASIC interpreter callable of running a program from SD. If your need is simple, maybe inspecting a few pins and setting others, such a setup may be satisfactory.

Ray

Or even simpler things like running business/accounting apps. ]:slight_smile: