Failing to unlock flash region on SAML21

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I want to save some data to a specific address in flash memory using the NVM module from ASF. I found out the region this address is located in is locked.

According to the ASF documentation I should be able to execute an UNLOCK REGION command to unlock the region.

I have tried execute this command and I do get STATUS_OK back. I then wait for the NVM module to finish executing this command by check nvm_is_ready() in a loop.

When this loop exits the command should have been executed and I believe the region should then be unlocked.

But when I check nvm_is_page_locked() I still get true for that address.

Am I missing some other instruction I should use to unlock this region?

 

nvm_get_config_defaults(&m_int_flash_config);
m_int_flash_config.manual_page_write = false;
ret_status_code = nvm_set_config(&m_int_flash_config);

ret_status_code = nvm_execute_command(NVM_COMMAND_UNLOCK_REGION, 0x4000, 0);
	
// wait for command to finish executing
bool isready = false;
while (!isready) {
	isready = nvm_is_ready();
}
volatile uint16_t temp = NVMCTRL->LOCK.reg; // All 1, so still locked?

if (!nvm_is_page_locked(0x4000)) {
    // The code for erasing and writing   
    // This code is not executed because the page is still locked
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I managed to fix it. I misread the documentation for nvm_is_page_locked(). Replacing nvm_is_page_locked(0x4000) with nvm_is_page_locked(0x4000/FLASH_PAGE_SIZE) fixed it.