writting to sd using kea128 (SPI)

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

writting to sd using kea128 (SPI)

Jump to solution
3,440 Views
emilioaronmoyer
Contributor III

Hello,

Does anyone have code for writting and reading to a SD card using KEA128 with SPI? or can someone explain to me me, I have no idea how to do it.

thanks a lot.

Labels (1)
Tags (1)
1 Solution
2,061 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi

Please refer my colleague write an document about how to use FATFS with SDCard via SPI port based on KL product:

Porting FatFs file system to KL26 SPI SD card code


Wish it helps.

Have a great day,
Ma Hui
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

38 Replies
1,815 Views
emilioaronmoyer
Contributor III

I already can write and read raw data to the SD card, what do i need to do to write a txt file that a computer can read? Do I need fat32 library? I attache my recent advances

0 Kudos
1,815 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi

Yes, it needs the file system.

Customer could refer KSDK software, which provides FATFS based on SD card using SPI interface.

And I also find ARM mbed also provides related SD Card File system.

Wish it helps.

Have a great day,
Ma Hui

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

0 Kudos
1,815 Views
emilioaronmoyer
Contributor III

Hello Hui,

I couldn't found the examples or libraries at KSDK software ,do you have them or a more specific link?.

Sorry for a lot of questions.

0 Kudos
1,815 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi

I download the KSDK1.3 for MKL46Z256xxx4 product, you could find related SPI SD FATFS source code at below folder:

C:\Freescale\SDK_1.3_MKL46Z256xxx4\middleware\filesystem\fatfs\fsl_sd_disk\src


Wish it helps.

Have a great day,
Ma Hui
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,815 Views
emilioaronmoyer
Contributor III

Hello,

I figure it out how to solve the enable slave select of the SD1 component, i could build the project with no errors, when I tried to run the program,pastedImage_1.png

it get into a while and never goes ot if it. This while is in the SD1_SPI_WRITE() funtion pastedImage_0.png

Then I tried just calling that funtion infinite times (infinite while) in the main.c to see with an oscilloscope if it is sending pulses, but I got the same result. Get into the same while.

Then i tried to with a code I had in the fuction SD1_SPI_WRITE() and do the same infinte while, but now I can see pulses with the oscilloscope. So the spi is well configure.

pastedImage_2.png

Do I configure something wrong of the sd componenet?? Why the spi is sending nothing with the code generated by defualt??

I attach my project.

Regards,

Emilio

0 Kudos
1,815 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi

I am using the same SPI setting with SPIMaster_LDD component, there without any problem to find the SPI0_MOSI(PTE1) pin with signal output.

Please check my tested code attached.


Wish it helps.

Have a great day,
Ma Hui
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,815 Views
emilioaronmoyer
Contributor III

Hello Hui_Ma,

Yes the problem is not SPI, the SPI works but when I load the other components (sd1:sd_card and fat1:fat_filesystem)

It never goes out the while loop of the SD1_SPI_WRITE() funtion.

pastedImage_0.png

Is there another way to do it?? or what am i doing wrong??

Regards,

Emilio

0 Kudos
2,062 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi

Please refer my colleague write an document about how to use FATFS with SDCard via SPI port based on KL product:

Porting FatFs file system to KL26 SPI SD card code


Wish it helps.

Have a great day,
Ma Hui
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

1,815 Views
emilioaronmoyer
Contributor III

Where can I dowload the .c file of the sd to add it to my project?? I'm using code warrior t is it possible to add the bin files to the project??

0 Kudos
1,815 Views
emilioaronmoyer
Contributor III

I upload to the card the SD_CARD_LFN_TEST.bin is it correct?? If yes, how can I see if it is working?

0 Kudos
1,815 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi

KEA128 product SPI module is very similar with KL25 product.

There is a document about using FRDM-KL25Z board working with SD Card using CodeWarrior Processor Expert component.

More detailed info, please check below link:

Arduino Data-Logger Shield with the FRDM-KL25Z Board | MCU on Eclipse

Re: FRDM-KL25Z - slow SPI clock

