Arduino接口-week3-Lesson2

来源:互联网 发布:独立开发者 知乎 编辑:程序博客网 时间:2024/06/09 17:32

This series of articles are the study notes of "Interfacing with the Arduino", by Prof. Harris, Department of Computer Science, University of California, Irvine. This article is the notes of week 3, lessen2.

2. Lesson 2


2.1 Lecture 2.1 - I2C Communication


Now we're going to look at a different library. The I2C library, actually it's called the wire library. But it supports what's called I2C communication.

2.1.1 I2C Communication Protocol

So one of the things that you often want to do in a system when you have a micro controller. You want toconnect it to another integrated circuit. Some other chip. Maybe it's a memory chip, maybe it's a digital analog converter, whatever it is. You want to connect your micro controller to other chips. So you can talk to those chips and tell them what to do, grab data from them and so forth. So these chips, when they communicate there are several different protocols. Communication protocols which they can use. The one we're going to talk about right here iscalled I2C.

(1) Synchronous, serial protocol

  • serial
I2C communication protocol is a serial protocol. Serial means it sends data over only one wire. Data is only traveling over one wire. This is good,serial protocols are good because they save bits, they save pins rather. You don't need many pins, you just need the one or in this case you need two total pins, but data is only going across one. So you need very few pins. The downside of that is that since it's serial it's sending one bit at a time. So it takes longer, it's slower. If you send eight bits in parallel that's a lot quicker than sending one at a time. So, it's a serial protocol. Which saves pins, which are important.
  • synchronous
I2C communication protocol is a synchronous protocol. What that means is that if there are two communicating entities,two integrated circuits, talking about I2C, they need to share the same clock. So they need to get the same clock. They need to share it so they can synchronize with each other. So, based on the clock, the receiver knows when the sender's going to be sending data. The receiver knows when to read the bus through, sender knows when to write. So it's a synchronous protocol.

(2) Multiple master, multiple slaves

And multiple masters and multiple slaves. So we're going to talk about this concept of master and slave. But basically, the master is the one who starts all the communications and the slave is the one who waits for communications, roughly. And you can have multiple masters and multiple slaves in this particular bus protocol.

(3) Bitwise is fixed, independent of number of slaves

Bit width is fixed. So when I say bit width is fixed,the number of bits traveling between these two, between any numbers of devices on an I2C bus is fixed. It's always two. There is the data signal and the clock signal, just two. And it's independent of the number of slaves. There are other protocols where every slave you have to add more bits. But I2C you can have as many slaves as you want, or up to the limit that they enforce. And you can, and it's the same number of bits, two bits. 

(4) Two wires SDA (serial data) and SCL (serial clock)

Two wires SDA and SCL. These are the common names.SDA is the data, that's the line where the data's going to travel. And SCL is a clock that's used to synchronize all the devices.

(5) Both lines, SDA and SCL, are open-drain

  • Pulled up to high by default
  • State of bus is always known
Both lines, SDA and SCL, are open-drain, so what that means is that they are connected to power through a big resistor.
And so what happens is they're pulled up high by default. That means if let's say there are lots of different I2C communicating entities on this bus. They're wired to the SDA and SCL lines. But nobody is talking on the bus. So none of these components are driving those two wires high or low. Because they're connected to power through a big resistor, they will automatically go high. They will be high. Now, if any one of these communicating entities wants to force a zero into the line. They can pull it down to a zero, because it's a very big resistor. So it's a weak pull up. So you can pull it down, but if nobody's talking on the bus, the state of the bus is still defined. You always know that it's going to be high by default. And that's actually useful in the protocol to know, that the state is always either zero or one. It's not some random intermediate value.

2.1.2 Terminology

(1) Master- Initiates and terminates transmission; generates SCL

So some of the terms we're going to be talking about are Bus Protocol. One is the master and slave. So a master is a node on the bus that initiates and terminates transmission. So when transmission starts the master starts it. When it stops the master stops it. Also it generates the clock SCL, so the clock is generated by the master. 

(2) Slave- Addressed by the Master

Now the Slave is addressed by the Master. So the Slave basically waits until the Master starts communication and until the Master stops communication. And does whatever the Master says. So these transmissions are either write transmissions or read. Sending to or from. And the slave just essentially obeys the master. If the master says I'm doing a write then the slave knows okay I better listen. I better read. I better read whatever's on the bus.

(3) Transmitter- Placing data on the bus

Now, in addition to that, there's also transmitter and receiver which are orthogonal to master and slave. So the transmitter is whatever node is putting data onto the bus.

(4) Receiver- Reading data from the bus

And the receiver is whatever date known as receiving data from the bus.
Now the master can be the transmitter, it can be the master that starts transmissions and puts data on the bus. Because they're doing a write. They want to send data to the slave,but the master can be the receiver. The master can start a transmission and say, okay slave, this is a reinteract, a retransmission. So it effectively orders the slave to give it information. So the master can also be the receiver, and the slave can be the transmitter or receiver also. So every node is either a master or a slave, and can also be either a receiver or a transmitter, and they can be mixed and matched.

