Sd card partition alligment while partitioning

Hello,

i am trying to use sd as fast as it possible.

There is point i dont understand :

in this line ,source code of FreeRtos, hidden sector set relative to sector count.

i was not able to find information about hidden sector nor alignment calculations.

switch (xPartition.ulSectorCount) {
        // For alignment:
        // See https://www.partitionwizard.com/help/align-partition.html
        case 15519744: // 8GB (7.4 GiB) SD card
            xPartition.ulHiddenSectors = 2048; 
            break;
        case 31205376: // 16GB (14.9 GiB) SD card
            xPartition.ulHiddenSectors = 67584; 
            break;
        case 62325760: // 32GB (29.72 GiB) SD card
            xPartition.ulHiddenSectors = 8192; 
            break;
        case 124702720: // 64GB (59.46 GiB) SD card
            xPartition.ulHiddenSectors = 32768; 
            break;
        case 249733120: // 128GB (119.08 GiB) SD card
            xPartition.ulHiddenSectors = 2048; 
            break;               
    }

i coulnt not understand why hidden sector is 2048 when sectorcount is 15519744 or 67584 when sectorcount is 31205376.

Hello @mustafaflz, welcome to the FreeRTOS forum!

I do not know the module that you are referring to, but I do know the person who wrote it. It think that @carlk3 will respond soon to your question.

Thanks,

Hi Mustafa,

I haven’t updated that library in years, but I have recently updated FreeRTOS-FAT-CLI-for-RPi-Pico. I rewrote that function. The idea is to align the volume to an SD card “allocation unit” or “segment”:

AU (Allocation Unit): is a physical boundary of the card and consists of one or more blocks and its size depends on each card. The maximum AU size is defined for memory capacity. Furthermore AU is the minimal unit in which the card guarantees its performance for devices which complies with Speed Class Specification. The information about the size and the Speed Class are stored in the SD Status.

– SD Card Association; Physical Layer Specification Version 3.01

My strategy has been to attempt to emulate the SD Memory Card Formatter because I figure the SD Association ought to know how to properly format an SD card. For a typical 16 GB card with a 4 MiB AU (“segment”) size, it produces a layout like:

Volume base sector: 8192
FAT base sector: 8790
Root directory base sector (FAT12/16) or cluster (FAT32/exFAT): 2
Data base sector: 16384
FAT Cluster size ("allocation unit"): 64 sectors (32768 bytes)

prvPartitionAndFormatDisk produces:

Volume base sector: 8192
FAT base sector: 8224
Root directory base sector (FAT12/16) or cluster (FAT32/exFAT): 2
Data base sector: 15772
FAT Cluster size ("allocation unit"): 64 sectors (32768 bytes)

but that is as close as I have been able to get. (I like that the SD Memory Card Formatter starts the data area on a new AU (“segment”), separate from the FAT, but FF_Format doesn’t give me that option.)

The big difference between FreeRTOS-FAT-CLI-for-PSoC-63 and FreeRTOS-FAT-CLI-for-RPi-Pico would be in the low levels of the SPI driver (e.g., spi.c). I would use something like WinMerge or a similar differencing and merging tool to combine them into an updated package. [If you do something like that, you are welcome to submit a Pull Request.]

If you want to go fast, if at all possible you want to ditch SPI and use SDIO.

I have written up my (limited) understanding of SD card performance tuning: Performance Tuning Tips.

1 Like

That is clear explanation. Thank you very much for your comment.