This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

interfacing sdhc card to mcu

Other Parts Discussed in Thread: TM4C123GH6PGE

i am interfacing sdhc card & referring below ti note for this.

http://www.ti.com/apps/docs/litabsmultiplefilelist.tsp?literatureNumber=spraao7&docCategoryId=1&appId=307

1. error in code:i think

in file,:SD_SPI_Initialization.c 

function: void sd_version2_initialization()

error:  for(i=0;i<3;i++)
            send_if_cond_response[i] = spi_xmit_byte(DUMMY_DATA);

i think i <= 3 should be there.

 

2. problem:

i am using spi protocol to interface sdhc card.

i have three different cards: sandisk , kingston , sony.

i am successfully interface sandisk , kingston card. but having problem in sony card.

problem is same as : http://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/p/291450/1018314.aspx#1018314.

Card don't go to idle state. code gets stuck in :
spi_xmit_command(GO_IDLE_STATE, STUFF_BITS, INITIAL_CRC);   
      
       RESET_RESPONSE;                            //Reset response   
    while(response != IN_IDLE_STATE)        //Wait until card responds with IDLE response
        sd_command_response();

As above link suggest I have already 10K pull up on DI , DO & unconnected 8 and 9 pin.
I haven't put pull up on CS & CLK pin.

3. where I can fat32 example code download link:
FatSD example from TI Resource Explorer/TI-RTOS/ARM/Tiva C Series/Tiva TM4C123GH6PGE/Examples.

  • Hello Aamir Ali1,

    The application note was written for a C2000 part. What platform and processor are you using?

    RE: 1

    The array is indeed of unsigned 16-bit integers and so it should probably be initializing all of them. I think it would be more readable to change the i<3 to i<4 but the i<=3 would work as well.

    RE: 2

    It is difficult to offer any advice based on the information you have provided.  Can you describe how you have the SD card interface attached? How do you have the pins configured?  Does the Sony card have any specific model information?

    RE: 3

    The FatSD example is a part of the TI-RTOS package. You can find more information and download links here: http://processors.wiki.ti.com/index.php/TI-RTOS .  You may also want to look at the 3rd-party FatFS package which is popular for embedded systems and is available on the web.

  • Hello Stellaris,

    I am using  MCU with TI's ADS-1248 to sample ADC channels & store them in SD card in fat32 format

    I am quite sure about configuration of SPI pins.

    1. Error:

    I think I have found one more error in code. After sending CMD55_APP_CMD ,  ACMD41_SEND_OP_COND should be sent to initialize card as per datasheet. but sample code send CMD1_SEND_OP_COND.

    Can you please check if code is tested

    2.Hardware:  I have used 10K pull upon DI,DO, Pin 8 & 9 of SDHC card. I haven't used pull-up on CS 7 CLK pin.

    3. Sony card link is below. I am using 4GB sony card. Specs don't mention it support SPI interface. But i guess by default all cards should do. I mailed to sony long back to confirm this but no reply till now.

    http://www.sony.co.in/product/sf-8n4

    4. I have downloaded RTOS example code. But I am using Keil for programming. So cannot open that fle. Do TI have similar application note explaining FAT32 interface. 

    This is "GIVE ME CODE" request. :)

    5. Below is my code to initialize sd card. I have tweaked code as per my need:

    /**
     *  @function : static uint8_t SD_init(void)
     *  @brief    : initialize sd card at 360Khz first
     *  @parameter: none
     *  @return   : 0 - no error , 1 error
     **/
    static uint8_t SD_init(void)
    {
    	uint32_t count;
    	uint8_t response;
        
        SD_CS_DISABLE();   
    	SD_INIT_SETTING(KHZ234);               /* initialize with 234Khz */
        
        wait_delay_us(1000); 
        
    	for(count=10;count>0;count--)          /* send 80 bit long cycle to establish link with SD card */
    		unsigned_sd_spi_byte(0xff);        /* fulfills minimum 74 clocl cycle requirement */
     
        wait_delay_us(1000); 
        
        
    /****************************************************************************/    
        SD_CS_ENABLE();
    		
        (void)SD_Command( CMD0_GO_IDLE_STATE, 0,0x95 ); 
        response = 0xff;
        
        count = 0xffff;
        while((response != 0x01) && (--count))
        {
            response = sd_command_response();
        }    
    
    	if( !count )
    		return( 1 );
        
    /**************************************************************************************************************/
        
    	response = 0xff;
    	unsigned_sd_spi_byte( 0xff );
        
    /******************************************************************************************************************/	
    	
        (void)SD_Command( CMD8_SEND_IF_COND , 0x000001AA, 0x87); 
        
        count = 0x0fff;
        while( (response != 0x01) && (response != 0x05) && (--count) )
        {
            response = unsigned_sd_spi_byte( 0xff );
        }    
        
        
        if( response == 0x05 )
        {
            //sd_ver1_init();
        }    
        else if( response == 0x01 )
        {
            response = sd_version2_initialization();
            if( response != 0 )
            {
                return 1;
            }    
        } 
        else
        {
            return 1;
        }    
        
        
        SD_CS_DISABLE();
        unsigned_sd_spi_byte(0xff); 
    
        return 0;
    
    } /* function ends here */
    
    
    uint8_t sd_version2_initialization(void)
    {
        uint8_t send_if_cond_response[4], ccs_check , response , ocr_data[4];
        uint32_t cnt , cnt2;
        
        for(cnt = 0 ; cnt < 4 ; cnt++)
        {
            send_if_cond_response[cnt] = unsigned_sd_spi_byte(0xff); 
        }
        
        if((send_if_cond_response[2] != 0x01) || (send_if_cond_response[3] != 0xaa))
        {
            return 1;
        }    
        
        response = 0xff;
    	unsigned_sd_spi_byte( 0xff );
        
        cnt2 = 0x0fff;
        while( (response != 0) && (--cnt2) )
        {
            response = 0xff;
            unsigned_sd_spi_byte( 0xff );
            
            (void)SD_Command( CMD55_APP_CMD , 0x00, 0xff); 
        
            cnt = 0x0fff;
            while( (response != 0x01) && (--cnt) )
            {
                response = unsigned_sd_spi_byte( 0xff );
            }    
            if( !cnt )
            {
                return 1;
            }    
        
            response = 0xff;
            unsigned_sd_spi_byte( 0xff );
            
            (void)SD_Command( ACMD41_SEND_OP_COND , 1073741824, 0xff);
            response = sd_command_response();
        }    
        if( !cnt2 )
        {
            return 1;
        }    
        
        
        response = 0xff;
        unsigned_sd_spi_byte( 0xff );    
    
        (void)SD_Command( CMD58_READ_OCR , 0, 0xff);
        cnt = 0x0fff;
        while((response != 0x00) && (--cnt))
        {
            response = sd_command_response();
        }       
        if(!cnt)
        {
            return 1;
        }    
        
        
        for( cnt = 0 ; cnt < 4 ;  cnt++ )
        {
            ocr_data[cnt] = unsigned_sd_spi_byte( 0xff );
        }
        unsigned_sd_spi_byte( 0xff );
        
        ccs_check = ocr_data[0] & 0x40; //Mask ocr_contents to test for High Capacity
        
        if( ccs_check == 0x40 )
        {
            high_capacity = 1;
        }    
        
        
        response = 0xff;
        unsigned_sd_spi_byte( 0xff );     
        
        return 0;
        
    }    
    
    
    uint8_t sd_command_response(void)
    {
        uint8_t response;
        uint32_t cnt;
        
        response = 0xff;
        
        for( cnt = 0 ; cnt < 8 ; cnt++ )
        {
            response = unsigned_sd_spi_byte( 0xff );
            
            if( (response == 0x01) || (response == 0x00) )
            {
                break;
            }    
            else if( response != 0xff )
            {
                break;
            }    
            
        }
        
        return response;
        
    }
    
    
    
    /**
     *  @function : static uint8_t SD_Command(uint8_t ThisCommand, uint32_t ThisArgument, uint8_t crc)
     *  @brief    : send command on SD card.
     *  @parameter: ThisCommand : Which command to be send on SD card
     *              ThisArgument: At which address 
     *              crc         : Crc 
     *  @return   : response
     **/
    static uint8_t SD_Command(uint8_t ThisCommand, uint32_t ThisArgument, uint8_t crc) 
    {
    	b_ul Temp; 
    	uint8_t count;												
    		  		  						  /* enable the device... */ 
    	unsigned_sd_spi_byte(0x40 | ThisCommand); 			  /* send argument or with 0x40 as first two bits are fixed 0 & 1 */ 
    	Temp.ul = ThisArgument;
    
    	for( count=4; count>0; count-- )                      
    		unsigned_sd_spi_byte( Temp.b[ count-1 ] ); 										
    	
    	unsigned_sd_spi_byte( crc );              
    	
    	
    	return( 1 ); 
    
    } /* function ends here */

  • Hello Aamir Ali1,

    You still have not indicated which MCU you are using. As mentioned, the application note and software you are referencing was created for the TI C2000 MCUs.  You have posted to the Tiva C Series MCU forums and thus I will be of limited help to you with C2000-related software and hardware questions.

    RE: 1

    I would expect from looking at the source code attached to this application note, that this is a sample implementation intended to provide a starting point and not a turn-key solution. The C2000 forum team may be able to provide you clarification.

    RE: 3

    I am not able to find any details on whether this SD card supports SPI mode. But, since it does not appear to be a microSD card, I would expect it to support SPI mode.

    RE: 4

    The TI-RTOS examples require the TI CCS toolchain.  If you are using a Tiva C Series MCU, we have examples in TivaWare for SD card SPI usage which demonstrate using of the FatFS 3rd-party package. The TivaWare examples are all buildable with Keil/IAR/GCC/CCS. See for instance the DK-TM4C129x kit software's sd_card example.

  • Hi Stellaris,

    I am TM4C123 launchpad kit & was taking reference from mentioned app not. 

    That was not working correctly. However code provided in DK-Tm4c worked fine & now sony card is also working.

    Thanks it solves the problem. 

    However I have some queries regrading that code, for which I will start another thread later.

    Regards

    Aamir

  • Just one question from your reply You had replied:

    " But, since it does not appear to be a microSD card, I would expect it to support SPI mode."

     

    Do uou mean to say that all/some microsd card don't support SPI mode. But I had read that microsd card also support SPI mode.

  • Hi Aamir Ali1,

    I thought I had remembered reading that SPI mode support in MicroSD cards was optional, but I just looked at the simplified spec and that does not appear to be the case. While other "embedded" SDIO devices are not required to support it, SPI mode is "mandatory" per the spec for all SD cards. 

    Sorry for the confusion.

  • Hi Ti..........................

    I want to interface SD card with TM4C123GH6PGE LAUNCHPAD . I don't know where should i start or what is the starting point.

    I am using KEIL.

    Plz tell me the key points of interfacing so that i can make it asap. 

    Thanks 

    KAMRAN AHMED  (kamranahmad1992@gmail.com)