A new single-wire serial communication system, called the Local Interconnect Network (LIN), is intended for electronic control units in vehicles (www.lin-subbus.org). Using a PC UART for LIN provides a convenient and low-cost solution for developing and testing LIN devices.
Excluding the wakeup signal, a LIN message begins with a Break Marker of at least 13 bits and a Stop Marker of 1 bit or more, followed by a standard 8N1-coding data stream. Generally, a low-level driver is needed to access the UART hardware and interrupts to generate a startup signal that's compliant with the timing constraints defined by the LIN specification. The technique used here generates the signal via the standard Microsoft VCOMM driver.
The circuit in the figure is based on a modified K-Line interface. It's connected to a standard PC COM port. Two D-type flip-flops (IC4A and IC4B) are connected in series and used as a delay line to generate the startup signal. The TXD signal supplies both the LIN data and the delay line's clock. Diodes D2 and D3, plus resistor R2, form an AND gate, so that either TXD or IC4B can pull the LIN bus low.
The PC starts the LIN message by toggling the RTS line to reset both the D-type flip-flops that pull the LIN bus low. Next, TXD immediately starts transmission. The first two bytes are used to shift 1s through both D-type flip-flops, so that a proper startup signal is generated. Then the LIN bus is controlled only by the TXD signal after the second byte has been transmitted. The timing of the LIN messages is well maintained by the standard VCOMM driver at any baud rate.
The following procedure demonstrates how to implement a LIN master using Win32 API functions. The COM port is initialized in the following steps:
- Open the COM port by calling CreateFile.
- Initialize the COM port by calling SetupComm.
- Set up the COM port by calling SetCommState, SetCommTimeouts.
- Set and then clear RTS by calling EscapeCommFunction to reset both D-type flip-flops to 0.
- Send 0xDF, which includes two rising edges, to TXD by calling WriteFile and FlushFileBuffers. Then both D-type flip-flops are set to 1, and the LIN bus becomes 1.
The LIN message header is generated by the following COM port manipulations (see the waveforms in the figure):
- Enter the "Critical Section" to prevent the long delays caused by switching to other threads.
- Set and then clear RTS by calling EscapeCommFunction to reset both D-type flip-flops to 0.
- Send 0xF0, 0xF0, 0x55, and LIN message ID to TXD by calling WriteFile and FlushFileBuffers. This generates a 15-bit break marker, followed by a 5-bit stop marker, and 0x55, and the ID.
- Leave the "Critical Section" to avoid blocking other threads.
- Read back the received data to empty the receiving buffer and verify the data sent. Only three bytes—0 (break), 0x55, and ID—are available.