STM32G030F6P6 Backup Registers

Post here first, or if you can't find a relevant section!
ccsk
Posts: 4
Joined: Fri Oct 20, 2023 5:36 pm

STM32G030F6P6 Backup Registers

Post by ccsk »

I'm trying to access the backup registers of the STM32G030F6P6 with the functions in backup.h https://github.com/stm32duino/Arduino_C ... 2/backup.h

This is the code:

Code: Select all

#include <SoftwareSerial.h>

SoftwareSerial myserial(PA7, PA8); // RX, TX

uint32_t WriteRegisterValue = 123456;
uint32_t ReadRegisterValue;


void setup() {
  myserial.begin(9600);
  delay (1000);
  myserial.println("test");

  enableBackupDomain(); 
}

void loop() {

  //Store number in RTC Backup Registers
  setBackupRegister(4, WriteRegisterValue);
  
 
  delay(2000);
  
  //Retrieve number in RTC Backup Registers
  ReadRegisterValue = getBackupRegister(4);
  myserial.println(ReadRegisterValue);
  
  
  delay(2000);
}
The issue I'm facing is that the value I get when I read the backupregister is 0 instead of 123456.

Maybe an issue with my code?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32G030F6P6 Backup Registers

Post by fpiSTM »

Will check monday.
ccsk
Posts: 4
Joined: Fri Oct 20, 2023 5:36 pm

Re: STM32G030F6P6 Backup Registers

Post by ccsk »

Thank you.

I just uploaded the same code to a Blue Pill and it works. I believe it's an issue with the implementation of the backup registers with the STM32G030F6P6
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: STM32G030F6P6 Backup Registers

Post by dannyf »

does the functionality (=backup data registers) even exist on the G0?
ccsk
Posts: 4
Joined: Fri Oct 20, 2023 5:36 pm

Re: STM32G030F6P6 Backup Registers

Post by ccsk »

Yes, from page 25 of the data sheet:

The device embeds an RTC and five 32-bit backup registers, located in the RTC domain of the silicon die.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: STM32G030F6P6 Backup Registers

Post by dannyf »

I think you are right, re. the datasheet mentioning it in the RTC chapter.

the reference manual mentions none of that in the RTC chapter. However, it mentions those backup registers (all 5 of them), in the TAMPER chapter :)

doesn't sound that difficult to write your own code for it.
ccsk
Posts: 4
Joined: Fri Oct 20, 2023 5:36 pm

Re: STM32G030F6P6 Backup Registers

Post by ccsk »

Yes thank you for the tip. I was able to come up with this code that works:

Code: Select all

#include <SoftwareSerial.h>

SoftwareSerial myserial(PA7, PA8); // RX, TX

uint32_t WriteRegisterValue = 254;
uint32_t ReadRegisterValue;


void setup() {
  myserial.begin(9600);
  delay (1000);
  myserial.println("test");

  //Enable backup registers
  RCC->APBENR1 |= 1 << 28; //SET PWREN bit to 1
  RCC->APBENR1 |= 1 << 10; //SET RTCAPBEN bit to 1
  PWR->CR1 |= 1 << 8; //SET DPB bit to 1
  RCC->BDCR |= 1 << 15; //SET RTCEN to 1

  
}

void loop() {

  //Store number in RTC Backup Registers
  //setBackupRegister(0, WriteRegisterValue);
  TAMP->BKP4R = WriteRegisterValue;

  
 
  delay(2000);
  
  //Retrieve number in RTC Backup Registers
  //ReadRegisterValue = getBackupRegister(0);
  //myserial.println(ReadRegisterValue);
  myserial.println(TAMP->BKP4R);
 
  
  delay(2000);
}
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: STM32G030F6P6 Backup Registers

Post by dannyf »

I can confirm that it works as well on my G0:

Code: Select all

//unlock the domain
//on some chips, it requires a set of magic numbers
#define bkpUnlock()		do {PWR->CR1 |= PWR_CR1_DBP;} while (!(PWR->CR1 & PWR_CR1_DBP))	//1->enable write assess, 0->disable write assess
#define bkpLock()		do {PWR->CR1 &=~PWR_CR1_DBP;} while ( (PWR->CR1 & PWR_CR1_DBP))	//1->enable write assess, 0->disable write assess

//reset the tamper to use the backup registers
void bkpInit(void) {
	//set dbp bit in pwr
	RCC->APBENR1 |= RCC_APBENR1_PWREN | RCC_APBENR1_RTCAPBEN;			//1->enable clock to pwr, 0->disable clock to pwr
	RCC->BDCR |= RCC_BDCR_RTCEN;				//enable RTC clock

	//PWR->CR1 |= PWR_CR1_DBP;					//1->enable write assess, 0->disable write access
}

//read backup register: 0..4 on G030
#define bkp0Get(n)		(TAMP->BKP0R)			//read bkp0
#define bkp0Set(val)	do {bkpUnlock(); TAMP->BKP0R = (val); bkpLock();} while (0)		//write bkp0
#define bkp1Get(n)		(TAMP->BKP1R)			//read bkp1
#define bkp1Set(val)	TAMP->BKP1R = (val)		//write bkp1
#define bkp2Get(n)		(TAMP->BKP2R)			//read bkp2
#define bkp2Set(val)	TAMP->BKP2R = (val)		//write bkp2
#define bkp3Get(n)		(TAMP->BKP3R)			//read bkp3
#define bkp3Set(val)	TAMP->BKP3R = (val)		//write bkp3
#define bkp4Get(n)		(TAMP->BKP4R)			//read bkp4
#define bkp4Set(val)	TAMP->BKP4R = (val)		//write bkp4
and in the user application:

Code: Select all

		u2Print("bkp0  =                    ", bkp0=bkp0Get()); bkp0Set(bkp0Get() + 1);	//READ_BIT back bkp0

across debug sessions (where the chip is reset), the value in bkp0/TAMP->BKP0R persisted.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: STM32G030F6P6 Backup Registers

Post by dannyf »

Our codes are identical.

In my case, I used this to initialize BKP0R in setup():

Code: Select all

	//tamper / backup registers
	bkpInit();									//reset the tamper
	if (bkp0Get()==0) bkp0Set(1);				//reset backup register - only during the first wrong
and I incremented theh value in BKP0R and I could watch it continued to increment from the prior debug session.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32G030F6P6 Backup Registers

Post by fpiSTM »

Post Reply

Return to “General discussion”