Is there any other way to write to Flash memory other than PROGMEM?

83 Views Asked by At

I am using Attiny1616 with Microchip Studio.

I want to store many results obtained from the code calculations in FLASH memory.

I found that the PROGMEM, which preserves constants, does not enable me to save them.

Next, I looked up NVMCTRL, which was listed in the datasheet, and I made the C code shown below.

#include <avr/io.h>

int main(void)
{
    while (1)
    {       
        while(NVMCTRL.STATUS & 0b00000001);     

        CCP = 0x9d;
        NVMCTRL.CTRLA = 0x06;
        
        NVMCTRL_ADDR = 0x8E00;
        NVMCTRL.DATA = 0xABCD;      
        CCP = 0x9d;
        NVMCTRL.CTRLA = 0x04;
    }
}

However, I could not understand the page buffer and could not understand how to write to FLASH.

Please tell me how to write the calculation results of the code to FLASH. Or please share the URL or code that describes how to do so.

I do not care about the write time.

Thank you in advance.

1

There are 1 best solutions below

0
Rev On

If your AVR device has a "Boot Flash Section" (seems to be true for Attiny1616), writing to the Flash memory from the "Application Flash Section" is not possible. In case of a firmware update, you would normally buffer the new firmware in an external flash, reboot the device and then write the application data from there.

Take a look at AVR106: C Functions for Reading and Writing to Flash Memory and AVR109: Self Programming.

On devices with boot block, the SPM instruction has the ability to write to the entire Flash memory, but can only be executed from the Boot section. Executing SPM from the Application section will have no effect. On the smaller devices that do not have a boot block, the SPM instruction can be executed from the entire flash memory.

So for you use case, depending on the amount of data, you should consider using the EEPROM or an external memory.