STM32F411RE Embedded FLASH(internal FLASH) write.

Dependencies:   mbed

Committer:
hiro99ma
Date:
Mon Nov 30 14:44:32 2015 +0000
Revision:
1:fa70127b3381
Parent:
0:23f5942ba14e
first commit(2)

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 1:fa70127b3381 4 #if 1
hiro99ma 1:fa70127b3381 5 #define DEBUG_PRINTF pc.printf
hiro99ma 1:fa70127b3381 6 #else
hiro99ma 1:fa70127b3381 7 #define DEBUG_PRINTF(...)
hiro99ma 1:fa70127b3381 8 #endif
hiro99ma 1:fa70127b3381 9
hiro99ma 0:23f5942ba14e 10 namespace {
hiro99ma 1:fa70127b3381 11 Serial pc(SERIAL_TX, SERIAL_RX);
hiro99ma 1:fa70127b3381 12
hiro99ma 0:23f5942ba14e 13 //STM32F411.C/E(DM00119316.pdf p.43 Table 4)
hiro99ma 1:fa70127b3381 14 const struct {
hiro99ma 1:fa70127b3381 15 uint8_t sector;
hiro99ma 1:fa70127b3381 16 uint32_t addr;
hiro99ma 1:fa70127b3381 17 } FLASH_SECTOR[] = {
hiro99ma 1:fa70127b3381 18 {
hiro99ma 1:fa70127b3381 19 FLASH_SECTOR_0,
hiro99ma 1:fa70127b3381 20 (uint32_t)0x08000000 /* Base address of Sector 0, 16 Kbytes */
hiro99ma 1:fa70127b3381 21 },
hiro99ma 1:fa70127b3381 22 {
hiro99ma 1:fa70127b3381 23 FLASH_SECTOR_1,
hiro99ma 1:fa70127b3381 24 (uint32_t)0x08004000 /* Base address of Sector 1, 16 Kbytes */
hiro99ma 1:fa70127b3381 25 },
hiro99ma 1:fa70127b3381 26 {
hiro99ma 1:fa70127b3381 27 FLASH_SECTOR_2,
hiro99ma 1:fa70127b3381 28 (uint32_t)0x08008000 /* Base address of Sector 2, 16 Kbytes */
hiro99ma 1:fa70127b3381 29 },
hiro99ma 1:fa70127b3381 30 {
hiro99ma 1:fa70127b3381 31 FLASH_SECTOR_3,
hiro99ma 1:fa70127b3381 32 (uint32_t)0x0800c000 /* Base address of Sector 3, 16 Kbytes */
hiro99ma 1:fa70127b3381 33 },
hiro99ma 1:fa70127b3381 34 {
hiro99ma 1:fa70127b3381 35 FLASH_SECTOR_4,
hiro99ma 1:fa70127b3381 36 (uint32_t)0x08010000 /* Base address of Sector 4, 64 Kbytes */
hiro99ma 1:fa70127b3381 37 },
hiro99ma 1:fa70127b3381 38 {
hiro99ma 1:fa70127b3381 39 FLASH_SECTOR_5,
hiro99ma 1:fa70127b3381 40 (uint32_t)0x08020000 /* Base address of Sector 5, 128 Kbytes */
hiro99ma 1:fa70127b3381 41 },
hiro99ma 1:fa70127b3381 42 {
hiro99ma 1:fa70127b3381 43 FLASH_SECTOR_6,
hiro99ma 1:fa70127b3381 44 (uint32_t)0x08040000 /* Base address of Sector 6, 128 Kbytes */
hiro99ma 1:fa70127b3381 45 },
hiro99ma 1:fa70127b3381 46 {
hiro99ma 1:fa70127b3381 47 FLASH_SECTOR_7,
hiro99ma 1:fa70127b3381 48 (uint32_t)0x08060000 /* Base address of Sector 7, 128 Kbytes */
hiro99ma 1:fa70127b3381 49 }
hiro99ma 1:fa70127b3381 50 };
hiro99ma 0:23f5942ba14e 51
hiro99ma 0:23f5942ba14e 52 /** @brief 内蔵FLASHへのbyte書込み(セクタ消去有り)
hiro99ma 0:23f5942ba14e 53 *
hiro99ma 0:23f5942ba14e 54 * @param[in] addr 書込み先アドレス(ADDR_FLASH_SECTOR_x)
hiro99ma 0:23f5942ba14e 55 * @param[in] pData 書き込みデータ
hiro99ma 0:23f5942ba14e 56 * @param[in] Len 書き込みデータサイズ
hiro99ma 0:23f5942ba14e 57 */
hiro99ma 1:fa70127b3381 58 void programByte(int sector, const uint8_t *pData, uint8_t Len)
hiro99ma 0:23f5942ba14e 59 {
hiro99ma 0:23f5942ba14e 60 HAL_StatusTypeDef ret;
hiro99ma 0:23f5942ba14e 61
hiro99ma 0:23f5942ba14e 62 /* flash control registerへのアクセス許可 */
hiro99ma 0:23f5942ba14e 63 HAL_FLASH_Unlock();
hiro99ma 1:fa70127b3381 64 DEBUG_PRINTF("unlocked.\n");
hiro99ma 0:23f5942ba14e 65
hiro99ma 0:23f5942ba14e 66 /* 消去(電圧 [2.7V to 3.6V]) */
hiro99ma 1:fa70127b3381 67 FLASH_Erase_Sector(FLASH_SECTOR[sector].sector, FLASH_VOLTAGE_RANGE_3);
hiro99ma 1:fa70127b3381 68 DEBUG_PRINTF("erased.\n");
hiro99ma 1:fa70127b3381 69
hiro99ma 0:23f5942ba14e 70 /* 書込み(4byte単位) */
hiro99ma 1:fa70127b3381 71 uint32_t addr = FLASH_SECTOR[sector].addr;
hiro99ma 1:fa70127b3381 72 for (int lp = 0 ; lp < Len; lp++) {
hiro99ma 1:fa70127b3381 73 DEBUG_PRINTF("addr:0x%08x data=%02x\n", addr, *pData);
hiro99ma 0:23f5942ba14e 74 ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, *pData);
hiro99ma 0:23f5942ba14e 75 if (ret != HAL_OK) {
hiro99ma 0:23f5942ba14e 76 //trouble!!
hiro99ma 1:fa70127b3381 77 DEBUG_PRINTF("fail\n");
hiro99ma 0:23f5942ba14e 78 while (true) {}
hiro99ma 0:23f5942ba14e 79 }
hiro99ma 0:23f5942ba14e 80 addr++;
hiro99ma 0:23f5942ba14e 81 pData++;
hiro99ma 0:23f5942ba14e 82 }
hiro99ma 0:23f5942ba14e 83
hiro99ma 0:23f5942ba14e 84 /* flash control registerへのアクセス禁止 */
hiro99ma 0:23f5942ba14e 85 HAL_FLASH_Lock();
hiro99ma 0:23f5942ba14e 86 }
hiro99ma 1:fa70127b3381 87
hiro99ma 1:fa70127b3381 88 const uint8_t *getFlash(int sector) { return (const uint8_t *)FLASH_SECTOR[sector].addr; }
hiro99ma 0:23f5942ba14e 89 }
hiro99ma 0:23f5942ba14e 90
hiro99ma 0:23f5942ba14e 91
hiro99ma 0:23f5942ba14e 92
hiro99ma 0:23f5942ba14e 93 int main()
hiro99ma 0:23f5942ba14e 94 {
hiro99ma 0:23f5942ba14e 95 uint8_t str[] = "Hello.";
hiro99ma 0:23f5942ba14e 96
hiro99ma 1:fa70127b3381 97 DEBUG_PRINTF("%s\n", str);
hiro99ma 1:fa70127b3381 98
hiro99ma 1:fa70127b3381 99 programByte(7, str, sizeof(str));
hiro99ma 1:fa70127b3381 100
hiro99ma 1:fa70127b3381 101 DEBUG_PRINTF("success[%s]\n", getFlash(7));
hiro99ma 0:23f5942ba14e 102 }