To access the internal flash of STM32F042K6.
Embed:
(wiki syntax)
Show/hide line numbers
f042k6_flash.h
00001 // STM32F042K6 Flash driver 00002 // 00003 // In theory, this should be working on STM32F0x1/STM32F0x2/STM32F0x8, but only 00004 // tested on STM32F042K6. 00005 // 00006 // Please refer to STM RM0091 document for details. 00007 // 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 00008 00009 #ifndef F042K6_FLASH_H_ 00010 #define F042K6_FLASH_H_ 00011 00012 #include "mbed.h" 00013 #include <stddef.h> 00014 #include <stdint.h> 00015 00016 // Memory address to read flash content 00017 // FLASH_BASE and FLASH_BANK1_END are defined in mbed/TARGET_NUCLEO_F042K6/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F042K6/device/stm32f042x6.h 00018 #define USER_FLASH_AREA_START (FLASH_BASE) 00019 #define USER_FLASH_AREA_SIZE (FLASH_BANK1_END - FLASH_BASE) // 32KB for F042K6 00020 static const size_t kFlashPageInBytes = 1024; 00021 00022 #define REG32(x) ((volatile uint32_t*)(x)) 00023 00024 // FLASH_R_BASE (0x40022000) is defined in mbed/TARGET_NUCLEO_F042K6/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F042K6/device/stm32f042x6.h 00025 // FIXME: looks like the definition in stm32f042x6.h is wrong. 00026 #undef FLASH_R_BASE 00027 #define FLASH_R_BASE (0x40022000) 00028 #define FLASH_ACR (FLASH_R_BASE + 0x00) // Flash access control register 00029 00030 #define FLASH_KEYR (FLASH_R_BASE + 0x04) // Flash key register 00031 00032 #define FLASH_OPTKEYR (FLASH_R_BASE + 0x08) // Flash option key register 00033 00034 #define FLASH_SR (FLASH_R_BASE + 0x0c) // Flash status register 00035 #define SR_BSY (1 << 0) // Busy 00036 00037 #define FLASH_CR (FLASH_R_BASE + 0x10) // Flash control register 00038 #define CR_LOCK (1 << 7) // Lock bit 00039 #define CR_STRT (1 << 6) // Start for erase 00040 #define CR_PER (1 << 1) // Page Erase 00041 #define CR_PG (1 << 0) // Program 00042 00043 #define FLASH_AR (FLASH_R_BASE + 0x14) // Flash address register 00044 00045 #define FLASH_OBR (FLASH_R_BASE + 0x1C) // Flash Option Byte register 00046 00047 #define FLASH_WRPR (FLASH_R_BASE + 0x20) // Write prtotection register 00048 00049 class FlashPartition { 00050 public: 00051 // Constructor: 00052 // Args: 00053 // start_addr: the starting address of the partition. Starting from 0. 00054 FlashPartition(uint32_t start_addr, size_t size) : 00055 start_addr_(start_addr), 00056 size_(size) {} // TODO: add assertion to check address and size. 00057 00058 // Read flash content in this partition. 00059 void Read(uint32_t offset, char* buf, size_t len); 00060 00061 // Write flash content into this partition. 00062 // Returns true for success. 00063 bool Write(uint32_t offset, const char* buf, size_t len); 00064 00065 // Erase whole partition 00066 // Returns true for success. 00067 bool EraseAll(); 00068 00069 private: 00070 void UnlockControlRegister(); 00071 00072 const uint32_t start_addr_; 00073 const size_t size_; 00074 }; 00075 00076 #endif // F042K6_FLASH_H_
Generated on Fri Jul 15 2022 00:06:06 by
1.7.2