why using the same approach in this case it works ?
Code: Select all
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
def get_bytearray(self):
return uctypes.bytearray_at(self.BKPSRAM, 4096)
bram = BkpRAM()
ba = bram.get_bytearray()
a={'header':'$$$','enable_a':'N','hrs_a':0,'mins_a':0,'hre_a':0,'mine_a':0,'lu_a':'N','ma_a':'N','me_a':'N','gi_a':'N','ve_a':'N','sa_a':'N','do_a':'N','vsonda_a':0,'enable_b':'N','hrs_b':0,'mins_b':0,'hre_b':0,'mine_b':0,'lu_b':'N','ma_b':'N','me_b':'N','gi_b':'N','ve_b':'N','sa_b':'N','do_b':'N','vsonda_b':0,'set_a':2000,'set_b':2000,'yyyy_h':2015,'mm_h':12,'gg_h':12,'wd_h':0,'hr_h':22,'min_h':16,'flg_a':'N','flg_b':'N','footer':'###','x':0}
z = json.dumps(a).encode('utf8')
print(z)
bram[0] = len(z)
ba[4: 4+len(z)] = z # Copy into backup RAM and go into standby
bram = BkpRAM()
ba = bram.get_bytearray()
b = json.loads(bytes(ba[4:4+bram[0]]).decode("utf-8")) # retrieve the object
k=b['header']
print(k)