SD/MMC card chip drivers fail Chan FatFs provided misaligned memory address tests

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Dec 7, 2016 by Dávid Molnár
Content originally posted in LPCWare by briching on Mon Oct 05 17:09:35 MST 2015

I have an embedded artists 4357 OEM board dev kit, and I am trying to get the SD card working with a file system.  The provided example (periph_sdmmc) runs fine on it's own, but when incorporating the example code into our production code, we end up with a corrupted file system.  I believe this is because the example provided with lpcopen is not fully compatible with Chan FatFS.

As an isolation test, I have downloaded the code at http://elm-chan.org/fsw/ff/img/app4.c in the hopes that I would be able to test out the drivers directly to see the solution.  It is not obvious to me what to do, since I will most likely have to modify the sd/mmc code within lpc-open to get it to work.

The periph_sdmmc project passes the first two tests and fails "**** Single sector write test 2 ****\n".  The problem is that the buffer gets populated with data in the wrong place.  Digging down into the chip drivers for the sd/mmc card, I can see that this may be due to DMA being used which expects buffers to be aligned on 32-bit addresses.  You can't just pass a void pointer in there and expect it to work out.  Chan needs to be able to pass in a byte pointer from up above.

If chan's provided checker utility doesn't pass for the example, without even a single line of our production code in it, I suspect the culprit is in lpcopen.  Try it out yourself as a 5 minute experiment.  Make a call to test_diskio from the periph_sdmmc main function, and you probably will see that it doesn't pass.  Dig in and look at the contents of memory and you'll see your data written and read being off by two bytes as shown below:

Here's what was written in the misaligned test (bytes ordered sequentially left to right):

B6DB6E37 188C46A3 52291788 44A2512B 168B46A3
52291708 84C2E173 BADDED75 391F0C86 C3E271BB
DEEF743A 1D8D4521 934A2511 8BC663B2 59AFD4EA
F579BF5C AED768B4 5A2D9549 27904824 12090780
C0E0F078 BC5E2F14 0A058143 A2D1EBF6 FBFE7F3C
1E8FC4E2 F1FBFE7F BCDE6F34 1A8DC5E1 733A9D4D
B85C2E97 48A452A9 57A8D46A 35190F04 8241A352
A9D7E8F4 FA7D3D9D 4DA5512B 160B0683 C2E1F3FA
FDFD7D3D 9D4D2511 8B462392 49279048 249249A7
50A8542A 15098740 2090C8E4 F279BFDC 6EB7D8EC
F6FB7EBF DC6E3718 8C46A3D2 E977381C 8E472090
48A4D269 3798CCE6 733A1D8D C561339A 4DA551AB
562B96CB 66B3DAED 75391F8C C663B2D9 6FB45AAD
D5E97738 1C8EC760 30180C86 C3E2713B 9ECFE472
B95F2C16 8BC6E372 399FCCE6 F37A3D9D CDE5713B
9ECFE4F2 F9FFFCFE 7F3C9E4F A4522917 88C46231
1B8EC760 B0582C16 0B060302 0183C2E1 F3FA7DBD
DD6D3599 CF643219 8F44A2D1 6B369B4E A7502894
CAE5F17B BEDF6C36 9B4E2790 C8E47239 1F0C86C3
DEEFF47A 3D1D0D85 41231289 47A0D068 341A0D05
81432211 8B462392 49A7D0E8 F47A3D9D 4DA5D1EB
76BB5E2F 148AC561 B35AAD55 A9572814 8A45A1D3
EA75391F 8CC663B2 592F148A 452193CA 65311B0E
0780C060 30188C46 23924927

Here's what was read back: 
6FB4B6DB 6E37188C 46A35229 178844A2 512B168B
46A35229 170884C2 E173BADD ED75391F 0C86C3E2
71BBDEEF 743A1D8D 4521934A 25118BC6 63B259AF
D4EAF579 BF5CAED7 68B45A2D 95492790 48241209
0780C0E0 F078BC5E 2F140A05 8143A2D1 EBF6FBFE
7F3C1E8F C4E2F1FB FE7FBCDE 6F341A8D C5E1733A
EE77B85C 2E9748A4 52A957A8 D46A3519 0F048241
A352A9D7 E8F4FA7D 3D9D4DA5 512B160B 0683C2E1
F3FAFDFD 7D3D9D4D 25118B46 23924927 90482492
49A750A8 542A1509 87402090 C8E4F279 BFDC6EB7
D8ECF6FB 7EBFDC6E 37188C46 A3D2E977 381C8E47
209048A4 D2693798 CCE6733A 1D8DC561 339A4DA5
51AB562B 96CB66B3 DAED7539 1F8CC663 B2D96FB4
5AADD5E9 77381C8E C7603018 0C86C3E2 713B9ECF
E472B95F 2C168BC6 E372399F CCE6F37A 3D9DCDE5
713B9ECF E4F2F9FF FCFE7F3C 9E4FA452 291788C4
62311B8E C760B058 2C160B06 03020183 C2E1F3FA
7DBDDD6D 3599CF64 32198F44 A2D16B36 9B4EA750
2894CAE5 F17BBEDF 6C369B4E 2790C8E4 72391F0C
7FBCDEEF F47A3D1D 0D854123 128947A0 D068341A
0D058143 22118B46 239249A7 D0E8F47A 3D9D4DA5
D1EB76BB 5E2F148A C561B35A AD55A957 28148A45
A1D3EA75 391F8CC6 63B2592F 148A4521 93CA6531
1B0E0780 C0603018 8C462300

I don't believe I'm responsible for aligning this memory since the place that has the problem appears to be inside the chip level drivers.  Am I missing something here?