Abstract: The Linux operating system is highly open and portable. It is widely used in embedded operating systems. This article first builds the hardware environment based on Samsung's S3C2410 processor, and then explores the touch screen driver in the embedded Linux operating system. Program And then the touch screen sampling data processing method. Finally, the touch screen calibration principle is elaborated in detail.
Keywords: Linux driver sampling processing Calibration
Introduction
With the development of computer-related technologies, embedded systems are widely used and closely integrated with people's daily lives. Touch screen devices are widely used in embedded fields because of their friendly human-computer interaction, convenient and flexible operations, and fast input speed. Embedded Linux system with developmentSource codeKernel stability and scalability attract the eyes of many commercial companies and free software developers and become one of the indispensable Operating Systems in the embedded system. Touch screen is a type of input device, which is easy to learn and easy to operate, with no extra space and high reliability. It is the most commonly used input device for portable systems. In particular, the structure of the resistive touch screen is simple and simple, with low cost and good light transmission performance. The working environment is completely isolated from the outside world, without fear of dust and moisture. It also features high resolution, high-speed transmission and response, and one-time correction, it features high stability and non-drift, so it is widely used in industrial control and indoor use.
1Hardware Platform
Samsung S3C2410 is a MCU based on arm t. It integrates many peripheral devices, including a 4-wire resistive touch screen controller and 8 analog input channels. In 8 A/D conversion channels, a [5] and a [7] are used as acquisition channels for the X and Y coordinates of the touch screen. The circuit diagram of the connection between S3C2410 and the 4-wire resistive touch screen is shown in Figure 1.
In the sampling process, you only need to read and write a series of special registers, the S3C2410 touch screen controller will automatically control the touch screen interface to open or close each MOS tube, collect the X and Y coordinate data of the touch screen in sequence.
Figure 1 circuit diagram of 4-wire resistive touch screen connected by S3C2410
2LinuxDevice Drivers
A device driver is an interface between the operating system kernel and the machine hardware. It consists of a group of functions and some private data, and serves as a bridge between applications and hardware devices. In the application's view, a hardware device is just a device file, and an application can operate the hardware device like a common file. The device driver is a part of the kernel and mainly implements the following functions: Device initialization and release; data is transmitted from the kernel to the hardware device and data is read from the hardware device; read application data transmitted to device files and send back application request data; detect and process hardware device errors.
Linux operating system devices include character devices, Block devices, and network devices. Character devices perform sequential read/write operations in a single byte. The caching technology is not used, such as the mouse and keyboard. Block device read and write are supported by the cache technology, and must be able to perform random access, mainly for disks and other slow devices. Network devices are mainly based on the BSD socket mechanism, which provides caching technology for sending and receiving data and support for multiple protocols, such as Ethernet cards.
3LinuxTouch screen driver implementation
In Linux, the device driver is a collection of related functions. It includes device service subprograms and interrupt handling programs. The device service subroutine contains all device-relatedCodeEach setup-slave service subroutine processes only one device or closely related device, and receives and executes abstract commands from device-independent software. When a request is executed, the specific operation is based on the Access Port provided by the Controller to the driver, and the interrupt mechanism is used to call the interrupt service subroutine to complete the request with the device. The device driver uses the file_operations structure to associate with the file system. The entry functions of various operations of the device are placed in the file_operations structure, including open (), release (), read () and write () interfaces to simplify the preparation of the driver. In this way, applications do not have to consider whether to operate devices or common files.CompositionProcessing, with clear and unified I/O interfaces. The file_operations structure of the touch screen is defined as follows:
Static struct file_operations s3c2410_ts_fops = {
Owner: this_module,
Read: s3c2410_ts_read,
Poll: s3c2410_ts_poll,
IOCTL: s3c2410_ts_ioctl,
Open: s3c2410_ts_open,
Release: s3c2410_ts_release,
Fasync: s3c2410_ts_fasync ,};
In the development of touch screen device drivers, the global variable struct s3c2410_ts_device global_ts is very important, the data structure struct s3c2410_ts_device is defined as follows:
Struct s3c2410_ts_device {// management touch screen class
Struct s3c2410_ts_general D; // touch screen setting parameters
Struct s3c2410_ts_calibration Cal; // calibrate touch screen parameters
Struct s3c2410_ts_event Buf [mousebuf_size]; // a buffer queue waiting for processing
Struct s3c2410_ts_event cur_data, samples [3], last_data ;};
// Current sample data, original sample data, last sample data
After learning about the above concepts, it is not complicated to compile the touch screen driver. The following work needs to be done:
3.1 module initialization Function
It is implemented by calling s3c2410_touchscreen_moudle_init (). It mainly completes the loading, initialization, interrupted registration, and device registration of the kernel module of the touch screen device. The main process involved is as follows:
Adctsc = (0 <8) | (1 <7) | (1 <6) | (0 <5) | (1 <4) | (0 <3) | (0 <2) | (3 );
// Settings of the touch screen adctsc
Adcdly = adc_delay_time; // touch screen start and interval Delay
Adccon = (1 <14) | (prescale_n <6) | (7 <3) | (0 <2) | (0 <1) | (0 );
// Touch screen controller settings
Request_irq (irq_adc_done, ts_down_interrupt, sa_interrupt, g_ts_id, ts_down_interrupt); // apply for irq_adc_done interruption
Request_irq (irq_tc, ts_up_interrupt, sa_interrupt, g_ts_id, ts_up_interrupt );
// Apply for irq_tc interruption
Devfs_register_chrdev (0, s3c2410_ts_module_name, & s3c2410_ts_fops );
// Register the file_operations Structure
Request_irq (irq_timer1, touch_timer_irq, sa_interrupt, g_ts_timer_id, null); // apply for irq_timer1 interruption
Touch_timer_irq (int irq, void * dev_id, struct pt_regs * regs)
// Call the touch interrupt according to the status to Control Data Sampling
The initialization module uses the request_irq function provided by the kernel to register the hitting and popping interrupt numbers to associate the interrupt number with the interrupt service function. The devfs_register_chrdev function is used, register a timer device with the system, and register a timer interrupt to control the data sampling of the touch screen.
3.2 set the touch pen status and corresponding processing
The Touch Screen interrupt service functions ts_down_interrupt and ts_up_interrupt are used to select the touch pen status based on the settings of adcdat1 and adcdat0, and then call the data sampling processing function of the touch screen coordinates s3c2410_ts_handler. Some code is as follows:
Static void ts_down_interrupt (int irq, void * dev_id, struct pt_regs * regs)
{
If (adcdat1 & 0x8000) | (adcdat0 & 0x8000 )){
Pen_data.state = pen_up;
}
Else {
Pen_data.state = pen_down;
}
S3c2410_ts_handler ();}
3.3 obtain the sample value
The touch pen has three working states: pen_up, pen_down, and pen_sample. In the sampling processing function, based on the touch pen status, call ts_timer_operation () to start or stop the sampling timer, and then call s3c2410_ts_handler () to perform different settings and processing based on different states, then different sample values are obtained.
3.4 process the obtained touch screen data.
It is implemented by calling the data_processing () function. The following median filtering method is used to process the sampled data. For the noise generated during the coordinate sampling process, the average method is generally used to remove the noise. However, this method has a small number of samples and a large number of noise sampling points, obtaining the average value will cause a large error in the final result and will not meet the data processing requirements. Therefore, this article uses the median filter method to filter out interference noise and further improve the sampling accuracy.
The principle of the mean-value filtering method is as follows: first, an odd number of touch sampling data is obtained, and then the data is sorted in ascending order based on the sample data size. Finally, the value at the intermediate position is obtained. This method can effectively reduce the error when there are not many sampling points and the error of some sampling data is large. The specific process is shown in figure 2.
3.5 touch screen Calibration
In practical applications, the touch screen is usually used in combination with the display screen as an input device. The coordinates obtained from the touch screen sampling must be mapped to the display coordinates of the screen. The touch screen and display screen are both standard rectangles, as shown in 3. The X-direction coordinate of the touch screen is only related to the X-direction of the display screen, and the Y-direction coordinate is only related to the Y-direction of the display screen.
Assume that the resolution of the display screen is W × H. The sampling coordinates of the touch screen in the upper left corner of the display area are (x1, Y1), and those in the lower right corner are (X2, Y2 ), then, the correspondence between any point on the touch screen and the display coordinate (XD, YD) can be calculated according to the following formula:
According to the above formula, the display coordinates corresponding to the actual touch screen are calculated, followed by a touch screen calibration process. In this paper, the three-point calibration method is used, compared with the two-point calibration, the three-point calibration model takes into account disguised form and rotation, which is closer to the actual situation. First, three sampling points are selected for calibration input, and their corresponding touch screen sampling coordinates are P0 (x0, y0), P1 (x1, Y1), P2 (X2, y2). The displayed coordinates are pd0 (xd0, yd0), pd1 (XD1, yd1), and Pd2 (xd2, yd2 ). The two points P and Pd in the Cartesian coordinate plane define P as the coordinate point of the touch screen space, PD as the coordinate point of the display space, and P can be obtained through rotation, proportional and horizontal motion. Simplified:
The above formula shows that there is a linear relationship between Pd and P points to meet the following requirements: XD = AX + by + c yd = dx + ey + F
For the same device, A, B, C, D, E, and F are constants, which are called calibration constants. Therefore, you only need to correct the six Constants on the touch screen, the screen space can be converted to the display space.
3.6 interrupted release and registration module uninstallation
It is implemented by calling s3c2410_ts_cleanup_module (). The interfaces of irq_timer1, irq_adc_done, irq_tc, and character device are released during the initialization process respectively. The devfs_register_chrdev () functions are as follows:
Free_irq (irq_timer1, g_ts_id );
Free_irq (irq_adc_done, g_ts_id );
Free_irq (irq_tc, g_ts_timer_id );
Devfs_unregister_chrdev (gmajor, h3600_ts_module_name); // uninstall the character device
4Conclusion
The author's innovation points: This paper introduces in detail the development process of the touch screen Driver Based on the embedded Linux operating system based on the actual hardware platform, and improves the method for processing the sampled data, at last, the common calibration methods are improved. The Touch Screen driver can meet the actual requirements. The touch screen driver has been used in real embedded products and runs stably and reliably. It has good development prospects and social and economic benefits.