Implementation of the driver for flashing LED light on tq2440

Source: Internet
Author: User

The main points of driver implementation in this article: first, automatic creation of device files is realized. You do not need to use the mknod command to automatically create device files every time you run the driver. This article uses udev (mdev) to automatically create device files. Second, the control of LED lights is not achieved by directly setting the binary bits of the relevant gpio. This article uses the operating functions of the S3C2410 gpio provided by the Linux system to directly implement the control of the relevant gpio. The third is to achieve the flashing effect of the LED lamp. Although this article does not directly implement the flashing effect of the LED lamp in the driver code, however, IOCTL in the driver is called by upper-layer applications to indirectly achieve the flashing effect of LED lights.


1. driver source 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> // defines gpio of S3C2410, s3c2410_gpb5 to s3c2410_gpb8 # include <Mach/hardware. h> // define the gpio function # include <Linux/device. h> // automatically create the header file that the device file should contain # define device_name "my_led" // load the module and run the device name shown in CAT/proc/devices # define led_major 103 // master device # define led_on 1 # define led_off 0 // led control pin // note that s3c2410_gpb5 is the gpio number, the rule that defines the type as unsigned long // number is to uniformly number all Io ports starting from 0, such as struct = 0 s3c2410_gpa1 = 1 s3c2410_gpb0 = 32 static unsigned long led_table [] = {s3c2410_gpb5, s3c2410_gpb6, struct, struct,}; static int my_led_open (struct inode * inode, 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 0 return 0; Case led_off: s3c2410_gpio_setpin (led_table [Arg], 1); // set the specified pin to 1 return 0 for the output level; default: Return-1 ;}/// defines the file operation file_operationsstatic struct file_operations my_led_fops = {. owner = this_module ,. open = my_led_open ,. IOCTL = my_led_ioctl,}; static struct class * led_class; static int _ init my_led_init (void) {int ret; printk ("my_led start \ n "); // register the device driver // The main device number, device name, And file_operations structure. // 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, enable mdev to create a device node in 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); printk (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 )); // deregister the device node class_destroy (led_class); // deregister class} module_init (my_led_init); module_exit (my_led_exit); module_license ("GPL ");

 

Source code analysis:

1. In this driver, the device file is automatically created by calling class_create in the driver initialization code to create a class for the device, and then calling device_create to create the corresponding device for the device. Class_create and device_create are defined in the kernel source code include/Linux. You can view their definitions in this directory.

2. The driver controls the output of the specified pins through the operating function s3c2410_gpio_setpin of the S3C2410 gpio provided in the kernel. This function is defined in the header file of hardware. H. You must add # include <Mach/hardware. h> to the driver.

3. For the implementation of the flashing effect of the LED lamp, list the upper-layer application code before analyzing it.


Ii. MAKEFILE file:

obj-m:=My_led.oCC=arm-linux-gccKERNELDIR=/usr/local/opt/EmbedSky/linux-2.6.30.4PWD:=$(shell pwd)default:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesclean:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install


3. the source code of the driver's upper-layer application is my_led_test.c.

#include <stdio.h>  #include <stdlib.h>  #include <unistd.h>      #include <sys/types.h>    #include <sys/stat.h>  #include <fcntl.h>    int main()  {      int fd,i,cmd=0;          fd=open("/dev/My_led",0);     if (fd<0)      {          printf("open led_driver error");          exit(1);      }    while(1)    {       switch(cmd)       {          case 0:          printf("All off\n");          for(i = 0;i < 4;i ++)              ioctl(fd,0,i);           for(i=0;i<100;++i);                    case 1:          printf("light first led\n");          ioctl(fd,1,0);           for(i=0;i<100;++i);                    case 2:          printf("light second led\n");          ioctl(fd,0,0);          ioctl(fd,1,1);           for(i=0;i<100;++i);                    case 3:          printf("light third led\n");          ioctl(fd,0,1);          ioctl(fd,1,2);           for(i=0;i<100;++i);                    case 4:          printf("light fourth led\n");          ioctl(fd,0,2);          ioctl(fd,1,3);           for(i=0;i<100;++i);                    case 5:          printf("All light \n");          for(i = 0;i < 4;i ++)          ioctl(fd,1,i);           for(i=0;i<100;++i);       }    }       return 0;   }


Source code analysis:

Case0, case1, case2, case3, case4, and case5 respectively indicate that all four lights are off, the first, the second, the third, and the fourth, respectively, the four cases use an empty loop for each other to achieve the effect of latency, and ultimately achieve the flashing effect of LED lights.



This article from the "can't stop thinking" blog, please be sure to keep this source http://9110091.blog.51cto.com/9100091/1546949

Implementation of the driver for flashing LED light on tq2440

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.