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.
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
- Popular Resources
- Popular amplifiers
- ICCV2023 Paper Summary: Fairness, Privacy, Ethics, Social-good, Transparency, Accountability in Vision
- STM32 official 1588 protocol routine modified version
- Understanding and using the Controller Area Network (CAN) communication protocol: theory and practice
- Detailed explanation of ARM application system development - system design based on S3C4510B
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- LCD Operation (S3C2440)
- The operational amplifier forms a three-point capacitor oscillation
- [NXP Rapid IoT Review] First give SLN-RPK-NODE an emulator
- Guangdong companies are looking for electronic engineers/structural engineers/acoustic engineers
- Can the output voltage of an LDO be equal to the input voltage?
- After importing the 2812 project example, it prompts that it does not match and cannot be run
- EFT 2B, but all others passed.
- STM32 freezes and needs power-on reset
- Schematic diagram and PCB of EBAZ4205 mining board based on Z7010
- 【Repost】Analysis of the process and principle of lithium battery core slurry