Design of serial communication between ARM and MCU under Linux

Publisher:快乐舞蹈Latest update time:2013-01-04 Source: 维库开发网Keywords:Linux  ARM  MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
0 Introduction

In the data acquisition system, since the single-chip microcomputer focuses on control and has weak data processing capabilities, it is cumbersome to operate 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 Connection

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 the s3c2440, and the serial port driver of the 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 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. The file descriptor is 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 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.

Operation process

Step 1: Open the serial port and 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 /ttySAC1int fd;fd = open("UART1",O_RDWR) /* Open the serial port device in readable and writable mode */

Step 2: Set the serial port attributes. 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 one is c_cflag. Users can set the serial port baud rate, data bit, stop bit, parity bit and other parameters by assigning values ​​to it. 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 to 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; /* Do not perform parity check* /newtio. c_cflag & = ~ CSTOPB; /* 1 stop bit* /newtio. c_cc[VMIN]= 1; /* Read when one 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* /}

Step 3: Serial port reading and writing, serial port closing After setting the communication parameters, you can use the standard file reading and writing 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",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 MCU, 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 MCU responds with a /0xbb after receiving it, indicating that it can be sent, otherwise it continues to communicate; (3) The MCU 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 MCU, 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. [page]

Program flow chart

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.

Communication Programming

4 Conclusion

With the growing application scope of embedded Linux in China in recent years, embedded Linux devices based on the ARM platform will also be increasingly used in data acquisition as a host computer to process, display, store and send data. The solution introduced in this article is suitable for the serial communication design of ARM and MCU under Linux in most occasions. Designers only need to modify or re-formulate the communication protocol according to their actual needs. In addition, it should be noted that since the speed of the host computer ARM is much faster than that of the MCU, too much data cannot be sent at one time, otherwise it is very likely that the sending buffer will overflow and data loss will occur. Developers should choose the appropriate frame length according to the status of the devices on both sides of the communication to achieve the best transmission state.

Keywords:Linux  ARM  MCU Reference address:Design of serial communication between ARM and MCU under Linux

Previous article:Design of low-power speech denoising system based on ARM
Next article:Research and Application of Image Data Acquisition System Based on ARM7

Recommended ReadingLatest update time:2024-11-16 18:08

Several MCU download line application circuits
    ByteBlaster download line circuit diagram  ByteBlaster MV download line circuit diagram    ByteBlaster II download line circuit diagram Ispdown download line interface standard Programming of     Atmel MCU     1. ISP method of AT89S series     1. The schematic diagram of AT89S series ISP is shown in
[Microcontroller]
Several MCU download line application circuits
The following factors affect the power consumption of PIC microcontrollers:
I have been working on the power consumption of PIC microcontrollers. Since the project is powered by batteries, the power consumption issue is very important. According to the data sheet and information on the Internet, the power consumption of microcontrollers is mainly affected by the following factors:   1: All I/
[Microcontroller]
How to mount the NFS development environment on the ARM development board (mini2440 diskless boot)
This part is still difficult to do. Imagine if you can't mount the network disk through nfs, the method he proposed would be useless. In this way, it is perfect. Before you experiment, you can mount it now (use busybox to generate the file system without compiling) to confirm that there is no problem before compiling a
[Microcontroller]
51 MCU learning summary example
I will be a senior soon and will be looking for a job soon, so I want to summarize and systematize what I have learned. Next, I will share the results of my summary with you. This chapter is about 51 single-chip microcomputer, mainly program examples. The code refers to Guo Tianxiang's single-chip microcomputer text
[Microcontroller]
Analysis of the internal structure and program execution process of 51 microcontroller
Note: 1. ROM: Read-only memory. The read-only storage area of ​​a microcontroller is mostly used to store programs, also called program memory. The 51 microcontroller is an EPROM, and the so-called EPROM is an erasable read-only memory. You can erase the original program and write a new one. It can be saved after
[Microcontroller]
Analysis of the internal structure and program execution process of 51 microcontroller
ARM9-based addressing mode
What is addressing: the method of addressing the address of the operand specified in the processor instruction system; or the method by which the operand is found; Instructions and instruction formats ARM instruction format:  Opcode { Condition } {S} Destination register , First operand {, Second operand}  : The con
[Microcontroller]
ARM9-based addressing mode
Design of floating point numbers and assembly program for C51 single chip microcomputer
In the data processing process of the single-chip microcomputer application system, decimal calculation problems are often encountered, such as solving the incremental formula of BCD, linearization processing, etc. Therefore, it is necessary to use binary numbers to represent decimals. There are generally two ways to r
[Microcontroller]
Distributed detection method of vibration signal based on single chip microcomputer and sensor
Introduction  Due to the distance limit of the connecting cable of the acceleration sensor and the charge amplifier, it is difficult to implement a centralized data acquisition and processing system in a space with a long distance, and its reliability and anti-interference problems are difficult to solve. Distributed
[Microcontroller]
Distributed detection method of vibration signal based on single chip microcomputer and sensor
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号