Skip to content

sdcard: wrong capacity determined when using sdxc cards #1053

@benwynn

Description

@benwynn

Description of issue:

When attempting to read a sdcard of type 'sdxc', those cards having a capacity between 32G - 2T, a wrong capacity is produced.
I have determined this is because the most-significant-bits of the 22-bit C_SIZE are not being read.

Environment:

  • MicroPython Version: v1.24.x, v1.26.1
  • board/device: Raspberry Pi Pico 2 W
  • package: sdcard, installed version 0.1.0, latest stable verison 0.1.0
  • installation method: thonny, tools -> manage packages

Demonstration code:

from machine import Pin, SPI
import os, vfs
import sdcard

SPI_BAUD = 25000000 # 25Mhz
# these just happen to be the pins i have wired up
spi = SPI(1, baudrate=SPI_BAUD, sck = Pin(10), mosi=Pin(11, Pin.OUT, value=1), miso=Pin(8))
cs = Pin(9, mode=Pin.OUT, value=1)
sd = sdcard.SDCard(spi, cs, baudrate = SPI_BAUD)

sectors = sd.ioctl(4, None)
blocksize = sd.ioctl(5, None)
size = sectors * blocksize

print(f"SD: {sd}, Sectors: {sectors}, Blocksize: {blocksize}")
print(f"Card Size: {size}, or {size // 1024}Kb, or {size // (1024*1024)}Mb or {size // (1024*1024*1024)}Gb") 

Actual Results:

Card labeled as 64G recognized as:

SD: <SDCard object at 20015940>, Sectors: 55083008, Blocksize: 512
Card Size: 28202500096, or 27541504Kb, or 26896Mb or 26Gb

Card labeled as 128G recognized as:

SD: <SDCard object at 20015940>, Sectors: 42950656, Blocksize: 512
Card Size: 21990735872, or 21475328Kb, or 20972Mb or 20Gb

Expected Results:

Card labeled as 64G recognized as:

SD: <SDCard object at 20015a90>, Sectors: 122191872, Blocksize: 512
Card Size: 62562238464, or 61095936Kb, or 59664Mb or 58Gb

confirmed capacity in windows: "59664 MB"

Card labeled as 128G recognized as:

SD: <SDCard object at 20015a90>, Sectors: 244277248, Blocksize: 512
Card Size: 125069950976, or 122138624Kb, or 119276Mb or 116Gb

confirmed capacity in windows: "119276 MB"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions