STM32F411RE Embedded FLASH(internal FLASH) write.

Dependencies:   mbed

Committer:
hiro99ma
Date:
Mon Nov 30 13:44:37 2015 +0000
Revision:
0:23f5942ba14e
Child:
1:fa70127b3381
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hiro99ma 0:23f5942ba14e 1 #include "mbed.h"
hiro99ma 0:23f5942ba14e 2 #include "stm32f4xx_hal_flash.h"
hiro99ma 0:23f5942ba14e 3
hiro99ma 0:23f5942ba14e 4 namespace {
hiro99ma 0:23f5942ba14e 5 //STM32F411.C/E(DM00119316.pdf p.43 Table 4)
hiro99ma 0:23f5942ba14e 6 const uint32_t ADDR_FLASH_SECTOR_0 = (uint32_t)0x08000000; /* Base address of Sector 0, 16 Kbytes */
hiro99ma 0:23f5942ba14e 7 const uint32_t ADDR_FLASH_SECTOR_1 = (uint32_t)0x08004000; /* Base address of Sector 1, 16 Kbytes */
hiro99ma 0:23f5942ba14e 8 const uint32_t ADDR_FLASH_SECTOR_2 = (uint32_t)0x08008000; /* Base address of Sector 2, 16 Kbytes */
hiro99ma 0:23f5942ba14e 9 const uint32_t ADDR_FLASH_SECTOR_3 = (uint32_t)0x0800c000; /* Base address of Sector 3, 16 Kbytes */
hiro99ma 0:23f5942ba14e 10 const uint32_t ADDR_FLASH_SECTOR_4 = (uint32_t)0x08010000; /* Base address of Sector 4, 64 Kbytes */
hiro99ma 0:23f5942ba14e 11 const uint32_t ADDR_FLASH_SECTOR_5 = (uint32_t)0x08020000; /* Base address of Sector 5, 128 Kbytes */
hiro99ma 0:23f5942ba14e 12 const uint32_t ADDR_FLASH_SECTOR_6 = (uint32_t)0x08040000; /* Base address of Sector 6, 128 Kbytes */
hiro99ma 0:23f5942ba14e 13 const uint32_t ADDR_FLASH_SECTOR_7 = (uint32_t)0x08060000; /* Base address of Sector 7, 128 Kbytes */
hiro99ma 0:23f5942ba14e 14
hiro99ma 0:23f5942ba14e 15 /** @brief 内蔵FLASHへのbyte書込み(セクタ消去有り)
hiro99ma 0:23f5942ba14e 16 *
hiro99ma 0:23f5942ba14e 17 * @param[in] addr 書込み先アドレス(ADDR_FLASH_SECTOR_x)
hiro99ma 0:23f5942ba14e 18 * @param[in] pData 書き込みデータ
hiro99ma 0:23f5942ba14e 19 * @param[in] Len 書き込みデータサイズ
hiro99ma 0:23f5942ba14e 20 */
hiro99ma 0:23f5942ba14e 21 void programByte(uint32_t addr, const uint8_t *pData, uint8_t Len)
hiro99ma 0:23f5942ba14e 22 {
hiro99ma 0:23f5942ba14e 23 HAL_StatusTypeDef ret;
hiro99ma 0:23f5942ba14e 24
hiro99ma 0:23f5942ba14e 25 /* flash control registerへのアクセス許可 */
hiro99ma 0:23f5942ba14e 26 HAL_FLASH_Unlock();
hiro99ma 0:23f5942ba14e 27
hiro99ma 0:23f5942ba14e 28 /* 消去(電圧 [2.7V to 3.6V]) */
hiro99ma 0:23f5942ba14e 29 FLASH_Erase_Sector(addr, FLASH_VOLTAGE_RANGE_3);
hiro99ma 0:23f5942ba14e 30
hiro99ma 0:23f5942ba14e 31 /* 書込み(4byte単位) */
hiro99ma 0:23f5942ba14e 32 for (int lp = 0 ; lp < Len; lp ++) {
hiro99ma 0:23f5942ba14e 33 ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, *pData);
hiro99ma 0:23f5942ba14e 34 if (ret != HAL_OK) {
hiro99ma 0:23f5942ba14e 35 //trouble!!
hiro99ma 0:23f5942ba14e 36 while (true) {}
hiro99ma 0:23f5942ba14e 37 }
hiro99ma 0:23f5942ba14e 38 addr++;
hiro99ma 0:23f5942ba14e 39 pData++;
hiro99ma 0:23f5942ba14e 40 }
hiro99ma 0:23f5942ba14e 41
hiro99ma 0:23f5942ba14e 42 /* flash control registerへのアクセス禁止 */
hiro99ma 0:23f5942ba14e 43 HAL_FLASH_Lock();
hiro99ma 0:23f5942ba14e 44 }
hiro99ma 0:23f5942ba14e 45 }
hiro99ma 0:23f5942ba14e 46
hiro99ma 0:23f5942ba14e 47
hiro99ma 0:23f5942ba14e 48
hiro99ma 0:23f5942ba14e 49 int main()
hiro99ma 0:23f5942ba14e 50 {
hiro99ma 0:23f5942ba14e 51 uint8_t str[] = "Hello.";
hiro99ma 0:23f5942ba14e 52
hiro99ma 0:23f5942ba14e 53 programByte(ADDR_FLASH_SECTOR_7, str, sizeof(str));
hiro99ma 0:23f5942ba14e 54 }