Drive 01.LED, led Drive Power Supply

Source: Internet
Author: User

Drive 01.LED, led Drive Power Supply

1. Write the leds_open and leds_write functions.
2.1 tell the kernel about the existence of these functions? Define a struct file_operations
2.2 tell the kernel about this struct? Use register_chrdev (major, name, file_operations)
// Register the master device number with the file_operations structure with the kernel
// Major = register_chrdev (0, name, file_operations) indicates that major is automatically allocated by the system;
3. Who will call register_chrdev? The first_drv_init entry function with a driver
4. How do I know that the entry function is first_drv_init? Use the module_init function to modify the entry function ,\
When the kernel is started, find the module_init struct. Eg: module_init (first_drv_init)
5. Of course, there will also be operations to deregister the corresponding function;
Eg: unregister_chrdev (major, "first_drv ")
Module_exit (first_drv_exit)
6. Add some necessary header files in the same way as other programs.
7. To enable the driver to support automatic creation of/dev/xxx, mdev must be supported in the driver.
7.1 mdev creates a device node (sys/class/firstdev) based on system information. Two struct structures need to be defined.
Static struct class * firstdrv_class;
Static struct class_device * firstdrv_class_dev;
7.2 Add the following two codes to first_drv_init:
Firstdrv_class = class_create (THIS_MODULE, "firstdrv ");
Firstdrv_class_dev = class_device_create (firstdrv_class, NULL, MKDEV (major, 0), NULL, "xyz");/*/dev/xyz */
Similarly, add the following two codes to first_drv_exit:
Class_device_unregister (firstdrv_class_dev );
Class_destroy (firstdrv_class );
/* Guess: Put firstdrv in the structure of firstdrv_class, and then create a device node using class_device_create */
8. Because the driver cannot directly operate on the physical address, you need to map a physical address to the virtual address.
View the 2440 manual, obtain the corresponding physical address, use the iorema function to complete the ing, and use iounmap to cancel the ing;
Eg: gpfcon = (volatile unsigned long *) ioremap (0x56000050, 16); // use volatile to prevent Compiler Optimization and must be checked every time.
Iounmap (gpfcon );
9. Add MODULE_LICENSE ("GPL"); // You must append this statement to the ko driver. Otherwise, the insmod driver cannot be connected to the symbols in/proc/kallsyms.
You can use modinfo xxx. ko to view the module on which it depends. It can be seen that licens depends on GPL.
10. Modify the last row of makefile to obj-m + = first_drv.o.
Put first_drv in the file location corresponding to makefile, execute make to get the first_drv.ko file, and use insmod, rmmod, lsmod, and modinfo to perform operations on it;
11. Test the driver
Arm-linux-gcc-o firstdrvtest. c
Perform operations according to the test program;
12. Because 2.6.22.6 kernel is used, you must use the gcc version 3.4.5 for cross-compilation. Otherwise, it cannot run.

1 # include <linux/module. h> 2 # include <linux/kernel. h> 3 # include <linux/fs. h> 4 # include <linux/init. h> 5 # include <linux/delay. h> 6 # include <asm/uaccess. h> 7 # include <asm/irq. h> 8 # include <asm/io. h> 9 # include <asm/arch/regs-gpio.h> 10 # include <asm/hardware. h> 11 12 13 static struct class * ptFirstdrvClass; 14 static struct class_device * ptFirstdrvClassDev; 15 16 volatile unsigned long * pulgpfcon = NULL; 17 volatile unsigned long * pulgpfdat = NULL; 18 19 static int firstdrv_open (struct inode * inode, struct file * file) 20 {21 // printk ("first_drv_open \ n"); 22/* configure GPF4, 5 or 6 as output */23 * pulgpfcon & = ~ (0x3 <(4*2) | (0x3 <(5*2 )) | (0x3 <(6*2); 24 * pulgpfcon | = (0x1 <(4*2 )) | (0x1 <(5*2) | (0x1 <(6*2); 25 return 0; 26} 27 28 static ssize_t firstdrv_write (struct file * file, const char _ user * buf, size_t count, loff_t * ppos) 29 {30 int val; 31 32 // printk ("first_drv_write \ n"); 33 34 copy_from_user (& val, buf, count); // copy_to_user (); 35 36 if (val = 1) 37 {38 // lighting 39 * pulgpfdat & = ~ (1 <4) | (1 <5) | (1 <6 )); 40} 41 else42 {43 // lamp removal 44 * pulgpfdat | = (1 <4) | (1 <5) | (1 <6 ); 45} 46 47 return 0; 48} 49 50 static struct file_operations firstdrv_ops = {51. owner = THIS_MODULE,/* This is a macro. The _ this_module variable */52 is automatically created when the module is pushed to compilation. open = firstdrv_open, 53. write = firstdrv_write, 54 55}; 56 57 int g_iMajor; 58 static int firstdrv_init (void) 59 {60 g_iMajor = register_chrdev (0, "first_drv", & firstdrv_ops ); 61 ptFirstdrvClass = class_create (THIS_MODULE, "firstdrv"); 62 ptFirstdrvClassDev = class_device_create (ptFirstdrvClass, NULL, MKDEV (g_iMajor, 0), NULL, "xyz "); /*/dev/xyz */63 64 pulgpfcon = (volatile unsigned long *) ioremap (0x56000050, 16); 65 pulgpfdat = pulgpfcon + 1; 66 67 return 0; 68 69 70} 71 72 static int firstdrv_exit (void) 73 {74 values (g_iMajor, "first_drv"); 75 class_device_unregister (values); 76 class_destroy (ptFirstdrvClass ); 77 78 iounmap (pulgpfcon); 79 80 return 0; 81 82} 83 84 85 86 module_init (firstdrv_init); 87 88 module_exit (firstdrv_exit); 89 90 MODULE_LICENSE ("GPL ");

 

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.