Page 1 of 1

[BLACK STM32F407VET6:STM32F407] Battery-backed/persistent memory

Posted: Mon Jul 08, 2019 4:37 am
by ProudPagan
Hi,

I would like to store some invariant data on the BLACK STM32F407VET6 board while
the main power is removed (coin cell remains connected).

The STM32F407 has 4kByte of battery backed SRAM. Is this accessible in MicroPython?

Thanks

PP

Re: [BLACK STM32F407VET6:STM32F407] Battery-backed/persistent memory

Posted: Mon Jul 08, 2019 6:30 am
by jimmo
I don't believe there's a Python-level API for this, however it's fairly straightforward to enable by setting a few bits in registers. See "Access to the backup SRAM" on p119 of RM0090. It's referred to by both "Backup SRAM" and "BKPSRAM" in the docs. Once enabled, you can access it from micropython using machine.mem8/16/32.

Code: Select all

CMSIS/STM32F4xx/Include/stm32f407xx.h
928:#define BKPSRAM_BASE          0x40024000U /*!< Backup SRAM(4 KB) base address in the alias region                         */

Re: [BLACK STM32F407VET6:STM32F407] Battery-backed/persistent memory

Posted: Tue Jul 09, 2019 8:34 am
by pythoncoder
You'll find some code for doing this on the Pyboard here (upower.py). I haven't checked if it works on the '407 but it might be worth a try.

Re: [BLACK STM32F407VET6:STM32F407] Battery-backed/persistent memory

Posted: Tue Jul 09, 2019 3:13 pm
by ProudPagan
Thanks!

I found there were 4 steps to do:

Code: Select all

	
	//set Backup regulator enable -- do once
	SET_BIT(PWR->CSR, PWR_CSR_BRE);

	//enable power interface clock
        __HAL_RCC_PWR_CLK_ENABLE();
	
	//enable BBRAM interface clock
	SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_BKPSRAMEN);

	//Enable access to RTC domain by setting the DBP bit
	//DBP = Disable Backup Protection
	HAL_PWR_EnableBkUpAccess();
	
Full code here.

@pythoncoder -- Your implementation is way more complete -- I wouldn't have tried to implement my own had I seen your reply earlier!

Re: [BLACK STM32F407VET6:STM32F407] Battery-backed/persistent memory

Posted: Wed Jun 30, 2021 11:22 pm
by bradstew
I am able to read and write to this region of SRAM with MicroPython with a STM32F405 (same as PyBoard) using upower.py.
Is it safe from normal MicroPython programs? In other words, is there a chance the bytecode or data can overwrite this region?

Re: [BLACK STM32F407VET6:STM32F407] Battery-backed/persistent memory

Posted: Thu Jul 01, 2021 3:15 am
by dhylands
The 4K battery backed up SRAM is outside of the heap, so you need to access it specially, and "regular" python programs won't touch it.