PDA

View Full Version : Problem with MSSP module.



HenrikOlsson
- 30th December 2011, 14:55
Hi,
I'm working with a SPI flash memory device (SST25VF080B) and I'm having some difficulties with MSSP module.

I'm trying to write to the status register of the device and this is the subroutine I'm using for that purpose:

SST_WriteStatus:
PORTB.7 = 1 ' Logic analyzer trig
PAUSEUS 5

' First we execute the EWSR instruction to enable writing to the status register.
SST_CE = 0

SSPBUF = SST_Cmd_EWSR
GOSUB SST_WaitForSPI

SST_CE = 1

' Then we execute the WRSR command to actually write the new value to the statusregister.
SST_CE = 0

SSPBUF = SST_Cmd_WRSR
GOSUB SST_WaitForSPI

SST_Status = 101010
'SSPBUF = SST_Status ' Thru variable
SSPBUF = 101010 ' Hardcoded
GOSUB SST_WaitForSPI

SST_CE = 1

PORTB.7 = 0
RETURN

The subroutine SST_WaitForSPI consists of the following code:

SST_WaitForSPI:
While SSPSTAT.0 = 0 : WEND
RETURN

I've marked the lines of interest in red in the code above. Here's what's happening:

When executing the code as it's shown above, ie writing the value 170 (101010) directly to the SSPBUF register everything works as expected which is shown in the following screenshot:
6206

As you can see here the ENABLE line is pulled low then the first command (80) in sent. Then there's a pulse on the ENABLE line after which a new command (1) and finally the data (170) is sent. When the last bit is out the ENABLE line goes high. This is all as expected and it works.

The problem comes when instead of writing 170 directly to SSPBUF I assign it to the variable SST_Status and then write THAT to SSPBUF, what's happening then can be seen here:
6207

Here, for whatever reason the ENABLE line goes high BEFORE the data is completely sent. This obviosuly makes the write invalind and not accepted by the device. I admit defeat - I have no idea what's going on.

The SSPSTAT.0 which I poll in the WaitForSPI subroutine indicates when 8 bits have been shifted IN from the slave device. In this particullar case nothing is, or should be comming in, the MISO-line is idling high. But why does it make a difference if I write a constant or a variable to the SSPBUF register? Why does it think it's received 8 bits? The MISO-line idles high in both cases and there are, as far as I can see, no other difference than SSPBUF being given a value either literally or thru a variable.

I've tried other values and it behaves exactly the same - when given the value as a constant it works but "thru" a variable it doesn't.

Any ideas?
/Henrik.


EDIT: And again the bloody editor is messing with the code - it's getting REALLY frustrating posting here. The binary values in the code should be 101010 (170) but the editor apparently decides something else for me and I can't change it.

EDIT2: I can't even write it in the edit comment above it's [Percent sign] [one] [zero] [one] [zero] [one] [zero] [one] [zero].

HenrikOlsson
- 30th December 2011, 15:08
Forgot to mention: 18F25K20 and PBP 3.0.4.4
Should've editied the post well....you know....

HenrikOlsson
- 30th December 2011, 17:13
Hi,
OK, it LOOKS like a dummy read of SSPBUF before writing the variable to it fixes the problem. The datasheet is fairly clear on this dummyread when expecting valid data - which I don't. And I still don't understand why it makes a difference where the actual value written to SSPBUF is comming from, ie. constant or variable.

/Henrik.

Demon
- 2nd January 2012, 15:50
Totally off the top of my head; maybe the dummy read clears a buffer?

And this buffer doesn't handle a constant and variable the same way (allocation of space, bit VS byte, etc).

Robert


EDIT: Looking over your code again and I just thought of something after seeing the PAUSE. Does the time it takes PBP to access the value stored in a variable interfere with the MSSP module?

Bear in mind that I have absolutely no clue what a MSSP module eats in wintertime.


EDIT SOME MORE: Forgot to ask, how are these variables defined?

SST_Status
SSPBUF (looks like a BYTE in MSSP section of 18f25k20 datasheet)