EMBED WITH ELLIOT: LIN IS FOR HACKERS

January 14, 2023 0 By pfut

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 Klaster. 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. Twoja decyzja.

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.

Mocne i słabe strony

No bus is ideal for all occasions, and LIN is no exception. LIN is not particularly fast, being developed around 19,200 baud UART. Aktualizacje przychodzą dość rzadko z perspektywy mikrokontrolera. Transakcja pełnej długości, a limit czasu trwa około dziesięciu milisekund. Jeśli Master Ankieta szesnaście urządzeń, to wskaźnik aktualizacji około siedmiu najgorszego przypadku Hertz. Oczywiście, master nie musi ankiety każdego czasu za każdym razem, a wiele razy wiadomości będą połowa tej długości, ale nie otrzymasz znacznie więcej niż 200 Hz. Z drugiej strony szybkość aktualizacji jest stała ze względu na zdolność do wykonywania ciasnych limitów czasu dla urządzeń flakowych, co jest wspaniałe dla niezawodności i prostoty, a nie jest to znacznie wolniejsze niż I2C.

Istnieją dwie główne wersje LIN, które zobaczysz na wolności, 1.x i 2.x. Oprócz opcjonalnych typów ramek omówionych powyżej, dwie wersje mają różne wzory kontrolne – a jeden w 2.x jest naprawdę dziwaczny – wymagający kalkulatora internetowego, aby upewnić się, że robisz to dobrze. Zamiast dodawania MOD-256 odejmują 255 z dowolnej wartości 256 lub więcej. To jak 8-bitowy przepełnienie, który owija się do 1 zamiast 0. Czy ma to sens do każdego z was?

Urządzenia LIN nie są tak rozpowszechnione poza przemysłem motoryzacyjnym jako I2C lub SPI, przez długi strzał, więc prawdopodobnie nigdy nie byłeś zmuszony do radzenia sobie z protokołem. Ale jeśli chcesz nawiązać niewielką liczbę modułów opartych na mikrokontrolerach, tak łatwo i tanio, jak to możliwe, używając tylko jednego przewodu (plus ziemi), trudno jest myśleć o czymś łatwiejszym. Pisanie kodu Slave I2C z pewnością nie ma pikniku. Kod pisania do słuchania konkretnego bajtu na linii UART, a następnie reaguj nie może być prostsze.

Chcesz zamienić swoją równinę-wanilię w autobusie? Weź stronę lub dwie z książki Lin! już zrobiłeś? Pokaż nam!