This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SPI: Send & Receive more than 16 Bits

Hi,

 

I am trying to transmit and receive more than 16 bits via SPI A.

After configuring the SPI A bus in 16 bit, 4-wire master mode with FIFO enabled and SPIFFCT.all = 0x00, I tried to send 48 bits within one CS frame. This works:

SpiaRegs.SPITXBUF = 0xFF;
SpiaRegs.SPITXBUF = 0x00;
SpiaRegs.SPITXBUF = 0xF0;

 

After the transmission I would like to receive the bits (tested by connecting MISO and MOSI).

Neither this:

SpiaRegs.SPITXBUF = 0xFF;
SpiaRegs.SPITXBUF = 0x00;
SpiaRegs.SPITXBUF = 0xF0;

RX1 = SpiaRegs.SPIRXBUF;
RX2 = SpiaRegs.SPIRXBUF;
RX3 = SpiaRegs.SPIRXBUF;

nor this:

SpiaRegs.SPITXBUF = 0xFF;
RX1 = SpiaRegs.SPIRXBUF;

SpiaRegs.SPITXBUF = 0x00;
RX2 = SpiaRegs.SPIRXBUF;

SpiaRegs.SPITXBUF = 0xF0;
RX3 = SpiaRegs.SPIRXBUF;

could deliver the sent bit sequence.

How can I implement this ? Transmit and receive of 48 bits within one CS frame.

Thank you ! 

- Brian

 

  • Brian,

    The CS (chip-select or SPISTE) will go active at the start of the frame and inactive at the end of the frame, with the frame size defined by a maximum character length of 16-bits.

    If you only have the (2) SPI devices connected then you can remove the CS line from the equation and leave it active, then you can use the FIFOs to send multiple frames, in your example it is (3) frames.

    If you have more than the (2) SPI devices on a SPI bus, then you could manage the CS with a GPIO and SW to set and reset the CS to the specific device.

    Jeff
  • Hi Jeff,

    thank you for the answer.

    If I do the following:

    SpiaRegs.SPITXBUF = 0xFF00;
    SpiaRegs.SPITXBUF = 0xF0F0;
    SpiaRegs.SPITXBUF = 0xFFFF;
    
    RX1 = SpiaRegs.SPIRXBUF;
    RX2 = SpiaRegs.SPIRXBUF;
    RX3 = SpiaRegs.SPIRXBUF;

    The transmit works as expected: The SPISTE (CS) goes low at the start of the transmission and back high at the end (after 48 transmitted bits).

    Analyzing the RX1-3, the content is not 100% right:

    RX1: 1111111110000000

    RX2: 0111100001111000

    RX3: 0111111111111111

    There is a shift of 1 bit but why ? I am still using the same SPI Interface (on a F28035) with a hardware loopback (MISO -> MOSI).

    - Brian

  • It's weird. Just sending & receiving 16 bits:

    Transmit: SpiaRegs.SPITXBUF = 0b0000000000000001;

    Receive:                                                 0b1000000000000000;

     

    Transmit: SpiaRegs.SPITXBUF = 0b0000000000000011;

    Receive:                                                 0b1000000000000001;

     

    Transmit: SpiaRegs.SPITXBUF = 0b0000000000000111;

    Receive:                                                 0b1000000000000011;

     

    Any ideas ?

    - Brian

    PS: It works with Software Loopback (SPILBK in SPICCR). What's wrong with the hardware loop (wire 4 cm) ? 

  • Brian,

    Interesting results - good details.

    Does this occur at even the slowest baud rate?
    Are you initializing the SPI as done in the example code?

    Jeff
  • Jeff Stafford said:
    Does this occur at even the slowest baud rate?

    Thanks for the advice. I changed SPIBRR from 0 to 127 and I am able to receive 16 bits correctly.

    What is the proper way to receive 48 bits ? Doing

    SpiaRegs.SPITXBUF = 0xFF00;
    SpiaRegs.SPITXBUF = 0xF0F0;
    SpiaRegs.SPITXBUF = 0xFFFF;
    
    RX1 = SpiaRegs.SPIRXBUF;
    RX2 = SpiaRegs.SPIRXBUF;
    RX3 = SpiaRegs.SPIRXBUF;

    gives me back 0xFFFF for RX1-3 :/ How to read the information from the RX FIFOs ?

    - Brian

  • Brian,

    Based on a previous msg I thought you were receiving different values for RX1-3:

    Analyzing the RX1-3, the content is not 100% right:

    RX1: 1111111110000000

    RX2: 0111100001111000

    RX3: 0111111111111111

    What changed?  

    Reading the SPIRXBUF is the correctly location for reading from the SPI RX FIFO.

    Jeff

  • Hi Jeff,

    in the post you refer to I just sent 16 bits and read the SPIRXBUF after that. My goal is to receive 48 bits.

    If I do

    SpiaRegs.SPITXBUF = 0xF0F0;
    RX1 = SpiaRegs.SPIRXBUF;
    
    SpiaRegs.SPITXBUF = 0xFF00;
    RX2 = SpiaRegs.SPIRXBUF;
    
    SpiaRegs.SPITXBUF = 0xFFFF;
    RX3 = SpiaRegs.SPIRXBUF;
    

    I get 0xFFFF in all RX variables (CCS expressions). Depending on the refresh interval, I can see other values (0xF0F0, 0xFF00), but 95% of the time all RX variables are 0xFFFF. If I insert breakpoints in the SPITXBUF lines, the assignment works better (except the order):

    RX1 = 0xFFFF

    RX2 = 0xF0F0

    RX3 = 0xFF00

    Do I need to wait for a flag or something ?

     

    - Brian 

  • I could solve the problem.

    I do a RXFIFORESET, then send the 48 bits via SPITXBUF. After that, I wait until TXFFST != 0 (no more data to transmit) and finally copy the data from SPIRXBUF to RX1-3.

    The TXFFST helped to get constant variables. With the RXFIFO pointer reset the order (RX1-3) was corrected.

    I am able to get up to 8.5 MHz of SPI CLK without the mentioned bit shift with hardware loopback.

    Thanks Jeff for your support.

    - Brian