SPI KE02

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

SPI KE02

Jump to solution
977 Views
huyrua291996
Contributor I

Hi

Here is my code for SPI, when I use oscilloscope to measure MISO, the data is true, but when I debug on CW, it always return 0

void SPI_Init(void)
{
SIM_SCGC |= SIM_SCGC_SPI0_MASK;
SPI0_C1 |= SPI_C1_MSTR_MASK; //master
SPI0_C1 &= ~SPI_C1_CPOL_MASK; //mode 2
SPI0_C1 |= SPI_C1_CPHA_MASK; //mode 2
/*SPI0_C1 |= SPI_C1_SSOE_MASK; //auto slave select
SPI0_C2 |= SPI_C2_MODFEN_MASK;//auto slave select*/
SPI0_C1 &= ~SPI_C1_SSOE_MASK; //manual slave select
SPI0_C2 &= ~SPI_C2_MODFEN_MASK; //manual slave select
SPI0_C1 &= ~SPI_C1_LSBFE_MASK;//msb
SPI0_C1 &= ~SPI_C1_SPIE_MASK; //no interrupt
SPI0_C1 &= ~SPI_C1_SPTIE_MASK;//no transfer interrupt
SPI0_C2 &= ~SPI_C2_SPC0_MASK;
SPI_SetBaudrate(1000000);
SPI0_C1 |= SPI_C1_SPE_MASK;
}

uint8_t SPI_SendAndRecvChar(uint8_t input)
{
while((SPI0_S & SPI_S_SPTEF_MASK) == 0);
SPI0_D = input;
while ((SPI0_S & SPI_S_SPRF_MASK) == 0);
/*uint8_t output;
output = SPI0_D;*/
return SPI0_D;
}

Tags (1)
0 Kudos
1 Solution
769 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello Huy Nguyen ,

I recommend you first run a demo code on your board, check whether it can work well, then compare the code.

For example , there is some example: create a Processor Expert project on CW, then add SPI component, choose

"Typical Usage" under "Help on component ":

pastedImage_1.png

pastedImage_2.png

pastedImage_3.png

pastedImage_4.png

Hope it helps

Alice

View solution in original post

0 Kudos
6 Replies
769 Views
huyrua291996
Contributor I

Yeah, thanks Alice

I found my problem. Just because the first command I didnt read the response so the result was shift to the right any bytes so I always read 0x00 instead of the right response

So now, I fix this

Thanks 

0 Kudos
769 Views
Fan_xy
Contributor III

Hello  Huy Nguyen,

I also have the same problem as yours , now I still don't  know where is wrong.

You said didn't read the response, while how about this code :

while ((SPI0_S & SPI_S_SPRF_MASK) == 0);
/*uint8_t output;
output = SPI0_D;*/

Could you please share with your fixed code .

Thank you very much!

0 Kudos
769 Views
huyrua291996
Contributor I

Hi,

- First, you should check the mode (0,1,2,3 which is supported)

- Check by oscilloscope 

- My code is used to communicate with external flash (w25q16), example:

   * Wrong code

      CS_CLEAR;
      uint8_t temp = 0;
      SPI_SendChar(W25X_DeviceID);
      SPI_SendAndRecvChar(Dummy_Byte);
      SPI_SendAndRecvChar(Dummy_Byte);
      SPI_SendAndRecvChar(Dummy_Byte);
      temp = SPI_SendAndRecvChar(Dummy_Byte);
      CS_SET;

* Right Code

CS_CLEAR;
      uint8_t temp = 0;
      SPI_SendAndRecvChar(W25X_DeviceID);
      SPI_SendAndRecvChar(Dummy_Byte);
      SPI_SendAndRecvChar(Dummy_Byte);
     SPI_SendAndRecvChar(Dummy_Byte);
      temp = SPI_SendAndRecvChar(Dummy_Byte);
      CS_SET;

The mistake is the SPI data return wrong (the data is shift to the right one byte so I always see the data is 0x00) so when I fix this code, the data is true

Hope this help

0 Kudos
770 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello Huy Nguyen ,

I recommend you first run a demo code on your board, check whether it can work well, then compare the code.

For example , there is some example: create a Processor Expert project on CW, then add SPI component, choose

"Typical Usage" under "Help on component ":

pastedImage_1.png

pastedImage_2.png

pastedImage_3.png

pastedImage_4.png

Hope it helps

Alice

0 Kudos
769 Views
huyrua291996
Contributor I

Hi @Alice_Yang, 

I used my code then I checked by oscilloscope

It had data on MISO.(0xEF4015)

But when I debug, the result is 0. 

What's wrong with my function SPI_SendAndReceive(uint8_t input);

This is the function I copy in example code of KE02 that I download in NXP website

0 Kudos
769 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hi ,

 - How about the MOIS ?    What is your slave ?

 - What about you whole project ? And what about the demo code you refer to .

- Please send your project to me , I will debug it on my side.

BR

Alice

0 Kudos