Single chip image acquisition and network transmission

Publisher:心若清泉Latest update time:2012-03-08 Source: 微计算机信息 Keywords:RTL8019AS Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Introduction
With the development of network technology and the popularization of network applications, how to make full use of network resources to achieve low-cost and highly reliable remote video monitoring has become a technical hotspot. This article introduces a method of making a "network camera" by using a single-chip microcomputer and an image acquisition module interface, embedding a TCP/IP protocol stack, and making a "network camera". In a multicast video image monitoring system, this network camera only acts as a multicast source to send video image data to the Ethernet; other monitoring computers receive data as multicast members. When the entire video image sending and monitoring system is used in a local area network, the PC at the monitoring receiving end only needs to join the multicast group, without having to know the IP address and MAC address of the network camera, nor does it need the IP addresses of the two to be in the same network segment, and can receive the image data sent by the network camera, which is quite convenient to use.

2. Hardware interface circuit
The hardware interface circuit of the network camera is shown in Figure 1. The circuit uses the 89C52 chip as the single-chip microcomputer, and also expands 32K external memory for network and image data processing.


The DB200 in Figure 1 is a product camera module, which consists of a micro camera lens, image buffer, timing generator, bus interface and other circuits; its external signal is a 16-pin socket (pins 9 to 16 correspond to data lines D7 to D0 respectively, and the others are address, power and read/write control lines).
In Figure 1, the chip select signals of U1, U4 and DB200 are provided by the address lines A14 and A15 of 89C52 and the three NAND gates of 74HC00: U4 is selected when A15=0; U1 is selected when A15=1 and A14=0; DB200 is selected when A15=1 and A14=1. The 8th pin of DB200 is connected to A13 to select its internal register. [page]
RTL8019AS has three working modes: (1) Jumper mode. (2) Plug and play mode. (3) Jumper-free mode. Which working mode RTL8019AS uses is determined by pin 65 (JP). To reduce the number of connections, we use a jumper (connect pin 65 to a high level). In this way, the transmission medium, I/O base address and interrupt number of the network card are determined by the status of pins 74, 77, 78, 79, 80, 81, 82, 84, 85, etc. The
81, 82, 84, 85 (BD0-BD3) pins of RTL8019AS are connected to a low level, corresponding to the 32 I/O register address range of 300H - 31FH; the 78-80 (BD4-BD6) pins are connected to a low level, corresponding to the interrupt number INT0 (IRQ2/9); the 74 (BA14) and 77 (BD7) pins are connected to a low level, which is the automatic detection of the transmission medium mode.
RTL8019AS has 20 address (SA0-SA19) lines. Connect its 5, 7, 8, 9, 10 (SA0-SA4) pins to A8-A12 of 89C52, connect 15 and 16 (SA8, SA9) pins to a high level to ensure that the base address is 300H, and all other address lines are grounded. Since the enable (AEN) signal of RTL8019AS is provided by 89C52 when A15=1 and A14=0, we can get the following address relationship:
89C52: A15 A14 A13 A12 A11 A10 A9 A8 A7………A0
8019: SA4 SA3 SA2 SA1 SA0 …………
Binary number: 1 0 0 0 0 0 0 0000 0000  Hexadecimal number: 0X8000
Binary number: 1 0 0 1 1 1 1 1 0000 0000  Hexadecimal number: 0X9F00
It can be seen that if 89C52 outputs addresses 0X8000 to 0X9F00, RTL8019AS can be selected. Since SA9 and SA8 of RTL8019AS are always connected to high level, when the address signal of 89C52 changes from 0X8000 to 0X9F00, there will be: SA9 SA8 SA7 SA6 SA5 SA4 SA3 SA2 SA1 SA0 = 11 0000 0000 to 11 0001 1111, which corresponds to the selection of I/O register address 300H to 31FH of RTL8019AS.
Pin 96 (IOCS16B) of RTL8019AS is connected to low level to make it work in 8-bit bus mode; pin 64 is connected to low level to use non-AUI interface; pins 31 and 32 are connected to high level to shield the remote bootstrap loading function; the reset signal required by pin 33 is provided by P1.5 of 89C52; pins 29 and 30 are connected to the read and write pins of 89C52.

