0

Adafruit I2C Non-Volatile FRAM Breakout
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit I2C Non-Volatile FRAM Breakout

by albyva on Thu Feb 18, 2021 7:28 pm

So I got my NV-FRAM connected to a Pi Zero and had no issues with getting it wired and working.
My question is, does anybody have a bit of sample Python code for something more practical?
Like how can I store a bunch of data and retrieve it? Like maybe some json data?

Example: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]


NOTE: I earlier set fram[0] = 2


Code: Select all | TOGGLE FULL SIZE
pi@raspberrypi:~ $ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import board
>>> import busio
>>> import digitalio
>>> import adafruit_fram
>>> i2c = busio.I2C(board.SCL, board.SDA)
>>> wp = digitalio.DigitalInOut(board.D21)
>>> fram = adafruit_fram.FRAM_I2C(i2c, wp_pin=wp)
>>> print(fram[0])
bytearray(b'\x02')
>>> fram[0] = 0
>>> print(fram[0])
bytearray(b'\x00')
]>>> exit()
pi@raspberrypi:~ $ exit




Thanks,

albyva
 
Posts: 10
Joined: Tue Jan 26, 2021 11:10 pm

Re: Adafruit I2C Non-Volatile FRAM Breakout

by adafruit_support_carter on Fri Feb 19, 2021 2:03 pm

In general, you need to do:

thing -> bytes -> write bytes to fram
read bytes from fram -> bytes -> thing

for the thing->bytes / bytes-> thing part, checkout encode()/decode() as well as struct.pack()/unpack().

adafruit_support_carter
 
Posts: 18893
Joined: Tue Nov 29, 2016 2:45 pm

Re: Adafruit I2C Non-Volatile FRAM Breakout

by albyva on Fri Feb 19, 2021 3:36 pm

I think I get the encode/decode to/from bytes.

Code: Select all | TOGGLE FULL SIZE
>>> message = "Python is fun"
>>> bytes_encoded = message.encode()
>>> bytes_decoded = bytes_encoded.decode()
>>> print(bytes_encoded)
b'Python is fun'
>>> print(bytes_decoded)
Python is fun
>>>



Now how do you write the bytes_encoded to Fram? Clearly based on the example of Fram[0] = 1 won't cut it for trying to swap in the bytes_encoded variable.
Sorry, I'm not a master python expert.

Code: Select all | TOGGLE FULL SIZE
>>> import board
>>> import busio
>>> import digitalio
>>> import adafruit_fram
>>> i2c = busio.I2C(board.SCL, board.SDA)
>>> wp = digitalio.DigitalInOut(board.D21)
>>> fram = adafruit_fram.FRAM_I2C(i2c, wp_pin=wp)
>>> message = "Python is fun"
>>> bytes_encoded = message.encode()
>>> fram[0] = bytes_encoded
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/dist-packages/adafruit_fram.py", line 174, in __setitem__
    "Data must be a single integer, or a bytearray," " list, or tuple."
ValueError: Data must be a single integer, or a bytearray, list, or tuple.
>>>


Now I did read up on using bytearray and that seems to be heading in that direction, but for only 1 character of my message vs. the whole thing.

Code: Select all | TOGGLE FULL SIZE
>>> array = bytearray(bytes_encoded)
>>> print(array)
bytearray(b'Python is fun')
>>> fram[0] = array
>>> print(fram[0])
bytearray(b'P')
>>>



Do you happen to have anything that's been used with this FRAM to write and read data?
My end goal is to take sensor data from a BMP390 and write it to the FRAM. Right now I've
got code which taking (pressure = bmp.pressure) and sending it via Mosquito (client.publish('Pressure', pressure)).
But I'd also like to write a hard copy of that same data to the FRAM chip.

Suggestions?

albyva
 
Posts: 10
Joined: Tue Jan 26, 2021 11:10 pm

Re: Adafruit I2C Non-Volatile FRAM Breakout

by adafruit_support_carter on Fri Feb 19, 2021 3:52 pm

If it's just sensor data you want to store, I'd look at struct.pack()/unpack(). It has a bit of an initial learning curve, but is really useful for storing values into raw bytes. You can turn things into strings once you've read it back out from the FRAM.

You're usage looks OK. To write multiple values, you just give the initial index, see second example here:
https://circuitpython.readthedocs.io/pr ... _setitem__

Not sure why bytes aren't just supported. I've opened an issue for that here:
https://github.com/adafruit/Adafruit_Ci ... /issues/24

adafruit_support_carter
 
Posts: 18893
Joined: Tue Nov 29, 2016 2:45 pm

Re: Adafruit I2C Non-Volatile FRAM Breakout

by albyva on Sat Feb 20, 2021 10:05 am

adafruit_support_carter wrote:If it's just sensor data you want to store, I'd look at struct.pack()/unpack(). It has a bit of an initial learning curve, but is really useful for storing values into raw bytes. You can turn things into strings once you've read it back out from the FRAM.

You're usage looks OK. To write multiple values, you just give the initial index, see second example here:
https://circuitpython.readthedocs.io/pr ... _setitem__

Not sure why bytes aren't just supported. I've opened an issue for that here:
https://github.com/adafruit/Adafruit_Ci ... /issues/24




Thanks for the tips. I'll work on playing with struck.pack/unpack this weekend and see how far it takes me.
I'll report back my success or lack thereof. lol

albyva
 
Posts: 10
Joined: Tue Jan 26, 2021 11:10 pm

Please be positive and constructive with your questions and comments.