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

sd_flash_write implementation without Softdevice

Hello,

I am starting to create a bootloader and I want it to be independant of the SoftDevice. Is there any open source implementation of a flash_write function that I could use? I've noticed that the official bootloader is using sd_mbr_command to initiate the copy process with the help of the softdevice. How is this copy process implemented?

  • Does it begin from the lowest page of the source and does it copy pagewise? E.g. what happens when the image starts at page 100, is 50 pages long and should be moved to page 80.

Thanks, Marius

  • Hello, attached some simple code I've made earlier that accesses the NVMC directly (blocking). This code can be used when the softdevice is disabled. Note that there is no memory alignment checks implemented here, so make sure to only used word aligned addresses when doing writes, and page aligned addresses when erasing.

    The MBR does not support the scenario you describe. Flash pages has to be erased before storing any new data, which is not the case when reaching the 100th page.The point of having the MBR is to allow the bootlaoder to safely update itself and the SD (SDK bootloader depends on SD to perform DFU). It is not used for application update.

  • Thank you a lot, that saved me some time :-)

    I've rewritten both functions and I was wondering why you enable and disable the write / erase mode after each write / page erase. I've checked if I can simply enable the controller, then do multiple writes and disable it again and it works. I've also read through the manual and it doesn't say that we have to re-enable it again after each write. Is there anything I overlooked?

    Also, the manual does not say that we have to check the READY register after enabling write mode, it only says it's busy when ERASE or WRITE is going on, why ist that?

  • Here's my code that I wrote based on @Vidar's Response:

    void eraseFlashPage(u32 pageNumber, u32 numPages){
    
    	// Turn on flash erase enable and wait until the NVMC is ready:
    	NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);
    
    	//Wait until the Non volatile memory controller is ready for erasing
    	while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
    
    	while(numPages > 0){
    
    		NRF_NVMC->ERASEPAGE = (uint32_t)((pageNumber+numPages-1) * NRF_FICR->CODEPAGESIZE);
    
    		//Wait until ready
    		while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
    
    		numPages--;
    	}
    
    	//Turn of flash erase
    	NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
    
    	//Wait until ready
    	while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
    }
    
    void copyFlashData(u32* destinationAddress, u32* sourceAddress, u32 size)
    {
        static u32 buffer  __attribute__((aligned (4)));
        uint8_t cnt = 0;
    
        // Turn on flash write enable and wait until the NVMC is ready:
        NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
    
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
    
        //Copy all data
        for(u32 i=0; i < size/4; i++){
        	buffer = *(sourceAddress + i);
    
        	*(destinationAddress + i) = buffer;
    
        	while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
    
        }
    
        // Turn off flash write enable and wait until the NVMC is ready:
        NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
    
    	while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
    }
    
  • It is not required to disable write in-between write operations, but it is important that erase and write is not enabled at the same time as it may result in unpredictable behavior as mentioned in chapter 6 of the nR51 RM. It also recommends to disable write/erase when not actively in use.

    *Also, the manual does not say that we have to check the READY register after enabling write mode, it only says it's busy when ERASE or WRITE is going on, why ist that? * This is not a requirement as far as I can tell. Tried to debug the code, and NVMC seems to be ready as soon as erase/write is enabled.

  • And one more thing: If SoftDevice protection is enabled, that means that the softDevice is in Code Region 0 and my app and the bootloader are in code region one. How will I be able to erase the softdevice?

Related