EEVblog Electronics Community Forum

Electronics => Beginners => Topic started by: ZeroResistance on December 23, 2016, 10:58:54 pm

Title: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 23, 2016, 10:58:54 pm
I linked up 2 arduino's to communicate by SPI between them.
I have connected 4 wires between the 2 arduino's CLK, MOSI, MISO and SS except that the SS of the master is going to the external interrupt pin of the slave. the SS pin of the slave is kept unconnected.
The GND's of both the arduino's are connected together.

Except that I have a wierd problem.

When I scope the CLK pin of the SPI bus I find the signal levels have dropped to around 2V and when I disconnect the SPI clk pin from the slave and just scope at the Master end it shows the correct signal levels.

The SPI slave setup code is
Code: [Select]
void setup (void)
{

  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);

  // turn on SPI in slave mode
  SPCR |= _BV(SPE);

}  // end of setup




inspiration taken from Nick Gammon's code..
Title: Re: SPI Master clock being loaded by Slave
Post by: ataradov on December 23, 2016, 11:19:13 pm
SPE bit just enables the SPI. You need to clear bit MSTR to set the slave mode.
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 03:21:49 am
SPE bit just enables the SPI. You need to clear bit MSTR to set the slave mode.

The initial value of MSTR bit is 0 so by default the chip starts in slave mode.
Title: Re: SPI Master clock being loaded by Slave
Post by: ataradov on December 24, 2016, 03:23:17 am
The initial value of MSTR bit is 0 so by default the chip starts in slave mode.
Well, yes, but I'm personally not sure that Arduino does not do something weird there.

Right not it look like you are fighting against another output driven to low level.
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 03:38:43 am
The initial value of MSTR bit is 0 so by default the chip starts in slave mode.
Well, yes, but I'm personally not sure that Arduino does not do something weird there.

Right not it look like you are fighting against another output driven to low level.

OK I did this

Code: [Select]
void setup (void)
{

  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);

  // SPI in slave mode
  SPCR &= ~(_BV(MSTR));

  // turn on SPI
  SPCR |= _BV(SPE);

}  // end of setup

but the signal is still loaded, faulty pin perhaps?
Title: Re: SPI Master clock being loaded by Slave
Post by: ataradov on December 24, 2016, 03:44:37 am
but the signal is still loaded, faulty pin perhaps?
I would not blame the hardware. Unless you have connected something wrong.

Try making this device a master and check if it can output the clock on that pin.
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 03:59:36 am
but the signal is still loaded, faulty pin perhaps?
I would not blame the hardware. Unless you have connected something wrong.

Try making this device a master and check if it can output the clock on that pin.

When I disable SPI on the slave and make SCK high impedance the MASTER clock is not loaded it only gets loaded when I enable the SPI.

And yes when I configure this slave as a master and transfer some bytes that pin generates clock pulses.
Title: Re: SPI Master clock being loaded by Slave
Post by: ataradov on December 24, 2016, 04:06:45 am
From your fist post, I understand that you are not driving SS pin on the slave? Then SPI slave will not work.

I would assume that SCK in that case will still remain an input, but reception will not work.

Try enabling pull-up register (or use external one) on that pin when it is not connected to anything and measure a voltage. If pull-up manages to bring it all the way to 5V, then it is indeed an input.

What you are doing from a software point o view is correct, so it is some hardware or connection issue.
Title: Re: SPI Master clock being loaded by Slave
Post by: rstofer on December 24, 2016, 04:17:19 am
It almost never hurts to put series resistors in uC outputs.  I usually use 330 Ohm resistors.
These resistors make up for a lot of misuses of outputs.

The slave should be configured as a slave and never drive the clock.  The master should drive the clock and CS'.  The slave needs to see CS'; to properly frame the transaction.
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 04:57:14 am


Try enabling pull-up register (or use external one) on that pin when it is not connected to anything and measure a voltage. If pull-up manages to bring it all the way to 5V, then it is indeed an input.


When I configure that pin as an input and connect a 1K pull-up to 5V the pin pulls-up to 3.3V shouldn't it have pulled up to 5V ..?
Title: Re: SPI Master clock being loaded by Slave
Post by: ataradov on December 24, 2016, 04:58:59 am
I'd check if this board has something else sitting on that pin. It looks like it is pulled low, but not hard, as MCU would. It looks like there is a  pull-down resistor.
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 04:59:32 am
It almost never hurts to put series resistors in uC outputs.  I usually use 330 Ohm resistors.
These resistors make up for a log of misuses of outputs.

The slave should be configured as a slave and never drive the clock.  The master should drive the clock and CS'.  The slave needs to see CS'' to properly frame the transaction.


The resistor is a good idea... yes I had programmed one arduino as slave and I don't think it drove the clock or CS the whole setup was working for a few hours until it started giving this wierd error..
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 05:16:14 am
I'd check if this board has something else sitting on that pin. It looks like it is pulled low, but not hard, as MCU would. It looks like there is a  pull-down resistor.


I'd check if this board has something else sitting on that pin. It looks like it is pulled low, but not hard, as MCU would. It looks like there is a  pull-down resistor.

