A automobile is a rolling pile of hundreds of microcontrollers these days — just ask any greybeard mechanic and he’ll start his “carburetor” rant. all of these systems and sub-systems need to talk to each other in an electrically hostile environment, and it’s not an exaggeration to say that miscommunication, or even delayed communication, can have major consequences. In-car networking is major business. Mass production of automobiles makes numerous of the relevant transceiver ICs low-cost for the non-automotive hardware hacker. So why don’t we see much more hacker projects that leverage this remarkable resource base?
The backbone of a car’s network is the Controller area Network (CAN). Hackaday’s own [Eric Evenchick] is a car-hacker extraordinaire, and wrote up many everything you’d want to know about the CAN bus in a multipart series that you’ll certainly want to bookmark for reading later. The engine, brakes, doors, and all instrumentation data reviews (differential) CAN. It’s fast and high reliability. It’s also complicated and a bit expensive to implement.
In the late 1990, numerous producers had their own proprietary bus protocols running alongside CAN for the non-critical parts of the automotive network: how a door-mounted console talks to the door-lock chauffeur and window motors, for instance. It isn’t worth cluttering up the main CAN bus with non-critical and local communications like that, so sub-networks were spun off the main CAN. These didn’t need the speed or reliability guarantees of the main network, and for cost reasons they had to be easy to implement. The smallest microcontroller ought to suffice to roll a window up and down, right?
In the early 2000s, the local Interconnect Network (LIN) specification standardized one method to these sub-networks, focusing on low cost of implementation, medium speed, reconfigurability, and predictable behavior for communication between one master microcontroller and a small number of slaves in ein Cluster. Cheap, simple, implementable on small microcontrollers, and just best for medium-scale projects? A hacker’s dream! Why are you not using LIN in your multiple-micro projects? Let’s dig in and you can see if any of this is helpful for you.
The LIN Protocol
A LIN “cluster”, which is what the local mini-network is called in the jargon, consists of a single master microcontroller and a number of slaves. LIN starts off as conventional 8N1 UART serial, typically at 19,200 Baud, and does away with one wire. Next, it adds a protocol that allows this single wire to be used as a bus, shared among multiple slaves. If you tried to roll your own network protocol for easy UART serial communications, you’d end up with something like LIN. Go fetch a copy of the specification (PDF) and read along!
Every LIN transaction is fundamentally the same: the master sends a header that includes a protected identifier (PID), which specifies the task to be carried out. tasks can be something like “report temperature sensor 2” or “set servo 3 position”. depending on the task, between one and eight bytes of data follow, with a two-byte checksum. The slaves have to know which tasks to respond to, and how to respond. So if “set servo 3 position” is sent, the servo 3 slave needs to listen for the next bytes and react accordingly. All slaves that don’t respond to the command can disregard data until the next preamble.
In the case of “report temperature sensor 2”, the slave with the temperature sensor sends its data right away after receiving the command. because the byte length is known in advance, and only sensor 2 is allowed to respond to this task, the master knows to listen for exactly, say, four bytes in reaction and knows how long that ought to take.
This polling system with the master sending headers and the slaves sending reactions guarantees that none of the devices will access the bus at the same time, so LIN gets by with just a single RX/TX line. The preamble includes a sync byte (0x55) that helps the slaves lock on to the master clock, so the slaves can run on less expensive RC clock sources and auto-bauding is possible.
Since the length of messages is known ahead of time, the timing for the master’s polling routine can be written down in a schedule. The master polls the network at defined intervals, and if the slave doesn’t respond within 1.4 times the required time for the transaction, it’s presumed to be missing in action. Either way, the master is on to the next item in its schedule, and won’t retry the potentially defective slave until its turn comes around again. This guarantees a known update rate for all of the devices, which makes life a lot simpler for programming the master.
Those are the basics. The master sends PIDs, and a series of data bytes follows. Everything’s comfy old UART, call and response, adapted as simply as possible to create a small network.
Extras
GUI LIN configuration app from an instructive video.
Keeping the network that easy requires that the master and slaves all agree on the command set and valid reaction lengths. That’s a lot of information needed for the LIN cluster to function, in principle. helping matters out somewhat, there’s a conventional format for notating all of this laid out in the LIN spec.
There’s also a conventional API for C that both the master and slave microcontrollers can use to make handling coding up behavior in a LIN cluster. Combined, this makes a conventional workflow for specifying and implementing LIN busses — very useful for the automakers, and not useless for the hacker either.
There is also a sleep state and behavior that’s defined for the bus, with associated sleep and wakeup signals. all of the slaves ought to respond to the sleep signal, and any of them ought to automatically go to sleep after a timeout of four seconds if they haven’t heard from the master. any node, slave or master, can send the wakeup command, and after that the master ought to go back to its normal polling schedule.
LIN version 2.0 included a number of optional frame types that make the network much more flexible. In particular, “sporadic frames” make the slave’s reaction optional if it hasn’t gotten any new data because the last update. “Event triggered frames” are like sporadic frames, except they can be additionally responded to by any slave node that has new data.
This introduces the possibility of a collision on the bus, in which case hopefully the checksum doesn’t add up and the master falls back to slave-specific frames as before. These two modes speed up the bus when data updates are infrequent, but add some indeterminacy to the schedule and conditional complexity to the code. use them only if you need them.
The master can also have multiple schedules, and switch among them. The slaves don’t care — they just listen for the tasks that are relevant to them anyway. There’s no reason for the master to send servo position data every period if it hasn’t changed, for instance, even if it makes things conceptually simpler. Ihr Anruf.
There is even an optional carry layer spec that is compatible with CAN bus and makes it simpler to integrate the local LIN cluster with the bigger network. In short, LIN is a very thoroughly thought through UART bus protocol with good industry adoption. You’ll find good tutorials from every vendor of LIN transceiver hardware. (Here’s a terrific intro from national Instruments.)
Hardware — The Physical Layer
Topping all of this protocol niceness off is a broad variety of LIN transceiver chips ranging from $0.25 to $0.50 for plain transceivers, on up to around a buck or two for “system basis” chips with integrated voltage regulators. These are especially slick, because the transceiver can take care of the sleep/wake logic and turn the power supply to your microcontroller on and off. This makes integrating a slave node that operates at 3.3 V very simple.
Little chip purchases you a lot.
Since the LIN bus is developed for automotive, it’s typically specced for 12 V because that’s what courses through the veins of your car’s wiring harness. LIN transceiver hardware needs to be able to accommodate even higher voltages, because automobile electrical systems can be spiky environments. They also have to cope with bus contention, when the transceiver chip may be trying to pull the LIN line down while someone else is trying to pull it up, so there’s overheating protection built in as well. LIN transceivers are robust little beasties.
In contrast to I2C lines, which are pulled up with puny resistors, an automotive LIN bus is pulled up to 12 V with a 1 kΩ resistor. To pull this line down fast enough, LIN transceivers need to be able to conduct tens of milliamps, so they have slightly beefy (for ICs) transistors built in. The combination of a high voltage and relatively high line current implies that an automotive-spec LIN bus is good for 40 meters, rather than the couple meters that I2C gives you without resorting to drivers. If you need the distance or the noise immunity, LIN is there for you.
But nothing forces you to run your bus at 12 V, even the transceiver hardware. The Microchip transceivers that I’ve seen run down to 5.5 V, while the ones from NXP and Melexis run down at an Arduino-compatible 5 V.
And nothing forces you to use transceiver hardware at all! You could simply connect a PNP transistor (or P-channel MOSFET) to the bus line and drive that with the UART TX, sampling the bus with the RX line. This has the drawback of local echo, but that could be handled in software. Or, with only a few much more parts, there’s this service that we’ve seen before. I couldn’t find any hacker projects implementing LIN transceivers from scratch, though. maybe that’s because the industrial ones are just so cheap.
Stärken und Schwächen
No bus is ideal for all occasions, and LIN is no exception. LIN is not particularly fast, being developed around 19,200 baud UART. Updates kommen ziemlich selten, aus Mikrocontroller-Perspektive. Eine vollständige Transaktion mit Timeout dauert rund zehn Millisekunden. Wenn der Master sechzehn Geräte abfragt, ist dies eine Aktualisierungsrate von rund sieben Hertz-Worst-Case. Natürlich muss der Meister jedes Gerät nicht jedes Mal abfragen, und zahlreiche Male sind die Botschaften die Hälfte dieser Länge, aber Sie werden nicht mehr als 200 Hz bekommen. Andererseits ist die Aktualisierungsrate konstant, da sich die Fähigkeit, enge Timeouts für flockige Geräte auszuführen, was für Zuverlässigkeit und Einfachheit großartig ist, und es ist nicht so langsamer als I2C.
Es gibt zwei Hauptversionen von Lin, die Sie in der Wildnis, 1.x und 2.x sehen werden. Neben den oben genannten optionalen Rahmentypen haben die beiden Versionen unterschiedliche Prüfsummenformeln – und derjenige in 2.x ist wirklich bizarr – erfordert einen webbasierten Rechner, um sicherzustellen, dass Sie es richtig machen. Anstelle von Addition Mod-256 subtrahieren sie 255 von einem beliebigen Wert 256 oder mehr. Es ist wie ein 8-Bit-Überlauf, der um 1 anstelle von 0 umwickelt, macht dies einen Sinn für einen von Ihnen?
LIN-Geräte sind nicht so verbreitet außerhalb der Automobilindustrie als I2C oder SPI, durch einen langen Schuss, also haben Sie wahrscheinlich nie mit dem Protokoll gezwungen gewesen. Wenn Sie jedoch eine kleine Anzahl von Microcontroller-basierten Modulen nutzen möchten, so einfach und billig wie möglich, mit nur einem Draht (plus Masse), ist es schwer, an etwas einfacher zu denken. Das Schreiben von I2C-Slave-Code ist sicherlich kein Picknick. Schreibcode, um ein bestimmtes Byte auf einer UART-Linie anzuhören und dann zu reagieren, kann nicht einfacher sein.
Willst du deine Note-Vanille-UART in einen Bus drehen? Nehmen Sie eine oder zwei Seiten aus dem Lin-Buch! Hast du es schon getan? Zeig uns!