A Practical LIN Protocol Driver Design

introduction:

This article refers to the address: http://

As an effective supplement to the CAN bus, the LIN bus replaces the CAN bus in the low-end body electronics field, which not only meets the functional requirements, but also saves costs, and is widely used in domestic vehicles that are more sensitive to cost. Different from the CAN bus, there is a special protocol driver. The user does not need to manage the underlying communication and directly write the application. 1. The LIN bus does not have a special protocol driver. Generally, the underlying communication needs to be implemented by software on the basis of the SCI module. A domestic car designed a LIN master node product, combined with the LIN 2.0 specification, first introduced the function of the LIN protocol driver, and then introduced the key design techniques of the protocol driver from the data link layer and the application layer.

1 drive function:

The LIN specification defines the data format, message format, and time-based scheduling communication mechanism. As the LIN master node, the functions that need to be implemented include:

1. Encapsulation and transmission, reception and parsing of the message, filling/extracting the ID and data according to the message format;

2. Communication management, controlling the rotation of the time slice and the transmission of the corresponding frame in the manner of a schedule;

3. Network management, hibernation and wake-up;

The LIN bus adopts the 8N1 SCI data format, and the protocol driver is implemented in the form of software on the basis of SCI. Software is "data + operation" 2, as a reusable, highly portable software module, its data structure and API function design are two important components of software module design, from the data link layer and application The two aspects of the layer introduce the data structure design and API function design of the protocol driver.

2 data link layer:

The data link layer mainly implements the sending and receiving of LIN messages. The format of the message is shown in Figure 1:

11.jpg

Figure 1 LIN message format

The LIN message consists of a message header + response. The message header includes three parts: synchronization interval, synchronization field and identifier. The synchronization interval is 10 bit 0, the synchronization field is 0x55, the identifier uniquely identifies the message, and the response includes data. And checksum two parts, the message data length is specified by the application layer design, can also be considered to be uniquely specified by the identifier, the checksum includes the classic checksum and the enhanced checksum, both with carry Addition is calculated, the difference is that the classic checksum only checks the data, while the checksum of the enhanced checksum contains the identifier, the diagnostic message uses the classic checksum, and the other messages use the enhanced calibration. Check and.

Since the LIN physical layer is a single-line communication, and adopts a multi-slave time slice rotation mode, there is no competition bus problem of the CAN bus. 3, the LIN node transmits data and can read back the same data, and the message is transmitted and received. It can be unified in the receiving interrupt of the SCI, in the form of a state machine to achieve 4, the state corresponds to the various components of the message, the state machine jump condition is the data receiving interrupt. According to the structure of the LIN message, a structure of the following form is designed,

Typedef struct

{

Uchar pid;

Uchar datalen;

Uchar data[8];

Uchar checksum;

L_bool done;

L_state state;

L_bool error;

}l_frame;

Where pid is the identifier, data is the packet data, datalen is the data length, checksum is the checksum, state is the state machine state, and its type is defined as follows:

Typedef enum

{

l_IDLE,

l_BREAK,

l_SYNC,

l_PID,

l_DATA,

l_CHECKSUM

}l_state;

The state machine design is implemented in the SCI receive interrupt handler function, and some of the implementations are as follows:

Void l_ifc_rx_BcmIfc(void)

{

Uchar ch,tmp,i;

Ch=Lin_periph[SCIDRL];

Switch(Cur_frame.state){

Case l_IDLE:

If(0x00==ch){

Cur_frame.state=l_BREAK;

l_SendChar(0x55);

}else{

Cur_frame.state=l_IDLE;

}

Break;

Case l_BREAK:

If(0x55==ch){

Cur_frame.state=l_SYNC;

l_SendChar(Cur_sch_item->pid);

}else{

Cur_frame.state=l_IDLE;

}

Break;

Case l_SYNC:

If(Cur_sch_item->pid!=ch){

Cur_frame.state=l_IDLE;

}else{

Cur_frame.state=l_PID;

Cur_frame.pid=Cur_sch_item->pid;

Cur_frame.datalen=Cur_sch_item->datalen;

If(l_SEND==Cur_sch_item->mode){

Tmp=Cur_sch_item->data[0];

l_SendChar(tmp);

Cur_frame.data[0]=tmp;

Cur_frame.datalen--;

}

}

Break;

Case l_PID:

Cur_frame.state=l_DATA;

If(l_SEND==Cur_sch_item->mode){

If(Cur_frame.datalen==0){

Cur_frame.check=l_CalcChksum();

l_SendChar(Cur_frame.checksum);

Cur_frame.done=1;

}else{

Tmp=Cur_sch_item->data[Cur_sch_item->datalen-Cur_frame.datalen];

l_SendChar(tmp);

Cur_frame.data[Cur_sch_item->datalen-Cur_frame.datalen]=tmp;

Cur_frame.datalen--;

}

}else{

Cur_frame.data[0]=ch;

Cur_frame.datalen--;

}

Break;

Case l_DATA:

...

Break;

Case l_CHECKSUM:

Default:

Break;

}

}

