How can I solve this problem? 'NVMCTRL' was not declared in this scope

Hi, I am a student studying Arduino.

I need your help.

I'm trying to put the one on the link above on Arduino Mega 2560 with there's Wiki, but it's not working.

VSCode outputs an error 'NVMCTRL' was not decimated in this scope. & 'NVIC_SystemReset()' was not decimated in this scope.

How can I solve this problem?

Is this about IDE setup problems or else?

VSCode outputs an error 'NVMCTRL' was not decimated in this scope. & 'NVIC_SystemReset()' was not decimated in this scope.

That one has me stumped.

Can you post the code, and cut and paste the error message(s)?

--whole source code

/*
Copyright (c) 2017 Arduino LLC. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>

#include "InternalStorage.h"

#define PAGE_SIZE (64)
#define PAGES (4096)
#define MAX_FLASH (PAGE_SIZE * PAGES)
#define ROW_SIZE (PAGE_SIZE * 4)

#define SKETCH_START_ADDRESS (0x2000)
#define MAX_PARTIONED_SKETCH_SIZE ((MAX_FLASH - SKETCH_START_ADDRESS) / 2)
#define STORAGE_START_ADDRESS (SKETCH_START_ADDRESS + MAX_PARTIONED_SKETCH_SIZE)

extern "C" {
// these functions must be in RAM (.data) and NOT inlined
// as they erase and copy the sketch data in flash

attribute ((long_call, noinline, section (".data#")))
static void eraseFlash(int address, int length)
{
for (int i = 0; i < length; i += ROW_SIZE) {
NVMCTRL->ADDR.reg = ((uint32_t)(address + i)) / 2;
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_ER;

while (!NVMCTRL->INTFLAG.bit.READY);
}
}

attribute ((long_call, noinline, section (".data#")))
static void copyFlashAndReset(int dest, int src, int length)
{
uint32_t* d = (uint32_t*)dest;
uint32_t* s = (uint32_t*)src;

eraseFlash(dest, length);

for (int i = 0; i < length; i += 4) {
*d++ = *s++;

while (!NVMCTRL->INTFLAG.bit.READY);
}

NVIC_SystemReset();
}
}

int InternalStorageClass::open()
{
_writeIndex = 0;
_writeAddress = (uint32_t*)STORAGE_START_ADDRESS;

// enable auto page writes
NVMCTRL->CTRLB.bit.MANW = 0;

eraseFlash(STORAGE_START_ADDRESS, MAX_PARTIONED_SKETCH_SIZE);

return 1;
}

size_t InternalStorageClass::write(uint8_t b)
{
_addressData.u8[_writeIndex] = b;
_writeIndex++;

if (_writeIndex == 4) {
_writeIndex = 0;

*_writeAddress = _addressData.u32;

_writeAddress++;

while (!NVMCTRL->INTFLAG.bit.READY);
}

return 1;
}

void InternalStorageClass::close()
{
while ((int)_writeAddress % PAGE_SIZE) {
write(0xff);
}
}

void InternalStorageClass::clear()
{
}

void InternalStorageClass::apply()
{
// disable interrupts, as vector table will be erase during flash sequence
noInterrupts();

copyFlashAndReset(SKETCH_START_ADDRESS, STORAGE_START_ADDRESS, MAX_PARTIONED_SKETCH_SIZE);
}

long InternalStorageClass::maxSize()
{
return MAX_PARTIONED_SKETCH_SIZE;
}

InternalStorageClass InternalStorage;

-error code

identifier "NVMCTRL" is undefined [40,7]
identifier "NVMCTRL_CTRLA_CMDEX_KEY" is undefined [41,28]
identifier "NVMCTRL_CTRLA_CMD_ER" is undefined [41, 54]
identifier "NVMCTRL" is undefined [58, 15]
identifier "NVIC_SystemReset" is undefined [61,5]
identifier "NVMCTRL" is undefined [71, 3]
identifier "NVMCTRL" is undefined [90, 13]

AVR-based Mega 2560 does not have an NVIC. You need an ARM-based platform for that.

Then How can I change that code??

Is there any other way?

I'm not optimistic about your chances of porting this to a Mega.

Thanks. So you're saying we should use an arm processor-based board?
ex) aduino zero or Adafruit Feather M0 WiFi Board etc.

Seems like that's what the author intended:

Finally, what's replacing NVIC_SystemReset and NVMCTRL command on avr-based board? If you know, I'd like to change it. If not, I'll change the board. Thank you.

NVIC is the ARM's interrupt controller. You can start reading about it here: Nested Vectored Interrupt Controller (NVIC). A Google search will provide many hits.

The ARV processors use a much simpler interrupt structure. You can read about it in the ATmega2560 Datasheet