Author Topic: SD card: Boot sector not readable  (Read 21119 times)

Offline martinh

  • Newbie
  • *
  • Posts: 25
    • View Profile
SD card: Boot sector not readable
« on: March 20, 2013, 02:24:03 PM »
Hello,

I am just trying to adapt parts of mass_storage.c from version 8 to my project (derived from version 4, LPC2468).
The SD card I am using is a Kingston SDHC 4GB class 4 card.
MCIClk has been raised to 18MHz. The card shall run in 4-bit-mode, no DMA so far.

The problem is that the bootsector cannot be read.
In _fnGetSector() the condition
    (MCIStatus & RxDataAvailbl)
never is met, which results in an infinite loop.

The SDHC card has been formatted (FAT32) by WINDOWS, hence it has no password and is not locked I should think.

The initialisation phase is passed without obvious problems.

Coming to Cmd16, I found that the command array is
static const unsigned char ucSET_BLOCK_LENGTH_CMD16[5]  = {SET_BLOCKLEN_CMD16, 0x00, 0x02, 0x00, 0x02};
It seems that the block size here is >512 bytes which results in setting the BLOCK_LEN_ERROR bit.
But as SDHC cards always use a block size of 512 bytes this may not be important.

READ_SINGLE_BLOCK_CMD17 is passed with ucresult==0.
But with the following function
fnGetSector(ptr_utDisk->ptrSectorData)
the infinite loop in _fnGetSector() is reached.

Can anybody thing of a reason why the boot sector cannot be read?

Regards,
Martin

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: SD card: Boot sector not readable
« Reply #1 on: March 24, 2013, 11:34:06 PM »
HI Martin

Have you tried with slower speed to see whether the problem is the same?

In my code I found

