Can I run part of my sketch on flash memory?

Hello, I have three sketches I would like to run but switch between them. I know it will need a hard restart but I was wonder the parts in my sketch that is in the loop area be hosted in the flash storage and some way if switching between them when I need to if I can do this?

The stuff in the startup or define area doesn’t change. Problem I have in my UNO board doesn’t have enough space to host everything.

This is just a curious thought at the moment.

Joseph

Did you mean Uno board?

Your sketch runs in flash memory that is in the MCU. Hosting the program in some additional flash memory and pulling it in to run on the MCUs flash memory on demand sounds like something I would attempt. It sounds monumental, a very big task, so it would be very much easier to make sure your code fits in the onboard flash and use an appropriately sized MCU if necessary, such as develop your project on ATmega2560 or AVR128DB28 (or other MCUs that just have enough flash onboard).

The way this is typically handled is that you have the N codes as a unfiied code and a way to configure which one to pick.

Imagine you have 2 dip switches allowing you to select 1 in 4 codes.
Code0 would have setup0() and loop0() (and other functions if needed)
Code1 would have setup1() and loop1() (and other functions if needed)
Code2 would have setup2() and loop2() (and other functions if needed)
Code3 would have setup3() and loop3() (and other functions if needed)

now your setup() will check the dip switches status and select the appropriate version of the code and your loop will monitor if the dip switches change (in which case you reboot to the new code) or just run the selected loop

The code architecture would look something like that:

const byte dip1 = 2;
const byte dip2 = 3;

byte mode;
void (*selectedSetup)();
void (*selectedLoop)();

// ------------- PROGRAM #0 --------------
void setup0() {}
void loop0() {}

// ------------- PROGRAM #1 --------------
void setup1() {}
void loop1() {}

// ------------- PROGRAM #2 --------------
void setup2() {}
void loop2() {}

// ------------- PROGRAM #3 --------------
void setup3() {}
void loop3() {}


// ------------- MAIN PROGRAM  --------------
void restart() { asm volatile("jmp 0");}

byte readMode() {
  return ((digitalRead(dip1) == LOW) << 1) | (digitalRead(dip2) == LOW);
}

void checkModeChange() {
  byte m = readMode();
  if (m != mode) restart();
}

void setup() {
  pinMode(dip1, INPUT_PULLUP);
  pinMode(dip2, INPUT_PULLUP);
  // we select the right setup and loop based on the dip switch status 
  mode = readMode();
  switch (lastMode) { 
    case 0: selectedSetup = setup0; selectedLoop = loop0; break;
    case 1: selectedSetup = setup1; selectedLoop = loop1; break;
    case 2: selectedSetup = setup2; selectedLoop = loop2; break;
    case 3: selectedSetup = setup3; selectedLoop = loop3; break;
  }
  selectedSetup();
}

void loop() {
  checkModeChange();
  selectedLoop();
}

(typed here, mind typos)

J-M-L, you are talking about the approach that assumes there is enough room in onboard flash for the code, is that right?

yes - if all the codes don't fit in flash, then you have a problem and the easiest way to fix it is to get a more capable arduino :slight_smile: as you said, it's "a very big task"...

As @josephchrzempiec states that the same libraries and data structure are needed for all, there may be significant optimizations in just restructuring the 3 distinct codes. @josephchrzempiec You could expose the code here, to get tips on optimization that may not have been thought of.

If the codes MUST remain separate, for some unstated reason, of course this isn’t a solution.

It should be possible to replace the bootloader, so that code could be read from an external flash memory and programmed into the arduino on reboot when you need to change sketches. You would be limited by the maximum erase/write cycles of the internal flash.
A processor with more internal flash is definitely the simpler method.

Clasical UNO (atmega328) has 32kB Flash and 2kB RAM.

Clasical Mega (atmega2560) has 256kB Flash and 8kB RAM. So it can contain 8x more code than UNO.

I have project MHF-002 (Mega Home FORTH) | Hackaday.io where I use atmega2560 with external 512kB RAM and FORTH, which is enable to interpret code from RAM (and Flash) and with SD card can execute nearly unlimited amount of code (GigaBytes).

So ways are, but not exactly easy and simple.

@dmjlambert Sorry about that you are right. I wrote it from my phone i wrote this and small fingers on scree, Yes it is a uno board.

Yes it is from an external Flash Winbond storage.

@J-M-L I di not know you can have multiple sketches running thank you. Problem is even if I can run multiple sketches on the processor that in fact it exceed the limit of the space on the processor by 300%

@dmjlambert No I'm referring to using an external storage to hold part of the sketch. Which is in the loops. Current if i put all of it somehow on the mcu programming memory it exceed the limit by 300%. I need to put all the multiple loops area on the external memory.

