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
[BLACK STM32F407VET6:STM32F407] Battery-backed/persistent memory
-
- Posts: 35
- Joined: Fri Apr 12, 2019 8:55 am
Re: [BLACK STM32F407VET6:STM32F407] Battery-backed/persistent memory
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 */
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: [BLACK STM32F407VET6:STM32F407] Battery-backed/persistent memory
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.
Index to my micropython libraries.
-
- Posts: 35
- Joined: Fri Apr 12, 2019 8:55 am
Re: [BLACK STM32F407VET6:STM32F407] Battery-backed/persistent memory
Thanks!
I found there were 4 steps to do:
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!
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();
@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
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?
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
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.