EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Micro SD card / MSP430 interface: write problem

Started by Christophe Braillon November 7, 2009
Hi everyone,

I have interfaced a Micro SD card to a MSP430 microcontroller using the 
SPI mode. Everything worked perfectly fine but I recently had problems 
when writing too often into my files.

Here is my setup. As I said the card (SDHC 4Go) is directly connected to 
the microcontroller via its USCI (SPI) peripheral. I have written the 
basic SD access (register read / write, basically to find if a card is a 
SDSC or SDHC, page read / write). I have followed the reduced SD 
sp�cification to do so (CRC activated, waiting after any operation for 
the card to leave the 'busy' state). On top of that I have implemented a 
FAT32 filesystem driver that is able to create / delete, read / write 
any file on the root directory (not directory handling, only short 
filenames).

Here is my problem: sometimes (I'm not able to give specific detail on 
'when' because it varies a lot in time and frequency), complete sectors 
of my create files are filled with zeroes...

I have investigated on my filesystem implementation because it seemed 
obvious that the problem what there. But it turned out that I have 
managed to reproduce that bug only writing pages using the low level SD 
access. For exemple if i write 4000 pages of 512 bytes filled with 0xAA 
bytes, I get 2 to 10 pages filled with 0x00.

Does anyone encountered this kind problem? I would be so grateful if 
someone could give me at least a hint on how to solve that bug because 
I'm wasting my last week investigating on that.

Regards,

