Announcement

Collapse
No announcement yet.

MSSP module query.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • MSSP module query.

    Hi,
    I'm using the MSSP module of a 18F25K20 to communicate with a SST25 series flash memory device. The PIC is running at 64MHz and the SPI clock at 4Mhz for now.

    I've had a problem which I have fixed or rather worked around but that I don't understand and I'm afraid my fix/workaround may come back and bite me further on down the road. Here goes....

    If I run the following code the PIC outputs the signals shown in the first screenshot.
    Code:
    SST_Cmd_EWSR                CON $50     ' Enable status register write
    SST_Cmd_WRSR                CON $01     ' Write status register
    
    SST_Data  VAR   BYTE
    SST_Data = 170
    
    SST_CE = 0                        ' Chip Enable
    SSPBUF = SST_Cmd_EWSR
    GOSUB WaitForSPI
    
    SST_CE = 1                       ' Flip CE to execute
    @ NOP
    SST_CE = 0
    
    SSPBUF = SST_Cmd_WRSR 
    GOSUB WaitForSPI 
    
    SSPBUF = 170
    GOSUB WaitForSPI
    SST_CE = 1
    
    
    WaitForSPI:
    WHILE SSPSTAT.0 = 0 : WEND
    RETURN
    Click image for larger version

Name:	3-Working_analyzer.jpg
Views:	1
Size:	19.7 KB
ID:	5410

    Everything above is as expected and it works (no it doesn't fall thru into the subroutine the real application). However, if I instead write the variable SST_Data (containing the value 170) the following occurs:

    Click image for larger version

Name:	4-NonWorking_analyzer.jpg
Views:	1
Size:	19.3 KB
ID:	5411

    As can be seen here the PIC thinks the MSSP module is finished and deselects the device in the middle of the operation.

    The PIC datasheet is pretty clear on that if valid data is to be expected from the slave device a dummy read of SSPBUF should be performed before writing to it. I'm not actually reading any data from the device in this particular case but doing this dummy read of SSPBUF is what fixed my problem and allowed me to "send" variables as well as constants.

    But why does it make a difference if the value being written to SSPBUF is comming from a constant rather than a variable? Can I safely NOT do the dummy read when writing constants and ONLY do it when writing variables to SSPBUF or have I just been lucky the hundred or so times I've captured the behaviour during debugging?

    Please, someone shed some light on what's going on and why.

    /Henrik.

    PS. I've posted on the same issue on the user forum and although I did get a reply it doesn't really explain the whats and whys of this - I'd like to understand what's going on here.

  • #2
    Hi Henrik,

    When Transmitting, you should use the interrupt flag to determine when it's finished, not the Buffer Full status bit.

    Code:
    SSPIF    VAR  PIR1.3
    
    WaitForSPI:
        SSPIF = 0
        WHILE SSPIF = 0 : WEND
    RETURN
    The BF - Buffer Full Status bit is for Receiving only and requires that you read the SSPBUF register to clear the BF bit.

    The BF bit can also be cleared by writing to the SSPBUF register, but the difference is in the way PBP does the write.

    With a constant, it will use movlw and movwf instructions, whereas a byte variable will use the movff instruction. movff doesn't clear status bits like the movwf instruction does.
    PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
    Never download a PIC datasheet from anywhere but microchip.com

    Comment


    • #3
      Thanks Darrel, that explains it!

      So, when the PIC is acting as the master, which is the case here, can I use SSPIF instead of BF for all transactions, both when I perform a write in which the slave doesn't clock any data out AND when I'm actually clocking in data.

      For example, reading a byte from the memory device involves sending four bytes during which the slave device doesn't output anything on the MISO line, then comes a fifth byte where the actual data to be read is clocked out of the slave device.

      Is it safe to use SSPIF for all five bytes in the above scenario or or should I poll SSPIF for the first four, outgoing, bytes and then BF for the fifth, incomming, byte

      Thanks again Darrel, much appreciated as always!
      /Henrik.

      Comment


      • #4
        Sure, you can do everything with the SSPIF flag.

        I think the Buffer Full bit is more usefull in a slave application where you aren't controlling the SPI CLK, and data could arrive at any time. SPI is so fast that you may need to receive multiple bytes in a single interrupt.

        In master mode, since the MSSP is full-duplex, it receives at the same time that it transmits, so SSPIF and BF always happen at the same time.
        But clearing the BF bit can be problematic, so SSPIF is an easier choice.
        PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
        Never download a PIC datasheet from anywhere but microchip.com

        Comment

        Working...
        X