And please check attached CodeWarrior project, which my colleague had tested working with TWR-KL25 and TWR-MEM (SD Card).

It was able to create the FILE.TXT fie in the SD card with "SD Card test" in the content.

Wish it helps

Have a great day,
Ma Hui

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

0 Kudos
1,815 Views
emilioaronmoyer
Contributor III

Hello Hui,

I decided to use processor expert, I create a new project following the data logger example of your collegue, but still having some errors. I attached the project, can you check it?, please!!

0 Kudos
1,815 Views
emilioaronmoyer
Contributor III

I forget to attached the project: Some of the error i got are:

pastedImage_0.png

pastedImage_1.png

pastedImage_4.png

pastedImage_3.png

Regards,

Emilio

0 Kudos
1,815 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi

I download your attached <fattest.rar> file and there without any compile error reported during project build.

If you attached another project?

You could using CW menu [help]->[About CodeWarrior Develop Studio]->[Installation Details] to check Processor Expert version.

Please check your processor expert tool version, below is my tested processor expert software version:

2016-03-08_14-12-46.jpg

Customer can use [Help] -> [Check for Updates] to update the Processor Expert software.


Wish it helps.

Have a great day,
Ma Hui
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,815 Views
emilioaronmoyer
Contributor III

Hello Ma Hui,

I update the processor expert version to the one you have. But I still have the same problem, in the SD1 component when I enable the Slave Select  I got this error:

pastedImage_0.png

pastedImage_1.png

If I disable the slave select, no errors appear but nothing happen with the sdcard.

pastedImage_3.png

It never enter to the FAT1_open funtion in the main part, it just do the fat1_mount and then finish the program.

pastedImage_2.png

I'm using this adafruit module to write to sd card.https://www.adafruit.com/products/254https://www.adafruit.com/products/254

I reattached the slave select enable, so you can see the error.

Regards,

Emilio

0 Kudos
1,815 Views
emilioaronmoyer
Contributor III

where is the spi cinfiguration? I cant find it

0 Kudos
1,815 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi

The SPI initialization code is located at <SM1.c> SM1_Init() function.

/*

** ===================================================================

**     Method      :  SM1_Init (component SPIMaster_LDD)

**

**     Description :

**         Initializes the device. Allocates memory for the device data

**         structure, allocates interrupt vectors and sets interrupt

**         priority, sets pin routing, sets timing, etc.

**         If the "Enable in init. code" is set to "yes" value then the

**         device is also enabled(see the description of the Enable()

**         method). In this case the Enable() method is not necessary

**         and needn't to be generated.

**         This method can be called only once. Before the second call

**         of Init() the Deinit() must be called first.

**     Parameters  :

**         NAME            - DESCRIPTION

**       * UserDataPtr     - Pointer to the user or

**                           RTOS specific data. This pointer will be

**                           passed as an event or callback parameter.

**     Returns     :

**         ---             - Device data structure pointer.

** ===================================================================

*/

LDD_TDeviceData* SM1_Init(LDD_TUserData *UserDataPtr)

