This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

flash_page_erase() related crash

Hi,

I'm writing a bootloader for nrf51822 to be able to update the nRF51's firmware through SPI (data transferred via an STM32 retrieving the file through USB).

My idea is to place a very basic bootloader at @0x0 through 0x37FF, and the firmware starting at 0x3800 to 0x3FFFF.

I want to keep memory usage as low as possible and to have the possibility to upgrade softDevice later. That's why I don't want softdevice in the bootloader.

So I wrote a program with SPI slave communication working, and as long as I am debugging step by step erasing and programming are ok too. But I'm stuck here because the device crashes when I run the erasing procedure without breakpoints. No hint on the type of error, because Keil IDE is exiting debug mode when the problem sparks. I tryied to add a wait loop for 1,2,and even 10 seconds between each page erase, but the behaviour is the same.

It can be tested with just these few lines.

void nrf_nvmc_page_erase(uint32_t address) //function from nrf_nvmc.c
{ 
	int i;
  // Enable erase.
  NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
  while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
  {
  }
  // Erase the page
  NRF_NVMC->ERASEPAGE = address;
  while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
  {
  }
  NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
  while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
  {
  }
}

#define FIRMWARE_START	0x3800
#define FIRMWARE_END	0x3FFFF 
#define PAGE_SIZE		0x400

int main(void)
{
   uint32_t addressToProg;
   for(addressToProg=FIRMWARE_START;addressToProg<FIRMWARE_END;addressToProg+=PAGE_SIZE)
	{
		flash_page_erase(addressToProg); //if breakpoint is placed here, it's fine. If not, it crashes
	}
}

Do you have any clue on the problem ? Am I doing anything wrong ? I'm pretty sure it's something stupid but I'm really running out of time so any help would be deeply appreciated.

Related