Page 1 of 1

Strange SPI behavior on STM32

Posted: Mon Jul 23, 2012 5:00 pm
by barthess
Hello guys.
I am trying to read 64 inputs using chain of 8 74HC165 shift registers via spi like described in this article http://www.avrfreaks.net/index.php?name ... w=previous. Communication looks fine on oscilloscope (NSS, clock, MISO). But first received byte contains only 7 bits of data and 9-th byte contains last one bit. Using uint16_t data words produces similar results: first data word contains 15 bits of data and the last bit lays in 5-th word.
My spi config.

Code: Select all

static const SPIConfig in_spicfg = {
  NULL,
  GPIOB,
  GPIOB_SR_IN_NSS,
  SPI_CR1_CPOL | SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0
};

Code reading data:

Code: Select all

  spiAcquireBus(spip);
  spiStart(spip, &in_spicfg);
  spiSelect(spip);
  spiReceive(spip, n, rxbuf);
  spiUnselect(spip);
  spiReleaseBus(spip);

Yes, I can construct single uint64_t from 9 uint8_t, but what I do wrong?

Re: Strange SPI behavior on STM32

Posted: Mon Jul 23, 2012 5:28 pm
by Giovanni
Hi,

Probably it is an issue with the CPOL and CPHA settings, try the various combinations.

Giovanni

Re: Strange SPI behavior on STM32

Posted: Tue Jul 24, 2012 8:00 am
by barthess
I am pretty sure that CPOL CPHA is correct. The only suitable combination is CPOL=1, CPHA=0. This will cause following behavior:
1) the first edge is falling
2) MCU gets samples on falling edges
3) 74hc165 shifts on every raising edge
That looks correct on scope.
Later I removed serial resistor from clock line to make clock edges more sharp and change settings to CPOL=1, CPHA=1. This produces correct results but looks ugly on scope. Yellow - data, magenta - clock. This is the same signals with different scales. As you can see zero sampled in too narrow place.

Re: Strange SPI behavior on STM32

Posted: Tue Jul 24, 2012 8:32 am
by barthess

Re: Strange SPI behavior on STM32

Posted: Tue Jul 24, 2012 1:54 pm
by barthess
SPI makes me crazy. Look at the attached screenshot. Imagine that [8]-th value has 7 leading zeroes. Now shift last right column one row up and you will get correct results in first 8 bytes. Any ideas?

Re: Strange SPI behavior on STM32

Posted: Tue Jul 24, 2012 2:06 pm
by Giovanni
Is the 74HC165 PL input connect to the SPI CS output? if so the shift register is parallel loaded when the CS goes down but it must return to high before data can be clocked out (looking at the truth table).

If this is true then your code should become:

Code: Select all

      spiAcquireBus(spip);
      spiStart(spip, &in_spicfg);
      spiSelect(spip);
      /* delay? */
      spiUnselect(spip);
      /* delay? */
      spiReceive(spip, n, rxbuf);
      spiReleaseBus(spip);


Giovanni

Re: Strange SPI behavior on STM32

Posted: Tue Jul 24, 2012 2:17 pm
by barthess
PL pin drives by separate pin. It pulls down and goes up before CS pulls down.
Is it bug in SPI cell? When I set CPOL=0, CPHA=0 than spi works exactly as expected, but very first bit drops because it shifted out before MCU sapmles MISO.

Re: Strange SPI behavior on STM32

Posted: Tue Jul 24, 2012 2:39 pm
by Giovanni
I found an application note:

http://www.st.com/internet/com/TECHNICA ... 165404.pdf

The suggested connection does not even require the clock inhibit.

Giovanni

Re: Strange SPI behavior on STM32

Posted: Tue Jul 24, 2012 3:03 pm
by barthess
Yes, but it is for bitbanged protocol. Looks like CPOL=1, CPHA=0 mode badly tested by ST. Never mind, I will use software workaround.