Design and Implementation of Network Interface Module Based on ax88796b
| [Date: 2008-7-22] |
Source: Today's electronics/21ic Author: Cui Wei, Chongqing University of Posts and Telecommunications |
[Font: large, medium, and small] |
Introduction
Today, with the increasing popularity of network technology, industrial field instruments, data collection and control equipment are also increasingly networked, and the combination of industrial automation and network is becoming closer and closer. The combination of Ethernet and embedded systems has become a trend in industrial control.
Ax88796b is a low-pin Ethernet Control Chip developed by asix for embedded and Industrial Ethernet applications. It is integrated with a 10/100 Mbit/s adaptive media access layer Mac and physical layer transceiver PHY and 8 K × 16 bit SRAM. Ax88796b can be applied to commercial 0 ~ 70c and industrial-40C ~ The temperature range of 85c. Ax88796b supports MCS-51 series, 80186 series, ISA bus, and SRAM-like interface types. Implements 10 Mbit/s and 100 Mbit/s Ethernet control functions based on the IEEE 802.3/802.3u LAN standard, and provides the send queue function to enhance the sending performance of the standard ne2000.
AT91R40008 microcontroller is an embedded microcontroller based on ARM7TDMI core, which is very suitable for industrial real-time control applications. The lqfp package of the ax88796b pin reduces PCB space and is compatible with ne2000. It is easy to program and its driver can be easily and quickly transplanted to many embedded systems. Therefore, based on the advantages of the two chips, we chose ax88796b and AT91R40008 as the solution for embedded network applications in Real-Time Industrial Control, which has good industrial application values and prospects.
Figure 1 ax88796b Internal Structure
Ax88796b internal structure and working principle
1 ax88796b Internal Structure
When the NIC is powered on and reset, The eeprom interface can access data through the SMI/EEPROM register with a 14 h offset in the CSR. the MAC address is read from the EEPROM and is automatically loaded to the memory H ~ 001fh and 0400h ~ In the internal storage area of the 040fh. If the EEPROM is not used, manually assign a MAC address to the ENI each time it is powered on.
Ax88796b has a 16 KB (0x4000 ~ 0x7fff)-sized SRAM, which is a dual-port SRAM that can be read and written by both the nic and the user. Part of this address space is reserved for storing a buffer ring structure, the other part can be used by users. In this design, 16 KB of SRAM is used to receive and send packets. Local DMA uses this buffer ring structure to receive packets. This structure consists of a series of adjacent buffer with a fixed length of 25 6B, each 256 bytes is called a page, a total of 64 pages. The page address is the 8-bit high address, and the page address is 0x00 ~ 0xff. 0x40 ~ 0x4b is the sending buffer of the NIC. There are 12 pages in total, and two Ethernet packets can be stored. Therefore, we divide the sending buffer into two parts for sending in turn. Use 0x4c ~ 0x7f 52 pages are the receipt buffer of the NIC. The SRAM space structure and the receiving and receiving Buffer Structure of ax88796b are shown in 2 and 3.
Figure 2 SRAM Space Structure
Figure 3 sending and receiving buffer
2 How ax88796b works
Four registers are used to control the operation of the receiving buffer. Page Start Address Register (PSTART) and Page Stop Address Register (PSTOP) define the starting and ending Page numbers of the receiving buffer to form a receiving buffer ring. The Current Page address Register points to the first buffer for storing data packets. Boundary Pointer Register points to the First unread packet in the buffer ring. If the local DMA address is equal to the Boundary Pointer Register value, the receiving process is aborted. The boundary pointer is also used to initialize the remote DMA to move a data packet. When the data packet is removed, the boundary pointer also grows forward. In this way, the Current Page Register is like a write Pointer, and the Boundary Pointer is like a read Pointer. When a data packet is received, it is stored starting from the page number specified in the SRS. If a 256B buffer is insufficient, DMA executes a forward connection to connect to the next buffer to store the rest of the data packet. The buffer must be continuous during connection, and a data packet is always stored in the adjacent buffer. Before connecting to the next buffer zone, the buffer management logic performs two comparisons. First, compare whether the DMA address of the next buffer zone is equal to the page Stop Register. If it is equal, the DMA address is restored to the address saved in page start address register. Second, it is compared with boundary pointer register. If it is equal, the receiving packet ends. Boundary Pointer Register can prevent data in the buffer from being overwritten before being read.
To send and receive data packets, you must read and write the internal SRAM of AX88796B through DMA. DMA is divided into Remote DMA and Local DMA ). The CPU reads and writes data from the SRAM of the NIC, which is called remote DMA. AX88796B read/write SRAM is called local DMA. Packet receiving process: When the packet arrives at the MAC layer of the network adapter, the local DMA first transmits the packet to the memory of the network adapter, the host CPU then reads data packets from the NIC memory through the remote DMA channel. Packet sending process: the host CPU sends the packet to be sent to the memory of the NIC through remote DMA, and the local DMA sends the packet to the MAC layer, then, it is sent to the network through the internal PHY layer.
Network Interface Hardware Design
1 ax88796b pin configuration
AX88796B can be connected to the CPU in MCS51, ISA, and 186 modes. This solution uses the ISA 8bit mode. Chip selection pin CSn, read and write signals RDn, WRn, RSTn are low level effective, respectively, and AT91r40008 corresponding PIN to connect. AX88796B data cable SD0 ~ The SD15 is connected to the AT91r40008 DATA [] and can be configured as a 16-bit or 8-bit dma data transmission mode through software. The IRQ of AX88796B is programmable. You can configure BTCR or EEPROM to select the trigger mode and interrupt I/O buffer type for the interrupt request signal, the AT91r40008 interrupt trigger method should be consistent with the configuration of BTCR. Other important pins such as IOIS16, PME, EECE, and EECK are suspended in this design. Pin configuration should be based on the NIC chip manual instructions and design solutions.
For operations on the AX88796B control status register (CSR), you need to determine the base address. The selection of the base address should be determined according to the address and chip selection signal of the AT91r40008 programmable External Bus EBI. The EBI processing of AT91r40008 is located in the address space 0x00400000 ~ 0xFFC00000 access operation, during the access process, it will generate control access signals for external devices. After the NIC Register address space is mapped to EBI, you can directly perform operations on the EBI address space to control the NIC register read/write. For each peripheral device mapped to the EBI interface, you can program the number of wait cycles, data floating time, and data bus width (8-bit or 16-bit.
2 AT91R40008 and ax88796b Interface Circuit
Figure 4 hardware connection schematic
Circuit 4.
Ax88796b Driver Design
The NIC Driver of AX88796B is the interface between the processor CPU and the NIC hardware. The NIC Driver consists of the following parts: Nic initialization, packet receiving and sending, and interrupt processing subroutine.
1 Nic Initialization
In addition to defining and assigning values to related registers, the NIC initialization also completes the construction of the receiving buffer ring. All registers of AX88796B are 8-Bit Data widths except the data port registers. The data port register can be set to 8-bit or 16-Bit Width by configuring WTS in the DCR register. The offset of AX88796B is 01h ~ 0fh registers are divided into four pages (Page0 ~ Page3. The NE2000-compatible register has only three pages (Page0 ~ Page2 ). For initialization, you need to set the registers related to page 0 and page 1. The registers on page 2 are read-only and cannot be set. The registers on page 3 are not NE2000 compatible and do not need to be set. To initialize a function, you must complete the following tasks.
/* AX88796B_init */
/* Call the reset subroutine to reset AX88796B. There are two methods to RESET: one is hardware RESET, to RESET AX88796B by pulling the high and low RESET pins; the other is software RESET, reading data from Port 0x1f to RESET AX88796B. */
ReadByte (RESET );
WriteByte (RESET, 0xff );
Delay (3000 );
WriteByte (IMR, 0);/* initialize the Interrupt Mask register IMR, write 0x00, and disable all interrupt requests. */
WriteByte (CR, 0x61);/* Select page 1 to stop AX88796B. */
Delay (1000 );
WriteByte (CPR, 0x4C);/* initialize the receiving buffer ring write page pointer CPR = PSTART */
WriteByte (PAR0 ~ PAR5, MAC);/* set the MAC address of the NIC chip to clear the multicast address register. */
WriteByte (MAR0 ~ MAR7, 0 );
WriteByte (CR, 0x21);/* Stop the NIC and switch to page 0. */
Delay (1000 );
WriteByte (BNRY, 0x79);/* write boundary pointer register (BNRY) is 0x79 ;*/
WriteByte (PSTART, 0x4c);/* set the receiving buffer's start page address 0x4c ;*/
WriteByte (PSTOP, 0x80);/* PSTOP receiving buffer end page address 0x80 ;*/
WriteByte (TPSR, 0x40);/* The starting address of the TPSR sending page is 0x40 ;*/
WriteByte (DCR, 0x80);/* set the Data configuration register to 8-bit data transmission mode. */
/* Set the receiving configuration register to 0x4C so that the receiving buffer can only receive broadcast address packets and multiple On-Demand address packets. */
WriteByte (RCR, 0x4C );
/* Set the sending configuration register of the CTL to 0x80. It adopts full duplex mode. When the packet length is less than 60 bytes, it is automatically filled, and CRC verification is added when the packet is sent. */
WriteByte (CRL, 0x80 );
WriteByte (ISR, 0xFF);/* clear the interrupt Status Register. */
WriteByte (IMR, 0x11);/* allows overwriting interruption and packet receipt interruption. */
WriteByte (BTCR, 0x30);/* the interrupt trigger mode is high-level valid, and the IRQ output is a push-pull drive mode */
WriteByte (CR, 0x22);/* Start the chip and initialize AX88796B. */
Delay (1000 );
Wrcurpge = read (CPR );
Rdnxtpge = wrcurpge;
2. send and receive data packets
There are two ways to determine whether AX88796B receives a new data packet: Round Robin and interruption. Single-Chip Microcomputer adopts many round robin modes. In order to improve CPU performance and real-time performance requirements, the interrupt method is used here. When the network adapter receives a new data packet, it is interrupted. First, it determines whether the CPR is equal to BNRY. If it is equal, it indicates that the receiving buffer is full, and the receiving data packet is stopped without overwriting the old data; if not, the starting address of the data packet to be read in the receiving buffer is written to the RSAR [] register. The starting 4 bytes of the data packet are written to the RBCR [] register, start the remote dma read command to obtain the length, receiving status, and pointer information of the next page to be read by reading four bytes of information. Then, the remote dma read command is used to read data packets from the nic sram into the CPU memory, and update the read page pointer register BNRY. Each time the CPU reads one page of data from the NIC memory, BNRY will add one, which needs to be implemented through a program. The network adapter writes the received packets to the receiving buffer through CPR. After each page is written, CPR will automatically add one. When it is added to the final blank page (PSTOP = 0x80, CPR is automatically restored to the home page of the receiving buffer (PSTART = 0x4c ). The key code for Nic reception is as follows:
/* AX88796B_receive */
Uint8 rcvinfo [4], I;
Uint16 pktlength, index;
/* Obtain the information of the data packet to be read and save it in the rcvinfo array */
WriteByte (RSAR0, 0x00 );
WriteByte (idx, RSAR1, rdnxtpge );
WriteByte (RBCR0, 0x04 );
WriteByte (RBCR1, 0x00 );
WriteByte (CR, 0x0A );
For (I = 0; I <4; I ++)
Rcvinfo [I] = * (uint8 *) (DP_PORT + Base_addr ));
Pktlength = rcvinfo [2] + (rcvinfo [3] * 256)-4;
/* Read the data packet into the CPU memory Buf */
WriteByte (RSAR0, 0x4 );
WriteByte (RSAR1, rdnxtpge );
WriteByte (RBCR0, (uint8) (pktlength & 0x00ff ));
WriteByte (RBCR1, (uint8) (pktlength> 8 ));
WriteByte (CR, 0x0A );
ReadData (uint16 * Buf, uint16 length );
/* Update the BNRY pointer and return the pktlength value */
Rdnxtpge = rcvinfo [1];
If (rdnxtpge = PSTART)
WriteByte (idx, BNRY, PSTOP-1 );
Else
WriteByte (idx, BNRY, rdnxtpge-1 );
Return pktlength;
The CPU writes the starting address of the NIC sending buffer and the number of bytes to be sent to RSAR [0, 1] and RBCR [0, 1] through the remote DMA channel, respectively. then, start the remote DMA write command to write the data packet to the NIC memory, and then write the number of bytes to the TBCR [] register. Start the send command to send the data packet to the network. The key code for sending an Eni is as follows:
/* AX88796B_transmit */
/* Write data to the sending buffer of the NIC */
WriteByte (RSAR0, 0x00 );
WriteByte (RSAR1, TX_BUF_Start );
WriteByte (RBCR0, (uint8) (len & 0xFF ));
WriteByte (RBCR1, (uint8) (len> 8 ));
WriteByte (CR, 0x12 );
WriteData (uint16 *) addr, len );
WriteByte (idx, TPSR, TX_BUF_Start );
/* Send the data in the sending buffer to the Network */
WriteByte (TBCR0, (uint8) (len & 0xFF ));
WriteByte (TBCR1, (uint8) (len> 8 ));
WriteByte (CR, 0x26 );
3. interrupt handling
The interrupt processing is closely related to the CPU. In the software, you must configure the chip line selection and interrupt signal line of the NIC. When the data packet arrives, the NIC saves it in the SARM and triggers an interruption. After receiving the interrupt signal, the processor enters the interrupt processing program. Read the interruption Status Register ISR of AX88796B In the interrupt processing program to determine the type of interruption. If the read value of the interrupt bit is 1, it indicates that the packet is interrupted, at this time, a message needs to be triggered to enter the read Nic function. The function of reading the NIC function is to receive network packets from the NIC memory to the host, and then transfer them to the upper layer for corresponding processing.
A message is sent to the memory of the NIC by calling the NIC function. Then, set the TXP (Transmit packet) Position 1 of the control register (CR) of AX88796 to send the packet.
Key code of the interrupt processing Subroutine:
/* Irq0_handler */
Uint8 interruptstatus;
Interruptstatus = read (ISR); // read interrupt Status Register
Write (IMR, 0x00); // disable interruption
Write (ISR, interruptstatus); // clear the interrupt Status Register
If (interruptstatus & 0x01) // determines whether the packet is interrupted
Osqpost (tcpipmsgq, & idx); // notifies the host machine to receive data packets
Write (IMR, 0x11); // enable to receive interrupt and overwrite interrupt