SPI : How to receive 2 bytes of data in KEA128

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

SPI : How to receive 2 bytes of data in KEA128

Jump to solution
2,004 Views
jashanm
Contributor III

hi all,

        i am working on SPI in KEA128, since the SPI0_D reg is 8 bits and my slave is sending me 16 bits of data. I am using the following code in KEA128 given below,

master: KEA128

            while(SPI_S_SPTEF_MASK){

                  if(SPI0_S & (1<<5))           //  checking for Tx buffer to be empty

                        SPI0_D = TX;              //  to send the data to slave

                  if(SPI0_S & SPI_S_SPRF_MASK){

                        RX = SPI0_D;              //  to receive data in master(KEA128)

                  }

             }

So how should i go through about this ??

Labels (1)
0 Kudos
1 Solution
1,378 Views
isaacavila
NXP Employee
NXP Employee

Hi Jashan,

I modified SPI.zip (In function that sends 16 bits data was missing to add return value).

It is necessary to analize how the slave needs data to be received. For example, If device needs 8-bits command data and then return 16 bits-data, transaction needs to be as follows:

8-bit Command, 16-bit Response.jpg

On the other hand, if device needs 16-bit command data and then returns 16-bit data, transaction needs to be as follows:

16-bit Command, 16-bit Response.jpg

In this case, if you are using the SPI0_Send_16Bits_Data() function, transaction may be as follows:

8-bit Command, 16-bit Response 2.jpg

As you can see, you must ensure transactions are done as slave's specifications.

Best Regards,

Isaac

View solution in original post

0 Kudos
12 Replies
1,378 Views
isaacavila
NXP Employee
NXP Employee

Hi Jashan

As you can send 8 bits on KEA's SPI module, you must send the first 8-bits and then, you need to send another 8-bits-dummy data, this is needed to produce SCK signal and device could send 16 bits back.

In this 16-bits transfer, it is necessary to drive low SS signal, so you must ensure your current configuration is SS controlled by GPIO instead SPI module, otherwise, SS signal will be driven high when an 8-bit transaction is completed.

I attach a basic SPI.c and SPI.h to do these.

I hope this can help

Regards,

Isaac

0 Kudos
1,378 Views
jashanm
Contributor III

Hi,

     i have added the .c and .h file in my project. SPI0_PCS is going low and high perfectly (as observed using multimeter) but still i couldnt receive the 16bit data. what might have gone wrong ??

0 Kudos
1,379 Views
isaacavila
NXP Employee
NXP Employee

Hi Jashan,

I modified SPI.zip (In function that sends 16 bits data was missing to add return value).

It is necessary to analize how the slave needs data to be received. For example, If device needs 8-bits command data and then return 16 bits-data, transaction needs to be as follows:

8-bit Command, 16-bit Response.jpg

On the other hand, if device needs 16-bit command data and then returns 16-bit data, transaction needs to be as follows:

16-bit Command, 16-bit Response.jpg

In this case, if you are using the SPI0_Send_16Bits_Data() function, transaction may be as follows:

8-bit Command, 16-bit Response 2.jpg

As you can see, you must ensure transactions are done as slave's specifications.

Best Regards,

Isaac

0 Kudos
1,378 Views
jashanm
Contributor III

hi Issac,

            I have followed the SPI files that you had sent me and then used in my project. Should i use RSTB_uc also for waking up the KITMC07XS6517ET (its an high side switch to wake that up) since i am getting 0xf004 value when i send a random 16bit from KEA128.

reagards,

Jashan M

0 Kudos
1,378 Views
isaacavila
NXP Employee
NXP Employee

Hi Jashan,

RSTB should be in high state (according to datasheet):

pastedImage_0.png

Regards,

Isaac

0 Kudos
1,378 Views
jashanm
Contributor III

hi,

    i tried it out, i found that when i debug the below code in CW i could see the data in the debug mode (in the SPI0_D register) but not reflected in the variable.

     UINT16 SPI_write_16bits_data(UINT8 MSB, UINT8 LSB){

         wdin ^= 0x08;                         

         MSB |= wdin;                            /* Refresh extreme switch wdog *********/

         SPI_write(MSB);

         SPI_write(LSB);        

         spi_data[1] = SPI_read();

         spi_data[0] = SPI_read();

         return spi_data;

     }

is this fine or should i edit something in this code ??

0 Kudos
1,378 Views
isaacavila
NXP Employee
NXP Employee

Hi Jashan,

You routine looks fine, (I hope SS signal to be driven low/high outside this function).

Remember SPI0_D register is the same for sending and receiving data so please be sure you are seeing the value from received byte.

Have you used a logic analyzer to see SPI transactions?

Regards,

Isaac

0 Kudos
1,378 Views
jashanm
Contributor III

Hi Isaac,

             yes i am driving SS signal low/high outside this routine. I will get back to you after using DSO. Thank you,

-Regards,

Jashan M

0 Kudos
1,378 Views
jashanm
Contributor III

hi Isaac,

          i could receive the data into my variable but the problem now is that i can read only last byte(LSB) from SPI0_D. I have some modifications in my code which is as follows.

          UINT16 SPI_write_16bits_data(UINT8 MSB, UINT8 LSB){

                   UINT16 final_data;

                   wdin ^= 0x08;                           /* 0000 x000 0000 0000 ****x=wdin bit used to toggle*****/               

                   MSB |= wdin;                            /* Refresh extreme switch wdog *******************************/

                   SPI_write(MSB);                       /* i dont receive this data ***************************************/

                   SPI_write(LSB);                        /* i receive data which i get from this write ******************/

                   spi_data[1] = SPI_read();

                   spi_data[0] = SPI_read();         /* this data will get to final_data *******************************/

                   final_data = spi_data[1];

                   final_data = final_data << 8;

                   final_data |= spi_data[0];

           return final_data;

           }

Regards,

Jashan M

0 Kudos
1,378 Views
jashanm
Contributor III

hi isaac,

          once i change the code to the below one i get both the data, is it fine doing this or not ??

          UINT16 SPI_write_16bits_data(UINT8 MSB, UINT8 LSB){

                   UINT16 final_data;

                   wdin ^= 0x08;                                       

                   MSB |= wdin;                          

                   SPI_write(MSB);      

                   spi_data[1] = SPI_read();                    /* i just swapped this with *******/

                   SPI_write(LSB);                                   /* this function *********************/

                   spi_data[0] = SPI_read();        

                   final_data = spi_data[1];

                   final_data = final_data << 8;

                   final_data |= spi_data[0];

           return final_data;

           }

Regards,

Jashan M

0 Kudos
1,378 Views
isaacavila
NXP Employee
NXP Employee

Hi Jashan,

Yes, In this case first received byte is MSB and second is LSB.

Remember that every time you send a data through MOSI pin, you are receiving another byte on MISO pin, that is why your program did not receive second byte (the first received byte was replaced by the second one.)

I think this final function can now receive and send 2 bytes for communicating with your slave device.

Regards,

Isaac

0 Kudos
1,378 Views
jashanm
Contributor III

hi Isaac,

            now i am able to receive 16 bit data from my KEA128, thanks for the help.

Regards,

Jashan M

0 Kudos