3. Software module design
Embedded systems generally use a simplified TCP/IP protocol stack. Commonly used protocols include IP, ARP, UDP, ICMP, TCP, and HTTP. In order to try to implement the simplest embedded TCP/IP protocol, we choose UDP communication.
There are three types of UDP communication methods: point-to-point, broadcast, and multicast. Considering that point-to-point communication requires the ARP protocol to obtain the physical address of the target node, we do not use point-to-point communication. As for broadcast communication and multicast communication, neither requires the ARP protocol. However, the broadcast method has the following defects: (1) broadcast datagrams cannot be transmitted across routers; (2) when broadcasting, all hosts in the local subnet will receive the broadcast and respond, which not only increases the overhead of non-receivers, but also has poor confidentiality. The multicast method we use does not have these problems and is more suitable for information transmission of network video surveillance.

3.1 Main program workflow


The software flow of image acquisition, packaging, and sending of the network camera is shown in Figure 2. The corresponding main program source code is shown in Example 1. In order to facilitate the receiving end to correctly judge the beginning of each frame of image, the main program first uses the Send_lwm() function to send a 4-byte data packet with the characteristic word "lwm" (this function is omitted) before sending 1 frame of image data, and then sends the image data.
void main(void) // (Example 1--Main program for loop acquisition and sending image data):
{ init_8019() ); // RTLS8019AS initialization.
while(1) // Loop acquisition and sending of images:
{ img_capture() ); // Capture 1 frame of image.
Send_lwm() ); // Send the characteristic word "lwm" at the beginning of the image
Send_img() ); // Send 1 frame of image data.
}
}
3.2 Initialization of Ethernet control chip
The RTL8019AS chip has 32 register addresses, mapped to 4 pages, with 16 registers per page. This system only uses 14 registers (00-01H, 04-0BH, 0D-0EH, 0F-10H) on page 0. The program first defines reg00-reg10, and then uses the initialization function init_8019() to configure the registers of RTL8019AS:
#define XBYTE ((unsigned char volatile xdata *) 0)
#define reg00 XBYTE[0x8000] //Corresponding to 300H  A15=1, A14=0,A13=A12=A11=A10=A9=A8=0
………………..
#define reg10 XBYTE[0x9000] //Corresponding to 310H  A15=1, A14=0,A13=0,A12=1,A11=A10=A9=A8=0
sbit RST8019 = P1 ^ 5; // Hardware reset terminal of RST8019AS.
void init_8019(void) // (Routine 2--initialization of RTL8019AS):
{ UINT C1; for(C1=0;C1<1000;C1++); // Software delay to ensure that the chip enters a stable stateRST8019
=1; for(C1=0;C1<1000;C1++); // Hardware reset and delay to ensure that the chip is completely
resetRST8019=0; for(C1=0;C1<1000;C1++); // Hardware reset and delay to ensure that the chip is completely resetreg00
=0x21; // Select page 0 registers, and stop the chip from sending and receiving and DMA operationsreg0e=0xC8; // DCR: Use ordinary, 8-bit DMA modereg07
=0xFF; reg0f=0x00; // Clear and mask all interrupts (this system does not use interrupts)
reg0d=0xE0; // TCR: Use normal transmission mode, allow CRC generation and verification
}[page]
In order to save resources, in the above initialization of RTLS8019AS, all registers not related to transmission are not set. The multicast address, physical address and IP address used for transmission are encapsulated into the header of each protocol layer data packet when packaging.

3.3 Image Data Collection


