FormazioneSitael / Mbed 2 deprecated TesysRail_Rev_FINALE

Dependencies:   mbed

main.cpp

Committer:
pinofal
Date:
2020-02-10
Revision:
3:307e8b3d4968
Parent:
2:0db92723d44f
Child:
4:3a3ba27a08ee

File content as of revision 3:307e8b3d4968:

#include "mbed.h"
#include "stm32f4xx_hal_flash.h"

#if 1
#define DEBUG_PRINTF        pc.printf
#else
#define DEBUG_PRINTF(...)
#endif

// tempo in secondi dl Ticker
#define SECPERTICK 5

// numero di volte che il programma entra nella funzione Ticker
int nCountTick;
int nOldCountTick;

// variabili contenenti le tensioni lette con gli ADC
float fOut;
float fPiezo;
float fBatt;

// indice per i cicli
int nIndex;



Ticker TimeTick;

namespace {
    Serial pc(USBTX, USBRX);
    
    //STM32F411.C/E(DM00119316.pdf p.43 Table 4)
    const struct {
        uint8_t     sector;
        uint32_t    addr;
    } FLASH_SECTOR[] = {
        {
            FLASH_SECTOR_0,
            (uint32_t)0x08000000      /* Base address of Sector 0,  16 Kbytes  */
        },
        {
            FLASH_SECTOR_1,
            (uint32_t)0x08004000      /* Base address of Sector 1,  16 Kbytes  */
        },
        {
            FLASH_SECTOR_2,
            (uint32_t)0x08008000      /* Base address of Sector 2,  16 Kbytes  */
        },
        {
            FLASH_SECTOR_3,
            (uint32_t)0x0800c000      /* Base address of Sector 3,  16 Kbytes  */
        },
        {
            FLASH_SECTOR_4,
            (uint32_t)0x08010000      /* Base address of Sector 4,  64 Kbytes  */
        },
        {
            FLASH_SECTOR_5,
            (uint32_t)0x08020000      /* Base address of Sector 5, 128 Kbytes  */
        },
        {
            FLASH_SECTOR_6,
            (uint32_t)0x08040000      /* Base address of Sector 6, 128 Kbytes  */
        },
        {
            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(int sector, const uint8_t *pData, uint8_t Len)
    {
        HAL_StatusTypeDef ret;
    
        /* flash control registerへのアクセス許可 */
        HAL_FLASH_Unlock();
        DEBUG_PRINTF("Unlocked\n\r");
    
        /* 消去(電圧 [2.7V to 3.6V]) */
        FLASH_Erase_Sector(FLASH_SECTOR[sector].sector, FLASH_VOLTAGE_RANGE_3);
        DEBUG_PRINTF("Erased\n\r");

        /* 書込み(4byte単位) */
        uint32_t addr = FLASH_SECTOR[sector].addr;
        DEBUG_PRINTF("Writing\n\r");
        for (int lp = 0 ; lp < Len; lp++) {
            //DEBUG_PRINTF("addr:0x%08x  data=%02x\n", addr, *pData);
            ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, *pData);
            if (ret != HAL_OK) {
                //trouble!!
                DEBUG_PRINTF("Fail\n\r");
                while (true) {}
            }
            addr++;
            pData++;
        }
    
        /* flash control registerへのアクセス禁止 */
        HAL_FLASH_Lock();
    }
    
    const uint8_t *getFlash(int sector) {
         return (const uint8_t *)FLASH_SECTOR[sector].addr; }
}

void TimeTickFunction()
{
    // indice di tick
    nCountTick++;
    
    // leggo i valori dagli ADC
    fOut++;
    fPiezo++;
    fBatt++;
    
    // visualizza i valori
    pc.printf("\r\n-- Nel ticker --\r\n");
    pc.printf("Seconds= %d; fOut = %.3f; fPiezo = %.3f; fBatt = %.3f\n\r", (nCountTick*SECPERTICK), fOut, fPiezo, fBatt); // scrive linea completa
}

int main()
{
    nCountTick=0;
    nOldCountTick =-1;
    char caLineToWrite[]="Seconds= 1800; fOut = 4.531; fPiezo = 19.872; fBatt = 3.523"; 
    //uint8_t str[]="Seconds= 0000; fOut = 00000; fPiezo = 000000; fBatt = 00000000";
    
    uint8_t str[8000];
    long nIndexFlash=0;
    fOut = 4.531;
    fPiezo = 9.872;
    fBatt = 3.523;
    
    // resetta array
    for(nIndex=0; nIndex < 8000; nIndex++)
    {
        str[nIndex] =0;
    }
    TimeTick.attach(&TimeTickFunction,SECPERTICK);
    while(true)
    {
        if(nCountTick != nOldCountTick)
        {
            nOldCountTick = nCountTick;
            sprintf(caLineToWrite, "Seconds= %d; fOut = %.3f; fPiezo = %.3f; fBatt = %.3f", (nCountTick*SECPERTICK), fOut, fPiezo, fBatt); // scrive linea completa
            for(nIndex =0; nIndex < strlen(caLineToWrite); nIndex++) // trasferisce linea completa in formato leggibile da programByte
            {
                str[nIndex] = caLineToWrite[nIndex];
            }
            //str[nIndex++] = NULL;
                
            //pc.printf("%s\n\r", caLineToWrite);    
            //DEBUG_PRINTF("%s\n\r", caLineToWrite);
            
            programByte(4, str, sizeof(str));
            wait(1);
            DEBUG_PRINTF("%s\n\r", getFlash(4));
        }    
    }
    /*
    programByte(4, str, sizeof(str));
    DEBUG_PRINTF("success[%s]\n\r", getFlash(4));
    */
    
    
    /*
    uint8_t str[] = "t:hh:mm:Ss;P=19.2;B=3.2;O=3.1";
    
    
    DEBUG_PRINTF("%s\n", str);
    
    programByte(4, str, sizeof(str));
    while(true)
    {
        DEBUG_PRINTF("success[%s]\n\r", getFlash(4));
        DEBUG_PRINTF("success[%s]\n\r", getFlash(7));
        wait(4);
    }
    */
    
}