Implementation of Serial Port Protocol Networking Using Single Chip Microcomputer

Publisher:huanhuiLatest update time:2011-08-25 Source: 维库 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The RAM area of ​​the network card is shown in Figure 2. When we set PSTART and PSTOP, the receiving area is determined to be between 46 and 7F, and the remaining 6 pages are used as the sending buffer, which can meet the requirement of sending the maximum Ethernet data packet with a capacity of 1514 bytes at a time. The initial value of CURR is set to 47, and the initial value of BNRY is set to 46. When a new data packet is received (for example, 3 pages), the network card puts them in pages 47, 48, and 49 in turn, and CURR automatically points to page 4A. When we finish reading the data packets of these 3 pages, we need to change BNRY to 49 to notify the network card that the data packet has been read.

After the network card chip receives the Ethernet data packet, it is stored in the page pointed to by CURR. A data packet can occupy one page or multiple pages. The packet header of the received packet contains the information of the packet. The format of the packet header is shown in Table 1:

After reading the packet header, we can know the receiving status of this packet, the packet length, and the position of the next packet. In the receiving loop, check the status of the interrupt status register ISR. If a new packet is found, read the packet header information first, then read the entire packet according to the packet header instructions, then rewrite BNRY, and then read the next packet, and so on until the CURR position is reached. Note that a packet may occupy the first and last pages of the receiving buffer, so be careful when reading.

Sending data packets is relatively simple. Use DMA to transfer the prepared data to the send buffer, then set the send length to TBCR1 and TBCR0, and then set the transmission start page, that is, set TPSR=0x40. Finally, set CR=0x26 to start the transmission.

After the transmission is completed, the transmission completion flag of the interrupt status register ISR should be cleared. It is worth noting that the packet length of the transmission packet cannot be less than 60 bytes specified by Ethernet, otherwise the network card will not send it.

3 TCP/IP protocol software design on single chip microcomputer

3.1 TCPIP protocol stack and link layer format

The Internet uses the TCP/IP protocol suite, which consists of four layers from bottom to top: data link layer, network layer (IP), transport layer (TCP), and application layer. What we can implement on the microcontroller is the function of the data link layer. Due to the limited resources of the microcontroller, the upper layer protocol can only implement some functions. The TCP/IP protocol stack is shown in Table 2:

The data link layer is at the lowest layer of the protocol stack and transmits the physical transmission frame of Ethernet. Its frame format is shown in Table 3:

The data link layer is the basis of all TCP/IP packets. All packets above it are encapsulated into the data segment of the link layer frame. The link layer is MAC-to-MAC communication.

3.2 Implementation of PING command on MCU

PING is a command of the network layer (IP layer), which is the communication between IP and IP. The host sends a status request to a target IP address, and the latter sends a response. In this way, it can be used to check whether the line between the two is unobstructed. There are two network layer protocols involved here:

ARP and ICMP. First, the MAC address of the target IP must be known in the network before data can be sent. In order to obtain the address, we send an ARP broadcast packet to the entire network, asking for the MAC address corresponding to the IP, and then the target IP responds, and we get the MAC address from the response information. Secondly, send an ICMP packet to request the target IP status, and the target IP responds, and the network connection test is completed. In our experiment, the PC host in the network sends the PING command, and our microcontroller system receives the signal and generates a response. We assume that the IP of the network card controlled by the microcontroller system is 192.168.0.176.

The first step is to implement the ARP protocol. ARP protocol is the abbreviation of "Address Resolution Protocol", and its function is to convert IP addresses into physical addresses (commonly known as MAC addresses). The packet format of protocol ARP is shown in Table 4:

When the main processor of the microcontroller processes the ARP broadcast request received by the network card, if it finds that it is requesting the MAC address of "192.168.0.176", it will send an ARP reply packet as required, put its own MAC address in the reply packet, and send it back to the network to complete the first step of PING. Note that an 18-byte patch must be added to the end of the reply packet, otherwise the reply packet length will not meet the minimum 60-byte requirement.

The second step is to implement the ICMP protocol. ICMP is the abbreviation of "Internet Control Message Protocol". It is used to transmit control messages between IP hosts and routers. Control messages refer to network messages such as whether the network is connected, whether the host is reachable, and whether the route is available. Although these control messages do not transmit user data, they play an important role in the transmission of user data.

ICMP is encapsulated in the IP protocol, so there is an IP packet header.

The ICMP protocol structure is shown in Table 5:

