Using GPIO as Chip Select pin for SPI

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

Using GPIO as Chip Select pin for SPI

7,229 Views
Amit_Kumar1
Senior Contributor II

Hi

I was wondering how efficient is it to use GPIO as Slave Select pin ?. When I wrote a code in which I am using a default Chip select pin and side by side using GPIO pin just to test what the difference is on probing both the pins, I found the following waveforms. The yellow(channel 1) of the oscilloscope shows the inbuilt CS pin. the Blue(channel 2) of the oscilloscope shows the GPIO pin.

cs.JPG.jpg

Here I am sending 6 bytes data so CS pin is going low 6 times in the first case it is going down in fixed interval but in later case CS pin is going to logic 0  for 6 times but not at fixed intervals. Below is the simple code which is in my infinite loop

for(;;)

{

CS_ClrVal(csp);

SM1_SendChar((read|ACC_X_H)<<8 |dc);

CS_SetVal(csp);

CS_ClrVal(csp);

SM1_SendChar((read|ACC_X_L)<<8 |dc);

CS_SetVal(csp);

CS_ClrVal(csp);

SM1_SendChar((read|ACC_Y_H)<<8 |dc);

CS_SetVal(csp);

CS_ClrVal(csp);

SM1_SendChar((read|ACC_Y_L)<<8 |dc);

CS_SetVal(csp);

CS_ClrVal(csp);

SM1_SendChar((read|ACC_Z_H)<<8 |dc);

CS_SetVal(csp);

CS_ClrVal(csp);

SM1_SendChar((read|ACC_Z_L)<<8 |dc);

CS_SetVal(csp);

}


The above test I did in FRDMK20D50M board where the SPI_CS is PTD0 and the external CS is PTE0

Actually in 1 of my project I am using KL05 uc in that there is only 1 dedicated CS pin, so in that case for adding multiple slave device I need to use GPIO. SO please help me out in this.


Regards

Amit Kumar

2 Replies

3,357 Views
martynhunt
NXP Employee
NXP Employee

Hi,

I think what is happening here is that there is no wait after the send function before toggling the GPIO.

If you add a delay, or check for TCF flag on K-Series, after this function and before the GPIO toggle it should help you. What can happen after you call the send function is that the core will exit the function and move on to the next one before the SPI module has shifted the whole byte out.Therefore, the GPIO will not be toggling at the same rate that the bytes are leaving the SPI module.

CS_ClrVal(csp);

SM1_SendChar((read|ACC_X_H)<<8 |dc);

//Insert delay or check for transmit complete here

CS_SetVal(csp);

Best regards,

Martyn

3,357 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Please be attention that GPIO is slow module, which exists delay when register was set or cleared. If customer want to use GPIO pin as SPI chip select, the GPIO pin toggle frequency should be slower than 2 MHz.

The key point your mentioned problem is not GPIO toggle frequency, please check if SPI communication interval time and SPI baud rate. Please add time delay between each  SPI communication and check again:

CS_ClrVal(csp);

SM1_SendChar((read|ACC_X_H)<<8 |dc);

CS_SetVal(csp);

Please add time delay here

CS_ClrVal(csp);

SM1_SendChar((read|ACC_X_L)<<8 |dc);

CS_SetVal(csp);

Wish it helps.


Have a great day,
Ma Hui

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