
STM32F411RE Embedded FLASH(internal FLASH) write.
Diff: main.cpp
- Revision:
- 0:23f5942ba14e
- Child:
- 1:fa70127b3381
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Nov 30 13:44:37 2015 +0000 @@ -0,0 +1,54 @@ +#include "mbed.h" +#include "stm32f4xx_hal_flash.h" + +namespace { + //STM32F411.C/E(DM00119316.pdf p.43 Table 4) + const uint32_t ADDR_FLASH_SECTOR_0 = (uint32_t)0x08000000; /* Base address of Sector 0, 16 Kbytes */ + const uint32_t ADDR_FLASH_SECTOR_1 = (uint32_t)0x08004000; /* Base address of Sector 1, 16 Kbytes */ + const uint32_t ADDR_FLASH_SECTOR_2 = (uint32_t)0x08008000; /* Base address of Sector 2, 16 Kbytes */ + const uint32_t ADDR_FLASH_SECTOR_3 = (uint32_t)0x0800c000; /* Base address of Sector 3, 16 Kbytes */ + const uint32_t ADDR_FLASH_SECTOR_4 = (uint32_t)0x08010000; /* Base address of Sector 4, 64 Kbytes */ + const uint32_t ADDR_FLASH_SECTOR_5 = (uint32_t)0x08020000; /* Base address of Sector 5, 128 Kbytes */ + const uint32_t ADDR_FLASH_SECTOR_6 = (uint32_t)0x08040000; /* Base address of Sector 6, 128 Kbytes */ + const uint32_t ADDR_FLASH_SECTOR_7 = (uint32_t)0x08060000; /* Base address of Sector 7, 128 Kbytes */ + + /** @brief 内蔵FLASHへのbyte書込み(セクタ消去有り) + * + * @param[in] addr 書込み先アドレス(ADDR_FLASH_SECTOR_x) + * @param[in] pData 書き込みデータ + * @param[in] Len 書き込みデータサイズ + */ + void programByte(uint32_t addr, const uint8_t *pData, uint8_t Len) + { + HAL_StatusTypeDef ret; + + /* flash control registerへのアクセス許可 */ + HAL_FLASH_Unlock(); + + /* 消去(電圧 [2.7V to 3.6V]) */ + FLASH_Erase_Sector(addr, FLASH_VOLTAGE_RANGE_3); + + /* 書込み(4byte単位) */ + for (int lp = 0 ; lp < Len; lp ++) { + ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, *pData); + if (ret != HAL_OK) { + //trouble!! + while (true) {} + } + addr++; + pData++; + } + + /* flash control registerへのアクセス禁止 */ + HAL_FLASH_Lock(); + } +} + + + +int main() +{ + uint8_t str[] = "Hello."; + + programByte(ADDR_FLASH_SECTOR_7, str, sizeof(str)); +}