2.1.3 I2C Network


SDA and SCL are bidirectional

This is a picture of what the bus network will typically look like. Each one of those blue blocks is labeled master, slave. Think of that as probably an integrated circuit, or some component that is talking on this bus. And they're labeled master and slave to say what their roles are on the bus. And there are a couple of masters and three slaves. You can mix and match. Each one of those connect to the same set of two lines SDA and SCL. So SDA and SCL are common to all the masters and slaves, all the nodes on the bus. And they share it, and they're bidirectional. So you can talk in two directions, you can talk, data can go either way along those wires. From left to right, right to left, whatever. Also notice that the two lines SDA and SCL are wired to Vcc, meaning power through big resistors. Those resistors aren't labeled, but they are presumably big. 10kΩ or something like that. We'll be talking about networks with just two nodes, one master and one slave, but you can have much larger networks.

2.2 Lecture 2.2 - I2C Transactions

So an I2C transaction, structure of a transaction, means the sequence of zeros and ones that you expect to see on the two lines. The data line and the clock line. In order to send data, to write data or read data. I'm going to go over what the transaction looks like in detail over time. 

2.2.1 I2C Transaction Structure

So what ones and zeros sequence is expected on those two lines? So one thing to note is that in I2C, every transaction is either a write transaction, meaning the master is writing to the slave, sending data to the slave.Or it's a re-transaction, meaning the master is going to receive data from the slave.

(1) Start Condition

Indicates the beginning of a transaction.
So, every transaction starts off with a start condition. A start condition is a condition on the two signals, the SDA and SCL that indicates the beginning of the transaction. I'll show you that on the next slide. So the start condition is meeting, once the start condition happens the master creates the start condition on the bus. And then the slaves are all listening to the bus. And they look for the start condition, when they see the start condition they sensibly perk up their ears, They start listening because they know a transaction is about to start.

(2) Address/ Direction Byte

  • Specifies slave for communication
So, after the start condition is sent, and then you are going to send the address. So every slave on this bus has an address, a known address. Andthe addresses are seven bits long. There's also another variance to this protocol where addresses can be ten bits long, but right now let's say seven bits long. So you got this seven bit long address that's associated with every slave and every slave knows their own address. 
So the master, when it's sending data to a particular slave, starting a transaction with a particular slave. It has to send that seven bit long address on the wires, one clock cycle at a time. On an SDA line, say the address is 0000001. So it puts that sequence on the SDA line. One clock cycle at a time, one SCL. So the first SCL clock, you'll have a zero. Next one, you'll have a zero, and so on. It puts them on one at a time.
  • Specifies read vs. write transaction
And then, in addition to the address, there's one more bit that is a direction. So all together, the address and direction make up a byte, that's eight bits all together. Soseven bit address plus a one bit direction. And the direction is, it's just a single bit, zero or one.It indicates whether this is a read transaction or a write transaction. So what happens is, at the point when the address and the data direction are being sent, the slaves are all listening for the address. They receive the address bits one at a time. Then once they receive all seven, they compare those address bits to their own address. And if it's a match, then they continue to interact on this transaction. And if it's not a match, then all the rest of the slaves are aware it's not a match. They just go to sleep and forget about this transaction and wait until it's over.

(3) Data Byte(s)

  • Transmitted by either master or slave
So once you've sent the address and the direction, then there's a data byte or a set of data bytes that can be sent. Now, if this is a right transaction, then you send as many data bytes as you want to write. You want to write ten bytes, you send ten data bytes. If this is a read transaction, then what happens is the slave has to send bytes back, as many bytes as are needed to be read. 

(4) Stop Condition

  • Indicates the end of a transaction
So there's a sequence of data bytes that happen, and data bytes keep getting transferred until finally, there's a stop condition. The message says okay, communication over. And it sets a stop condition, sends that on the data line, the SDA line. And the slave recognizes that, and then stops the transmission.

2.2.2 Start and Stop Conditions


(1) Start Condition:

SDA = 1->0
SCL = 1

  • Falling transition on SDA while SCL=1

(2) Stop Condition:

SDA = 0->1
SCL = 1

  • Rising transition on SDA while SCL=1
So that's the structure, that just to look at the start of this, the beginning of this, we talked about a start condition. So a start condition and a stop condition, here's what they look like. This is how you start and stop any transaction. So there are two lines, SDA and SCL. The SDA line, if it falls, if there is a falling edge on that line. So you can see the SDA line is high. It goes from high to low. If that happens while the SCL line, the clock line, is high, then that's a start condition. Now, stop conditions is the reverse. The SDA line goes from zero to one while the clock line, the SCL line, is high. So that's a stop condition. So when the master wants to start a communication, they, the master drives SDA from high to low on wall SCL. Serving SCL high and if it wants to stop the communication, then it just does the opposite. It drives SDA from low to high while the SCL is high. So start condition is falling transition on SDA while SCL=1. And stop condition is a rising transition on SDA while SCL=1.

