howto mount sd card in script

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
icenov
Posts: 9
Joined: Sun Mar 12, 2017 1:27 am

howto mount sd card in script

Post by icenov » Wed Apr 12, 2017 6:43 am

I'm running a script on a pyboard (V1.1) that logs solar data to an SD card. I've decided to (foolishly!) include a check to see if the card is present, and if not then stop the script and print a warning. It works to a point - the switch press works, but I can't find a way to mount the card in software and continue the script. If the card is present on boot, then everything works.

Code: Select all

try:
    os.chdir('/sd')
except OSError:
    print('No SD card detected! - insert card and press usr switch')
    while switchpress == 0:
        print('waiting for switchpress...')
        utime.sleep(1)
    else:
        print('switchpress registered')
        #  need to mount SD card!
        # switchpress=0
        # 
I've tried to use pyb.mount(sd,/sd) and some of the other obvious(?) commands with no luck. I'm trying to avoid running a reboot after inserting the card. I recall reading a way to do this in the forum, but can't seem to find it again.
Any suggestions appreciated.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: howto mount sd card in script

Post by dhylands » Wed Apr 12, 2017 6:57 am

After waiting for the user switch, you could do a hard-reset by using pyb.hard_reset() or machine.reset().

icenov
Posts: 9
Joined: Sun Mar 12, 2017 1:27 am

Re: howto mount sd card in script

Post by icenov » Wed Apr 12, 2017 7:06 am

I did initially use that, but then instantly lost my terminal connection (using rshell) so thought there might be a more subtle way!

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: howto mount sd card in script

Post by dhylands » Wed Apr 12, 2017 7:14 am

rshell should reconnect (at least under linux). Press RETURN to see the prompt again.

icenov
Posts: 9
Joined: Sun Mar 12, 2017 1:27 am

Re: howto mount sd card in script

Post by icenov » Wed Apr 12, 2017 7:31 am

Not for my setup unfortunately (Ubuntu 16.10) . It drops back to rshell, but REPL won't connect and I have to ctrl-C out of rshell, restart rshell and then I can REPL and ctrl-D soft reboot. It's not a problem - and it might be related to auto-mounting the SD card, which I haven't disabled yet.

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

Re: howto mount sd card in script

Post by pythoncoder » Thu Apr 13, 2017 7:00 pm

I can confirm this, Dave, under Mint 17.1 (rebecca) on a Pyboard 1.1 with an SD card fitted and MSC mode disabled.

Code: Select all

[adminpete@axolotl]: ~
$ rshell
Connecting to /dev/pyboard ...
Welcome to rshell. Use Control-D to exit.
/home/adminpete> repl
Entering REPL. Use Control-X to exit.
>
MicroPython v1.8.7-589-gd7310fa on 2017-04-08; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> 
>>> pyb.hard_reset()autoconnect: /dev/ttyACM0 action: remove

                                                            USB Serial device '/dev/ttyACM0' disconnected
                                                                                                         autoconnect: /dev/ttyACM0 action: add
                        Connecting to /dev/ttyACM0 ...

serial port /dev/pyboard closed
/home/adminpete> repl
Entering REPL. Use Control-X to exit.


^X^C
[adminpete@axolotl]: ~
At the REPL Enter just gave blank lines. <ctrl>D had no effect. <ctrl>X just printed ^X. The only way out was <ctrl>C.
boot.py:

Code: Select all

import pyb
pyb.usb_mode('CDC') # act as a serial (CDC) and a storage device (MSC)
Peter Hinch
Index to my micropython libraries.

Post Reply