Design and implementation of layered serial frame communication in embedded environment

Publisher:BlissfulJoyLatest update time:2013-02-25 Source: dzscKeywords:S3C2440 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

      There are usually two ways of communication between embedded systems: parallel communication and serial communication. The parallel method has a fast data transmission speed, but occupies more communication lines. The reliability of data transmission decreases with the increase of distance, and it is only suitable for short-distance data transmission. In long-distance data communication, serial communication is generally used, which has the advantages of occupying less communication lines and low cost. At present, RS 232 serial port is the most widely used serial interface in PC and communication industry. It is used in point-to-point communication mode. In actual use, the simplest three-wire connection is often used, that is, the serial ports of the devices at both ends are only connected to the three wires of receiving, sending and grounding, so that simple full-duplex communication can be realized. The communication protocol is the language for data exchange between the devices at both ends and is the guarantee of communication reliability. Under the premise of ensuring the function, the communication protocol should be simple.

  1 System communication requirements

  This system mainly completes the functions of time interval measurement and transient data collection in the field environment. Each module in the system uses Samsung's S3C2440 chip as the processor, and the operating system uses embedded Linux. The main tasks of inter-module communication are the issuance and response of control commands, reporting of working status and collected data, etc., with high requirements on communication reliability and no data encryption requirements.

  According to the system hardware and software conditions, the serial port working parameters are set as follows: 115 200 baud rate, 8 data bits, 1 stop bit, odd parity, no flow control. The baud rate setting needs to comprehensively consider the serial port performance of the selected chip, the length of the serial port connection line, the maximum frame length of the transmitted data, and the bit error rate during the application process; no flow control is because the serial port uses a three-wire connection.

  2. Layered Structure of Communication Protocol

  In order to ensure the universality of communication protocols between different devices and reduce the complexity of implementation, the communication protocol is divided into two layers: the upper layer is the application layer, which standardizes the application layer data format used by application programs for communication between devices; the lower layer is the link layer, which provides the sending and receiving of physical line data, application layer data splitting and merging, encapsulation and decapsulation, and error detection functions. The application layer part of the protocol uses different data formats according to the different functions of each device, while the link layer part is completely universal. The application layer communication process is shown in Figure 1.

Application layer communication process
Figure 1 Application layer communication process

  The sending device constructs the application layer data according to the agreed application layer data format, and submits it to the link layer for data splitting, encapsulation, and verification, and then sends the generated data to the physical line in the form of data frames; the receiving device receives data from the physical line, performs frame positioning, decapsulation, error detection, data merging, etc., and finally submits the application layer data to the application layer for processing. The application layer of the sending and receiving devices can continuously modify the application layer data format according to changes in user needs, and use the functional interface provided by the link layer to complete the communication function. Therefore, the key to the design of the communication protocol lies in the link layer. The following focuses on the design and implementation of the link layer.

  3 Link Layer Design

  The link layer mainly includes the following functions: data splitting and merging, data encapsulation and decapsulation, sending and receiving of data frames, and error detection and retransmission mechanism.

  3.1 Data Splitting and Merging

  Data splitting is to divide the overlong application layer data into several parts, send them in multiple data frames, and then merge the data after receiving them, and hand them over to the application layer for processing. If the overlong application layer data is not split, it may cause the data frame to exceed the designed buffer size, or it may cause the sending time to be too long, resulting in a timeout error. This length needs to be reasonably set according to actual needs. When an error occurs in the data frame transmission, the frame of data needs to be retransmitted. If the length is too large, it will cause a large overhead; if the length is too small, the proportion of overhead bytes generated during encapsulation will be too high, affecting the transmission efficiency.

  3.2 Data encapsulation and decapsulation

  Data encapsulation is to add function indication, data length and other fields to the split application layer data in a certain format so that the other party knows how to process it after receiving it. The data encapsulation format and the meaning of the function indication field are shown in Table 1 and Table 2.

Table 1 Table 2

  3.3 Frame sending and receiving

  The link layer sends and receives data in frames. A common method to define the start and end of a frame is to add special start and end codes to the head and tail of the data to be sent. If this code pattern appears in the data, it must be escaped before the data is sent to convert it into other code patterns, otherwise it will cause frame positioning errors and data communication failure. Many protocol implementers do not perform this escape for the sake of simplicity, which poses a risk of communication failure. In fact, an escape processing method is described in the Point-to-Point Protocol (PPP protocol). After simplification, it is not complicated to implement. The description is as follows:

  The data sender sends 0x7E as the start code at the beginning of the frame, and sends the encapsulated data byte by byte. When encountering 0x7E, it sends a byte sequence of 0x7D and 0x5E. When encountering 0x7D, it sends a byte sequence of 0x7D and 0x5D. Finally, it sends 0x7E as the end code at the end of the frame. [page]

  The data receiver searches for the first 0x7E in the serial port data stream as the start of the frame (for consecutive 0x7E, the last one is the start of the frame), receives the data byte by byte, and when it encounters 0x7D, it skips it and adds 0x20 to the next byte of the byte until it encounters 0x7E, which is considered the end of the frame.

  Before the link frame is sent, the CRC16 algorithm should be used to check the encapsulated data. The check polynomial is, and the check value is written into the check field. After the link frame is received, it is checked first. If the check succeeds, the data is decapsulated. If the check fails, it is resent according to the following retransmission mechanism.

  3.4 Error Detection and Retransmission Mechanism

  Considering the simplicity of protocol implementation and the reliability of data transmission and reception, it is decided to use the stop-and-wait protocol for data transmission and reception. The process is as follows:

  After sending a data frame, the sender sets a maximum waiting time to wait for the other party's confirmation frame or rejection frame. If a confirmation frame is received, the next frame is sent; if a rejection frame is received or no confirmation frame or rejection frame is received within the timeout period, the current frame is resent. The frame resent due to waiting timeout must set the timeout indication bit. If a rejection frame is received three times in a row or a timeout is resent three times in a row, the other party is considered unreachable, the sending of the current frame is canceled, and the error is reported to the application layer.

  After the receiver receives the data frame, when the timeout indication bit is 0, if the check is correct, a confirmation frame is sent and the frame is processed. If the check is wrong, a rejection frame is sent and the frame is not processed. When the timeout indication bit is 1, it means that the other party has not correctly received the confirmation frame or the rejection frame. If the check result of the last non-retransmitted frame is correct, the frame has actually been processed and a confirmation frame can be sent directly. If the check result of the last non-retransmitted frame is wrong, the frame is processed normally according to the check result.

  4 Link Layer Implementation

  The link layer is implemented in C++ language to facilitate code reuse in various module programs. The application layer data sending and receiving process is shown in Figure 2 and Figure 3.

