本文共 1737 字,大约阅读时间需要 5 分钟。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include <..>
#define DEVICE_NAME "led" /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define LED_GPIO MXS_PIN_TO_GPIO(PINID_LCD_D23) //硬件引脚
static struct class *led_class;
static struct class_device *led_class_devs;
static int led_open(struct inode *inode, struct file *file)
{
printk("led open ok\n");
gpio_request(LED_GPIO, "led");
return 0;
}
static int led_release(struct inode *inode, struct file *filp)
{
gpio_free(LED_GPIO);
return 0;
}
ssize_t led_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
char data[2];
copy_from_user(data, buf, count);
gpio_direction_output(LED_GPIO, data[0]);
printk("write data:%d \n",data[0]);
return count;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = led_open,
.write = led_write,
.release = led_release,
};
int major;
static int __init led_init(void)
{
major = register_chrdev(0, DEVICE_NAME, &led_fops); //主设备号为0 就自动分配
if (major < 0) {
printk(" can't register major number\n");
return major;
}
printk("register sucess /dev/led OK!\n");
led_class = class_create(THIS_MODULE, DEVICE_NAME);
if (IS_ERR(led_class))
return PTR_ERR(led_class);
led_class_devs = device_create(led_class, NULL, MKDEV(major, 0), NULL, "led");
if (unlikely(IS_ERR(led_class_devs)))
return PTR_ERR(led_class_devs);
printk(DEVICE_NAME " initialized\n");
return 0;
}
static void __exit led_exit(void)
{
unregister_chrdev(major, DEVICE_NAME);
device_unregister(led_class_devs);
class_destroy(led_class);
printk(" led rmmod OK!\n");
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
转载地址:https://blog.csdn.net/weixin_32946543/article/details/116909832 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!