3
\$\begingroup\$

I'm attempting to set up SPI communications on a breadboard between two PIC18LF2550 microcontrollers. I'm using a fairly slow clock of ~750kHz, waiting ~300us between bytes, and signal integrity on the scope looks good.

I'm using SPI mode 0,1 (i.e. with configuration bits on both the master and slave PICs set to CKP=0, CKE=0), with a slave select line. I have cleared the SMP bit on both master and slave (this is required for the slave), meaning that SDI is sampled in the middle of the data output time.

When I disconnect the slave entirely and loop back the master SDO into the master SDI, the communication works reliably. Furthermore, if I add a delay of 100ns or so (5% of the clock period) to either the clock or data lines, the connection is still reliable; this is expected, since the sampling is in the middle of the data output time. This, and the fact that all the lines (SDO/SDI/SCK/SS) match figure 19-3 of the datasheet (pg. 203) (with CKE=CKP=SMP=0) makes me think that the master is working correctly.

When the slave MCU is connected, the data sent from the slave to the master is always received reliably, but only about 50% of the bits sent from the master are received correctly at the slave. The incorrect data received are usually shifted one bit 'early', as though the slave is clocking the data at the very end of the clock period, and often reading data from the next bit instead. This hypothesis is supported by the fact that if the master SDO (slave SDI) line is delayed by a few nanoseconds, the data is 100% correct, whereas if the SCK line is delayed by a few ns instead, the data received is 100% incorrect.

Input sampling on the slave therefore seems to be occurring at the end of the data bit, but I would like it to occur in the middle of the data bit. However, the datasheet implies that, in slave mode, sampling can only occur in the middle of a data bit! (SMP must be cleared, as it is in my code). If I had set CKP or CKE wrongly, this might lead to sampling at the wrong time, but these are both set to 0 in both master and slave as required, and bits on the SDI/SDO lines are not half-a-bit out of sync, as might be expected if the clock polarity was set wrongly on one PIC.

Can anyone suggest what is going on?

\$\endgroup\$
  • \$\begingroup\$ It seems you have already diagnosed it yourself. The slave is set up to sample on the wrong edge of the clock. Change it to sample on the other edge. \$\endgroup\$ – Olin Lathrop Feb 11 '14 at 17:57
  • \$\begingroup\$ @OlinLathrop I will clarify the question; the problem is that when in slave mode the sampling should only ever occur in the middle of the data period (this is what is set in the code, and is the only setting that the PIC allows)... but the PIC seems to be sampling at the end of the data nonetheless. \$\endgroup\$ – Chris Johnson Feb 11 '14 at 18:06
  • 1
    \$\begingroup\$ There's a lot of SPI-related errata on that chip. microchip.com/mymicrochip/filehandler.aspx?ddocname=en544178 \$\endgroup\$ – Spehro Pefhany Feb 11 '14 at 18:33
  • \$\begingroup\$ @SpehroPefhany Thanks - I thought I had checked the errata, but item 37 looks a likely candidate: "If configured in SPI Slave mode, the MSSP may not successfully recognize data packets generated by an external master processor..." Work around is to insert a series resistor (presumably to slow down the transitions). Microchip helpfully note that "The required value for the resistor varies with the application system’s characteristics and the process variations between the microcontrollers. Experimentation and thorough testing are encouraged.".... \$\endgroup\$ – Chris Johnson Feb 11 '14 at 20:05
2
\$\begingroup\$

As Sphero Pefhany notes, the list of silicon errata for the PIC18F2455/2550/4455/4550 family is extensive. In particular, erratum 37 (applicable to three revisions, including my revision B5 chips) reads

If configured in SPI Slave mode, the MSSP may not successfully recognize data packets generated by an external master processor. This applies to all SPI Slave modes (CKE/CKP = 1 or 0), whether or not slave select is enabled (SSPM<3:0> = 010x).

Work around: Insert a series resistor between the SPI master Serial Data Out (SDO) and the corresponding SPI slave Serial Data In (SDI) input line of the microcontroller. The required value for the resistor varies with the application system’s characteristics and the process variations between the microcontrollers. Experimentation and thorough testing are encouraged.

I assume the purpose of this resistor is to slow down the data transition, to make sampling at the end of the clock period (more) reliable.

This bug has been fixed in the latest (B7) revision.

\$\endgroup\$
0
\$\begingroup\$

I assume the purpose of this resistor is to slow down the data transition, to make sampling at the end of the clock period (more) reliable.

The series resistor reduces ringing, and overshoot / undershoot. It doesn't significantly affect the edge placement.

Electrically you are creating a low-pass RC filter, with the capacitance supplied primarily by the input, and to a smaller extent by any stray layout and pin-to-pin capacitance.

As it filters-out the higher frequencies, the edges will be less sharp and more rounded. The key is to not add too much resistance to make the edge too slow / rounded.

Typically a low-value resistance is used, anywhere from 22 Ohms to 470 Ohms. I tend to use 100 Ohms or 220 Ohms, but the value used is operating frequency dependent.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.