3

I am using the MSSP module of a PIC 18F25K22 (datasheet) for SPI. This works successfully except for when I try to use the maximum SPI clock rate of 16Mhz.

The target device is an SD card and the symptoms described below happen with a selection of different cards, tested with 6 cards from different manufacturers.

The microcontroller clock is the internal 16Mhz oscillator with 4x PLL, setup as shown below.

// Internal 64Mhz oscillator
IRCF2 = 1;
IRCF1 = 1;
IRCF0 = 1;
PLLEN = 1;

Circuit diagram

The relevant portion of the circuit is shown below, connections go directly to the PIC (note I used 47k resistors when assembling the PCB).

Circuit diagram

There are some right angles on the PCB where I broke out the SPI pins for debugging. On the top layer the traces go to the debug header, on the bottom to the PIC. I include this in case signal integrity might be relevant.

PCB top layer

PCB bottom layer

What works

Any speed below the theoretical maximum works successfully.

The following setup should produce an 8Mhz SPI clock from the formula 64000000 / (4 * 2).

// SPI clock = Fosc / (4 * (SSPADD + 1))
SSPCON1bits.SSPM = 0b1010; 
SSP1ADD = 1;
SSPSTATbits.CKE = 1; // Work around known silicon bug, see errata
SSPCON1bits.SSPEN = 1;

Data is written using the most basic method, no interrupts are used:

unsigned char spi_byte(uint8_t data) {
    SSPBUF = data;
    // Wait for the buffer to fill with incoming data
    while (!SSPSTATbits.BF);

    data = SSPBUF;
    return data;
}

When tested with a logic analyser there's a decent 8Mhz clock and my code works fine:

enter image description here

What doesn't work

I'd like to use the fastest clock possible, the datasheet shows two ways to do this:

enter image description here

I should be able to:

  • Set the clock rate directly to Fosc / 4
  • Set SSPxADD = 0 for the same effect - Fosc / (4 * (SSPxADD + 1)) = Fosc / 4 * 1

With my 64Mhz clock this should give a theoretical SPI clock speed of 16Mhz from the MSSP module.

SD initialisation requires a slow speed, below this can be seen marked by the red "1".

Overview at highest speed

This phase is successful. Therefore I should be able to speed up, so I tested with SSP1ADD set to zero (this happens around marker "A1"). The faster clock should be present in the period marked by a red "2".

SSPCON1bits.SSPEN = 0;
SSP1ADD = 0; // BUG: Set to 1 to make this work, 0 gives unstable clock?
SSPCON1bits.SSPEN = 1; 

However this does not work and produces a very unstable clock of about 82Khz, shown below (this is a zoom of the period marked "2" above).

Zoom of unstable SPI clock at highest speed

The first three bytes here have a slightly faster clock but it's still only ~400Khz (far below 16Mhz) and the duty cycle is nowhere near 50%. Then the clock rate mysteriously changes to ~82Khz during the fourth byte and is more like a series of pulses than a clock.

The 18F25K22 does have slew rate control, which has an impact. The above traces were taken with the default settings, slew rate control enabled. When disabled the above settings give a SPI clock of 400Khz (which is more stable and does work), but is far slower what it should be.

Notes

  • There is nothing relevant in the Microchip errata (I have a revision 5 chip)
  • I work around the one MSSP bug which still affects revision 5 by setting CKE = 1, though this isn't relevant to the problem here.
  • I can take scope traces if required, but don't have it with me right now.
  • I found this unanswered question which is similar, but I can't switch to MSSP2.

Question

Does anybody know if I should be able to use an SPI clock of 16Mhz, or am I missing something from the datasheet?

David
  • 4,504
  • 2
  • 25
  • 44
  • If you could take some scopeshot (not logic analyzer) there's a slim chance that would help. But it seems to me this is not about external components, plus 16MHz is not that fast. – Vladimir Cravero Nov 08 '15 at 22:18
  • Unfortunately I'm travelling and won't have access to a scope for a few days, but I can do this later in the week. I do not think this is external components, I think it's likely an unusual problem with the MSSP baud rate generator OR I've missed something important in the datasheet. – David Nov 08 '15 at 22:32
  • "I should be able to: Set the clock rate directly to Fosc / 4" - did you try this, and if so what was the result? – Bruce Abbott Nov 08 '15 at 23:48
  • @BruceAbbott yes I also tried setting `SSP1M=0` and it either produces the same result as the alternative or occasionally locks the uC up (no further SPI activity, requires a reset). Because random lock ups are harder to debug I focussed on the alternative. Note the lock up is possibly `while (!SSPSTATbits.BF)` but I have not debugged this. – David Nov 08 '15 at 23:51

3 Answers3

2

LOOK AT THE SLRCONX REGISTER, WHERE X IS PORT A, B OR C. Default bits are set, which limits slew rate to 25nSec, too slow for 16MHz SCK! Reset the bits corresponding to the port and pins you are using for SPI clock and data and it will work great. I got bit the same way, found the solution on another forum.

Dave
  • 21
  • 2
  • This, indeed, solves the issue with 16MHz SPI. It's clearly a bug in the datasheet, they should've mentioned it. Thanks. – jackhab Sep 08 '20 at 11:18
1

Maybe this is unrelated, but in the PIC16F145X datasheet a fifth note is added for SSPM bits for SPI mode.

enter image description here

So in this other PIC you cannot use SSPADD with 0 and have Fosc/4 when using SSPM = 1010. I think the SPI module may have the same rules as in your PIC18F25K22, but somehow that may not be shown in the datasheet.

0

In the datasheet : Datasheet

you see that it begins count down, and when the value is 0 or 1 this is very difficult to do by the micro. In other datasheet(dspic30F3012) they said that clearly in I2C:

enter image description here

The use of 0 or 1 is illegal . so use greater PLL or greater crystal.

Zara Zara
  • 61
  • 1
  • 7
  • A similar note is in the datasheet image in the question, but this applies to I2C mode not SPI master mode. This also doesn't explain why `Fosc/4` mode doesn't work. – David Nov 11 '15 at 13:27
  • The first image is for SPI, how does the pic can begin counting down from zero ?? – Zara Zara Nov 11 '15 at 14:12
  • Whilst this is an interesting observation it is not supported by other documented restrictions in the datasheet. It doesn't explain why `Fosc/4` doesn't work, or why switching to MSSP2 module works for another individual. – David Nov 11 '15 at 14:16
  • And the answer to "how can you count down" could be the same as timers, they interrupt / act on underflow on decrement from zero. We simply cannot know because the datasheet does not specify this, nor does it tell us that zero is an illegal value for SPI master mode. – David Nov 11 '15 at 15:47
  • you can save time and try what I said about the increasing the frequency or the PLL factor . or you can wait another response , I wish that . – Zara Zara Nov 12 '15 at 08:40
  • You will be aware that 64Mhz is the fastest this part operates either from an internal or external clock source. Therefore I am already using it at the maximum possible speed. – David Nov 12 '15 at 13:17
  • Aha yes , I didn't pay attention about the maximum frequency. but I think that the problem is the reload value of baud rate. so you should change the pic to DSPIC or another with greater operating frequency. – Zara Zara Nov 13 '15 at 13:01