SD card interfacing with KL25Z

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SD card interfacing with KL25Z

Jump to solution
3,013 Views
arthshah
Contributor II

i am trying to port fatfs to Sd card.

i refer this link https://community.nxp.com/docs/DOC-330958 

now my question is that which file i need to added in my project

because in main() function no any spi related function are call.

assign to @TIC kerry 

0 Kudos
1 Solution
1,922 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hello Arth Shah,

    Thanks a lot for your patient.

    Just as you send me the message, you need the KDS SD Fatfs project for KL25, and you refer to my post before which just have MDK project.

   Now, I already help you finished the KDS project, the same board as my own post which you already give the link, the KDS project is working OK on my side, you can refer to the attachment.

   The project can be find in folder:  SD_FatFS-KL26Z_KDS\boards\frdmkl26z\demo_apps\hello_world\kds

   When you open the KDS IDE, just select the workspace path in : SD_FatFS-KL26Z_KDS

  This is the test result:

 Com print information:

pastedImage_1.png

SD card information:

pastedImage_2.png

Wish it helps you!


Have a great day,

Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
49 Replies
1,498 Views
pulsatrixp1
Contributor III

Hello, 

I am wondering if this supports SPI communication to SD card without using a FAT system, and to use the SD card only as storage.

If so, how to proceed?

Thanks in advance

0 Kudos
1,498 Views
mjbcswitzerland
Specialist V

Hi

Any FAT SD card solution can be used also as a low level sector read/write interface.

You still need to switch the card into SPI mode (the first part of SD card mounting) but after you can read and write sectors however you like.

Regards

Mark

0 Kudos
1,498 Views
mjbcswitzerland
Specialist V

Hi

Here is a very short video that I have made for you showing reading and writing RAW sectors: https://youtu.be/VY-I0_h5p4Q
You can get this form the open source uTasker project at http://www.utasker.com/kinetis.html which contains all needed for SD card operation as well as complete operation simulation. If you are no interested in a complete working solution you can simply use VS to step through all code to see how it works and then copy the bits that are of interest.

SPI interface details are in the first chapters of http://www.utasker.com/docs/uTasker/uTasker_utFAT.PDF

Regards

Mark

0 Kudos
1,923 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hello Arth Shah,

    Thanks a lot for your patient.

    Just as you send me the message, you need the KDS SD Fatfs project for KL25, and you refer to my post before which just have MDK project.

   Now, I already help you finished the KDS project, the same board as my own post which you already give the link, the KDS project is working OK on my side, you can refer to the attachment.

   The project can be find in folder:  SD_FatFS-KL26Z_KDS\boards\frdmkl26z\demo_apps\hello_world\kds

   When you open the KDS IDE, just select the workspace path in : SD_FatFS-KL26Z_KDS

  This is the test result:

 Com print information:

pastedImage_1.png

SD card information:

pastedImage_2.png

Wish it helps you!


Have a great day,

Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,499 Views
arthshah
Contributor II

hello kerry,

as we discuss this topic in message i have problem in spi_send or spi_init(); 

my execution stop in spi_send() function.

please help me for this.

thank you.

0 Kudos
1,499 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Arth Shah,

     Comment the Fatfs code, just leave MMCinit(),

    Main code like this:

int main(void)
{
    char ch;
    unsigned int i,j;
    FATFS fs;
    FRESULT fr;
    FIL            fil;
    UINT bw;
    char file_name1[12]="Test.csv";
    char file_name2[12]="Test.txt";

    /* Init board hardware. */
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    PRINTF("hello world.\r\n");
    spiInit(Master);

    MMCInit();

/*
    fr= f_mount(&fs,file_name1,0);
    if(fr)
    {
        PRINTF("\nError mounting file system\r\n");
        for(;;){}
    }
    fr = f_open(&fil, file_name1, FA_WRITE | FA_OPEN_ALWAYS);//н¨csvÎļþ
    if(fr)
    {
        PRINTF("\nError opening text file\r\n");
        for(;;){}
    }
    fr = f_write(&fil, "Test1 ,Test2 ,Test3 ,Test4 \r\n", 29, &bw); //±íÍ·
    if(fr)
    {
        PRINTF("\nError write text file\r\n");
        for(;;){}
    }
    fr = f_close(&fil);
    if(fr)
    {
        PRINTF("\nError close text file\r\n");
        for(;;){}
    }
    fr= f_mount(&fs,file_name2,0);
    if(fr)
    {
        PRINTF("\nError mounting file system\r\n");
        for(;;){}
    }
    fr = f_open(&fil, file_name2, FA_WRITE | FA_OPEN_ALWAYS);//н¨txtÎļþ
    if(fr)
    {
        PRINTF("\nError opening text file\r\n");
        for(;;){}
    }
    fr = f_write(&fil, "Test1 ,Test2 ,Test3 ,Test4 \r\n", 29, &bw); //±íÍ·
    if(fr)
    {
        PRINTF("\nError write text file\r\n");
        for(;;){}
    }
    fr = f_close(&fil);
    if(fr)
    {
        PRINTF("\nError close text file\r\n");
        for(;;){}
    }
*/
    while (1)
    {
        //ch = GETCHAR();
       // PUTCHAR(ch);
           for(i=0;i<10;i++) for(j=0;j<65535;j++);
                 printf("\ntest_sd\n");//
    }
}