while ((MCIStatus & (DataEnd | RxDataAvailbl)) != DataEnd) {        // while data is still being received

which looks to be the loop that you are referring to. Is your loop different?

Regards

Mark


Offline martinh

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: SD card: Boot sector not readable
« Reply #2 on: April 02, 2013, 02:40:47 PM »
Hi Mark,

yes, that is the loop.
Independently of whether SD_CONTROLLER_DMA is defined or not it comes to an infinite loop in _fnGetSector().
Either at
Code: [Select]
    while (!(MCIStatus & DataEnd)) {    // wait until the DMA has completed the transfer to the destination buffer
    }
or at
Code: [Select]
    while ((MCIStatus & (DataEnd | RxDataAvailbl)) != DataEnd) {         // while data is still being received
The reason for that is that I had changed initialization in SET_SPI_SD_INTERFACE_FULL_SPEED().
I had NOT set the MCI_CLK_PwrSave bit for register MCIClock and this caused the malfunction.
That is a little bit annoying because MCICLK (18MHz) is needed as a masterclock in the project and setting the bit means that clock output is only enabled when the bus is active.
Is there a chance to have the clock output always enabled and nevertheless access the SD card?

Regards,

Martin

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: SD card: Boot sector not readable
« Reply #3 on: April 02, 2013, 09:05:15 PM »
Hi Martin

I don't see any restrictions as to allowing the MCICLK to be output all of the time (not setting the power save bit) - also I found no errata concerning this signal.

It should be optional whether the clock is output all the time or is only output when the bus is in use and I would expect that it should be able to operate like this.

Furthermore I understand that the MCIStatus would operate irrespective of whether the clock is being output (or connected) since the output clock is used by the external card and not by the internal controller.

Are you sure that there can't be another explanation for this?

Regards

Mark

Offline martinh

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: SD card: Boot sector not readable
« Reply #4 on: April 05, 2013, 01:09:07 PM »
Hi Mark

no, I am not sure, but I did not find another explanation so far.
Until I have found one, I am afraid I will have to toggle the bit.

Regards,
Martin

Offline martinh

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: SD card: Boot sector not readable
« Reply #5 on: September 22, 2013, 12:43:12 PM »
Hi Mark,

I did some further investigation on that issue and it seems - as you said - that the MCI_CLK_PwrSave is not responsible for the
trouble.
I have made some progress and meanwhile can write files to the SD card.
The tests I made so far were limited to small files, so no further errors arised.

Now, to store nearly 32MB of data to a SD card, I am running a loop that puts 512 bytes per loop cycle into the file:
Code: [Select]
   while(j) {      // j here is 62765
      Sd.Anz = 512;
      result = utWriteFile(&Sd.utFile, Sd.ptSrc, Sd.Anz);
      if(result != UTFAT_SUCCESS) {   // Fehler
        ErrorPrint("File write error: ", -1); printf("%x %s", result,Sd.ptWrFileName);
      }
      SendMsgU0(" %x", k-j);  // to monitor how many loops are done finally
      // some results: 0x5fE, 0x60fE,0x269E, 0xE8DE

      j -= 1;
      Sd.ptSrc += Sd.Anz;  
    }
The programme still hangs in function _fnGetSector() at
Code: [Select]
  while (!(MCIStatus & DataEnd)) { }   // wait until the DMA has completed the transfer to the destination bufferbut this time lots of write operations were succesful.
(The contents of location 0xe008c034 (MCIStatus) remains at 0x200,
i.e. StartBitErr - Start bit not detected on all data signals in wide bus mode.)

The clock already has been reduced from 18 to 9MHz as I remembered your comment regarding 18MHz.
Also the 2 delay times in function _fnGetSector() have been increased temporarily.
The test was done in debug mode and as a release.
I might try reduce the clock to a value beneath 9MHz but that would slow down the whole process and I can't imagine that this would solve the problem.

Perhaps it would be best to have a time-out loop and try to rewrite the last memory block.
But when I do that, re-initialize the SD card and try to open the last file, utOpenFile() returns a UTFAT_DISK_READ_ERROR.
Does anybody have a better suggestion? One that eliminates the reason for that fault?

Thanks!
Martin
« Last Edit: September 24, 2013, 08:22:20 AM by martinh »

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: SD card: Boot sector not readable
« Reply #6 on: September 26, 2013, 01:00:27 AM »
Hi Martin

It may be interesting to run in SPI mode rather than SDHC mode (if the SPI pins match on your HW - often the MUX pins allow this) to see whether the problems then stop (although SPI mode will of course be a little slower).

Regards

Mark



Offline martinh

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: SD card: Boot sector not readable
« Reply #7 on: October 08, 2013, 02:31:55 PM »
Hi Mark,

do you mean I should undefine SD_CONTROLLER_DMA and SD_CONTROLLER_AVAILABLE?
I am afraid I can't do that. The board is a LPC2468-16 OEM board from EA. They use ports P1.2, P1.6, P1.3. For none of these pins a MISO, MOSI or SCLK function can be selected.

I finally disabled all interrupts and repeated tests with different MCIClocks (18MHz... 2.25MHz).
The Kingston Micro SDHC (SDC 10/32) did not store the whole amount of 31282kB reliably neither with SD_CONTROLLER_DMA defined nor
without. That means in most cases it hang before the whole file was written. Though it has to be mentioned that the amount of stored 512-byte-blocks sometimes was very high before the programm hang in _fnGetSector().
(In the rare cases when it passed the storage test, it took less then 3 minutes (MCIClock=18MHz, DMA enabled) to store the data.)

At that infinite loop MCIStatus was either
  0x20 - RxOverrun Receive FIFO overrun error.
  or
  0x200- StartBitErr - Start bit not detected on all data signals in wide bus mode.

The Kingston SDC 4/4GB 18 did a better job.
But in DMA mode with MCIClock=18MHz it took nearly 20 minutes (!!!) to store 31282kB.
Without DMA mode MCIClock had to be reduced to 4,5MHz. The time required for the same amount of data was about the same.
It still has to be checked whether or not data is stored reliably, i.e. each time the whole amount of data. But that is too slow anyway.

Perhaps a class 10 SD card of another manufacturer might do a better job?
What SD card did you use for testing?
Or can you recommend a special type / manufacturer?
Did you do the tests with a large data block or just a small one?

Regards,
Martin

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: SD card: Boot sector not readable
« Reply #8 on: October 09, 2013, 04:41:30 PM »
Hi Martin

The write speed is limited by the SD card type so high speed cards will of course be quicker.
However I don't remember at the moment the typical write times that I expect (but I I think it will be between about 200k and 500k Bytes/second) ut I will do some tests with the LPC2468 and Kinetis (both in SDHC mode) later on and use FTP to transfer files of 32MBytes (your reference).

Comparing two processor types will allow any (unexpected) speed differences to be identified as well as any unreliability when writes are made (assuming related to one processor type as well).

Once I have results using my hardware and the SD cards that I have I will report back. I wouldn't expect that SD card manufacturers are critical apart for some expected speed advantages the higher speed (and more expensive) ones should offer.

Regards

Mark

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: SD card: Boot sector not readable
« Reply #9 on: October 09, 2013, 10:30:21 PM »
Martin

I didn't do the test just yet but found the following results from tests with a 17MByte file via FTP (Total Commander) to and from a Kinetis K60F120 (120MHz) with SDHC:

-   Copying a file from the PC to the SD card via FTP gave a speed of 95kBytes/s (SD card write speed limits this)
-   Copying a file from the SD card to the PC via FTP gave a speed of 550kB/s (I tested a 17MByte file)

I didn't have any problems with reliability when doing that test (performed in July 2013). A 32MByte file would take over 5 minutes to write and about 1 minute to read (including FTP transfer) but the SD card write time seems to be the main factor (considering the difference). If you were getting 3 minutes with the LPC2468 it seems essentially reasonable (in comparison).

If there are LPC2468 reliability issues I think that there should be a way for the driver to abort and retry rather than hang as yours looks to be doing. Don't know yet whether this is reproducable with my board or not. Hopefully will find out shortly.

Regards

Mark



Offline martinh

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: SD card: Boot sector not readable
« Reply #10 on: October 10, 2013, 08:22:00 AM »
Hi Mark,

just a remark on aborting:
I set a counter to exit that infinite loop after a certain time, deleted MCIStatus bits 10...0 and repeated the function call of _fnGetSector().
That did not lead to a solution - the loop remained infinite.

Regards,
Martin

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: SD card: Boot sector not readable
« Reply #11 on: October 10, 2013, 03:15:18 PM »
Martin

I have just done a series of tests with my latest project on an LPC2478 board, LPC2378 and Kinetis K60F120 and K60N512 boards (SanDisk 8G D card but others similar).

There are problems with the LPC operation but the Kinetis operation is reliable.

First some speed notes from the Kinetis boards:
- 32 Meg FTP write speed 389s (89kB/s)
- 32 Meg UDB MSD write speed 180s (170kB/s)

- 32 Meg FTP read speed 102s (339kB/s)
- 32 Meg USB read 72s (440kB/s)

In each case I did "diffs" on the copied files and the content was always OK. I had no erros in the operation at any point.

When I read and write from/to the same SD card inserted in the SD card drive of my PC I find that the PC can write the same data to it in about 9s and read it back in about 7s. This seems to be using high speed USB as communication channel to the SD card reader whereas the USB-MSD test above uses full-speed USB.

Full speed USB limits the transfer rate to about 1.2 MB/s so if there were no SD card read/write delays the 32MB read/write would be about 30s.
The USB-MSD FAT operation is controlled by the PC and so bypasses utFAT (accessing directly the sector read/write routines of utFAT) and the USB-MSD speeds were better, although the 440kB/s maximum rate is presumably limited by the Kinetis (reaction time causing the USB bandwidth not be be fully utilised).
Practically the read limit of about 440kB/s may be realistic over USB-MSD protocol, whereby the PC interface will have a certain advantage which allows it to achieve 4MB/s read speed (therefore plausible).

What concerns me however is that the write speed is less than half the speed on the Kinetis whereby the PC doesn't get throttled as much. Watching the USB-MSD operation with a USB analyser I see that each time a sector is committed it takes about 1ms to write it to the card, which would mean that it will require over 65s to actually write the sectors (plus other overhead involved with cluster management writes). The write times are restricted by the SD card clock and the SD card signalling that it is no longer busy so I am wondering how the PC can obviously write the sectors much faster to the SD card (by the same reconning it must be achieving sector write times of the order of 100us).

My conclusion is that the PC must either be working with much faster serial data rates to the SD card (although I don't expect a factor of more than x3 compared to the Kinetis SDHC clock and wouldn't expain the x10 factor) or it must be doing some other tricks to achieve this write speed. Some additional investigation looks worthwhile.


Whereas the tests were reliable with the Kinetis they were not good with the LPCs. Without DMA I was getting almost immediate data aborts (exceptions) when formatting the SD card or when writing data to it [Note that I never managed to format a card - even 1GByte ones - it would not give an error but never completed; just looked to stop at some random point Update - this was due to polled card removal detection interrupting the formatting, which has been corrected in the meantime]. With DMA it was not much better since it was also doing much the same even if it may have looked to work for a short time. In one case I managed a 32M FTP write in about 290s (120kB/s) but when I wanted to read the data back the SD card was blank (?) [USB-MSD tests also had write problems]
The results on the LPC2478 and LPC2378 were similar although in a couple of cases it got stuck in a software loop waiting for !(MCIStatus & DataEnd) and I could see that the status register had the DataTimeOut flag set.

Reading was better but far from perfect. I could transfer a 32MB file from the SD card in 122s (284kB/s) but when I did a "diff" there were (sometimes?) errors in the data content.

Therefore I can confirm that the LPC operation is not reliable:
- data content errors after read
- data aborts (exceptions) can occur when writing
- software loops can hang due to the status register signalling errors (variour error flags have been seen/reported)

Since the Kinetis devices are working reliably (although maybe not as fast as hoped) with large data read/writes (via FTP and USB-MSD) I don't expect that the utFAT or USB-MSD parts have problems (much of utFAT is bypassed by USB-MSD operation but FTP uses it fully and didn't show error issues). This would mean that the issues are more likely in the LPC SDHC driver or some LPC configuration.


The next step I did was to check the behaviour on a simpler LPC device connected via SPI. On the Olimex LPC-P2148 I checked the write/read via USB-MSD:
- 32MB write 320s (100kBytes/s)
- 32MB read 190s (168kBytes/s)
A "diff" showed that the content was exact - although slower (60MHz processor via SPI) the operation was reliable, again pointing the source of the issue to be in the SDHC driver.

Note that GCC was used for compiling in all cases.


Since I don't remember such issues when originally working with the driver I am wondering whether they developed after the original version(s). Comparisons with older releases would be interesting to see whether these were good/better but I didn't try this yet.


Unfortunately however this excercise has proven that there are difficulties localised to the use of the LPC SDHC in the present driver version. I will keep working on this to try to identify whether such difficulties were introduced only in more recent versions and what needs to be done to improve the situation. It is however not the simplest work and so may take some time before a solution can be found - due to various other tasks in progress it is difficult to say when the excercise can be completed by - in the meantime it may be worth checking with a previous release to see whether the operation is not being affected by other changes to the project as it evolved.

Regards

Mark



One last observation that I just made:
I formatted a 1G SD card on the Kinetis - the PC could read it well, but the LPC2478 said it was not formatted. When I looked at a sector ("sect f8d") where I knew that the volume label is stored I could see the string but it was shifted 5 long words to the right, meaning that presumably the LPC was reading data from the sector/card shifted by this amount and so not recogising the card as being formatted.
The LPC2378 board however recognised it normally...curiouser and curiouser..


« Last Edit: October 11, 2013, 10:41:38 PM by mark »

Offline martinh

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: SD card: Boot sector not readable
« Reply #12 on: October 15, 2013, 02:56:40 PM »
Hi Mark,

thanks for your trouble and your very comprehensive investigations!

Yesterday the 32GB card from SanDisk arrived.
Right from the beginning I could store files of 32032036 bytes in size within 2'50'' to 2'58' (about 174kB/s). I had it running over night and after 197 files had been written  it stopped because an external event was missing. But as soon as that event was reestablished storing files was continued.

I will have it running again tonight and do some file comparison tomorrow.

Regards,
Martin

Offline martinh

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: SD card: Boot sector not readable
« Reply #13 on: October 16, 2013, 08:38:45 AM »
Hi Mark,

data storage was still in progress this morning. 964 files had been stored meanmwhile.
Comparison of 10 files proofed that the files were identical.
Hopefully I will get the same results with more cards of the same type from SanDisk.
It still has to be checked however if enabled interrupts have any influence on the result. So far all of them had been disabled.

So if you modify the driver I would be delighted to hear about the results. Especially that code that led to an infinite loop still seems to be a weak point.

But for the moment I am happy with the results.
Thank you for all.

Regards,
Martin