ARM-based matrix keyboard design and its linux driver implementation

1 Introduction

ARM microprocessors have been widely used in industrial control, consumer electronics, communications systems and other fields. The matrix keyboard is a commonly used keyboard form. It is designed to be M rows and N columns. This requires a total of M+N signal lines, but it can drive M&TImes; N buttons, which greatly saves I/O resources. This paper introduces a method of extending the 5&TImes; 4 matrix keyboard by using the GPIO port of the TQ2440 development board, and re-laying all the keys into the keyboard form of the handheld terminal for convenient operation.

2. Hardware design

This design extends the matrix keyboard of 5 rows and 4 columns, as shown in Figure 1. The row lines ROW1-ROW5 are connected to the interrupt pins EINT8, EINT9, EINT11, EINT13, EINT14[1] of S3C2440. These interrupt pins are themselves connected to a 10kΩ pull-up resistor to pull the interrupt pin high, ensuring that the interrupt is not triggered when the button is idle. The column line COL1-COL4 is connected to the common I/O ports GPF3, GPF4, GPG7, GPG10 of the S3C2440. The problem to be noted here is to ensure that the interrupt used by the line is not used in other Linux devices, otherwise the driver will be caused. Program or other driver initialization failed.

5 row 4 column matrix keyboard circuit schematic

Taking into account the commonality and ease of operation of the buttons of the handheld terminal device, only the first 18 keys of the matrix keyboard are taken and re-arranged into the form of FIG. The Ent key has a dual function, namely the confirmation function (short press) and the power on/off function (long press), which will be implemented in the driver.

Keyboard layout

3. Matrix driver Linux driver design

3.1 Overview of the keyboard driver

A driver is an interface between an operating system kernel and a hardware device. The device driver shields the application from the details of the hardware, allowing the application to operate the hardware device as if it were a normal file [2]. The driver does not have a main function, it takes a module initialization function as an entry, and it no longer runs after initialization, waiting for a system call.

The driver is part of the linux kernel, so the linux expression should be used in programming. First define the column I/O port as an array: col_table [] ={ S3C2410_GPF3, S3C2410_GPF4, ...}, the row I/O port is defined as a structure:

Button_irqs [] ={ {IRQ_EINT8,S3C2410_GPG0,S3C2410_GPG0_EINT8, 0,"R1"},

{IRQ_EINT9, S3C2410_GPG1, S3C2410_GPG1_EINT9, 1, "R2"},

...}. / / Interrupt number (irq), pin (pin), pin settings, serial number, name

The matrix keyboard is registered as a character device for Linux into the system. We first register the matrix keyboard device with the system, including the device number, device name and file_operaTIons structure; the member function of the file_operaTIons structure is the main content of the character device driver design. These functions will actually be open() in the application. The system calls such as write(), read(), and close() are finally called [3]. The user does not have a write operation on the keyboard, and the member functions of the file_operations structure are open(), read(), close(), and poll().

Interrupt registration and row and column initialization are implemented when the keyboard is opened (ie in the open() function). Registration interrupts include: interrupt number, interrupt entry procedure, interrupt mode, interrupt name and code. The key statements are: request_irq(button_irqs[i].irq,buttons_interrupt,IRQ_TYPE_EDGE_FALLING,button_irqs[i].name,(void*)&button_irqs[i]). IRQ_TYPE_EDGE_FALLING means a falling edge trigger. Then perform row and column initialization: set the row line to interrupt, enable pull-up, in linux its expression is:

S3c2410_gpio_cfgpin(button_irqs[i].

Pin, S3C2410_GPIO_SFN2); / / set the ith line pin as interrupt

S3c2410_gpio_pullup(button_irqs[i].

Pin,1); //The ith row pin pullup

Set the column line to output and set it low. Statements are expressed in the same way. Due to space limitations, they are not listed here.

The read() function implements reading data from the device. This function realizes that the program goes to sleep when no button is pressed. The key code is:

