Strange SPI behavior on STM32

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Strange SPI behavior on STM32

Post 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?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Strange SPI behavior on STM32

Post by Giovanni »

Hi,

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

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Strange SPI behavior on STM32

Post 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.
Attachments
TEK00004.PNG
TEK00004.PNG (14.64 KiB) Viewed 6615 times
TEK00003.PNG
TEK00003.PNG (14.83 KiB) Viewed 6615 times
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Strange SPI behavior on STM32

Post by barthess »

User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Strange SPI behavior on STM32

Post 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?
Attachments
Screenshot.png
Screenshot.png (9.62 KiB) Viewed 6604 times
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Strange SPI behavior on STM32

Post 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
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Strange SPI behavior on STM32

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Strange SPI behavior on STM32

Post 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
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Strange SPI behavior on STM32

Post 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.
Post Reply