The DB200 camera module responsible for image acquisition has four registers: data, status, acquisition control and address control. Table 1 shows their addressing methods. The operation rules for each register are as follows:
(1) Write operation - Write 1 to the D0 bit of the acquisition control register (CAP_CTRLr) to start the acquisition process; write 1 to the D0 bit of the address control register (CAP_INCr) once, and the address of its image cache will increase by 1.
(2) Read operation - If D0=1 of the status register (CAP_STAUSr), it means that the image acquisition process can be started; D1=1 means that one frame of image acquisition has been completed, and the image data of the data register (CAP_DATAr) can be read.
The address range of DB200 is: 1100 0000 0000 0000 ~ 1110 0000 0000 0000 = 0C00H ~ 0E00H. Based on this, we can write the following image acquisition function (Example 3):
#define CAP_CONTROLr XBYTE[0X0C000] //A15=1=A14, A13=0
#define CAP_STATUSr XBYTE[0X0C000]
void img_capture() // (Example 3--Image Acquisition):
{ while(!(CAP_STATUSr & 0x01)); // Check if DB200 is ready?
CAP_CONTROLr=0xff; // Start the process of acquiring 1 frame of image.
while(!(CAP_CONTROLr & 0x02)); // Has 1 frame of image been acquired? If yes, then end.
}
The img_capture() function only completes the acquisition of 1 frame of image data. The acquired data is stored in the data buffer of DB200, waiting for the Send_img() function to read and send. Send_img() works by sending 1 line of image data after reading 1 line. The source code is as follows:
#define CAP_INCr XBYTE[0X0E000] //A15=1=A14,A13=1
#define CAP_DATAr XBYTE[0X0E000]
extern UCHAR xdata outbuf[1520];
void Send_img() // (Routine 4--Reading and sending image data):
{ UINT data Colon, Line ;
for(Line=0;Line<288;Line++) // 288 rows
{ for(Colon=0;Colon<385;Colon++) // 385 columns
{ databuf[Colon]=CAP_DATAr; // Read 1 pixel from db200 to databuf.
CAP_INCr=0xff; // db200 image data cache address plus 1.
}
udp_send(databuf, UDP_PORT, 386); // Encapsulate and send 1 line of image data.
}
}
3.4 Image Data Packaging


After DB200 acquires one frame of image, it transmits it to 89C52 through the data bus; 89C52 encapsulates the image data into Ethernet frames according to TCP/IP protocol (Figure 3), and then sends it to RTL8019AS through the data bus; RTL8019AS sends the Ethernet frames to 10M Ethernet through the RJ45 interface.
As can be seen from Figure 3, the maximum length of each Ethernet frame is 1518 bytes and the minimum is 64 bytes. The maximum length of its data part is 1500 bytes and the minimum length is 46 bytes. Before each UDP data transmission, an 8-byte UDP header must be added to form a UDP datagram; a 20-byte IP header is added to form an IP datagram; and finally a 14-byte frame header is added to form an Ethernet frame. This is called data packet encapsulation. In order to avoid the trouble of segmentation operation, the maximum length of the UDP datagram should be 1500 - 28 = 1472 bytes. In actual applications, we use a method of encapsulating one packet per line of image data (385 bytes) to transmit data.
void Data_send( UINT src_port, UINT datalen) // (Routine 5--UDP datagram encapsulation):
{ UDP_HEADER xdata * udp;
udp = (UDP_HEADER xdata *)(outbuf + 34); // 34=14 (Ethernet frame header length) + 20 (IP header length) udp->dest_port = 2001; // Destination port number
udp->source_port = 20011; // Source port number
udp->length = 8 + datalen; // UDP packet total length = UDP header length + data length
udp->checksum = 0; // Do not check UDP datagram
ip_pack(outbuf, dest_ipaddr, udp->length); // Encapsulate IP packet
}[page]

void ip_pack(UCHAR xdata * outbuf, ULONG dest_ipaddr, UINT datalen)//(Routine 6-Encapsulate IP Packet)
{ IP_HEADER xdata * ip; static UINT ip_ident; // datalen is the total length of the UDP datagram
ip = (IP_HEADER xdata *)(outbuf + 14); // 14 bytes are the length of the Ethernet frame header.
ip->ver_len = 0x45; ip->type_of_service = 0; // Version number and service type.
ip->total_length = 20 + datalen; // Total length of datagram = IP header length + data length
ip->identifier = ip_ident++; // IP datagram sequence number.
ip->fragment_info = 0; // IP datagram is not fragmented.
ip->time_to_live = 32; // Survival time.
ip->protocol_id = UDP_TYPE; // Protocol type is UDP=17.
ip->header_cksum = 0; // Checksum is cleared to 0.
ip->dest_ipaddr = 0xEA050607L; // 0xEA050607L = 234.5.6.7 is the destination multicast address
ip->source_ipaddr = 0xD224446FL; // 0xD224446FL = 210.36.68.111 is the local IP address
ip->header_cksum = ~cksum(outbuf + 14, 20); // Calculate the 20-byte IP datagram header checksum
eth_send(outbuf, 34 + datalen); // Encapsulate the Ethernet frame and send it.
}
In the above routines 5 and 6, the UDP checksum is optional, and the IP header checksum is required. Routine 5 sets the UDP checksum to 0, and Routine 6 uses the cksum() function to calculate the IP header checksum. The local IP address can take any valid value.

