7 years, 9 months ago.

Implementing a FATFileSystem for SDMMC

I am trying to read a FATFS formatted SD card using the embedded SD interface (MMCIO). The SDFileSystem library uses ChaNs FATFS library enabling communication with an SD card using the SPI interface. During the process of creating a public library (since I can image other people would want to use it) I encountered some problems.

One of the FATFS interface functions I have to implement is:

function prototype

int SDMMC::disk_read(uint8_t *buffer, uint32_t sector, uint32_t count)

As you can see, the caller provides 1) a buffer at which 8-bit unsigned ints are stored, 2) the LBA adres of the sector you want to start reading bytes from, and lastly 3) the amount of sectors you want to read.

STM provided the following function to read raw data from the SD card:

function prototype

uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumOfBlocks)

I currently have the following implementation:

implementation of fatfs interface function disk_read

int SDMMC::disk_read(uint8_t *buffer, uint32_t sectorNr, uint32_t count)
{
    uint32_t largerBuffer[count];
    uint64_t logicBlockAddress = (uint64_t)sector;
    int nLocation = 0;

    int res = BSP_SD_ReadBlocks(largerBuffer, logicBlockAddress, 512, count);
    
    if (res == MSD_OK) {
        do {
            for (int x = 0; x < 512; x++) {
                uint32_t addressValue = largerBuffer[nLocation];
                if (addressValue > 255)
                {
                    return -1; //The caller monitors this return status
                }
                *buffer = (uint8_t)addressValue;
                buffer++;
                nLocation++;
                printf("%X", addressValue);
            }
        } while (--count);
        printf("\r\n");
        return RES_OK;
    } else {
        return RES_ERROR;
    }
}

The problem is that "addressValue" is larger than uint8_t (-795951053) So somewhere something is going wrong.

Does anybody have a clue?

I am not familiar with those functions. However are you sure the BSP_SD_ReadBlocks function returns it as 8-bit data inside 32-bit integers? I can't easily find any proper documentation on the function, but it seems incredibly wasteful to use 32-bit integers to store 8-bit data.

posted by Erik - 30 Jan 2017

Thanks Erik for your response! The BSP_SD_ReadBlocks equation uses the HAL_SD_ReadBlocks(...) equation as defined in:

https://developer.mbed.org/teams/Senior-Design-Sound-Monitor/code/STM32L4xx_HAL_Driver/docs/tip/group__SD__Exported__Functions__Group2.html#gad0d930e6695258d4227f3cc85690b35e

posted by Alex van Rijs 30 Jan 2017

How do you interpret the HAL_SD_ReadBlocks function?

posted by Alex van Rijs 30 Jan 2017

That function I indeed also managed to find. But I would guess it fills the 32-bit values completely. So per 512 bytes you got 512/4 = 128 32-bit words coming out.

posted by Erik - 30 Jan 2017
Be the first to answer this question.