“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 single-chip microcomputer under Linux, and gives the hardware connection and communication program flowchart. This method is reliable and practical, and is suitable for most Linux ARM and MCU serial communication occasions.
“
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 single-chip microcomputer under Linux, and gives the hardware connection and communication program flowchart. This method is reliable and practical, and is suitable for most Linux ARM and MCU serial communication occasions.
In the data acquisition system, because the single-chip microcomputer focuses on control, the data processing ability is weak, and it is more cumbersome to calculate and process the collected data. If you communicate with the upper computer through the serial port, use the powerful data processing ability of the upper computer and the friendly control interface to perform the data processing. Processing and Display can improve design efficiency. Serial communication has become the first choice for communication between upper and lower computers with its simple hardware connection and mature communication protocol. The s3c2440 ported with the Linux operating system can operate the serial port in the Linux environment, which reduces the difficulty of serial port operation and enables developers to concentrate on developing large-scale applications without having to spend time on operating the underlying design.
1 Hardware connection
s3c2440 is a processor based on the ARM9 core produced by Samsung, and uses 3.3 V power supply; C8051Fxxx series single-chip microcomputers are 8051-compatible high-performance high-speed single-chip microcomputers launched by American CYGNAL company, and use 3.3 V voltage power supply. Both have the same power supply voltage, so there is no need to perform level conversion during serial port communication. The hardware connection adopts the most commonly used TXD, RXD, GND three-wire connection. Pay attention to the cross connection method, namely TXDRXD, RXDTXD.
2 Serial communication under Linux
2.1 Description of serial device under Linux
The Linux 2.6.32 operating system is ported to s3c2440, and the serial port driver of s3c2440 is loaded. Through the serial port operation function and file operation function provided by Linux, the operation of the serial port is equivalent to the file operation, which reduces the difficulty of serial port operation and improves efficiency. . In the program, devices and files are operated through file descriptors, which are a non-negative integer in the Linux kernel. Linux device files are stored in the “/dev” directory, and the serial port is no exception. The device file corresponding to the serial port can be found in /dev. The device file path of the serial port 1 in this article is “/dev/ttySAC1”.
2.2 Serial communication program design under Linux
Serial communication needs to set some parameters, such as baud rate, data bit, stop bit, input and output mode, etc. These parameters all exist in the termios structure provided by Linux, which is a standard interface used by the Linux system to query and operate various terminals, defined in the header file, 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 communication steps can be divided into the following three steps, the operation process is shown in Figure 1.
1 Open the serial port
Call the open() function to open the serial device file, if an error occurs, it returns -1, and if it succeeds, it returns the file handle.
#define UART1 /dev /ttySAC1
int fd;
fd = open (“UART1”,O_RDWR) /* Open the serial device in a readable and writable way* /
2 Set serial port properties
The function tcsetattr () can set the structural attributes of the serial port, and tcgetatt () can get the structural attributes of the serial port. In the termios structure, the most important is c_cflag. The user can set the serial port baud rate, data bit, stop bit, parity bit and other parameters by assigning it. The two variables VMIN and VTIME in the c_cc array determine whether to return the input, c _cc[VTIME]Set byte input time timer, c _cc[VMIN]Set the minimum number of received bytes that satisfy the read function. The values of these two variables must be set reasonably to ensure the success rate of serial communication.
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); /* flush input and output buffer * /
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, wait for the lower computer to send data* /
read(fd, rbuf,10);
printf(“read string is %sn”, rbuf);
}
3 Communication program design
The serial communication program of ARM and single-chip microcomputer includes two aspects: one is the serial communication program of ARM as the upper computer, and the other is the serial communication program of the single-chip microcomputer as the lower computer. Before communication, a reasonable communication protocol must be established to ensure the reliability and success rate of communication. It is now agreed that the communication agreement between the two parties is 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, 1 stop bit); (2) Due to the upper position The speed of the ARM is much higher than that of the MCU of the lower computer, so the upper computer actively contacts and the lower computer waits. ARM sends the contact signal /0xaa before data transmission, and the microcontroller responds with a /0xbb after receiving it, indicating that it can be sent, otherwise the contact will continue; (3) The microcontroller can send and receive serial data through interrupt and query methods. This article uses interrupt mode; (4) ARM processor s3c2440 uses UART1 to communicate with the microcontroller, while UART0 serves as the s3c2440 terminal console.
3.1 The communication program design of the host computer ARM
Since s3c2440 has transplanted the customized and tailored Linux2.6.32 kernel operating system, the operation of the serial port adopts the above-mentioned serial port operation method under Linux, and the program flow chart is shown in Figure 2.
3.2 The communication program design of the single-chip microcomputer of the lower computer
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), using interrupts Way to send and receive data. The program flow chart is shown as in Fig. 3.
The serial communication program under Linux uses arm-linux-gcc under the PC Linux RHEL5. 4.4.3 The cross-compilation tool is compiled to run on s3c2440 through NFS, and the communication program on the microcontroller side is compiled and downloaded by Cygnal integrated development environment (IDE) Run in C8051F021.
The Links: NL6448BC26-26D NL8060BC2635BA IGBTMODULE