Application of LIN bus development based on finite state machine

Application of LIN bus development based on finite state machine

With the increase in the level of automotive intelligence and the need for rapid upgrades, automotive electronic networks are developing from centralized control to distributed control. As an effective supplement to the CAN bus, the LIN bus effectively solves the problem of cost increase caused by distributed control at low speed and low cost. In the distributed body control system developed by the author, CAN bus and LIN bus are used to connect each control unit to complete the data exchange between the units. The system structure is shown in Figure 1.

Figure 1 Structure diagram of body control system

The body control system includes a car light module, a door module, an interior module, a control panel module and an instrument panel module, which respectively complete the control and monitoring of the corresponding loads, and at the same time complete the data exchange through the CAN bus. Among them, the lamp module and the door module are implemented in a distributed form based on the CAN / LIN bus. This article focuses on the implementation of the LIN protocol.

LIN protocol analysis and state machine design

The finite state machine is composed of a set of states, a starting state, inputs, and a conversion function that converts the input and current state to the next state. It is a special directed graph, including some state nodes and those connecting these states. Directed arc. For a specific state machine, we must first establish some effective states, and then design the corresponding algorithm to complete the state transition.

A complete LIN frame is composed of interval field, synchronization field, PID, data field and checksum field. What the LIN protocol driver needs to achieve is to sequentially complete the detection of the interval field and synchronization field, the transmission and reception of ID, the data field Byte transmission and reception, and finally complete the verification. The state machine form is consistent with the definition of the LIN protocol data link layer specification. The corresponding field can be described by establishing the corresponding state to describe the entire LIN frame, and the current state can be monitored to switch its state according to the currently received bytes. In order to complete the sending and receiving of each field in a state transition manner. Design the following states: IDLE, BREAK, SYNBYTE, IDENTIFY, TRANCEIVE and CHECKS UM, set the flag state to reflect the state of LINdriver, idle, pendin g, succe ss, when in the intermediate state of LIN correct communication, state = pending, When LIN communication fails, state = idle, and when a complete LIN communication is completed, state = success.

Status monitoring and conversion is implemented in the UART receive interrupt service routine. Because the LIN bus uses single-wire communication and the UART is connected to the LIN physical layer transceiver, when the bus data sent by the UART is the same as the received bus data, the data received by the UART is the data sent by the UART, and the interrupt service program is received. Complete the state transition algorithm in Figure 2. Switch the state of LINdriver by judging the current state and the received data, and update the flag at the same time.


Figure 2 LIN protocol state machine

software design

The software design process is shown in Figure 3. First of all, the initialization includes UART module initialization and I / O pin initialization, setting the baud rate, enabling receiving interrupts, setting the corresponding I / O pin direction and level of the LIN physical layer transceiver, and then receiving the interrupt service program in the UART The LIN communication is completed in the form of a state machine. The state machine algorithm is described below in conjunction with the LIN frame structure and the state transition of LINdriver.

Figure 3 Software flow chart

The LIN frame starts with the interval field, the initial state of the LIN-driver is IDLE, and state = idle. The interval field is a 13-bit 0 field, which causes the reception interrupt to set the frameerror bit of the UART module at the same time. In the UART reception interrupt service program, when the received data is judged to be 0x00 and the frameerror flag bit is 1, it is considered that the interval is received Field, then LINdriver switches from IDLE to BREAK, state = pending.

The sync field is a 0x55 byte. When the current state is BREAK, if the received data is 0x55, the LINdriver is switched to SYNBYTE, state = pending remains unchanged, otherwise it is switched to IDLE, state = idle;

PID is an ID with parity check. It represents the ID with the lower 6 digits ID0-ID5. The upper two digits P0 and P1 are the parity check for the ID. When the current status is SY NBYTE, the received data is the PID. Firstly, the PID is verified according to the parity check algorithm, and then the PID is filtered according to the application layer protocol. If the filtering is successful, the LINdriver is switched to TRANCEIVE. If the verification fails or the filtering fails, the LINdriver returns IDLE, state = idle;

When the current state is TRANCEIVE, the received data is the byte in the data field. When the data reception is completed, LINdriver switches to CHECKSUM. When the current state is CHECKSUM, the received data is the checksum field. If the check succeeds, state = success is set, indicating that a LIN communication has been successfully completed. At this time, the data in the data field is valid and can be used for Application program; Failed verification means that LIN communication failed, and the data in the data field is invalid.

Conclusion

This article aims at the ordinary single-chip microcomputer, with the help of its on-chip UART, realizes the LIN protocol drive in the form of a state machine, reduces the implementation cost of LIN, and operates reliably and stably.

Portable video Player

This Portable video Player is our classic product, it is praised by our customers since it appeared on the market, it combined the DVD, TV and radio together. It is a small, portable player. It has fashionable design and light weight. It supports various format video files. It has HD screen, built in loud speaker, and chargeable battery with big capacity, can enjoy video program long time. We have different size and color for you to choose. This Portable DVD Player is a ideal gifts for your kids, elders and friends. 

Portable DVD PlayerPortable Video Player


Portable Video Player

Portable Video Player,Mini Portable Video Player,Media Player

Shenzhen New Wonderful Technology Co., Ltd. , https://www.sznewwonderful.com