Can be stored data inside non volative memory ?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
BOB63
Posts: 58
Joined: Sat Jul 25, 2015 8:24 pm
Location: Monza , Italy

Can be stored data inside non volative memory ?

Post by BOB63 » Sun Sep 27, 2015 8:03 pm

Hi,
I've the need to store some data received by serial so that in case of power drop at the power up I can recall these data to be used as setting for my project.
I know that I could write the data inside a csv file but I'm curious to know if like can be done with a PIC there is an area of memory as EEPROM that can be used for this scope , and if possible have some example about how implement ?

Thanks.
Thanks. Roberto

User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Re: Can be stored data inside non volative memory ?

Post by dbc » Mon Sep 28, 2015 1:26 am

Yes, both the left over flash memory and the sdcard can be written. See http://docs.micropython.org/en/latest/library/os.html

This thread might give you some clues and warnings: http://forum.micropython.org/viewtopic.php?t=86

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

Re: Can be stored data inside non volative memory ?

Post by dhylands » Mon Sep 28, 2015 5:26 am

There is a small amount of RAM (4 Kbytes) whose contents are retained as long as VBAT is supplied (typically using a coin cell).

Otherwise, you can use the flash file system to store information in a file.

User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Re: Can be stored data inside non volative memory ?

Post by dbc » Mon Sep 28, 2015 6:25 pm

How do you get to that RAM? Is it part of the file system and not given over to the memory allocator?

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

Re: Can be stored data inside non volative memory ?

Post by dhylands » Tue Sep 29, 2015 3:36 am

The datasheet (document RM0090) shows (on page 65) that the backup lives at address 0x40024000 - 0x40024fff

Page 118 has most of the stuff that needs to be initialized, although I think that the BRE bit (which enables the backup regulator) is needed in order to have this RAM retain its contents when VBAT is present).

So that 4K block is not included in the normal RAM that MicroPython processes, and is also not part of the file system.

BOB63
Posts: 58
Joined: Sat Jul 25, 2015 8:24 pm
Location: Monza , Italy

Re: Can be stored data inside non volative memory ?

Post by BOB63 » Tue Sep 29, 2015 7:02 pm

dbc and Dave,
thank you so much for your suggestions.
For sure the use of file system is the most easy way , and I've already use in the past.
Let me see if I'm able to complicate my life trying to address the memory directly. :lol:
I can't promise good results.. ;)

Thanks again.
Thanks. Roberto

User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Re: Can be stored data inside non volative memory ?

Post by dbc » Wed Sep 30, 2015 2:49 am

bob63,

I may not have completely answered your whole question. So just to follow up, ST parts do not generally have EEPROM as a separate storage structure as found on some chips. ST expects you to use left-over program flash for that, and publishes an ap note on how to do that from low level code. In general, it isn't as nice a solution, since program flash must be erased and reprogrammed a page at a time. Most EEPROM (like on the AVR chips, for instance) is programmable a single byte at a time. Also, Atmel makes EEPROM using larger structures so that they can withstand more write cycles before dying.

In the case of the ST chip here, I suspect they expect you to use the battery-backed SRAM in the place of EEPROM.

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

Re: Can be stored data inside non volative memory ?

Post by pythoncoder » Wed Sep 30, 2015 12:55 pm

@BOB63 For most purposes I'd use the filesystem, either via file read and write or via Pickle. However if you want to use the backup RAM - in particular if you want the RAM contents to be accessible after recovery from pyb.standby() - the following seems OK.

Code: Select all

import stm
stm.mem32[stm.RCC + stm.RCC_APB1ENR] |= 0x10000000 # PWREN bit
stm.mem32[stm.PWR + stm.PWR_CR] |= 0x100 # Set the DBP bit in the PWR power control register
stm.mem32[stm.RCC +stm.RCC_AHB1ENR]|= 0x40000 # enable BKPSRAMEN
stm.mem32[stm.PWR + stm.PWR_CSR] |= 0x200 # BRE backup register enable bit
To faciltate accessing the RAM as an array of 32 bit words I use the following:

Code: Select all

class BkpRAM(object):
    def __getitem__(self, idx):
        assert idx >= 0 and idx <= 0x3ff, "Index must be between 0 and 1023"
        return stm.mem32[BKPSRAM + idx * 4]
    def __setitem__(self, idx, val):
        assert idx >= 0 and idx <= 0x3ff, "Index must be between 0 and 1023"
        stm.mem32[BKPSRAM + idx * 4] = val
b = BkpRAM()
b[0] += 1 # Modify the contents of RAM address 0
Peter Hinch

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: Can be stored data inside non volative memory ?

Post by blmorris » Wed Sep 30, 2015 3:18 pm

@pythoncoder - Nice! That is a great example of using stm to elegantly expose an otherwise unsupported function in Python. I'm still learning, none of my efforts in that direction were nearly as pretty ;)

-Bryan

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

Re: Can be stored data inside non volative memory ?

Post by pythoncoder » Thu Oct 01, 2015 6:34 am

@blmorris Thank you ;)

The code can be made neater by putting the mem32 stuff in the constructor thus:

Code: Select all

import stm

class BkpRAM(object):
    BKPSRAM = 0x40024000
    def __init__(self):
      stm.mem32[stm.RCC + stm.RCC_APB1ENR] |= 0x10000000 # PWREN bit
      stm.mem32[stm.PWR + stm.PWR_CR] |= 0x100 # Set the DBP bit in the PWR power control register
      stm.mem32[stm.RCC +stm.RCC_AHB1ENR]|= 0x40000 # enable BKPSRAMEN
      stm.mem32[stm.PWR + stm.PWR_CSR] |= 0x200 # BRE backup register enable bit
    def __getitem__(self, idx):
        assert idx >= 0 and idx <= 0x3ff, "Index must be between 0 and 1023"
        return stm.mem32[self.BKPSRAM + idx * 4]
    def __setitem__(self, idx, val):
        assert idx >= 0 and idx <= 0x3ff, "Index must be between 0 and 1023"
        stm.mem32[self.BKPSRAM + idx * 4] = val

b = BkpRAM()
b[0] += 1
Peter Hinch

Post Reply