Network communication design based on uCLinux and S3C4510B

Publisher:神雕Latest update time:2012-10-29 Source: 21IC Keywords:uCLinux  S3C4510B  DM9161 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

introduction

Linux is a very popular operating system that is compatible with UNIX systems and has open source code. It was originally designed as a desktop system, but is now widely used in embedded devices. uCLinux was born in this atmosphere. In the English word uCLinux, u stands for Micro, which means "small"; C stands for Control, which means "control", so uCLinux is Micro-Control-Linux, which literally means "a Linux system designed for the micro-control field". It is also an operating system designed for microprocessors without MMU (memory management unit module). S3C4510B is a microprocessor of this type.

Samsung's S3C4510B is a cost-effective 16/32-bit RISC microcontroller based on Ethernet application systems. It contains a 16/32-bit ARM7TDMI RISC processor core designed by ARM. ARM7TDMI is a low-power, high-performance 16/32 core that is most suitable for applications that are sensitive to price and power consumption. In addition to the ARM7TDMI core, the S3C4510B also has many important on-chip peripheral function modules, including an Ethernet controller for network communication between the S3C4510B system and other devices. The uCLinux operating system has been transplanted to the network control platform of the S3C4510B, and various network control functions have been implemented on this embedded platform. The network communication project described in this article is the most important function.

1 Design ideas and implementation of Ethernet circuit based on S3C4510B

As an excellent network controller, if the system based on S3C4510B does not have an Ethernet interface, its application value will be greatly reduced. Therefore, for the entire system, the Ethernet interface circuit should be indispensable, but it is also relatively complex. From the hardware point of view, the Ethernet interface circuit is mainly composed of two parts: the MAC controller and the physical layer interface (Physical Layer, PHY).

S3C4510B has an embedded Ethernet controller, which supports Media Independent Interface (MII) and Buffered DMA Interface (BDI), and can provide 10M/100Mbps Ethernet access in half-duplex or full-duplex mode. In half-duplex mode, the controller supports CSMA/CD protocol, and in full-duplex mode, it supports IEEE802.3MAC control layer protocol. Therefore, S3C4510B actually contains Ethernet MAC control, but does not provide a physical layer interface, so an external physical layer chip is required to provide an Ethernet access channel.

Commonly used single-port 10M/100Mbps high-speed Ethernet physical layer interface devices include RTL8201, DM9161, etc., which provide MII interface and traditional 7-wire network interface, and can be easily interfaced with S3C4510B. The main functions of Ethernet physical layer interface devices generally include: physical coding sublayer, physical media attachment, twisted pair physical media sublayer, 10BASE-TX encoder/decoder and twisted pair media access unit, etc.

In this design, DP9161 is used as the physical layer interface of Ethernet. DM9161 is a low-power, high-performance CMOS chip that supports 10M and 100M Ethernet transmission. It plays the role of encoding, decoding input and output data. Its pin connection with S3C4510B is shown in Figure 1.

Since the S3C4510B chip has a MAC controller with an MII interface, and the DM9161 also provides an MII interface, and the definitions of various signals are also very clear, the connection timing between the DM9161 and the S3C4510B can achieve a good network signal transmission purpose. Figure 2 shows the actual application circuit of the DM9161 in this system (1, 2, 3 and 14, 15, 16 in the lower right corner of the figure are connected to the corresponding pins of the network isolation transformer respectively).

The MAC controller of S3C4510B can control up to 1 DM9161 through MDC/MDIO management interface. Each DM9161 should have a different PHY address (from 00001B to 11111B). When the system is reset, DM9161 latches the initial state of pins 9, 10, 12, 13, and 15 as the PHY address for communication with S3C4510B management interface; but the address cannot be set to 00000B, otherwise DM9161 enters power-down mode.

The signal sending and receiving ends should be connected to the transmission media through a network isolation transformer and an RJ45 interface. The actual application circuit is shown in the library.



2 Analysis of Network Programming Protocols under Linux

The Internet address family is implemented between the various layers of the TCP/IP network protocol stack under Linux through a series of interconnected layer software. The structural hierarchy is shown in Figure 4.

