Arduino Forum

Using Arduino => General Electronics => Topic started by: 012anonymousxyz on Feb 08, 2013, 03:41 pm

Title: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: 012anonymousxyz on Feb 08, 2013, 03:41 pm
Hello and thanks a lot! I'm really sorry if this is an annoying question that is obvious but I attempted to google it with no success.
Today is a snow-day here but (on Monday now) I am going to attempt to connect my Arduino to another Arduino.
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: fungus on Feb 08, 2013, 03:59 pm
It depends on how they need to communicate to each other.

Can either of them start a conversation? Only one of them...?
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: hmarrero on Feb 08, 2013, 04:18 pm
This is a great place to start... I hope it helps.

http://www.i2c-bus.org/i2c-primer/

Hugo
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: Denbo on Feb 08, 2013, 04:27 pm
Why not just look at the tutorial on the Arduino website?   http://arduino.cc/en/Tutorial/MasterReader (http://arduino.cc/en/Tutorial/MasterReader)
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: Docedison on Feb 08, 2013, 05:57 pm
I read that link and..
I Think This phrase is incorrect
Quote
iIf powering the Arduinos independently is an issue, connect the 5V output of the Master Arduino to the VIN pin on the slave.
As the Vin porrt is the input to the regulator and would underpower the second Arduino by the inherent I/O differential of the second regulator because of the 5V source, The sentence should read:
If powering the Arduinos independently is an issue, connect the VIN Pin on the  Master Arduino to the VIN pin on the slave.
This will work without issue.. as long as you remember to "Connect the Grounds".

Bob
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: 012anonymousxyz on Feb 08, 2013, 08:03 pm
According to what I read, in a scenario where multiple devices have the capability to be a master, or simply just in general, the master is the one which chooses the clock speed.
But I also read the code example in the link provided, and I don't see where the master Arduino set the speed. If in the line:
Wire.begin();        // join i2c bus (address optional for master)
One adds the optional parameter for the master Arduino, the code is practically the same.
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: Denbo on Feb 08, 2013, 08:29 pm

But I also read the code example in the link provided, and I don't see where the master Arduino set the speed. If in the line:


It is setting it by default to 100Mhz.   See this post for how to change that  http://arduino.cc/forum/index.php/topic,16793.0.html (http://arduino.cc/forum/index.php/topic,16793.0.html)
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: 012anonymousxyz on Feb 08, 2013, 09:30 pm

It is setting it by default to 100Mhz.   See this post for how to change that  http://arduino.cc/forum/index.php/topic,16793.0.html (http://arduino.cc/forum/index.php/topic,16793.0.html)


Okay, so the Wire.begin(); sets the clock speed. But both Arduinos have a command in their code called Wire.begin(). So whos becomes the master?
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: SirNickity on Feb 08, 2013, 09:40 pm
The one that starts talking.  There isn't any hardware difference.  If you speak without being spoken to, you're the master.  You can have multiple masters.  The slave is merely any device that has been addressed by a/the master.  This is why the master doesn't have to assign itself an address.  If no one is going to direct communication at you (except in reply), there's no real need for an address.

Contention on i2c is resolved by holding the clock.  If you read the data bus after transmitting your data and the line is not high, it's because someone else caused a collision.  These are resolved by the bit pattern.  The one holding the line low the longest wins.

SPI is different.  Electrically, there is a master and one or more slaves.  This determines which pin is set as an output and which is an input, and how you deal with slave select -- either listening for it or toggling digital output pins to select other ICs.
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: Denbo on Feb 08, 2013, 09:43 pm

Okay, so the Wire.begin(); sets the clock speed. But both Arduinos have a command in their code called Wire.begin(). So whos becomes the master?


If you read the Wire.begin() reference you would see the following:
   Parameters
      address: the 7-bit slave address (optional); if not specified, join the bus as a master.

http://arduino.cc/en/Reference/WireBegin (http://arduino.cc/en/Reference/WireBegin)

Go look at the link I provided in an earlier post in this thread.   It shows both the master and slave code examples.
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: 012anonymousxyz on Feb 08, 2013, 09:55 pm
*Bows head*

I truly and greatly appreciate your help.
Yes, I skimmed through it, and that was my mistake. I appreciate the help!
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: 012anonymousxyz on Feb 08, 2013, 10:00 pm

The one that starts talking.  There isn't any hardware difference.  If you speak without being spoken to, you're the master.  You can have multiple masters.  The slave is merely any device that has been addressed by a/the master.  This is why the master doesn't have to assign itself an address.  If no one is going to direct communication at you (except in reply), there's no real need for an address.

Contention on i2c is resolved by holding the clock.  If you read the data bus after transmitting your data and the line is not high, it's because someone else caused a collision.  These are resolved by the bit pattern.  The one holding the line low the longest wins.

SPI is different.  Electrically, there is a master and one or more slaves.  This determines which pin is set as an output and which is an input, and how you deal with slave select -- either listening for it or toggling digital output pins to select other ICs.


I somehow missed this post! But it makes the most sense. Because if you go ahead and provide the optional parameter to a master, according to the description, it should not join the bus as a master.
But the one that talks is the one that sets the clock speed makes sense. The "master" switches between the two Arduinos.
I appreciate it!
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: SirNickity on Feb 08, 2013, 11:13 pm
Yep!  I find many tutorials to be great at explaining how to accomplish a simple task, but not so great at explaining how it's actually done under the hood.  A true understanding of how it all really works goes a long way in making sure your designs will handle corner cases or unique configurations.

I tend to reinvent the wheel a lot as a way of quizzing my own understanding.  Writing my own AVR C modules for these things quickly points out the areas I don't know as well as I think I do.  Then, it's back to data sheets and blog posts I go.  I'm happy to be able to pass on the knowledge that I've collected from so many other incredibly valuable sources along the way.
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: Denbo on Feb 08, 2013, 11:24 pm

Yep!  I find many tutorials to be great at explaining how to accomplish a simple task, but not so great at explaining how it's actually done under the hood. 


In the case of I2C, a cheap logic analyzer goes a long way as well   :)
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: graynomad on Feb 09, 2013, 12:54 am
Quote
It is setting it by default to 100Mhz.

Don't we wish, maybe 100kHz :)

______
Rob
Title: Re: How Are Masters and Slaves designated in I2C and SPI connections?
Post by: Denbo on Feb 09, 2013, 01:11 am

Don't we wish, maybe 100kHz :)


Hate it when I do that.   :smiley-red: