1

I want to create a small single threaded os for my arduino.The first thing I want to do is to allow my arduino to read a section from flash/eeprom and execute it. I have some question regarding the memory. The microcontroller is executing one command at a time from..where? Does it directly acces the flash containing the code and take every command at a time?(that would be slow) Or does it first load the entire program into sram?(also wouldn't make sense because sram is 2k while flash is 32k). Either way,if I can find in sram /flash my code( or the header to some void) then i could jump there.Am i wrong?Is it possible(to do such os)?

Something like this:

uint16_t addr=0x1234;
void (*function)(void)=addr;
function();

(or maybe with some avr like assembly code?)

1 Answer 1

7

The microcontroller is executing one command at a time from..where?

Flash memory directly. The AVR core can only execute instructions from Flash memory. It uses a two-bus system known as Harvard Architecture, where the Instruction Bus is connected to Flash and is used to execute instructions, and the Data Bus is connected to SRAM, EEPROM, all the peripherals, GPIO pins, etc.

It is possible to jump to any location in memory in a similar way as you have depicted, but in order to know where you first need to know the functions you want to call. You can get the address of any function and assign it to a pointer, and then call that pointer as a function, but what would that achieve? You may as well just call the function directly and save time. Unless you want to store the addresses of those functions in an array or something to maybe associate them with a command to trigger the execution of that function. In which case you don't need anything "fancy", just pointers to the functions you want to use.

4
  • I was thinking i could execute from sd card. Just find an empty sector in flash...write there the code from sd and then jump. I believe then this is possible right? Apr 10, 2017 at 18:48
  • 1
    No, you can't. You can only write to the flash memory from code executed from the bootloader portion of flash (which means the bootloader has to do it), so you can't write to flash from your program. Also you'd wear out your flash pretty quick.
    – Majenko
    Apr 10, 2017 at 18:50
  • Basically you are going from Arduino to AVR :) (Still can use the same HW+IDE+such, but you overgrown Arduino's limits) Next step should be download datasheat for your AVR to study and have it handy and read it (not need to understand it yet (too much info), but to have some idea, what are you going to play with)
    – gilhad
    Apr 11, 2017 at 2:53
  • @Majenko: "...what would that achieve?..." Actually, its a game-changer, as can be seen using 3-instruction Forth approach discussed here. The key is to get the bootloaded code to do it (remote store, fetch, and call instructions), but requires a generic header/hook/container into which machine code can be injected dynamically. Jun 17, 2020 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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