The serial number of the product is stored in the memory of the nRF52. After DFU update, this memory area is erased (or changed to new one from new FW).
Please let us know how we can save data on chip's memory in a proper way (and not loss in while DFU update)
PS: Next month we have to produce ~3k units and I really do not want the serial numbers to be lost after the update.
below is a piece of code:
uint32_t flash_dword_read(uint32_t address)
{
return *((uint32_t*) address);
}
void flash_dword_write(uint32_t address, uint32_t data)
{
// Enable write.
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
}
((uint32_t*)address)[0] = data;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){;}
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
}
}
/*
* Function for delete a page memory
*/
void nrf_nvmc_page_erase(uint32_t address)
{
// Enable erase.
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
__ISB();
__DSB();
// Erase the page
NRF_NVMC->ERASEPAGE = address;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy) {;}//wait_for_flash_ready
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
__ISB();
__DSB();
}
#define DATA_BLOCK_ADDR (0x0005B000)
#define VIN1_ADDRESS (0x0005B008) /* VIN_0 to VIN_3 */
...
#define VIN5_ADDRESS (0x0005B018) /* VIN_16 to VIN_19*/
#define SERIAL1_ADDRESS (0x0005B01c) /* SN_0to3 */
#define SERIAL2_ADDRESS (0x0005B020) /* SN_4to7 */
#define SERIAL3_ADDRESS (0x0005B024) /* SN_8to9 */
...
int main(void)
{
...
//read SN
uint32_t SN_0to3 = flash_dword_read(SERIAL1_ADDRESS);
uint32_t SN_4to7 = flash_dword_read(SERIAL2_ADDRESS);
uint32_t SN_8to9 = flash_dword_read(SERIAL3_ADDRESS);
...
//write SN
__disable_irq();
nrf_delay_ms(5);
nrf_nvmc_page_erase(DATA_BLOCK_ADDR);
nrf_delay_ms(5);
flash_dword_write(SERIAL1_ADDRESS,0x12345678);
flash_dword_write(SERIAL2_ADDRESS,0x12345678);
flash_dword_write(SERIAL3_ADDRESS,0x12345678);
__enable_irq();
...
}