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));
f042k6_flash.cpp
- Committer:
- yjlou
- Date:
- 2017-09-11
- Revision:
- 1:7a490a05943a
- Parent:
- 0:a71e3b72379a
File content as of revision 1:7a490a05943a:
#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) { *REG32(FLASH_KEYR) = 0x45670123; *REG32(FLASH_KEYR) = 0xCDEF89AB; } }