How to know when it's safe to restart after a write to flash

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
Polo
Posts: 10
Joined: Tue Oct 02, 2018 1:07 pm

How to know when it's safe to restart after a write to flash

Post by Polo » Fri Jul 03, 2020 5:41 pm

Hi everyone,

A lot of times resetting too early corrupts the filesystem.

I know the red LED indicates when the flash is being written to, but is there a way to check for this in software?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to know when it's safe to restart after a write to flash

Post by pythoncoder » Sat Jul 04, 2020 8:45 am

Please see this post.

tl;dr File corruption will occur unless you take specific precautions.
Peter Hinch
Index to my micropython libraries.

Polo
Posts: 10
Joined: Tue Oct 02, 2018 1:07 pm

Re: How to know when it's safe to restart after a write to flash

Post by Polo » Sun Jul 05, 2020 5:08 pm

Thanks for the response.
Could you elaborate what these precautions are?

Using rshell is not an option for me.
I'm sending filecontents from one pyboard to another.
Then the receiving pyboard writes the filecontents to flash.
At this point I'd like to know when the flash write was completed and the receiving pyboard is safe to reset.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to know when it's safe to restart after a write to flash

Post by pythoncoder » Sun Jul 05, 2020 5:20 pm

The precaution is to disable MSC mode. Failing that, avoid mounting the filesystem on a PC. Sharing the filesystem between a PC and code which updates the contents will lead to corruption.
Peter Hinch
Index to my micropython libraries.

Polo
Posts: 10
Joined: Tue Oct 02, 2018 1:07 pm

Re: How to know when it's safe to restart after a write to flash

Post by Polo » Sun Jul 05, 2020 5:38 pm

The receiving pyboard is not connected to a PC.
The filesystem is only updated from code.

Maybe this could help to clarify.

Code: Select all

import machine

with open('main.py', 'w') as f:
    f.write('print("hello world")')

while flash.active():
    pass
machine.reset()
I'd like to know if there's a function or method like flash.active()

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to know when it's safe to restart after a write to flash

Post by pythoncoder » Mon Jul 06, 2020 5:58 am

Unlike a PC a Pyboard is a bare-metal system. There is no underlying OS, no background tasks and no concurrency. So the file closure implied by completion of your context manager should mean that it is safe to reboot.
Peter Hinch
Index to my micropython libraries.

Post Reply