I can't access microSD cards with Pyboard 1.1

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.
ThomasChr
Posts: 121
Joined: Sat Nov 25, 2017 7:50 am

Re: I can't access microSD cards with Pyboard 1.1

Post by ThomasChr » Tue Apr 16, 2019 5:43 pm

The problem is that the chinese people "forgot" to connect the Pin A8 which is the SD Detection Pin.
I assume they opted for a cheaper microsd connector which simply does not have such a switch (0.00 Cent saved!).
You can use your SD-Card like so:

Code: Select all

mport os
import pyb
import machine

# Forces the pin low, which tricks the board into thinking that an SD Card is connected
# Normaly there is a hardware switch for that, but on the crow board it seems to be left out
# If ommited os.mount() won't mount the sd card (even if present) with OSError: 16 (EBUSY)
machine.Pin('A8', mode=machine.Pin.OUT, pull=None)

# Now mount the sd card
sd = pyb.SDCard()
os.mount(sd, '/sd')

# Now you can work with the SDCard as usual...
os.listdir('/sd')
It seems that the mounting stopped when sd.present() is False.
Unfortunately I've not found the piece of code where that happens to see if there is some kind of force-Option to ignore if the sd card is resent and try anyway!

As an Alternative you can access the SD Card by hand with an SDCard Module... there are some out there.
You can read where the SDCard is connected in the Pyboard Shematics.

User avatar
oserror
Posts: 43
Joined: Tue Feb 12, 2019 12:26 am

Re: I can't access microSD cards with Pyboard 1.1

Post by oserror » Tue Apr 16, 2019 6:21 pm

@ThomasChr:
Great detective work! I just successfully mounted a microSD card in the REPL.

Did you figure that out by looking at the schematic? I looked at some of their documents but I didn't learn anything.

I can see how I could just add this code to the /flash/boot.py to perform this operation, and that will certainly work. I was able to load a program on the sd card with just an import statement, so the path is all set to go.

For now, that workaround will do the job nicely. I was getting a little worried that I'd have to locate a brand-name Pyboard 1.1 -- they seem to be very scarce and the Amazon sellers are selling them for a costly sum.

Thanks a lot for digging into the problem!

ThomasChr
Posts: 121
Joined: Sat Nov 25, 2017 7:50 am

Re: I can't access microSD cards with Pyboard 1.1

Post by ThomasChr » Tue Apr 16, 2019 6:27 pm

I though that maybe they forgot one pin on the hardware side, and after looking at the schematic I remembered that there are two kinds of microsd connectors, with and without detection switch.
So I tried to forcefully change the detection pin and it worked :-)

The next step would have been to use a sd card library where you can init with the Pins from the schematic and so don‘t need os.mount()

Keep me posted if you can figure something out in the C-Code. The automount is in main.c and uses something like is_sdcard_present() to check if automount should occur.
But where exactly os.mount() happens in the C-Code and where the hell it checks if the SDCard object it‘s getting is present() - I have no clue...

Thomas

User avatar
oserror
Posts: 43
Joined: Tue Feb 12, 2019 12:26 am

Re: I can't access microSD cards with Pyboard 1.1

Post by oserror » Tue Apr 16, 2019 6:47 pm

I merely put these lines into boot.py:

Code: Select all

# omitting your comment for brevity
machine.Pin('A8', mode=machine.Pin.OUT, pull=None)
# execute main.py on sd card
pyb.main('/sd/main.py')
and the async led flasher ran, with the uasyncio library on /sd/lib/uasyncio.

This is pretty darn close to what I want anyway with my regular code. It would be great to be able to see the sd card mounted in Linux, but it's not a deal breaker.

I'll look at the C-code but I'm not sure what I'll accomplish. If I swing the C-equivalent of a monkey wrench, I may fix something by percussive maintenance. :P Anything's possible! I have a feeling that a finer tool set may be required.

User avatar
oserror
Posts: 43
Joined: Tue Feb 12, 2019 12:26 am

Re: I can't access microSD cards with Pyboard 1.1

Post by oserror » Tue Apr 16, 2019 7:23 pm

You know, I'm looking at this code in main.c: (line 646)

Code: Select all

    bool mounted_sdcard = false;
    #if MICROPY_HW_SDCARD_MOUNT_AT_BOOT
    // if an SD card is present then mount it on /sd/
    if (sdcard_is_present()) {
        // if there is a file in the flash called "SKIPSD", then we don't mount the SD card
        if (!mounted_flash || f_stat(&fs_user_mount_flash.fatfs, "/SKIPSD", NULL) != FR_OK) {
            mounted_sdcard = init_sdcard_fs();
        }
    }
    #endif
It looks like you'd just have to eliminate that 'if' statement:

