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

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
ProudPagan
Posts: 35
Joined: Fri Apr 12, 2019 8:55 am

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

Post by ProudPagan » Mon Jul 08, 2019 4:37 am

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

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

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

Post by jimmo » Mon Jul 08, 2019 6:30 am

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                         */

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

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

Post by pythoncoder » Tue Jul 09, 2019 8:34 am

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.
Peter Hinch
Index to my micropython libraries.

ProudPagan
Posts: 35
Joined: Fri Apr 12, 2019 8:55 am

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

Post by ProudPagan » Tue Jul 09, 2019 3:13 pm

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!

bradstew
Posts: 41
Joined: Thu Nov 29, 2018 9:29 pm

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

Post by bradstew » Wed Jun 30, 2021 11:22 pm

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

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

Post by dhylands » Thu Jul 01, 2021 3:15 am

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.

Post Reply