Design of Embedded Web Server Based on Cortex-M3 kernel processor
Introduction
Currently, network control has become the main research direction of remote control. Using networks to monitor devices in the local area and even the world is the development trend of industrial control systems [1]. Embedded Internet
As a representative of network control, remote monitoring solves the problem of heterogeneous network interconnection in the industrial control field, improves the intelligence level of traditional equipment, and promotes the adjustment of the traditional industrial structure. Embedded Web Services
It is especially suitable for Embedded Internet applications. It can be easily connected to any network through the Ethernet or Modem connection to achieve Remote Management and Control of devices.
Implementation Scheme
Embedded Web servers have the following basic functions: they can control the connected devices and obtain the status and data of the devices. On-site information can be published on a webpage. They can respond to remote user control commands in a timely manner. In addition, embedded devices should have features such as simple functions, low power consumption, and portable. For this reason, the system 1 designed by the author is shown.
Figure 1 system diagram
The system adopts the STM32 microprocessor STM32F103RB of Cortex-M3 kernel of ST Company. This chip is an enhanced type of the STM32 series and provides up to KB
In-chip Flah, 20 kB RAM, and a wide range of peripheral interfaces. In the design of the Cortex-M3 kernel, the characteristics of industrial-level embedded product fields that meet the low power consumption and strong real-time performance are specially considered. Adequacy
Under the same conditions, the power consumption of STM32 products is 75% lower than that of products of the same level, and the operating environment temperature reaches 105 ℃. Due to the high integration level, the LQFP100 chip is the smallest system except for a power supply.
Only seven external capacitors are required.
The TCP/IP protocol stack is supported by the W5100, which integrates the TCP/IP protocol stack, Ethernet MAC, and PHY.
The 16 KB sending/receiving buffer can exchange data quickly, with a maximum communication rate of 25 Mbps. The 10 BaseT/100BaseTX Ethernet physical layer is embedded and supports automatic response.
Multiple bus interface modes are provided, which can be easily connected to various MCU, simplifying the hardware circuit design, enabling the embedded system without the support of the operating system, it truly realizes the ideal of Single-Chip access to the Internet.
In order to reduce PCB Area and wiring complexity, considering that the system's data transmission rate is not high, select the SPI interface as the connection method of STM32F103RB and W5100. The STM32F103RB, W5100, and RJ45 interfaces constitute a typical embedded Web server.
As an application example, this design uses the chip ADC and GPIO of STM32F103RB to connect the server to the LED and pressure sensor. Once the server receives a connection from the browser
Request, the server will respond in a timely manner, the current pressure parameters, LED status and temperature information embedded into the Web page and published to the Internet, so that the site information is displayed in the browser. In addition
The control button on the page can remotely control the LED status, and the operation results can be reported to the webpage in a timely manner.
Hardware Design
Figure 2 Simplified circuits for Embedded Web Servers
Figure shows the connection modes of the three core devices that constitute the server. RJ45 interfaces are physical layer interfaces with network transformers. In W5100, the SEN pin is pulled to
High to enable it in SPI slave mode. CS is a chip selection, and MISO and MOSI are two data transmission lines. The SPI clock of W5100 is raised by the active mode STM32F103RB.
Supply. To make it easier for STM32F103RB to determine the W5100 operating state, the interrupt pin INT of W5100 must be connected to the external disconnection of STM32F103RB. W5100
The 5th, 6, 8, and 9 pins of the PHY signal line are used to connect to the RJ45 interface. In addition to power supply pins, other W5100 pins can be left empty.
Figure 2 Simplified Circuit Diagram of Embedded Web Server
Software Design
Main Program
The main program flowchart 3 is shown.
Figure 3 main program flowchart
When the program starts to start, the program first initializes STM32 and W5100, and configures STM32 to work in SPI master mode, while W5100 to work in slave mode, start the/D and On-chip temperature sensors of STM32, and configure the W5100 to work in TCP server mode. In TCP server mode, the port status is changed to 4.
Figure 4 transition of the W5100 port status in TCP Server Mode
When using an internal temperature sensor, note that the temperature sensor has been established for a period of time before it can output the sample voltage in normal horizontal mode; after the ADC is awakened, it also takes a period of time to establish.
Minimum delay, ADON bit (used to start A/D conversion) and TSVREFE bit (used to wake up the temperature sensor from the power-on mode) should be set at the same time. In addition, the sampling time for the temperature sensor must be greater
2.2 μs. Next is an infinite loop body. The program starts to constantly check the WInterFlag and the Socket receives the data status mark.
(SockRecvFlag) Whether to change and start to process A/D converted data to ensure that the server can obtain the current data when generating A webpage. Once the W5100 Socket end
When an interrupt event occurs at the port, the W5100 will trigger the External Interrupt of the STM32 through the interrupt pin INT, so that the STM32 enters the External Interrupt Processing Function and changes the WInterFlag.
Go to the W5100 interrupt event processing function. In this function, STM32 checks the W5100 end by accessing the W5100 interrupt register (IR) and Port 0 interrupt register (S0_IR ).
The interrupt event generated by port 0. If the Socket receives data, SockRecvFlag is changed.
The program detects a SockRecvFlag change and immediately enters the processing and receiving data function. Processing and receiving data functions are the core of the entire program. They are mainly responsible for sending and receiving data, parsing HTTP protocols, and responding to different user requests. Figure 5 shows the program flowchart of the process.
Data Processing Program
HTTP is a request/response protocol. When HTTP-based client/server data is exchanged, an HTTP Communication starts when an HTTP request is generated. The process is as follows:
The host in the URL sends a request to DNS to resolve the IP address of the host name. The DNS resolves the address and returns the result to the browser. The browser sends a request to the address to establish a TCP connection.
The server sends a Response Message to the browser and sends the specified data to the browser.
According to the HTTP protocol, if the client does not send a request, the server will not automatically send the page. Therefore, by adding tags to the webpage program, the browser periodically refreshes the page without user intervention, so that the user can obtain the remote site information in a timely manner.
An HTTP message includes a starting line, zero or multiple message header fields, an entity line indicating the end Of the header field, and a possible message body. This program mainly parses the starting line of the message and ignores other content.
These processing methods also meet the complexity requirements of embedded system hardware for software implementation. The request method determines how the resource specified by the request URI is operated. The GET method reads the resource specified by the URL. Generally
It is used to transmit a small amount of transparent data to the server. The total amount of data is limited to 255 characters, while the POST method can transmit a large amount of data, work with HTML form features to achieve Remote Dynamic Interaction Control
.
When the connection starts, the client sends a GET request to the server to access the webpage of the server. to restrict access by specific users and increase data security, add basic authentication to the webpage.
Mechanism. When the client requests a webpage protected by the Basic authentication mechanism, the server requires user authentication. The server decodes the user login information by Base64. the browser can obtain the correct information only after the authentication is successful.
Real webpage data. Correct control is shown in page 6. If the user information is incorrect, the server returns a Response Message starting with Status Code 401.
Figure 5 Data Processing Procedure Flowchart
Because the data detected on the server is constantly changing, in order to promptly update the webpage data, the data to be displayed during the initialization of the webpage program is temporarily represented by string tags, before sending a Response Message to the port, replace the defined string mark on the webpage with the actual value of the current device.
For LED control, Webpage Programs use HTML forms and HTTP request Methods Adopt POST. When POST is used as a request method, it first establishes a connection with the Web server.
The browser sends the values in the HTML form to the Web server. This method does not limit the length of characters. form data is sent as an additional document rather than as part of a URL. The server program passes
Determine the different control commands of the customer to make the switch response of the LED lamp in real time.
After each connection is successful, the server program returns a Response Message starting with status code 200. The starting behavior of the message is HTTP/1.1 200 OK. When a user requests a file that does not exist on the server side, the server returns a Response Message starting with Status Code 404.
Figure 6 control webpages
Conclusion
This hardware-based TCP/IP network chip W5100 and STM32 processor with Cotex-M3 kernel constitute an Embedded Web Server solution cost-effective. Through the network
The user can receive the data collected by the remote field sensor in a timely manner on the browser side. The remote field switch status indicator can respond to the user's switch signal operation request in real time. The test shows that the system transfers large data files.
The speed can reach 100 KBPS. To improve the security of network resources, AES (Advanced Encryption) can be used.
Standard, Advanced Encryption Standard) and other encryption mechanisms to encrypt private data that must be protected