Black Lives Matter - Action and Equality.
0

Slow Writes using NVM on ItsyBitsy M4
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Slow Writes using NVM on ItsyBitsy M4

by Tavda3172 on Wed May 13, 2020 11:39 am

I have been experimenting with using the NVM module to store my program state, and I have found that the write times are typically around 37-40 ms. I am running the program test below on the ItsyBitsy M4. Is this typical performance for this module? Can anyone else confirm these results? Is there way to get better write times? Why is it SO slow!?

Code: Select all | TOGGLE FULL SIZE
import time
import microcontroller as mcu

def savetonvm(n):
    t = time.monotonic()
    mcu.nvm[0] = n
    print(time.monotonic() - t)
   
savetonvm(255)

Tavda3172
 
Posts: 16
Joined: Sat Nov 26, 2016 8:08 pm

Re: Slow Writes using NVM on ItsyBitsy M4

by mikeysklar on Wed May 13, 2020 1:18 pm

Hi Tavada3172,

Thank you for sharing your elegant example for write speed test. I believe you are seeing the correct results @ 37-40ms assuming the print function to console is not slowing down the output.

While the NVM is excellent for permanent storage of flags and variables it does wear out with too many writes so it is intended more for write occasionally read from many type usage.

Your ItsyBitsy M4 has 2MB of flash storage that you can read/write to from CircuitPython. Have you tried using it and comparing the performance?

https://learn.adafruit.com/adafruit-fea ... on-storage

boot.py:
Code: Select all | TOGGLE FULL SIZE
import board
import digitalio
import storage

# For Gemma M0, Trinket M0, Metro M0/M4 Express, ItsyBitsy M0/M4 Express
switch = digitalio.DigitalInOut(board.D2)

# For Feather M0/M4 Express
# switch = digitalio.DigitalInOut(board.D5)

# For Circuit Playground Express, Circuit Playground Bluefruit
# switch = digitalio.DigitalInOut(board.D7)

switch.direction = digitalio.Direction.INPUT
switch.pull = digitalio.Pull.UP

# If the switch pin is connected to ground CircuitPython can write to the drive
storage.remount("/", switch.value)


CircuitPython Logger

Code: Select all | TOGGLE FULL SIZE
import time

import board
import digitalio
import microcontroller

led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()

try:
    with open("/temperature.txt", "a") as fp:
        while True:
            temp = microcontroller.cpu.temperature
            # do the C-to-F conversion here if you would like
            fp.write('{0:f}\n'.format(temp))
            fp.flush()
            led.value = not led.value
            time.sleep(1)
except OSError as e:
    delay = 0.5
    if e.args[0] == 28:
        delay = 0.25
    while True:
        led.value = not led.value
        time.sleep(delay)

mikeysklar
 
Posts: 1068
Joined: Mon Aug 01, 2016 8:10 pm

Re: Slow Writes using NVM on ItsyBitsy M4

by Tavda3172 on Wed May 13, 2020 3:49 pm

Hi mikeysklar,

Thank you for your informative reply! The example code was actually written by my friend and collaborator FutureTechLab, so all credit goes to him:)

That is a bummer that the write time is so long:( The problem we are experiencing is that the write cycle is blocking, so as our loop cycles over just a single byte it is delaying us by ~320ms! I was not aware that the nvm storage had lower write capability than the external flash so that is good to know. Although now I'm remembering that the external flash might be NOR flash so that would make sense.

In regards to the data logger example, we had tried that initially but we will need to write to the storage dynamically while it is hooked up to USB and that did not seem to be possible using that method. Do you know if that is the only method to write to the external flash storage? We only need to write a single byte to memory and were expecting a write time in microseconds so this was all very surprising.

Thank you for your help,
Kevin

Tavda3172
 
Posts: 16
Joined: Sat Nov 26, 2016 8:08 pm

Re: Slow Writes using NVM on ItsyBitsy M4

by mikeysklar on Thu May 14, 2020 1:43 pm

Hi Kevin,

I think you can work around the dynamic storage issues by using a custom boot.py. You can decide which device (your ItsyBitsy M4 or host computer) gets to mount the USB read-only versus read-write. The second link below is the only way interface I currently know for writing to exernal storage with CircuitPython.

https://learn.adafruit.com/welcome-to-c ... itpy-drive
https://learn.adafruit.com/adafruit-fea ... on-storage

Code: Select all | TOGGLE FULL SIZE
 import storage

storage.remount("/", readonly=True)

m = storage.getmount("/")
m.label = "NEW_NAME"

storage.remount("/", readonly=False)

mikeysklar
 
Posts: 1068
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.


cron