From 4717cf0a1d6c210a362e8bdf63265c4910e2c8cc Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 24 May 2020 20:30:06 +0200 Subject: Add driver for writing into the internal flash. Write the OK flag for mcuboot using this driver. --- src/drivers/InternalFlash.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/drivers/InternalFlash.h | 15 +++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/drivers/InternalFlash.cpp create mode 100644 src/drivers/InternalFlash.h (limited to 'src/drivers') diff --git a/src/drivers/InternalFlash.cpp b/src/drivers/InternalFlash.cpp new file mode 100644 index 00000000..bc89ff1a --- /dev/null +++ b/src/drivers/InternalFlash.cpp @@ -0,0 +1,39 @@ +#include +#include "InternalFlash.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) {;} +} diff --git a/src/drivers/InternalFlash.h b/src/drivers/InternalFlash.h new file mode 100644 index 00000000..fd25bf46 --- /dev/null +++ b/src/drivers/InternalFlash.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace Pinetime { + namespace Drivers { + class InternalFlash { + public: + static void ErasePage(uint32_t address); + static void WriteWord(uint32_t address, uint32_t value); + private: + static inline void Wait(); + }; + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2