FormazioneSitael / Mbed 2 deprecated SITAEL-TesysRail_Diag_Rev2_FINALE

Dependencies:   mbed

Committer:
di_tanno
Date:
Mon Feb 10 11:56:18 2020 +0000
Revision:
2:0db92723d44f
Parent:
1:fa70127b3381
Child:
3:307e8b3d4968
yugyt

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 {
di_tanno 2:0db92723d44f 11 Serial pc(USBTX, USBRX);
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
di_tanno 2:0db92723d44f 88 const uint8_t *getFlash(int sector) {
di_tanno 2:0db92723d44f 89 return (const uint8_t *)FLASH_SECTOR[sector].addr; }
hiro99ma 0:23f5942ba14e 90 }
hiro99ma 0:23f5942ba14e 91
hiro99ma 0:23f5942ba14e 92
hiro99ma 0:23f5942ba14e 93
hiro99ma 0:23f5942ba14e 94 int main()
hiro99ma 0:23f5942ba14e 95 {
di_tanno 2:0db92723d44f 96
di_tanno 2:0db92723d44f 97 uint8_t str[] = "t:hh:mm:Ss;P=19.2;B=3.2;O=3.1";
di_tanno 2:0db92723d44f 98
hiro99ma 0:23f5942ba14e 99
hiro99ma 1:fa70127b3381 100 DEBUG_PRINTF("%s\n", str);
hiro99ma 1:fa70127b3381 101
di_tanno 2:0db92723d44f 102 programByte(4, str, sizeof(str));
di_tanno 2:0db92723d44f 103 while(true)
di_tanno 2:0db92723d44f 104 {
di_tanno 2:0db92723d44f 105 DEBUG_PRINTF("success[%s]\n\r", getFlash(4));
di_tanno 2:0db92723d44f 106 DEBUG_PRINTF("success[%s]\n\r", getFlash(7));
di_tanno 2:0db92723d44f 107 wait(4);
di_tanno 2:0db92723d44f 108 }
hiro99ma 1:fa70127b3381 109
hiro99ma 0:23f5942ba14e 110 }