2.3 Lecture 2.3 - Sending Bits

This whole protocol is about sending one bit at a time. First you're sending the address bits, the direction bits. Then you're sending the data bits and so on. So you have to be able to send one bit at a time on these lines.

2.3.1 Sending a Bit

(1) SDA and SCL

There are two wires, SDA and SCL. And the data, whatever bit you're going to send, that's going to go on the SDA line. But while the SDA line is going,the SCL line, it's a clock, so its going up and down. So this is showing how sending a bit how a might look. Now you can see at the bottom, the SCL that's low then high then low, that's one clock pulse. The SDA line however, that can change from zero to one, one to zero. The SDA, depending on what bit you're sending, you might be sending a zero or a one.
So, notice that in the time diagram picture here for the SDA, you can see that the SDA, you have it on both low and high. That's representing the fact that it could be zero or one.

(2) SDA is sampled by receiver on the rising edge of SCL

But you'll notice there are two places where it crosses, where it starts out low high then it crosses and then it's low high again, then it crosses again. So those crosses what they mean in this diagram is that those are the times when SDA can change value. So all this diagram is trying to say is that, say you want to send a bit, right? A zero or a one, so you put that on SDA. But,SDA is sampled by the receiver on the rising edge of SCL. So, the value of SDA that matters is the value of SDA that SDA has right when the clock edge is going hot. So, you see where that SCL line goes from zero to one, the value at SDA at that time is what counts. So whoever is doing the transmission, they have to apply a value on SDA before the SCL goes high and hold it while SCL is going high.

(3) SDA must be constant where SCL is high

Also, there's a rule about this protocol that, SDA, the data line has to be held constant as long as the clock is high. And you can see that in this picture. The SDA line, it can change values but notice that, the changes in value happen either before the clock goes high, or after the clock goes high.
Always when the clock is low, that's when the change happens. There can't be a change during the time when the clock is high. That's just a rule. They only time you can break that rule is if you're doing a start condition or a stop condition. Those conditions explicitly violate those rules.
  • Exception is Start/Stop Condition
A start condition is where the SDA goes low while the clock is high. And a stop is where it goes high while the clock is high. But if you're sending data, you're sending actual data, then the rule is that the SDA line has to be held constant the whole time that the clock is high.

2.3.2 Acknowledge Bit

An acknowledge bit. What this means is the way this protocol's organized. It's organized around sending bites. All data is sent one byte at a time. So you do a start condition. Then you send a byte. The first seven bits of the byte are the address. And then the next one bit is the direction. But all together that's a chuck of eight bytes. Then after you send that byte, let's say it's a right transaction, then you start sending data bytes. One bit at a time, but you send them in chunks of eight. Now, what happens is, after a chunk of eight is sent, after a sequence of eight bits is sent, the receiver has to acknowledge that it received the eight bits.
Now this is for error checking essentially, because you never know what kind of error might have happened in communication. So if it didn't receive the eight bits, then you need to restart your communication. So after every chunk of eight, the receiver has to transmit what's called an acknowledge bit.


(1) After each byte is sent, the receiver must acknowledge

Now what that means is, let's say we've got the SDA line, SCL line. SCL's pulsing high low and SDA, somebody, say the master, is transmitting to the slave. So,it transmits eight bits and we've numbered them up there on the SDA line 1, 2, 3, ..., to bit 8. Soonce it sends out all eight of these bits, at the point, for one SCL clock pulse, the master does not drive the SDA line. It's up to the receiver, the receiver at that point to pull the SDA line low.That's called acknowledge. See where it says ACK, notice that the SDA line is low when it says ACK. So for that SCL clock pulse, the receiver has to pull that SCL line low and acknowledge. If it doesn't do that then the sender has to assume that something went wrong and restart the communication.

(2) Transmitter releases SDA, receiver must pull SDA low

  • Must be low for one pulse of SCL
So after each byte is sent, the receiver must acknowledge by pulling that SDA line low. And once the transmitter has to release the SDA line so the receiver can pull it low. 
  • If SDA is not pulled low, transmission is aborted
It must be low for one pulse of the clock or else the transmissions aborted.

2.3.3 Typical I2C Transaction


  • Each slave has a unique 7-bit address
  • Direction bit: 0 indicates write, 1 indicates read
So typical I2C transaction, a very short ITC transaction. Here's what it'll look like overall. There's a start condition, that starting blue block right there. That's how you initiate everything with the start condition. Then you send 8 bits, the address bits, 7 address bits plus a direction bit. So that's 8 bits. Then there's an acknowledgement to ACK right there. Then you send 8 more data bits. And then there's another acknowledge, and then you can stop. Now this is short, right? It's only transmitting one data byte. This could be extended. You could have many data bytes, right? In this case I just did one. So this is sort of a typical I2C transaction, what it looks like on the SDA lines.

0 0
原创粉丝点击