SPI Master clock being loaded by Slave - Page 1
Forum section: electronics / beginners

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 23, 2016, 10:58:54 pm
content: 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 isCode:void setup (void){// have to send on master in, *slave out*pinMode(MISO, OUTPUT);// turn on SPI in slave modeSPCR |= _BV(SPE);} // end of setupinspiration taken from Nick Gammon's code..

author: ataradov
author_rank: Super Contributor
timestamp: December 23, 2016, 11:19:13 pm
content: SPE bit just enables the SPI. You need to clear bit MSTR to set the slave mode.

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 03:21:49 am
content: Quote from: ataradov on December 23, 2016, 11:19:13 pmThe initial value of MSTR bit is 0 so by default the chip starts in slave mode.

author: ataradov
author_rank: Super Contributor
timestamp: December 24, 2016, 03:23:17 am
content: Quote from: ZeroResistance on December 24, 2016, 03:21:49 amWell, 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.

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 03:38:43 am
content: Quote from: ataradov on December 24, 2016, 03:23:17 amOK I did thisCode:void setup (void){// have to send on master in, *slave out*pinMode(MISO, OUTPUT);// SPI in slave modeSPCR &= ~(_BV(MSTR));// turn on SPISPCR |= _BV(SPE);} // end of setupbut the signal is still loaded, faulty pin perhaps?

author: ataradov
author_rank: Super Contributor
timestamp: December 24, 2016, 03:44:37 am
content: Quote from: ZeroResistance on December 24, 2016, 03:38:43 amI 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.

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 03:59:36 am
content: Quote from: ataradov on December 24, 2016, 03:44:37 amWhen 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.

author: ataradov
author_rank: Super Contributor
timestamp: December 24, 2016, 04:06:45 am
content: 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.

author: rstofer
author_rank: Super Contributor
timestamp: December 24, 2016, 04:17:19 am
content: 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.

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 04:57:14 am
content: Quote from: ataradov on December 24, 2016, 04:06:45 amWhen 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 ..?

author: ataradov
author_rank: Super Contributor
timestamp: December 24, 2016, 04:58:59 am
content: 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.

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 04:59:32 am
content: Quote from: rstofer on December 24, 2016, 04:17:19 amThe 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..

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 05:16:14 am
content: Quote from: ataradov on December 24, 2016, 04:58:59 amQuote from: ataradov on December 24, 2016, 04:58:59 amThe 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..

author: ataradov
author_rank: Super Contributor
timestamp: December 24, 2016, 05:18:14 am
content: 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.

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 05:24:34 am
content: Quote from: ataradov on December 24, 2016, 05:18:14 amWith 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

author: ataradov
author_rank: Super Contributor
timestamp: December 24, 2016, 05:30:28 am
content: 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.

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 05:43:00 am
content: Quote from: ataradov on December 24, 2016, 05:30:28 amI 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 ...

author: ataradov
author_rank: Super Contributor
timestamp: December 24, 2016, 05:55:05 am
content: Quote from: ZeroResistance on December 24, 2016, 05:43:00 amIt has that LED as well.Quote from: ZeroResistance on December 24, 2016, 05:43:00 amI'd just remove that LED.

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 06:02:56 am
content: Quote from: ataradov on December 24, 2016, 05:55:05 amIt's wierd that no one else complained and also there is no info on the net about this issue?

author: ataradov
author_rank: Super Contributor
timestamp: December 24, 2016, 06:06:24 am
content: Quote from: ZeroResistance on December 24, 2016, 06:02:56 amI 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.

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 06:12:14 am
content: Quote from: ataradov on December 24, 2016, 06:06:24 amGood 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

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 06:57:39 am
content: I removed the 1K of one board but the problem still presists!! Its still loading the clock signal...

author: rstofer
author_rank: Super Contributor
timestamp: December 24, 2016, 04:27:10 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: December 24, 2016, 07:27:52 pm
content: Quote from: ZeroResistance on December 24, 2016, 06:57:39 amI'd remove the other one as well.Quote from: ZeroResistance on December 24, 2016, 06:57:39 amArduinos are 5V systems.

author: ZeroResistance
author_rank: Frequent Contributor
timestamp: December 24, 2016, 07:46:56 pm
content: Quote from: rstofer on December 24, 2016, 04:27:10 pmI'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 scopeMy 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...