SD card after ejecting and re-inserting.

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
sim222
Posts: 20
Joined: Wed Nov 27, 2019 12:04 am

SD card after ejecting and re-inserting.

Post by sim222 » Mon Jan 11, 2021 11:03 am

Hi guys,
Thanks for your valuable posting!!

I have searched the article last a few days however, failed to find any proper answer regarding my problem.
My project is the data logger using pyboard which should receive three sensors and send it's data through serial port and save any data.
So I need to save the data into the sd card.
My simple cord worked well however, after ejecting and re-inserting the card I can't access the card anymore until the power-resetting even keyboard-reset(Ctrl-D) didn't work.

So tested the mount method uos.mount() / uos.umount() / os.mount() / os.umount()
From the test, I have got the conclusion that ejecting the card makes me not to access the card anymore.

1. before ejecting, I can mount and unmount freely and access the card any time I want.

2. After re-inserting, I can't use any method related the access sd card before power-reset.

Got this kind of message to access the card.

Is it impossible to access it without power-resetting?
I think this kind of issue would occur easily in the real world because of the vibration.

How can I deal with this issue regarding sd card?

>>> uos.mount(sd,'/sd')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 19] ENODEV


Here's my cord.

#simple SD recorder

#Logs the data from the accelerometer to a file n the SD card

import pyb,lcd160cr
from BMP280 import BMP280_I2C

lcd = lcd160cr.LCD160CR('X')
lcd.set_orient(lcd160cr.PORTRAIT)
lcd.set_pos(0, 0)
lcd.set_text_color(lcd.rgb(255, 0, 0), lcd.rgb(0, 0, 0))
lcd.set_font(1,0)
lcd.write('Hello MicroPython!\n')
lcd.erase()

accel=pyb.Accel()
blue=pyb.LED(4)
s=BMP280_I2C(2)

f.write('From accelerometer, time,x,y,z,temperature,pressure\n')
f.close()

blue.on()

while True:
blue.on()
lcd.erase()
lcd.set_pos(0,0)

f=open('test.log','a')

for n in range (5):
t=pyb.millis()
rv=s.read()

x,y,z=accel.filtered_xyz()
f.write('{},{},{},{},{},{}\n'.format(t,x,y,z,s.temperature,s.mbar))
print('{},{},{},{},{},{}\n'.format(t,x,y,z,s.temperature,s.mbar))
lcd.set_pos(0,n*25)
lcd.write('{},{},{},{}\n'.format(t,x,y,z))
lcd.set_pos(0,n*25+10)
lcd.write('{0:0.2f},{1:0.2f}\n'.format(s.temperature,s.mbar))
pyb.delay(500) # 500ms


f.close()
blue.off()
pyb.delay(500)

User avatar
water
Posts: 75
Joined: Sun Sep 24, 2017 9:16 am

Re: SD card after ejecting and re-inserting.

Post by water » Thu Jan 28, 2021 2:12 pm

Try sd.deinit() after uos.umount('/sd') or eject SD card, and initialize 'sd' object again after re-insert SD card.

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

Re: SD card after ejecting and re-inserting.

Post by pythoncoder » Fri Jan 29, 2021 12:24 pm

Worth a try, but I don't hold out much hope. I think hot plugging the Pyboard SD card is unsupported.
Peter Hinch
Index to my micropython libraries.

sim222
Posts: 20
Joined: Wed Nov 27, 2019 12:04 am

Re: SD card after ejecting and re-inserting.

Post by sim222 » Fri Feb 05, 2021 6:27 am

water wrote:
Thu Jan 28, 2021 2:12 pm
Try sd.deinit() after uos.umount('/sd') or eject SD card, and initialize 'sd' object again after re-insert SD card.
Thanks for your suggestion.
However, I can't find deinit() in my pyb1.1 (fw 1.4)

User avatar
water
Posts: 75
Joined: Sun Sep 24, 2017 9:16 am

Re: SD card after ejecting and re-inserting.

Post by water » Fri Feb 12, 2021 7:00 pm

.deinit() is affect to ESP32 port, I confuse them, on PyBoard, use sd.power() instead,
before eject SD card,

Code: Select all

uos.umount('/sd')
sd.power(0)
True
then re-insert SD card,

Code: Select all

sd.power(1)
True

Code: Select all

uos.mount(sd, '/sd')
If none SD card inserted, execute sd.power(0) or sd.power(1) will return False.

sim222
Posts: 20
Joined: Wed Nov 27, 2019 12:04 am

Re: SD card after ejecting and re-inserting.

Post by sim222 » Fri Feb 26, 2021 12:25 am

water wrote:
Fri Feb 12, 2021 7:00 pm
.deinit() is affect to ESP32 port, I confuse them, on PyBoard, use sd.power() instead,
before eject SD card,

Code: Select all

uos.umount('/sd')
sd.power(0)
True
then re-insert SD card,

Code: Select all

sd.power(1)
True

Code: Select all

uos.mount(sd, '/sd')
If none SD card inserted, execute sd.power(0) or sd.power(1) will return False.
It looks work!! Thanks a lot.
I will test it again!!

Post Reply