4

In STM32 microcontrollers it is possible to use a technique that ST calls IAP, or In-Application Programming, in order to write to the Flash memory from your running application. They even have a nice Application Note (ex: AN2594) to describe a really ingenious way to use two Flash memory pages to emulate EEPROM while using "wear-leveling" to reduce Flash memory wear during erase cycles.

I have wondered for years: can AVR do this? Ex: the ATmega328 mcu in the Arduino Uno, Nano, etc--can you write code to store data from your application into Flash memory, thereby increasing non-volatile storage space for data acquisition for instance?

If so, can you please point me to some resources or examples, including example code, reference manuals or datasheets, etc, that might help me implement this? I've done some looking in the datasheet and haven't found anything so far but maybe I'm just not looking hard enough or know well enough what I'm looking for.

3 Answers 3

5

Yes you can. This Arduino IDE add on lets you make calls to code store in the bootload of memory so you can make changes to the flash memory. https://majek.sh/en/writing-to-internal-flash-on-arduino/

The number of times you rewrite a cell are limited. I'd suggest you are better off with adding a FRAM memory chip. Get a 5V part with SPI interface. Fast reads, virtually unlimitless writes (trillions) https://www.digikey.com/products/en/integrated-circuits-ics/memory/774?k=fram+memory&k=&pkeyword=fram+memory&pv276=15&FV=ffe00306%2C1f140000%2C1fec000b&quantity=0&ColumnSort=0&page=1&stock=1&pageSize=25

5

The answer is "yes, you can use extra AVR Flash memory as non-volatile EEPROM-like Flash memory storage, very similar to an STM32 microcontroller."

One of these days I'm going to write an Arduino library to make this really easy for the beginner. For now, however, I'm going to simply post links to valuable references that contain the full answers and that I still need to thoroughly study myself.

Documentation on how to implement Flash memory reading/writing:

-This is frequently called "self programming" by Atmel (now owned by Microchip).

To find these resources I went to the main ATmega328 reference page then clicked the "Documentation" link, then I searched the web page for the word "flash".

Here's what I found (in order of importance--most pertinent references first):

  1. AVR106: C functions for reading and writing to Flash memory on tinyAVR and megaAVR devices
    1. "Description: This application note provides C functions for accessing the Flash memory using the Self programming Program memory which allows to reprogram the Flash memory during program run."
    2. "Introduction: The Atmel® AVR® devices have a feature called Self programming Program memory. This feature enables an AVR device to reprogram the Flash memory while executing the program. Such a feature is helpful for applications that must self-update firmware or store parameters in Flash. This application note provides the details about the C functions for accessing the Flash memory."
    3. This Application Note contains source code, including with the following ready-to-use C functions:
      • ReadFlashByte()
      • ReadFlashPage()
      • WriteFlashByte()
      • WriteFlashPage()
      • RecoverFlash()
  2. AVR105: Power efficient high endurance parameter storage in tinyAVR and megaAVR devices Flash memory
  3. AVR116: Wear Leveling on DataFlash
  4. Atmega328 datasheet
    • Section 30: Boot Loader Support – Read-While-Write Self-programming (BTLDR), p. 328-344.
    • As pointed out by user SDF in his answer, especially pay attention to Section 30.8: Self-Programming the Flash, p. 334-342.
    • Also see Section 12.2: In-System Reprogrammable Flash Program Memory, p36
    • And Section 31.6: Page Size, p. 349.
1

Take a look at section 30.8 in the datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf

It has all the information to reprogram the bootloader section of the flash during runtime.

The bootloader partition size can be adjusted as well.

2
  • I'll take a look. What about the rest of the Flash? STM32 allows you to use any part of the flash to write in during runtime, rather than limiting you to one dedicated part of Flash. Commented Mar 29, 2018 at 4:56
  • In STM32 You can even move your program to RAM and then erase the Flash you just copied from, for instance--not that I'd want to do that though, it just demonstrates the versatility I'm seeking to find out about for AVR. Commented Mar 29, 2018 at 5:03

Your Answer

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

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