cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F411RC SRAM or FLASH USE to store static data

jay1991
Associate II
Posted on May 24, 2016 at 12:55

Hello Everyone, I am using STM32F411RC. Here i want to store a variable as static. We can say that if we power off the MCU then also it will have same variable. how can i do that. I want to Read and write in SRAM or Flash.Any suggestions?

#stm32f #rewrite #sram #flash #stm32f411rc #!stm32f4
9 REPLIES 9
Posted on May 24, 2016 at 14:51

How large is the structure, and how often do you update it?

Have you considered using the battery backed SRAM region?

For flash, you'd want to use one of the smaller blocks, as they erase much quicker. You could journal the structure across the block, and pick the most recent one. Depending on how critical the data is you could ping-pong between two blocks, and erase the old block once you'd safely written to the other.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jay1991
Associate II
Posted on May 24, 2016 at 15:00

I want to store one phone number and a string. or 3 phone numbers.

jay1991
Associate II
Posted on May 24, 2016 at 15:02

I have used this code

int8_t write_to_backup_sram( uint8_t *data, uint16_t bytes, uint16_t offset )
{
uint8_t* base_addr = (uint8_t *) BKPSRAM_BASE;
uint16_t i;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); 
PWR_BackupAccessCmd(ENABLE); 
PWR_BackupRegulatorCmd(ENABLE); 
for
( i = 0; i < bytes; i++ ) { 
*(base_addr + offset + i) = *(data + i); 
//TEST VALUES
}
PWR_BackupAccessCmd(DISABLE); 
return
0;
}

Posted on May 24, 2016 at 15:26

uint8_t* base_addr = (uint8_t *) BKPSRAM_BASE;

The 'F411 has no BKPSRAM. You can store up to 80 bytes in the RTC_BKPxR registers, though. Refer to the RTC chapter in RM. I can't help with ''libraries'', I don't use them. JW
Walid FTITI_O
Senior II
Posted on May 24, 2016 at 16:16

Hi katira.jay,

IftheRTCis already used/enabled in the backup domain and you only need to store <= 80 bytes, then you can succefully store in RTC backup registers.

Here is a model code to follow to write in those registers:

int8_t write_to_backup_rtc( uint32_t *data, uint16_t bytes, uint16_t offset ) { 
const uint16_t backup_size = 80; 
volatile uint32_t* base_addr = &(RTC->BKP0R); 
uint16_t i; 
/* disable backup domain write protection */ 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); 
PWR_BackupAccessCmd(ENABLE); 
for( i = 0; i < bytes; i++ ) { 
*(base_addr + offset + i) = *(data + i); 
} 
PWR_BackupAccessCmd(DISABLE); 
return 0; 
} 

-Hannibal-

jay1991
Associate II
Posted on May 25, 2016 at 07:14

Hello Hannibal, Here, offset is 4 right? or i can use any number as per addresses available.

jay1991
Associate II
Posted on May 25, 2016 at 07:15

Thank you JW and Hannible.

jay1991
Associate II
Posted on May 25, 2016 at 11:37

Hello hannibal, I am little confused that what should be there in offset , byte and data.

I understand that data what i want to enter then how much byte of data i want to store and whats for offset?

Help me with this

Walid FTITI_O
Senior II
Posted on May 25, 2016 at 13:15

Hi ira.jay,

It is the RTC_BKPxR register offset wich is 0x50 to 0x9C for the 20 register starting from BKP0R.

If you are using Hal library there is a dedicated functions for that

HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)

and

HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister

And you shoud initialize your BackupRegister as:

uint32_t aBKPDataReg[BACKUP_COUNT] =

{

RTC_BKP_DR0, RTC_BKP_DR1, RTC_BKP_DR2,

RTC_BKP_DR3, RTC_BKP_DR4, RTC_BKP_DR5,

RTC_BKP_DR6, RTC_BKP_DR7, RTC_BKP_DR8,

RTC_BKP_DR9, RTC_BKP_DR10, RTC_BKP_DR11,

RTC_BKP_DR12, RTC_BKP_DR13, RTC_BKP_DR14,

RTC_BKP_DR15

};

with BACKUP_COUNT = 20

You can refer to ''RTC_Tamper'' example in STM32CubeF4 at this path STM32Cube_FW_F4_V1.12.0\Projects\STM324xG_EVAL\Examples\RTC\RTC_Tamper

-Hannibal-