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.h@1:7a490a05943a, 2017-09-11 (annotated)
- Committer:
- yjlou
- Date:
- Mon Sep 11 05:09:15 2017 +0000
- Revision:
- 1:7a490a05943a
- Parent:
- 0:a71e3b72379a
Remove debug message.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yjlou | 0:a71e3b72379a | 1 | // STM32F042K6 Flash driver |
yjlou | 0:a71e3b72379a | 2 | // |
yjlou | 0:a71e3b72379a | 3 | // In theory, this should be working on STM32F0x1/STM32F0x2/STM32F0x8, but only |
yjlou | 0:a71e3b72379a | 4 | // tested on STM32F042K6. |
yjlou | 0:a71e3b72379a | 5 | // |
yjlou | 0:a71e3b72379a | 6 | // Please refer to STM RM0091 document for details. |
yjlou | 0:a71e3b72379a | 7 | // http://www.st.com/content/ccc/resource/technical/document/reference_manual/c2/f8/8a/f2/18/e6/43/96/DM00031936.pdf/files/DM00031936.pdf/jcr:content/translations/en.DM00031936.pdf |
yjlou | 0:a71e3b72379a | 8 | |
yjlou | 0:a71e3b72379a | 9 | #ifndef F042K6_FLASH_H_ |
yjlou | 0:a71e3b72379a | 10 | #define F042K6_FLASH_H_ |
yjlou | 0:a71e3b72379a | 11 | |
yjlou | 0:a71e3b72379a | 12 | #include "mbed.h" |
yjlou | 0:a71e3b72379a | 13 | #include <stddef.h> |
yjlou | 0:a71e3b72379a | 14 | #include <stdint.h> |
yjlou | 0:a71e3b72379a | 15 | |
yjlou | 0:a71e3b72379a | 16 | // Memory address to read flash content |
yjlou | 0:a71e3b72379a | 17 | // FLASH_BASE and FLASH_BANK1_END are defined in mbed/TARGET_NUCLEO_F042K6/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F042K6/device/stm32f042x6.h |
yjlou | 0:a71e3b72379a | 18 | #define USER_FLASH_AREA_START (FLASH_BASE) |
yjlou | 0:a71e3b72379a | 19 | #define USER_FLASH_AREA_SIZE (FLASH_BANK1_END - FLASH_BASE) // 32KB for F042K6 |
yjlou | 0:a71e3b72379a | 20 | static const size_t kFlashPageInBytes = 1024; |
yjlou | 0:a71e3b72379a | 21 | |
yjlou | 0:a71e3b72379a | 22 | #define REG32(x) ((volatile uint32_t*)(x)) |
yjlou | 0:a71e3b72379a | 23 | |
yjlou | 0:a71e3b72379a | 24 | // FLASH_R_BASE (0x40022000) is defined in mbed/TARGET_NUCLEO_F042K6/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F042K6/device/stm32f042x6.h |
yjlou | 0:a71e3b72379a | 25 | // FIXME: looks like the definition in stm32f042x6.h is wrong. |
yjlou | 0:a71e3b72379a | 26 | #undef FLASH_R_BASE |
yjlou | 0:a71e3b72379a | 27 | #define FLASH_R_BASE (0x40022000) |
yjlou | 0:a71e3b72379a | 28 | #define FLASH_ACR (FLASH_R_BASE + 0x00) // Flash access control register |
yjlou | 0:a71e3b72379a | 29 | |
yjlou | 0:a71e3b72379a | 30 | #define FLASH_KEYR (FLASH_R_BASE + 0x04) // Flash key register |
yjlou | 0:a71e3b72379a | 31 | |
yjlou | 0:a71e3b72379a | 32 | #define FLASH_OPTKEYR (FLASH_R_BASE + 0x08) // Flash option key register |
yjlou | 0:a71e3b72379a | 33 | |
yjlou | 0:a71e3b72379a | 34 | #define FLASH_SR (FLASH_R_BASE + 0x0c) // Flash status register |
yjlou | 0:a71e3b72379a | 35 | #define SR_BSY (1 << 0) // Busy |
yjlou | 0:a71e3b72379a | 36 | |
yjlou | 0:a71e3b72379a | 37 | #define FLASH_CR (FLASH_R_BASE + 0x10) // Flash control register |
yjlou | 0:a71e3b72379a | 38 | #define CR_LOCK (1 << 7) // Lock bit |
yjlou | 0:a71e3b72379a | 39 | #define CR_STRT (1 << 6) // Start for erase |
yjlou | 0:a71e3b72379a | 40 | #define CR_PER (1 << 1) // Page Erase |
yjlou | 0:a71e3b72379a | 41 | #define CR_PG (1 << 0) // Program |
yjlou | 0:a71e3b72379a | 42 | |
yjlou | 0:a71e3b72379a | 43 | #define FLASH_AR (FLASH_R_BASE + 0x14) // Flash address register |
yjlou | 0:a71e3b72379a | 44 | |
yjlou | 0:a71e3b72379a | 45 | #define FLASH_OBR (FLASH_R_BASE + 0x1C) // Flash Option Byte register |
yjlou | 0:a71e3b72379a | 46 | |
yjlou | 0:a71e3b72379a | 47 | #define FLASH_WRPR (FLASH_R_BASE + 0x20) // Write prtotection register |
yjlou | 0:a71e3b72379a | 48 | |
yjlou | 0:a71e3b72379a | 49 | class FlashPartition { |
yjlou | 0:a71e3b72379a | 50 | public: |
yjlou | 0:a71e3b72379a | 51 | // Constructor: |
yjlou | 0:a71e3b72379a | 52 | // Args: |
yjlou | 0:a71e3b72379a | 53 | // start_addr: the starting address of the partition. Starting from 0. |
yjlou | 0:a71e3b72379a | 54 | FlashPartition(uint32_t start_addr, size_t size) : |
yjlou | 0:a71e3b72379a | 55 | start_addr_(start_addr), |
yjlou | 0:a71e3b72379a | 56 | size_(size) {} // TODO: add assertion to check address and size. |
yjlou | 0:a71e3b72379a | 57 | |
yjlou | 0:a71e3b72379a | 58 | // Read flash content in this partition. |
yjlou | 0:a71e3b72379a | 59 | void Read(uint32_t offset, char* buf, size_t len); |
yjlou | 0:a71e3b72379a | 60 | |
yjlou | 0:a71e3b72379a | 61 | // Write flash content into this partition. |
yjlou | 0:a71e3b72379a | 62 | // Returns true for success. |
yjlou | 0:a71e3b72379a | 63 | bool Write(uint32_t offset, const char* buf, size_t len); |
yjlou | 0:a71e3b72379a | 64 | |
yjlou | 0:a71e3b72379a | 65 | // Erase whole partition |
yjlou | 0:a71e3b72379a | 66 | // Returns true for success. |
yjlou | 0:a71e3b72379a | 67 | bool EraseAll(); |
yjlou | 0:a71e3b72379a | 68 | |
yjlou | 0:a71e3b72379a | 69 | private: |
yjlou | 0:a71e3b72379a | 70 | void UnlockControlRegister(); |
yjlou | 0:a71e3b72379a | 71 | |
yjlou | 0:a71e3b72379a | 72 | const uint32_t start_addr_; |
yjlou | 0:a71e3b72379a | 73 | const size_t size_; |
yjlou | 0:a71e3b72379a | 74 | }; |
yjlou | 0:a71e3b72379a | 75 | |
yjlou | 0:a71e3b72379a | 76 | #endif // F042K6_FLASH_H_ |