Christophe
In article <4af5a261$0$29577$426a74cc@news.free.fr>,=20
christophe.braillon@free.fr says...
> Hi everyone, >=20 > I have interfaced a Micro SD card to a MSP430 microcontroller using the=
=20
> SPI mode. Everything worked perfectly fine but I recently had problems=20 > when writing too often into my files. >=20 > Here is my setup. As I said the card (SDHC 4Go) is directly connected to=
=20
> the microcontroller via its USCI (SPI) peripheral. I have written the=20 > basic SD access (register read / write, basically to find if a card is a=
=20
> SDSC or SDHC, page read / write). I have followed the reduced SD=20 > sp=E9cification to do so (CRC activated, waiting after any operation for=
=20
> the card to leave the 'busy' state). On top of that I have implemented a=
=20
> FAT32 filesystem driver that is able to create / delete, read / write=20 > any file on the root directory (not directory handling, only short=20 > filenames). >=20 > Here is my problem: sometimes (I'm not able to give specific detail on=20 > 'when' because it varies a lot in time and frequency), complete sectors=
=20
> of my create files are filled with zeroes... >=20 > I have investigated on my filesystem implementation because it seemed=20 > obvious that the problem what there. But it turned out that I have=20 > managed to reproduce that bug only writing pages using the low level SD=
=20
> access. For exemple if i write 4000 pages of 512 bytes filled with 0xAA=
=20
> bytes, I get 2 to 10 pages filled with 0x00. >=20 > Does anyone encountered this kind problem? I would be so grateful if=20 > someone could give me at least a hint on how to solve that bug because=20 > I'm wasting my last week investigating on that. >=20
My experience with SD cards and the MSP430 has led me to believe that almost all errors are related to the timing of operations. In=20 particular, the time for the SD card to indicate that it is ready for a new write increases drastically when the card's internal processor decides it is time to erase a new block of the flash so that new data can be written. On one set of cards, this happened at 32KB intervals. At that point, the time to write a block=20 (after waiting for the card to indicate it is not busy) would bump up from a few milliseconds to over 50 mSec. The delay probably depends on the speed rating of the card. My technique for debugging these issues was to toggle an I/O bit at the start and end of the write function and observe the=20 timing. Of course, the other possibility is that your write routine isn't sending what you think it is. Perhaps you could repeat your test, but insert code in the routine that writes the buffer to check to make sure it isn't writing a buffer full of zeros. A wayward buffer pointer might cause something like this. Mark Borgerson
Mark Borgerson a &#4294967295;crit :
> In article <4af5a261$0$29577$426a74cc@news.free.fr>, > christophe.braillon@free.fr says... >> Hi everyone, >> >> I have interfaced a Micro SD card to a MSP430 microcontroller using the >> SPI mode. Everything worked perfectly fine but I recently had problems >> when writing too often into my files. >> >> Here is my setup. As I said the card (SDHC 4Go) is directly connected to >> the microcontroller via its USCI (SPI) peripheral. I have written the >> basic SD access (register read / write, basically to find if a card is a >> SDSC or SDHC, page read / write). I have followed the reduced SD >> sp&#4294967295;cification to do so (CRC activated, waiting after any operation for >> the card to leave the 'busy' state). On top of that I have implemented a >> FAT32 filesystem driver that is able to create / delete, read / write >> any file on the root directory (not directory handling, only short >> filenames). >> >> Here is my problem: sometimes (I'm not able to give specific detail on >> 'when' because it varies a lot in time and frequency), complete sectors >> of my create files are filled with zeroes... >> >> I have investigated on my filesystem implementation because it seemed >> obvious that the problem what there. But it turned out that I have >> managed to reproduce that bug only writing pages using the low level SD >> access. For exemple if i write 4000 pages of 512 bytes filled with 0xAA >> bytes, I get 2 to 10 pages filled with 0x00. >> >> Does anyone encountered this kind problem? I would be so grateful if >> someone could give me at least a hint on how to solve that bug because >> I'm wasting my last week investigating on that. >> > > My experience with SD cards and the MSP430 has led me to believe that > almost all errors are related to the timing of operations. In > particular, the time for the SD card to indicate that it is ready > for a new write increases drastically when the card's internal > processor decides it is time to erase a new block of the flash so > that new data can be written. On one set of cards, this happened > at 32KB intervals. At that point, the time to write a block > (after waiting for the card to indicate it is not busy) would bump > up from a few milliseconds to over 50 mSec. The delay probably > depends on the speed rating of the card. > > My technique for debugging these issues was to toggle an I/O bit > at the start and end of the write function and observe the > timing. > > Of course, the other possibility is that your write routine isn't > sending what you think it is. Perhaps you could repeat your > test, but insert code in the routine that writes the buffer to > check to make sure it isn't writing a buffer full of zeros. > A wayward buffer pointer might cause something like this. > > Mark Borgerson >
Thanks Mark for your answer, I did print my buffer before sending it, it is correctly filled, but when I do outputs on the serial interface, write errors do not occur. Generally speaking, they do no occur if I add delays (by adding printf, or anything that is a bit time consuming). It seems to corroborate what you say. If I understand well, you say that even if the card is telling me it is not busy it might actually be? Is there any mean to know that? (other than waiting for XXX ms) I've seen that it is possible to pre-erase block, would it help? For the record, I do write operation about 1 to 5 times per second (512 bytes blocks). Christophe
Mark Borgerson a &#4294967295;crit :
> In article <4af5a261$0$29577$426a74cc@news.free.fr>, > christophe.braillon@free.fr says... >> Hi everyone, >> >> I have interfaced a Micro SD card to a MSP430 microcontroller using the >> SPI mode. Everything worked perfectly fine but I recently had problems >> when writing too often into my files. >> >> Here is my setup. As I said the card (SDHC 4Go) is directly connected to >> the microcontroller via its USCI (SPI) peripheral. I have written the >> basic SD access (register read / write, basically to find if a card is a >> SDSC or SDHC, page read / write). I have followed the reduced SD >> sp&#4294967295;cification to do so (CRC activated, waiting after any operation for >> the card to leave the 'busy' state). On top of that I have implemented a >> FAT32 filesystem driver that is able to create / delete, read / write >> any file on the root directory (not directory handling, only short >> filenames). >> >> Here is my problem: sometimes (I'm not able to give specific detail on >> 'when' because it varies a lot in time and frequency), complete sectors >> of my create files are filled with zeroes... >> >> I have investigated on my filesystem implementation because it seemed >> obvious that the problem what there. But it turned out that I have >> managed to reproduce that bug only writing pages using the low level SD >> access. For exemple if i write 4000 pages of 512 bytes filled with 0xAA >> bytes, I get 2 to 10 pages filled with 0x00. >> >> Does anyone encountered this kind problem? I would be so grateful if >> someone could give me at least a hint on how to solve that bug because >> I'm wasting my last week investigating on that. >> > > My experience with SD cards and the MSP430 has led me to believe that > almost all errors are related to the timing of operations. In > particular, the time for the SD card to indicate that it is ready > for a new write increases drastically when the card's internal > processor decides it is time to erase a new block of the flash so > that new data can be written. On one set of cards, this happened > at 32KB intervals. At that point, the time to write a block > (after waiting for the card to indicate it is not busy) would bump > up from a few milliseconds to over 50 mSec. The delay probably > depends on the speed rating of the card. > > My technique for debugging these issues was to toggle an I/O bit > at the start and end of the write function and observe the > timing. > > Of course, the other possibility is that your write routine isn't > sending what you think it is. Perhaps you could repeat your > test, but insert code in the routine that writes the buffer to > check to make sure it isn't writing a buffer full of zeros. > A wayward buffer pointer might cause something like this. > > Mark Borgerson >
Hi, I have read once again the SD simplified specification and Samsung MicroSD Card datasheet, it seems that there is a flag called READY_FOR_DATA in the card status (not in SD Status register) that indicates the card accepts write operations. It seems to be accessible in the R1 response but only in SD Mode, I have not seen any mean to access this bit in SPI mode, does anyone know how to have this information? Christophe
In article <4af7e1dd$0$11637$426a74cc@news.free.fr>,=20
christophe.braillon@free.fr says...
> Mark Borgerson a =E9crit : > > In article <4af5a261$0$29577$426a74cc@news.free.fr>,=20 > > christophe.braillon@free.fr says... > >> Hi everyone, > >> > >> I have interfaced a Micro SD card to a MSP430 microcontroller using th=
e=20
> >> SPI mode. Everything worked perfectly fine but I recently had problems=
=20
> >> when writing too often into my files. > >> > >> Here is my setup. As I said the card (SDHC 4Go) is directly connected =
to=20
> >> the microcontroller via its USCI (SPI) peripheral. I have written the=
=20
> >> basic SD access (register read / write, basically to find if a card is=
a=20
> >> SDSC or SDHC, page read / write). I have followed the reduced SD=20 > >> sp=E9cification to do so (CRC activated, waiting after any operation f=
or=20
> >> the card to leave the 'busy' state). On top of that I have implemented=
a=20
> >> FAT32 filesystem driver that is able to create / delete, read / write=
=20
> >> any file on the root directory (not directory handling, only short=20 > >> filenames). > >> > >> Here is my problem: sometimes (I'm not able to give specific detail on=
=20
> >> 'when' because it varies a lot in time and frequency), complete sector=
s=20
> >> of my create files are filled with zeroes... > >> > >> I have investigated on my filesystem implementation because it seemed=
=20
> >> obvious that the problem what there. But it turned out that I have=20 > >> managed to reproduce that bug only writing pages using the low level S=
D=20
> >> access. For exemple if i write 4000 pages of 512 bytes filled with 0xA=
A=20
> >> bytes, I get 2 to 10 pages filled with 0x00. > >> > >> Does anyone encountered this kind problem? I would be so grateful if=
=20
> >> someone could give me at least a hint on how to solve that bug because=
=20
> >> I'm wasting my last week investigating on that. > >> > >=20 > > My experience with SD cards and the MSP430 has led me to believe that > > almost all errors are related to the timing of operations. In=20 > > particular, the time for the SD card to indicate that it is ready > > for a new write increases drastically when the card's internal > > processor decides it is time to erase a new block of the flash so > > that new data can be written. On one set of cards, this happened > > at 32KB intervals. At that point, the time to write a block=20 > > (after waiting for the card to indicate it is not busy) would bump > > up from a few milliseconds to over 50 mSec. The delay probably > > depends on the speed rating of the card. > >=20 > > My technique for debugging these issues was to toggle an I/O bit > > at the start and end of the write function and observe the=20 > > timing. > >=20 > > Of course, the other possibility is that your write routine isn't > > sending what you think it is. Perhaps you could repeat your > > test, but insert code in the routine that writes the buffer to > > check to make sure it isn't writing a buffer full of zeros. > > A wayward buffer pointer might cause something like this. > >=20 > > Mark Borgerson > >=20 >=20 > Hi, >=20 > I have read once again the SD simplified specification and Samsung=20 > MicroSD Card datasheet, it seems that there is a flag called=20 > READY_FOR_DATA in the card status (not in SD Status register) that=20 > indicates the card accepts write operations. It seems to be accessible=20 > in the R1 response but only in SD Mode, I have not seen any mean to=20 > access this bit in SPI mode, does anyone know how to have this informatio=
n?
>=20
Have you tried your code on a SanDisk micro SD card? I have found that different manufacturers have very different delays before the card is=20 ready. In my SD_Send_Command code, I execute the following check whenever the=20 LAST operation was a write i =3D 0; SD_CS_ON; // macro to set CS bit low do { // Check for card busy =09if(i++ > 30000) break; uc =3D Get_SD_SPI(); // get a byte from the SPI } while(uc =3D=3D 0); // uc stays zero while card busy ..... // more code to actually send the command At the processor and SPI clock speed I am using, 30,000 times through this loop is about 100 milliseconds. At one time, I added code to keep track of the loop count in=20 this routine. IIRC, it was normally under 20, but would bump up to the 30,000 limit at times. I also have code that checks for valid responses to commands. If I don't get a valid response, I increment a soft error count and try the command again---including the wait for the card to be not busy. I generally see two or three of these soft errors in an hour----indicating that the write command timed out. If I get 5 consecutive soft errors, I increment a hard error count, power down the card, reinitialize and start over with the command. This can take almost a second. Thankfully, I have not had any hard errors in the last few months. Mark Borgerson
Mark Borgerson a &#4294967295;crit :
> In article <4af7e1dd$0$11637$426a74cc@news.free.fr>, > christophe.braillon@free.fr says... >> Mark Borgerson a &#4294967295;crit : >>> In article <4af5a261$0$29577$426a74cc@news.free.fr>, >>> christophe.braillon@free.fr says... >>>> Hi everyone, >>>> >>>> I have interfaced a Micro SD card to a MSP430 microcontroller using the >>>> SPI mode. Everything worked perfectly fine but I recently had problems >>>> when writing too often into my files. >>>> >>>> Here is my setup. As I said the card (SDHC 4Go) is directly connected to >>>> the microcontroller via its USCI (SPI) peripheral. I have written the >>>> basic SD access (register read / write, basically to find if a card is a >>>> SDSC or SDHC, page read / write). I have followed the reduced SD >>>> sp&#4294967295;cification to do so (CRC activated, waiting after any operation for >>>> the card to leave the 'busy' state). On top of that I have implemented a >>>> FAT32 filesystem driver that is able to create / delete, read / write >>>> any file on the root directory (not directory handling, only short >>>> filenames). >>>> >>>> Here is my problem: sometimes (I'm not able to give specific detail on >>>> 'when' because it varies a lot in time and frequency), complete sectors >>>> of my create files are filled with zeroes... >>>> >>>> I have investigated on my filesystem implementation because it seemed >>>> obvious that the problem what there. But it turned out that I have >>>> managed to reproduce that bug only writing pages using the low level SD >>>> access. For exemple if i write 4000 pages of 512 bytes filled with 0xAA >>>> bytes, I get 2 to 10 pages filled with 0x00. >>>> >>>> Does anyone encountered this kind problem? I would be so grateful if >>>> someone could give me at least a hint on how to solve that bug because >>>> I'm wasting my last week investigating on that. >>>> >>> My experience with SD cards and the MSP430 has led me to believe that >>> almost all errors are related to the timing of operations. In >>> particular, the time for the SD card to indicate that it is ready >>> for a new write increases drastically when the card's internal >>> processor decides it is time to erase a new block of the flash so >>> that new data can be written. On one set of cards, this happened >>> at 32KB intervals. At that point, the time to write a block >>> (after waiting for the card to indicate it is not busy) would bump >>> up from a few milliseconds to over 50 mSec. The delay probably >>> depends on the speed rating of the card. >>> >>> My technique for debugging these issues was to toggle an I/O bit >>> at the start and end of the write function and observe the >>> timing. >>> >>> Of course, the other possibility is that your write routine isn't >>> sending what you think it is. Perhaps you could repeat your >>> test, but insert code in the routine that writes the buffer to >>> check to make sure it isn't writing a buffer full of zeros. >>> A wayward buffer pointer might cause something like this. >>> >>> Mark Borgerson >>> >> Hi, >> >> I have read once again the SD simplified specification and Samsung >> MicroSD Card datasheet, it seems that there is a flag called >> READY_FOR_DATA in the card status (not in SD Status register) that >> indicates the card accepts write operations. It seems to be accessible >> in the R1 response but only in SD Mode, I have not seen any mean to >> access this bit in SPI mode, does anyone know how to have this information? >> > > Have you tried your code on a SanDisk micro SD card? I have found that > different manufacturers have very different delays before the card is > ready. > > In my SD_Send_Command code, I execute the following check whenever the > LAST operation was a write > i = 0; > SD_CS_ON; // macro to set CS bit low > do { // Check for card busy > if(i++ > 30000) break; > uc = Get_SD_SPI(); // get a byte from the SPI > } while(uc == 0); // uc stays zero while card busy > ..... > // more code to actually send the command > > > > At the processor and SPI clock speed I am using, 30,000 times > through this loop is about 100 milliseconds. > > > At one time, I added code to keep track of the loop count in > this routine. IIRC, it was normally under 20, but would bump > up to the 30,000 limit at times. > > I also have code that checks for valid responses to commands. > If I don't get a valid response, I increment a soft error count > and try the command again---including the wait for the card > to be not busy. I generally see two or three of these soft > errors in an hour----indicating that the write command timed > out. If I get 5 consecutive soft errors, I increment a hard > error count, power down the card, reinitialize and start > over with the command. This can take almost a second. Thankfully, > I have not had any hard errors in the last few months. > > > Mark Borgerson
I added exactly the same kind of code after reading the samsung datasheet on micro SD (the description of SPI protocol is far more detailed than in the simplified SD specification). I have not seen any write problem ever since. But has Dijkstra said, test can only show the presence of bug not their absence ;) Thank you again for your advice I'll keep in mind your system for error recovery, this might be useful Christophe
Christophe Braillon wrote:
> > I added exactly the same kind of code after reading the samsung > datasheet on micro SD
Is this data sheet online somewhere ? Google has not turned it up for me. :-( Thanks don
don a &#4294967295;crit :
> Christophe Braillon wrote: >> >> I added exactly the same kind of code after reading the samsung >> datasheet on micro SD > > Is this data sheet online somewhere ? > > Google has not turned it up for me. :-( > > Thanks > > don
I found it here: http://www.mt-system.ru/documents/microsd%20version%200.3.pdf Christophe
>don a &#65533;crit : >> Christophe Braillon wrote: >>> >>> I added exactly the same kind of code after reading the samsung >>> datasheet on micro SD >> >> Is this data sheet online somewhere ? >> >> Google has not turned it up for me. :-( >> >> Thanks >> >> don > >I found it here: >http://www.mt-system.ru/documents/microsd%20version%200.3.pdf > >Christophe >
Hello Christophe, I read your posts and found that you have done something exactly I am doing these days. I have developed FAT32 file system on micro SD card using Freescale's MPC5554. I am using 4GB class 4 Kingston card. My read/write in file is working absolutely fine. The problem I am getting is with speed. When I use 3MHz SPI clock, I can write 1MB of data in 1 min which is pretty low speed!! When I try to use SPI clock above 3 MHz (after initialization), I am not able to read / write from the card. What is your speed of data transfer with SD card? I have read that I can go up to 25MB/s of speed by using 4 data lines. What maximum speed I can go up to using SPI mode (that is single data line)and how can I achieve this speed? I would be so grateful if you could please help me. I have been working on this problem a long time but couldn't find any solution till today. Thanks, Sana --------------------------------------- Posted through http://www.EmbeddedRelated.com

The 2024 Embedded Online Conference