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;

(or maybe with some avr like assembly code?)


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.

| improve this answer | |
  • 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? – sergiu reznicencu Apr 10 '17 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 '17 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 '17 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. – Assad Ebrahim Jun 17 at 11:46

Something like this:

you are basically asking for function pointers. easily done like this, on a pic24 but applies to pretty much anything.

static void (* _isr_ptr)(void)=empty_handler;               //tmr1_ptr pointing to empty_handler by default

void _ISR _T1Interrupt(void) {
    _T1IF=0;                            //clear tmr1 interrupt flag
    _isr_ptr();             //execute user tmr1 isr

//activate the isr handler
void tmr1_act(void (*isr_ptr)(void)) {
    _isr_ptr=isr_ptr;           //activate the isr handler

the ISR executes _isr_ptr(), which is hooked to a user routine via tmr1_act().

the only difference is that this approach doesn't require knowing the fixed address. instead, it is provided for in tmr1_act() through isr_ptr, the function to be pointed to by _isr_ptr.

but the basic concept is the same.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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