Debug again, where you stop?

This is my debug picture:

pastedImage_2.png

You can see, even I didn't insert the SD card, the SPI also can send data.

But take care, if your SD card can't give feedback. It may caused by the SD card, this code just for SD2.0 card.

  You can debug the MMCInit, and check where it can run, whether CMD0 have feedback.

  Besides, also refer to my post, check your SD card hardware circuit.


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,499 Views
arthshah
Contributor II

hello kerry,

i had done that what you ask for.

here is my debug picture.

kerry.png

0 Kudos
1,499 Views
arthshah
Contributor II

hello kerry,

i want to tell you something,

i have one new project created by me before some week from link provided by you 

Porting FatFs file system to KL26 SPI SD card code

today i try this project.

unfortunately this project  is work but not for all .sometimes it's work or sometimes not.

please check this code on your side please.

0 Kudos
1,499 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Arth Shah,

    I think you also need to check the hardware, the SPI wire between the MCU and your SD card.

   If there has a lot of distrub in the wire, it may cause the problem.

    You can check your SPI bus wave, the SPI line should not long, it should as short as possible.


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,499 Views
arthshah
Contributor II

thanks karry for the reply.

i already checked my hardware connection many times and SPI line is also short,the problem is occur yet .

0 Kudos
1,499 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Arth Shah,

   Give me some picture of your board and SD card connection.


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,499 Views
arthshah
Contributor II

hello kerry ,

here is attachment of my board and SD interfacing pictures.IMG_20170510_131946.jpg

0 Kudos
1,498 Views
arthshah
Contributor II

IMG_20170510_131957.jpg

0 Kudos
1,498 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Arth Shah,

    Thank you for your pictures.

    Please divide all the wire which connect together, now you are using cable which connect all the SPI wire together.

   Please divide it one by one, especial the SPI_CLK, SPI_MOSI, SPI_MISO, then try again.


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,498 Views
arthshah
Contributor II

hello kerry,

i try this one but still remain same problem on my code,

it's sometime work and sometime not work.

your code is not work still now.

and here i attach my debug picture of my code.

kerry2.png

every time my code is stop here.

what's the problem in SPI?

0 Kudos
1,498 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Arth Shah,

    Try to modify my spiinit code, to add the internal port pull up.

void spiInit (uint8 bMode)
{
    PORTC->PCR[4]  |= PORT_PCR_MUX(1) | PORT_PCR_PE_MASK |PORT_PCR_PS_MASK;                                    
    PORTC->PCR[5]  |= PORT_PCR_MUX(2) | PORT_PCR_PE_MASK |PORT_PCR_PS_MASK;                                  
    PORTC->PCR[6]  |= PORT_PCR_MUX(2) | PORT_PCR_PE_MASK |PORT_PCR_PS_MASK;                                  
    PORTC->PCR[7]  |= PORT_PCR_MUX(2) | PORT_PCR_PE_MASK |PORT_PCR_PS_MASK;

===========

I don't know whether your SD card board have the according external pull up or not, my SD card circuit have the external pull up resistor, so just enable the internal pull up and try again.


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,498 Views
arthshah
Contributor II

hello kerry,

thanks for the help

i had try to make internal pull up enable but still your code is not working on my board but my code is working completely now.

so i want to go ahed with this project

thank you so much for the help.

0 Kudos
1,498 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Arth Shah,

   I simply check your project, still the same structure with me.

   Anyway, it's good to hear your can work on your board,

  For the new question, please create a new question post about it, thank you !


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,498 Views
arthshah
Contributor II

hello kerry,

thanks for your help

i post my new question on Community 

thanks again.

0 Kudos
1,499 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Arth Shah,

   Run into MMCInit, then put break point in the following yellow line:

pastedImage_1.png

Can you run to this place?

Besides, what the board you are using now?


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos