cancel
Showing results for 
Search instead for 
Did you mean: 

SD_ReadBlock weird behaviour

dmg0048
Associate II
Posted on December 30, 2013 at 14:46

Hello Community,

I am trying to implement FatFS via SDIO interface on a STM32F2xx uC, I've checked a few examples online that use DMA and interrupts, however for the purpose of self learning and simplicity I have decided to implement my own using SDIO polling mode, I will later move on implementing the rest. I am using a lot of functions from stm32f2xx_eval_sdio.c of the Standard Peripheral Library. The initialization part works ok, I can read the CID and CSD registers and print out the capacity of the SD used, the only part that seems to fail is enabling four bit mode, as if I enable it, a transmission causes an Start Bit error on the interface, indicating that one of the D0-D3 lines didn't transmit an start bit. To avoid this, I commented the function that enables the bus wide mode and I am working on 1 bit mode. The main problem I have is the SD_ReadBlock function. I use a Disk Editor (HxD) tool in order to check that the boot sector on the uSD card is ok, when I try to read the sector on my card by using this code:

SD_Card Status = SD_OK;
uint8_t Buffer[512];
memset(Buffer, 0xD0, sizeof(Buffer)); // 0xD0 so I can notice those bytes changes.
Status = SD_ReadBlock(Buffer, 0, 512);

Buffer contains 447 zeros, some more data that is not the same I see on the Disk Editor, another bunch of zeros and finally at offset 510d 0x55AA. FatFs succeeds to verify the bytes at offset 510, 0x55AA, but it fails to verify the Fat File System string as the buffer always contains zeroes there, therefore FatFs doesn't detect a valid FAT file system on the SD card. The uC is working at 60MHz and the SDIO clock is enabled, per the Configuration Tool 1.2.0 xls file. #fatfs-sdio-sd-usd-readblock
2 REPLIES 2
Posted on December 30, 2013 at 15:02

The data you're describing sounds like a MBR (Master Boot Record - Partition Table) which is what I'd expect to find at block zero. It in turn (record at 0x1BE) should point off to the BPB for the FAT file system. Make sure you have the right defines so FatFs chases through the partition table.

http://en.wikipedia.org/wiki/Master_boot_record

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
dmg0048
Associate II
Posted on December 30, 2013 at 16:07

Thanks Clive, you cleared things out for me a lot. The problem was about physical and logical sectors.

I downloaded a more professional disk analyzer, WinHex, which allows me to open the uSD via physical and logical means. Using this program I could verify that what FatFs reads is the MBR, located at PHYSICAL sector 0, here it collects the location to the physical sector location of the LOGICAL sector 0, which was what I initially expected the SD_ReadBlock function to read.