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

Enabling access port protection from code

Hello,

How reset device from code to take effect of enabling access port protection? NVIC_SystemReset() doesn't work. I can read flash using j-link.

Device: nRF52832

Parents
  • Hello,

    How do you enable the readback protection?

    The two following ways seems to work:

    You can set it as a const in e.g. your main.c file, outside any function:

    const uint32_t approtect_set __attribute__((used,at(0x10001208))) = 0xFFFFFF00;

    This will set the readback protection, and you don't need a system reset.

    Otherwise, you can set it in the application, e.g. in the top of your main() function. This, however, requires a system reset, which you can do in application. Please use the delay, to prevent a system reset loop (which makes it difficult to reset the chip).

    void nrf_nvmc_write_word(uint32_t address, uint32_t value)
    {
        // Enable write.
        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
        __ISB();
        __DSB();
    
        *(uint32_t*)address = value;
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy) {;}
    
        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
        __ISB();
        __DSB();
    }
    
    int main(void)
    {
        nrf_delay_ms(1000);
        if (NRF_UICR->APPROTECT == 0xFFFFFFFF)
        {
            nrf_nvmc_write_word((uint32_t)&(NRF_UICR->APPROTECT), 0xFFFFFF00);
            
            NVIC_SystemReset();
        }    
        nrf_gpio_cfg_output(LED_1);
        nrf_gpio_cfg_output(LED_2);
        while (true)
        {
            if (NRF_UICR->APPROTECT != 0xFFFFFFFF)
                nrf_gpio_pin_toggle(LED_1);
            else
                nrf_gpio_pin_toggle(LED_2);
            nrf_delay_ms(100);
        }
    }

    You can of course remove the lines controlling the LEDs, but if you use a DK, it will show you that the registers are set correct.

    Also, using nrfjprog to try to read the memory will give the return:

    >nrfjprog --memrd 0x10001208

    ERROR: The operation attempted is unavailable due to readback protection in

    ERROR: your device. Please use --recover to unlock the device.

    Best regards,

    Edvin

  • Hello,

    I will try to add delay at begin. But why is it necessary? Why system can go into reset loop? Isn't APPROTECT register updated before program execution?

    My code:

    	if ((NRF_UICR->APPROTECT & UICR_APPROTECT_PALL_Msk) != UICR_APPROTECT_PALL_Enabled)
    	{
    		uint32_t tmp;
    
    		/* Enable access to write to flash */
    		NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
    	    while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
    
    		/* Enable Access Port Protection
    		 * Access to CPU registers and memory will be disabled
    		 * to disable protection ERASEALL command must be applied. */
    		tmp = NRF_UICR->APPROTECT & ~UICR_APPROTECT_PALL_Msk;
    		NRF_UICR->APPROTECT = tmp | UICR_APPROTECT_PALL_Enabled;
    
    	    while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
    
    		/* Perform reset to take effect of changes */
    		NVIC_SystemReset();
    	}

    Console:

    >nrfjprog --recover
    Recovering device. This operation might take 30s.
    Erasing user code and UICR flash areas.
    
    >nrfjprog --program "file.hex"
    Parsing hex file.
    Reading flash area to program to guarantee it is erased.
    Checking that the area to write is not protected.
    Programing device.
    
    >nrfjprog --memrd 0x10001208
    0x10001208: FFFFFFFF                              |....|
    
    >nrfjprog --run
    Run.
    
    >nrfjprog --memrd 0x10001208
    0x10001208: FFFFFFFF                              |....|
    
    >nrfjprog --reset
    Applying system reset.
    Run.
    
    >nrfjprog --memrd 0x10001208
    ERROR: The operation attempted is unavailable due to readback protection in
    ERROR: your device. Please use --recover to unlock the device.

    Best regards,
    Lukasz

Reply
  • Hello,

    I will try to add delay at begin. But why is it necessary? Why system can go into reset loop? Isn't APPROTECT register updated before program execution?

    My code:

    	if ((NRF_UICR->APPROTECT & UICR_APPROTECT_PALL_Msk) != UICR_APPROTECT_PALL_Enabled)
    	{
    		uint32_t tmp;
    
    		/* Enable access to write to flash */
    		NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
    	    while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
    
    		/* Enable Access Port Protection
    		 * Access to CPU registers and memory will be disabled
    		 * to disable protection ERASEALL command must be applied. */
    		tmp = NRF_UICR->APPROTECT & ~UICR_APPROTECT_PALL_Msk;
    		NRF_UICR->APPROTECT = tmp | UICR_APPROTECT_PALL_Enabled;
    
    	    while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
    
    		/* Perform reset to take effect of changes */
    		NVIC_SystemReset();
    	}

    Console:

    >nrfjprog --recover
    Recovering device. This operation might take 30s.
    Erasing user code and UICR flash areas.
    
    >nrfjprog --program "file.hex"
    Parsing hex file.
    Reading flash area to program to guarantee it is erased.
    Checking that the area to write is not protected.
    Programing device.
    
    >nrfjprog --memrd 0x10001208
    0x10001208: FFFFFFFF                              |....|
    
    >nrfjprog --run
    Run.
    
    >nrfjprog --memrd 0x10001208
    0x10001208: FFFFFFFF                              |....|
    
    >nrfjprog --reset
    Applying system reset.
    Run.
    
    >nrfjprog --memrd 0x10001208
    ERROR: The operation attempted is unavailable due to readback protection in
    ERROR: your device. Please use --recover to unlock the device.

    Best regards,
    Lukasz

Children
Related