123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #include <nrfx.h>
- #include "nrf_nvmc.h"
- static inline void wait_for_flash_ready(void)
- {
- while (NRF_NVMC->READY == NVMC_READY_READY_Busy) {;}
- }
- void nrf_nvmc_page_erase(uint32_t address)
- {
-
- NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
- __ISB();
- __DSB();
-
- NRF_NVMC->ERASEPAGE = address;
- wait_for_flash_ready();
- NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
- __ISB();
- __DSB();
- }
- void nrf_nvmc_write_byte(uint32_t address, uint8_t value)
- {
- uint32_t byte_shift = address & (uint32_t)0x03;
- uint32_t address32 = address & ~byte_shift;
- uint32_t value32 = (*(uint32_t*)address32 & ~((uint32_t)0xFF << (byte_shift << (uint32_t)3)));
- value32 = value32 + ((uint32_t)value << (byte_shift << 3));
-
- NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
- __ISB();
- __DSB();
- *(uint32_t*)address32 = value32;
- wait_for_flash_ready();
- NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
- __ISB();
- __DSB();
- }
- void nrf_nvmc_write_word(uint32_t address, uint32_t value)
- {
-
- NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
- __ISB();
- __DSB();
- *(uint32_t*)address = value;
- wait_for_flash_ready();
- NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
- __ISB();
- __DSB();
- }
- void nrf_nvmc_write_bytes(uint32_t address, const uint8_t * src, uint32_t num_bytes)
- {
- uint32_t i;
- for (i = 0; i < num_bytes; i++)
- {
- nrf_nvmc_write_byte(address + i,src[i]);
- }
- }
- void nrf_nvmc_write_words(uint32_t address, const uint32_t * src, uint32_t num_words)
- {
- uint32_t i;
-
- NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
- __ISB();
- __DSB();
- for (i = 0; i < num_words; i++)
- {
- ((uint32_t*)address)[i] = src[i];
- wait_for_flash_ready();
- }
- NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
- __ISB();
- __DSB();
- }
|