Linux driver-LED Drivers _ 2, linuxled_2
In the previous post, we talked about how to write a driver for the led, but the implementation of the function is relatively simple, next we want to implement the function is on the basis of the previous one can independently control each led lamp.
Link to the previous post: linux driver-LED driver _ 1
The driver source code in the previous post is: click open link test source code at: click open link
Purpose:
Each led on the board can be controlled independently.
Method: 0. Use the device number to access each led1, and change the entry function: assign a device number for each led.
for(minor=0;minor<4;minor++){firstdrv_class_dev[minor] = device_create(firstdrv_class,NULL,MKDEV(major,minor),NULL,"wq_led%d",minor);if(unlikely(IS_ERR(firstdrv_class_dev[minor])))return PTR_ERR(firstdrv_class_dev[minor]);}
2. Similarly, change the egress address:
for(minor=0;minor<4;minor++){device_unregister(firstdrv_class_dev[minor]);}
3. When the node is mounted, four devices in/dev are displayed: wq_led0, wq_led1, wq_led2, and wq_led34. change the open function and add the function of setting each led pin separately: perform operations based on the device number.
Int minor = MINOR (inode-> I _rdev); // switch (minor) {case 0: // configure GPB5 as output {* gpbcon & = ~ (0x3 <(5*2); * gpbcon | = (0x1 <(5*2); break;} case 1: // configure GPB6 as output {* gpbcon & = ~ (0x3 <(6*2); * gpbcon | = (0x1 <(6*2); break;} case 2: // configure GPB7 as output {* gpbcon & = ~ (0x3 <(7*2); * gpbcon | = (0x1 <(7*2); break;} case 3: // configure GPB8 as output {* gpbcon & = ~ (0x3 <(8*2); * gpbcon | = (0x1 <(8*2); break ;}}
5. Change the write function to operate the led Based on the data transmitted from the user space and the sub-device number.
Int minor = MINOR (file-> f_dentry-> d_inode-> I _rdev); // get the device number copy_from_user (& val, buf, count ); // copy data from the user space to the kernel space switch (minor) {case 0: // GPB5 {if (val = 1) // bright {* gpbdat & = ~ (1 <5);} else // destroy {* gpbdat | = (1 <5);} break;} case 1: // GPB6 {if (val = 1) // bright {* gpbdat & = ~ (1 <6);} else // destroy {* gpbdat | = (1 <6);} break;} case 2: // GPB7 {if (val = 1) // bright {* gpbdat & = ~ (1 <7);} else // destroy {* gpbdat | = (1 <7);} break;} case 3: // GPB8 {if (val = 1) // bright {* gpbdat & = ~ (1 <8);} else // destroy {* gpbdat | = (1 <8);} break ;}}Code details:
Driver: Click the open link
Test Program: Click the open link