Application layer data sending flow chart
Application layer data receiving flow chart

  5 Conclusion

  The role of the link layer is to reliably send the application layer data to the peer device, but if that is all, the application is not very convenient to use. If the object-oriented programming method is used to encapsulate the link layer code in a class and provide the application with some relatively simple functional interfaces, such as sending data, receiving data, and detecting whether the peer is reachable, the usability problem can be well solved. In addition, when data transmission fails, the application should be notified by return value or event. When there is application layer data to be processed, it is best to activate the application layer handler by callback function or event to avoid inefficient loop detection of the application. By optimizing both the protocol design and protocol implementation, the protocol has shown excellent reliability and certain versatility in actual application, which can be used for reference.

Keywords:S3C2440 Reference address:Design and implementation of layered serial frame communication in embedded environment

Previous article:Embedded Internet Video Application Development Based on IPv6
Next article:Design of water-saving irrigation system based on ZigBee wireless sensor network

Recommended ReadingLatest update time:2024-11-16 14:42

ARM History 4 - LCD
    It has been 10 days since I last wrote about my journey. It was the National Day holiday, so I gave myself a few days off - I played some games, chess, etc.     In fact, it took a lot of time and brainpower to write the touch screen driver and understand the interrupt process in ARM. I will simply share the cond
[Microcontroller]
S3C2440 Architecture
This article is a study of the ARM processor architecture, targeting the S3C2440 model. It refers to the content of the PROGRAMMER'S MODEL section in Samsung's official technical document S3C2440.pdf. ARM and THUMB instruction modes S3C2440 uses armv4t instruction set, and supports both arm instruction set and thumb i
[Microcontroller]
S3C2440 FCLK, HCLK, PCLK configuration
The FLCK value of the official wince system of Samsung is 400MHz, the HCLK value is 100MHz, and the PCLK value is 50MHz. So how are these values ​​calculated? The general process is as follows: these values ​​are multiplied to the core frequency we need, such as 400MHz, based on the external crystal oscillator 12MHz
[Microcontroller]
S3C2440 FCLK, HCLK, PCLK configuration
S3C2440 NAND Flash Controller
First, let's start the NAND Flash process. Because NAND Flash does not support programs running inside NAND, we need to move NAND to memory to run. Since the SRAM inside S3C2440 is only 4K, this method will not work when the program is much larger than 4K. So we can move the program after 4K in NAND to SDRAM to run.
[Microcontroller]
S3C2440 NAND Flash Controller
s3c2440 bare metal - nandflash programming - 2 - nand controller and access timing
How simple, nothing more than read, write and erase, like our nand data width 8bit, one cycle is more than enough. But the address is different, for example, this nandflash capacity is 256M = 2^28, then 28 data lines are needed to transmit in one cycle, but the data bus width of this nandflash is only 8bit, there ar
[Microcontroller]
Transplantation of DM9000 network card driver based on S3C2440
0 Preface   Ethernet is a computer LAN networking technology. In a LAN, multiple nodes share the transmission medium. This requires some mechanism to determine which device occupies the transmission medium to transmit data at a certain time. Therefore, the link layer of the LAN must have the function of media access c
[Microcontroller]
Design of ECG monitor based on Linux and MCU
  As people's pace of life accelerates and the population gradually ages, heart disease has become one of the major diseases that endanger human health and life. The ECG monitoring system provides an effective means for the diagnosis and treatment of heart disease patients, and is of great significance to the preventi
[Microcontroller]
Design of ECG monitor based on Linux and MCU
LCD display of ARM9 (S3C2440) - theoretical knowledge
      Today I would like to discuss the LCD display problem of S3C2440 with you. I hope you can give me more advice. If I say something wrong, I hope you can help me correct it in time so that I can increase my knowledge and not cause inconvenience to others' learning. Haha Let me first take a look at an article I f
[Microcontroller]
LCD display of ARM9 (S3C2440) - theoretical knowledge
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号