The BSD socket layer is handled by a generic socket manager specifically designed to handle BSD sockets, which is supported by the INET socket layer. INET sockets manage transmission endpoints for the IP-based protocols TCP and UDP. UDP (User Datagram Protocol) is a connectionless protocol, while TCP (Transmission Control Protocol) is a reliable end-to-end protocol. When transmitting UDP packets, Linux does not know or care whether they arrive safely at their destination. TCP is different. A number needs to be added to both ends of the TCP connection to ensure that the transmitted data is received correctly. At the IP layer, the Internet Protocol code is implemented, which adds an IP header to the transmitted data and knows how to send the incoming IP packets to the TCP or UDP protocol. Below the IP layer, there are network devices to support all Linux network work, such as PLIP, SLIP, and Ethernet.

3 Socket programming in uClinux environment

The transmission of network socket data is a special I/O. Socket is also a file descriptor and has a file-like function call socket(). This function returns an integer socket descriptor. Subsequent connection establishment, data transmission and other operations are implemented through this socket function. There are two commonly used socket types: stream socket and datagram socket. The difference between the two is that the former corresponds to TCP service and the latter corresponds to UDP service.

3.1 Functions used in socket programming in uCLinux

(1) Socket function

In order to perform I/O, the first thing a process must do is to call the socket function, specifying the desired communication protocol type (TCP using IPv4, UDP using IPv6, Unix domain byte stream protocol, etc.). The function structure is as follows: int socket(int family, int type, int protocol);

/*Return: non-negative description word - success, -1 - error*/

The family in the code indicates the protocol family. The socket type type is a constant value. Generally speaking, the parameter protocol of the socket function is set to 0, and the socket function returns a small non-negative integer value when it succeeds. To get this value, we specify the protocol family (IPv4IP, v6 or Unix) and the socket type (byte stream, datagram or raw socket). [page]

(2) connect function

The TCP client uses the connect function to establish a connection with a TCP server.

Int connect(int sockfd,const struct sockaddr* servaddr,socklen_t addrlen);/*Return: 0—success, -1—error*/

Sockfd is the value returned by the socket function. The second and third parameters are a pointer to a socket address structure and the size of the structure. The socket address structure must contain the server's IP address and port number.

(3) bind function

The bind function assigns a local protocol address to the socket. For the Internet Protocol, the protocol address is a combination of the non-reversed 2-bit IPv4 address and the 16-bit TCP or UDP port number.

Int bind(int sockfd,const struct sockaddr* myaddr,socklen_t addrlen);/*Return: 0 - success, -1 - error*/

The second parameter is a pointer to a protocol-specific address structure, and the third parameter is the length of the address structure. For TCP, calling the bind function can specify a port and an IP address. You can specify both or neither.

(4) listen function

The listen function is only called by TCP servers. It does two things. When a socket is created by the socket function, it is assumed to be an active socket. That is, it is a client socket that will call connect to initiate a connection. The listen function converts an unconnected socket into a passive socket, indicating that the kernel should accept connection requests directed to this socket. Calling the listen function causes the socket to transition from the CLOSED state to the LISEN state according to the TCP state transition. The second parameter of the function specifies the maximum number of connections that the kernel will queue for this socket.

Int listen(int sockfd,int backlog);

/*Return: 0 - success, -1 - error*/

Generally speaking, this function should be called after calling the socket and bind functions, and before calling the accept function.

(5) accept function

The accept function is called by the TCP server and returns the next completed connection from the head of the completed connection queue. If the completed connection queue is empty, the process goes to sleep. (Assuming the default blocking mode of the socket)

int accept(int sockfd,struct sockaddr*cliaddr,socklen_t*addrlen);/*Return a non-negative value—OK, -1—error*/

The parameters cliaddr and addrlen are used to return the protocol address of the other process (client) connected. Addrlen is the result parameter. Before calling, the integer value indicated by *addrlen is set to the length of the socket address structure indicated by cliaddr. When returning, this integer value is the exact number of bytes stored in this socket address structure by the kernel.

3.2 Implementation of network communication programming in uClinux

When performing socket programming in uCLinux, network applications are generally written according to the process shown in the book materials.

In addition to being familiar with the functions mentioned above, you should also know two important data structures. Because in computers, data is stored in two byte priorities: high byte priority and low byte priority. On the Internet, data is transmitted in high byte priority, so for data stored internally in low byte priority, it needs to be converted before it can be transmitted on the Internet.

