0

I know this fits close to the "stupid-question" category, but I've been researching how to execute machine code after bootloading on AVR 8-bit and have come to an understanding that the Harvard Architecture that is used on the AVR 8-bit MCUs makes it impossible to execute code from anywhere other than flash. How then is it possible to introduce new executable code at runtime using inline asm?

JSON
  • 1,819
  • 20
  • 27

2 Answers2

7

You are mixing up three things:

  1. Inline assembly

    Inline assembly is used to pass assembler instructions to the C (or whatever language) compiler. The compiler will add that assembler instructions to the code it produces. In the end the inline assembler instructions are stored the same way as instructions that have been generated by the compiler. If you write the program to the flash memory the inline instructions will also be located in flash memory.

  2. Boot loader

    The boot loader typically will read data from some input (such as an USB interface) and write the data into the flash memory. So the program sent to the AVR will later be executed from flash memory, not from RAM.

  3. Executing code from RAM

    Many processors support that. Also many boot loaders (for other microcontrollers) allow loading code into the RAM instead of flash and executing the code from there. You are right: At least most (maybe all??) AVR 8-bit microcontrollers do not support this!

Community
  • 1
  • 1
Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • 1
    More properly, the assembler-output machine code from the inline-asm instructions will be written to flash. But yes, the key point is as you say: inline asm mixes with compiler-generated asm, under control of the constraints you write, at compile time. – Peter Cordes Oct 19 '17 at 07:07
  • 1
    To my knowledge, there's no AVR that would be able to execute code from RAM (All of the instruction bus part goes towards internal Flash memory and is not even accessible externally). So, your "maybe" in the last paragraph is a "definitely". – tofro Oct 19 '17 at 12:33
0

... the Harvard Architecture that is used on the AVR 8-bit MCUs makes it impossible to execute code from anywhere other than flash.

Your understanding is correct, code can be only excecuted from flash.

How then is it possible to introduce new executable code at runtime using inline asm?

You can write code which writes/reads the Flash memory as plain data. In fact this is exactly what is done by any bootloaders. Even if there is no explicit "boot loader section", such as in the attiny family, accessing the flash in this way is possible. The datasheet of your part (page 263 in this example) contains a chapter for this topic, I'd suggest reading it in detail.

That said, I've not seen code which utilizes this function for running additional yet. The additional complexity might make it infeasible for a small, low power microcontroller like the AVR.

nqtronix
  • 463
  • 3
  • 14
  • Will this code be executable? Other processors require executable instructions to be in a memory block with executable privilege (not sure about AVR). My understanding is that AVR makes executable memory blocks read only. My specific understanding is that, depending on booted, such blocks can either be writable but not executable OR executable but not writable – JSON Jul 21 '18 at 22:56
  • @JSON I haven't used this function myself, but there are bootloaders for even the smallest AVRs, such as TinySafeBoot: http://jtxp.org/tech/tinysafeboot_en.htm, which write instructions to the flash at runtime. – nqtronix Jul 23 '18 at 04:52