SDHC card Interface, taking long time while writing

Discussion in 'General Electronics Chat' started by aamirali, Dec 13, 2012.

  1. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    HI,

    I have written SDHC code,for writing block of data.Problem is block,italic part took long time i.e around 2ms.Return byte took long time to get to zero so that condition exits, My other code has 1ms interrupt in which it has to do other task, those tasks cannot be written in interrupt. Can someone suggest me some way how to deal with this. So that when control reaches here,I can jump to take my reading of 1ms & return here after 2 ms to try again.

    Or I am doing some mistake here in code & it should not take 2ms .

    Rest my code is working fine, writing & reading SD card are OK.

    Code ( (Unknown Language)):
    1.  
    2. response = SD_Command(CMD24_WRITE_SINGLE_BLOCK , startBlock,0x00);   // if reponse not zero then there is error    // try calling sdhc_write_block again
    3.  
    4. if(response != 0)
    5.  
    6. return response;
    7.  
    8.  
    9. SD_CSenable(); //enable chip select by setting it zero
    10.  
    11. SPI_Byte( 0xfe ); // start block token 11111110b
    12.  
    13.  
    14. for(i = 0; i < 512; i++) //write globally defined buffer into sd card
    15. {
    16. SPI_Byte(buffer1[i]);
    17. }
    18.  
    19.  
    20. SPI_Byte(0xff); // bogus CRC (16-bit), CRC ignored
    21. SPI_Byte(0xff);
    22.  
    23. response = SPI_Byte(0xff);
    24.  
    25. if( (response & 0x1f) != 0x05)
    26. {
    27. SD_CSdisable();
    28. return response;
    29. }
    30.  
    31. [B][I]count = 0xfffe;
    32. while(!SPI_Byte(0xff)) // wait for write complete and go idle
    33. {
    34. if(count-- < 10)
    35. {
    36. SD_CSdisable();
    37. return SDHC_ERR_W_TIMEOUT;
    38. }
    39. }[/I][/B]
    40.  
    41. SD_CSdisable();
    42.  
    43. SPI_Byte(0xff); // 8 clock cycle delay while SS line high
    44.  
    45. SD_CSenable(); // reset SS line low to verify if card still busy
    46.  
    47. count = 0;
    48. while(!SPI_Byte(0xff)) // wait for write complete and go idle
    49. {
    50. if(count++ > 0xfffe)
    51. {
    52. SD_CSdisable();
    53. return SDHC_ERR_W_TIMEOUT;}
    54. }
    55. SD_CSdisable();
    56.  
    57.  
    58. return SDHC_ERR_NONE;
    59.  
    60. [/i]
     
  2. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
    Does this happen on every write?

    SD cards sometimes do take a long time to complete a write; as long as 35 milliseconds in some cases.

    I once ran a test of timing writes as my microcontroller program wrote several thousand sequential sectors to an SD card. Most writes took only about 600 microseconds (if I remember correctly) but approximately (but not strictly) every 512 sectors, the write would have the very long delay of many milliseconds.

    I assume that the card internal firmware occasionally needs the extra time to pre-erase large blocks of sectors, but that's just a guess.

    If the SDHC card is behaving differently than standard SD cards, all I can suggest is to read the manufacturer's documentation to see if they warn of any possible performance issues. Or try another card.
     
Loading...