*struct sockaddr: used to save socket information

struct sockaddr{unsigned short sa_family;/*address family, AF_xxx*/

char sa_data[14]; /*14 bytes of protocol address*/};

*struct sockaddr_in; and to convert data types

struct sockaddr_in{

short int sin_family; /*Address family*/

unsigned short int sin_port; /*port number*/

sruct in_addr sin_addr; /*IP address*/

unsigned cha sin_zero[8]; /*fill with 0 to keep the same size as struct sockaddr*/};

At this point, the network communication engineering program of uCLinux can be compiled. Here are some examples of writing network communication source code and Makefile files under uCLinux.

Some of the code in the main() function is as follows:

int sockfd;

unsigned int uiip;

char szsendbuf[1024];

char head[8];

int*phead=head+4,nsize=1024,allsize=0;

struct sockaddr_in servaddr;

sockfd=socket(AF_INET,SOCK_STREAM,0);/*Create socket*/

bzero(&servaddr,sizeof(struct sockaddr_in));

servaddr.sin_family=AF_INET;[page]

servaddr.sin_port=8888;//htons(8888); /*Specify the communication port*/Convert the string IP input in the command line to the integer uiip that can be recognized by the connect function. Originally, the C library function inet_pton() could be used when developing on Linux, but this function is not supported in the uCLinux library, so I had to implement the function myself.

aiptoi() looks like this:

aiptoi(argv[1],&uiip);

servaddr.sin_addr.s_addr=uiip; /*Specify the peer IP of the connection*/

connect(sockfd,(struct sockaddr)&servaddr,sizeof(struct sockaddr));

/*Connect to the other end to receive code*/

fp=fopen("kongzhi.htm","r"); /*Open the control page*/

while(nsize==1024)

{bzero(szsendbuf,1024); /*Read 1024 bytes from the file and send them out each time. If less than 1024 bytes are read, the file ends.*/

nsize=phead=fread(szsendbuf,1,1024,fp);/*Read from the file and fill in the send BUFFER*/

write(sockfd,head,8);/*Send protocol header*/\'

nsize=write(sockfd,szsendbuf,nsize);/*send*/}

fclose(fp);

The modifications to the Makefile in uCLinux are as follows:

CC=gcc

COFF2FLAT=/uclinux/coff2flt-0.3/coff2flt

CFLAGS=-I/uclinux/uC-libc-pic/include

LDFLAGS=/uclinux/uC-libc-pic/libc.a

ethernet:Ethernet.o

$(CC)-o $@.coff ethernet.c $(CFLAGS)$(LDFLAGS)

$(COFF2FLAT)-o Ethernet ethernet.coff

cp Ethernet /Ethernet

clean:

rm -f Ethernet Ethernet.o

It should be noted that: ① uCLinux does not have a pthread library, so keep this in mind when writing network programs; ② In the uCLinux environment, neither the processor (hardware) nor the kernel (software) provides a memory management mechanism, so the address space of the program is equivalent to the physical address space of the memory. In the program, the I/O address can be directly operated without applying for and releasing the I/O space, but the user needs to check the occupancy of the I/O address being operated.

Conclusion

Since network communication engineering is widely used in embedded devices, previous articles only generally describe one aspect of network communication design. This article combines actual engineering projects and describes the key points of hardware circuit construction and application software design. This has important reference significance for the application of network communication in embedded devices, especially in uCLinux-based systems.

Keywords:uCLinux  S3C4510B  DM9161 Reference address:Network communication design based on uCLinux and S3C4510B

Previous article:Improvement of the main control module of the seabed magnetotelluric signal acquisition system based on ARM
Next article:Application of ARM core AT75C220 in fingerprint recognition system

Recommended ReadingLatest update time:2024-11-16 15:47

MAC and IP address settings for embedded network devices
Embedded products can be divided into information appliances, mobile computing devices, network equipment, industrial control, simulation, etc. As the Internet becomes increasingly important, more and more embedded products have the requirement of networking. Among the above categories of embedded products, except f
[Microcontroller]
MAC and IP address settings for embedded network devices
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号