SPI Reading a Register, How does it happen?

Thread Starter

joelindo

Joined Apr 21, 2017
2
When sending either a read or write request to the slave, the slave is simultaneously sending data back to the master. If that request is asking for data in a register, does the slave reply immediately, or is another command required? For the RFM69, the difference between write and read is the MSB of the address. If it's a 1, it's a write request. If it's a 0, it's a read. I'm thinking, since it's an MSB first transaction, the slave knows it needs to respond and does so with the data stored at the requested address, so the slave provides the requested information during the initial transaction, but I'm not sure.
 

Papabravo

Joined Feb 24, 2006
21,225
Every SPI transfer is a simultaneous WRITE and READ. Sometimes the READ data is meaningless and you can throw it away. Sometimes the WRITE data is arbitrary and is ignored by peripheral device. This happens on a bit by bit basis. For every bit that is clocked out there is a corresponding bit that is clocked in.
 

jpanhalt

Joined Jan 18, 2008
11,087
I assume you are not writing Assembly, but here is an example with that language:

Code:
PutAddr                       ;call from any bank, returns |B0
     movlb     2              ;                                                 |B2                                 
     bcf       TC_CS          ;                                                 |B2
     movlb     4              ;                                                 |B4
     movwf     SSPBUF         ;                                                 |B4
     btfss     SSPSTAT,BF     ;                                                 |B4
     bra       $-1            ;                                                 |B4
     return
GetReg
     clrf      SSPBUF         ;if read not necessary, then just clrf            |B4
     btfss     SSPSTAT,BF     ;                                                 |B4
     bra       $-1            ;                                                 |B4
     movf      SSPBUF,w       ;move fisrt read byte to WREG                     |B4
     return                   ;NB:returns in|B4
Thanslated, you set (write) the address you want to read (or write) and ignore the response. Then do a read command with a nonsense value (like 0) and keep what's returned. If you want to write to that register, then you send something meaningful. Of course, the register has to allow writing to it, e.g.,

Code:
PutReg                        ;enter w/ value to be written in WREG
     movwf     SSPBUF         ;                                                 |B4
     btfss     SSPSTAT,BF     ;                                                 |B4
     bra       $-1            ;                                                 |B4
     movlb     2              ;                                                 |B2
     bsf       TC_CS          ;                                                 |B2
     movlb     0              ;                                                 |B0
     return
EDIT: Some devices have a pin or bit that you set or clear to indicate whether it is a write or read; others allow the bytes to be written or read sequentially. The code above is for a project that has two slaves. One does not require that, while the other does.
 
Last edited:

MrChips

Joined Oct 2, 2009
30,795
As PB says, every SPI transfer is a simultaneous WRITE and READ.
The data read back must have been acquired before the SPI transaction began. Therefore the data received could not have been in response to the current WRITE request. Some systems respond by sending the current status condition prior to the SPI transaction. In general, a request for data will receive the corresponding data on the next SPI transfer or the data is sent using multi-byte SPI transfer.
 

Thread Starter

joelindo

Joined Apr 21, 2017
2
I've made some progress on this, thanks to all of you. I can now read and write the RFM69 registers like a pro. However, I'm still stuck. The introduction-to-the-rfm69hw-transceiver is a good start, but all that complicated software doesn't help me much understand the basics. I'm more interested in how it works rather than making it work. . . My problem now is, I can't get anything out of the antenna. I have an SDR that seems useful as a spectrum analyzer. I think it's working because I can see other signals in the range, but not mine. My code is attached. What am I missing?
 

Attachments

Top