InfiniTime.git

ref: 0.12.0

src/drivers/InternalFlash.cpp


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "InternalFlash.h"
#include <mdk/nrf.h>
using namespace Pinetime::Drivers;

void InternalFlash::ErasePage(uint32_t address) {
  // Enable erase.
  NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
  __ISB();
  __DSB();

  // Erase the page
  NRF_NVMC->ERASEPAGE = address;
  Wait();

  // Disable erase
  NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
  __ISB();
  __DSB();
}

void InternalFlash::WriteWord(uint32_t address, uint32_t value) {
  // Enable write.
  NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
  __ISB();
  __DSB();

  // Write word
  *(uint32_t*)address = value;
  Wait();

  // Disable write
  NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
  __ISB();
  __DSB();
}

void InternalFlash::Wait() {
  while (NRF_NVMC->READY == NVMC_READY_READY_Busy) {;}
}