int fd;
int data = 0;
fd = open("/dev/xxx_dev", O_RDWR); /* 阻塞方式打开 */
ret = read(fd, &data, sizeof(data)); /* 读取数据 */
若用户以非阻塞的方式访问设备文件,则当设备资源不可获取时,设备驱动的 xxx_read() 、 xxx_write () 等操作应立即返回,read() 、write() 等系统调用也随即被返回,应用程序收到-EAGAIN 返回值。
int fd;
int data = 0;
fd = open("/dev/xxx_dev", O_RDWR | O_NONBLOCK); /* 非阻塞方式打开 */
ret = read(fd, &data, sizeof(data)); /* 读取数据 */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//定义结构体表示我们的节点
struct device_node *test_device_node;
struct property *test_node_property;
//要申请的中断号
int irq;
int gpio_nu;
//用来模拟管脚的状态
int value = 0;
/**
* @description: 中断处理函数 test_key
* @param {int} irq :要申请的中断号
* @param {void} *args :
* @return {*}IRQ_HANDLED
*/
irqreturn_t test_key(int irq, void *args)
{
value = !value;
return IRQ_RETVAL(IRQ_HANDLED);
}
int misc_open(struct inode *node, struct file *file)
{
printk("hello misc_open \n");
return 0;
}
int misc_release(struct inode *node, struct file *file)
{
printk("hello misc_release bye bye\n");
return 0;
}
ssize_t misc_read(struct file *file, const char __user *ubuf, size_t size, loff_t *loff_t)
{
if (copy_to_user(ubuf, &value, sizeof(value)) != 0)
{
printk("copy_to_user error\n");
return -1;
}
return 0;
}
//文件操作集
struct file_operations misc_fops = {
.owner = THIS_MODULE,
.open = misc_open,
.release = misc_release,
.read = misc_read};
struct miscdevice misc_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "test_wq",
.fops = &misc_fops,
};
/**
* @brief beep_probe : 与设备信息层(设备树)匹配成功后自动执行此函数,
* @param inode : 文件索引
* @param file : 文件
* @return 成功返回 0
*/
int beep_probe(struct platform_device *pdev)
{
int ret = 0;
printk("beep_probe\n");
//of_find_node_by_path 函数通过路径查找节点,/test_key 是设备树下的节点路径
test_device_node = of_find_node_by_path("/test_key");
if (test_device_node == NULL)
{
printk("of_find_node_by_path is error\n");
return -1;
}
//of_get_named_gpio 函数获取 GPIO 编号
gpio_nu = of_get_named_gpio(test_device_node, "gpios", 0);
if (gpio_nu < 0)
{
printk("of_get_named_gpio is error\n");
return -1;
}
//设置 GPIO 为输入模式
gpio_direction_input(gpio_nu);
//获取 GPIO 对应的中断号
irq = irq_of_parse_and_map(test_device_node, 0);
printk("irq is %d \n", irq);
/*申请中断,irq:中断号名字
test_key:中断处理函数
IRQF_TRIGGER_RISING:中断标志,意为上升沿触发
"test_key":中断的名字
*/
ret = request_irq(irq, test_key, IRQF_TRIGGER_RISING, "test_key", NULL);
if (ret < 0)
{
printk("request_irq \n");
return -1;
}
//注册杂项设备
ret = misc_register(&misc_dev);
if (ret < 0)
{
printk("misc_register is error\n");
return -1;
}
printk("misc_register is successd \n");
}
int beep_remove(struct platform_device *pdev)
{
printk("beep_remove \n");
return 0;
}
const struct platform_device_id beep_idtable = {
.name = "beep_test",
};
const struct of_device_id of_match_table_test[] = {
{.compatible = "keys"},
{},
};
struct platform_driver beep_driver = {
//3. 在 beep_driver 结构体中完成了 beep_probe 和 beep_remove
.probe = beep_probe,
.remove = beep_remove,
.driver = {
.owner = THIS_MODULE,
.name = "beep_test",
.of_match_table = of_match_table_test
},
//4 .id_table 的优先级要比 driver.name 的优先级要高,优先与.id_table 进行匹配
.id_table = &beep_idtable
};
static int beep_driver_init(void)
{
//1.我们看驱动文件要从 init 函数开始看
int ret = 0;
//2. 在 init 函数里面注册了 platform_driver
ret = platform_driver_register(&beep_driver);
if (ret < 0)
{
printk("platform_driver_register error \n");
return ret;
}
printk("platform_driver_register ok \n");
return 0;
}
static void beep_driver_exit(void)
{
printk("gooodbye! \n");
free_irq(irq, NULL);
misc_deregister(&misc_dev);
platform_driver_unregister(&beep_driver);
}
module_init(beep_driver_init);
module_exit(beep_driver_exit);
MODULE_LICENSE("GPL");
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
int fd;
int value;
//打开设备节点
fd = open("/dev/test_wq",O_RDWR);
if(fd < 0)
{
//打开设备节点失败
perror("open error \n");
return fd;
}
while(1)
{
read(fd,&value,sizeof(value));
printf("value is %d \n",value);
}
close(fd);
return 0;
}