Writing to flash memory

Hi,

I'm using an nRF52810 on a custom board and I'm using softdevice s112_nrf52_7.2.0_softdevice.hex.

I want to write some information (just 4 bytes) to flash memory, because I want the data to survive a hard reset and power cycling.

I have free space in code memory, so I decided to do as seen in my code below. 

Is there any problems in doing a flash write like this?

 All comments are welcome. Thanks

#define DATA_ADDRESS ((NRF_FICR->CODESIZE * NRF_FICR->CODEPAGESIZE) - 0x04)
#define DATA_VALUE 0xDEADBEEF

void write_value(void)
{
    uint32_t address;
    uint32_t val;
    uint32_t err_code;

    // Write 4 bytes at the last page of code flash
    address = DATA_ADDRESS;
    val = DATA_VALUE;
    err_code = sd_flash_write((uint32_t*)address, &val, 1);
    if (err_code == NRF_SUCCESS)
    {
        // Success
    }
}

void erase_value(void)
{
    uint32_t page_number;
    uint32_t err_code;

    // Erase the last page of code flash
    page_number = NRF_FICR->CODESIZE - 1;
    err_code = sd_flash_page_erase(page_number);
    if (err_code == NRF_SUCCESS)
    {
        // Success
    }
}

bool test_value(void)
{
    uint32_t address;
    uint32_t val;

    // Read the last word of the code space
    address = DATA_ADDRESS;
    val = *(uint32_t*)address;

    return (val == DATA_VALUE);
}

Related