After receiving the ARP reply packet, the network host extracts the MAC address and then sends an ICMP request to the MAC. When the main processor processes the request, it fills the request packet's identifier and sequence number into the ICMP reply packet, and fills in the other parts as required. There is a mature formula for calculating the checksum, which is easy to calculate. Just set the checksum part to 0 first, and then fill the calculation result of the packet header into the checksum.

Finally, the ICMP reply packet is sent to the network, thus completing the whole process of PING. At this point, the network host that issued the PING command will receive the information "Replyfrom192.168.0.176:bytes=32time<10msTTL=128".

4 Discussion on data exchange between serial data and network

Above the network layer, there are two transport layer protocols: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). For situations with high data transmission requirements, the TCP protocol needs to be used as a two-way communication method, but this method is relatively complex. For the transmission of some real-time signals and their reverse control signals, the UDP protocol can meet the requirements. Table 6 is the UDP protocol structure:

When the original data is sent from the serial port to the main processor, the main processor packages it into a UDP message and sends it to the network. The network monitoring host receives and processes the UDP message, and then sends the control information in the same UDP message. The main processor returns the network control signal to the serial port. In this way, we can easily complete the monitoring and control process of the on-site serial port data source on the Internet.

5 Conclusion

Because the MCU has good access and control capabilities for various electrical equipment, after we realize the network access function of the MCU, we have built a bridge between the hardware equipment of the electrical appliances and the network. Through it, we can realize remote monitoring of various real-time signals, instruments, household facilities and other targets, unify the management of various equipment on a macro level, and realize the harmonious interaction between people and the system. This will surely greatly improve work efficiency, improve the working environment, and enhance people's production and living standards.

This paper studies in depth the use of single-chip microcomputer to control the network card, access Ethernet, that is, IEEE802.3 protocol network, to achieve the networking of serial port instruments.

1 Hardware structure and circuit design

1.1 Overall Design

The whole system design uses ATMEL's 8-bit general-purpose microcontroller AT89C51 as the main processor, driving REALTEK's 10M Ethernet control chip RTL8019AS to achieve serial port data and external network interconnection.

1.2 Network card wiring design

RTL8019AS is a 100-pin PQFP package with an operating voltage of 5 V. Its wiring schematic is shown in Figure 1. Address SA0-4 is connected to the lower five bits of P2 of the microcontroller; SA8 and SA9 are connected to the power supply; the remaining 13 pins SA5-7, SA10-19 are all grounded; IORB and IOWB are connected to the read and write signal terminals of the microcontroller respectively; RSTDRV is connected to P1.2; the 8-bit data SD0-7 is connected to the P0.020.7 pins of the microcontroller in sequence; TPOUT+ and PTOUT- are the sending pin pair, connected to the sending pins 1 and 2 of the RJ45 port; and TPIN+ and TPIN- are the receiving pin pair, connected to the receiving pins 3 and 6 of the RJ45 port; a 20M crystal oscillator and a grounding capacitor are connected between pins X1 and X2; LED0 and LED1 are connected in series with a light-emitting diode and a 1k8 resistor respectively, and connected to a 5V power supply; IOCS16B pin is connected in series with a 27k8 resistor and grounded; pins BD0-3 (IOS0-3) are responsible for defining the base address position, all of which are left floating as 0 input.

LED0 indicates communication conflict COL by default, and LED1 indicates receiving data packets. The sending pair and receiving pair cannot be directly connected to the RJ45 plug, but must be connected to the RJ45 through an isolation voltage module (select 20F-01). The network card has a 16-bit data line, and data can be transmitted in 16-bit or 8-bit mode, making the IOCS16B pin low. We selected the 8-bit mode.

1.3 Serial interface part

1 The serial port part uses MAX232 and 9-pin serial port. The microcontroller P3.0/RXD0 and P3.0/RXD0 are connected to the 2nd and 3rd pins of the serial port through the MAX232 chip. The 5th pin of the serial port is grounded.

The serial port selects working mode 1, and the baud rate calculation formula is:

When the serial port is at 9600 baud rate, the crystal oscillator is 11.0592M, the preset value is 0xFD, smod=0; the crystal oscillator is 16MHz, the preset value is 0xF7, smod=1.

2 Network card initialization and working process

2.1 Control method of network card chip RTL8019AS

The control of the network card chip RTL8019AS is realized by reading and writing the 32-byte control register group on the chip. In addition, the chip contains 16kbyte RAM, the address is 0x400020x7fff. These RAMs cannot be directly addressed by the microcontroller, and must be read and written in DMA mode through the 32-byte control register group.

The 32-byte control register group can be directly addressed by the microcontroller, but its base address is configured through pins BD0-3 (IOS0-3). In the circuit, all four pins are left floating and all 0s are input. The product data shows that its base address is 300H.

