CS pin in SPI (Slave)

Hi my friends :slightly_smiling_face: . I've made a connection between two arduinos based on SPI protocol and it works very well. (uno)
My question is how I can get a received data flag on the slave side? (I need to track every received data frame on the slave)
I tried to read the status of CS of slave device. (As you know, based on SPI, the CS will be high or low during a sending operation and then change.) but it doesn't work at all.
I also spent some time to get the status of SPIE and SPIF registers after receiving data and again nothing happend. (Maybe I am doing something wrong)
Can someone tell me what I should do?
or show me a relevant code . Thanks!

Maybe you have CS connected o the wrong pin

I just checked it and it is pin 10! Have you tried it before? it worked?

Is that the pin number you check in your code?

You said you already have it working?

So, whenever your slave Peripheral code has received some data, just have it set the flag!

Why? That's already integral to the operation of the slave Peripheral !

yes it is. but there is something which is really weird!
I just found that even without CS pin I can transfer data! My data is transfering only with MOSI and MISO pins.!!!!!! :rofl:
OMG!!! So what is the role of CS in arduino!
I need it because I want to use the slave to make a communication with FPGA!

That's how SPI works. If the slave doesn't need CS to function , the transfer will still occur.
The purpose of CS is to select individual chips (Chip Select) on the SPI bus if you have multiple slaves (Chips)

1. Check that your SPI wiring agrees with the diagram of Fig-1.


Figure-1:

2. When a data bytes from Master to Slave, the SPIF-bit/flag of SPSR Register becomes HIGH.

3. If you enable the LIE bit (Local Interrupt Enable) and GIE bit (Global Interrupt Enable), the SPIF-bit will force the MCU to jump to the following ISR() function.

ISR(SPI_STC_vect)
{
     //code to increment a globally declared byteCounter.
}

4. You can increment the byteCounter in the ISR() function every time a byte arrives from the Master. You can easily know how many bytes of data have arrived from Master over 5-sec time (for example).

5. You try to write some codes whatever it may and submit them here, which I believe will be moderated by the Forum Members.

Do you have pin 10 mode set as input on the slave?

It seems helpful! Thanks my friend.

So how are you doing your SPI?

The hardware SPI should depend on CS:

Why do you need an Arduino for that?
Surely, an FPGA can do SPI itself?!

Yes you are right. to be honnest I am new in arduino world :grin:
I am designing a SPI controller for FPGA. so my FPGA would be the master.
The reality is I don't want to burden all responsibilities on my FPGA side. my arduino also should follow some standards.
the CS pin is not only responsible for selecting the right slave but also stay 1 or 0 during a sending operation. Can be a difference between SPI protocols? :laughing:
I think as @GolamMostafa mentioned, I should enable some registers and use them in interrupt function.

There is only one protocol.
You just dont have your CS reading code set-up correctly.

Do you have the slave Uno CS pin set as input?
Are you checking it with digitalRead()?
Is the master actually setting the pin low?
Is pin 10 of the master connected to pin 10 of the slave?

An FPGA is vastly more powerful than an Arduino UNO - anything the UNO could do wouldn't be much of a "burden" to an FPGA!

Is it talking to multiple slaves Peripherals, or is the Arduino the only thing on the SPI?
If the Arduino is the only thing, then you really don't need CS.

That's the thing about SPI: unlike I2C, beyond the basic hardware connections, there is no defined protocol!

I want to send the data from FPGA to arduino. but I need to track each data frame one by one on the slave side. I can create something on FPGA side and use it for example as a flag.
But I don't want to do that intentionally!
For me the easiest way is using two FPGA boards. But I need to make a communication to arduino.

The following diagram might help you to enable the interrupt logic.

This library looks like it uses the SPI hardware - so that should include the CS pin:

A discussion on doing SPI Slave (in software) here:

Please, post your working sketches and I will show where to bring changes in the Slave sketch in order to count the number of data bytes arriving from Master.

Operation of the CS pin is independant of the hardware SPI, it doesnt happen automatically
You need to have code that sets it HIGH and LOW

When you include the following lines in the Arduino UNO sketch, the SS (Slave Select; Software Name; SS/ is the Hardware Name) becomes an output line with HIGH state and is normally connected with SS/-pin of Slave-UNO (Fig-1 of post #8) or CS/ (active low) line of a sensor.

#include<SPI.h>
SPI.begin();

Prior to initiate SPI transaction, the Master-UNO brings down the SS line at LOW state to select the SPI Port of the Slave. The code is:

digitalWrite(SS, LOW);  //SS/ hardware line of Master becomes LOW

The meanings of SS, MOSI, MISO, and SCK are known from the dictionary of SPI.h Library.