+ Post New Thread
Results 1 to 16 of 16
  1. #1
    Full Member level 4
    Points: 2,317, Level: 11
    Achievements:
    7 years registered

    Join Date
    May 2012
    Posts
    223
    Helped
    10 / 10
    Points
    2,317
    Level
    11

    clarification on the SPI communication

    I read the following statement in the datasheet of the SPI family dspic33ev family and it is creating so much confusion. The statement reads like this

    If the user application does not change the data in the SPIxBUF register, every new transmission shifts the SPIxBUF register value instead of shifting the value received in the Shift register.

    What does it mean? I cannot send a constant value like 0x02 continuously do i need to keep changing or toggling the data? Please help.

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 76,403, Level: 67
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,489
    Helped
    3525 / 3525
    Points
    76,403
    Level
    67

    Re: clarification on the SPI communication

    Hi,

    I assume (I really don't know)
    * there are two hardware (shift) registers sharing the same address (or bus)
    * "reading" accesses the receive shift register
    * "writing" accesses the transmit shift register

    Imagine you want to send out "0x02"
    With an SPI transfer "transmit" and "receive" takes place at the same time.
    Therefore during "transmit" the receive register is updated, too. (Let's say 0xAA is received)
    Reading the shift register contents will give 0xAA, but since there are two physical registers 0xAA is not shifted out as "transmit" data.

    Your text doesn't say what data is transmitted...but I assume it's 0x02 again

    Klaus



  3. #3
    Full Member level 4
    Points: 2,317, Level: 11
    Achievements:
    7 years registered

    Join Date
    May 2012
    Posts
    223
    Helped
    10 / 10
    Points
    2,317
    Level
    11

    Re: clarification on the SPI communication

    Ok i will come up with the program in some time. Request to please review that code.



  4. #4
    Super Moderator
    Points: 258,568, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,147
    Helped
    13724 / 13724
    Points
    258,568
    Level
    100

    Re: clarification on the SPI communication

    You should always specify a quoted document unequivocally. I'm unable to locate the statement in Microchip data sheets.

    I guess the statement is related to slave mode where transmit is triggered externally, not by writing to SPIxBUF. Only in this case, unintended retransmission can occur.

    Are you asking about slave mode? Otherwise the statement is irrelevant.



    •   AltAdvertisement

        
       

  5. #5
    Full Member level 4
    Points: 2,317, Level: 11
    Achievements:
    7 years registered

    Join Date
    May 2012
    Posts
    223
    Helped
    10 / 10
    Points
    2,317
    Level
    11

    Re: clarification on the SPI communication

    No it is master mode. Please find the document attached. Page number is 13 and note 3.



  6. #6
    Super Moderator
    Points: 258,568, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,147
    Helped
    13724 / 13724
    Points
    258,568
    Level
    100

    Re: clarification on the SPI communication

    Thanks. The correct document reference is "dsPIC33/PIC24 Family Reference Manual", section SPI.

    The statement is a clarification about the operation of SPIxBUF SFR which actually represents two physical registers. I believe that it doesn't has much relevance in master mode. You are starting a transmission by writing to SPIxBUF, no matter if the data is constant or changing.



    •   AltAdvertisement

        
       

  7. #7
    Full Member level 4
    Points: 2,317, Level: 11
    Achievements:
    7 years registered

    Join Date
    May 2012
    Posts
    223
    Helped
    10 / 10
    Points
    2,317
    Level
    11

    Re: clarification on the SPI communication

    Ok understood. Thank you.



  8. #8
    Super Moderator
    Points: 76,403, Level: 67
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,489
    Helped
    3525 / 3525
    Points
    76,403
    Level
    67

    Re: clarification on the SPI communication

    Hi,
    Your text doesn't say what data is transmitted...but I assume it's 0x02 again
    ...yes, it's 0x02...

    In my eyes the note just says "SPIxTXB" is a buffer that it keeps it's contents...the same data is transmitted repeatedly if not changed by the user"

    Klaus



  9. #9
    Full Member level 4
    Points: 2,317, Level: 11
    Achievements:
    7 years registered

    Join Date
    May 2012
    Posts
    223
    Helped
    10 / 10
    Points
    2,317
    Level
    11

    Re: clarification on the SPI communication

    Sorry understanding problem.



  10. #10
    Super Moderator
    Points: 258,568, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,147
    Helped
    13724 / 13724
    Points
    258,568
    Level
    100

    Re: clarification on the SPI communication

    the same data is transmitted repeatedly if not changed by the user
    Yes, but it's effectively only possible in slave mode. You can't transmit in master mode without rewriting SPIxBUF.



  11. #11
    Full Member level 4
    Points: 2,317, Level: 11
    Achievements:
    7 years registered

    Join Date
    May 2012
    Posts
    223
    Helped
    10 / 10
    Points
    2,317
    Level
    11

    Re: clarification on the SPI communication

    Finally I have written the program. Request to please review for mistake, i have made it little bit detailed just in case it is not working and i can make changes to the code and for clarity.

    - - - Updated - - -

    I think there is a mistake in the code. Will this correct it?
    Code:
    void TransmitReceiveSpiData(uint16_t *a_tx_data_u16, uint16_t *a_rx_data_u16 )
    {
        while(!SPI1STATbits.SPITBF); /* Wait till the spi1 txb buffer is empty to load the data */
         SPI1BUF = *a_tx_data_u16; /* Load the data into the SPI1 buf to start transmission */
        /* immediately start reading the data it is mandatory else overflow will happen */
        /* read only when the rxb buffer is filled with data */
        while(!SPI1STATbits.SPIRBF);
         *a_rx_data_u16 = SPI1BUF;   
            
    }



  12. #12
    Full Member level 4
    Points: 2,317, Level: 11
    Achievements:
    7 years registered

    Join Date
    May 2012
    Posts
    223
    Helped
    10 / 10
    Points
    2,317
    Level
    11

    Re: clarification on the SPI communication

    Somehow the code is not working. I am testing with Beagle by total phase bus analyzer https://www.totalphase.com/protocols...gKJhoC5irw_wcB. I am not sure why it is not working it is sending some invalid data. One clarification i require is dspic33 family works only in DMA mode? Right now i have not enabled DMA mode. Please suggest.



  13. #13
    Super Moderator
    Points: 76,403, Level: 67
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,489
    Helped
    3525 / 3525
    Points
    76,403
    Level
    67

    Re: clarification on the SPI communication

    Hi,

    Where is your detailed error description?
    "Code is not working" tells nothing.

    Klaus



  14. #14
    Full Member level 4
    Points: 2,317, Level: 11
    Achievements:
    7 years registered

    Join Date
    May 2012
    Posts
    223
    Helped
    10 / 10
    Points
    2,317
    Level
    11

    Re: clarification on the SPI communication

    Sorry for not being clear as i really could not understand what data was coming on bus analyzer as the data was constantly changing but i was sending constant data. I changed some settings in the analyzer from mode 0 to mode 1 then it seems to receive the data properly. But in between i am seeing some 2 to 3 error frames not sure why? Do i need to look into it or can i leave it?



  15. #15
    Super Moderator
    Points: 76,403, Level: 67
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,489
    Helped
    3525 / 3525
    Points
    76,403
    Level
    67

    Re: clarification on the SPI communication

    Hi,

    Why not simply showing us the analyzer output?

    Maybe there is one member that can see what is going wrong (or not).
    But remember to post ALL necessary informations at once. Code, analyzer output, your description, maybe hardware....

    Klaus



    •   AltAdvertisement

        
       

  16. #16
    Advanced Member level 4
    Points: 7,740, Level: 20

    Join Date
    Jan 2015
    Posts
    1,086
    Helped
    345 / 345
    Points
    7,740
    Level
    20

    Re: clarification on the SPI communication

    Quote Originally Posted by electronicsman View Post
    No it is master mode. Please find the document attached. Page number is 13 and note 3.
    Please be VERY CAREFUL when reading FRM sections that they do actually relate to the SPECIFIC MCU that you are using. As it says in that document (in the note at the tip of Page 2:
    Depending on the device variant, this manual section may not apply to all dsPIC33/PIC24 devices.
    Therefore it would help f you identify the device you are using completely.
    (Having said that, the way that the SPIxBUF register works is pretty much the same across most device variants with that you read NOT being what you wrote, and needing to be very careful when using the debugger or 'watch' capabilities on this register.)
    Susan



--[[ ]]--