• AVR Freaks

AnsweredWhat is the exact use of SSPSTAT.BF in SPI master and slave

Page: < 12345.. > >> Showing page 2 of 6
Author
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:29:50 (permalink)
0
ur code works too
#21
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:32:14 (permalink)
+2 (2)
ubuntuman
what should i read if the slave didnt send anything to me

You still don't understand how SPI works.
Every single transfer is a simultaneous send and receive.
If there is no slave connected, then you will receive 0x00 or 0xFF, depending upon if the SDI pin is high or low.
 
 

why should i put all in one function ?

Because that is how SPI works.
There is no difference between a "read" and a "write", they both happen at the same time.
 

what if slave send me a byte without writing to her ? isnt this possible if i will program the slave pic  too 

It can't.
All transfers are initiated by the Master.
They only happen when the Master writes a byte to its SSPBUF register.
It is the slave's job to place the next byte it wants to send into its own SSPBUF register, but the transfer does not happen until the Master triggers it.
 
Once you grasp this concept, SPI is really simple.
 
#22
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:33:38 (permalink)
0
thank u so much i understand now 
#23
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:34:22 (permalink)
+1 (1)
you guys have a problem with that CS line grin

GENOVA :D :D ! GODO
#24
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:34:41 (permalink)
0
BF works the same in slave too ?
 
#25
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:37:07 (permalink)
0
and what if i write twice in the master without reading the buffer ? there is no overflow bit for that  in datasheet
 
#26
1and0
Access is Denied
  • Total Posts : 9316
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:49:30 (permalink)
+1 (1)
ubuntuman
what should i read if the slave didnt send anything to me 
why should i put all in one function ? what if slave send me a byte without writing to her ? isnt this possible if i will program the slave pic  too 

The only way to clear BF is by reading SSPBUF. In one function because SPI transmits and receives simultaneously. The slave is clocked by the master, which transmits a byte to the slave and the slave returns a byte.
 
In a nutshell, BF is set when transmit is completed and data is received.
#27
1and0
Access is Denied
  • Total Posts : 9316
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:51:19 (permalink)
+1 (1)
What happened to this thread just now??? After I posted, it seems a bunch of posts got disappeared!!!
Never mind.  I did not see there is a page 2 to this thread. Sorry.
 
post edited by 1and0 - 2017/11/16 14:52:54
#28
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:51:22 (permalink)
0
the question if 
what happen if the master continue writing byte  to SSPBUF without reading SSPBUF
#29
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:57:09 (permalink)
+1 (1)
ubuntuman
and what if i write twice in the master without reading the buffer ? there is no overflow bit for that  in datasheet

The WCOL bit is set if you try to write to SSPBUF while a previous transfer is still underway.
That cannot happen in correctly written Master code.
 
ubuntuman
BF works the same in slave too ?

It works the same in the sense that the flag is set straight after you have received something from the Master.
The slave must read that byte to clear BF, and immediately write the next byte it wants to send to the Master.
That must be done before the Master starts another transfer.
 
 
#30
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:58:38 (permalink)
+1 (1)
ubuntuman
the question if 
what happen if the master continue writing byte  to SSPBUF without reading SSPBUF

No error is set, but if you don't read SSPBUF, the BF will still be set, so the next time you try to transfer you won't know when it has finished because BF is already high.
 
 
#31
1and0
Access is Denied
  • Total Posts : 9316
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 15:10:04 (permalink)
+1 (1)
ubuntuman
the question if 
what happen if the master continue writing byte  to SSPBUF without reading SSPBUF

You will lose the data in the master if you don't read SSPBUF to clear BF between each transmission, as the previous transmission can still be underway.
#32
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 15:10:29 (permalink)
0
if i depend on interrupt PIR1.SSPIF == 1 , each time i will make a write operation and it is done it will be high then i clear it in software  tehn write without reading . so what happen here ? does the data be sent or no ? 
#33
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 15:13:55 (permalink)
+2 (2)
DarioG
you guys have a problem with that CS line 

Agreed. I didn't change how that worked (i.e. driven low at the start, and left low).
 
In a typical application, it should be driven low just before writing to SSPBUF, and raised straight after the transfer finishes.
If you only ever transfer 8 bits, it could be done inside the spi_xfer_byte() function.
If you need to transfer more than one byte in each "transaction" (e.g. to a 16 bit peripheral), then you'd drive CS low, call spi_xfer_byte() however many times you need to, then raise the CS pin again.
 
 
Edit (extra info)
Many people think "why don't I just leave it low? I only have one slave, so why toggle it?"
The reason is, the signal dropping also indicates to the slave which is the first clock pulse in each transfer. Without that, it is possible for the slave to get out of synch with the Master, which corrupts everything.
Asserting CS before the transfer, and de-asserting it after, keeps everything synchronised.
 
 
 
 
post edited by qhb - 2017/11/16 15:21:40
#34
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 15:17:47 (permalink)
+1 (1)
ubuntuman
if i depend on interrupt PIR1.SSPIF == 1 , each time i will make a write operation and it is done it will be high then i clear it in software  tehn write without reading . so what happen here ? does the data be sent or no ? 

If you use SSPIF instead of BF to detect end of transfer, then yes, I think you can get way without doing the read, but why bother?
You still have to manually clear SSPIF if you do it that way, so you're not gaining anything, and you still have to read SSPBUF in cases where you DO want to receive data from the slave.
 
In almost every instance, the function I put in the code I posted earlier will do exactly what you need.
You just have to work out what you need to do with the CS/SS pin, and you have all the code you need.
 
 
#35
1and0
Access is Denied
  • Total Posts : 9316
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 15:25:52 (permalink)
+1 (1)
As Qhb said, you are not gaining anything by using SSPIF which requires a cycle to clear SSPIF.  So why not use that cycle instead to read SSPBUF which you may use?!
 
#36
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11220
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 15:34:53 (permalink)
+2 (2)
Many people think "why don't I just leave it low? I only have one slave, so why toggle it?"
The reason is, the signal dropping also indicates to the slave which is the first clock pulse in each transfer.

 
Also, some SPI devices "do something" on the rising edge of CS, like latch their data to their output pins.
#37
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/17 04:28:50 (permalink)
0
guys , i will do the code s u say . i just here now just to understand 
i have a question
master write a byte to spi to slave , when exactly SSPIF turns to be 1 ?
#38
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/17 04:34:18 (permalink)
+1 (1)
ubuntuman
...
master write a byte to spi to slave , when exactly SSPIF turns to be 1 ?

Have a look in the PIC18F4620 datasheet, at:
"FIGURE 17-3: SPI MODE WAVEFORM (MASTER MODE)"
and you will see it happens as soon as the last data bit is transferred.
 
#39
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/17 04:38:51 (permalink)
0
qhb
ubuntuman
...
master write a byte to spi to slave , when exactly SSPIF turns to be 1 ?

Have a look in the PIC18F4620 datasheet, at:
"FIGURE 17-3: SPI MODE WAVEFORM (MASTER MODE)"
and you will see it happens as soon as the last data bit is transferred.
 


good so master write to slave a byte when the last bit of the byte is transferred SSPIF will be one  so if i make master to write another byte to slave as i understood from u if i did that without reading the master buffer first even if slave dont send the master anything , i must have data collision 
so how u say above that it could works without reading the buffer 
i am confused 
#40
Page: < 12345.. > >> Showing page 2 of 6
Jump to:
© 2019 APG vNext Commercial Version 4.5