I'm designing a lightweight circuit, but the ATtiny85 micro-controller I'm using to power it doesn't have nearly enough flash memory to store the code for the devices I'll have hooked up to it (It has 8kb of flash). I was wondering if it'd be possible to have the ATtiny use compiled code from an external flash memory chip of larger capacity elsewhere on the board like its own internal memory. Thanks in advance.
3 Answers
No, it's not possible.
AVR chips can only execute code from their internal flash.
-
Although you can put a VM in the internal flash and then run bytecode from anywhere. Oct 24, 2017 at 22:31
-
Well, yes, but only if you're a complete psychopath. Talk about resource wastage on an already resource starved chip...– MajenkoOct 24, 2017 at 23:32
Although you cannot run code from external (S)RAM, you can:
- optimize your code (reducing the number of lines of code by):
- use parameterized functions instead of copying/paste code
- reduce the type (storage) variables (like constants) which are kept in memory, e.g. instead of 200 integers of 2/4 bytes use 200 bytes
- instead of storing const calculated array values, calculate them runtime (causing a performance reduction)
- remove unused libraries
- strip down used libraries (not needed to remove unused functions)
You could write a small bootloader and load code segments when needed. The internal flash can be written at least 10,000 times so you cannot swap segments too often.
To do this you need to 1) write a bootloader that can load segments from the external flash, 2) partition your sketch into segments, 3) compile and link them to the same address space in program memory, 4) write the segments to the external flash memory.
Extra is handling segment to segment calls as special care is needed by the loader.
An even more elaborate scheme is to allow multiple modules and swap them depending on the execution path (e.g. module to module calls). Segments would be built up of modules, and modules would be reusable between segments. What modules are in memory would depend on some "statistics". This is basically what the instruction cache does in modern processors.
Typical segments could be application level sub-functions such as printing, uploading measurements, etc. Typically functions that are not used very frequently, and where the "background" application is stopped, e.g. data collect.