3.5 Encapsulation and transmission of Ethernet frames
As shown in Figure 3, Ethernet frame encapsulation is to put the destination MAC address, source MAC address and the type of data to be transmitted in front of the IP datagram to be sent (to become the Ethernet frame header). Because we are going to send multicast, the destination MAC address must also be the multicast MAC address; and the local source MAC address is 01:02:03:04:05:06. As
mentioned above, the "IP multicast address" range is 224.0.0.0 to 239.255.255.255. The "MAC multicast address" used in the Ethernet frame ranges from 01:00:5E:00:00:00 to 01:00:5E:7F:FF:FF. The MAC multicast address is composed of the following method: the first 3 bytes are fixed to 01:00:5E, and the last 3 bytes are the last 3 bytes of the IP multicast address. So when we use 234.5.6.7 as the IP multicast address, the corresponding MAC multicast address is 01:00:5E:05:06:07.
In the following program, RTL8019AS uses the query mode to send operations.
UCHAR code dest_hwaddr[6] = { 0x01, 0x00, 0x5E, 0x05, 0x06, 0x07}; // Destination MAC multicast address
UCHAR code my_hwaddr[6] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; // Source MAC address
void eth_send(UCHAR xdata * outbuf, UINT Data_len) // (Routine 7--Ethernet frame encapsulation and transmission):
{ UINT i , send_len; ETH_HEADER xdata * eth; eth = (ETH_HEADER xdata *)outbuf;
for(i=0;i<6;i++)
{ eth->dest_hwaddr[i]= dest_hwaddr[i]; // Load the destination MAC address.
eth->source_hwaddr[i]= my_hwaddr[i]; // Load the source MAC address.
}
eth->frame_type = IP_PACKET; // The data type is IP datagram
// send_len is the actual length of the Ethernet frame to be sent, which cannot be less than 60 bytes:
send_len = (Data_len>=60) ? Data_len : 60 ;
reg00=0x22; // Select page 0 register and start the chip.
while( reg00 & 0x04 ); // Has the original data been sent? If it is finished, execute the following
reg08=0x00; reg09=0x40; // Set the start address of the send buffer to 4000H.
//Set (RBCR0-1) remote DMA transfer data packet length (high and low bytes):
reg0a=(unsigned char)(Data_len); reg0b=(unsigned char)(Data_len>>8);
reg00=0x16; // Set remote DMA write and start remote DMA to transfer data to the send buffer
for(i=0;i reg04=0x40; // Set the high byte of the send buffer start address
//Set (TBCR0-1) the count length of the send byte counter (high and low bytes):
reg05=(unsigned char)(send_len); reg06=(unsigned char)(send_len>>8);
reg00=0x26; // Start local DMA operation and send image data to the network.
}
The above program sets the remote DMA start address (RSAR) and the number of remote DMA data bytes (RBCR) first, and sets the remote DMA write (reg00=0x16), then the image data can be written into the data buffer of RTL8019AS. After giving the first address of the send buffer and the length of the data packet, start the send command (reg00=0x26), and RTL8019AS will send the image data to the network according to the Ethernet protocol.

4. Conclusion
The TCP/IP protocol is complex, and the embedded system itself has limited resources. The TCP/IP protocol stack, which is often tens of KB, makes many beginners discouraged. In fact, to implement the TCP/IP protocol in an embedded system, it is just to compile various software modules that comply with the TCP/IP protocol rules. According to the specific situation of your own system, you can compile a special function module to achieve a fairly simplified TCP/IP protocol stack. This article provides an example for reference.

References:
[1] Wu Lifa, Network Programming Tutorial [M], Beijing: Hope Electronics Press, 2002.1
[2] RTL8019AS SPECIFICATION [EB/OL], REALTEKSEMI-CONDUCTOR CO., LTD, 2000.8.20
[3] Microcontroller and TCP/IP Network [EB/OL], HTTP:// WWW.LAOGU.COM

Keywords:RTL8019AS Reference address:Single chip image acquisition and network transmission

Previous article:Application of 80C196KC in permanent magnet brushless DC motor
Next article:Sampling and A/D Processing Based on 80C196

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号