Page 1 of 4

Can be stored data inside non volative memory ?

Posted: Sun Sep 27, 2015 8:03 pm
by BOB63
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.

Re: Can be stored data inside non volative memory ?

Posted: Mon Sep 28, 2015 1:26 am
by dbc
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

Re: Can be stored data inside non volative memory ?

Posted: Mon Sep 28, 2015 5:26 am
by dhylands
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.

Re: Can be stored data inside non volative memory ?

Posted: Mon Sep 28, 2015 6:25 pm
by dbc
How do you get to that RAM? Is it part of the file system and not given over to the memory allocator?

Re: Can be stored data inside non volative memory ?

Posted: Tue Sep 29, 2015 3:36 am
by dhylands
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.

Re: Can be stored data inside non volative memory ?

Posted: Tue Sep 29, 2015 7:02 pm
by BOB63
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.

Re: Can be stored data inside non volative memory ?

Posted: Wed Sep 30, 2015 2:49 am
by dbc
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.

Re: Can be stored data inside non volative memory ?

Posted: Wed Sep 30, 2015 12:55 pm
by pythoncoder
@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

Re: Can be stored data inside non volative memory ?

Posted: Wed Sep 30, 2015 3:18 pm
by blmorris
@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

Re: Can be stored data inside non volative memory ?

Posted: Thu Oct 01, 2015 6:34 am
by pythoncoder
@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