eapbg #48 Introduction to CAN (Controller Area Network)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to the electronics and programming beginner's guide today we're going to take a look at the canvas the controller area network bus this is a communications bus that is one of my favorites because of the elegance of the implementation that uses the km bus was originally created by Bosh Bosh is a company that makes automotive components such as spark plugs oxygen sensors anti-lock brakes I think they even make airbag computers and you have to remember while we discuss all of this that can was me for automotive applications and that's what makes it a different and unique in the way it operates can is now found in airplanes and is fairly widely used in industrial applications but again remember cars this is meant for cars there are three areas where can really differs from other communications protocols such as I squared C SBI or spy whatever you want to call it and yours the first being is that the protocols let's say such as spy spy can move quite a bit of data fairly rapidly a spy is commonly used on like SD cards to talk to them and spy can stream data fairly quickly all it has to do is handshake with the particular device and then you can pull the data off same thing with I squared C I squared C is commonly used to talk to let's say eproms and with I squared C you hit the slave address of the EEPROM you address into where you want to start reading and then you can stream data for a fairly long period of time obviously as long as you don't run out of the memory that you're streaming through and you know you are just things really nilly as we talked about with can-can is actually fairly limited in the amount of data you can move in the sense of that a single cam packet only moves up to 8 bytes of data at a time you have to keep that in mind only 8 bytes of data at a time you know you're not going to be moving gigabytes of information from one location to another over Kin Kin ISM and for a quick message as can is meant to be a very limber to be able to respond quickly to stuff changing and again remember automotive application that's what you really need in a car when you have many computers talking to each other over a single bus that limberness allows you to very quickly adjust and move information back and forth not a lot of information again but still lots of information the second thing to understand about can is that can does not work in a classic master/slave type setup let's take a squared C or a spy for example there's a very distinct master which is talking to a very distinct slave this does not hold true for can with your because your uses a full duplex communication we're going to leave you are out of this one but both spy and I squared C effectively use half duplex that it gets a little muddled with the spy but that's okay but the thing you have to understand about can is can is effectively multi master type system again with the I squared C there is a kind of set up that allows you to use a multi master set up but with I squared C it's messy and it's not very stable can is specifically designed around being a multi master set up so any node can be a master because any node can be a master can uses a system of arbitration to decide who is the more master and we will get into the very nitty-gritty of how the arbitration works later in this video the final thing to understand about kin and mind you this is probably the most difficult one to understand because we are so accustomed to like I squared C or spy or your is that I squared C and spy very specifically we talk node to node or module to module however you want to think about it you have the master and when the master wants to talk to a slave is select the slave address and it sends a message and when the particular slave that that address matches you know responds those two things effectively talk to each other its node to node communication and same thing with a spy well spy uses the chip select line to select the slave that it wants to talk to and then you have communication back and forth between the master and the slave and again it's node to know this is not how can works and I can't stress that enough this is not how can works can works on a messaging type system that means that no two nodes ever directly talk to each other it's a it's very rare for a message to be met specifically from one node to another node when a node on the canvas speaks everybody on that bus can hear it and if anybody is interested in the data that that particular node is transmitting it will pull in that data as I said everybody hears the message think of it like this when you call your friend on a phone that's very similar to I squared C or spy you are talking to your friend your friend is talking back to you and it's just between us so you know nobody else is in that mutation land but can works more in the principle of like a public announcement system where you get on thing and you go a flight eight to seven is about to board and everybody in the airport hurts you and the people that are interested in flight 87 will you know walk over to the gate and begin board to begin our in-depth discussion about can first let's look at how can is wired what makes can so great for cars as can uses a differential signal meaning that it compares highs and lows between each other instead of just comparing something against the ground and that's what makes can so much more noising you so to begin drawing the canvas we need two wires like that one wire is they can high signal one wires they can low signal because cam is capable of up to one megabit of transmission rate can is getting awfully close to RF frequencies and because of that can uses something called a termination resistor to make it even more noise immune because the termination resistor reduces reflections at the end of the bus and so can use is a very specific a terminating resistor and they go here on the end and it is 120 ohms plus 120 ohms is defined in the protocol all the standards and everything that bashed without and the other side of the bus here has that same 120 ohm resistor now the thing to understand is this is the can bus this basic structure has to be in place for the campus to properly function and when I say that we're going to start hanging stuff off the bus here and the thing to remember it's you have two wires that go end-to-end and you a 120 ohm terminating resistor on either end you only need two resistors one at one end one at the other end that's it you don't get a resistor per node you don't get anything else it's one two so now let's start hanging things off the bus so normally what you see is you have a microcontroller you know for our purposes or you know since we do embedded systems when the top of our microcontroller the microcontroller has in it can Hardware which is phenomenal because you don't have to do all those yucky stuff we're going to talk about later yourself all you have to do is tap the hardware but that hardware cannot directly talk on the can bus because it needs a transceiver so remember with every microcontroller you always need a transceiver and that will look like this where you have a transceiver and that receiver ties into McCann high and can low line like that and the transceiver will be labeled with what the can high and can low lines are now the connection between the transceiver and the microcontroller is very important and very easy to screw up because we're looking at stuff from the point of view of viewer so the transceiver will have a T X and then rx T X or transmit rx or receive the TX Rx and then your microcontroller is going to have a can T X and the can Rx and like I said because we're so used to you are your first things can go a load line thing to be crossed no no no no no fastest way to screw up your canvas for can the TX and rx line I'll go straight through to the transceiver reboot TX and rx just like that and then when you start hanging other things off the bus again you get another node to ties into can hi it ties into can low and then you have your TX and rx while that tie into the my ping controller this is the basic layout for can you need a minimum of two nodes to talk out on the canvas obviously because somebody has to hear and reply to it but this is the general structure you can hang a bunch of more stuff off with more nodes etc but again you want the bus to look like a straight line that goes all the way across with the terminating resistor on one end terminating resistor on the other end and all of you know hung off of those lines before we get into the nitty-gritty of how the actual protocol works we have to look at how it looks electrically because how the protocol works is really tied up in the electrical part as I mentioned earlier the canvas is a differential signal meaning there's two signals that you have to deal with one is the can high and one is the can lo the can high here is represented by the red they can't low is represented by the green the two are effectively a mirror image of each other when the canvas is idling when there's no information going down the bus both the can high ink and low idle at about Q and a half volts and my view this is in the 5 volt can system there are 3.3 volt can systems there are some other voltages that you have to account for but that's the beauty of having a transceiver that is external to the microcontroller because the microcontroller can talk to any transceiver and then you can do your voltages and then transceiver as you see fit but anyway the canvas idles as to the half volts and a 5 volt can bus and the thing to remember is that a an idling state is also known as a recessive state and that recessive state means a 1 bit whines when the canvas asserts itself or asserts a dominant state the hot can hi goes hi close to 5 volts and the can low goes low close to zero volts and this representation 0 this is a dominant state and then the bus now transitions you know to get your bid from recessive dominant back to recessive etc so if this is a one then this is a zero then this is a one again and this is a zero against that and it's very too important to remember that recessive state is a one dominant state is a zero now looking at the bits that the canvas actually transmits we can tease out the beauty of the canvas so here's the the beginning of a camp acket and at first it looks a little confusing and it'll actually get a lot more confusing before it gets clear what makes Kane interesting is the arbitration process what the arbitration process does is that whenever the somebody wants to transmit on the bus what they do is they go through an arbitration process to select the the most master master as funny as that sounds the way this arbitration process happens is every single node that wants to talk on the bus I don't know if I made that clear enough every single node that wants to talk on the bus talks at the same time and talks on top of each other and by the end of the arbitration process there is only a single node that is left which then it finishes its message said that sounds incredibly confusing and let's take a quick peek at how this process actually happens the canvas have certain timings it has to hold to and all of those timings are defined in the standard blah blah blah what those timings say is that the bus has to remain idle for a certain period of time that time is predefined for the speed and everything else etcetera and then every single other every single node that wants to send a message out a sense a what they call to sof start of frame back so the bus is idling is at a recessive state and then everybody transmit the transmits to start a frame at the same time onto the bus so the bus goes from a recessive state to a dominant state the reason why you need to start a frame is because not everybody is the timing your crystal or all kinds of other things are exactly the same and so effectively the very first note that jumps out onto the bus with the start our frame going from recessive to dominant the start of frame that clocks you know and synchronizes everybody else and so everybody else will come up onto the bus with their start a friend synchronize to the very first note it doesn't matter if that note is the first note could change you know it's very relative it's very valuable it's very fluid so everybody is transmitting a starter frame bit onto the bus and the bus is dominant now the very first thing that gets transmitted is the first bit of the address as I mentioned previously a can is message based it's not note based and what the message address is it's an identifier that tells everybody else what kind of data you're going to be sending and the data is prioritized in a vehicle for example data from the anti-lock brakes for example you know let's say you're on icing and driving on you get the brakes and now your wheels are going through an EDS stop that data is prioritized to be a higher level than let's say data from your radio because who cares what your volume is when you're trying to panic stop on ice and so the way data is prioritized is by its address this is kind of the confusing part of just a little bit because the lower the address the higher the priority so an address of all zeros has absolutely the highest priority in the canvass that message cannot be overwritten by somebody else the other thing to understand is that no two nodes in the entire bus are coded in such a way that they have a message address that they can transmit that's the same as somebody else's message address and that's very very important no two nodes can transmit the same message address at the same time so after the start of frame you have a bits number one of the address everybody transmits that bit onto the bus some nodes will have a dominant state which is a zero and some those will have a recessive state which is what the thing that the thing to note is remember when we drew the canvas here earlier the microcontroller had a TX and rx connection to the transceiver at the same time and both of those connections are utilized simultaneously so every node asserts something over TX whether it's recessive or dominant and then over the rx it reads back what the bus state actually is and as I mentioned previously a dominant state will overwrite a recessive State so let's say half the nodes that in our imaginary transmission here want to transmit a 0 which is a dominant state and the other half want to transmit a 1 which is a recessive state obviously the dominant is going to win over the recessive and by the end of bit number 1 all of the nodes that are putting it a recessive state on the bus have realized that I do not have priority to speak somebody has a higher priority message than I do and they stop transmitting and the same process will happen with every other bit so now we're down to half of the nodes on the bus so let's say the next bit everybody puts a recessive state on the bus and they read the bus and they go ok I still have priority because I from the under the recessive state and the bus is a recessive state so you know I must still have priority so you move on to the third bit and let's say again half of the nodes will transmit a dominant state half of the nodes will transmit a resusci state so everybody will you know apply their bits to the bus at the same time and everybody that applied a recessive state reads the bus and says oh the bus is dominant I put a recessive bit on the bus I must not have priority anymore and then that all of those nodes drop off the idea is that by the time you get to the end of the address the eleventh bit only a single note is left that still has priority so you know if you're the airbag and you're about to you know the car has sense that you're crashing who cares about the radio the airbag messages have a much higher priority message then the radio does and so this message is guaranteed to get through arbitration whereas the lower priority messages just fall off to meet this arbitration process the process in which the message with the highest priority selected is the most beautiful part of can because any node can be a master as long as it's message priority is a high enough and the wind the timings and the journal structure the bus are designed is that regardless of your message priority you will eventually get a message through is just you may have to wait for other higher priority messages to make it across the bus before your lower prayers message finally gets the gift through arbitration in your node is the one that's able to transmit something else to note what we're looking at the address is that if you notice the address is 11 bits long that means that with a standard can set up you can address 2,000 or 48 different unique messages that means that you have to sprinkle that 2040 different unique messages amongst all the nodes in you both the beauty of can is that Bosch also thought of in advance and there is actually what they refer to as an extended version of can which we'll look at here in just a second that extended version of can gives you a twenty nine bit address which is I think five hundred and thirty six million different unique identifiers specially a little more than that I just didn't want to remember all the other digits but you can address pretty much whatever you want so you can take a larger data and break it down but now it becomes a management problem of you have to know which addresses are which pieces of data after the address is transmitted we have our next little group of bits that we need to look at so directly after the our address is the RTR which is remote transmitter request this bit will tell you if you want data from somebody else in the case of the canned buzz the RTR is about the only time that you can talk module to module because oftentimes the RTR bit is used in the sense of like hey you over there I need data from you I haven't heard it in a while you know please send it back to me this can be used in the sense of like a throttling for example you know let's say you don't need to regularly hear some sort of a message and to reduce congestion on the bus you want to request that data and so you use the RTR bit to do that the next bit after that is the IDE and that's what's interesting about the wait box did the standard is that they built stuff into the standard for future use and the OEE which is identifier extension that is the one one of the ones that got used later on so if you went through an 11-bit address and then you hit a recessive IDE that means that there are will that be another 18 bits coming which then finish off the address and but if the IDE is dominant that means you're only using an 11-bit bus 11 bit address then you have a reserved bit and the documentation says that hey this will be for future use so there's actually still even more expandability that or functionality that could be possibly added to the bus after the reserved bit you have four bits that tell you how much data is going to be coming down the bus and you can send anything from the zero bytes to eight bytes of data and this will tell you you know how are you sending two months are you sending six bytes you sending zero bytes are you sending eight bytes after you send out the DLC you send out the data I drew the data is a little block but this could be up to 64 bits which is eight bytes or could be as short as nothing where it's zero and this block is omitted completely that's entirely up to you how to set up because as you deal with the bus you associate a particular amount of data that comes through for each address after the data you have the CRC which stands for cyclic redundant what the CRC does is it crunches a formula on the data and it validates the data by comparing the CRC that came through with the CRC that's calculated by the received data and that is 15 bits again I'm not going to draw out all 15 squares here because that will be a waste of time after the CRC you have the CRC delimiter this is just a little gap to give you a space between the CRC and the rest of it then you have back big knowledge so after you know after the dominant node transmitted all of its stuff and the CRC etc the CRC delimiter gives you just a little bit of time to be able to calculate the CRC and do the comparison and after the CRC delimiter the ACK is where all of the other nodes on the bus have the ability to say heard you loud and clear or didn't hear you so the DES master node the node that's transmitting will do a recessive ACK and then everybody else who heard the message correctly will do a dominant ACK and that tells the a transmitting party that you know heard you loud and clear of big thumbs up the the only drawback to this kind of system is if only a single node out of a whole bunch of nodes didn't understand the message correctly because it was garbled they effectively get left out after the app you have the act delimiter just another little blank space and then you have seven bits which bar the end of frame and the end of frame idles the bus at a recessive state for seven bits as soon as the end the frame and you can then begin the start of frame now yet another oddity that can has is something called bit stuffing bit stuffing is a technique used to maintain synchronization during transmission of a packet the idea behind bit stuffing is that whenever you have a five consecutive bits that are the same ones or all zeros you insert a bit of the opposite polarity just after them so it's five that you know so if let's say the in the address field he has zero zero zero zero zero between five and six you would actually insert a one you add an extra bits to this for the bit stuffing to make sure that you are able to maintain synchronization with the pulses this is also for if let's say a node is transmitting in it detects what could be construed as a collision event for you know for some reason the you have two notes that transmitted the same address and they continue to transmit but down here your DLC RT R IDE or the data doesn't match the node can say that oh I just had a collision and if it puts a six or more bits of a particular state out onto the bus that's considered an error and then all nodes are reset this is like a safety provision to make sure that you're able to restart the bus in case a collision happens or something along those lines and for that instance you can either put all ones or all zeros usually are ones I'm sorry usually all zeros so you don't have some sort of you know you just put constant dominance data onto the bus and nobody can override you and you can error a packet and you know restart all the communication but this is so if you're looking at the cam packet with like an EMS or something like that and you suddenly go oh no I have a one here I don't understand why well if the one is following a whole of five zeros or you have five ones and then a zero that's the bit stuffing this has been a basic introduction to the can bus the controller area network bus we looked at the topology of the bus and how you have two lines that go all the way across maybe a resistor on the ends and then use transceivers to hook into that and you use a microcontrollers to tie into those not reversing the TX and rx lines of TX goes the KIAC and are exposed to art we then looked at the signal levels and what a recessive bit looks like which is a 1 and what a dominant bit looks like which is a zero and then finally we looked at how the actual arbitration process works and that everybody talked out onto the bus at the same time and by the end of the address here the a single node floats up to the surface as being the most master master as funny as that statements out and then that node will finish the message sending out the data length code and all this other stuff and the data and the ax etc and finally we looked at some other things but anyway something that I wanted to note and I wanted to be very clear and explicit about this the beauty of CAM is the fact that all of the can Smart's the stuff that actually deals with all of these bits etc is built into the hardware this is the hardware level of can what that means is Oh after you set up you know let's say your clock rate and all this other stuff which is kind of a pain in the butt to set up not going to lie to you you know I had to read the data sheet over and over and over again and look at example code etc to you and understand how to set up the bit rate and the clock speeds of the bus etc but once you get all that done more or less everything else is taken care of by the harbour for you so you all we have to do is you load your address depending on the logic board depending on the hardware that you're using you sometimes have to load the dlc sometimes it's just a matter of the data and the module knows what the deal he is and the thing that I really love is that the CRC is calculated for you by the hardware you don't have to do it in software all you have to do is just copy all of the you know fill it like filling out the form you fill out although you know you check all the necessary boxes etc and then if you're familiar with the the wrong code just set it and forget it and what that means is once you fill all this out you effectively hit go and then the hardware will sit there and monitor the bus and actively talk out onto the bus by using the address until it gets you know Arbor eventually gets passed arbitration and sends out the message and all you have to do is just kind of monitor the bits that these are related to and the same thing is true in the other direction that you don't constantly have to keep pulling the data out of the can buffers because you can actually put filters on the can buffers that you're only interested in certain other thing number ranges or maybe one specific address etc that whenever that address comes through or something of that address comes through you know you can throw in interrupts or set a flag etc you don't have to sit there and babysit it the hardware takes care of everything for you and that's where once you get past that initial configuration of Kin Kin becomes a very beautiful bus to deal with and because it's a differential bus it's very noisy noon you can actually get a you know several meter just you know tens of meters of distances out of it you you do have to transmit at a little rate but you can go quite far with the can bus again because of the differential stuff but just don't forget that all the grounds have to be tied together because kin is not a fully isolated bus but anyway those are just kind of endnotes and thank you for watching if you have any questions or comments you're more than welcome to put them down below and always don't forget to give me a big old thumbs up
Info
Channel: eapbg
Views: 96,759
Rating: undefined out of 5
Keywords: Introduction, CAN, Controller Area Network
Id: 3lkfK2-BUno
Channel Id: undefined
Length: 35min 8sec (2108 seconds)
Published: Thu May 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.