To access the internal flash of STM32F042K6.

This library provides a Partition class used to segment the STM32F042K6 internal flash so that the caller can read/write/erase the partition.

Examples

    FlashPartition config(0x7c00, 0x400);

    uint8_t buf[16];
    config.Read(address, buf, sizeof(buf));

    config.EraseAll();
    config.Write(address, buf, sizeof(buf));

Revision:
0:a71e3b72379a
Child:
1:7a490a05943a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/f042k6_flash.cpp	Tue Aug 29 02:36:52 2017 +0000
@@ -0,0 +1,62 @@
+#include "f042k6_flash.h"
+
+#include <string.h>
+
+// Read flash content in this partition.
+void FlashPartition::Read(uint32_t offset, char* buf, size_t len) {
+    memcpy(buf, (void*)(USER_FLASH_AREA_START + start_addr_ + offset), len);
+}
+
+// Write flash content into this partition.
+// Returns true for success.
+bool FlashPartition::Write(uint32_t offset, const char* buf, size_t len) {
+    UnlockControlRegister();
+
+    if (*REG32(FLASH_SR) & SR_BSY) {
+        return false;
+    }
+
+    *REG32(FLASH_CR) |= CR_PG;
+    
+    const uint16_t* src = (uint16_t*)buf;
+    uint16_t* dst = (uint16_t*)(USER_FLASH_AREA_START + start_addr_ + offset);
+    for (int i = 0; i < (len + 1) / 2; i++) {
+        *(dst++) = *(src++);
+        while (*REG32(FLASH_SR) & SR_BSY);
+    }
+
+    *REG32(FLASH_CR) &= ~CR_PG;
+
+    return true;
+}
+
+// Erase whole partition
+// Returns true for success.
+bool FlashPartition::EraseAll() {
+    UnlockControlRegister();
+    
+    if (*REG32(FLASH_SR) & SR_BSY) {
+        return false;
+    }
+
+    *REG32(FLASH_CR) |= CR_PER;
+    
+    for (uint32_t page = USER_FLASH_AREA_START + start_addr_;
+         page < (USER_FLASH_AREA_START + start_addr_ + size_);
+         page += kFlashPageInBytes) {
+        *REG32(FLASH_AR) = page;
+        *REG32(FLASH_CR) |= CR_STRT;
+        while (*REG32(FLASH_SR) & SR_BSY);
+    }
+    *REG32(FLASH_CR) &= ~CR_PER;
+
+    return true;
+}
+
+void FlashPartition::UnlockControlRegister() {
+    if (*REG32(FLASH_CR) & CR_LOCK) {
+        printf("Unlocking control register\r\n");
+        *REG32(FLASH_KEYR) = 0x45670123;
+        *REG32(FLASH_KEYR) = 0xCDEF89AB;
+    }
+}
\ No newline at end of file