0

Writing/Reading Feather M0 Flash Memory
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Writing/Reading Feather M0 Flash Memory

by wkolari on Thu Jan 18, 2018 3:38 pm

I got my Feather M0 and have been trying to work with the microcontroller.nvm command to write/read data for a project. I understand I can write an integer to this byte array by using something like microcontroller.nvm[0] = 22. However, I can't figure out how to work with values that aren't round integers (EX: 8.19378 which is a float, and (1, 2, 3) which is a tuple). Is there a special method to store values other than round integers in the microcontroller.nvm byte array? I'm trying to store a 3-tuple accelerometer value. Any help is much appreciated!

For my Trinket M0 project, once I got the boot.py code set up with storage.remount("/", False) to boot into read/write mode, I was easily able to write strings to my data file and read them back as a tuple value using the below code. I wanted to share that code below for anyone that needs it in the future.

Code: Select all | TOGGLE FULL SIZE
#sample tuple values that might come out of the LIS3DH accelerometer
values = (8.2939, 2.2938, 1.29392)

#save values to a file named data.txt on the filesystem
with open("/data.txt", "w") as f:
    f.write(Str(values))
    f.flush()

#read values back for data.txt
with open("/data.txt") as f:
    values=eval(f.read())

wkolari
 
Posts: 18
Joined: Mon Dec 04, 2017 2:28 pm

Re: Writing/Reading Feather M0 Flash Memory

by danhalbert on Thu Jan 18, 2018 4:44 pm

You can use the `ustruct` module, which is built-in, to convert various data types to/from sequences of bytes. See http://circuitpython.readthedocs.io/en/ ... truct.html. Also see https://docs.python.org/3/library/struct.html, which is a superset of ustruct (ustruct implements its essential parts). There are examples toward the end of that.

danhalbert
 
Posts: 514
Joined: Tue Aug 08, 2017 12:37 pm

Re: Writing/Reading Feather M0 Flash Memory

by wkolari on Sat Jan 20, 2018 12:37 am

Hey Dan, I worked through packing and unpacking tuples with the ustruct module. However, this hasn't helped me with writing that byte data to the flash memory through microcontroller.nvm. I must be passing the packed ustruct object to microcontroller.nvm incorrectly. Once packed to bytes, I assumed the packed ustruct value would be used with microcontroller.nvm (my packed value(s) are defined as structdata below).

Here's an example of what I'm talking about:

Code: Select all | TOGGLE FULL SIZE
#read accelerometer value and separate X,Y,Z values from tuple to float (so I can pack them using ustruct with floats)
>>> import ustruct
>>> import microcontroller
>>> acceldata = (3.333, 4.444, 5.555)
>>> acceldataX = float(acceldata[0])
>>> acceldataY = float(acceldata[1])
>>> acceldataZ = float(acceldata[2])

#pack the 3 float values using ustruct, which returns bytes
#**I assumed the packed structdata value below would be used with microcontroller.nvm to store to Feather M0 flash memory
>>> structdata = ustruct.pack("fff", acceldataX, acceldataY, acceldataZ)
>>> structdata
b'\xdcOU@@5\x8e@\x90\xc2\xb1@'

#trying to figure out how to use my packed byte data with microcontroller.nvm
>>> help(microcontroller.nvm)
object <ByteArray> is of type ByteArray

#attempt #1 with error
>>> microcontroller.nvm(structdata)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'ByteArray' object is not callable

#attempt #2 with error
>>> microcontroller.nvm[0] = structdata
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert bytes to int

wkolari
 
Posts: 18
Joined: Mon Dec 04, 2017 2:28 pm

Re: Writing/Reading Feather M0 Flash Memory

by danhalbert on Sat Jan 20, 2018 12:45 am

try
Code: Select all | TOGGLE FULL SIZE
microcontroller.nvm[0:len(structdata)] = structdata


Then check by looking at `microcontroller.nvm[0:len(structdata)]`

danhalbert
 
Posts: 514
Joined: Tue Aug 08, 2017 12:37 pm

Re: Writing/Reading Feather M0 Flash Memory

by wkolari on Sat Jan 20, 2018 12:46 am

I'm going to work through the struct.pack_into and struct.unpack_from functions to see if that will allow me to accomplish writing the packed data to the Feather M0 flash memory.

From the article you posted above about the struct.pack_into function:
struct.pack_into(fmt, buffer, offset, v1, v2, ...)
Pack the values v1, v2, … according to the format string fmt and write the packed bytes into the writable buffer starting at position offset. Note that offset is a required argument.

Something like this is what I'm thinking, but will need to test:
Code: Select all | TOGGLE FULL SIZE
structdata = struct.pack_into("fff", microcontroller.nvm, 0, acceldataX, acceldataY, acceldataZ)

wkolari
 
Posts: 18
Joined: Mon Dec 04, 2017 2:28 pm

Re: Writing/Reading Feather M0 Flash Memory

by wkolari on Sat Jan 20, 2018 12:47 am

Okay, let me try your suggestion. Thank you.

wkolari
 
Posts: 18
Joined: Mon Dec 04, 2017 2:28 pm

Re: Writing/Reading Feather M0 Flash Memory

by danhalbert on Sat Jan 20, 2018 12:49 am

yeah, pack_into() should work too; that's probably clearer

danhalbert
 
Posts: 514
Joined: Tue Aug 08, 2017 12:37 pm

Re: Writing/Reading Feather M0 Flash Memory

by wkolari on Sat Jan 20, 2018 1:06 am

Thank you SO MUCH again, Dan! I was able to get the packed ustruct into memory with the code you posted. I may mess a bit more with pack_into(), but at least I know I can make things work with this!

Code: Select all | TOGGLE FULL SIZE
microcontroller.nvm[0:len(structdata)] = structdata

wkolari
 
Posts: 18
Joined: Mon Dec 04, 2017 2:28 pm

Please be positive and constructive with your questions and comments.