The board is plugged into a breadboard and there is nothing else on that pin...
Also when I switch on that board I can see that the pin with the external pullup momentarily goes up to 5V and then comes back to 3.3V..
Title: Re: SPI Master clock being loaded by Slave
Post by: ataradov on December 24, 2016, 05:18:14 am
Then it is an output. And someone makes it this way.

Try with empty setup() and loop() functions.

It may be also set up by the bootloader.
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 05:24:34 am
Then it is an output. And someone makes it this way.

Try with empty setup() and loop() functions.

It may be also set up by the bootloader.

With empty setup() and loop() functions that pin just stays low, only when I say pinMode(SCK, INPUT) does that pin pull-up to 3.3V
Title: Re: SPI Master clock being loaded by Slave
Post by: ataradov on December 24, 2016, 05:30:28 am
If you are using Arduino UNO board, then SCK(PB5) pin is connected to some yellow LED. It may be a source if the problem.
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 05:43:00 am
If you are using Arduino UNO board, then SCK(PB5) pin is connected to some yellow LED. It may be a source if the problem.

I have Aruduino Nano, but I just checked don an Arduino UNO and the same problem on both boards, but when I put a 1K resistor in series the clock now only drops by 0.2Volts ... I wonder why no one else complained maybe I should just stick with a series resistor at the moment ...
Title: Re: SPI Master clock being loaded by Slave
Post by: ataradov on December 24, 2016, 05:55:05 am
I have Aruduino Nano
It has that LED as well.

but when I put a 1K resistor in series the clock now only drops by 0.2Volts
I'd just remove that LED.
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 06:02:56 am
I have Aruduino Nano
It has that LED as well.

but when I put a 1K resistor in series the clock now only drops by 0.2Volts
I'd just remove that LED.

It's wierd that no one else complained and also there is no info on the net about this issue?
Title: Re: SPI Master clock being loaded by Slave
Post by: ataradov on December 24, 2016, 06:06:24 am
It's wierd that no one else complained and also there is no info on the net about this issue?
I have no idea.  It is too late and I'm too tired to figure out hoe exactly that LED affects things. I guess the issue is aggravated by the fact that you have LEDs on both sides, and not a lot of people connect arduinos like that.

And it will probably work as is with lower level anyway.  Lower level would be a concern if it was due to MCUs fighting. In this case it is due to LEDs, so not that important really.
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 06:12:14 am
It's wierd that no one else complained and also there is no info on the net about this issue?
I have no idea.  It is too late and I'm too tired to figure out hoe exactly that LED affects things. I guess the issue is aggravated by the fact that you have LEDs on both sides, and not a lot of people connect arduinos like that.

And it will probably work as is with lower level anyway.  Lower level would be a concern if it was due to MCUs fighting. In this case it is due to LEDs, so not that important really.

Good catch about the LED's and many thanks!... anyway I rechecked and on the board are 102 resistors in series with the LED so that would be 1K so 10mA for 2 boards that shouldn't cause problems ...
anyway I will remove those LED's and check again
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 06:57:39 am
I removed the 1K of one board but the problem still presists!! Its still loading the clock signal...
Title: Re: SPI Master clock being loaded by Slave
Post by: rstofer on December 24, 2016, 04:27:10 pm
What happens with the clock when you completely disconnect the slave?  At least you can isolate which board is causing the problem.
I know I have used Arduino Uno's for SPI and had no problems so there is something weird about your setup.  The fact that you can't find similar topics on the Internet pretty much tells you that the problem is all yours.  Nobody else is having this problem.

As a side note, I wouldn't ever apply 5v, even through a resistor, to a 3.3V uC.  The pins may be 5V tolerant under some circumstances but not under for all possible actions.
Title: Re: SPI Master clock being loaded by Slave
Post by: ataradov on December 24, 2016, 07:27:52 pm
I removed the 1K of one board but the problem still presists!! Its still loading the clock signal...
I'd remove the other one as well.

As a side note, I wouldn't ever apply 5v, even through a resistor, to a 3.3V uC.  The pins may be 5V tolerant under some circumstances but not under for all possible actions.
Arduinos are 5V systems.
Title: Re: SPI Master clock being loaded by Slave
Post by: ZeroResistance on December 24, 2016, 07:46:56 pm
What happens with the clock when you completely disconnect the slave?  At least you can isolate which board is causing the problem.
I know I have used Arduino Uno's for SPI and had no problems so there is something weird about your setup.  The fact that you can't find similar topics on the Internet pretty much tells you that the problem is all yours.  Nobody else is having this problem.

As a side note, I wouldn't ever apply 5v, even through a resistor, to a 3.3V uC.  The pins may be 5V tolerant under some circumstances but not under for all possible actions.

I'm using Arduino Nano and the uC is 5V supply capable I think the UNO also has the same processor so it should also be 5V capable.

I finally discovered the just by juggling some code up and down in my setup() function of the slave board the clock seems to be now stable.
Basically I just started commenting code in the setup() function and kept reprograming and checking the clock levels on the scope

My first statement in the setup was an attachInterrupt call and if  I commented that out the clocks would come back to normal... I  finally did a manual config of ext 0 interrupt using low level registers...
I don't know why this happened but I will surely like to know because I spent 5 hours of my time debugging this...