This article introduces the design method and steps of serial communication under Linux environment, and introduces the design method of serial communication between ARM9 microprocessor s3c2440 and C8051Fxxx series microcontroller under Linux, and gives the hardware connection and communication program flow chart. This method is reliable and practical, and is suitable for most occasions of serial communication between Linux ARM and microcontroller.
In the data acquisition system, since the single-chip microcomputer focuses on control and has weak data processing capabilities, it is cumbersome to calculate and process the collected data. If the communication with the host computer is carried out through the serial port, the design efficiency can be improved by using the host computer's powerful data processing capabilities and friendly control interface to process and display the data. Serial communication has become the first choice for communication between the upper and lower computers with its simple hardware connection and mature communication protocol. The s3c2440 transplanted with the Linux operating system can operate the serial port in the Linux environment, which reduces the difficulty of serial port operation and allows developers to focus on developing large-scale applications without spending time on operating the underlying design.
1 Hardware Connections
s3c2440 is a processor based on ARM9 core produced by Samsung, powered by 3.3 V voltage; C8051Fxxx series microcontrollers are high-performance and high-speed microcontrollers compatible with 8051 launched by CYGNAL of the United States, powered by 3.3 V voltage. The power supply voltage of the two is the same, so no level conversion is required for serial port communication. The hardware connection adopts the most commonly used TXD, RXD, GND three-wire connection method. Note that the cross connection method is used, that is, TXD RXD, RXD TXD.
2 Serial communication under Linux"
2.1 Serial port device description under Linux
The Linux 2.6.32 operating system was transplanted to s3c2440, and the serial port driver of s3c2440 was loaded. Through the serial port operation function and file operation function provided by Linux, the operation of the serial port is equivalent to file operation, which reduces the difficulty of serial port operation and improves efficiency. In the program, devices and files are operated through file descriptors. File descriptors are non-negative integers in the Linux kernel. Linux device files are stored in the "/dev" directory, and serial ports are no exception. The device files corresponding to the serial ports can be found in /dev. The device file path of serial port 1 corresponding to this article is "/dev/ttySAC1".
2.2 Serial communication programming under Linux
Serial communication requires setting some parameters, such as baud rate, data bits, stop bits, input and output modes, etc. These parameters are all in the termios structure provided by Linux, which is a standard interface used by the Linux system to query and operate various terminals. It is defined in the header file < ter-mios.h > as shown below:
STruct termios{
tcflag_t c_iflag; /* Input flag */
tcflag_t c_oflag; /* output flag */
tcflag_t c_cflag /* control flag */
tcflag_t c_lflag /* local flag */
cc_t c_cc[NCCS]; /* Control characteristics* /
} ;
The Linux serial port communication steps can be divided into the following three steps, and the operation process is shown in Figure 1.
1
Open the serial port
Call the open() function to open the serial port device file. If an error occurs, it returns -1. If successful, it returns the file handle.
#define UART1 /dev /ttySAC1
int fd;
fd = open("UART1",O_RDWR) /* Open the serial port device in readable and writable mode */
2
Set serial port properties
The function tcsetattr() can set the structure attributes of the serial port, and tcgetatt() can get the structure attributes of the serial port. In the termios structure, the most important is c_cflag. By assigning values to it, users can set the parameters such as the serial port baud rate, data bits, stop bits, and parity bits. The two variables VMIN and VTIME in the c_cc array determine whether to return input. c_cc[VTIME] sets the byte input time timer, and c_cc[VMIN] sets the minimum number of received bytes that meet the reading function. The values of these two variables must be set reasonably to ensure the communication success rate of the serial port.
int set_attr( int fd)
{
struct termios newtio,oldtio;
tcgetattr( fd,&oldtio) ;
cfsetispeed( &newtio,B9600) ; /* Set the read baud rate to 9600 * /
cfsetospeed( &newtio,B9600) ; /* Set the write baud rate to 9600 * /
memset( &newtio,0
, sizeof( newtio) )
;
newtio.c_cflag = CS8 | CREAD; /* Set the data bit to 8 bits and enable reception */
newtio.c_cflag & = ~PARENB; /* No parity check */
newtio.c_cflag & = ~ CSTOPB; /* 1 stop bit */
newtio.c_cc[VMIN]= 1; /* Read when a byte of data is received*/
newtio.c_cc[VTIME]= 0; /* Do not use timer */
tcflush( fd,TCIOFLUSH) ; /* Clear the input and output buffers */
tcsetattr( fd,TCSANOW,&newtio) /* Make the set terminal attributes take effect immediately */
}
3
Serial port read and write, serial port closed
After setting the communication parameters, you can use the standard file read and write commands read() and write() to operate the serial port. Finally, before exiting, use the close() function to close the serial port.
void rd_wr( )
{
write( fd,wbuf,10) ;
usleep( 500000) ; /* Delay 50 ms to wait for the lower computer to send data* /
read( fd, rbuf,10) ;
printf( "read string is %s n", rbuf) ;
}
"3 Communication Programming"
The serial communication program between ARM and MCU includes two aspects: On the one hand, it is the serial communication program of ARM as the host computer, and on the other hand, it is the serial communication program of MCU as the slave computer. Before communication, a reasonable communication protocol must be formulated to ensure the reliability and success rate of communication. The communication protocol between the two parties is now agreed as follows:
(1) The baud rate is 9600 bit/s, and the frame format is 1-8-N-1 (1 start bit, 8 data bits, no parity check, 1 stop bit); (2) Since the speed of the upper computer ARM is much higher than that of the lower computer microcontroller, the upper computer takes the initiative to communicate and the lower computer waits. Before data transmission, ARM first sends a communication signal /0xaa, and the microcontroller responds with a /0xbb after receiving it, indicating that it can be sent, otherwise it continues to communicate; (3) The microcontroller side can have interrupt and query methods to send and receive serial port data. This article adopts the interrupt method; (4) The ARM processor s3c2440 uses UART1 to communicate with the microcontroller, and UART0 is used as the s3c2440 terminal console.
3.1 Communication Program Design of Host Computer ARM
Since s3c2440 has transplanted a customized and tailored Linux2.6.32 kernel operating system, the serial port operation method under Linux mentioned above is used for serial port operation. The program flow chart is shown in Figure 2.
3.2 Communication Program Design of Lower MCU
The timer T1 of C8051F021 is selected as the baud rate generator, the crystal oscillator uses 11.0592 MHz, the timer works in mode 2, the initial count value is 0xfd, the serial port works in serial mode 1 (1-8-N-1), and the interrupt mode is used to send and receive data. The program flow chart is shown in Figure 3.
The serial communication program under Linux is compiled with arm-linux-gcc 4.4.3 cross-compilation tool under Linux RHEL5 of PC and mounted on s3c2440 through NFS to run. The communication program on the microcontroller side is compiled with Cygnal's integrated development environment (IDE) and downloaded to C8051F021 for running.
Previous article:Design of car black box based on ARM
Next article:Design and implementation of indoor temperature control system based on ARM
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Playing with Zynq Serial 28——[ex50] The first Zynq system project "Hello Zynq"
- msp430 watchdog settings
- 【DIY Creative LED V2】Comparison of V1 and V2 effects
- Battery power calculation dod0-dodateoc!=Qstart
- How long is the life of lithium batteries?
- Advanced attitude solution: complementary filtering (gyroscope, accelerometer, magnetometer data fusion)
- Can different levels communicate with each other?
- How to burn the program into the ESP8266MOD module
- Design of FPGA verification platform for SoC based on ARM7TDMI.pdf
- Quartus simulation software cannot be opened