“In today’s increasingly widespread application of network technology, network transmission is the most cost-effective way of data transmission. How to use the cheap 51 single-chip microcomputer to control the network card chip for data transmission, load the TCP/IP protocol to connect to the Internet, and realize network communication has become the goal of many designers. However, due to the constraints of instructions and resources, the implementation process will have many difficulties. We abandoned the resource-consuming high-level protocol in the design scheme, and adopted the method of sending small data packets to avoid fragmentation, to simplify the TCP protocol and the UDP protocol, and realize interconnection access.
“
In today’s increasingly widespread application of network technology, network transmission is the most cost-effective way of data transmission. How to use the cheap 51 single-chip microcomputer to control the network card chip for data transmission, load the TCP/IP protocol to connect to the Internet, and realize network communication has become the goal of many designers. However, due to the constraints of instructions and resources, the implementation process will have many difficulties. We abandoned the resource-consuming high-level protocol in the design scheme, and adopted the method of sending small data packets to avoid fragmentation, to simplify the TCP protocol and the UDP protocol, and realize interconnection access.
Hardware Design and Implementation
The hardware structure block diagram of the system is shown in Figure 1. The microcontroller of this system is 78E58 of Winbond Company, and the network interface chip is RTL8019AS of ReaLTEk Company which is compatible with NE2000 series. The RTL8019AS has a built-in 10BASE-T transceiver, an external isolation LPF filter, and outputs through the RJ-45 interface. External RAM is 62256, 24C02 is EEPROM for I2C bus.
Figure 1 Embedded protocol conversion hardware block diagram
System software design and implementation
In order to meet the needs of Internet access, the system software design mainly includes two parts: one is to perform the control functions of RTL8019AS, etc., and the other is to perform functions related to connecting to the Internet and realize the TCP/IP protocol. This article focuses on the second part, the main program is written in C51 language.
RTL8019AS initialization
To connect the embedded system to the Ethernet, the working mode and working state of the RTL8019AS should be set first, the buffer for sending and receiving data should be allocated, and the receiving and sending of the Ethernet frame should be completed by reading and writing the address and data port. Then set the working parameters of RTL8019AS, that is, set the internal control register. After setting the working parameters of the RTL8019AS, enter the normal working state, and then read and write the RAM of the RTL8019AS to complete the reception and transmission of data packets. Due to the limited space, it will not be described in detail here.
TCP/IP model
The TCP/IP protocol is a set of protocols that interconnect various systems on the Internet to ensure accurate and fast transmission of data on the Internet. TCP/IP usually adopts a simplified four-layer model: application layer, transport layer, network layer, link layer.
In this system, the application layer transmits the data from the Ethernet and the data terminal, and packs and unpacks the datagram. The transport layer uses the Transmission Control Protocol TCP or the User Data Protocol UDP. The network layer implements the IP protocol, and also implements the ICMP protocol that can report data transmission errors and so on. The link layer part is completed by RTL8019AS, and the link layer is composed of the underlying protocol that controls the data transmission between different machines on the same physical network.
In the single-chip microcomputer, only the parts related to the needs are realized, and the protocols that are not used are not supported at all. Most of the TCP/IP protocol used by the single-chip microcomputer is to complete data acquisition and data transmission, without the functions of web browsing and file transmission.
ARP Protocol (Address Resolution Protocol)
Ethernet is a local area network technology mainly used by the TCP/IP protocol, and is the basis for the system to access the Internet. The essence of ARP is to complete the dynamic mapping of network addresses to Ethernet physical addresses. The ARP protocol of UNIX system supports Ethernet, Token Ring and other networks, but our single-chip system only supports Ethernet.
IP Protocol (Internet Protocol)
IP is the most core protocol in the TCP/IP protocol suite. All TCP, UDP, ICMP and IGMP data are transmitted in IP datagram format. For some protocols, IP packets can be up to 65K and can be transmitted in segments, but such large data packets cannot be accommodated in a single-chip microcomputer, so segmentation is generally not supported. Our design uses sending small packets to avoid fragmentation.
TCP protocol (Transmission Control Protocol)
The TCP data is encapsulated in an IP datagram and has its own TCP header. The definition of the TCP protocol is very complicated. In view of the limited on-chip resources of the 51 microcontroller, this system simplifies the TCP protocol to a certain extent. The standard TCP protocol uses a slow-start sliding window mechanism, and if only a single window is used, it becomes a simple acknowledgment method. That is, it only needs to send and confirm a single datagram, which saves system resources and makes maintenance more convenient.
Another difficulty in programming the TCP protocol lies in the realization of the specific process of TCP connection establishment and termination. The TCP protocol is a connection-oriented protocol. No matter which side of the connection sends data to the other side, it must first establish a connection between the two parties through the “three-way handshake” process, and terminate the connection through the “four-way handshake”.
Once the connection is established, TCP can send blocks of data, called segments. When TCP sends a segment, it starts a timer and waits for the destination to acknowledge receipt of the segment. If an acknowledgment cannot be received in time, the segment will be resent. In addition, TCP will keep checksums of its headers and data.
Implementation of the main application program of the system
After the system is initialized, it enters two parts of the main program loop: one is to unpack the received Ethernet data frame for use by the application program; All computers in the network can receive this data frame. Figure 2 is a flow chart of the main application of the system.
Design of 51 single chip microcomputer to control network card chip for data transmission
Difficulties in Realizing TCP/IP Protocol with Single Chip Microcomputer
The source code of TCP/IP protocol implemented on UNIX or Windows cannot be directly transplanted to 8-bit MCU due to the program space, available memory RAM, operation speed and instruction set of 51 MCU. Writing code on the 51 single-chip microcomputer will be subject to many restrictions, especially the realization of complex programs such as the TCP/IP protocol. We must explore the performance of the 51 single-chip microcomputer as much as possible according to the actual situation. In general, the implementation of single-chip microcomputer and UNIX implementation of TCP/IP have the following differences:
(1) Operating system: Windows or UNIX are multi-tasking operating systems, which simplifies the code writing. In the single-chip microcomputer, it can only be a single-tasking system, and the code structure is sequential execution + hardware interrupt, which cannot be executed concurrently.
(2) Memory allocation: The memory allocation of Windows or UNIX is dynamic. The general microcontroller only has an external 32K-byte RAM, which is used by various protocols at the same time. A largest Ethernet packet has 1.5K bytes, and it takes 1.5K bytes to allocate a packet buffer. To this end, we allocate a fixed RAM of 256×6=1536 bytes to store the received Ethernet packets. Process a package as you receive it.
(3) Pointer: All programs in the PC must be placed in RAM before they can be run, so its pointers all point to RAM. The structure of a single-chip microcomputer is very different from that of a PC. There are many types of pointers, and the speed of each pointer operation is also different. In particular, the “general pointer” operation is very slow, and it will take up a lot of program space. In the source code of UNIX to realize TCP/IP, the most used pointer is the pointer, and in the microcontroller, it is generally required to use less pointer, or use a specific type of pointer. Many changes are required to source code using UNIX.
(4) Parameter transfer: In the TCP/IP source code implemented by UNIX, there are generally many parameters to be transferred, while the parameters allowed to be transferred in the single-chip microcomputer are limited (because it is limited by the internal RAM), and the process of parameter transfer requires The program code space is wasted, and the execution speed of the microcontroller is also reduced. Therefore, in the implementation of single-chip microcomputer, generally do not do too much parameter transfer, but use public global variables to realize the calling process.
(5) Hardware interface: In UNIX or Windows, the network card driver is all interrupted without exception, because the processing speed of the PC is fast, and the processing time of one interrupt is also very short, which will not affect other interrupts in the system. In the application of single-chip microcomputer, most of the schemes are query type. The NE2000 network card of the PC generally uses 16-bit DMA, but only 8-bit DMA is used in the microcontroller. This also makes UNIX’s code for the network card driver not directly portable.
Epilogue
The embedded network access scheme designed in this paper adopts cheap 8-bit 51 single-chip microcomputer to realize simplified TCP protocol and UDP protocol, and supports active and passive connection, cross gateway, and realize Internet access. A transparent transmission channel is provided between the two devices, and users can enjoy the benefits of the network without any modification to the original serial device or other digital devices. At present, the system in this paper has been successfully used in networked data collectors.