Code: Select all

    bool mounted_sdcard = false;
    #if MICROPY_HW_SDCARD_MOUNT_AT_BOOT
    // The Crow Pyboard can't detect an SD card so let's forge ahead
    // if there is a file in the flash called "SKIPSD", then we don't mount the SD card
    if (!mounted_flash || f_stat(&fs_user_mount_flash.fatfs, "/SKIPSD", NULL) != FR_OK) {
        mounted_sdcard = init_sdcard_fs();
    }
    #endif
init_sdcard_fs() returns a boolean. It's already checking to see if the fs is viable. The question is, is it robust enough to handle when there is no card present at all? For questions like these, I have to use the monkey wrench (compile the firmware and test). I am not sure if it will crash or not. If init_sdcard_fs() can't handle it, then the C equivalent of 'try/except' would have to be added, which would be trivial for an ace, not so trivial for me but I'd try it.

ThomasChr
Posts: 121
Joined: Sat Nov 25, 2017 7:50 am

Re: I can't access microSD cards with Pyboard 1.1

Post by ThomasChr » Tue Apr 16, 2019 7:29 pm

I would have done the exact same think. After a little bit of testing you surely will know if it‘s stable enough.

User avatar
oserror
Posts: 43
Joined: Tue Feb 12, 2019 12:26 am

Re: I can't access microSD cards with Pyboard 1.1

Post by oserror » Tue Apr 16, 2019 9:04 pm

I tried it. I have to leave the house in a few minutes, so I'll have to put this off for a while, but my initial test was to power up the Crow Pyboard with the altered firmware and no SD card.

I'll give it another shot later.

That is, it didn't boot.

User avatar
oserror
Posts: 43
Joined: Tue Feb 12, 2019 12:26 am

Re: I can't access microSD cards with Pyboard 1.1

Post by oserror » Tue Apr 16, 2019 11:17 pm

Okay, the issue was that I was compiling for the defaulted Pyboard 1.0. I changed that in the very top of the Makefile.

Using @ThomasChr's hack, I was able to patch the code in one place in the sdcard.c file, line 174:

Code: Select all

    mp_hal_pin_config(MICROPY_HW_SDCARD_DETECT_PIN, MP_HAL_PIN_MODE_INPUT, MICROPY_HW_SDCARD_DETECT_PULL, 0);
}
Change that to:

Code: Select all

    mp_hal_pin_config(MICROPY_HW_SDCARD_DETECT_PIN, MP_HAL_PIN_MODE_OUTPUT, MICROPY_HAL_PIN_PULL_NONE, 0);
}
and compile. I just made an Arch Linux VM as the dependencies were listed for that distribution, but it certainly would have built in some other flavor. I didn't even see a warning and it took about 2 minutes.

Instructions are at the Github page near the bottom. I had to also do a small substitution (as found when searching for the name resolution error on the git issues pages) to avoid a problem with the first step. This must be done right after the git clone.

Code: Select all

sed -i -e 's|gnu\.org/r|nongnu.org/git|' .gitmodules
Your mileage may vary; I could not resolve that particular internet address.

I hope this patch helps someone else. I do enjoy it when something works!

jamonda
Posts: 36
Joined: Thu May 21, 2020 3:48 pm

Re: I can't access microSD cards with Pyboard 1.1

Post by jamonda » Thu May 21, 2020 3:53 pm

Hi, friends.
I bought this clone:

https://br.banggood.com/Pyboard-MicroPy ... rehouse=CN

I can't get the micro sd card to work. The workaround described here does not work.
Can anybody help me?

adumbname
Posts: 2
Joined: Sat Aug 14, 2021 1:19 am

Re: I can't access microSD cards with Pyboard 1.1

Post by adumbname » Tue Aug 17, 2021 10:49 pm

Hello friends!

I'm pretty good at Python on a laptop, but a newbie using microPython on a PyBoard, which I need for a portable environmental sensor / data-logging application. I have everything working except that I can't access the SD Card.

This Pyboard was bought on Amazon ( https://www.amazon.com/Homyl-Piece-Micr ... 975&sr=8-1 ).

The SD card is a 4 GB Kingston micro SD HC and has been formatted to "loop" and FAT32.

Communication to the PyBoard is via Linux/Mint/Cinnamon: having better luck using screen /dev/ttyACM0 than rshell --port /dev/ttyACM0.

I have seen the OS error 16 (once), but can't get this suggestion to work, regardless of which drive it's on, with or without SKIPSD on the SD card:

machine.Pin('A8', mode=machine.Pin.OUT, pull=None)
sd = pyb.SDCard()
os.mount(sd, '/sd')

I have not tried modifying any c code: has anyone gotten that to work?

I, too, have tried "everything on the site" and nothing has worked. (Though it'd be great if all of the relevant suggestion, such as which SD Card to buy, how to format it, listings of boot & main on both /flash and /sd were all in one place ;) )

Or... failing a solution for this board, does anyone know of a vendor that sells a proven good board with an SD card inserted and ready to go - true "plug and play"!?

Any help would be very much appreciated... including: "it's a bad board: return it!"

Post Reply