Mount SD card later in my program using os.mount

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
techtoys
Posts: 2
Joined: Tue Oct 17, 2017 7:46 am

Mount SD card later in my program using os.mount

Post by techtoys » Tue Oct 17, 2017 8:24 am

I'm trying to build a datalogger with PYBv1.1 and MicroPython V1.9.1 and I want to mount the SD card from within my main.py program. I don't want to be told to play with various USB modes during boot. I just want to do exactly what the documentation says is possible.

"If needed, you can prevent the use of the SD card by creating an empty file called /flash/SKIPSD. If this file exists when the pyboard boots up then the SD card will be skipped and the pyboard will always boot from the internal filesystem (in this case the SD card won’t be mounted but you can still mount and use it later in your program using os.mount)."

I put the SKIPSD empty file on /flash and it works as described, which is excellent. So, what I now need is the correct syntax for mounting the SD card using os.mount.

I've spent hours searching for an example that works, or attempting to work it out via trial and error. But every attempt caused error messages about missing modules, bad syntax, or objects not being callable. I'm feeling pretty stupid and frustrated.

I think I need to do 3 things:
1. Import some module(s) such as os, pyb, or SD depending on which example I've looked at
2. Create an sd object including a definition of the correct pins connected to the pyboard's micro-sd connector
3. Do the actual "mount" command

Is anybody able to help by providing a working example of approximately 3 or 4 lines of code?

Thanks.

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

Re: Mount SD card later in my program using os.mount

Post by pythoncoder » Wed Oct 18, 2017 12:56 pm

I take it you want to be able to change SD cards at runtime, with the Pyboard powered up. As I understand it this is unsupported. See https://github.com/micropython/micropython/issues/1915.
Peter Hinch
Index to my micropython libraries.

techtoys
Posts: 2
Joined: Tue Oct 17, 2017 7:46 am

Re: Mount SD card later in my program using os.mount

Post by techtoys » Wed Oct 18, 2017 7:58 pm

Hi Peter

No, I just want /FLASH to be mounted at boot and be the USB drive viewable on my Windows computer, even if the SD card is still inserted into its socket. This allows me to easily edit main.py and any other files I use to store configuration data.

Once main.py is running, it will mount the SD card and can then store sensor data within a CSV file on the card. The SD card never needs to be viewable as a USB drive, but it needs to be available for storing sensor data.

To retrieve the CSV file containing sensor data, I could possibly remove the SD card and take it away to another computer with an SD cardreader. This this the backup plan in case of a technical problem. Or my main.py program might optionally have a function to copy the CSV file back to the /Flash drive.

One thing my datalogger MUST have is a function to transmit the contents of the CSV file out from a uart line, where some extra hardware converts it to an infrared stream of characters through a transparent window in the datalogger housing. This allows the data to be retrieved without opening the housing. The datalogger is intended to operate at 2000 meters below the ocean surface, and un-neccessarily opening/closing the pressure housing risks damaging the o-ring seals.

Anyway, thanks for your reply. I think it helped me find the correct syntax for the solution by following one of the links in the page you referred me to. I still need to do more thorough testing, but it initially seems to work.

# Mount the SD card
import os
sd = pyb.SDCard()
os.mount(sd, '/sd')

Niallp
Posts: 26
Joined: Sun Nov 29, 2015 10:25 pm
Location: Pender Island, BC

Re: Mount SD card later in my program using os.mount

Post by Niallp » Mon Nov 19, 2018 8:21 pm

I have a similar application, want to use SD for datalog only, not code. When I try this with the latest though (1.9.4) I get

>>> sd = pyb.SDCard()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SDCard'

so clearly not defined ... is the SDCard() object only built conditionally ?

EDIT: yes ! it is only built if mpconfigboard.h has #define MICROPY_HW_HAS_SDCARD (1)

Post Reply