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.
Step 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 /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. 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; /* 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.
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.
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.
Previous article:Application of AVR microcontroller in LED remote control lighting
Next article:Charging method based on high voltage lithium-ion battery pack
- MathWorks and NXP Collaborate to Launch Model-Based Design Toolbox for Battery Management Systems
- STMicroelectronics' advanced galvanically isolated gate driver STGAP3S provides flexible protection for IGBTs and SiC MOSFETs
- New diaphragm-free solid-state lithium battery technology is launched: the distance between the positive and negative electrodes is less than 0.000001 meters
- [“Source” Observe the Autumn Series] Application and testing of the next generation of semiconductor gallium oxide device photodetectors
- 采用自主设计封装,绝缘电阻显著提高!ROHM开发出更高电压xEV系统的SiC肖特基势垒二极管
- Will GaN replace SiC? PI's disruptive 1700V InnoMux2 is here to demonstrate
- From Isolation to the Third and a Half Generation: Understanding Naxinwei's Gate Driver IC in One Article
- The appeal of 48 V technology: importance, benefits and key factors in system-level applications
- Important breakthrough in recycling of used lithium-ion batteries
- 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
- There is a circuit for fast shut-off of MOS tube. Does anyone know the specific working principle? How to shut down quickly?
- Application of Field Effect Transistor
- What is overshoot? How to solve the problem of overshoot in high-speed circuit signals
- Where can I find the development environment for the domestically produced aerospace-grade CPU of the BM3803MGRH model?
- What is the difference between using * and ? in the day of the week in cron expressions? I checked some information but it's not clear.
- GD32E231 DIY Contest Brushless Motor Driver
- FAQ Power Management, Battery Management
- Together wow: Smart soldering iron based on domestic chip, portable soldering iron system IronOS (FreeRTOS)
- How to add code to ardunio ide
- Blackboard with 24 touch keys, 9 customizable LEDs