The 32-byte control register group is divided into 4 pages. The 00H register is called Command Register (CR). The highest two bits of CR represent which page the register is currently in. 01H to 0FH have different meanings in different pages. At the same time, even in the same page, the meanings of read and write may be different. This is worth noting. 10H217H is the remote DMA port, and 18H-1FH is the network card reset port.

2.2 DMA Data Channel

The RTL8019AS is internally divided into two parts: the remote DMA channel and the local DMA channel. The local DMA completes the data exchange between the controller and the network cable, and the remote DMA completes the data exchange between the main processor and the network card. The main processor of the microcontroller only needs to operate the remote DMA to send and receive data. When receiving data, the data received by the RTL8019AS is compared by MAC and CRC, and then stored in the receiving buffer by FIFO. After receiving a full frame, the main processor is notified by interrupt or register flag, and the main processor reads it out through the remote DMA channel. When the main processor wants to send data to the Ethernet, it first sends a frame of data to the send buffer in the RTL8019AS through the remote DMA channel, and then issues a transmission command; after completing the transmission of the previous frame, the RTL8019AS completes the transmission of this frame.

The so-called DMA is Direct Memory Access. The ordinary addressing method is to give an address and then retrieve the corresponding value. In DMA mode, we specify a register address, and the host can retrieve or write a large amount of data by repeatedly reading or writing this address. The remote DMA port specified by the network card is used for this purpose. After setting the starting address and the number of bytes to be read, we repeatedly read the remote DMA port, and then we can read the data packet received from the network cable in the network card from the RAM area of ​​0x400020x7fff; conversely, after setting the starting address and the number of bytes to be written, we repeatedly write to the remote DMA port, and then we can send the data to the send buffer RAM of the network card, and then issue a send command to send the data packet to the network cable.

2.3 Network Card Initialization Process

Reset, make RSTDRV high first and then low, note that there should be a 100ms delay in each step to ensure a successful reset. Then perform a hot reset, which is to read and write the network card reset port in sequence.

Set CR=0x21, stop the chip and select page 0.

Set RBCR1=0; RBCR0=0 to clear the number of transfer bytes of remote DMA operation.

Set PSTART=0x46;PSTOP=0x80;BNRY=0x46 to set the receiving start page, end page and boundary page.

Set IMR = 0x0 to clear the interrupt mask register.

Set RCR=0x08+0x04, set the receive configuration register, and allow receiving multi-address and broadcast messages.

Set TCR=0, set the transmit configuration register, and use the default configuration.

Set DCR=0x80+0x40+0x00, set the data configuration register, and select byte DMA.

Set ISR=-1, set the interrupt status register, and clear all existing interrupts.

Use DMA to get the MAC address of this network card from 0000H2000BH. Note that each byte of the MAC address is stored repeatedly, so just take the odd bytes 1, 3, 5, etc.

Set CR=0x61 and select page 1.

Put the MAC address just obtained into PAR0-5. The network card checks whether the received packet matches the MAC value in PAR, and then decides to accept or discard it. Therefore, you can also set this PAR value arbitrarily to forge your own MAC value.

Set MAR02MAR7 to 0xFF to allow reception of all multi-address packets.

Make CURR = 0x47 and set the current receiving page to 0x47.

Set CR=0x22, start the network card chip, and start the receiving and sending process.

2.4 Network card working receiving and sending process

The NIC RAM is stored in a page structure with 256 bytes as one page. The high 8 bits of the 16-bit RAM address are also called the page number. The NIC's 16k RAM address is from 0x400020x7fff, from page 0x40 to page 0x7f, a total of 64 pages. 64 pages are used to receive and send data packets. Both reception and transmission are performed in the smallest unit of page. The receive buffer needs to be defined, and the rest can be used as the send buffer.

The receive buffer is determined by two registers: PSTART (PageStartRegister) and PSTOP (PageStopRegister). After the receive buffer is set, the buffer forms a circular queue. There are two registers CURR and BNRY that control the receive buffer. CURR is the pointer of the network card write buffer, pointing to the page to be written; BNRY is the read pointer, pointing to the page that the user has read.

BNRY cannot exceed CURR, otherwise the data not read by the user will be overwritten. After the user sets CURR, you don't have to worry about it. The network card will automatically modify it after receiving new data. After the user reads the data, the value of BNRY must be modified to notify the network card that the data has been read.

Reference address:Implementation of Serial Port Protocol Networking Using Single Chip Microcomputer

Previous article:Interface technology and application of LCD module MGLS-12864
Next article:Wireless temperature monitoring system based on ATmega16 microcontroller

Latest Industrial Control Articles
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号