FormazioneSitael / Mbed 2 deprecated TesysRail_Rev_FINALE

Dependencies:   mbed

Committer:
pinofal
Date:
Mon Feb 10 15:47:04 2020 +0000
Revision:
3:307e8b3d4968
Parent:
2:0db92723d44f
Child:
4:3a3ba27a08ee
ticker

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
pinofal 3:307e8b3d4968 10 // tempo in secondi dl Ticker
pinofal 3:307e8b3d4968 11 #define SECPERTICK 5
pinofal 3:307e8b3d4968 12
pinofal 3:307e8b3d4968 13 // numero di volte che il programma entra nella funzione Ticker
pinofal 3:307e8b3d4968 14 int nCountTick;
pinofal 3:307e8b3d4968 15 int nOldCountTick;
pinofal 3:307e8b3d4968 16
pinofal 3:307e8b3d4968 17 // variabili contenenti le tensioni lette con gli ADC
pinofal 3:307e8b3d4968 18 float fOut;
pinofal 3:307e8b3d4968 19 float fPiezo;
pinofal 3:307e8b3d4968 20 float fBatt;
pinofal 3:307e8b3d4968 21
pinofal 3:307e8b3d4968 22 // indice per i cicli
pinofal 3:307e8b3d4968 23 int nIndex;
pinofal 3:307e8b3d4968 24
pinofal 3:307e8b3d4968 25
pinofal 3:307e8b3d4968 26
pinofal 3:307e8b3d4968 27 Ticker TimeTick;
pinofal 3:307e8b3d4968 28
hiro99ma 0:23f5942ba14e 29 namespace {
di_tanno 2:0db92723d44f 30 Serial pc(USBTX, USBRX);
hiro99ma 1:fa70127b3381 31
hiro99ma 0:23f5942ba14e 32 //STM32F411.C/E(DM00119316.pdf p.43 Table 4)
hiro99ma 1:fa70127b3381 33 const struct {
hiro99ma 1:fa70127b3381 34 uint8_t sector;
hiro99ma 1:fa70127b3381 35 uint32_t addr;
hiro99ma 1:fa70127b3381 36 } FLASH_SECTOR[] = {
hiro99ma 1:fa70127b3381 37 {
hiro99ma 1:fa70127b3381 38 FLASH_SECTOR_0,
hiro99ma 1:fa70127b3381 39 (uint32_t)0x08000000 /* Base address of Sector 0, 16 Kbytes */
hiro99ma 1:fa70127b3381 40 },
hiro99ma 1:fa70127b3381 41 {
hiro99ma 1:fa70127b3381 42 FLASH_SECTOR_1,
hiro99ma 1:fa70127b3381 43 (uint32_t)0x08004000 /* Base address of Sector 1, 16 Kbytes */
hiro99ma 1:fa70127b3381 44 },
hiro99ma 1:fa70127b3381 45 {
hiro99ma 1:fa70127b3381 46 FLASH_SECTOR_2,
hiro99ma 1:fa70127b3381 47 (uint32_t)0x08008000 /* Base address of Sector 2, 16 Kbytes */
hiro99ma 1:fa70127b3381 48 },
hiro99ma 1:fa70127b3381 49 {
hiro99ma 1:fa70127b3381 50 FLASH_SECTOR_3,
hiro99ma 1:fa70127b3381 51 (uint32_t)0x0800c000 /* Base address of Sector 3, 16 Kbytes */
hiro99ma 1:fa70127b3381 52 },
hiro99ma 1:fa70127b3381 53 {
hiro99ma 1:fa70127b3381 54 FLASH_SECTOR_4,
hiro99ma 1:fa70127b3381 55 (uint32_t)0x08010000 /* Base address of Sector 4, 64 Kbytes */
hiro99ma 1:fa70127b3381 56 },
hiro99ma 1:fa70127b3381 57 {
hiro99ma 1:fa70127b3381 58 FLASH_SECTOR_5,
hiro99ma 1:fa70127b3381 59 (uint32_t)0x08020000 /* Base address of Sector 5, 128 Kbytes */
hiro99ma 1:fa70127b3381 60 },
hiro99ma 1:fa70127b3381 61 {
hiro99ma 1:fa70127b3381 62 FLASH_SECTOR_6,
hiro99ma 1:fa70127b3381 63 (uint32_t)0x08040000 /* Base address of Sector 6, 128 Kbytes */
hiro99ma 1:fa70127b3381 64 },
hiro99ma 1:fa70127b3381 65 {
hiro99ma 1:fa70127b3381 66 FLASH_SECTOR_7,
hiro99ma 1:fa70127b3381 67 (uint32_t)0x08060000 /* Base address of Sector 7, 128 Kbytes */
hiro99ma 1:fa70127b3381 68 }
hiro99ma 1:fa70127b3381 69 };
hiro99ma 0:23f5942ba14e 70
hiro99ma 0:23f5942ba14e 71 /** @brief 内蔵FLASHへのbyte書込み(セクタ消去有り)
hiro99ma 0:23f5942ba14e 72 *
hiro99ma 0:23f5942ba14e 73 * @param[in] addr 書込み先アドレス(ADDR_FLASH_SECTOR_x)
hiro99ma 0:23f5942ba14e 74 * @param[in] pData 書き込みデータ
hiro99ma 0:23f5942ba14e 75 * @param[in] Len 書き込みデータサイズ
hiro99ma 0:23f5942ba14e 76 */
hiro99ma 1:fa70127b3381 77 void programByte(int sector, const uint8_t *pData, uint8_t Len)
hiro99ma 0:23f5942ba14e 78 {
hiro99ma 0:23f5942ba14e 79 HAL_StatusTypeDef ret;
hiro99ma 0:23f5942ba14e 80
hiro99ma 0:23f5942ba14e 81 /* flash control registerへのアクセス許可 */
hiro99ma 0:23f5942ba14e 82 HAL_FLASH_Unlock();
pinofal 3:307e8b3d4968 83 DEBUG_PRINTF("Unlocked\n\r");
hiro99ma 0:23f5942ba14e 84
hiro99ma 0:23f5942ba14e 85 /* 消去(電圧 [2.7V to 3.6V]) */
hiro99ma 1:fa70127b3381 86 FLASH_Erase_Sector(FLASH_SECTOR[sector].sector, FLASH_VOLTAGE_RANGE_3);
pinofal 3:307e8b3d4968 87 DEBUG_PRINTF("Erased\n\r");
hiro99ma 1:fa70127b3381 88
hiro99ma 0:23f5942ba14e 89 /* 書込み(4byte単位) */
hiro99ma 1:fa70127b3381 90 uint32_t addr = FLASH_SECTOR[sector].addr;
pinofal 3:307e8b3d4968 91 DEBUG_PRINTF("Writing\n\r");
hiro99ma 1:fa70127b3381 92 for (int lp = 0 ; lp < Len; lp++) {
pinofal 3:307e8b3d4968 93 //DEBUG_PRINTF("addr:0x%08x data=%02x\n", addr, *pData);
hiro99ma 0:23f5942ba14e 94 ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, *pData);
hiro99ma 0:23f5942ba14e 95 if (ret != HAL_OK) {
hiro99ma 0:23f5942ba14e 96 //trouble!!
pinofal 3:307e8b3d4968 97 DEBUG_PRINTF("Fail\n\r");
hiro99ma 0:23f5942ba14e 98 while (true) {}
hiro99ma 0:23f5942ba14e 99 }
hiro99ma 0:23f5942ba14e 100 addr++;
hiro99ma 0:23f5942ba14e 101 pData++;
hiro99ma 0:23f5942ba14e 102 }
hiro99ma 0:23f5942ba14e 103
hiro99ma 0:23f5942ba14e 104 /* flash control registerへのアクセス禁止 */
hiro99ma 0:23f5942ba14e 105 HAL_FLASH_Lock();
hiro99ma 0:23f5942ba14e 106 }
hiro99ma 1:fa70127b3381 107
di_tanno 2:0db92723d44f 108 const uint8_t *getFlash(int sector) {
di_tanno 2:0db92723d44f 109 return (const uint8_t *)FLASH_SECTOR[sector].addr; }
hiro99ma 0:23f5942ba14e 110 }
hiro99ma 0:23f5942ba14e 111
pinofal 3:307e8b3d4968 112 void TimeTickFunction()
pinofal 3:307e8b3d4968 113 {
pinofal 3:307e8b3d4968 114 // indice di tick
pinofal 3:307e8b3d4968 115 nCountTick++;
pinofal 3:307e8b3d4968 116
pinofal 3:307e8b3d4968 117 // leggo i valori dagli ADC
pinofal 3:307e8b3d4968 118 fOut++;
pinofal 3:307e8b3d4968 119 fPiezo++;
pinofal 3:307e8b3d4968 120 fBatt++;
pinofal 3:307e8b3d4968 121
pinofal 3:307e8b3d4968 122 // visualizza i valori
pinofal 3:307e8b3d4968 123 pc.printf("\r\n-- Nel ticker --\r\n");
pinofal 3:307e8b3d4968 124 pc.printf("Seconds= %d; fOut = %.3f; fPiezo = %.3f; fBatt = %.3f\n\r", (nCountTick*SECPERTICK), fOut, fPiezo, fBatt); // scrive linea completa
pinofal 3:307e8b3d4968 125 }
hiro99ma 0:23f5942ba14e 126
hiro99ma 0:23f5942ba14e 127 int main()
hiro99ma 0:23f5942ba14e 128 {
pinofal 3:307e8b3d4968 129 nCountTick=0;
pinofal 3:307e8b3d4968 130 nOldCountTick =-1;
pinofal 3:307e8b3d4968 131 char caLineToWrite[]="Seconds= 1800; fOut = 4.531; fPiezo = 19.872; fBatt = 3.523";
pinofal 3:307e8b3d4968 132 //uint8_t str[]="Seconds= 0000; fOut = 00000; fPiezo = 000000; fBatt = 00000000";
di_tanno 2:0db92723d44f 133
pinofal 3:307e8b3d4968 134 uint8_t str[8000];
pinofal 3:307e8b3d4968 135 long nIndexFlash=0;
pinofal 3:307e8b3d4968 136 fOut = 4.531;
pinofal 3:307e8b3d4968 137 fPiezo = 9.872;
pinofal 3:307e8b3d4968 138 fBatt = 3.523;
pinofal 3:307e8b3d4968 139
pinofal 3:307e8b3d4968 140 // resetta array
pinofal 3:307e8b3d4968 141 for(nIndex=0; nIndex < 8000; nIndex++)
pinofal 3:307e8b3d4968 142 {
pinofal 3:307e8b3d4968 143 str[nIndex] =0;
pinofal 3:307e8b3d4968 144 }
pinofal 3:307e8b3d4968 145 TimeTick.attach(&TimeTickFunction,SECPERTICK);
pinofal 3:307e8b3d4968 146 while(true)
pinofal 3:307e8b3d4968 147 {
pinofal 3:307e8b3d4968 148 if(nCountTick != nOldCountTick)
pinofal 3:307e8b3d4968 149 {
pinofal 3:307e8b3d4968 150 nOldCountTick = nCountTick;
pinofal 3:307e8b3d4968 151 sprintf(caLineToWrite, "Seconds= %d; fOut = %.3f; fPiezo = %.3f; fBatt = %.3f", (nCountTick*SECPERTICK), fOut, fPiezo, fBatt); // scrive linea completa
pinofal 3:307e8b3d4968 152 for(nIndex =0; nIndex < strlen(caLineToWrite); nIndex++) // trasferisce linea completa in formato leggibile da programByte
pinofal 3:307e8b3d4968 153 {
pinofal 3:307e8b3d4968 154 str[nIndex] = caLineToWrite[nIndex];
pinofal 3:307e8b3d4968 155 }
pinofal 3:307e8b3d4968 156 //str[nIndex++] = NULL;
pinofal 3:307e8b3d4968 157
pinofal 3:307e8b3d4968 158 //pc.printf("%s\n\r", caLineToWrite);
pinofal 3:307e8b3d4968 159 //DEBUG_PRINTF("%s\n\r", caLineToWrite);
pinofal 3:307e8b3d4968 160
pinofal 3:307e8b3d4968 161 programByte(4, str, sizeof(str));
pinofal 3:307e8b3d4968 162 wait(1);
pinofal 3:307e8b3d4968 163 DEBUG_PRINTF("%s\n\r", getFlash(4));
pinofal 3:307e8b3d4968 164 }
pinofal 3:307e8b3d4968 165 }
pinofal 3:307e8b3d4968 166 /*
pinofal 3:307e8b3d4968 167 programByte(4, str, sizeof(str));
pinofal 3:307e8b3d4968 168 DEBUG_PRINTF("success[%s]\n\r", getFlash(4));
pinofal 3:307e8b3d4968 169 */
pinofal 3:307e8b3d4968 170
pinofal 3:307e8b3d4968 171
pinofal 3:307e8b3d4968 172 /*
di_tanno 2:0db92723d44f 173 uint8_t str[] = "t:hh:mm:Ss;P=19.2;B=3.2;O=3.1";
di_tanno 2:0db92723d44f 174
hiro99ma 0:23f5942ba14e 175
hiro99ma 1:fa70127b3381 176 DEBUG_PRINTF("%s\n", str);
hiro99ma 1:fa70127b3381 177
di_tanno 2:0db92723d44f 178 programByte(4, str, sizeof(str));
di_tanno 2:0db92723d44f 179 while(true)
di_tanno 2:0db92723d44f 180 {
di_tanno 2:0db92723d44f 181 DEBUG_PRINTF("success[%s]\n\r", getFlash(4));
di_tanno 2:0db92723d44f 182 DEBUG_PRINTF("success[%s]\n\r", getFlash(7));
di_tanno 2:0db92723d44f 183 wait(4);
di_tanno 2:0db92723d44f 184 }
pinofal 3:307e8b3d4968 185 */
hiro99ma 1:fa70127b3381 186
hiro99ma 0:23f5942ba14e 187 }