cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F746G-DISCO, memory

AD�?b
Senior

He

How to remember the variable in STM32F746G-DISCO to restore it after power off?

best regards

Andrzej

1 ACCEPTED SOLUTION

Accepted Solutions
AD�?b
Senior

Thank you.

I will try.

best regards

Andrzej

View solution in original post

3 REPLIES 3
TDK
Guru

You can store data in flash, which is nonvolatile memory, but it's more effort than just using random access memory.

https://github.com/STMicroelectronics/STM32CubeF7/tree/master/Projects/STM32746G-Discovery/Applications/EEPROM/EEPROM_Emulation

If you feel a post has answered your question, please click "Accept as Solution".
MS.9
Associate III

Here is something I did the other day to write to the QUADspi flash memory on the Disco board

This is for the STM32F469I-Disco, but I think it'll work on the board u are using.

It assumes the chip is enabled elsewhere (see the BSP and CubeMX for examples)

It uses a single 64K block - its very hard coded for this, and it could be improved to make it more flexable - but as it is, its easy to see whats going on.

Since the block erase size is 64K (can be 4K if needed), and I've chosen 512 as the write size, you'll get 128 writes before you need to erase the sector - keeps the erase cycles down. Clearly this can be increased as needed.

So it writes consecutive 512 chunks until the 64K is full, then erases and starts again - if you're dont understand this bit , go read about this chip.

The FindLatest function gets the latest written structure and where to write the next.

The usedSignature tells which 512 chunks are used.

I've sort of tested it, and seems to work - if not it'll give you a help on what to look for in the docs and example code.

#include "cmsis_os.h"
#include "settings.h"
#include "stm32469i_discovery_qspi.h"
 
#define UsedSignature 0xFACEF00D
#define SettingsBlockSize 512
#define NumSettings 128         //can hold 128 512byte blocks in 64K
 
static SystemSettings settings;     //user settings - whole system accessses this structure
static SystemSettings readSettings; //temp for reading 
 
#define BaseAddress  0x00000 //address offset in QUADspi memory (should really use memory config loader thingy)
#define SectorNumber 0		 //sector number in QUADspi relating to the address.
 
static int FindLatest();
 
int eraseCount = 0;	//to stop broken application
 
//uses QUADspi flash memory - 
//uses SettingsBlockSize granularity in a single 64K sector.
//setting are written sequentially through the 64K memory (saves writing same block over and over)
//if it fills, the sectors are erased and starts from first address
//
//define a data structure to write something like this.
//typedef struct
//{
//    uint32_t usedSignature;  // need this.
//    uint32_t myValue1;
//    uint32_t myValue2;
//
//} SystemSettings;
//NOTE will all this work while TouchGFX is using memory mapped access.
 
//-------------------------------------------------------------------------------------
//get data last read and place to put the latest values for writing.
//
SystemSettings* GetSettings()
{
    return &settings;
}
//-------------------------------------------------------------------------------------
//copy to read structure and return true if all good
//
bool ReadSettings ()
{
    //coding error, need to increase block write size
    //will optimise out?
    //
    if (sizeof(SystemSettings) > 512)
    {
        Error_Handler();
    }
    //look for the latest recorded setting
    //
    int last = FindLatest();
    if (last < 0)
    {
        return false;
    }
    memcpy(&settings, &readSettings, sizeof (SystemSettings));
    return true;
   
}
//-------------------------------------------------------------------------------------
//write the contents of settings to the next free slot
//
bool WriteSettings ()
{
    settings.usedSignature = UsedSignature;
    int last = FindLatest();
    if (last <0)
    {
        EraseSettings();
        last=-1;
    }
    int newSlot = last +1;
    uint8_t status  = BSP_QSPI_Write((uint8_t*)&settings, BaseAddress + newSlot*SettingsBlockSize, sizeof(SystemSettings));
    return  status == QSPI_OK;
 
}
//-------------------------------------------------------------------------------------
//erase the sector
//
bool EraseSettings()
{
    //check app not gone mad
    //if had 128*5 writes then app has gone doolally
    //
    if (++eraseCount > 5)
    {
		return false;
    }
    uint8_t status =BSP_QSPI_Erase_Block(SectorNumber);
    return  status == QSPI_OK;
}
//-------------------------------------------------------------------------------------
//look for the latest used slot (the latest version of the stored data)
static int FindLatest()
{
    
    for (int i=0;i<NumSettings;i++)
    {
        uint8_t status = BSP_QSPI_Read((uint8_t*)&readSettings, BaseAddress + i* SettingsBlockSize, sizeof(SystemSettings));
        if (status != QSPI_OK)
        {
            return -3;
        }
        if (readSettings.usedSignature == 0xFFFFFFFF)
        {
            //return previous slot
            //
            return i-1;
        }
        if (readSettings.usedSignature != 0xFACEF00D)
        {
            //corrupt (not 0xFFFFFFFF or 0xFACEF00D)
            //
            return -2;
        }
    }
    //no data
    //
    return -1;
}

AD�?b
Senior

Thank you.

I will try.

best regards

Andrzej