Static DECLARE_WAIT_QUEUE_HEAD(button_waitq); //Generate a wait queue header queue named button_waitq

Static volatile int ev_press = 0; / / set to 1, indicating that there is a key press

When ev_press is 0, the statement is executed: wait_event_interruptible(button_waitq, ev_press), and the program goes to sleep. When ev_press is 1, copy data from kernel space to user space. The key statement:

Copy_to_user(buff,(const void *)key_values,min(sizeof(key_values),count));//buff is a pointer to user space, key_values ​​is a kernel space pointer, and the last parameter is to copy data from kernel space to user space. The number of bytes, we take the minimum of the actual size and the user-specified size. Returns zero if the data is copied successfully; returns the number of data bytes that were not successfully copied when an error occurred.

The close() function implements closing the matrix keyboard device and releasing the registered interrupts. The key statement is free_irq(button_irqs[i].irq,(void *)&button_irqs[i]).

The Poll() function implements polling. If there is no key data, it calls linux's poll_wait function to wait; if there is button data, the select function will return immediately.

3.2 Interrupt Processing and Keyboard Scanner

The name of the interrupt handler is the buttons_interrupt registered above. The specific program flow is shown in Figure 3. When a button is pressed, the row and column of the button are turned on. The low level of the column pulls the row low, which in turn triggers an interrupt. Then, enter the interrupt handler. Due to the jitter of the button, it is unreliable to determine that a button press is triggered by the trigger of one interrupt, so the timer is delayed for 10 ms before entering the keyboard scan function.

Keyboard scanner flow chart

The keyboard scanning program of this design adopts the method of first determining the row and then determining the column, and finally performing a certain operation on the row and column to obtain the key value. First determine the line: progressive scan to determine if there is a row pin low. If so, save the row value (row). Continue to determine the column: low level by column, when the column is pressed, the line will be low again to determine the column (column). Then calculate the row and column: k=row*4+column, then each key of the matrix keyboard is corresponding to the key number 0-19. After the keyboard layout is the form shown in Figure 2, we only take the first 18 keys of the matrix keyboard ( Key number 0-17), the key value is saved as k+1. For the Ent key, it is determined whether the function is on or off by the length of time pressed. The pressing time is less than 0.5 seconds for the confirmation function, and the pressing time is greater than 1.6 seconds. For the on/off function, the time between 0.5 and 1.6 seconds is considered invalid. The timing method is:

If the line is still low and the integer cnt is less than 1700: the delay is 1ms, cnt++; according to the cnt value, the time is pressed.

The power on/off function is saved as the 18th key number, and the key value is 19.

4. Driver testing

The test program belongs to the upper application, and directly calls the interface provided by the keyboard driver to realize the operation of the keyboard. We call the open() function to open the matrix keyboard device, and then call the read() function to read the keyboard data and save it to its own defined array. Finally, use the printf() function to display the test result.

The function is applied to the author's project, and the correct rate and response time of the keyboard input meet the design requirements.

5. Summary

This paper introduces a method of extending the matrix keyboard directly from the ARM I/O port. It does not need to add other interface components, the design is fast and practical, and realizes the driver under the Linux system, and extends the handheld terminal type for the ARM embedded device. The keyboard provides a solution.

High efficient charging speed for Samsung laptop, stable current outlet can offer power for the laptop at the same time charge the laptop battery. The best choice for your replacement adapter. We can meet your specific requirement of the products, like label design. The plug type is US/UK/AU/EU. The material of this product is PC+ABS. All condition of our product is 100% brand new. 

Our products built with input/output overvoltage protection, input/output overcurrent protection, over temperature protection, over power protection and short circuit protection. You can send more details of this product, so that we can offer best service to you!

Samsung Adapter

Samsung Adapter,Charger For Samsung,Power Supply For Samsung,Laptop Charger For Samsung

Shenzhen Waweis Technology Co., Ltd. , https://www.waweis.com