{

  /* Allocate LDD device structure */

  SM1_TDeviceDataPtr DeviceDataPrv;

  /* {Default RTOS Adapter} Driver memory allocation: Dynamic allocation is simulated by a pointer to the static object */

  DeviceDataPrv = &DeviceDataPrv__DEFAULT_RTOS_ALLOC;

  DeviceDataPrv->UserData = UserDataPtr; /* Store the RTOS device structure */

  /* Interrupt vector(s) allocation */

  /* {Default RTOS Adapter} Set interrupt vector: IVT is static, ISR parameter is passed by the global variable */

  INT_SPI1__DEFAULT_RTOS_ISRPARAM = DeviceDataPrv;

  DeviceDataPrv->ErrFlag = 0x00U;      /* Clear error flags */

  /* Clear the receive counters and pointer */

  DeviceDataPrv->InpRecvDataNum = 0x00U; /* Clear the counter of received characters */

  DeviceDataPrv->InpDataNumReq = 0x00U; /* Clear the counter of characters to receive by ReceiveBlock() */

  DeviceDataPrv->InpDataPtr = NULL;    /* Clear the buffer pointer for received characters */

  /* Clear the transmit counters and pointer */

  DeviceDataPrv->OutSentDataNum = 0x00U; /* Clear the counter of sent characters */

  DeviceDataPrv->OutDataNumReq = 0x00U; /* Clear the counter of characters to be send by SendBlock() */

  DeviceDataPrv->OutDataPtr = NULL;    /* Clear the buffer pointer for data to be transmitted */

  /* SIM_SCGC4: SPI1=1 */

  SIM_SCGC4 |= SIM_SCGC4_SPI1_MASK;                                                  

  /* Interrupt vector(s) priority setting */

  /* NVIC_IPR2: PRI_11=0x80 */

  NVIC_IPR2 = (uint32_t)((NVIC_IPR2 & (uint32_t)~(uint32_t)(

               NVIC_IP_PRI_11(0x7F)

              )) | (uint32_t)(

               NVIC_IP_PRI_11(0x80)

              ));                                                 

  /* NVIC_ISER: SETENA|=0x0800 */

  NVIC_ISER |= NVIC_ISER_SETENA(0x0800);                                                  

  /* PORTE_PCR3: ISF=0,MUX=2 */

  PORTE_PCR3 = (uint32_t)((PORTE_PCR3 & (uint32_t)~(uint32_t)(

                PORT_PCR_ISF_MASK |

                PORT_PCR_MUX(0x05)

               )) | (uint32_t)(

                PORT_PCR_MUX(0x02)

               ));                                                 

  /* PORTE_PCR1: ISF=0,MUX=2 */

  PORTE_PCR1 = (uint32_t)((PORTE_PCR1 & (uint32_t)~(uint32_t)(

                PORT_PCR_ISF_MASK |

                PORT_PCR_MUX(0x05)

               )) | (uint32_t)(

                PORT_PCR_MUX(0x02)

               ));                                                 

  /* PORTE_PCR2: ISF=0,MUX=2 */

  PORTE_PCR2 = (uint32_t)((PORTE_PCR2 & (uint32_t)~(uint32_t)(

                PORT_PCR_ISF_MASK |

                PORT_PCR_MUX(0x05)

               )) | (uint32_t)(

                PORT_PCR_MUX(0x02)

               ));                                                 

  /* SPI1_C1: SPIE=0,SPE=0,SPTIE=0,MSTR=1,CPOL=0,CPHA=0,SSOE=1,LSBFE=0 */

  SPI1_C1 = (SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK); /* Set configuration register */

  /* SPI1_C2: SPMIE=0,??=0,TXDMAE=0,MODFEN=1,BIDIROE=0,RXDMAE=0,SPISWAI=0,SPC0=0 */

  SPI1_C2 = SPI_C2_MODFEN_MASK;        /* Set configuration register */

  /* SPI1_BR: ??=0,SPPR=0,SPR=6 */

  SPI1_BR = SPI_BR_SPR(0x06);          /* Set baud rate register */

  /* SPI1_C1: SPE=1 */

  SPI1_C1 |= SPI_C1_SPE_MASK;          /* Enable SPI module */

  /* Registration of the device structure */

  PE_LDD_RegisterDeviceStructure(PE_LDD_COMPONENT_SM1_ID,DeviceDataPrv);

  return ((LDD_TDeviceData *)DeviceDataPrv); /* Return pointer to the data data structure */

}


Wish it helps
Have a great day,
Ma Hui

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

0 Kudos
1,815 Views
emilioaronmoyer
Contributor III

I don't know how to start using your colleague code, can you guide me. I tried writting this code but the sd card never answer, the spi y running correctly i can comunicate with and arduino.

#include "derivative.h" /* include peripheral declarations */

#include "CLK.h"

#include "SPI.h"

#include "typedefs.h"

static UINT8 errorSD;

int main(void)