There is no room or restructuring that is needed because of the mcu programming space exceed the limit by 300% and I'm trying to figure out how to do all this.

How close are you? Do you happen to have two Unos?

@david_2018 yes it is much more simple to run it all internal. However exceeds the amount of programming sketch then what the uno board has by over 300%. That is why I need to use an external memory to store part of the sketch externally.

Right. To rephrase, “I want to put 30 kilos of potatos in this bag, it will only hold 10. No, I won’t dehydrate the potatos, they need to remain untouched. How do I get the rest in the bag?”

You don’t. You change bags. Next topic? Seriously, you’re telling us you’ve written a code that is at least 3x the size of the memory available in the Uno, you’re not willing to expose the code, and you want a solution?

You must be trolling us. I’m out.

The hardware is already set. PCB is already made. There is a spot for a later use for a external storage if needed and looks like it is needed. I can not change this. I'm trying to figure out way to run or storage part of the sketch the loop area and below to the external flah.

This is not my project. I'm trying to have a help a friend of mine. He already has a PCB that is made the uno processor but there is a spot for a external flash that can be added. I can not change any of the hardware.

void setup() and void loop() are just functions. Sure, you can add as many functions as you have space for. You say you're at 3x the space of your board, if we could see your sketch posted here using code tags, there may be ways to get some of that into the larger flash memory space, such as using the F macro to move say Serial.print("some text"); to Serial.print(F("some text"));

Consider the following sketch

bool usesFMacro;

void setup() {
  Serial.begin(9600);
  usesFMacro = true;
  /*
Sketch uses 2194 bytes (6%) of program storage space. Maximum is 32256 bytes.
Global variables use 706 bytes (34%) of dynamic memory, leaving 1342 bytes for local variables. Maximum is 2048 bytes.
  */
  if (usesFMacro == false) noFmacro();
  /*
Sketch uses 2208 bytes (6%) of program storage space. Maximum is 32256 bytes.
Global variables use 188 bytes (9%) of dynamic memory, leaving 1860 bytes for local variables. Maximum is 2048 bytes.
  */
  else if (usesFMacro == true) fMacro();
}

void loop() {
  // put your main code here, to run repeatedly:
}

void noFmacro() {
  Serial.println("abcdefghijklmnopqrstuvwxyz0123456789");
  Serial.println("qwertyqbert");
  Serial.println("justalineofnonsense");
  Serial.println("inwinterwatchoutforicicles");
  Serial.println("santaclausiscomingtotown");
  Serial.println("Arduino is fun");
  Serial.println("spaces              count as characters, too");
  Serial.println("ontario");
  Serial.println("quebec");
  Serial.println("newfoundland");
  Serial.println("alberta");
  Serial.println("saskatchewan");
  Serial.println("british columbia");
  Serial.println("new brunswick");
  Serial.println("nova scotia");
  Serial.println("manitoba");
  Serial.println("prince edward island");
  Serial.println("Santa lives in Canada");
  Serial.println("His postal code is H0H 0H0");
  Serial.println("Yes, it's a real, assigned postal code");
  Serial.println("The official address is Santa Claus, North Pole, H0H 0H0, Canada");
  Serial.println("Santa's closest neighbors live in Alert, Nunavut, 817 km away");
}
void fMacro() {
  Serial.println(F("abcdefghijklmnopqrstuvwxyz0123456789"));
  Serial.println(F("qwertyqbert"));
  Serial.println(F("justalineofnonsense"));
  Serial.println(F("inwinterwatchoutforicicles"));
  Serial.println(F("santaclausiscomingtotown"));
  Serial.println(F("Arduino is fun"));
  Serial.println(F("spaces              count as characters, too"));
  Serial.println(F("ontario"));
  Serial.println(F("quebec"));
  Serial.println(F("newfoundland"));
  Serial.println(F("alberta"));
  Serial.println(F("saskatchewan"));
  Serial.println(F("british columbia"));
  Serial.println(F("new brunswick"));
  Serial.println(F("nova scotia"));
  Serial.println(F("manitoba"));
  Serial.println(F("prince edward island"));
  Serial.println(F("Santa lives in Canada"));
  Serial.println(F("His postal code is H0H 0H0"));
  Serial.println(F("Yes, it's a real, assigned postal code"));
  Serial.println(F("The official address is Santa Claus, North Pole, H0H 0H0, Canada"));
  Serial.println(F("Santa's closest neighbors live in Alert, Nunavut, 817 km away"));
}

the comments in setup were generated from the IDE when I switched the variable from false to true

I have not written anything. A Friend of mine I'm trying to help out. He has stuff he compiled somehow that would exceed the programming sketch of the uno processor.

@hallowed31 Not my project its a friend of mine. I don't know what is on the sketches or projects. I will ask him when he wakes up.