When declaring variables and functions, they all begin with "l_", which avoids conflicts with other modules in the variable and function namespaces, thus enhancing portability.

3 application layer:

The application layer mainly implements message signal access and communication management.

3.1 Signal access

Firstly, the data field of each message is designed according to the position and length of the signal in the data field of the message, and then the signal is accessed by means of the structure member variable. Taking the message sent by a sunlight sensor communicating with the node as an example, the length of the message data field is l_SunSensLen=4, and the signal includes the sunlight sampling value, the headlight operation request, the small lamp operation request, etc., the message data field structure As follows:

Typedef struct

{

L_bool l_ss_sshealth:1;

L_u8 l_ss_headlampreq: 2;

L_bool l_ss_poslampreq: 2;

L_u8 :3;

L_u8 l_ss_ssvalue: 8;

L_u8 l_ss_headlampswth: 8;

L_bool l_ss_sserror: 1;

L_u8 :3;

L_u8 l_ss_ssmsgcounter: 4;

}l_ss_msgType;

For ease of use, define the union as follows:

Typedef union

{

L_u8 data[l_SunSensLen];

l_ss_msgType sunsens;

}l_ss_msgBuf;

Define a global variable l_ss_msgBuf l_SunSens for the message data field; take the "access mode without copy" 5, directly assign and value the LIN signal, such as reading and writing l_SunSens.sunsens.l_ss_headlampreq to achieve the request for the headlight operation Signal access. The reason for this is that the LIN message period using the schedule method is fixed, and the speed of signal change is an integer multiple of the length of the schedule. For LIN applications, it is basically on the order of hundreds of milliseconds. The application is for LIN. The access speed of the signal data is much faster than this change speed, that is, it has been accessed before the data changes, which is simple and intuitive and saves the variable space.

3.2 Communication Management

LIN communication uses time slice rotation to schedule communication. Schedule management is the core of communication management. The data structure of the schedule entry is given below:

Typedef struct

{

Uchar handle;

Uchar pid;

l_Resp_mode mode;

Uchar datalen;

Uchar *data;

Uchar ticks;

}l_sch_table_item;

The schedule is an array of l_sch_table_item structures, pid indicates which packet the entry corresponds to, mode indicates whether the node sends or receives the data field, *data is the address of the data field structure of the message, and ticks is the length of the time slot. When the schedule table array is initialized, the address of the message data field structure variable is assigned to *data in the schedule entry, thus implementing the "access mode without copy" in the access mode section. The schedule is a circular sequence. When scheduling to the end of the table, it switches to the header and continues to rotate. The rotation function of the schedule is as follows:

Void l_sch_tick(void)

{

If(1==TM[LIN_TIMESLOT_MS].overflow_flag){

TM[LIN_TIMESLOT_MS].overflow_flag=0;

If(Cur_sch_item==&l_sch_table_main[l_MAIN_SLOTS-1]){

Cur_sch_item=l_sch_table_main;

}else{

Cur_sch_item++;

}

Cur_frame.state=l_IDLE;

Cur_frame.done=0;

Cur_frame.error=0;

If(Cur_sch_item->pid!=l_Freepid){

l_SendBreak();

}else{

;

}

TimerStart(LIN_TIMESLOT_MS, Cur_sch_item->ticks,0,1);

}

}

The application layer functions also include the sleep and wake functions, which are not described here.

Conclusion

The LIN protocol driver module implemented in this paper can be easily integrated into the application, and is independent of the specific processor and the operating system used. It has good portability and good practical value and reference significance.

Wall Socket Expansion allowing you to expand one in wall socket to multi outlets, as well as to add more functions, e.g. socket plus USB ports, or with surge protector, or with overload protection.

- Multiple Outlets allowing you to plug in multiple electrical devices.

- Compact Wall Expansion is also convenience as travel power adapter from its weight and size point of view.

- PC/ABS flame retardant, phosphor bronze+pure copper.

- Space saving and cost saving

We have been doing OEM service for Germany customer since the very beginning time when we started power strip manufacturing.

  Now we still continue with OEM/ODM services and doing our own design as well.

Own factory


Wall Socket Expansion

Wall Socket Expansion, Compact Wall Expansion, USB Wall Socket, Multi Outlets Wall Socket

ZhongShan JITONGLONG Plastic Hardware Co. Ltd. , https://www.toukoo-electronics.com