21 #include "common_util.h" 24 #define PAGE_START_ADDR_SUFFIX 0x1000 26 uint32_t hal_nvmc_erase_page (uint32_t page_start_address)
28 if((page_start_address % PAGE_START_ADDR_SUFFIX) != 0)
30 log_printf(
"Invalid address\n");
35 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
36 while(NRF_NVMC->READY != NVMC_READY_READY_Ready);
37 NRF_NVMC->ERASEPAGE = page_start_address;
38 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
39 while(NRF_NVMC->READY != NVMC_READY_READY_Ready);
45 void hal_nvmc_write_data (
void * p_destination,
void * p_source, uint32_t size_of_data)
47 uint32_t start_address = ((uint32_t)p_destination/4)*4;
48 uint32_t end_address = (
CEIL_DIV(((uint32_t)p_destination + size_of_data), 4))*4;
49 uint32_t no_of_words = (end_address - start_address)/4;
50 log_printf(
"Start Address : %x, End Address : %x, Size in bytes : %d, No of words : %d\n",
51 start_address, end_address, size_of_data, no_of_words);
54 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
55 while(NRF_NVMC->READY != NVMC_READY_READY_Ready);
56 uint32_t word_to_write;
57 uint32_t * loc_to_write = (uint32_t *)start_address;
58 uint32_t head_offset = ((uint32_t)p_destination - start_address)%4;
59 uint32_t * data_to_write = (uint32_t *)((uint32_t)p_source);
60 word_to_write = *((uint32_t *)(data_to_write));
61 for(uint32_t cnt = 0; cnt < head_offset; cnt++)
63 word_to_write = (word_to_write << 8) | 0x000000FF;
65 *loc_to_write = word_to_write;
71 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
72 while(NRF_NVMC->READY != NVMC_READY_READY_Ready);
76 data_to_write = (uint32_t *)((uint32_t)data_to_write - head_offset);
78 for(int32_t i = 1; i< (no_of_words - 1); i++)
81 word_to_write = (*(uint32_t*)(data_to_write));
82 *loc_to_write = word_to_write;
89 uint32_t tail_offset = ((no_of_words*4) - size_of_data - head_offset)%4;
91 word_to_write = *(data_to_write);
93 for(uint32_t cnt = 0; cnt < tail_offset; cnt++)
95 word_to_write = (word_to_write << 8);
97 for(uint32_t cnt=0; cnt <tail_offset; cnt++)
99 word_to_write = (word_to_write >> 8) | 0xFF000000;
101 *loc_to_write = word_to_write;
103 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
104 while(NRF_NVMC->READY != NVMC_READY_READY_Ready);
#define CEIL_DIV(A, B)
When the result of a division is not an integer, the result is rounded up to the next integer.