"Linux driver" TQ2440 led driver

Source: Internet
Author: User

★ General Introduction

LED driver mainly realizes the hardware drive of 4 LED lights on the TQ2440 Development Board, realizes the Pin GPIOB5, GPIOB6, GPIOB7, The high and low level setting of the GPIOB8 (the configuration of the PIN is already implemented in the COMMON-SMDK.C), the driver is called with the test program, and the LED light is illuminated by command.

★ Detailed INTRODUCTION 1, Driver code: MY_LED.C

#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h > #include <linux/delay.h> #include <asm/irq.h> #include <mach/regs-gpio.h> #include <mach/ hardware.h> #include <linux/device.h> #define DEVICE_NAME "my_led"/** the device name you see in cat/proc/devices after loading the module **/# Define LED_MAJOR 103/** main device number **/#define LED_ON 1#define led_off 0/**led control pin **/static Unsigne D Long led_table[] ={s3c2410_gpb5, s3c2410_gpb6,s3c2410_gpb7,s3c2410_gpb8,};static int my_led_open (struct inode *i    Node,struct file *file) {PRINTK ("my_led open\n"); return 0;}  static int My_led_ioctl (struct inode * inode, struct file * file,unsigned int cmd,unsigned long arg) {if (Arg > 4) {return                    -1;        }switch (cmd) {case Led_on:s3c2410_gpio_setpin (Led_table[arg], 0);//Set the specified PIN to the output level of 0 return 0;  Case Led_off:s3c2410_gpio_setpin (Led_table[arg], 1);//Set the specified PIN to the output level of 1 return 0; default:return-1;}} static struct file_operations my_led_fops ={.owner = This_module,.open = My_led_open,.ioctl = My_led_ioctl,};static Struc T class *led_class;static int __init my_led_init (void) {int ret;printk ("my_led start\n"),/** register character device driver **//** parameter as main device number, Device name, file_operations structure **//** so the main device number is associated with file_operations **/ret = Register_chrdev (Led_major, Device_name, &my_ Led_fops); if (Ret < 0) {PRINTK ("can ' t register major number\n"); return ret;} Register a class so that Mdev can establish a device node under the/dev/directory Led_class = class_create (This_module, Device_name), if (Is_err (Led_class)) {PRINTK (" Failed in my_led class.\n "); return-1;} Device_create (Led_class, NULL, MKDEV (led_major,0), NULL, device_name);p rintk (device_name "initialized\n"); return 0;}    static void __exit my_led_exit (void) {Unregister_chrdev (led_major, device_name); Device_destroy (Led_class, MKDEV (led_major,0));//Unregister the device node Class_destroy (led_class);//Unregister Class}module_init (My_led_init); Module_exit (My_led_exit); Module_license ("GPL");

2. Macro definition

#define DEVICE_NAME "my_led"//Load module to perform the device name seen in Cat/proc/devices
#define LED_MAJOR 103//main device number
#define LED_ON 1
#define The parameter command to be entered in the Led_off 0 my_led_ioctl function, led_on executes the command to turn on the light, led_off the command to perform the Turn off light.

3, LED lamp pin control

Define a led_table[] array, which is easier to invoke later.

Describe in detail what S3C2410_GPB5 means:




As shown, it is all code related to S3C2410_GPB5. The above code can conclude that S3C2410_GPB5 is 37. In fact S3C2410_GPB5 is the number of the port, the bank is the base number of the group, offset is the offsets within the group. The fifth pin of the GPIOB for the TQ2440 Development Board is 37.

4. My_led_ioctl () function

This function is primarily responsible for responding to the application's related commands, and then executes the associated code according to the command. The cmd command includes the led_on, Led_off, and macro definitions.

S3c2410_gpio_setpin () function

Think of this as the most important function of this driver, which is to set the specified pin to low or high level. This driver only uses the interface provided by the kernel to achieve the control of the pin, but it will certainly look at this function.

5, registration of a class

If you do not register this class, then when you mount the driver on the board, one more thing to do is to manually "generate the device node" or use the Mknod command. However, the driver begins to work with the operating system, so the device node is generated automatically, not manually. That's why you register a class that automatically generates a device node after the driver is mounted. The core code of the registration class:

Led_class = Class_create (This_module, Device_name), if (Is_err (Led_class)) {PRINTK ("failed in my_led class.\n"), Return- 1;} Device_create (Led_class, NULL, MKDEV (led_major,0), NULL, device_name);

6. Test procedure

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #in          Clude <sys/stat.h> #include <fcntl.h> int main () {int fd,i,cmd;     Fd=open ("/dev/my_led", 0);          if (fd<0) {printf ("Open led_driver error");      Exit (1);    } while (1) {scanf ("%d", &cmd);    Switch (CMD) {case 0:printf ("All off\n");     for (i = 0;i < 4;i + +) IOCTL (fd,0,i);    Break;case 1:printf ("Light First led\n");     IOCTL (fd,1,0);                    Break;case 2:printf ("Light Second led\n");    IOCTL (fd,0,0);     IOCTL (fd,1,1);                    Break;case 3:printf ("Light third led\n");    IOCTL (fd,0,1);     IOCTL (fd,1,2);                    Break;case 4:printf ("Light Fourth led\n");    IOCTL (fd,0,2);     IOCTL (fd,1,3);    Break;case 5:printf ("All light \ n");     for (i = 0;i < 4;i + +) IOCTL (fd,1,i);    BREAK;DEFAULT:I = 10; Break;    } if (i = = ten) break;   } return 0;   }

★ Errors encountered

1. File not Found

The main reason for this error is that the kernel version differs, and the path to the header files of different kernels is different. Therefore, when compiling the driver error "File not found", check the path of the header file is correct

2. Makefile file specifies cross compiler

The following is a driver makefile file

#LED_makefileKERNELDIR: =/home/xg/linux_arm/linux-2.6.30.4/pwd: =$ (Shell PWD) all:make-c $ (kerneldir) m=$ (PWD) Modules Arch=arm cross_compile=/opt/opt/embedsky/4.3.3/bin/arm-linux-test:/opt/opt/embedsky/4.3.3/bin/ Arm-linux-gcc-o Test TEST.CCLEAN:RM-RF *.o *koobj-m: =MY_LED.O
The error was that the path of the compiler was not clear.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.