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 ??
已解决! 转到解答。
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:
On the other hand, if device needs 16-bit command data and then returns 16-bit data, transaction needs to be as follows:
In this case, if you are using the SPI0_Send_16Bits_Data() function, transaction may be as follows:
As you can see, you must ensure transactions are done as slave's specifications.
Best Regards,
Isaac
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
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 ??
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:
On the other hand, if device needs 16-bit command data and then returns 16-bit data, transaction needs to be as follows:
In this case, if you are using the SPI0_Send_16Bits_Data() function, transaction may be as follows:
As you can see, you must ensure transactions are done as slave's specifications.
Best Regards,
Isaac
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
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 ??
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
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
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
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