{

  int counter = 0;

  Clk_Init();

  initSPI();

  int i;

  for(i=0;i<8;i++){

  SPI_Send_byte(0xff);

  }

  SPI_Send_byte(0x40);

  for(i=0;i<8;i++){

  SPI_Send_byte(0x00);

  }

  SPI_Send_byte(0x95);

  SPI_Send_byte(0xff);

  while(SPI_Receive_byte!=0x01);

  for(;;) {  

    counter++;

    //SPI_Send_byte(0x45);

  }

  return 0;

}

SPI initialization

#include "SPI.h"

void initSPI(){

  CONFIG_PIN_AS_GPIO(PORT_E,0,OUTPUT); /* CLK*/

  CONFIG_PIN_AS_GPIO(PORT_E,1,OUTPUT); /* MOSI*/

  CONFIG_PIN_AS_GPIO(PORT_E,2,INPUT); /* MISO*/

  CONFIG_PIN_AS_GPIO(PORT_E,3,OUTPUT); /*PS*/

  OUTPUT_SET(PORT_E,3);

  SIM_SCGC |= SIM_SCGC_SPI0_MASK;

  SIM_PINSEL0 |= SIM_PINSEL_SPI0PS_MASK;

  SPI0_C1 |= SPI_C1_MSTR_MASK; // SPI0 master mode 

                          

  SPI0_BR = 0x43;  //SPPR = 4, SPR = 3, bps div = (SPPR+1)*2^(SPR+1) = 80, baudrate= 24Mhz/80=300khz 

  SPI0_C1 |= SPI_C1_SSOE_MASK;                           

  SPI0_C1 &= (~SPI_C1_CPHA_MASK);  // clock polarity 

  SPI0_C1 &= (~SPI_C1_CPOL_MASK);  //clock phase 

  SPI0_C1 &= (~SPI_C1_LSBFE_MASK);  // LSB:most significant  

 

  SPI0_C1 &= (~SPI_C1_SPIE_MASK);                  //Disable RX interrrupt   

  SPI0_C1 &= (~SPI_C1_SPTIE_MASK);         //Disable the transmit interrupt  

  SPI0_C2 |= SPI_C2_MODFEN_MASK;  

  

  SPI0_C1 |= SPI_C1_SPE_MASK;  // enable SPI module

}

void SPI_Send_byte(UINT8 u8Data){

  while ((SPI0_S & SPI_S_SPTEF_MASK) == 0);

  (void)SPI0_S;

  SPI0_D=u8Data;

}

UINT8 SPI_Receive_byte(void)

{

  //(void)SPI0_D;

  //SPI0_D=0xff;

  SPI_Send_byte(0xff);

  while ((SPI0_S & SPI_S_SPRF_MASK) == 0);

  return SPI0_D;

}

void SPI_High_rate(void)

{

  SPI0_C1 = 0x00;

  SPI0_BR = 0x01; //11 3 MHz 24MHz  -  01 4MHz 16MHz   

  SPI0_C1=0x50;

}

0 Kudos
1,815 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi

KEA128 product SPI module is same with KE02 product.

Please check below KE02 bare-metal project, which provides the SPI related demo code and driver.


Wish it helps
Have a great day,
Ma Hui

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

0 Kudos
1,815 Views
mjbcswitzerland
Specialist V

Hi

The uTasker project includes SD card support for the KEA128: http://www.utasker.com/kinetis/TRK-KEA128.html

It simulates the KEA128, SPI and SD card to give most efficient application development capabilities or for detailed study purposes, in case that is your primary intention.

http://www.utasker.com/docs/uTasker/uTasker_utFAT.PDF explains details about SPI, SD card and FAT12/16/32 with SFN and LFN.

In case you are not interested in an out-of-the-box solution you can also take FsFat and port it to the KEA128 - this is fairly straight-forward since the KEA128 has SPI highly compatible with KL parts so you should be able to have a working project within a couple of weeks.

If you need to re-develop a solution of your own you can use the ideas in the links, where there are also further links to FAT and SD card specifications where it is usually possible to have basic reliable operation after around 2 months work and long file name and advanced support within about 4..6 months.

Regards

Mark

0 Kudos